Linux Audio

Check our new training course

Loading...
v3.1
   1/* $Id: isdn_common.c,v 1.1.2.3 2004/02/10 01:07:13 keil Exp $
   2 *
   3 * Linux ISDN subsystem, common used functions (linklevel).
   4 *
   5 * Copyright 1994-1999  by Fritz Elfert (fritz@isdn4linux.de)
   6 * Copyright 1995,96    Thinking Objects Software GmbH Wuerzburg
   7 * Copyright 1995,96    by Michael Hipp (Michael.Hipp@student.uni-tuebingen.de)
   8 *
   9 * This software may be used and distributed according to the terms
  10 * of the GNU General Public License, incorporated herein by reference.
  11 *
  12 */
  13
  14#include <linux/module.h>
  15#include <linux/init.h>
  16#include <linux/poll.h>
  17#include <linux/slab.h>
  18#include <linux/vmalloc.h>
  19#include <linux/isdn.h>
  20#include <linux/mutex.h>
  21#include "isdn_common.h"
  22#include "isdn_tty.h"
  23#include "isdn_net.h"
  24#include "isdn_ppp.h"
  25#ifdef CONFIG_ISDN_AUDIO
  26#include "isdn_audio.h"
  27#endif
  28#ifdef CONFIG_ISDN_DIVERSION_MODULE
  29#define CONFIG_ISDN_DIVERSION
  30#endif
  31#ifdef CONFIG_ISDN_DIVERSION
  32#include <linux/isdn_divertif.h>
  33#endif /* CONFIG_ISDN_DIVERSION */
  34#include "isdn_v110.h"
  35
  36/* Debugflags */
  37#undef ISDN_DEBUG_STATCALLB
  38
  39MODULE_DESCRIPTION("ISDN4Linux: link layer");
  40MODULE_AUTHOR("Fritz Elfert");
  41MODULE_LICENSE("GPL");
  42
  43isdn_dev *dev;
  44
  45static DEFINE_MUTEX(isdn_mutex);
  46static char *isdn_revision = "$Revision: 1.1.2.3 $";
  47
  48extern char *isdn_net_revision;
  49extern char *isdn_tty_revision;
  50#ifdef CONFIG_ISDN_PPP
  51extern char *isdn_ppp_revision;
  52#else
  53static char *isdn_ppp_revision = ": none $";
  54#endif
  55#ifdef CONFIG_ISDN_AUDIO
  56extern char *isdn_audio_revision;
  57#else
  58static char *isdn_audio_revision = ": none $";
  59#endif
  60extern char *isdn_v110_revision;
  61
  62#ifdef CONFIG_ISDN_DIVERSION
  63static isdn_divert_if *divert_if; /* = NULL */
  64#endif /* CONFIG_ISDN_DIVERSION */
  65
  66
  67static int isdn_writebuf_stub(int, int, const u_char __user *, int);
  68static void set_global_features(void);
  69static int isdn_wildmat(char *s, char *p);
  70static int isdn_add_channels(isdn_driver_t *d, int drvidx, int n, int adding);
  71
  72static inline void
  73isdn_lock_driver(isdn_driver_t *drv)
  74{
  75	try_module_get(drv->interface->owner);
  76	drv->locks++;
  77}
  78
  79void
  80isdn_lock_drivers(void)
  81{
  82	int i;
  83
  84	for (i = 0; i < ISDN_MAX_DRIVERS; i++) {
  85		if (!dev->drv[i])
  86			continue;
  87		isdn_lock_driver(dev->drv[i]);
  88	}
  89}
  90
  91static inline void
  92isdn_unlock_driver(isdn_driver_t *drv)
  93{
  94	if (drv->locks > 0) {
  95		drv->locks--;
  96		module_put(drv->interface->owner);
  97	}
  98}
  99
 100void
 101isdn_unlock_drivers(void)
 102{
 103	int i;
 104
 105	for (i = 0; i < ISDN_MAX_DRIVERS; i++) {
 106		if (!dev->drv[i])
 107			continue;
 108		isdn_unlock_driver(dev->drv[i]);
 109	}
 110}
 111
 112#if defined(ISDN_DEBUG_NET_DUMP) || defined(ISDN_DEBUG_MODEM_DUMP)
 113void
 114isdn_dumppkt(char *s, u_char * p, int len, int dumplen)
 115{
 116	int dumpc;
 117
 118	printk(KERN_DEBUG "%s(%d) ", s, len);
 119	for (dumpc = 0; (dumpc < dumplen) && (len); len--, dumpc++)
 120		printk(" %02x", *p++);
 121	printk("\n");
 122}
 123#endif
 124
 125/*
 126 * I picked the pattern-matching-functions from an old GNU-tar version (1.10)
 127 * It was originally written and put to PD by rs@mirror.TMC.COM (Rich Salz)
 128 */
 129static int
 130isdn_star(char *s, char *p)
 131{
 132	while (isdn_wildmat(s, p)) {
 133		if (*++s == '\0')
 134			return (2);
 135	}
 136	return (0);
 137}
 138
 139/*
 140 * Shell-type Pattern-matching for incoming caller-Ids
 141 * This function gets a string in s and checks, if it matches the pattern
 142 * given in p.
 143 *
 144 * Return:
 145 *   0 = match.
 146 *   1 = no match.
 147 *   2 = no match. Would eventually match, if s would be longer.
 148 *
 149 * Possible Patterns:
 150 *
 151 * '?'     matches one character
 152 * '*'     matches zero or more characters
 153 * [xyz]   matches the set of characters in brackets.
 154 * [^xyz]  matches any single character not in the set of characters
 155 */
 156
 157static int
 158isdn_wildmat(char *s, char *p)
 159{
 160	register int last;
 161	register int matched;
 162	register int reverse;
 163	register int nostar = 1;
 164
 165	if (!(*s) && !(*p))
 166		return(1);
 167	for (; *p; s++, p++)
 168		switch (*p) {
 169			case '\\':
 170				/*
 171				 * Literal match with following character,
 172				 * fall through.
 173				 */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 174				p++;
 175			default:
 176				if (*s != *p)
 177					return (*s == '\0')?2:1;
 178				continue;
 179			case '?':
 180				/* Match anything. */
 181				if (*s == '\0')
 182					return (2);
 183				continue;
 184			case '*':
 185				nostar = 0;	
 186				/* Trailing star matches everything. */
 187				return (*++p ? isdn_star(s, p) : 0);
 188			case '[':
 189				/* [^....] means inverse character class. */
 190				if ((reverse = (p[1] == '^')))
 191					p++;
 192				for (last = 0, matched = 0; *++p && (*p != ']'); last = *p)
 193					/* This next line requires a good C compiler. */
 194					if (*p == '-' ? *s <= *++p && *s >= last : *s == *p)
 195						matched = 1;
 196				if (matched == reverse)
 197					return (1);
 198				continue;
 199		}
 200	return (*s == '\0')?0:nostar;
 201}
 202
 203int isdn_msncmp( const char * msn1, const char * msn2 )
 204{
 205	char TmpMsn1[ ISDN_MSNLEN ];
 206	char TmpMsn2[ ISDN_MSNLEN ];
 207	char *p;
 208
 209	for ( p = TmpMsn1; *msn1 && *msn1 != ':'; )  // Strip off a SPID
 210		*p++ = *msn1++;
 211	*p = '\0';
 212
 213	for ( p = TmpMsn2; *msn2 && *msn2 != ':'; )  // Strip off a SPID
 214		*p++ = *msn2++;
 215	*p = '\0';
 216
 217	return isdn_wildmat( TmpMsn1, TmpMsn2 );
 218}
 219
 220int
 221isdn_dc2minor(int di, int ch)
 222{
 223	int i;
 224	for (i = 0; i < ISDN_MAX_CHANNELS; i++)
 225		if (dev->chanmap[i] == ch && dev->drvmap[i] == di)
 226			return i;
 227	return -1;
 228}
 229
 230static int isdn_timer_cnt1 = 0;
 231static int isdn_timer_cnt2 = 0;
 232static int isdn_timer_cnt3 = 0;
 233
 234static void
 235isdn_timer_funct(ulong dummy)
 236{
 237	int tf = dev->tflags;
 238	if (tf & ISDN_TIMER_FAST) {
 239		if (tf & ISDN_TIMER_MODEMREAD)
 240			isdn_tty_readmodem();
 241		if (tf & ISDN_TIMER_MODEMPLUS)
 242			isdn_tty_modem_escape();
 243		if (tf & ISDN_TIMER_MODEMXMIT)
 244			isdn_tty_modem_xmit();
 245	}
 246	if (tf & ISDN_TIMER_SLOW) {
 247		if (++isdn_timer_cnt1 >= ISDN_TIMER_02SEC) {
 248			isdn_timer_cnt1 = 0;
 249			if (tf & ISDN_TIMER_NETDIAL)
 250				isdn_net_dial();
 251		}
 252		if (++isdn_timer_cnt2 >= ISDN_TIMER_1SEC) {
 253			isdn_timer_cnt2 = 0;
 254			if (tf & ISDN_TIMER_NETHANGUP)
 255				isdn_net_autohup();
 256			if (++isdn_timer_cnt3 >= ISDN_TIMER_RINGING) {
 257				isdn_timer_cnt3 = 0;
 258				if (tf & ISDN_TIMER_MODEMRING)
 259					isdn_tty_modem_ring();
 260			}
 261			if (tf & ISDN_TIMER_CARRIER)
 262				isdn_tty_carrier_timeout();
 263		}
 264	}
 265	if (tf) 
 266		mod_timer(&dev->timer, jiffies+ISDN_TIMER_RES);
 267}
 268
 269void
 270isdn_timer_ctrl(int tf, int onoff)
 271{
 272	unsigned long flags;
 273	int old_tflags;
 274
 275	spin_lock_irqsave(&dev->timerlock, flags);
 276	if ((tf & ISDN_TIMER_SLOW) && (!(dev->tflags & ISDN_TIMER_SLOW))) {
 277		/* If the slow-timer wasn't activated until now */
 278		isdn_timer_cnt1 = 0;
 279		isdn_timer_cnt2 = 0;
 280	}
 281	old_tflags = dev->tflags;
 282	if (onoff)
 283		dev->tflags |= tf;
 284	else
 285		dev->tflags &= ~tf;
 286	if (dev->tflags && !old_tflags)
 287		mod_timer(&dev->timer, jiffies+ISDN_TIMER_RES);
 288	spin_unlock_irqrestore(&dev->timerlock, flags);
 289}
 290
 291/*
 292 * Receive a packet from B-Channel. (Called from low-level-module)
 293 */
 294static void
 295isdn_receive_skb_callback(int di, int channel, struct sk_buff *skb)
 296{
 297	int i;
 298
 299	if ((i = isdn_dc2minor(di, channel)) == -1) {
 300		dev_kfree_skb(skb);
 301		return;
 302	}
 303	/* Update statistics */
 304	dev->ibytes[i] += skb->len;
 305	
 306	/* First, try to deliver data to network-device */
 307	if (isdn_net_rcv_skb(i, skb))
 308		return;
 309
 310	/* V.110 handling
 311	 * makes sense for async streams only, so it is
 312	 * called after possible net-device delivery.
 313	 */
 314	if (dev->v110[i]) {
 315		atomic_inc(&dev->v110use[i]);
 316		skb = isdn_v110_decode(dev->v110[i], skb);
 317		atomic_dec(&dev->v110use[i]);
 318		if (!skb)
 319			return;
 320	}
 321
 322	/* No network-device found, deliver to tty or raw-channel */
 323	if (skb->len) {
 324		if (isdn_tty_rcv_skb(i, di, channel, skb))
 325			return;
 326		wake_up_interruptible(&dev->drv[di]->rcv_waitq[channel]);
 327	} else
 328		dev_kfree_skb(skb);
 329}
 330
 331/*
 332 * Intercept command from Linklevel to Lowlevel.
 333 * If layer 2 protocol is V.110 and this is not supported by current
 334 * lowlevel-driver, use driver's transparent mode and handle V.110 in
 335 * linklevel instead.
 336 */
 337int
 338isdn_command(isdn_ctrl *cmd)
 339{
 340	if (cmd->driver == -1) {
 341		printk(KERN_WARNING "isdn_command command(%x) driver -1\n", cmd->command);
 342		return(1);
 343	}
 344	if (!dev->drv[cmd->driver]) {
 345		printk(KERN_WARNING "isdn_command command(%x) dev->drv[%d] NULL\n",
 346			cmd->command, cmd->driver);
 347		return(1);
 348	}
 349	if (!dev->drv[cmd->driver]->interface) {
 350		printk(KERN_WARNING "isdn_command command(%x) dev->drv[%d]->interface NULL\n",
 351			cmd->command, cmd->driver);
 352		return(1);
 353	}
 354	if (cmd->command == ISDN_CMD_SETL2) {
 355		int idx = isdn_dc2minor(cmd->driver, cmd->arg & 255);
 356		unsigned long l2prot = (cmd->arg >> 8) & 255;
 357		unsigned long features = (dev->drv[cmd->driver]->interface->features
 358						>> ISDN_FEATURE_L2_SHIFT) &
 359						ISDN_FEATURE_L2_MASK;
 360		unsigned long l2_feature = (1 << l2prot);
 361
 362		switch (l2prot) {
 363			case ISDN_PROTO_L2_V11096:
 364			case ISDN_PROTO_L2_V11019:
 365			case ISDN_PROTO_L2_V11038:
 366			/* If V.110 requested, but not supported by
 367			 * HL-driver, set emulator-flag and change
 368			 * Layer-2 to transparent
 369			 */
 370				if (!(features & l2_feature)) {
 371					dev->v110emu[idx] = l2prot;
 372					cmd->arg = (cmd->arg & 255) |
 373						(ISDN_PROTO_L2_TRANS << 8);
 374				} else
 375					dev->v110emu[idx] = 0;
 376		}
 377	}
 378	return dev->drv[cmd->driver]->interface->command(cmd);
 379}
 380
 381void
 382isdn_all_eaz(int di, int ch)
 383{
 384	isdn_ctrl cmd;
 385
 386	if (di < 0)
 387		return;
 388	cmd.driver = di;
 389	cmd.arg = ch;
 390	cmd.command = ISDN_CMD_SETEAZ;
 391	cmd.parm.num[0] = '\0';
 392	isdn_command(&cmd);
 393}
 394
 395/*
 396 * Begin of a CAPI like LL<->HL interface, currently used only for 
 397 * supplementary service (CAPI 2.0 part III)
 398 */
 399#include <linux/isdn/capicmd.h>
 400
 401static int
 402isdn_capi_rec_hl_msg(capi_msg *cm)
 403{
 404	switch(cm->Command) {
 405		case CAPI_FACILITY:
 406			/* in the moment only handled in tty */
 407			return(isdn_tty_capi_facility(cm));
 408		default:
 409			return(-1);
 410	}
 411}
 412
 413static int
 414isdn_status_callback(isdn_ctrl * c)
 415{
 416	int di;
 417	u_long flags;
 418	int i;
 419	int r;
 420	int retval = 0;
 421	isdn_ctrl cmd;
 422	isdn_net_dev *p;
 423
 424	di = c->driver;
 425	i = isdn_dc2minor(di, c->arg);
 426	switch (c->command) {
 427		case ISDN_STAT_BSENT:
 428			if (i < 0)
 429				return -1;
 430			if (dev->global_flags & ISDN_GLOBAL_STOPPED)
 431				return 0;
 432			if (isdn_net_stat_callback(i, c))
 433				return 0;
 434			if (isdn_v110_stat_callback(i, c))
 435				return 0;
 436			if (isdn_tty_stat_callback(i, c))
 437				return 0;
 438			wake_up_interruptible(&dev->drv[di]->snd_waitq[c->arg]);
 439			break;
 440		case ISDN_STAT_STAVAIL:
 441			dev->drv[di]->stavail += c->arg;
 442			wake_up_interruptible(&dev->drv[di]->st_waitq);
 443			break;
 444		case ISDN_STAT_RUN:
 445			dev->drv[di]->flags |= DRV_FLAG_RUNNING;
 446			for (i = 0; i < ISDN_MAX_CHANNELS; i++)
 447				if (dev->drvmap[i] == di)
 448					isdn_all_eaz(di, dev->chanmap[i]);
 449			set_global_features();
 450			break;
 451		case ISDN_STAT_STOP:
 452			dev->drv[di]->flags &= ~DRV_FLAG_RUNNING;
 453			break;
 454		case ISDN_STAT_ICALL:
 455			if (i < 0)
 456				return -1;
 457#ifdef ISDN_DEBUG_STATCALLB
 458			printk(KERN_DEBUG "ICALL (net): %d %ld %s\n", di, c->arg, c->parm.num);
 459#endif
 460			if (dev->global_flags & ISDN_GLOBAL_STOPPED) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 461				cmd.driver = di;
 462				cmd.arg = c->arg;
 463				cmd.command = ISDN_CMD_HANGUP;
 464				isdn_command(&cmd);
 465				return 0;
 466			}
 467			/* Try to find a network-interface which will accept incoming call */
 468			r = ((c->command == ISDN_STAT_ICALLW) ? 0 : isdn_net_find_icall(di, c->arg, i, &c->parm.setup));
 469			switch (r) {
 470				case 0:
 471					/* No network-device replies.
 472					 * Try ttyI's.
 473					 * These return 0 on no match, 1 on match and
 474					 * 3 on eventually match, if CID is longer.
 475					 */
 476                                        if (c->command == ISDN_STAT_ICALL)
 477					  if ((retval = isdn_tty_find_icall(di, c->arg, &c->parm.setup))) return(retval);
 478#ifdef CONFIG_ISDN_DIVERSION 
 479                                         if (divert_if)
 480                 	                  if ((retval = divert_if->stat_callback(c))) 
 481					    return(retval); /* processed */
 482#endif /* CONFIG_ISDN_DIVERSION */                       
 483					if ((!retval) && (dev->drv[di]->flags & DRV_FLAG_REJBUS)) {
 484						/* No tty responding */
 485						cmd.driver = di;
 486						cmd.arg = c->arg;
 487						cmd.command = ISDN_CMD_HANGUP;
 488						isdn_command(&cmd);
 489						retval = 2;
 490					}
 491					break;
 492				case 1:
 493					/* Schedule connection-setup */
 494					isdn_net_dial();
 495					cmd.driver = di;
 496					cmd.arg = c->arg;
 497					cmd.command = ISDN_CMD_ACCEPTD;
 498					for ( p = dev->netdev; p; p = p->next )
 499						if ( p->local->isdn_channel == cmd.arg )
 500						{
 501							strcpy( cmd.parm.setup.eazmsn, p->local->msn );
 502							isdn_command(&cmd);
 503							retval = 1;
 504							break;
 505						}
 506					break;
 507
 508				case 2:	/* For calling back, first reject incoming call ... */
 509				case 3:	/* Interface found, but down, reject call actively  */
 510					retval = 2;
 511					printk(KERN_INFO "isdn: Rejecting Call\n");
 512					cmd.driver = di;
 513					cmd.arg = c->arg;
 514					cmd.command = ISDN_CMD_HANGUP;
 515					isdn_command(&cmd);
 516					if (r == 3)
 517						break;
 518					/* Fall through */
 519				case 4:
 520					/* ... then start callback. */
 521					isdn_net_dial();
 522					break;
 523				case 5:
 524					/* Number would eventually match, if longer */
 525					retval = 3;
 526					break;
 527			}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 528#ifdef ISDN_DEBUG_STATCALLB
 529			printk(KERN_DEBUG "ICALL: ret=%d\n", retval);
 530#endif
 531			return retval;
 532			break;
 533		case ISDN_STAT_CINF:
 534			if (i < 0)
 535				return -1;
 536#ifdef ISDN_DEBUG_STATCALLB
 537			printk(KERN_DEBUG "CINF: %ld %s\n", c->arg, c->parm.num);
 538#endif
 539			if (dev->global_flags & ISDN_GLOBAL_STOPPED)
 540				return 0;
 541			if (strcmp(c->parm.num, "0"))
 542				isdn_net_stat_callback(i, c);
 543			isdn_tty_stat_callback(i, c);
 544			break;
 545		case ISDN_STAT_CAUSE:
 546#ifdef ISDN_DEBUG_STATCALLB
 547			printk(KERN_DEBUG "CAUSE: %ld %s\n", c->arg, c->parm.num);
 548#endif
 549			printk(KERN_INFO "isdn: %s,ch%ld cause: %s\n",
 550			       dev->drvid[di], c->arg, c->parm.num);
 551			isdn_tty_stat_callback(i, c);
 552#ifdef CONFIG_ISDN_DIVERSION
 553                        if (divert_if)
 554                         divert_if->stat_callback(c); 
 555#endif /* CONFIG_ISDN_DIVERSION */
 556			break;
 557		case ISDN_STAT_DISPLAY:
 558#ifdef ISDN_DEBUG_STATCALLB
 559			printk(KERN_DEBUG "DISPLAY: %ld %s\n", c->arg, c->parm.display);
 560#endif
 561			isdn_tty_stat_callback(i, c);
 562#ifdef CONFIG_ISDN_DIVERSION
 563                        if (divert_if)
 564                         divert_if->stat_callback(c); 
 565#endif /* CONFIG_ISDN_DIVERSION */
 566			break;
 567		case ISDN_STAT_DCONN:
 568			if (i < 0)
 569				return -1;
 570#ifdef ISDN_DEBUG_STATCALLB
 571			printk(KERN_DEBUG "DCONN: %ld\n", c->arg);
 572#endif
 573			if (dev->global_flags & ISDN_GLOBAL_STOPPED)
 574				return 0;
 575			/* Find any net-device, waiting for D-channel setup */
 576			if (isdn_net_stat_callback(i, c))
 577				break;
 578			isdn_v110_stat_callback(i, c);
 579			/* Find any ttyI, waiting for D-channel setup */
 580			if (isdn_tty_stat_callback(i, c)) {
 581				cmd.driver = di;
 582				cmd.arg = c->arg;
 583				cmd.command = ISDN_CMD_ACCEPTB;
 584				isdn_command(&cmd);
 585				break;
 586			}
 587			break;
 588		case ISDN_STAT_DHUP:
 589			if (i < 0)
 590				return -1;
 
 
 591#ifdef ISDN_DEBUG_STATCALLB
 592			printk(KERN_DEBUG "DHUP: %ld\n", c->arg);
 593#endif
 594			if (dev->global_flags & ISDN_GLOBAL_STOPPED)
 595				return 0;
 596			dev->drv[di]->online &= ~(1 << (c->arg));
 597			isdn_info_update();
 598			/* Signal hangup to network-devices */
 599			if (isdn_net_stat_callback(i, c))
 600				break;
 601			isdn_v110_stat_callback(i, c);
 602			if (isdn_tty_stat_callback(i, c))
 603				break;
 604#ifdef CONFIG_ISDN_DIVERSION
 605                        if (divert_if)
 606                         divert_if->stat_callback(c); 
 607#endif /* CONFIG_ISDN_DIVERSION */
 608			break;
 
 
 609			break;
 610		case ISDN_STAT_BCONN:
 611			if (i < 0)
 612				return -1;
 
 
 
 
 
 
 613#ifdef ISDN_DEBUG_STATCALLB
 614			printk(KERN_DEBUG "BCONN: %ld\n", c->arg);
 615#endif
 616			/* Signal B-channel-connect to network-devices */
 617			if (dev->global_flags & ISDN_GLOBAL_STOPPED)
 618				return 0;
 619			dev->drv[di]->online |= (1 << (c->arg));
 620			isdn_info_update();
 621			if (isdn_net_stat_callback(i, c))
 622				break;
 623			isdn_v110_stat_callback(i, c);
 624			if (isdn_tty_stat_callback(i, c))
 625				break;
 626			break;
 627		case ISDN_STAT_BHUP:
 628			if (i < 0)
 629				return -1;
 630#ifdef ISDN_DEBUG_STATCALLB
 631			printk(KERN_DEBUG "BHUP: %ld\n", c->arg);
 632#endif
 633			if (dev->global_flags & ISDN_GLOBAL_STOPPED)
 634				return 0;
 635			dev->drv[di]->online &= ~(1 << (c->arg));
 636			isdn_info_update();
 637#ifdef CONFIG_ISDN_X25
 638			/* Signal hangup to network-devices */
 639			if (isdn_net_stat_callback(i, c))
 640				break;
 641#endif
 642			isdn_v110_stat_callback(i, c);
 643			if (isdn_tty_stat_callback(i, c))
 644				break;
 645			break;
 646		case ISDN_STAT_NODCH:
 647			if (i < 0)
 648				return -1;
 
 649#ifdef ISDN_DEBUG_STATCALLB
 650			printk(KERN_DEBUG "NODCH: %ld\n", c->arg);
 651#endif
 652			if (dev->global_flags & ISDN_GLOBAL_STOPPED)
 653				return 0;
 654			if (isdn_net_stat_callback(i, c))
 655				break;
 656			if (isdn_tty_stat_callback(i, c))
 657				break;
 658			break;
 659		case ISDN_STAT_ADDCH:
 660			spin_lock_irqsave(&dev->lock, flags);
 661			if (isdn_add_channels(dev->drv[di], di, c->arg, 1)) {
 662				spin_unlock_irqrestore(&dev->lock, flags);
 663				return -1;
 664			}
 665			spin_unlock_irqrestore(&dev->lock, flags);
 666			isdn_info_update();
 667			break;
 668		case ISDN_STAT_DISCH:
 669			spin_lock_irqsave(&dev->lock, flags);
 670			for (i = 0; i < ISDN_MAX_CHANNELS; i++)
 671				if ((dev->drvmap[i] == di) &&
 672				    (dev->chanmap[i] == c->arg)) {
 673				    if (c->parm.num[0])
 674				      dev->usage[i] &= ~ISDN_USAGE_DISABLED;
 675				    else
 676				      if (USG_NONE(dev->usage[i])) {
 677					dev->usage[i] |= ISDN_USAGE_DISABLED;
 678				      }
 679				      else 
 680					retval = -1;
 681				    break;
 682				}
 683			spin_unlock_irqrestore(&dev->lock, flags);
 684			isdn_info_update();
 685			break;
 686		case ISDN_STAT_UNLOAD:
 687			while (dev->drv[di]->locks > 0) {
 688				isdn_unlock_driver(dev->drv[di]);
 689			}
 690			spin_lock_irqsave(&dev->lock, flags);
 691			isdn_tty_stat_callback(i, c);
 692			for (i = 0; i < ISDN_MAX_CHANNELS; i++)
 693				if (dev->drvmap[i] == di) {
 694					dev->drvmap[i] = -1;
 695					dev->chanmap[i] = -1;
 696					dev->usage[i] &= ~ISDN_USAGE_DISABLED;
 697				}
 698			dev->drivers--;
 699			dev->channels -= dev->drv[di]->channels;
 700			kfree(dev->drv[di]->rcverr);
 701			kfree(dev->drv[di]->rcvcount);
 702			for (i = 0; i < dev->drv[di]->channels; i++)
 703				skb_queue_purge(&dev->drv[di]->rpqueue[i]);
 704			kfree(dev->drv[di]->rpqueue);
 705			kfree(dev->drv[di]->rcv_waitq);
 706			kfree(dev->drv[di]);
 707			dev->drv[di] = NULL;
 708			dev->drvid[di][0] = '\0';
 709			isdn_info_update();
 710			set_global_features();
 711			spin_unlock_irqrestore(&dev->lock, flags);
 712			return 0;
 713		case ISDN_STAT_L1ERR:
 714			break;
 715		case CAPI_PUT_MESSAGE:
 716			return(isdn_capi_rec_hl_msg(&c->parm.cmsg));
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 717#ifdef CONFIG_ISDN_TTY_FAX
 718		case ISDN_STAT_FAXIND:
 719			isdn_tty_stat_callback(i, c);
 720			break;
 721#endif
 722#ifdef CONFIG_ISDN_AUDIO
 723		case ISDN_STAT_AUDIO:
 724			isdn_tty_stat_callback(i, c);
 725			break;
 726#endif
 727#ifdef CONFIG_ISDN_DIVERSION
 728	        case ISDN_STAT_PROT:
 729	        case ISDN_STAT_REDIR:
 730                        if (divert_if)
 731                          return(divert_if->stat_callback(c));
 732#endif /* CONFIG_ISDN_DIVERSION */
 733		default:
 734			return -1;
 735	}
 736	return 0;
 737}
 738
 739/*
 740 * Get integer from char-pointer, set pointer to end of number
 741 */
 742int
 743isdn_getnum(char **p)
 744{
 745	int v = -1;
 746
 747	while (*p[0] >= '0' && *p[0] <= '9')
 748		v = ((v < 0) ? 0 : (v * 10)) + (int) ((*p[0]++) - '0');
 749	return v;
 750}
 751
 752#define DLE 0x10
 753
 754/*
 755 * isdn_readbchan() tries to get data from the read-queue.
 756 * It MUST be called with interrupts off.
 757 *
 758 * Be aware that this is not an atomic operation when sleep != 0, even though 
 759 * interrupts are turned off! Well, like that we are currently only called
 760 * on behalf of a read system call on raw device files (which are documented
 761 * to be dangerous and for debugging purpose only). The inode semaphore
 762 * takes care that this is not called for the same minor device number while
 763 * we are sleeping, but access is not serialized against simultaneous read()
 764 * from the corresponding ttyI device. Can other ugly events, like changes
 765 * of the mapping (di,ch)<->minor, happen during the sleep? --he 
 766 */
 767int
 768isdn_readbchan(int di, int channel, u_char * buf, u_char * fp, int len, wait_queue_head_t *sleep)
 769{
 770	int count;
 771	int count_pull;
 772	int count_put;
 773	int dflag;
 774	struct sk_buff *skb;
 775	u_char *cp;
 776
 777	if (!dev->drv[di])
 778		return 0;
 779	if (skb_queue_empty(&dev->drv[di]->rpqueue[channel])) {
 780		if (sleep)
 781			interruptible_sleep_on(sleep);
 
 782		else
 783			return 0;
 784	}
 785	if (len > dev->drv[di]->rcvcount[channel])
 786		len = dev->drv[di]->rcvcount[channel];
 787	cp = buf;
 788	count = 0;
 789	while (len) {
 790		if (!(skb = skb_peek(&dev->drv[di]->rpqueue[channel])))
 791			break;
 792#ifdef CONFIG_ISDN_AUDIO
 793		if (ISDN_AUDIO_SKB_LOCK(skb))
 794			break;
 795		ISDN_AUDIO_SKB_LOCK(skb) = 1;
 796		if ((ISDN_AUDIO_SKB_DLECOUNT(skb)) || (dev->drv[di]->DLEflag & (1 << channel))) {
 797			char *p = skb->data;
 798			unsigned long DLEmask = (1 << channel);
 799
 800			dflag = 0;
 801			count_pull = count_put = 0;
 802			while ((count_pull < skb->len) && (len > 0)) {
 803				len--;
 804				if (dev->drv[di]->DLEflag & DLEmask) {
 805					*cp++ = DLE;
 806					dev->drv[di]->DLEflag &= ~DLEmask;
 807				} else {
 808					*cp++ = *p;
 809					if (*p == DLE) {
 810						dev->drv[di]->DLEflag |= DLEmask;
 811						(ISDN_AUDIO_SKB_DLECOUNT(skb))--;
 812					}
 813					p++;
 814					count_pull++;
 815				}
 816				count_put++;
 817			}
 818			if (count_pull >= skb->len)
 819				dflag = 1;
 820		} else {
 821#endif
 822			/* No DLE's in buff, so simply copy it */
 823			dflag = 1;
 824			if ((count_pull = skb->len) > len) {
 825				count_pull = len;
 826				dflag = 0;
 827			}
 828			count_put = count_pull;
 829			skb_copy_from_linear_data(skb, cp, count_put);
 830			cp += count_put;
 831			len -= count_put;
 832#ifdef CONFIG_ISDN_AUDIO
 833		}
 834#endif
 835		count += count_put;
 836		if (fp) {
 837			memset(fp, 0, count_put);
 838			fp += count_put;
 839		}
 840		if (dflag) {
 841			/* We got all the data in this buff.
 842			 * Now we can dequeue it.
 843			 */
 844			if (fp)
 845				*(fp - 1) = 0xff;
 846#ifdef CONFIG_ISDN_AUDIO
 847			ISDN_AUDIO_SKB_LOCK(skb) = 0;
 848#endif
 849			skb = skb_dequeue(&dev->drv[di]->rpqueue[channel]);
 850			dev_kfree_skb(skb);
 851		} else {
 852			/* Not yet emptied this buff, so it
 853			 * must stay in the queue, for further calls
 854			 * but we pull off the data we got until now.
 855			 */
 856			skb_pull(skb, count_pull);
 857#ifdef CONFIG_ISDN_AUDIO
 858			ISDN_AUDIO_SKB_LOCK(skb) = 0;
 859#endif
 860		}
 861		dev->drv[di]->rcvcount[channel] -= count_put;
 862	}
 863	return count;
 864}
 865
 866/*
 867 * isdn_readbchan_tty() tries to get data from the read-queue.
 868 * It MUST be called with interrupts off.
 869 *
 870 * Be aware that this is not an atomic operation when sleep != 0, even though
 871 * interrupts are turned off! Well, like that we are currently only called
 872 * on behalf of a read system call on raw device files (which are documented
 873 * to be dangerous and for debugging purpose only). The inode semaphore
 874 * takes care that this is not called for the same minor device number while
 875 * we are sleeping, but access is not serialized against simultaneous read()
 876 * from the corresponding ttyI device. Can other ugly events, like changes
 877 * of the mapping (di,ch)<->minor, happen during the sleep? --he
 878 */
 879int
 880isdn_readbchan_tty(int di, int channel, struct tty_struct *tty, int cisco_hack)
 881{
 882	int count;
 883	int count_pull;
 884	int count_put;
 885	int dflag;
 886	struct sk_buff *skb;
 887	char last = 0;
 888	int len;
 889
 890	if (!dev->drv[di])
 891		return 0;
 892	if (skb_queue_empty(&dev->drv[di]->rpqueue[channel]))
 893			return 0;
 894
 895	len = tty_buffer_request_room(tty, dev->drv[di]->rcvcount[channel]);
 896	if(len == 0)
 897		return len;
 898
 899	count = 0;
 900	while (len) {
 901		if (!(skb = skb_peek(&dev->drv[di]->rpqueue[channel])))
 902			break;
 903#ifdef CONFIG_ISDN_AUDIO
 904		if (ISDN_AUDIO_SKB_LOCK(skb))
 905			break;
 906		ISDN_AUDIO_SKB_LOCK(skb) = 1;
 907		if ((ISDN_AUDIO_SKB_DLECOUNT(skb)) || (dev->drv[di]->DLEflag & (1 << channel))) {
 908			char *p = skb->data;
 909			unsigned long DLEmask = (1 << channel);
 910
 911			dflag = 0;
 912			count_pull = count_put = 0;
 913			while ((count_pull < skb->len) && (len > 0)) {
 914				/* push every character but the last to the tty buffer directly */
 915				if ( count_put )
 916					tty_insert_flip_char(tty, last, TTY_NORMAL);
 917				len--;
 918				if (dev->drv[di]->DLEflag & DLEmask) {
 919					last = DLE;
 920					dev->drv[di]->DLEflag &= ~DLEmask;
 921				} else {
 922					last = *p;
 923					if (last == DLE) {
 924						dev->drv[di]->DLEflag |= DLEmask;
 925						(ISDN_AUDIO_SKB_DLECOUNT(skb))--;
 926					}
 927					p++;
 928					count_pull++;
 929				}
 930				count_put++;
 931			}
 932			if (count_pull >= skb->len)
 933				dflag = 1;
 934		} else {
 935#endif
 936			/* No DLE's in buff, so simply copy it */
 937			dflag = 1;
 938			if ((count_pull = skb->len) > len) {
 939				count_pull = len;
 940				dflag = 0;
 941			}
 942			count_put = count_pull;
 943			if(count_put > 1)
 944				tty_insert_flip_string(tty, skb->data, count_put - 1);
 945			last = skb->data[count_put - 1];
 946			len -= count_put;
 947#ifdef CONFIG_ISDN_AUDIO
 948		}
 949#endif
 950		count += count_put;
 951		if (dflag) {
 952			/* We got all the data in this buff.
 953			 * Now we can dequeue it.
 954			 */
 955			if(cisco_hack)
 956				tty_insert_flip_char(tty, last, 0xFF);
 957			else
 958				tty_insert_flip_char(tty, last, TTY_NORMAL);
 959#ifdef CONFIG_ISDN_AUDIO
 960			ISDN_AUDIO_SKB_LOCK(skb) = 0;
 961#endif
 962			skb = skb_dequeue(&dev->drv[di]->rpqueue[channel]);
 963			dev_kfree_skb(skb);
 964		} else {
 965			tty_insert_flip_char(tty, last, TTY_NORMAL);
 966			/* Not yet emptied this buff, so it
 967			 * must stay in the queue, for further calls
 968			 * but we pull off the data we got until now.
 969			 */
 970			skb_pull(skb, count_pull);
 971#ifdef CONFIG_ISDN_AUDIO
 972			ISDN_AUDIO_SKB_LOCK(skb) = 0;
 973#endif
 974		}
 975		dev->drv[di]->rcvcount[channel] -= count_put;
 976	}
 977	return count;
 978}
 979
 980
 981static inline int
 982isdn_minor2drv(int minor)
 983{
 984	return (dev->drvmap[minor]);
 985}
 986
 987static inline int
 988isdn_minor2chan(int minor)
 989{
 990	return (dev->chanmap[minor]);
 991}
 992
 993static char *
 994isdn_statstr(void)
 995{
 996	static char istatbuf[2048];
 997	char *p;
 998	int i;
 999
1000	sprintf(istatbuf, "idmap:\t");
1001	p = istatbuf + strlen(istatbuf);
1002	for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
1003		sprintf(p, "%s ", (dev->drvmap[i] < 0) ? "-" : dev->drvid[dev->drvmap[i]]);
1004		p = istatbuf + strlen(istatbuf);
1005	}
1006	sprintf(p, "\nchmap:\t");
1007	p = istatbuf + strlen(istatbuf);
1008	for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
1009		sprintf(p, "%d ", dev->chanmap[i]);
1010		p = istatbuf + strlen(istatbuf);
1011	}
1012	sprintf(p, "\ndrmap:\t");
1013	p = istatbuf + strlen(istatbuf);
1014	for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
1015		sprintf(p, "%d ", dev->drvmap[i]);
1016		p = istatbuf + strlen(istatbuf);
1017	}
1018	sprintf(p, "\nusage:\t");
1019	p = istatbuf + strlen(istatbuf);
1020	for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
1021		sprintf(p, "%d ", dev->usage[i]);
1022		p = istatbuf + strlen(istatbuf);
1023	}
1024	sprintf(p, "\nflags:\t");
1025	p = istatbuf + strlen(istatbuf);
1026	for (i = 0; i < ISDN_MAX_DRIVERS; i++) {
1027		if (dev->drv[i]) {
1028			sprintf(p, "%ld ", dev->drv[i]->online);
1029			p = istatbuf + strlen(istatbuf);
1030		} else {
1031			sprintf(p, "? ");
1032			p = istatbuf + strlen(istatbuf);
1033		}
1034	}
1035	sprintf(p, "\nphone:\t");
1036	p = istatbuf + strlen(istatbuf);
1037	for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
1038		sprintf(p, "%s ", dev->num[i]);
1039		p = istatbuf + strlen(istatbuf);
1040	}
1041	sprintf(p, "\n");
1042	return istatbuf;
1043}
1044
1045/* Module interface-code */
1046
1047void
1048isdn_info_update(void)
1049{
1050	infostruct *p = dev->infochain;
1051
1052	while (p) {
1053		*(p->private) = 1;
1054		p = (infostruct *) p->next;
1055	}
1056	wake_up_interruptible(&(dev->info_waitq));
1057}
1058
1059static ssize_t
1060isdn_read(struct file *file, char __user *buf, size_t count, loff_t * off)
1061{
1062	uint minor = iminor(file->f_path.dentry->d_inode);
1063	int len = 0;
1064	int drvidx;
1065	int chidx;
1066	int retval;
1067	char *p;
1068
1069	mutex_lock(&isdn_mutex);
1070	if (minor == ISDN_MINOR_STATUS) {
1071		if (!file->private_data) {
1072			if (file->f_flags & O_NONBLOCK) {
1073				retval = -EAGAIN;
1074				goto out;
1075			}
1076			interruptible_sleep_on(&(dev->info_waitq));
 
1077		}
1078		p = isdn_statstr();
1079		file->private_data = NULL;
1080		if ((len = strlen(p)) <= count) {
1081			if (copy_to_user(buf, p, len)) {
1082				retval = -EFAULT;
1083				goto out;
1084			}
1085			*off += len;
1086			retval = len;
1087			goto out;
1088		}
1089		retval = 0;
1090		goto out;
1091	}
1092	if (!dev->drivers) {
1093		retval = -ENODEV;
1094		goto out;
1095	}
1096	if (minor <= ISDN_MINOR_BMAX) {
1097		printk(KERN_WARNING "isdn_read minor %d obsolete!\n", minor);
1098		drvidx = isdn_minor2drv(minor);
1099		if (drvidx < 0) {
1100			retval = -ENODEV;
1101			goto out;
1102		}
1103		if (!(dev->drv[drvidx]->flags & DRV_FLAG_RUNNING)) {
1104			retval = -ENODEV;
1105			goto out;
1106		}
1107		chidx = isdn_minor2chan(minor);
1108		if (!(p = kmalloc(count, GFP_KERNEL))) {
1109			retval = -ENOMEM;
1110			goto out;
1111		}
1112		len = isdn_readbchan(drvidx, chidx, p, NULL, count,
1113				     &dev->drv[drvidx]->rcv_waitq[chidx]);
1114		*off += len;
1115		if (copy_to_user(buf,p,len)) 
1116			len = -EFAULT;
1117		kfree(p);
1118		retval = len;
1119		goto out;
1120	}
1121	if (minor <= ISDN_MINOR_CTRLMAX) {
1122		drvidx = isdn_minor2drv(minor - ISDN_MINOR_CTRL);
1123		if (drvidx < 0) {
1124			retval = -ENODEV;
1125			goto out;
1126		}
1127		if (!dev->drv[drvidx]->stavail) {
1128			if (file->f_flags & O_NONBLOCK) {
1129				retval = -EAGAIN;
1130				goto out;
1131			}
1132			interruptible_sleep_on(&(dev->drv[drvidx]->st_waitq));
 
1133		}
1134		if (dev->drv[drvidx]->interface->readstat) {
1135			if (count > dev->drv[drvidx]->stavail)
1136				count = dev->drv[drvidx]->stavail;
1137			len = dev->drv[drvidx]->interface->readstat(buf, count,
1138				drvidx, isdn_minor2chan(minor - ISDN_MINOR_CTRL));
1139			if (len < 0) {
1140				retval = len;
1141				goto out;
1142			}
1143		} else {
1144			len = 0;
1145		}
1146		if (len)
1147			dev->drv[drvidx]->stavail -= len;
1148		else
1149			dev->drv[drvidx]->stavail = 0;
1150		*off += len;
1151		retval = len;
1152		goto out;
1153	}
1154#ifdef CONFIG_ISDN_PPP
1155	if (minor <= ISDN_MINOR_PPPMAX) {
1156		retval = isdn_ppp_read(minor - ISDN_MINOR_PPP, file, buf, count);
1157		goto out;
1158	}
1159#endif
1160	retval = -ENODEV;
1161 out:
1162	mutex_unlock(&isdn_mutex);
1163	return retval;
1164}
1165
1166static ssize_t
1167isdn_write(struct file *file, const char __user *buf, size_t count, loff_t * off)
1168{
1169	uint minor = iminor(file->f_path.dentry->d_inode);
1170	int drvidx;
1171	int chidx;
1172	int retval;
1173
1174	if (minor == ISDN_MINOR_STATUS)
1175		return -EPERM;
1176	if (!dev->drivers)
1177		return -ENODEV;
1178
1179	mutex_lock(&isdn_mutex);
1180	if (minor <= ISDN_MINOR_BMAX) {
1181		printk(KERN_WARNING "isdn_write minor %d obsolete!\n", minor);
1182		drvidx = isdn_minor2drv(minor);
1183		if (drvidx < 0) {
1184			retval = -ENODEV;
1185			goto out;
1186		}
1187		if (!(dev->drv[drvidx]->flags & DRV_FLAG_RUNNING)) {
1188			retval = -ENODEV;
1189			goto out;
1190		}
1191		chidx = isdn_minor2chan(minor);
1192		while ((retval = isdn_writebuf_stub(drvidx, chidx, buf, count)) == 0)
1193			interruptible_sleep_on(&dev->drv[drvidx]->snd_waitq[chidx]);
1194		goto out;
1195	}
1196	if (minor <= ISDN_MINOR_CTRLMAX) {
1197		drvidx = isdn_minor2drv(minor - ISDN_MINOR_CTRL);
1198		if (drvidx < 0) {
1199			retval = -ENODEV;
1200			goto out;
1201		}
1202		/*
1203		 * We want to use the isdnctrl device to load the firmware
1204		 *
1205		 if (!(dev->drv[drvidx]->flags & DRV_FLAG_RUNNING))
1206		 return -ENODEV;
1207		 */
1208		if (dev->drv[drvidx]->interface->writecmd)
1209			retval = dev->drv[drvidx]->interface->
1210				writecmd(buf, count, drvidx,
1211				isdn_minor2chan(minor - ISDN_MINOR_CTRL));
1212		else
1213			retval = count;
1214		goto out;
1215	}
1216#ifdef CONFIG_ISDN_PPP
1217	if (minor <= ISDN_MINOR_PPPMAX) {
1218		retval = isdn_ppp_write(minor - ISDN_MINOR_PPP, file, buf, count);
1219		goto out;
1220	}
1221#endif
1222	retval = -ENODEV;
1223 out:
1224	mutex_unlock(&isdn_mutex);
1225	return retval;
1226}
1227
1228static unsigned int
1229isdn_poll(struct file *file, poll_table * wait)
1230{
1231	unsigned int mask = 0;
1232	unsigned int minor = iminor(file->f_path.dentry->d_inode);
1233	int drvidx = isdn_minor2drv(minor - ISDN_MINOR_CTRL);
1234
1235	mutex_lock(&isdn_mutex);
1236	if (minor == ISDN_MINOR_STATUS) {
1237		poll_wait(file, &(dev->info_waitq), wait);
1238		/* mask = POLLOUT | POLLWRNORM; */
1239		if (file->private_data) {
1240			mask |= POLLIN | POLLRDNORM;
1241		}
1242		goto out;
1243	}
1244	if (minor >= ISDN_MINOR_CTRL && minor <= ISDN_MINOR_CTRLMAX) {
1245		if (drvidx < 0) {
1246			/* driver deregistered while file open */
1247			mask = POLLHUP;
1248			goto out;
1249		}
1250		poll_wait(file, &(dev->drv[drvidx]->st_waitq), wait);
1251		mask = POLLOUT | POLLWRNORM;
1252		if (dev->drv[drvidx]->stavail) {
1253			mask |= POLLIN | POLLRDNORM;
1254		}
1255		goto out;
1256	}
1257#ifdef CONFIG_ISDN_PPP
1258	if (minor <= ISDN_MINOR_PPPMAX) {
1259		mask = isdn_ppp_poll(file, wait);
1260		goto out;
1261	}
1262#endif
1263	mask = POLLERR;
1264 out:
1265	mutex_unlock(&isdn_mutex);
1266	return mask;
1267}
1268
1269
1270static int
1271isdn_ioctl(struct file *file, uint cmd, ulong arg)
1272{
1273	uint minor = iminor(file->f_path.dentry->d_inode);
1274	isdn_ctrl c;
1275	int drvidx;
1276	int ret;
1277	int i;
1278	char __user *p;
1279	char *s;
1280	union iocpar {
1281		char name[10];
1282		char bname[22];
1283		isdn_ioctl_struct iocts;
1284		isdn_net_ioctl_phone phone;
1285		isdn_net_ioctl_cfg cfg;
1286	} iocpar;
1287	void __user *argp = (void __user *)arg;
1288
1289#define name  iocpar.name
1290#define bname iocpar.bname
1291#define iocts iocpar.iocts
1292#define phone iocpar.phone
1293#define cfg   iocpar.cfg
1294
1295	if (minor == ISDN_MINOR_STATUS) {
1296		switch (cmd) {
1297			case IIOCGETDVR:
1298				return (TTY_DV +
1299					(NET_DV << 8) +
1300					(INF_DV << 16));
1301			case IIOCGETCPS:
1302				if (arg) {
1303					ulong __user *p = argp;
1304					int i;
1305					if (!access_ok(VERIFY_WRITE, p,
1306							sizeof(ulong) * ISDN_MAX_CHANNELS * 2))
1307						return -EFAULT;
1308					for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
1309						put_user(dev->ibytes[i], p++);
1310						put_user(dev->obytes[i], p++);
1311					}
1312					return 0;
1313				} else
1314					return -EINVAL;
1315				break;
1316#ifdef CONFIG_NETDEVICES
1317			case IIOCNETGPN:
1318				/* Get peer phone number of a connected 
1319				 * isdn network interface */
1320				if (arg) {
1321					if (copy_from_user(&phone, argp, sizeof(phone)))
1322						return -EFAULT;
1323					return isdn_net_getpeer(&phone, argp);
1324				} else
1325					return -EINVAL;
1326#endif
1327			default:
1328				return -EINVAL;
 
 
 
 
 
 
 
 
 
 
 
 
1329		}
1330	}
1331	if (!dev->drivers)
1332		return -ENODEV;
1333	if (minor <= ISDN_MINOR_BMAX) {
1334		drvidx = isdn_minor2drv(minor);
1335		if (drvidx < 0)
1336			return -ENODEV;
1337		if (!(dev->drv[drvidx]->flags & DRV_FLAG_RUNNING))
1338			return -ENODEV;
1339		return 0;
1340	}
1341	if (minor <= ISDN_MINOR_CTRLMAX) {
1342/*
1343 * isdn net devices manage lots of configuration variables as linked lists.
1344 * Those lists must only be manipulated from user space. Some of the ioctl's
1345 * service routines access user space and are not atomic. Therefore, ioctl's
1346 * manipulating the lists and ioctl's sleeping while accessing the lists
1347 * are serialized by means of a semaphore.
1348 */
1349		switch (cmd) {
1350			case IIOCNETDWRSET:
1351				printk(KERN_INFO "INFO: ISDN_DW_ABC_EXTENSION not enabled\n");
1352				return(-EINVAL);
1353			case IIOCNETLCR:
1354				printk(KERN_INFO "INFO: ISDN_ABC_LCR_SUPPORT not enabled\n");
1355				return -ENODEV;
1356#ifdef CONFIG_NETDEVICES
1357			case IIOCNETAIF:
1358				/* Add a network-interface */
1359				if (arg) {
1360					if (copy_from_user(name, argp, sizeof(name)))
1361						return -EFAULT;
1362					s = name;
 
 
 
 
 
 
 
1363				} else {
1364					s = NULL;
1365				}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1366				ret = mutex_lock_interruptible(&dev->mtx);
1367				if( ret ) return ret;
1368				if ((s = isdn_net_new(s, NULL))) {
1369					if (copy_to_user(argp, s, strlen(s) + 1)){
1370						ret = -EFAULT;
1371					} else {
1372						ret = 0;
1373					}
1374				} else
1375					ret = -ENODEV;
1376				mutex_unlock(&dev->mtx);
1377				return ret;
1378			case IIOCNETASL:
1379				/* Add a slave to a network-interface */
1380				if (arg) {
1381					if (copy_from_user(bname, argp, sizeof(bname) - 1))
 
 
 
 
 
 
 
 
 
 
 
 
 
1382						return -EFAULT;
1383				} else
1384					return -EINVAL;
 
 
 
 
 
 
 
1385				ret = mutex_lock_interruptible(&dev->mtx);
1386				if( ret ) return ret;
1387				if ((s = isdn_net_newslave(bname))) {
1388					if (copy_to_user(argp, s, strlen(s) + 1)){
1389						ret = -EFAULT;
1390					} else {
1391						ret = 0;
1392					}
1393				} else
1394					ret = -ENODEV;
1395				mutex_unlock(&dev->mtx);
1396				return ret;
1397			case IIOCNETDIF:
1398				/* Delete a network-interface */
1399				if (arg) {
1400					if (copy_from_user(name, argp, sizeof(name)))
1401						return -EFAULT;
1402					ret = mutex_lock_interruptible(&dev->mtx);
1403					if( ret ) return ret;
1404					ret = isdn_net_rm(name);
1405					mutex_unlock(&dev->mtx);
1406					return ret;
1407				} else
1408					return -EINVAL;
1409			case IIOCNETSCF:
1410				/* Set configurable parameters of a network-interface */
1411				if (arg) {
1412					if (copy_from_user(&cfg, argp, sizeof(cfg)))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1413						return -EFAULT;
1414					return isdn_net_setcfg(&cfg);
1415				} else
1416					return -EINVAL;
1417			case IIOCNETGCF:
1418				/* Get configurable parameters of a network-interface */
1419				if (arg) {
1420					if (copy_from_user(&cfg, argp, sizeof(cfg)))
1421						return -EFAULT;
1422					if (!(ret = isdn_net_getcfg(&cfg))) {
1423						if (copy_to_user(argp, &cfg, sizeof(cfg)))
1424							return -EFAULT;
1425					}
1426					return ret;
1427				} else
1428					return -EINVAL;
1429			case IIOCNETANM:
1430				/* Add a phone-number to a network-interface */
1431				if (arg) {
1432					if (copy_from_user(&phone, argp, sizeof(phone)))
1433						return -EFAULT;
1434					ret = mutex_lock_interruptible(&dev->mtx);
1435					if( ret ) return ret;
1436					ret = isdn_net_addphone(&phone);
1437					mutex_unlock(&dev->mtx);
1438					return ret;
1439				} else
1440					return -EINVAL;
1441			case IIOCNETGNM:
1442				/* Get list of phone-numbers of a network-interface */
1443				if (arg) {
1444					if (copy_from_user(&phone, argp, sizeof(phone)))
 
 
 
 
 
 
 
 
 
1445						return -EFAULT;
1446					ret = mutex_lock_interruptible(&dev->mtx);
1447					if( ret ) return ret;
1448					ret = isdn_net_getphones(&phone, argp);
1449					mutex_unlock(&dev->mtx);
1450					return ret;
1451				} else
1452					return -EINVAL;
1453			case IIOCNETDNM:
1454				/* Delete a phone-number of a network-interface */
1455				if (arg) {
1456					if (copy_from_user(&phone, argp, sizeof(phone)))
1457						return -EFAULT;
1458					ret = mutex_lock_interruptible(&dev->mtx);
1459					if( ret ) return ret;
1460					ret = isdn_net_delphone(&phone);
1461					mutex_unlock(&dev->mtx);
1462					return ret;
1463				} else
1464					return -EINVAL;
1465			case IIOCNETDIL:
1466				/* Force dialing of a network-interface */
1467				if (arg) {
1468					if (copy_from_user(name, argp, sizeof(name)))
1469						return -EFAULT;
1470					return isdn_net_force_dial(name);
1471				} else
1472					return -EINVAL;
1473#ifdef CONFIG_ISDN_PPP
1474			case IIOCNETALN:
1475				if (!arg)
1476					return -EINVAL;
1477				if (copy_from_user(name, argp, sizeof(name)))
1478					return -EFAULT;
1479				return isdn_ppp_dial_slave(name);
1480			case IIOCNETDLN:
1481				if (!arg)
1482					return -EINVAL;
1483				if (copy_from_user(name, argp, sizeof(name)))
1484					return -EFAULT;
1485				return isdn_ppp_hangup_slave(name);
1486#endif
1487			case IIOCNETHUP:
1488				/* Force hangup of a network-interface */
1489				if (!arg)
1490					return -EINVAL;
1491				if (copy_from_user(name, argp, sizeof(name)))
1492					return -EFAULT;
1493				return isdn_net_force_hangup(name);
1494				break;
1495#endif                          /* CONFIG_NETDEVICES */
1496			case IIOCSETVER:
1497				dev->net_verbose = arg;
1498				printk(KERN_INFO "isdn: Verbose-Level is %d\n", dev->net_verbose);
1499				return 0;
1500			case IIOCSETGST:
1501				if (arg)
1502					dev->global_flags |= ISDN_GLOBAL_STOPPED;
1503				else
1504					dev->global_flags &= ~ISDN_GLOBAL_STOPPED;
1505				printk(KERN_INFO "isdn: Global Mode %s\n",
1506				       (dev->global_flags & ISDN_GLOBAL_STOPPED) ? "stopped" : "running");
1507				return 0;
1508			case IIOCSETBRJ:
1509				drvidx = -1;
1510				if (arg) {
1511					int i;
1512					char *p;
1513					if (copy_from_user(&iocts, argp,
1514					     sizeof(isdn_ioctl_struct)))
1515						return -EFAULT;
1516					iocts.drvid[sizeof(iocts.drvid)-1] = 0;
1517					if (strlen(iocts.drvid)) {
1518						if ((p = strchr(iocts.drvid, ',')))
1519							*p = 0;
1520						drvidx = -1;
1521						for (i = 0; i < ISDN_MAX_DRIVERS; i++)
1522							if (!(strcmp(dev->drvid[i], iocts.drvid))) {
1523								drvidx = i;
1524								break;
1525							}
1526					}
1527				}
1528				if (drvidx == -1)
1529					return -ENODEV;
1530				if (iocts.arg)
1531					dev->drv[drvidx]->flags |= DRV_FLAG_REJBUS;
1532				else
1533					dev->drv[drvidx]->flags &= ~DRV_FLAG_REJBUS;
1534				return 0;
1535			case IIOCSIGPRF:
1536				dev->profd = current;
1537				return 0;
1538				break;
1539			case IIOCGETPRF:
1540				/* Get all Modem-Profiles */
1541				if (arg) {
1542					char __user *p = argp;
1543					int i;
1544
1545					if (!access_ok(VERIFY_WRITE, argp,
1546					(ISDN_MODEM_NUMREG + ISDN_MSNLEN + ISDN_LMSNLEN)
1547						   * ISDN_MAX_CHANNELS))
1548						return -EFAULT;
1549
1550					for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
1551						if (copy_to_user(p, dev->mdm.info[i].emu.profile,
1552						      ISDN_MODEM_NUMREG))
1553							return -EFAULT;
1554						p += ISDN_MODEM_NUMREG;
1555						if (copy_to_user(p, dev->mdm.info[i].emu.pmsn, ISDN_MSNLEN))
1556							return -EFAULT;
1557						p += ISDN_MSNLEN;
1558						if (copy_to_user(p, dev->mdm.info[i].emu.plmsn, ISDN_LMSNLEN))
1559							return -EFAULT;
1560						p += ISDN_LMSNLEN;
1561					}
1562					return (ISDN_MODEM_NUMREG + ISDN_MSNLEN + ISDN_LMSNLEN) * ISDN_MAX_CHANNELS;
1563				} else
1564					return -EINVAL;
1565				break;
1566			case IIOCSETPRF:
1567				/* Set all Modem-Profiles */
1568				if (arg) {
1569					char __user *p = argp;
1570					int i;
1571
1572					if (!access_ok(VERIFY_READ, argp,
1573					(ISDN_MODEM_NUMREG + ISDN_MSNLEN + ISDN_LMSNLEN)
1574						   * ISDN_MAX_CHANNELS))
1575						return -EFAULT;
1576
1577					for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
1578						if (copy_from_user(dev->mdm.info[i].emu.profile, p,
1579						     ISDN_MODEM_NUMREG))
1580							return -EFAULT;
1581						p += ISDN_MODEM_NUMREG;
1582						if (copy_from_user(dev->mdm.info[i].emu.plmsn, p, ISDN_LMSNLEN))
1583							return -EFAULT;
1584						p += ISDN_LMSNLEN;
1585						if (copy_from_user(dev->mdm.info[i].emu.pmsn, p, ISDN_MSNLEN))
1586							return -EFAULT;
1587						p += ISDN_MSNLEN;
1588					}
1589					return 0;
1590				} else
1591					return -EINVAL;
1592				break;
1593			case IIOCSETMAP:
1594			case IIOCGETMAP:
1595				/* Set/Get MSN->EAZ-Mapping for a driver */
1596				if (arg) {
1597
1598					if (copy_from_user(&iocts, argp,
1599					     sizeof(isdn_ioctl_struct)))
1600						return -EFAULT;
1601					iocts.drvid[sizeof(iocts.drvid)-1] = 0;
1602					if (strlen(iocts.drvid)) {
1603						drvidx = -1;
1604						for (i = 0; i < ISDN_MAX_DRIVERS; i++)
1605							if (!(strcmp(dev->drvid[i], iocts.drvid))) {
1606								drvidx = i;
 
 
 
1607								break;
 
 
1608							}
1609					} else
1610						drvidx = 0;
1611					if (drvidx == -1)
1612						return -ENODEV;
1613					if (cmd == IIOCSETMAP) {
1614						int loop = 1;
1615
1616						p = (char __user *) iocts.arg;
1617						i = 0;
1618						while (loop) {
1619							int j = 0;
1620
1621							while (1) {
1622								if (!access_ok(VERIFY_READ, p, 1))
1623									return -EFAULT;
1624								get_user(bname[j], p++);
1625								switch (bname[j]) {
1626									case '\0':
1627										loop = 0;
1628										/* Fall through */
1629									case ',':
1630										bname[j] = '\0';
1631										strcpy(dev->drv[drvidx]->msn2eaz[i], bname);
1632										j = ISDN_MSNLEN;
1633										break;
1634									default:
1635										j++;
1636								}
1637								if (j >= ISDN_MSNLEN)
1638									break;
1639							}
1640							if (++i > 9)
1641								break;
1642						}
1643					} else {
1644						p = (char __user *) iocts.arg;
1645						for (i = 0; i < 10; i++) {
1646							snprintf(bname, sizeof(bname), "%s%s",
1647								strlen(dev->drv[drvidx]->msn2eaz[i]) ?
1648								dev->drv[drvidx]->msn2eaz[i] : "_",
1649								(i < 9) ? "," : "\0");
1650							if (copy_to_user(p, bname, strlen(bname) + 1))
1651								return -EFAULT;
1652							p += strlen(bname);
1653						}
1654					}
1655					return 0;
1656				} else
1657					return -EINVAL;
1658			case IIOCDBGVAR:
1659				if (arg) {
1660					if (copy_to_user(argp, &dev, sizeof(ulong)))
1661						return -EFAULT;
1662					return 0;
1663				} else
1664					return -EINVAL;
1665				break;
1666			default:
1667				if ((cmd & IIOCDRVCTL) == IIOCDRVCTL)
1668					cmd = ((cmd >> _IOC_NRSHIFT) & _IOC_NRMASK) & ISDN_DRVIOCTL_MASK;
1669				else
1670					return -EINVAL;
1671				if (arg) {
1672					int i;
1673					char *p;
1674					if (copy_from_user(&iocts, argp, sizeof(isdn_ioctl_struct)))
1675						return -EFAULT;
1676					iocts.drvid[sizeof(iocts.drvid)-1] = 0;
1677					if (strlen(iocts.drvid)) {
1678						if ((p = strchr(iocts.drvid, ',')))
1679							*p = 0;
1680						drvidx = -1;
1681						for (i = 0; i < ISDN_MAX_DRIVERS; i++)
1682							if (!(strcmp(dev->drvid[i], iocts.drvid))) {
1683								drvidx = i;
1684								break;
1685							}
1686					} else
1687						drvidx = 0;
1688					if (drvidx == -1)
1689						return -ENODEV;
1690					if (!access_ok(VERIFY_WRITE, argp,
1691					     sizeof(isdn_ioctl_struct)))
1692						return -EFAULT;
1693					c.driver = drvidx;
1694					c.command = ISDN_CMD_IOCTL;
1695					c.arg = cmd;
1696					memcpy(c.parm.num, &iocts.arg, sizeof(ulong));
1697					ret = isdn_command(&c);
1698					memcpy(&iocts.arg, c.parm.num, sizeof(ulong));
1699					if (copy_to_user(argp, &iocts, sizeof(isdn_ioctl_struct)))
1700						return -EFAULT;
1701					return ret;
1702				} else
1703					return -EINVAL;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1704		}
1705	}
1706#ifdef CONFIG_ISDN_PPP
1707	if (minor <= ISDN_MINOR_PPPMAX)
1708		return (isdn_ppp_ioctl(minor - ISDN_MINOR_PPP, file, cmd, arg));
1709#endif
1710	return -ENODEV;
1711
1712#undef name
1713#undef bname
1714#undef iocts
1715#undef phone
1716#undef cfg
1717}
1718
1719static long
1720isdn_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1721{
1722	int ret;
1723
1724	mutex_lock(&isdn_mutex);
1725	ret = isdn_ioctl(file, cmd, arg);
1726	mutex_unlock(&isdn_mutex);
1727
1728	return ret;
1729}
1730
1731/*
1732 * Open the device code.
1733 */
1734static int
1735isdn_open(struct inode *ino, struct file *filep)
1736{
1737	uint minor = iminor(ino);
1738	int drvidx;
1739	int chidx;
1740	int retval = -ENODEV;
1741
1742	mutex_lock(&isdn_mutex);
1743	if (minor == ISDN_MINOR_STATUS) {
1744		infostruct *p;
1745
1746		if ((p = kmalloc(sizeof(infostruct), GFP_KERNEL))) {
1747			p->next = (char *) dev->infochain;
1748			p->private = (char *) &(filep->private_data);
1749			dev->infochain = p;
1750			/* At opening we allow a single update */
1751			filep->private_data = (char *) 1;
1752			retval = 0;
1753			goto out;
1754		} else {
1755			retval = -ENOMEM;
1756			goto out;
1757		}
1758	}
1759	if (!dev->channels)
1760		goto out;
1761	if (minor <= ISDN_MINOR_BMAX) {
1762		printk(KERN_WARNING "isdn_open minor %d obsolete!\n", minor);
1763		drvidx = isdn_minor2drv(minor);
1764		if (drvidx < 0)
1765			goto out;
1766		chidx = isdn_minor2chan(minor);
1767		if (!(dev->drv[drvidx]->flags & DRV_FLAG_RUNNING))
1768			goto out;
1769		if (!(dev->drv[drvidx]->online & (1 << chidx)))
1770			goto out;
1771		isdn_lock_drivers();
1772		retval = 0;
1773		goto out;
1774	}
1775	if (minor <= ISDN_MINOR_CTRLMAX) {
1776		drvidx = isdn_minor2drv(minor - ISDN_MINOR_CTRL);
1777		if (drvidx < 0)
1778			goto out;
1779		isdn_lock_drivers();
1780		retval = 0;
1781		goto out;
1782	}
1783#ifdef CONFIG_ISDN_PPP
1784	if (minor <= ISDN_MINOR_PPPMAX) {
1785		retval = isdn_ppp_open(minor - ISDN_MINOR_PPP, filep);
1786		if (retval == 0)
1787			isdn_lock_drivers();
1788		goto out;
1789	}
1790#endif
1791 out:
1792	nonseekable_open(ino, filep);
1793	mutex_unlock(&isdn_mutex);
1794	return retval;
1795}
1796
1797static int
1798isdn_close(struct inode *ino, struct file *filep)
1799{
1800	uint minor = iminor(ino);
1801
1802	mutex_lock(&isdn_mutex);
1803	if (minor == ISDN_MINOR_STATUS) {
1804		infostruct *p = dev->infochain;
1805		infostruct *q = NULL;
1806
1807		while (p) {
1808			if (p->private == (char *) &(filep->private_data)) {
1809				if (q)
1810					q->next = p->next;
1811				else
1812					dev->infochain = (infostruct *) (p->next);
1813				kfree(p);
1814				goto out;
1815			}
1816			q = p;
1817			p = (infostruct *) (p->next);
1818		}
1819		printk(KERN_WARNING "isdn: No private data while closing isdnctrl\n");
1820		goto out;
1821	}
1822	isdn_unlock_drivers();
1823	if (minor <= ISDN_MINOR_BMAX)
1824		goto out;
1825	if (minor <= ISDN_MINOR_CTRLMAX) {
1826		if (dev->profd == current)
1827			dev->profd = NULL;
1828		goto out;
1829	}
1830#ifdef CONFIG_ISDN_PPP
1831	if (minor <= ISDN_MINOR_PPPMAX)
1832		isdn_ppp_release(minor - ISDN_MINOR_PPP, filep);
1833#endif
1834
1835 out:
1836	mutex_unlock(&isdn_mutex);
1837	return 0;
1838}
1839
1840static const struct file_operations isdn_fops =
1841{
1842	.owner		= THIS_MODULE,
1843	.llseek		= no_llseek,
1844	.read		= isdn_read,
1845	.write		= isdn_write,
1846	.poll		= isdn_poll,
1847	.unlocked_ioctl	= isdn_unlocked_ioctl,
1848	.open		= isdn_open,
1849	.release	= isdn_close,
1850};
1851
1852char *
1853isdn_map_eaz2msn(char *msn, int di)
1854{
1855	isdn_driver_t *this = dev->drv[di];
1856	int i;
1857
1858	if (strlen(msn) == 1) {
1859		i = msn[0] - '0';
1860		if ((i >= 0) && (i <= 9))
1861			if (strlen(this->msn2eaz[i]))
1862				return (this->msn2eaz[i]);
1863	}
1864	return (msn);
1865}
1866
1867/*
1868 * Find an unused ISDN-channel, whose feature-flags match the
1869 * given L2- and L3-protocols.
1870 */
1871#define L2V (~(ISDN_FEATURE_L2_V11096|ISDN_FEATURE_L2_V11019|ISDN_FEATURE_L2_V11038))
1872
1873/*
1874 * This function must be called with holding the dev->lock.
1875 */
1876int
1877isdn_get_free_channel(int usage, int l2_proto, int l3_proto, int pre_dev
1878		      ,int pre_chan, char *msn)
1879{
1880	int i;
1881	ulong features;
1882	ulong vfeatures;
1883
1884	features = ((1 << l2_proto) | (0x10000 << l3_proto));
1885	vfeatures = (((1 << l2_proto) | (0x10000 << l3_proto)) &
1886		     ~(ISDN_FEATURE_L2_V11096|ISDN_FEATURE_L2_V11019|ISDN_FEATURE_L2_V11038));
1887	/* If Layer-2 protocol is V.110, accept drivers with
1888	 * transparent feature even if these don't support V.110
1889	 * because we can emulate this in linklevel.
1890	 */
1891	for (i = 0; i < ISDN_MAX_CHANNELS; i++)
1892		if (USG_NONE(dev->usage[i]) &&
1893		    (dev->drvmap[i] != -1)) {
1894			int d = dev->drvmap[i];
1895			if ((dev->usage[i] & ISDN_USAGE_EXCLUSIVE) &&
1896			((pre_dev != d) || (pre_chan != dev->chanmap[i])))
1897				continue;
1898			if (!strcmp(isdn_map_eaz2msn(msn, d), "-"))
1899				continue;
1900			if (dev->usage[i] & ISDN_USAGE_DISABLED)
1901			        continue; /* usage not allowed */
1902			if (dev->drv[d]->flags & DRV_FLAG_RUNNING) {
1903				if (((dev->drv[d]->interface->features & features) == features) ||
1904				    (((dev->drv[d]->interface->features & vfeatures) == vfeatures) &&
1905				     (dev->drv[d]->interface->features & ISDN_FEATURE_L2_TRANS))) {
1906					if ((pre_dev < 0) || (pre_chan < 0)) {
1907						dev->usage[i] &= ISDN_USAGE_EXCLUSIVE;
1908						dev->usage[i] |= usage;
1909						isdn_info_update();
1910						return i;
1911					} else {
1912						if ((pre_dev == d) && (pre_chan == dev->chanmap[i])) {
1913							dev->usage[i] &= ISDN_USAGE_EXCLUSIVE;
1914							dev->usage[i] |= usage;
1915							isdn_info_update();
1916							return i;
1917						}
1918					}
1919				}
1920			}
1921		}
1922	return -1;
1923}
1924
1925/*
1926 * Set state of ISDN-channel to 'unused'
1927 */
1928void
1929isdn_free_channel(int di, int ch, int usage)
1930{
1931	int i;
1932
1933	if ((di < 0) || (ch < 0)) {
1934		printk(KERN_WARNING "%s: called with invalid drv(%d) or channel(%d)\n",
1935			__func__, di, ch);
1936		return;
1937	}
1938	for (i = 0; i < ISDN_MAX_CHANNELS; i++)
1939		if (((!usage) || ((dev->usage[i] & ISDN_USAGE_MASK) == usage)) &&
1940		    (dev->drvmap[i] == di) &&
1941		    (dev->chanmap[i] == ch)) {
1942			dev->usage[i] &= (ISDN_USAGE_NONE | ISDN_USAGE_EXCLUSIVE);
1943			strcpy(dev->num[i], "???");
1944			dev->ibytes[i] = 0;
1945			dev->obytes[i] = 0;
1946// 20.10.99 JIM, try to reinitialize v110 !
1947			dev->v110emu[i] = 0;
1948			atomic_set(&(dev->v110use[i]), 0);
1949			isdn_v110_close(dev->v110[i]);
1950			dev->v110[i] = NULL;
1951// 20.10.99 JIM, try to reinitialize v110 !
1952			isdn_info_update();
1953			if (dev->drv[di])
1954				skb_queue_purge(&dev->drv[di]->rpqueue[ch]);
1955		}
1956}
1957
1958/*
1959 * Cancel Exclusive-Flag for ISDN-channel
1960 */
1961void
1962isdn_unexclusive_channel(int di, int ch)
1963{
1964	int i;
1965
1966	for (i = 0; i < ISDN_MAX_CHANNELS; i++)
1967		if ((dev->drvmap[i] == di) &&
1968		    (dev->chanmap[i] == ch)) {
1969			dev->usage[i] &= ~ISDN_USAGE_EXCLUSIVE;
1970			isdn_info_update();
1971			return;
1972		}
1973}
1974
1975/*
1976 *  writebuf replacement for SKB_ABLE drivers
1977 */
1978static int
1979isdn_writebuf_stub(int drvidx, int chan, const u_char __user * buf, int len)
1980{
1981	int ret;
1982	int hl = dev->drv[drvidx]->interface->hl_hdrlen;
1983	struct sk_buff *skb = alloc_skb(hl + len, GFP_ATOMIC);
1984
1985	if (!skb)
1986		return -ENOMEM;
1987	skb_reserve(skb, hl);
1988	if (copy_from_user(skb_put(skb, len), buf, len)) {
1989		dev_kfree_skb(skb);
1990		return -EFAULT;
1991	}
1992	ret = dev->drv[drvidx]->interface->writebuf_skb(drvidx, chan, 1, skb);
1993	if (ret <= 0)
1994		dev_kfree_skb(skb);
1995	if (ret > 0)
1996		dev->obytes[isdn_dc2minor(drvidx, chan)] += ret;
1997	return ret;
1998}
1999
2000/*
2001 * Return: length of data on success, -ERRcode on failure.
2002 */
2003int
2004isdn_writebuf_skb_stub(int drvidx, int chan, int ack, struct sk_buff *skb)
2005{
2006	int ret;
2007	struct sk_buff *nskb = NULL;
2008	int v110_ret = skb->len;
2009	int idx = isdn_dc2minor(drvidx, chan);
2010
2011	if (dev->v110[idx]) {
2012		atomic_inc(&dev->v110use[idx]);
2013		nskb = isdn_v110_encode(dev->v110[idx], skb);
2014		atomic_dec(&dev->v110use[idx]);
2015		if (!nskb)
2016			return 0;
2017		v110_ret = *((int *)nskb->data);
2018		skb_pull(nskb, sizeof(int));
2019		if (!nskb->len) {
2020			dev_kfree_skb(nskb);
2021			return v110_ret;
2022		}
2023		/* V.110 must always be acknowledged */
2024		ack = 1;
2025		ret = dev->drv[drvidx]->interface->writebuf_skb(drvidx, chan, ack, nskb);
2026	} else {
2027		int hl = dev->drv[drvidx]->interface->hl_hdrlen;
2028
2029		if( skb_headroom(skb) < hl ){
2030			/* 
2031			 * This should only occur when new HL driver with
2032			 * increased hl_hdrlen was loaded after netdevice
2033			 * was created and connected to the new driver.
2034			 *
2035			 * The V.110 branch (re-allocates on its own) does
2036			 * not need this
2037			 */
2038			struct sk_buff * skb_tmp;
2039
2040			skb_tmp = skb_realloc_headroom(skb, hl);
2041			printk(KERN_DEBUG "isdn_writebuf_skb_stub: reallocating headroom%s\n", skb_tmp ? "" : " failed");
2042			if (!skb_tmp) return -ENOMEM; /* 0 better? */
2043			ret = dev->drv[drvidx]->interface->writebuf_skb(drvidx, chan, ack, skb_tmp);
2044			if( ret > 0 ){
2045				dev_kfree_skb(skb);
2046			} else {
2047				dev_kfree_skb(skb_tmp);
2048			}
2049		} else {
2050			ret = dev->drv[drvidx]->interface->writebuf_skb(drvidx, chan, ack, skb);
2051		}
2052	}
2053	if (ret > 0) {
2054		dev->obytes[idx] += ret;
2055		if (dev->v110[idx]) {
2056			atomic_inc(&dev->v110use[idx]);
2057			dev->v110[idx]->skbuser++;
2058			atomic_dec(&dev->v110use[idx]);
2059			/* For V.110 return unencoded data length */
2060			ret = v110_ret;
2061			/* if the complete frame was send we free the skb;
2062			   if not upper function will requeue the skb */ 
2063			if (ret == skb->len)
2064				dev_kfree_skb(skb);
2065		}
2066	} else
2067		if (dev->v110[idx])
2068			dev_kfree_skb(nskb);
2069	return ret;
2070}
2071
2072static int
2073isdn_add_channels(isdn_driver_t *d, int drvidx, int n, int adding)
2074{
2075	int j, k, m;
2076
2077	init_waitqueue_head(&d->st_waitq);
2078	if (d->flags & DRV_FLAG_RUNNING)
2079		return -1;
2080       	if (n < 1) return 0;
2081
2082	m = (adding) ? d->channels + n : n;
2083
2084	if (dev->channels + n > ISDN_MAX_CHANNELS) {
2085		printk(KERN_WARNING "register_isdn: Max. %d channels supported\n",
2086		       ISDN_MAX_CHANNELS);
2087		return -1;
2088	}
2089
2090	if ((adding) && (d->rcverr))
2091		kfree(d->rcverr);
2092	if (!(d->rcverr = kzalloc(sizeof(int) * m, GFP_ATOMIC))) {
2093		printk(KERN_WARNING "register_isdn: Could not alloc rcverr\n");
2094		return -1;
2095	}
2096
2097	if ((adding) && (d->rcvcount))
2098		kfree(d->rcvcount);
2099	if (!(d->rcvcount = kzalloc(sizeof(int) * m, GFP_ATOMIC))) {
2100		printk(KERN_WARNING "register_isdn: Could not alloc rcvcount\n");
2101		if (!adding)
2102			kfree(d->rcverr);
2103		return -1;
2104	}
2105
2106	if ((adding) && (d->rpqueue)) {
2107		for (j = 0; j < d->channels; j++)
2108			skb_queue_purge(&d->rpqueue[j]);
2109		kfree(d->rpqueue);
2110	}
2111	if (!(d->rpqueue = kmalloc(sizeof(struct sk_buff_head) * m, GFP_ATOMIC))) {
2112		printk(KERN_WARNING "register_isdn: Could not alloc rpqueue\n");
2113		if (!adding) {
2114			kfree(d->rcvcount);
2115			kfree(d->rcverr);
2116		}
2117		return -1; 
2118	}
2119	for (j = 0; j < m; j++) {
2120		skb_queue_head_init(&d->rpqueue[j]);
2121	}
2122
2123	if ((adding) && (d->rcv_waitq))
2124		kfree(d->rcv_waitq);
2125	d->rcv_waitq = kmalloc(sizeof(wait_queue_head_t) * 2 * m, GFP_ATOMIC);
2126	if (!d->rcv_waitq) {
2127		printk(KERN_WARNING "register_isdn: Could not alloc rcv_waitq\n");
2128		if (!adding) {
2129			kfree(d->rpqueue);
2130			kfree(d->rcvcount);
2131			kfree(d->rcverr);
2132		}
2133		return -1;
2134	}
2135	d->snd_waitq = d->rcv_waitq + m;
2136	for (j = 0; j < m; j++) {
2137		init_waitqueue_head(&d->rcv_waitq[j]);
2138		init_waitqueue_head(&d->snd_waitq[j]);
2139	}
2140
2141	dev->channels += n;
2142	for (j = d->channels; j < m; j++)
2143		for (k = 0; k < ISDN_MAX_CHANNELS; k++)
2144			if (dev->chanmap[k] < 0) {
2145				dev->chanmap[k] = j;
2146				dev->drvmap[k] = drvidx;
2147				break;
2148			}
2149	d->channels = m;
2150	return 0;
2151}
2152
2153/*
2154 * Low-level-driver registration
2155 */
2156
2157static void
2158set_global_features(void)
2159{
2160	int drvidx;
2161
2162	dev->global_features = 0;
2163	for (drvidx = 0; drvidx < ISDN_MAX_DRIVERS; drvidx++) {
2164		if (!dev->drv[drvidx])
2165			continue;
2166		if (dev->drv[drvidx]->interface)
2167			dev->global_features |= dev->drv[drvidx]->interface->features;
2168	}
2169}
2170
2171#ifdef CONFIG_ISDN_DIVERSION
2172
2173static char *map_drvname(int di)
2174{
2175  if ((di < 0) || (di >= ISDN_MAX_DRIVERS)) 
2176    return(NULL);
2177  return(dev->drvid[di]); /* driver name */
2178} /* map_drvname */
2179
2180static int map_namedrv(char *id)
2181{  int i;
2182
2183   for (i = 0; i < ISDN_MAX_DRIVERS; i++)
2184    { if (!strcmp(dev->drvid[i],id)) 
2185        return(i);
2186    }
2187   return(-1);
2188} /* map_namedrv */
2189
2190int DIVERT_REG_NAME(isdn_divert_if *i_div)
2191{
2192  if (i_div->if_magic != DIVERT_IF_MAGIC) 
2193    return(DIVERT_VER_ERR);
2194  switch (i_div->cmd)
2195    {
2196      case DIVERT_CMD_REL:
2197        if (divert_if != i_div) 
2198          return(DIVERT_REL_ERR);
2199        divert_if = NULL; /* free interface */
2200        return(DIVERT_NO_ERR);
2201
2202      case DIVERT_CMD_REG:
2203        if (divert_if) 
2204          return(DIVERT_REG_ERR);
2205        i_div->ll_cmd = isdn_command; /* set command function */
2206        i_div->drv_to_name = map_drvname; 
2207        i_div->name_to_drv = map_namedrv; 
2208        divert_if = i_div; /* remember interface */
2209        return(DIVERT_NO_ERR);
2210
2211      default:
2212        return(DIVERT_CMD_ERR);   
2213    }
2214} /* DIVERT_REG_NAME */
2215
2216EXPORT_SYMBOL(DIVERT_REG_NAME);
2217
2218#endif /* CONFIG_ISDN_DIVERSION */
2219
2220
2221EXPORT_SYMBOL(register_isdn);
2222#ifdef CONFIG_ISDN_PPP
2223EXPORT_SYMBOL(isdn_ppp_register_compressor);
2224EXPORT_SYMBOL(isdn_ppp_unregister_compressor);
2225#endif
2226
2227int
2228register_isdn(isdn_if * i)
2229{
2230	isdn_driver_t *d;
2231	int j;
2232	ulong flags;
2233	int drvidx;
2234
2235	if (dev->drivers >= ISDN_MAX_DRIVERS) {
2236		printk(KERN_WARNING "register_isdn: Max. %d drivers supported\n",
2237		       ISDN_MAX_DRIVERS);
2238		return 0;
2239	}
2240	if (!i->writebuf_skb) {
2241		printk(KERN_WARNING "register_isdn: No write routine given.\n");
2242		return 0;
2243	}
2244	if (!(d = kzalloc(sizeof(isdn_driver_t), GFP_KERNEL))) {
2245		printk(KERN_WARNING "register_isdn: Could not alloc driver-struct\n");
2246		return 0;
2247	}
2248
2249	d->maxbufsize = i->maxbufsize;
2250	d->pktcount = 0;
2251	d->stavail = 0;
2252	d->flags = DRV_FLAG_LOADED;
2253	d->online = 0;
2254	d->interface = i;
2255	d->channels = 0;
2256	spin_lock_irqsave(&dev->lock, flags);
2257	for (drvidx = 0; drvidx < ISDN_MAX_DRIVERS; drvidx++)
2258		if (!dev->drv[drvidx])
2259			break;
2260	if (isdn_add_channels(d, drvidx, i->channels, 0)) {
2261		spin_unlock_irqrestore(&dev->lock, flags);
2262		kfree(d);
2263		return 0;
2264	}
2265	i->channels = drvidx;
2266	i->rcvcallb_skb = isdn_receive_skb_callback;
2267	i->statcallb = isdn_status_callback;
2268	if (!strlen(i->id))
2269		sprintf(i->id, "line%d", drvidx);
2270	for (j = 0; j < drvidx; j++)
2271		if (!strcmp(i->id, dev->drvid[j]))
2272			sprintf(i->id, "line%d", drvidx);
2273	dev->drv[drvidx] = d;
2274	strcpy(dev->drvid[drvidx], i->id);
2275	isdn_info_update();
2276	dev->drivers++;
2277	set_global_features();
2278	spin_unlock_irqrestore(&dev->lock, flags);
2279	return 1;
2280}
2281
2282/*
2283 *****************************************************************************
2284 * And now the modules code.
2285 *****************************************************************************
2286 */
2287
2288static char *
2289isdn_getrev(const char *revision)
2290{
2291	char *rev;
2292	char *p;
2293
2294	if ((p = strchr(revision, ':'))) {
2295		rev = p + 2;
2296		p = strchr(rev, '$');
2297		*--p = 0;
2298	} else
2299		rev = "???";
2300	return rev;
2301}
2302
2303/*
2304 * Allocate and initialize all data, register modem-devices
2305 */
2306static int __init isdn_init(void)
2307{
2308	int i;
2309	char tmprev[50];
2310
2311	if (!(dev = vmalloc(sizeof(isdn_dev)))) {
 
2312		printk(KERN_WARNING "isdn: Could not allocate device-struct.\n");
2313		return -EIO;
2314	}
2315	memset((char *) dev, 0, sizeof(isdn_dev));
2316	init_timer(&dev->timer);
2317	dev->timer.function = isdn_timer_funct;
2318	spin_lock_init(&dev->lock);
2319	spin_lock_init(&dev->timerlock);
2320#ifdef MODULE
2321	dev->owner = THIS_MODULE;
2322#endif
2323	mutex_init(&dev->mtx);
2324	init_waitqueue_head(&dev->info_waitq);
2325	for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
2326		dev->drvmap[i] = -1;
2327		dev->chanmap[i] = -1;
2328		dev->m_idx[i] = -1;
2329		strcpy(dev->num[i], "???");
2330		init_waitqueue_head(&dev->mdm.info[i].open_wait);
2331		init_waitqueue_head(&dev->mdm.info[i].close_wait);
2332	}
2333	if (register_chrdev(ISDN_MAJOR, "isdn", &isdn_fops)) {
2334		printk(KERN_WARNING "isdn: Could not register control devices\n");
2335		vfree(dev);
2336		return -EIO;
2337	}
2338	if ((isdn_tty_modem_init()) < 0) {
2339		printk(KERN_WARNING "isdn: Could not register tty devices\n");
2340		vfree(dev);
2341		unregister_chrdev(ISDN_MAJOR, "isdn");
2342		return -EIO;
2343	}
2344#ifdef CONFIG_ISDN_PPP
2345	if (isdn_ppp_init() < 0) {
2346		printk(KERN_WARNING "isdn: Could not create PPP-device-structs\n");
2347		isdn_tty_exit();
2348		unregister_chrdev(ISDN_MAJOR, "isdn");
2349		vfree(dev);
2350		return -EIO;
2351	}
2352#endif                          /* CONFIG_ISDN_PPP */
2353
2354	strcpy(tmprev, isdn_revision);
2355	printk(KERN_NOTICE "ISDN subsystem Rev: %s/", isdn_getrev(tmprev));
2356	strcpy(tmprev, isdn_tty_revision);
2357	printk("%s/", isdn_getrev(tmprev));
2358	strcpy(tmprev, isdn_net_revision);
2359	printk("%s/", isdn_getrev(tmprev));
2360	strcpy(tmprev, isdn_ppp_revision);
2361	printk("%s/", isdn_getrev(tmprev));
2362	strcpy(tmprev, isdn_audio_revision);
2363	printk("%s/", isdn_getrev(tmprev));
2364	strcpy(tmprev, isdn_v110_revision);
2365	printk("%s", isdn_getrev(tmprev));
2366
2367#ifdef MODULE
2368	printk(" loaded\n");
2369#else
2370	printk("\n");
2371#endif
2372	isdn_info_update();
2373	return 0;
2374}
2375
2376/*
2377 * Unload module
2378 */
2379static void __exit isdn_exit(void)
2380{
2381#ifdef CONFIG_ISDN_PPP
2382	isdn_ppp_cleanup();
2383#endif
2384	if (isdn_net_rmall() < 0) {
2385		printk(KERN_WARNING "isdn: net-device busy, remove cancelled\n");
2386		return;
2387	}
2388	isdn_tty_exit();
2389	unregister_chrdev(ISDN_MAJOR, "isdn");
2390	del_timer(&dev->timer);
2391	/* call vfree with interrupts enabled, else it will hang */
2392	vfree(dev);
2393	printk(KERN_NOTICE "ISDN-subsystem unloaded\n");
2394}
2395
2396module_init(isdn_init);
2397module_exit(isdn_exit);
v3.15
   1/* $Id: isdn_common.c,v 1.1.2.3 2004/02/10 01:07:13 keil Exp $
   2 *
   3 * Linux ISDN subsystem, common used functions (linklevel).
   4 *
   5 * Copyright 1994-1999  by Fritz Elfert (fritz@isdn4linux.de)
   6 * Copyright 1995,96    Thinking Objects Software GmbH Wuerzburg
   7 * Copyright 1995,96    by Michael Hipp (Michael.Hipp@student.uni-tuebingen.de)
   8 *
   9 * This software may be used and distributed according to the terms
  10 * of the GNU General Public License, incorporated herein by reference.
  11 *
  12 */
  13
  14#include <linux/module.h>
  15#include <linux/init.h>
  16#include <linux/poll.h>
  17#include <linux/slab.h>
  18#include <linux/vmalloc.h>
  19#include <linux/isdn.h>
  20#include <linux/mutex.h>
  21#include "isdn_common.h"
  22#include "isdn_tty.h"
  23#include "isdn_net.h"
  24#include "isdn_ppp.h"
  25#ifdef CONFIG_ISDN_AUDIO
  26#include "isdn_audio.h"
  27#endif
  28#ifdef CONFIG_ISDN_DIVERSION_MODULE
  29#define CONFIG_ISDN_DIVERSION
  30#endif
  31#ifdef CONFIG_ISDN_DIVERSION
  32#include <linux/isdn_divertif.h>
  33#endif /* CONFIG_ISDN_DIVERSION */
  34#include "isdn_v110.h"
  35
  36/* Debugflags */
  37#undef ISDN_DEBUG_STATCALLB
  38
  39MODULE_DESCRIPTION("ISDN4Linux: link layer");
  40MODULE_AUTHOR("Fritz Elfert");
  41MODULE_LICENSE("GPL");
  42
  43isdn_dev *dev;
  44
  45static DEFINE_MUTEX(isdn_mutex);
  46static char *isdn_revision = "$Revision: 1.1.2.3 $";
  47
  48extern char *isdn_net_revision;
 
  49#ifdef CONFIG_ISDN_PPP
  50extern char *isdn_ppp_revision;
  51#else
  52static char *isdn_ppp_revision = ": none $";
  53#endif
  54#ifdef CONFIG_ISDN_AUDIO
  55extern char *isdn_audio_revision;
  56#else
  57static char *isdn_audio_revision = ": none $";
  58#endif
  59extern char *isdn_v110_revision;
  60
  61#ifdef CONFIG_ISDN_DIVERSION
  62static isdn_divert_if *divert_if; /* = NULL */
  63#endif /* CONFIG_ISDN_DIVERSION */
  64
  65
  66static int isdn_writebuf_stub(int, int, const u_char __user *, int);
  67static void set_global_features(void);
  68static int isdn_wildmat(char *s, char *p);
  69static int isdn_add_channels(isdn_driver_t *d, int drvidx, int n, int adding);
  70
  71static inline void
  72isdn_lock_driver(isdn_driver_t *drv)
  73{
  74	try_module_get(drv->interface->owner);
  75	drv->locks++;
  76}
  77
  78void
  79isdn_lock_drivers(void)
  80{
  81	int i;
  82
  83	for (i = 0; i < ISDN_MAX_DRIVERS; i++) {
  84		if (!dev->drv[i])
  85			continue;
  86		isdn_lock_driver(dev->drv[i]);
  87	}
  88}
  89
  90static inline void
  91isdn_unlock_driver(isdn_driver_t *drv)
  92{
  93	if (drv->locks > 0) {
  94		drv->locks--;
  95		module_put(drv->interface->owner);
  96	}
  97}
  98
  99void
 100isdn_unlock_drivers(void)
 101{
 102	int i;
 103
 104	for (i = 0; i < ISDN_MAX_DRIVERS; i++) {
 105		if (!dev->drv[i])
 106			continue;
 107		isdn_unlock_driver(dev->drv[i]);
 108	}
 109}
 110
 111#if defined(ISDN_DEBUG_NET_DUMP) || defined(ISDN_DEBUG_MODEM_DUMP)
 112void
 113isdn_dumppkt(char *s, u_char *p, int len, int dumplen)
 114{
 115	int dumpc;
 116
 117	printk(KERN_DEBUG "%s(%d) ", s, len);
 118	for (dumpc = 0; (dumpc < dumplen) && (len); len--, dumpc++)
 119		printk(" %02x", *p++);
 120	printk("\n");
 121}
 122#endif
 123
 124/*
 125 * I picked the pattern-matching-functions from an old GNU-tar version (1.10)
 126 * It was originally written and put to PD by rs@mirror.TMC.COM (Rich Salz)
 127 */
 128static int
 129isdn_star(char *s, char *p)
 130{
 131	while (isdn_wildmat(s, p)) {
 132		if (*++s == '\0')
 133			return (2);
 134	}
 135	return (0);
 136}
 137
 138/*
 139 * Shell-type Pattern-matching for incoming caller-Ids
 140 * This function gets a string in s and checks, if it matches the pattern
 141 * given in p.
 142 *
 143 * Return:
 144 *   0 = match.
 145 *   1 = no match.
 146 *   2 = no match. Would eventually match, if s would be longer.
 147 *
 148 * Possible Patterns:
 149 *
 150 * '?'     matches one character
 151 * '*'     matches zero or more characters
 152 * [xyz]   matches the set of characters in brackets.
 153 * [^xyz]  matches any single character not in the set of characters
 154 */
 155
 156static int
 157isdn_wildmat(char *s, char *p)
 158{
 159	register int last;
 160	register int matched;
 161	register int reverse;
 162	register int nostar = 1;
 163
 164	if (!(*s) && !(*p))
 165		return (1);
 166	for (; *p; s++, p++)
 167		switch (*p) {
 168		case '\\':
 169			/*
 170			 * Literal match with following character,
 171			 * fall through.
 172			 */
 173			p++;
 174		default:
 175			if (*s != *p)
 176				return (*s == '\0') ? 2 : 1;
 177					continue;
 178		case '?':
 179			/* Match anything. */
 180			if (*s == '\0')
 181				return (2);
 182			continue;
 183		case '*':
 184			nostar = 0;
 185			/* Trailing star matches everything. */
 186			return (*++p ? isdn_star(s, p) : 0);
 187		case '[':
 188			/* [^....] means inverse character class. */
 189			if ((reverse = (p[1] == '^')))
 190				p++;
 191			for (last = 0, matched = 0; *++p && (*p != ']'); last = *p)
 192				/* This next line requires a good C compiler. */
 193				if (*p == '-' ? *s <= *++p && *s >= last : *s == *p)
 194					matched = 1;
 195			if (matched == reverse)
 196				return (1);
 197			continue;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 198		}
 199	return (*s == '\0') ? 0 : nostar;
 200}
 201
 202int isdn_msncmp(const char *msn1, const char *msn2)
 203{
 204	char TmpMsn1[ISDN_MSNLEN];
 205	char TmpMsn2[ISDN_MSNLEN];
 206	char *p;
 207
 208	for (p = TmpMsn1; *msn1 && *msn1 != ':';)  // Strip off a SPID
 209		*p++ = *msn1++;
 210	*p = '\0';
 211
 212	for (p = TmpMsn2; *msn2 && *msn2 != ':';)  // Strip off a SPID
 213		*p++ = *msn2++;
 214	*p = '\0';
 215
 216	return isdn_wildmat(TmpMsn1, TmpMsn2);
 217}
 218
 219int
 220isdn_dc2minor(int di, int ch)
 221{
 222	int i;
 223	for (i = 0; i < ISDN_MAX_CHANNELS; i++)
 224		if (dev->chanmap[i] == ch && dev->drvmap[i] == di)
 225			return i;
 226	return -1;
 227}
 228
 229static int isdn_timer_cnt1 = 0;
 230static int isdn_timer_cnt2 = 0;
 231static int isdn_timer_cnt3 = 0;
 232
 233static void
 234isdn_timer_funct(ulong dummy)
 235{
 236	int tf = dev->tflags;
 237	if (tf & ISDN_TIMER_FAST) {
 238		if (tf & ISDN_TIMER_MODEMREAD)
 239			isdn_tty_readmodem();
 240		if (tf & ISDN_TIMER_MODEMPLUS)
 241			isdn_tty_modem_escape();
 242		if (tf & ISDN_TIMER_MODEMXMIT)
 243			isdn_tty_modem_xmit();
 244	}
 245	if (tf & ISDN_TIMER_SLOW) {
 246		if (++isdn_timer_cnt1 >= ISDN_TIMER_02SEC) {
 247			isdn_timer_cnt1 = 0;
 248			if (tf & ISDN_TIMER_NETDIAL)
 249				isdn_net_dial();
 250		}
 251		if (++isdn_timer_cnt2 >= ISDN_TIMER_1SEC) {
 252			isdn_timer_cnt2 = 0;
 253			if (tf & ISDN_TIMER_NETHANGUP)
 254				isdn_net_autohup();
 255			if (++isdn_timer_cnt3 >= ISDN_TIMER_RINGING) {
 256				isdn_timer_cnt3 = 0;
 257				if (tf & ISDN_TIMER_MODEMRING)
 258					isdn_tty_modem_ring();
 259			}
 260			if (tf & ISDN_TIMER_CARRIER)
 261				isdn_tty_carrier_timeout();
 262		}
 263	}
 264	if (tf)
 265		mod_timer(&dev->timer, jiffies + ISDN_TIMER_RES);
 266}
 267
 268void
 269isdn_timer_ctrl(int tf, int onoff)
 270{
 271	unsigned long flags;
 272	int old_tflags;
 273
 274	spin_lock_irqsave(&dev->timerlock, flags);
 275	if ((tf & ISDN_TIMER_SLOW) && (!(dev->tflags & ISDN_TIMER_SLOW))) {
 276		/* If the slow-timer wasn't activated until now */
 277		isdn_timer_cnt1 = 0;
 278		isdn_timer_cnt2 = 0;
 279	}
 280	old_tflags = dev->tflags;
 281	if (onoff)
 282		dev->tflags |= tf;
 283	else
 284		dev->tflags &= ~tf;
 285	if (dev->tflags && !old_tflags)
 286		mod_timer(&dev->timer, jiffies + ISDN_TIMER_RES);
 287	spin_unlock_irqrestore(&dev->timerlock, flags);
 288}
 289
 290/*
 291 * Receive a packet from B-Channel. (Called from low-level-module)
 292 */
 293static void
 294isdn_receive_skb_callback(int di, int channel, struct sk_buff *skb)
 295{
 296	int i;
 297
 298	if ((i = isdn_dc2minor(di, channel)) == -1) {
 299		dev_kfree_skb(skb);
 300		return;
 301	}
 302	/* Update statistics */
 303	dev->ibytes[i] += skb->len;
 304
 305	/* First, try to deliver data to network-device */
 306	if (isdn_net_rcv_skb(i, skb))
 307		return;
 308
 309	/* V.110 handling
 310	 * makes sense for async streams only, so it is
 311	 * called after possible net-device delivery.
 312	 */
 313	if (dev->v110[i]) {
 314		atomic_inc(&dev->v110use[i]);
 315		skb = isdn_v110_decode(dev->v110[i], skb);
 316		atomic_dec(&dev->v110use[i]);
 317		if (!skb)
 318			return;
 319	}
 320
 321	/* No network-device found, deliver to tty or raw-channel */
 322	if (skb->len) {
 323		if (isdn_tty_rcv_skb(i, di, channel, skb))
 324			return;
 325		wake_up_interruptible(&dev->drv[di]->rcv_waitq[channel]);
 326	} else
 327		dev_kfree_skb(skb);
 328}
 329
 330/*
 331 * Intercept command from Linklevel to Lowlevel.
 332 * If layer 2 protocol is V.110 and this is not supported by current
 333 * lowlevel-driver, use driver's transparent mode and handle V.110 in
 334 * linklevel instead.
 335 */
 336int
 337isdn_command(isdn_ctrl *cmd)
 338{
 339	if (cmd->driver == -1) {
 340		printk(KERN_WARNING "isdn_command command(%x) driver -1\n", cmd->command);
 341		return (1);
 342	}
 343	if (!dev->drv[cmd->driver]) {
 344		printk(KERN_WARNING "isdn_command command(%x) dev->drv[%d] NULL\n",
 345		       cmd->command, cmd->driver);
 346		return (1);
 347	}
 348	if (!dev->drv[cmd->driver]->interface) {
 349		printk(KERN_WARNING "isdn_command command(%x) dev->drv[%d]->interface NULL\n",
 350		       cmd->command, cmd->driver);
 351		return (1);
 352	}
 353	if (cmd->command == ISDN_CMD_SETL2) {
 354		int idx = isdn_dc2minor(cmd->driver, cmd->arg & 255);
 355		unsigned long l2prot = (cmd->arg >> 8) & 255;
 356		unsigned long features = (dev->drv[cmd->driver]->interface->features
 357					  >> ISDN_FEATURE_L2_SHIFT) &
 358			ISDN_FEATURE_L2_MASK;
 359		unsigned long l2_feature = (1 << l2prot);
 360
 361		switch (l2prot) {
 362		case ISDN_PROTO_L2_V11096:
 363		case ISDN_PROTO_L2_V11019:
 364		case ISDN_PROTO_L2_V11038:
 365			/* If V.110 requested, but not supported by
 366			 * HL-driver, set emulator-flag and change
 367			 * Layer-2 to transparent
 368			 */
 369			if (!(features & l2_feature)) {
 370				dev->v110emu[idx] = l2prot;
 371				cmd->arg = (cmd->arg & 255) |
 372					(ISDN_PROTO_L2_TRANS << 8);
 373			} else
 374				dev->v110emu[idx] = 0;
 375		}
 376	}
 377	return dev->drv[cmd->driver]->interface->command(cmd);
 378}
 379
 380void
 381isdn_all_eaz(int di, int ch)
 382{
 383	isdn_ctrl cmd;
 384
 385	if (di < 0)
 386		return;
 387	cmd.driver = di;
 388	cmd.arg = ch;
 389	cmd.command = ISDN_CMD_SETEAZ;
 390	cmd.parm.num[0] = '\0';
 391	isdn_command(&cmd);
 392}
 393
 394/*
 395 * Begin of a CAPI like LL<->HL interface, currently used only for
 396 * supplementary service (CAPI 2.0 part III)
 397 */
 398#include <linux/isdn/capicmd.h>
 399
 400static int
 401isdn_capi_rec_hl_msg(capi_msg *cm)
 402{
 403	switch (cm->Command) {
 404	case CAPI_FACILITY:
 405		/* in the moment only handled in tty */
 406		return (isdn_tty_capi_facility(cm));
 407	default:
 408		return (-1);
 409	}
 410}
 411
 412static int
 413isdn_status_callback(isdn_ctrl *c)
 414{
 415	int di;
 416	u_long flags;
 417	int i;
 418	int r;
 419	int retval = 0;
 420	isdn_ctrl cmd;
 421	isdn_net_dev *p;
 422
 423	di = c->driver;
 424	i = isdn_dc2minor(di, c->arg);
 425	switch (c->command) {
 426	case ISDN_STAT_BSENT:
 427		if (i < 0)
 428			return -1;
 429		if (dev->global_flags & ISDN_GLOBAL_STOPPED)
 430			return 0;
 431		if (isdn_net_stat_callback(i, c))
 432			return 0;
 433		if (isdn_v110_stat_callback(i, c))
 434			return 0;
 435		if (isdn_tty_stat_callback(i, c))
 436			return 0;
 437		wake_up_interruptible(&dev->drv[di]->snd_waitq[c->arg]);
 438		break;
 439	case ISDN_STAT_STAVAIL:
 440		dev->drv[di]->stavail += c->arg;
 441		wake_up_interruptible(&dev->drv[di]->st_waitq);
 442		break;
 443	case ISDN_STAT_RUN:
 444		dev->drv[di]->flags |= DRV_FLAG_RUNNING;
 445		for (i = 0; i < ISDN_MAX_CHANNELS; i++)
 446			if (dev->drvmap[i] == di)
 447				isdn_all_eaz(di, dev->chanmap[i]);
 448		set_global_features();
 449		break;
 450	case ISDN_STAT_STOP:
 451		dev->drv[di]->flags &= ~DRV_FLAG_RUNNING;
 452		break;
 453	case ISDN_STAT_ICALL:
 454		if (i < 0)
 455			return -1;
 456#ifdef ISDN_DEBUG_STATCALLB
 457		printk(KERN_DEBUG "ICALL (net): %d %ld %s\n", di, c->arg, c->parm.num);
 458#endif
 459		if (dev->global_flags & ISDN_GLOBAL_STOPPED) {
 460			cmd.driver = di;
 461			cmd.arg = c->arg;
 462			cmd.command = ISDN_CMD_HANGUP;
 463			isdn_command(&cmd);
 464			return 0;
 465		}
 466		/* Try to find a network-interface which will accept incoming call */
 467		r = ((c->command == ISDN_STAT_ICALLW) ? 0 : isdn_net_find_icall(di, c->arg, i, &c->parm.setup));
 468		switch (r) {
 469		case 0:
 470			/* No network-device replies.
 471			 * Try ttyI's.
 472			 * These return 0 on no match, 1 on match and
 473			 * 3 on eventually match, if CID is longer.
 474			 */
 475			if (c->command == ISDN_STAT_ICALL)
 476				if ((retval = isdn_tty_find_icall(di, c->arg, &c->parm.setup))) return (retval);
 477#ifdef CONFIG_ISDN_DIVERSION
 478			if (divert_if)
 479				if ((retval = divert_if->stat_callback(c)))
 480					return (retval); /* processed */
 481#endif /* CONFIG_ISDN_DIVERSION */
 482			if ((!retval) && (dev->drv[di]->flags & DRV_FLAG_REJBUS)) {
 483				/* No tty responding */
 484				cmd.driver = di;
 485				cmd.arg = c->arg;
 486				cmd.command = ISDN_CMD_HANGUP;
 487				isdn_command(&cmd);
 488				retval = 2;
 489			}
 490			break;
 491		case 1:
 492			/* Schedule connection-setup */
 493			isdn_net_dial();
 494			cmd.driver = di;
 495			cmd.arg = c->arg;
 496			cmd.command = ISDN_CMD_ACCEPTD;
 497			for (p = dev->netdev; p; p = p->next)
 498				if (p->local->isdn_channel == cmd.arg)
 499				{
 500					strcpy(cmd.parm.setup.eazmsn, p->local->msn);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 501					isdn_command(&cmd);
 502					retval = 1;
 
 
 
 
 
 
 
 
 
 503					break;
 504				}
 505			break;
 506
 507		case 2:	/* For calling back, first reject incoming call ... */
 508		case 3:	/* Interface found, but down, reject call actively  */
 509			retval = 2;
 510			printk(KERN_INFO "isdn: Rejecting Call\n");
 511			cmd.driver = di;
 512			cmd.arg = c->arg;
 513			cmd.command = ISDN_CMD_HANGUP;
 514			isdn_command(&cmd);
 515			if (r == 3)
 516				break;
 517			/* Fall through */
 518		case 4:
 519			/* ... then start callback. */
 520			isdn_net_dial();
 521			break;
 522		case 5:
 523			/* Number would eventually match, if longer */
 524			retval = 3;
 525			break;
 526		}
 527#ifdef ISDN_DEBUG_STATCALLB
 528		printk(KERN_DEBUG "ICALL: ret=%d\n", retval);
 529#endif
 530		return retval;
 531		break;
 532	case ISDN_STAT_CINF:
 533		if (i < 0)
 534			return -1;
 535#ifdef ISDN_DEBUG_STATCALLB
 536		printk(KERN_DEBUG "CINF: %ld %s\n", c->arg, c->parm.num);
 537#endif
 538		if (dev->global_flags & ISDN_GLOBAL_STOPPED)
 539			return 0;
 540		if (strcmp(c->parm.num, "0"))
 541			isdn_net_stat_callback(i, c);
 542		isdn_tty_stat_callback(i, c);
 543		break;
 544	case ISDN_STAT_CAUSE:
 545#ifdef ISDN_DEBUG_STATCALLB
 546		printk(KERN_DEBUG "CAUSE: %ld %s\n", c->arg, c->parm.num);
 547#endif
 548		printk(KERN_INFO "isdn: %s,ch%ld cause: %s\n",
 549		       dev->drvid[di], c->arg, c->parm.num);
 550		isdn_tty_stat_callback(i, c);
 551#ifdef CONFIG_ISDN_DIVERSION
 552		if (divert_if)
 553			divert_if->stat_callback(c);
 554#endif /* CONFIG_ISDN_DIVERSION */
 555		break;
 556	case ISDN_STAT_DISPLAY:
 557#ifdef ISDN_DEBUG_STATCALLB
 558		printk(KERN_DEBUG "DISPLAY: %ld %s\n", c->arg, c->parm.display);
 559#endif
 560		isdn_tty_stat_callback(i, c);
 561#ifdef CONFIG_ISDN_DIVERSION
 562		if (divert_if)
 563			divert_if->stat_callback(c);
 564#endif /* CONFIG_ISDN_DIVERSION */
 565		break;
 566	case ISDN_STAT_DCONN:
 567		if (i < 0)
 568			return -1;
 569#ifdef ISDN_DEBUG_STATCALLB
 570		printk(KERN_DEBUG "DCONN: %ld\n", c->arg);
 571#endif
 572		if (dev->global_flags & ISDN_GLOBAL_STOPPED)
 573			return 0;
 574		/* Find any net-device, waiting for D-channel setup */
 575		if (isdn_net_stat_callback(i, c))
 576			break;
 577		isdn_v110_stat_callback(i, c);
 578		/* Find any ttyI, waiting for D-channel setup */
 579		if (isdn_tty_stat_callback(i, c)) {
 580			cmd.driver = di;
 581			cmd.arg = c->arg;
 582			cmd.command = ISDN_CMD_ACCEPTB;
 583			isdn_command(&cmd);
 
 
 584			break;
 585		}
 586		break;
 587	case ISDN_STAT_DHUP:
 588		if (i < 0)
 589			return -1;
 590#ifdef ISDN_DEBUG_STATCALLB
 591		printk(KERN_DEBUG "DHUP: %ld\n", c->arg);
 592#endif
 593		if (dev->global_flags & ISDN_GLOBAL_STOPPED)
 594			return 0;
 595		dev->drv[di]->online &= ~(1 << (c->arg));
 596		isdn_info_update();
 597		/* Signal hangup to network-devices */
 598		if (isdn_net_stat_callback(i, c))
 
 
 
 
 
 
 
 
 599			break;
 600		isdn_v110_stat_callback(i, c);
 601		if (isdn_tty_stat_callback(i, c))
 602			break;
 603#ifdef CONFIG_ISDN_DIVERSION
 604		if (divert_if)
 605			divert_if->stat_callback(c);
 606#endif /* CONFIG_ISDN_DIVERSION */
 607		break;
 608		break;
 609	case ISDN_STAT_BCONN:
 610		if (i < 0)
 611			return -1;
 612#ifdef ISDN_DEBUG_STATCALLB
 613		printk(KERN_DEBUG "BCONN: %ld\n", c->arg);
 614#endif
 615		/* Signal B-channel-connect to network-devices */
 616		if (dev->global_flags & ISDN_GLOBAL_STOPPED)
 617			return 0;
 618		dev->drv[di]->online |= (1 << (c->arg));
 619		isdn_info_update();
 620		if (isdn_net_stat_callback(i, c))
 621			break;
 622		isdn_v110_stat_callback(i, c);
 623		if (isdn_tty_stat_callback(i, c))
 624			break;
 625		break;
 626	case ISDN_STAT_BHUP:
 627		if (i < 0)
 628			return -1;
 629#ifdef ISDN_DEBUG_STATCALLB
 630		printk(KERN_DEBUG "BHUP: %ld\n", c->arg);
 631#endif
 632		if (dev->global_flags & ISDN_GLOBAL_STOPPED)
 633			return 0;
 634		dev->drv[di]->online &= ~(1 << (c->arg));
 635		isdn_info_update();
 636#ifdef CONFIG_ISDN_X25
 637		/* Signal hangup to network-devices */
 638		if (isdn_net_stat_callback(i, c))
 639			break;
 640#endif
 641		isdn_v110_stat_callback(i, c);
 642		if (isdn_tty_stat_callback(i, c))
 
 643			break;
 644		break;
 645	case ISDN_STAT_NODCH:
 646		if (i < 0)
 647			return -1;
 648#ifdef ISDN_DEBUG_STATCALLB
 649		printk(KERN_DEBUG "NODCH: %ld\n", c->arg);
 650#endif
 651		if (dev->global_flags & ISDN_GLOBAL_STOPPED)
 652			return 0;
 653		if (isdn_net_stat_callback(i, c))
 
 
 
 654			break;
 655		if (isdn_tty_stat_callback(i, c))
 
 
 
 
 
 
 
 656			break;
 657		break;
 658	case ISDN_STAT_ADDCH:
 659		spin_lock_irqsave(&dev->lock, flags);
 660		if (isdn_add_channels(dev->drv[di], di, c->arg, 1)) {
 
 
 
 
 
 
 
 
 
 
 
 661			spin_unlock_irqrestore(&dev->lock, flags);
 662			return -1;
 663		}
 664		spin_unlock_irqrestore(&dev->lock, flags);
 665		isdn_info_update();
 666		break;
 667	case ISDN_STAT_DISCH:
 668		spin_lock_irqsave(&dev->lock, flags);
 669		for (i = 0; i < ISDN_MAX_CHANNELS; i++)
 670			if ((dev->drvmap[i] == di) &&
 671			    (dev->chanmap[i] == c->arg)) {
 672				if (c->parm.num[0])
 
 673					dev->usage[i] &= ~ISDN_USAGE_DISABLED;
 674				else
 675					if (USG_NONE(dev->usage[i])) {
 676						dev->usage[i] |= ISDN_USAGE_DISABLED;
 677					}
 678					else
 679						retval = -1;
 680				break;
 681			}
 682		spin_unlock_irqrestore(&dev->lock, flags);
 683		isdn_info_update();
 684		break;
 685	case ISDN_STAT_UNLOAD:
 686		while (dev->drv[di]->locks > 0) {
 687			isdn_unlock_driver(dev->drv[di]);
 688		}
 689		spin_lock_irqsave(&dev->lock, flags);
 690		isdn_tty_stat_callback(i, c);
 691		for (i = 0; i < ISDN_MAX_CHANNELS; i++)
 692			if (dev->drvmap[i] == di) {
 693				dev->drvmap[i] = -1;
 694				dev->chanmap[i] = -1;
 695				dev->usage[i] &= ~ISDN_USAGE_DISABLED;
 696			}
 697		dev->drivers--;
 698		dev->channels -= dev->drv[di]->channels;
 699		kfree(dev->drv[di]->rcverr);
 700		kfree(dev->drv[di]->rcvcount);
 701		for (i = 0; i < dev->drv[di]->channels; i++)
 702			skb_queue_purge(&dev->drv[di]->rpqueue[i]);
 703		kfree(dev->drv[di]->rpqueue);
 704		kfree(dev->drv[di]->rcv_waitq);
 705		kfree(dev->drv[di]);
 706		dev->drv[di] = NULL;
 707		dev->drvid[di][0] = '\0';
 708		isdn_info_update();
 709		set_global_features();
 710		spin_unlock_irqrestore(&dev->lock, flags);
 711		return 0;
 712	case ISDN_STAT_L1ERR:
 713		break;
 714	case CAPI_PUT_MESSAGE:
 715		return (isdn_capi_rec_hl_msg(&c->parm.cmsg));
 716#ifdef CONFIG_ISDN_TTY_FAX
 717	case ISDN_STAT_FAXIND:
 718		isdn_tty_stat_callback(i, c);
 719		break;
 720#endif
 721#ifdef CONFIG_ISDN_AUDIO
 722	case ISDN_STAT_AUDIO:
 723		isdn_tty_stat_callback(i, c);
 724		break;
 725#endif
 726#ifdef CONFIG_ISDN_DIVERSION
 727	case ISDN_STAT_PROT:
 728	case ISDN_STAT_REDIR:
 729		if (divert_if)
 730			return (divert_if->stat_callback(c));
 731#endif /* CONFIG_ISDN_DIVERSION */
 732	default:
 733		return -1;
 734	}
 735	return 0;
 736}
 737
 738/*
 739 * Get integer from char-pointer, set pointer to end of number
 740 */
 741int
 742isdn_getnum(char **p)
 743{
 744	int v = -1;
 745
 746	while (*p[0] >= '0' && *p[0] <= '9')
 747		v = ((v < 0) ? 0 : (v * 10)) + (int) ((*p[0]++) - '0');
 748	return v;
 749}
 750
 751#define DLE 0x10
 752
 753/*
 754 * isdn_readbchan() tries to get data from the read-queue.
 755 * It MUST be called with interrupts off.
 756 *
 757 * Be aware that this is not an atomic operation when sleep != 0, even though
 758 * interrupts are turned off! Well, like that we are currently only called
 759 * on behalf of a read system call on raw device files (which are documented
 760 * to be dangerous and for debugging purpose only). The inode semaphore
 761 * takes care that this is not called for the same minor device number while
 762 * we are sleeping, but access is not serialized against simultaneous read()
 763 * from the corresponding ttyI device. Can other ugly events, like changes
 764 * of the mapping (di,ch)<->minor, happen during the sleep? --he
 765 */
 766int
 767isdn_readbchan(int di, int channel, u_char *buf, u_char *fp, int len, wait_queue_head_t *sleep)
 768{
 769	int count;
 770	int count_pull;
 771	int count_put;
 772	int dflag;
 773	struct sk_buff *skb;
 774	u_char *cp;
 775
 776	if (!dev->drv[di])
 777		return 0;
 778	if (skb_queue_empty(&dev->drv[di]->rpqueue[channel])) {
 779		if (sleep)
 780			wait_event_interruptible(*sleep,
 781				!skb_queue_empty(&dev->drv[di]->rpqueue[channel]));
 782		else
 783			return 0;
 784	}
 785	if (len > dev->drv[di]->rcvcount[channel])
 786		len = dev->drv[di]->rcvcount[channel];
 787	cp = buf;
 788	count = 0;
 789	while (len) {
 790		if (!(skb = skb_peek(&dev->drv[di]->rpqueue[channel])))
 791			break;
 792#ifdef CONFIG_ISDN_AUDIO
 793		if (ISDN_AUDIO_SKB_LOCK(skb))
 794			break;
 795		ISDN_AUDIO_SKB_LOCK(skb) = 1;
 796		if ((ISDN_AUDIO_SKB_DLECOUNT(skb)) || (dev->drv[di]->DLEflag & (1 << channel))) {
 797			char *p = skb->data;
 798			unsigned long DLEmask = (1 << channel);
 799
 800			dflag = 0;
 801			count_pull = count_put = 0;
 802			while ((count_pull < skb->len) && (len > 0)) {
 803				len--;
 804				if (dev->drv[di]->DLEflag & DLEmask) {
 805					*cp++ = DLE;
 806					dev->drv[di]->DLEflag &= ~DLEmask;
 807				} else {
 808					*cp++ = *p;
 809					if (*p == DLE) {
 810						dev->drv[di]->DLEflag |= DLEmask;
 811						(ISDN_AUDIO_SKB_DLECOUNT(skb))--;
 812					}
 813					p++;
 814					count_pull++;
 815				}
 816				count_put++;
 817			}
 818			if (count_pull >= skb->len)
 819				dflag = 1;
 820		} else {
 821#endif
 822			/* No DLE's in buff, so simply copy it */
 823			dflag = 1;
 824			if ((count_pull = skb->len) > len) {
 825				count_pull = len;
 826				dflag = 0;
 827			}
 828			count_put = count_pull;
 829			skb_copy_from_linear_data(skb, cp, count_put);
 830			cp += count_put;
 831			len -= count_put;
 832#ifdef CONFIG_ISDN_AUDIO
 833		}
 834#endif
 835		count += count_put;
 836		if (fp) {
 837			memset(fp, 0, count_put);
 838			fp += count_put;
 839		}
 840		if (dflag) {
 841			/* We got all the data in this buff.
 842			 * Now we can dequeue it.
 843			 */
 844			if (fp)
 845				*(fp - 1) = 0xff;
 846#ifdef CONFIG_ISDN_AUDIO
 847			ISDN_AUDIO_SKB_LOCK(skb) = 0;
 848#endif
 849			skb = skb_dequeue(&dev->drv[di]->rpqueue[channel]);
 850			dev_kfree_skb(skb);
 851		} else {
 852			/* Not yet emptied this buff, so it
 853			 * must stay in the queue, for further calls
 854			 * but we pull off the data we got until now.
 855			 */
 856			skb_pull(skb, count_pull);
 857#ifdef CONFIG_ISDN_AUDIO
 858			ISDN_AUDIO_SKB_LOCK(skb) = 0;
 859#endif
 860		}
 861		dev->drv[di]->rcvcount[channel] -= count_put;
 862	}
 863	return count;
 864}
 865
 866/*
 867 * isdn_readbchan_tty() tries to get data from the read-queue.
 868 * It MUST be called with interrupts off.
 869 *
 870 * Be aware that this is not an atomic operation when sleep != 0, even though
 871 * interrupts are turned off! Well, like that we are currently only called
 872 * on behalf of a read system call on raw device files (which are documented
 873 * to be dangerous and for debugging purpose only). The inode semaphore
 874 * takes care that this is not called for the same minor device number while
 875 * we are sleeping, but access is not serialized against simultaneous read()
 876 * from the corresponding ttyI device. Can other ugly events, like changes
 877 * of the mapping (di,ch)<->minor, happen during the sleep? --he
 878 */
 879int
 880isdn_readbchan_tty(int di, int channel, struct tty_port *port, int cisco_hack)
 881{
 882	int count;
 883	int count_pull;
 884	int count_put;
 885	int dflag;
 886	struct sk_buff *skb;
 887	char last = 0;
 888	int len;
 889
 890	if (!dev->drv[di])
 891		return 0;
 892	if (skb_queue_empty(&dev->drv[di]->rpqueue[channel]))
 893		return 0;
 894
 895	len = tty_buffer_request_room(port, dev->drv[di]->rcvcount[channel]);
 896	if (len == 0)
 897		return len;
 898
 899	count = 0;
 900	while (len) {
 901		if (!(skb = skb_peek(&dev->drv[di]->rpqueue[channel])))
 902			break;
 903#ifdef CONFIG_ISDN_AUDIO
 904		if (ISDN_AUDIO_SKB_LOCK(skb))
 905			break;
 906		ISDN_AUDIO_SKB_LOCK(skb) = 1;
 907		if ((ISDN_AUDIO_SKB_DLECOUNT(skb)) || (dev->drv[di]->DLEflag & (1 << channel))) {
 908			char *p = skb->data;
 909			unsigned long DLEmask = (1 << channel);
 910
 911			dflag = 0;
 912			count_pull = count_put = 0;
 913			while ((count_pull < skb->len) && (len > 0)) {
 914				/* push every character but the last to the tty buffer directly */
 915				if (count_put)
 916					tty_insert_flip_char(port, last, TTY_NORMAL);
 917				len--;
 918				if (dev->drv[di]->DLEflag & DLEmask) {
 919					last = DLE;
 920					dev->drv[di]->DLEflag &= ~DLEmask;
 921				} else {
 922					last = *p;
 923					if (last == DLE) {
 924						dev->drv[di]->DLEflag |= DLEmask;
 925						(ISDN_AUDIO_SKB_DLECOUNT(skb))--;
 926					}
 927					p++;
 928					count_pull++;
 929				}
 930				count_put++;
 931			}
 932			if (count_pull >= skb->len)
 933				dflag = 1;
 934		} else {
 935#endif
 936			/* No DLE's in buff, so simply copy it */
 937			dflag = 1;
 938			if ((count_pull = skb->len) > len) {
 939				count_pull = len;
 940				dflag = 0;
 941			}
 942			count_put = count_pull;
 943			if (count_put > 1)
 944				tty_insert_flip_string(port, skb->data, count_put - 1);
 945			last = skb->data[count_put - 1];
 946			len -= count_put;
 947#ifdef CONFIG_ISDN_AUDIO
 948		}
 949#endif
 950		count += count_put;
 951		if (dflag) {
 952			/* We got all the data in this buff.
 953			 * Now we can dequeue it.
 954			 */
 955			if (cisco_hack)
 956				tty_insert_flip_char(port, last, 0xFF);
 957			else
 958				tty_insert_flip_char(port, last, TTY_NORMAL);
 959#ifdef CONFIG_ISDN_AUDIO
 960			ISDN_AUDIO_SKB_LOCK(skb) = 0;
 961#endif
 962			skb = skb_dequeue(&dev->drv[di]->rpqueue[channel]);
 963			dev_kfree_skb(skb);
 964		} else {
 965			tty_insert_flip_char(port, last, TTY_NORMAL);
 966			/* Not yet emptied this buff, so it
 967			 * must stay in the queue, for further calls
 968			 * but we pull off the data we got until now.
 969			 */
 970			skb_pull(skb, count_pull);
 971#ifdef CONFIG_ISDN_AUDIO
 972			ISDN_AUDIO_SKB_LOCK(skb) = 0;
 973#endif
 974		}
 975		dev->drv[di]->rcvcount[channel] -= count_put;
 976	}
 977	return count;
 978}
 979
 980
 981static inline int
 982isdn_minor2drv(int minor)
 983{
 984	return (dev->drvmap[minor]);
 985}
 986
 987static inline int
 988isdn_minor2chan(int minor)
 989{
 990	return (dev->chanmap[minor]);
 991}
 992
 993static char *
 994isdn_statstr(void)
 995{
 996	static char istatbuf[2048];
 997	char *p;
 998	int i;
 999
1000	sprintf(istatbuf, "idmap:\t");
1001	p = istatbuf + strlen(istatbuf);
1002	for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
1003		sprintf(p, "%s ", (dev->drvmap[i] < 0) ? "-" : dev->drvid[dev->drvmap[i]]);
1004		p = istatbuf + strlen(istatbuf);
1005	}
1006	sprintf(p, "\nchmap:\t");
1007	p = istatbuf + strlen(istatbuf);
1008	for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
1009		sprintf(p, "%d ", dev->chanmap[i]);
1010		p = istatbuf + strlen(istatbuf);
1011	}
1012	sprintf(p, "\ndrmap:\t");
1013	p = istatbuf + strlen(istatbuf);
1014	for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
1015		sprintf(p, "%d ", dev->drvmap[i]);
1016		p = istatbuf + strlen(istatbuf);
1017	}
1018	sprintf(p, "\nusage:\t");
1019	p = istatbuf + strlen(istatbuf);
1020	for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
1021		sprintf(p, "%d ", dev->usage[i]);
1022		p = istatbuf + strlen(istatbuf);
1023	}
1024	sprintf(p, "\nflags:\t");
1025	p = istatbuf + strlen(istatbuf);
1026	for (i = 0; i < ISDN_MAX_DRIVERS; i++) {
1027		if (dev->drv[i]) {
1028			sprintf(p, "%ld ", dev->drv[i]->online);
1029			p = istatbuf + strlen(istatbuf);
1030		} else {
1031			sprintf(p, "? ");
1032			p = istatbuf + strlen(istatbuf);
1033		}
1034	}
1035	sprintf(p, "\nphone:\t");
1036	p = istatbuf + strlen(istatbuf);
1037	for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
1038		sprintf(p, "%s ", dev->num[i]);
1039		p = istatbuf + strlen(istatbuf);
1040	}
1041	sprintf(p, "\n");
1042	return istatbuf;
1043}
1044
1045/* Module interface-code */
1046
1047void
1048isdn_info_update(void)
1049{
1050	infostruct *p = dev->infochain;
1051
1052	while (p) {
1053		*(p->private) = 1;
1054		p = (infostruct *) p->next;
1055	}
1056	wake_up_interruptible(&(dev->info_waitq));
1057}
1058
1059static ssize_t
1060isdn_read(struct file *file, char __user *buf, size_t count, loff_t *off)
1061{
1062	uint minor = iminor(file_inode(file));
1063	int len = 0;
1064	int drvidx;
1065	int chidx;
1066	int retval;
1067	char *p;
1068
1069	mutex_lock(&isdn_mutex);
1070	if (minor == ISDN_MINOR_STATUS) {
1071		if (!file->private_data) {
1072			if (file->f_flags & O_NONBLOCK) {
1073				retval = -EAGAIN;
1074				goto out;
1075			}
1076			wait_event_interruptible(dev->info_waitq,
1077						 file->private_data);
1078		}
1079		p = isdn_statstr();
1080		file->private_data = NULL;
1081		if ((len = strlen(p)) <= count) {
1082			if (copy_to_user(buf, p, len)) {
1083				retval = -EFAULT;
1084				goto out;
1085			}
1086			*off += len;
1087			retval = len;
1088			goto out;
1089		}
1090		retval = 0;
1091		goto out;
1092	}
1093	if (!dev->drivers) {
1094		retval = -ENODEV;
1095		goto out;
1096	}
1097	if (minor <= ISDN_MINOR_BMAX) {
1098		printk(KERN_WARNING "isdn_read minor %d obsolete!\n", minor);
1099		drvidx = isdn_minor2drv(minor);
1100		if (drvidx < 0) {
1101			retval = -ENODEV;
1102			goto out;
1103		}
1104		if (!(dev->drv[drvidx]->flags & DRV_FLAG_RUNNING)) {
1105			retval = -ENODEV;
1106			goto out;
1107		}
1108		chidx = isdn_minor2chan(minor);
1109		if (!(p = kmalloc(count, GFP_KERNEL))) {
1110			retval = -ENOMEM;
1111			goto out;
1112		}
1113		len = isdn_readbchan(drvidx, chidx, p, NULL, count,
1114				     &dev->drv[drvidx]->rcv_waitq[chidx]);
1115		*off += len;
1116		if (copy_to_user(buf, p, len))
1117			len = -EFAULT;
1118		kfree(p);
1119		retval = len;
1120		goto out;
1121	}
1122	if (minor <= ISDN_MINOR_CTRLMAX) {
1123		drvidx = isdn_minor2drv(minor - ISDN_MINOR_CTRL);
1124		if (drvidx < 0) {
1125			retval = -ENODEV;
1126			goto out;
1127		}
1128		if (!dev->drv[drvidx]->stavail) {
1129			if (file->f_flags & O_NONBLOCK) {
1130				retval = -EAGAIN;
1131				goto out;
1132			}
1133			wait_event_interruptible(dev->drv[drvidx]->st_waitq,
1134						 dev->drv[drvidx]->stavail);
1135		}
1136		if (dev->drv[drvidx]->interface->readstat) {
1137			if (count > dev->drv[drvidx]->stavail)
1138				count = dev->drv[drvidx]->stavail;
1139			len = dev->drv[drvidx]->interface->readstat(buf, count,
1140								    drvidx, isdn_minor2chan(minor - ISDN_MINOR_CTRL));
1141			if (len < 0) {
1142				retval = len;
1143				goto out;
1144			}
1145		} else {
1146			len = 0;
1147		}
1148		if (len)
1149			dev->drv[drvidx]->stavail -= len;
1150		else
1151			dev->drv[drvidx]->stavail = 0;
1152		*off += len;
1153		retval = len;
1154		goto out;
1155	}
1156#ifdef CONFIG_ISDN_PPP
1157	if (minor <= ISDN_MINOR_PPPMAX) {
1158		retval = isdn_ppp_read(minor - ISDN_MINOR_PPP, file, buf, count);
1159		goto out;
1160	}
1161#endif
1162	retval = -ENODEV;
1163out:
1164	mutex_unlock(&isdn_mutex);
1165	return retval;
1166}
1167
1168static ssize_t
1169isdn_write(struct file *file, const char __user *buf, size_t count, loff_t *off)
1170{
1171	uint minor = iminor(file_inode(file));
1172	int drvidx;
1173	int chidx;
1174	int retval;
1175
1176	if (minor == ISDN_MINOR_STATUS)
1177		return -EPERM;
1178	if (!dev->drivers)
1179		return -ENODEV;
1180
1181	mutex_lock(&isdn_mutex);
1182	if (minor <= ISDN_MINOR_BMAX) {
1183		printk(KERN_WARNING "isdn_write minor %d obsolete!\n", minor);
1184		drvidx = isdn_minor2drv(minor);
1185		if (drvidx < 0) {
1186			retval = -ENODEV;
1187			goto out;
1188		}
1189		if (!(dev->drv[drvidx]->flags & DRV_FLAG_RUNNING)) {
1190			retval = -ENODEV;
1191			goto out;
1192		}
1193		chidx = isdn_minor2chan(minor);
1194		wait_event_interruptible(dev->drv[drvidx]->snd_waitq[chidx],
1195			(retval = isdn_writebuf_stub(drvidx, chidx, buf, count)));
1196		goto out;
1197	}
1198	if (minor <= ISDN_MINOR_CTRLMAX) {
1199		drvidx = isdn_minor2drv(minor - ISDN_MINOR_CTRL);
1200		if (drvidx < 0) {
1201			retval = -ENODEV;
1202			goto out;
1203		}
1204		/*
1205		 * We want to use the isdnctrl device to load the firmware
1206		 *
1207		 if (!(dev->drv[drvidx]->flags & DRV_FLAG_RUNNING))
1208		 return -ENODEV;
1209		*/
1210		if (dev->drv[drvidx]->interface->writecmd)
1211			retval = dev->drv[drvidx]->interface->
1212				writecmd(buf, count, drvidx,
1213					 isdn_minor2chan(minor - ISDN_MINOR_CTRL));
1214		else
1215			retval = count;
1216		goto out;
1217	}
1218#ifdef CONFIG_ISDN_PPP
1219	if (minor <= ISDN_MINOR_PPPMAX) {
1220		retval = isdn_ppp_write(minor - ISDN_MINOR_PPP, file, buf, count);
1221		goto out;
1222	}
1223#endif
1224	retval = -ENODEV;
1225out:
1226	mutex_unlock(&isdn_mutex);
1227	return retval;
1228}
1229
1230static unsigned int
1231isdn_poll(struct file *file, poll_table *wait)
1232{
1233	unsigned int mask = 0;
1234	unsigned int minor = iminor(file_inode(file));
1235	int drvidx = isdn_minor2drv(minor - ISDN_MINOR_CTRL);
1236
1237	mutex_lock(&isdn_mutex);
1238	if (minor == ISDN_MINOR_STATUS) {
1239		poll_wait(file, &(dev->info_waitq), wait);
1240		/* mask = POLLOUT | POLLWRNORM; */
1241		if (file->private_data) {
1242			mask |= POLLIN | POLLRDNORM;
1243		}
1244		goto out;
1245	}
1246	if (minor >= ISDN_MINOR_CTRL && minor <= ISDN_MINOR_CTRLMAX) {
1247		if (drvidx < 0) {
1248			/* driver deregistered while file open */
1249			mask = POLLHUP;
1250			goto out;
1251		}
1252		poll_wait(file, &(dev->drv[drvidx]->st_waitq), wait);
1253		mask = POLLOUT | POLLWRNORM;
1254		if (dev->drv[drvidx]->stavail) {
1255			mask |= POLLIN | POLLRDNORM;
1256		}
1257		goto out;
1258	}
1259#ifdef CONFIG_ISDN_PPP
1260	if (minor <= ISDN_MINOR_PPPMAX) {
1261		mask = isdn_ppp_poll(file, wait);
1262		goto out;
1263	}
1264#endif
1265	mask = POLLERR;
1266out:
1267	mutex_unlock(&isdn_mutex);
1268	return mask;
1269}
1270
1271
1272static int
1273isdn_ioctl(struct file *file, uint cmd, ulong arg)
1274{
1275	uint minor = iminor(file_inode(file));
1276	isdn_ctrl c;
1277	int drvidx;
1278	int ret;
1279	int i;
1280	char __user *p;
1281	char *s;
1282	union iocpar {
1283		char name[10];
1284		char bname[22];
1285		isdn_ioctl_struct iocts;
1286		isdn_net_ioctl_phone phone;
1287		isdn_net_ioctl_cfg cfg;
1288	} iocpar;
1289	void __user *argp = (void __user *)arg;
1290
1291#define name  iocpar.name
1292#define bname iocpar.bname
1293#define iocts iocpar.iocts
1294#define phone iocpar.phone
1295#define cfg   iocpar.cfg
1296
1297	if (minor == ISDN_MINOR_STATUS) {
1298		switch (cmd) {
1299		case IIOCGETDVR:
1300			return (TTY_DV +
1301				(NET_DV << 8) +
1302				(INF_DV << 16));
1303		case IIOCGETCPS:
1304			if (arg) {
1305				ulong __user *p = argp;
1306				int i;
1307				if (!access_ok(VERIFY_WRITE, p,
1308					       sizeof(ulong) * ISDN_MAX_CHANNELS * 2))
1309					return -EFAULT;
1310				for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
1311					put_user(dev->ibytes[i], p++);
1312					put_user(dev->obytes[i], p++);
1313				}
1314				return 0;
1315			} else
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1316				return -EINVAL;
1317			break;
1318		case IIOCNETGPN:
1319			/* Get peer phone number of a connected
1320			 * isdn network interface */
1321			if (arg) {
1322				if (copy_from_user(&phone, argp, sizeof(phone)))
1323					return -EFAULT;
1324				return isdn_net_getpeer(&phone, argp);
1325			} else
1326				return -EINVAL;
1327		default:
1328			return -EINVAL;
1329		}
1330	}
1331	if (!dev->drivers)
1332		return -ENODEV;
1333	if (minor <= ISDN_MINOR_BMAX) {
1334		drvidx = isdn_minor2drv(minor);
1335		if (drvidx < 0)
1336			return -ENODEV;
1337		if (!(dev->drv[drvidx]->flags & DRV_FLAG_RUNNING))
1338			return -ENODEV;
1339		return 0;
1340	}
1341	if (minor <= ISDN_MINOR_CTRLMAX) {
1342/*
1343 * isdn net devices manage lots of configuration variables as linked lists.
1344 * Those lists must only be manipulated from user space. Some of the ioctl's
1345 * service routines access user space and are not atomic. Therefore, ioctl's
1346 * manipulating the lists and ioctl's sleeping while accessing the lists
1347 * are serialized by means of a semaphore.
1348 */
1349		switch (cmd) {
1350		case IIOCNETDWRSET:
1351			printk(KERN_INFO "INFO: ISDN_DW_ABC_EXTENSION not enabled\n");
1352			return (-EINVAL);
1353		case IIOCNETLCR:
1354			printk(KERN_INFO "INFO: ISDN_ABC_LCR_SUPPORT not enabled\n");
1355			return -ENODEV;
1356		case IIOCNETAIF:
1357			/* Add a network-interface */
1358			if (arg) {
1359				if (copy_from_user(name, argp, sizeof(name)))
1360					return -EFAULT;
1361				s = name;
1362			} else {
1363				s = NULL;
1364			}
1365			ret = mutex_lock_interruptible(&dev->mtx);
1366			if (ret) return ret;
1367			if ((s = isdn_net_new(s, NULL))) {
1368				if (copy_to_user(argp, s, strlen(s) + 1)) {
1369					ret = -EFAULT;
1370				} else {
1371					ret = 0;
1372				}
1373			} else
1374				ret = -ENODEV;
1375			mutex_unlock(&dev->mtx);
1376			return ret;
1377		case IIOCNETASL:
1378			/* Add a slave to a network-interface */
1379			if (arg) {
1380				if (copy_from_user(bname, argp, sizeof(bname) - 1))
1381					return -EFAULT;
1382			} else
1383				return -EINVAL;
1384			ret = mutex_lock_interruptible(&dev->mtx);
1385			if (ret) return ret;
1386			if ((s = isdn_net_newslave(bname))) {
1387				if (copy_to_user(argp, s, strlen(s) + 1)) {
1388					ret = -EFAULT;
1389				} else {
1390					ret = 0;
1391				}
1392			} else
1393				ret = -ENODEV;
1394			mutex_unlock(&dev->mtx);
1395			return ret;
1396		case IIOCNETDIF:
1397			/* Delete a network-interface */
1398			if (arg) {
1399				if (copy_from_user(name, argp, sizeof(name)))
1400					return -EFAULT;
1401				ret = mutex_lock_interruptible(&dev->mtx);
1402				if (ret) return ret;
1403				ret = isdn_net_rm(name);
 
 
 
 
 
 
 
1404				mutex_unlock(&dev->mtx);
1405				return ret;
1406			} else
1407				return -EINVAL;
1408		case IIOCNETSCF:
1409			/* Set configurable parameters of a network-interface */
1410			if (arg) {
1411				if (copy_from_user(&cfg, argp, sizeof(cfg)))
1412					return -EFAULT;
1413				return isdn_net_setcfg(&cfg);
1414			} else
1415				return -EINVAL;
1416		case IIOCNETGCF:
1417			/* Get configurable parameters of a network-interface */
1418			if (arg) {
1419				if (copy_from_user(&cfg, argp, sizeof(cfg)))
1420					return -EFAULT;
1421				if (!(ret = isdn_net_getcfg(&cfg))) {
1422					if (copy_to_user(argp, &cfg, sizeof(cfg)))
1423						return -EFAULT;
1424				}
1425				return ret;
1426			} else
1427				return -EINVAL;
1428		case IIOCNETANM:
1429			/* Add a phone-number to a network-interface */
1430			if (arg) {
1431				if (copy_from_user(&phone, argp, sizeof(phone)))
1432					return -EFAULT;
1433				ret = mutex_lock_interruptible(&dev->mtx);
1434				if (ret) return ret;
1435				ret = isdn_net_addphone(&phone);
 
 
 
 
 
 
 
1436				mutex_unlock(&dev->mtx);
1437				return ret;
1438			} else
1439				return -EINVAL;
1440		case IIOCNETGNM:
1441			/* Get list of phone-numbers of a network-interface */
1442			if (arg) {
1443				if (copy_from_user(&phone, argp, sizeof(phone)))
1444					return -EFAULT;
1445				ret = mutex_lock_interruptible(&dev->mtx);
1446				if (ret) return ret;
1447				ret = isdn_net_getphones(&phone, argp);
1448				mutex_unlock(&dev->mtx);
1449				return ret;
1450			} else
1451				return -EINVAL;
1452		case IIOCNETDNM:
1453			/* Delete a phone-number of a network-interface */
1454			if (arg) {
1455				if (copy_from_user(&phone, argp, sizeof(phone)))
1456					return -EFAULT;
1457				ret = mutex_lock_interruptible(&dev->mtx);
1458				if (ret) return ret;
1459				ret = isdn_net_delphone(&phone);
1460				mutex_unlock(&dev->mtx);
1461				return ret;
1462			} else
1463				return -EINVAL;
1464		case IIOCNETDIL:
1465			/* Force dialing of a network-interface */
1466			if (arg) {
1467				if (copy_from_user(name, argp, sizeof(name)))
1468					return -EFAULT;
1469				return isdn_net_force_dial(name);
1470			} else
1471				return -EINVAL;
1472#ifdef CONFIG_ISDN_PPP
1473		case IIOCNETALN:
1474			if (!arg)
1475				return -EINVAL;
1476			if (copy_from_user(name, argp, sizeof(name)))
1477				return -EFAULT;
1478			return isdn_ppp_dial_slave(name);
1479		case IIOCNETDLN:
1480			if (!arg)
1481				return -EINVAL;
1482			if (copy_from_user(name, argp, sizeof(name)))
1483				return -EFAULT;
1484			return isdn_ppp_hangup_slave(name);
1485#endif
1486		case IIOCNETHUP:
1487			/* Force hangup of a network-interface */
1488			if (!arg)
1489				return -EINVAL;
1490			if (copy_from_user(name, argp, sizeof(name)))
1491				return -EFAULT;
1492			return isdn_net_force_hangup(name);
1493			break;
1494		case IIOCSETVER:
1495			dev->net_verbose = arg;
1496			printk(KERN_INFO "isdn: Verbose-Level is %d\n", dev->net_verbose);
1497			return 0;
1498		case IIOCSETGST:
1499			if (arg)
1500				dev->global_flags |= ISDN_GLOBAL_STOPPED;
1501			else
1502				dev->global_flags &= ~ISDN_GLOBAL_STOPPED;
1503			printk(KERN_INFO "isdn: Global Mode %s\n",
1504			       (dev->global_flags & ISDN_GLOBAL_STOPPED) ? "stopped" : "running");
1505			return 0;
1506		case IIOCSETBRJ:
1507			drvidx = -1;
1508			if (arg) {
1509				int i;
1510				char *p;
1511				if (copy_from_user(&iocts, argp,
1512						   sizeof(isdn_ioctl_struct)))
1513					return -EFAULT;
1514				iocts.drvid[sizeof(iocts.drvid) - 1] = 0;
1515				if (strlen(iocts.drvid)) {
1516					if ((p = strchr(iocts.drvid, ',')))
1517						*p = 0;
1518					drvidx = -1;
1519					for (i = 0; i < ISDN_MAX_DRIVERS; i++)
1520						if (!(strcmp(dev->drvid[i], iocts.drvid))) {
1521							drvidx = i;
1522							break;
1523						}
1524				}
1525			}
1526			if (drvidx == -1)
1527				return -ENODEV;
1528			if (iocts.arg)
1529				dev->drv[drvidx]->flags |= DRV_FLAG_REJBUS;
1530			else
1531				dev->drv[drvidx]->flags &= ~DRV_FLAG_REJBUS;
1532			return 0;
1533		case IIOCSIGPRF:
1534			dev->profd = current;
1535			return 0;
1536			break;
1537		case IIOCGETPRF:
1538			/* Get all Modem-Profiles */
1539			if (arg) {
1540				char __user *p = argp;
1541				int i;
1542
1543				if (!access_ok(VERIFY_WRITE, argp,
1544					       (ISDN_MODEM_NUMREG + ISDN_MSNLEN + ISDN_LMSNLEN)
1545					       * ISDN_MAX_CHANNELS))
1546					return -EFAULT;
1547
1548				for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
1549					if (copy_to_user(p, dev->mdm.info[i].emu.profile,
1550							 ISDN_MODEM_NUMREG))
1551						return -EFAULT;
1552					p += ISDN_MODEM_NUMREG;
1553					if (copy_to_user(p, dev->mdm.info[i].emu.pmsn, ISDN_MSNLEN))
 
 
 
 
 
1554						return -EFAULT;
1555					p += ISDN_MSNLEN;
1556					if (copy_to_user(p, dev->mdm.info[i].emu.plmsn, ISDN_LMSNLEN))
 
 
 
 
 
 
 
 
 
1557						return -EFAULT;
1558					p += ISDN_LMSNLEN;
1559				}
1560				return (ISDN_MODEM_NUMREG + ISDN_MSNLEN + ISDN_LMSNLEN) * ISDN_MAX_CHANNELS;
1561			} else
1562				return -EINVAL;
1563			break;
1564		case IIOCSETPRF:
1565			/* Set all Modem-Profiles */
1566			if (arg) {
1567				char __user *p = argp;
1568				int i;
1569
1570				if (!access_ok(VERIFY_READ, argp,
1571					       (ISDN_MODEM_NUMREG + ISDN_MSNLEN + ISDN_LMSNLEN)
1572					       * ISDN_MAX_CHANNELS))
1573					return -EFAULT;
1574
1575				for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
1576					if (copy_from_user(dev->mdm.info[i].emu.profile, p,
1577							   ISDN_MODEM_NUMREG))
1578						return -EFAULT;
1579					p += ISDN_MODEM_NUMREG;
1580					if (copy_from_user(dev->mdm.info[i].emu.plmsn, p, ISDN_LMSNLEN))
 
 
 
 
 
 
 
 
 
1581						return -EFAULT;
1582					p += ISDN_LMSNLEN;
1583					if (copy_from_user(dev->mdm.info[i].emu.pmsn, p, ISDN_MSNLEN))
 
 
 
 
 
 
 
 
 
1584						return -EFAULT;
1585					p += ISDN_MSNLEN;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1586				}
 
 
 
 
 
 
 
 
 
1587				return 0;
1588			} else
1589				return -EINVAL;
1590			break;
1591		case IIOCSETMAP:
1592		case IIOCGETMAP:
1593			/* Set/Get MSN->EAZ-Mapping for a driver */
1594			if (arg) {
 
 
 
 
1595
1596				if (copy_from_user(&iocts, argp,
1597						   sizeof(isdn_ioctl_struct)))
1598					return -EFAULT;
1599				iocts.drvid[sizeof(iocts.drvid) - 1] = 0;
1600				if (strlen(iocts.drvid)) {
1601					drvidx = -1;
1602					for (i = 0; i < ISDN_MAX_DRIVERS; i++)
1603						if (!(strcmp(dev->drvid[i], iocts.drvid))) {
1604							drvidx = i;
1605							break;
1606						}
 
 
1607				} else
1608					drvidx = 0;
1609				if (drvidx == -1)
1610					return -ENODEV;
1611				if (cmd == IIOCSETMAP) {
1612					int loop = 1;
 
 
 
 
 
 
 
1613
1614					p = (char __user *) iocts.arg;
1615					i = 0;
1616					while (loop) {
1617						int j = 0;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1618
1619						while (1) {
1620							if (!access_ok(VERIFY_READ, p, 1))
1621								return -EFAULT;
1622							get_user(bname[j], p++);
1623							switch (bname[j]) {
1624							case '\0':
1625								loop = 0;
1626								/* Fall through */
1627							case ',':
1628								bname[j] = '\0';
1629								strcpy(dev->drv[drvidx]->msn2eaz[i], bname);
1630								j = ISDN_MSNLEN;
1631								break;
1632							default:
1633								j++;
1634							}
1635							if (j >= ISDN_MSNLEN)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1636								break;
1637						}
1638						if (++i > 9)
1639							break;
 
 
 
 
 
 
 
 
 
1640					}
1641				} else {
1642					p = (char __user *) iocts.arg;
1643					for (i = 0; i < 10; i++) {
1644						snprintf(bname, sizeof(bname), "%s%s",
1645							 strlen(dev->drv[drvidx]->msn2eaz[i]) ?
1646							 dev->drv[drvidx]->msn2eaz[i] : "_",
1647							 (i < 9) ? "," : "\0");
1648						if (copy_to_user(p, bname, strlen(bname) + 1))
1649							return -EFAULT;
1650						p += strlen(bname);
1651					}
1652				}
1653				return 0;
1654			} else
1655				return -EINVAL;
1656		case IIOCDBGVAR:
1657			if (arg) {
1658				if (copy_to_user(argp, &dev, sizeof(ulong)))
1659					return -EFAULT;
1660				return 0;
1661			} else
1662				return -EINVAL;
1663			break;
1664		default:
1665			if ((cmd & IIOCDRVCTL) == IIOCDRVCTL)
1666				cmd = ((cmd >> _IOC_NRSHIFT) & _IOC_NRMASK) & ISDN_DRVIOCTL_MASK;
1667			else
1668				return -EINVAL;
1669			if (arg) {
1670				int i;
1671				char *p;
1672				if (copy_from_user(&iocts, argp, sizeof(isdn_ioctl_struct)))
1673					return -EFAULT;
1674				iocts.drvid[sizeof(iocts.drvid) - 1] = 0;
1675				if (strlen(iocts.drvid)) {
1676					if ((p = strchr(iocts.drvid, ',')))
1677						*p = 0;
1678					drvidx = -1;
1679					for (i = 0; i < ISDN_MAX_DRIVERS; i++)
1680						if (!(strcmp(dev->drvid[i], iocts.drvid))) {
1681							drvidx = i;
1682							break;
1683						}
 
 
 
 
1684				} else
1685					drvidx = 0;
1686				if (drvidx == -1)
1687					return -ENODEV;
1688				if (!access_ok(VERIFY_WRITE, argp,
1689					       sizeof(isdn_ioctl_struct)))
1690					return -EFAULT;
1691				c.driver = drvidx;
1692				c.command = ISDN_CMD_IOCTL;
1693				c.arg = cmd;
1694				memcpy(c.parm.num, &iocts.arg, sizeof(ulong));
1695				ret = isdn_command(&c);
1696				memcpy(&iocts.arg, c.parm.num, sizeof(ulong));
1697				if (copy_to_user(argp, &iocts, sizeof(isdn_ioctl_struct)))
1698					return -EFAULT;
1699				return ret;
1700			} else
1701				return -EINVAL;
1702		}
1703	}
1704#ifdef CONFIG_ISDN_PPP
1705	if (minor <= ISDN_MINOR_PPPMAX)
1706		return (isdn_ppp_ioctl(minor - ISDN_MINOR_PPP, file, cmd, arg));
1707#endif
1708	return -ENODEV;
1709
1710#undef name
1711#undef bname
1712#undef iocts
1713#undef phone
1714#undef cfg
1715}
1716
1717static long
1718isdn_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1719{
1720	int ret;
1721
1722	mutex_lock(&isdn_mutex);
1723	ret = isdn_ioctl(file, cmd, arg);
1724	mutex_unlock(&isdn_mutex);
1725
1726	return ret;
1727}
1728
1729/*
1730 * Open the device code.
1731 */
1732static int
1733isdn_open(struct inode *ino, struct file *filep)
1734{
1735	uint minor = iminor(ino);
1736	int drvidx;
1737	int chidx;
1738	int retval = -ENODEV;
1739
1740	mutex_lock(&isdn_mutex);
1741	if (minor == ISDN_MINOR_STATUS) {
1742		infostruct *p;
1743
1744		if ((p = kmalloc(sizeof(infostruct), GFP_KERNEL))) {
1745			p->next = (char *) dev->infochain;
1746			p->private = (char *) &(filep->private_data);
1747			dev->infochain = p;
1748			/* At opening we allow a single update */
1749			filep->private_data = (char *) 1;
1750			retval = 0;
1751			goto out;
1752		} else {
1753			retval = -ENOMEM;
1754			goto out;
1755		}
1756	}
1757	if (!dev->channels)
1758		goto out;
1759	if (minor <= ISDN_MINOR_BMAX) {
1760		printk(KERN_WARNING "isdn_open minor %d obsolete!\n", minor);
1761		drvidx = isdn_minor2drv(minor);
1762		if (drvidx < 0)
1763			goto out;
1764		chidx = isdn_minor2chan(minor);
1765		if (!(dev->drv[drvidx]->flags & DRV_FLAG_RUNNING))
1766			goto out;
1767		if (!(dev->drv[drvidx]->online & (1 << chidx)))
1768			goto out;
1769		isdn_lock_drivers();
1770		retval = 0;
1771		goto out;
1772	}
1773	if (minor <= ISDN_MINOR_CTRLMAX) {
1774		drvidx = isdn_minor2drv(minor - ISDN_MINOR_CTRL);
1775		if (drvidx < 0)
1776			goto out;
1777		isdn_lock_drivers();
1778		retval = 0;
1779		goto out;
1780	}
1781#ifdef CONFIG_ISDN_PPP
1782	if (minor <= ISDN_MINOR_PPPMAX) {
1783		retval = isdn_ppp_open(minor - ISDN_MINOR_PPP, filep);
1784		if (retval == 0)
1785			isdn_lock_drivers();
1786		goto out;
1787	}
1788#endif
1789out:
1790	nonseekable_open(ino, filep);
1791	mutex_unlock(&isdn_mutex);
1792	return retval;
1793}
1794
1795static int
1796isdn_close(struct inode *ino, struct file *filep)
1797{
1798	uint minor = iminor(ino);
1799
1800	mutex_lock(&isdn_mutex);
1801	if (minor == ISDN_MINOR_STATUS) {
1802		infostruct *p = dev->infochain;
1803		infostruct *q = NULL;
1804
1805		while (p) {
1806			if (p->private == (char *) &(filep->private_data)) {
1807				if (q)
1808					q->next = p->next;
1809				else
1810					dev->infochain = (infostruct *) (p->next);
1811				kfree(p);
1812				goto out;
1813			}
1814			q = p;
1815			p = (infostruct *) (p->next);
1816		}
1817		printk(KERN_WARNING "isdn: No private data while closing isdnctrl\n");
1818		goto out;
1819	}
1820	isdn_unlock_drivers();
1821	if (minor <= ISDN_MINOR_BMAX)
1822		goto out;
1823	if (minor <= ISDN_MINOR_CTRLMAX) {
1824		if (dev->profd == current)
1825			dev->profd = NULL;
1826		goto out;
1827	}
1828#ifdef CONFIG_ISDN_PPP
1829	if (minor <= ISDN_MINOR_PPPMAX)
1830		isdn_ppp_release(minor - ISDN_MINOR_PPP, filep);
1831#endif
1832
1833out:
1834	mutex_unlock(&isdn_mutex);
1835	return 0;
1836}
1837
1838static const struct file_operations isdn_fops =
1839{
1840	.owner		= THIS_MODULE,
1841	.llseek		= no_llseek,
1842	.read		= isdn_read,
1843	.write		= isdn_write,
1844	.poll		= isdn_poll,
1845	.unlocked_ioctl	= isdn_unlocked_ioctl,
1846	.open		= isdn_open,
1847	.release	= isdn_close,
1848};
1849
1850char *
1851isdn_map_eaz2msn(char *msn, int di)
1852{
1853	isdn_driver_t *this = dev->drv[di];
1854	int i;
1855
1856	if (strlen(msn) == 1) {
1857		i = msn[0] - '0';
1858		if ((i >= 0) && (i <= 9))
1859			if (strlen(this->msn2eaz[i]))
1860				return (this->msn2eaz[i]);
1861	}
1862	return (msn);
1863}
1864
1865/*
1866 * Find an unused ISDN-channel, whose feature-flags match the
1867 * given L2- and L3-protocols.
1868 */
1869#define L2V (~(ISDN_FEATURE_L2_V11096 | ISDN_FEATURE_L2_V11019 | ISDN_FEATURE_L2_V11038))
1870
1871/*
1872 * This function must be called with holding the dev->lock.
1873 */
1874int
1875isdn_get_free_channel(int usage, int l2_proto, int l3_proto, int pre_dev
1876		      , int pre_chan, char *msn)
1877{
1878	int i;
1879	ulong features;
1880	ulong vfeatures;
1881
1882	features = ((1 << l2_proto) | (0x10000 << l3_proto));
1883	vfeatures = (((1 << l2_proto) | (0x10000 << l3_proto)) &
1884		     ~(ISDN_FEATURE_L2_V11096 | ISDN_FEATURE_L2_V11019 | ISDN_FEATURE_L2_V11038));
1885	/* If Layer-2 protocol is V.110, accept drivers with
1886	 * transparent feature even if these don't support V.110
1887	 * because we can emulate this in linklevel.
1888	 */
1889	for (i = 0; i < ISDN_MAX_CHANNELS; i++)
1890		if (USG_NONE(dev->usage[i]) &&
1891		    (dev->drvmap[i] != -1)) {
1892			int d = dev->drvmap[i];
1893			if ((dev->usage[i] & ISDN_USAGE_EXCLUSIVE) &&
1894			    ((pre_dev != d) || (pre_chan != dev->chanmap[i])))
1895				continue;
1896			if (!strcmp(isdn_map_eaz2msn(msn, d), "-"))
1897				continue;
1898			if (dev->usage[i] & ISDN_USAGE_DISABLED)
1899				continue; /* usage not allowed */
1900			if (dev->drv[d]->flags & DRV_FLAG_RUNNING) {
1901				if (((dev->drv[d]->interface->features & features) == features) ||
1902				    (((dev->drv[d]->interface->features & vfeatures) == vfeatures) &&
1903				     (dev->drv[d]->interface->features & ISDN_FEATURE_L2_TRANS))) {
1904					if ((pre_dev < 0) || (pre_chan < 0)) {
1905						dev->usage[i] &= ISDN_USAGE_EXCLUSIVE;
1906						dev->usage[i] |= usage;
1907						isdn_info_update();
1908						return i;
1909					} else {
1910						if ((pre_dev == d) && (pre_chan == dev->chanmap[i])) {
1911							dev->usage[i] &= ISDN_USAGE_EXCLUSIVE;
1912							dev->usage[i] |= usage;
1913							isdn_info_update();
1914							return i;
1915						}
1916					}
1917				}
1918			}
1919		}
1920	return -1;
1921}
1922
1923/*
1924 * Set state of ISDN-channel to 'unused'
1925 */
1926void
1927isdn_free_channel(int di, int ch, int usage)
1928{
1929	int i;
1930
1931	if ((di < 0) || (ch < 0)) {
1932		printk(KERN_WARNING "%s: called with invalid drv(%d) or channel(%d)\n",
1933		       __func__, di, ch);
1934		return;
1935	}
1936	for (i = 0; i < ISDN_MAX_CHANNELS; i++)
1937		if (((!usage) || ((dev->usage[i] & ISDN_USAGE_MASK) == usage)) &&
1938		    (dev->drvmap[i] == di) &&
1939		    (dev->chanmap[i] == ch)) {
1940			dev->usage[i] &= (ISDN_USAGE_NONE | ISDN_USAGE_EXCLUSIVE);
1941			strcpy(dev->num[i], "???");
1942			dev->ibytes[i] = 0;
1943			dev->obytes[i] = 0;
1944// 20.10.99 JIM, try to reinitialize v110 !
1945			dev->v110emu[i] = 0;
1946			atomic_set(&(dev->v110use[i]), 0);
1947			isdn_v110_close(dev->v110[i]);
1948			dev->v110[i] = NULL;
1949// 20.10.99 JIM, try to reinitialize v110 !
1950			isdn_info_update();
1951			if (dev->drv[di])
1952				skb_queue_purge(&dev->drv[di]->rpqueue[ch]);
1953		}
1954}
1955
1956/*
1957 * Cancel Exclusive-Flag for ISDN-channel
1958 */
1959void
1960isdn_unexclusive_channel(int di, int ch)
1961{
1962	int i;
1963
1964	for (i = 0; i < ISDN_MAX_CHANNELS; i++)
1965		if ((dev->drvmap[i] == di) &&
1966		    (dev->chanmap[i] == ch)) {
1967			dev->usage[i] &= ~ISDN_USAGE_EXCLUSIVE;
1968			isdn_info_update();
1969			return;
1970		}
1971}
1972
1973/*
1974 *  writebuf replacement for SKB_ABLE drivers
1975 */
1976static int
1977isdn_writebuf_stub(int drvidx, int chan, const u_char __user *buf, int len)
1978{
1979	int ret;
1980	int hl = dev->drv[drvidx]->interface->hl_hdrlen;
1981	struct sk_buff *skb = alloc_skb(hl + len, GFP_ATOMIC);
1982
1983	if (!skb)
1984		return -ENOMEM;
1985	skb_reserve(skb, hl);
1986	if (copy_from_user(skb_put(skb, len), buf, len)) {
1987		dev_kfree_skb(skb);
1988		return -EFAULT;
1989	}
1990	ret = dev->drv[drvidx]->interface->writebuf_skb(drvidx, chan, 1, skb);
1991	if (ret <= 0)
1992		dev_kfree_skb(skb);
1993	if (ret > 0)
1994		dev->obytes[isdn_dc2minor(drvidx, chan)] += ret;
1995	return ret;
1996}
1997
1998/*
1999 * Return: length of data on success, -ERRcode on failure.
2000 */
2001int
2002isdn_writebuf_skb_stub(int drvidx, int chan, int ack, struct sk_buff *skb)
2003{
2004	int ret;
2005	struct sk_buff *nskb = NULL;
2006	int v110_ret = skb->len;
2007	int idx = isdn_dc2minor(drvidx, chan);
2008
2009	if (dev->v110[idx]) {
2010		atomic_inc(&dev->v110use[idx]);
2011		nskb = isdn_v110_encode(dev->v110[idx], skb);
2012		atomic_dec(&dev->v110use[idx]);
2013		if (!nskb)
2014			return 0;
2015		v110_ret = *((int *)nskb->data);
2016		skb_pull(nskb, sizeof(int));
2017		if (!nskb->len) {
2018			dev_kfree_skb(nskb);
2019			return v110_ret;
2020		}
2021		/* V.110 must always be acknowledged */
2022		ack = 1;
2023		ret = dev->drv[drvidx]->interface->writebuf_skb(drvidx, chan, ack, nskb);
2024	} else {
2025		int hl = dev->drv[drvidx]->interface->hl_hdrlen;
2026
2027		if (skb_headroom(skb) < hl) {
2028			/*
2029			 * This should only occur when new HL driver with
2030			 * increased hl_hdrlen was loaded after netdevice
2031			 * was created and connected to the new driver.
2032			 *
2033			 * The V.110 branch (re-allocates on its own) does
2034			 * not need this
2035			 */
2036			struct sk_buff *skb_tmp;
2037
2038			skb_tmp = skb_realloc_headroom(skb, hl);
2039			printk(KERN_DEBUG "isdn_writebuf_skb_stub: reallocating headroom%s\n", skb_tmp ? "" : " failed");
2040			if (!skb_tmp) return -ENOMEM; /* 0 better? */
2041			ret = dev->drv[drvidx]->interface->writebuf_skb(drvidx, chan, ack, skb_tmp);
2042			if (ret > 0) {
2043				dev_kfree_skb(skb);
2044			} else {
2045				dev_kfree_skb(skb_tmp);
2046			}
2047		} else {
2048			ret = dev->drv[drvidx]->interface->writebuf_skb(drvidx, chan, ack, skb);
2049		}
2050	}
2051	if (ret > 0) {
2052		dev->obytes[idx] += ret;
2053		if (dev->v110[idx]) {
2054			atomic_inc(&dev->v110use[idx]);
2055			dev->v110[idx]->skbuser++;
2056			atomic_dec(&dev->v110use[idx]);
2057			/* For V.110 return unencoded data length */
2058			ret = v110_ret;
2059			/* if the complete frame was send we free the skb;
2060			   if not upper function will requeue the skb */
2061			if (ret == skb->len)
2062				dev_kfree_skb(skb);
2063		}
2064	} else
2065		if (dev->v110[idx])
2066			dev_kfree_skb(nskb);
2067	return ret;
2068}
2069
2070static int
2071isdn_add_channels(isdn_driver_t *d, int drvidx, int n, int adding)
2072{
2073	int j, k, m;
2074
2075	init_waitqueue_head(&d->st_waitq);
2076	if (d->flags & DRV_FLAG_RUNNING)
2077		return -1;
2078	if (n < 1) return 0;
2079
2080	m = (adding) ? d->channels + n : n;
2081
2082	if (dev->channels + n > ISDN_MAX_CHANNELS) {
2083		printk(KERN_WARNING "register_isdn: Max. %d channels supported\n",
2084		       ISDN_MAX_CHANNELS);
2085		return -1;
2086	}
2087
2088	if ((adding) && (d->rcverr))
2089		kfree(d->rcverr);
2090	if (!(d->rcverr = kzalloc(sizeof(int) * m, GFP_ATOMIC))) {
2091		printk(KERN_WARNING "register_isdn: Could not alloc rcverr\n");
2092		return -1;
2093	}
2094
2095	if ((adding) && (d->rcvcount))
2096		kfree(d->rcvcount);
2097	if (!(d->rcvcount = kzalloc(sizeof(int) * m, GFP_ATOMIC))) {
2098		printk(KERN_WARNING "register_isdn: Could not alloc rcvcount\n");
2099		if (!adding)
2100			kfree(d->rcverr);
2101		return -1;
2102	}
2103
2104	if ((adding) && (d->rpqueue)) {
2105		for (j = 0; j < d->channels; j++)
2106			skb_queue_purge(&d->rpqueue[j]);
2107		kfree(d->rpqueue);
2108	}
2109	if (!(d->rpqueue = kmalloc(sizeof(struct sk_buff_head) * m, GFP_ATOMIC))) {
2110		printk(KERN_WARNING "register_isdn: Could not alloc rpqueue\n");
2111		if (!adding) {
2112			kfree(d->rcvcount);
2113			kfree(d->rcverr);
2114		}
2115		return -1;
2116	}
2117	for (j = 0; j < m; j++) {
2118		skb_queue_head_init(&d->rpqueue[j]);
2119	}
2120
2121	if ((adding) && (d->rcv_waitq))
2122		kfree(d->rcv_waitq);
2123	d->rcv_waitq = kmalloc(sizeof(wait_queue_head_t) * 2 * m, GFP_ATOMIC);
2124	if (!d->rcv_waitq) {
2125		printk(KERN_WARNING "register_isdn: Could not alloc rcv_waitq\n");
2126		if (!adding) {
2127			kfree(d->rpqueue);
2128			kfree(d->rcvcount);
2129			kfree(d->rcverr);
2130		}
2131		return -1;
2132	}
2133	d->snd_waitq = d->rcv_waitq + m;
2134	for (j = 0; j < m; j++) {
2135		init_waitqueue_head(&d->rcv_waitq[j]);
2136		init_waitqueue_head(&d->snd_waitq[j]);
2137	}
2138
2139	dev->channels += n;
2140	for (j = d->channels; j < m; j++)
2141		for (k = 0; k < ISDN_MAX_CHANNELS; k++)
2142			if (dev->chanmap[k] < 0) {
2143				dev->chanmap[k] = j;
2144				dev->drvmap[k] = drvidx;
2145				break;
2146			}
2147	d->channels = m;
2148	return 0;
2149}
2150
2151/*
2152 * Low-level-driver registration
2153 */
2154
2155static void
2156set_global_features(void)
2157{
2158	int drvidx;
2159
2160	dev->global_features = 0;
2161	for (drvidx = 0; drvidx < ISDN_MAX_DRIVERS; drvidx++) {
2162		if (!dev->drv[drvidx])
2163			continue;
2164		if (dev->drv[drvidx]->interface)
2165			dev->global_features |= dev->drv[drvidx]->interface->features;
2166	}
2167}
2168
2169#ifdef CONFIG_ISDN_DIVERSION
2170
2171static char *map_drvname(int di)
2172{
2173	if ((di < 0) || (di >= ISDN_MAX_DRIVERS))
2174		return (NULL);
2175	return (dev->drvid[di]); /* driver name */
2176} /* map_drvname */
2177
2178static int map_namedrv(char *id)
2179{  int i;
2180
2181	for (i = 0; i < ISDN_MAX_DRIVERS; i++)
2182	{ if (!strcmp(dev->drvid[i], id))
2183			return (i);
2184	}
2185	return (-1);
2186} /* map_namedrv */
2187
2188int DIVERT_REG_NAME(isdn_divert_if *i_div)
2189{
2190	if (i_div->if_magic != DIVERT_IF_MAGIC)
2191		return (DIVERT_VER_ERR);
2192	switch (i_div->cmd)
2193	{
2194	case DIVERT_CMD_REL:
2195		if (divert_if != i_div)
2196			return (DIVERT_REL_ERR);
2197		divert_if = NULL; /* free interface */
2198		return (DIVERT_NO_ERR);
2199
2200	case DIVERT_CMD_REG:
2201		if (divert_if)
2202			return (DIVERT_REG_ERR);
2203		i_div->ll_cmd = isdn_command; /* set command function */
2204		i_div->drv_to_name = map_drvname;
2205		i_div->name_to_drv = map_namedrv;
2206		divert_if = i_div; /* remember interface */
2207		return (DIVERT_NO_ERR);
2208
2209	default:
2210		return (DIVERT_CMD_ERR);
2211	}
2212} /* DIVERT_REG_NAME */
2213
2214EXPORT_SYMBOL(DIVERT_REG_NAME);
2215
2216#endif /* CONFIG_ISDN_DIVERSION */
2217
2218
2219EXPORT_SYMBOL(register_isdn);
2220#ifdef CONFIG_ISDN_PPP
2221EXPORT_SYMBOL(isdn_ppp_register_compressor);
2222EXPORT_SYMBOL(isdn_ppp_unregister_compressor);
2223#endif
2224
2225int
2226register_isdn(isdn_if *i)
2227{
2228	isdn_driver_t *d;
2229	int j;
2230	ulong flags;
2231	int drvidx;
2232
2233	if (dev->drivers >= ISDN_MAX_DRIVERS) {
2234		printk(KERN_WARNING "register_isdn: Max. %d drivers supported\n",
2235		       ISDN_MAX_DRIVERS);
2236		return 0;
2237	}
2238	if (!i->writebuf_skb) {
2239		printk(KERN_WARNING "register_isdn: No write routine given.\n");
2240		return 0;
2241	}
2242	if (!(d = kzalloc(sizeof(isdn_driver_t), GFP_KERNEL))) {
2243		printk(KERN_WARNING "register_isdn: Could not alloc driver-struct\n");
2244		return 0;
2245	}
2246
2247	d->maxbufsize = i->maxbufsize;
2248	d->pktcount = 0;
2249	d->stavail = 0;
2250	d->flags = DRV_FLAG_LOADED;
2251	d->online = 0;
2252	d->interface = i;
2253	d->channels = 0;
2254	spin_lock_irqsave(&dev->lock, flags);
2255	for (drvidx = 0; drvidx < ISDN_MAX_DRIVERS; drvidx++)
2256		if (!dev->drv[drvidx])
2257			break;
2258	if (isdn_add_channels(d, drvidx, i->channels, 0)) {
2259		spin_unlock_irqrestore(&dev->lock, flags);
2260		kfree(d);
2261		return 0;
2262	}
2263	i->channels = drvidx;
2264	i->rcvcallb_skb = isdn_receive_skb_callback;
2265	i->statcallb = isdn_status_callback;
2266	if (!strlen(i->id))
2267		sprintf(i->id, "line%d", drvidx);
2268	for (j = 0; j < drvidx; j++)
2269		if (!strcmp(i->id, dev->drvid[j]))
2270			sprintf(i->id, "line%d", drvidx);
2271	dev->drv[drvidx] = d;
2272	strcpy(dev->drvid[drvidx], i->id);
2273	isdn_info_update();
2274	dev->drivers++;
2275	set_global_features();
2276	spin_unlock_irqrestore(&dev->lock, flags);
2277	return 1;
2278}
2279
2280/*
2281*****************************************************************************
2282* And now the modules code.
2283*****************************************************************************
2284*/
2285
2286static char *
2287isdn_getrev(const char *revision)
2288{
2289	char *rev;
2290	char *p;
2291
2292	if ((p = strchr(revision, ':'))) {
2293		rev = p + 2;
2294		p = strchr(rev, '$');
2295		*--p = 0;
2296	} else
2297		rev = "???";
2298	return rev;
2299}
2300
2301/*
2302 * Allocate and initialize all data, register modem-devices
2303 */
2304static int __init isdn_init(void)
2305{
2306	int i;
2307	char tmprev[50];
2308
2309	dev = vzalloc(sizeof(isdn_dev));
2310	if (!dev) {
2311		printk(KERN_WARNING "isdn: Could not allocate device-struct.\n");
2312		return -EIO;
2313	}
 
2314	init_timer(&dev->timer);
2315	dev->timer.function = isdn_timer_funct;
2316	spin_lock_init(&dev->lock);
2317	spin_lock_init(&dev->timerlock);
2318#ifdef MODULE
2319	dev->owner = THIS_MODULE;
2320#endif
2321	mutex_init(&dev->mtx);
2322	init_waitqueue_head(&dev->info_waitq);
2323	for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
2324		dev->drvmap[i] = -1;
2325		dev->chanmap[i] = -1;
2326		dev->m_idx[i] = -1;
2327		strcpy(dev->num[i], "???");
 
 
2328	}
2329	if (register_chrdev(ISDN_MAJOR, "isdn", &isdn_fops)) {
2330		printk(KERN_WARNING "isdn: Could not register control devices\n");
2331		vfree(dev);
2332		return -EIO;
2333	}
2334	if ((isdn_tty_modem_init()) < 0) {
2335		printk(KERN_WARNING "isdn: Could not register tty devices\n");
2336		vfree(dev);
2337		unregister_chrdev(ISDN_MAJOR, "isdn");
2338		return -EIO;
2339	}
2340#ifdef CONFIG_ISDN_PPP
2341	if (isdn_ppp_init() < 0) {
2342		printk(KERN_WARNING "isdn: Could not create PPP-device-structs\n");
2343		isdn_tty_exit();
2344		unregister_chrdev(ISDN_MAJOR, "isdn");
2345		vfree(dev);
2346		return -EIO;
2347	}
2348#endif                          /* CONFIG_ISDN_PPP */
2349
2350	strcpy(tmprev, isdn_revision);
2351	printk(KERN_NOTICE "ISDN subsystem Rev: %s/", isdn_getrev(tmprev));
 
 
2352	strcpy(tmprev, isdn_net_revision);
2353	printk("%s/", isdn_getrev(tmprev));
2354	strcpy(tmprev, isdn_ppp_revision);
2355	printk("%s/", isdn_getrev(tmprev));
2356	strcpy(tmprev, isdn_audio_revision);
2357	printk("%s/", isdn_getrev(tmprev));
2358	strcpy(tmprev, isdn_v110_revision);
2359	printk("%s", isdn_getrev(tmprev));
2360
2361#ifdef MODULE
2362	printk(" loaded\n");
2363#else
2364	printk("\n");
2365#endif
2366	isdn_info_update();
2367	return 0;
2368}
2369
2370/*
2371 * Unload module
2372 */
2373static void __exit isdn_exit(void)
2374{
2375#ifdef CONFIG_ISDN_PPP
2376	isdn_ppp_cleanup();
2377#endif
2378	if (isdn_net_rmall() < 0) {
2379		printk(KERN_WARNING "isdn: net-device busy, remove cancelled\n");
2380		return;
2381	}
2382	isdn_tty_exit();
2383	unregister_chrdev(ISDN_MAJOR, "isdn");
2384	del_timer_sync(&dev->timer);
2385	/* call vfree with interrupts enabled, else it will hang */
2386	vfree(dev);
2387	printk(KERN_NOTICE "ISDN-subsystem unloaded\n");
2388}
2389
2390module_init(isdn_init);
2391module_exit(isdn_exit);