capi.c 36.6 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/* $Id: capi.c,v 1.1.2.7 2004/04/28 09:48:59 armin Exp $
 *
 * CAPI 2.0 Interface for Linux
 *
 * Copyright 1996 by Carsten Paeth <calle@calle.de>
 *
 * This software may be used and distributed according to the terms
 * of the GNU General Public License, incorporated herein by reference.
 *
 */

#include <linux/module.h>
#include <linux/errno.h>
#include <linux/kernel.h>
#include <linux/major.h>
#include <linux/sched.h>
#include <linux/slab.h>
#include <linux/fcntl.h>
#include <linux/fs.h>
#include <linux/signal.h>
21
#include <linux/mutex.h>
L
Linus Torvalds 已提交
22
#include <linux/mm.h>
J
Jonathan Corbet 已提交
23
#include <linux/smp_lock.h>
L
Linus Torvalds 已提交
24 25 26 27 28 29 30 31
#include <linux/timer.h>
#include <linux/wait.h>
#include <linux/tty.h>
#include <linux/netdevice.h>
#include <linux/ppp_defs.h>
#include <linux/if_ppp.h>
#include <linux/skbuff.h>
#include <linux/proc_fs.h>
32
#include <linux/seq_file.h>
L
Linus Torvalds 已提交
33 34 35 36 37 38 39 40
#include <linux/poll.h>
#include <linux/capi.h>
#include <linux/kernelcapi.h>
#include <linux/init.h>
#include <linux/device.h>
#include <linux/moduleparam.h>
#include <linux/isdn/capiutil.h>
#include <linux/isdn/capicmd.h>
J
Jan Kiszka 已提交
41

L
Linus Torvalds 已提交
42 43 44 45 46 47 48 49 50 51 52 53
#include "capifs.h"

MODULE_DESCRIPTION("CAPI4Linux: Userspace /dev/capi20 interface");
MODULE_AUTHOR("Carsten Paeth");
MODULE_LICENSE("GPL");

#undef _DEBUG_REFCOUNT		/* alloc/free and open/close debug */
#undef _DEBUG_TTYFUNCS		/* call to tty_driver */
#undef _DEBUG_DATAFLOW		/* data flow */

/* -------- driver information -------------------------------------- */

54
static struct class *capi_class;
A
Adrian Bunk 已提交
55
static int capi_major = 68;		/* allocated */
56 57 58

module_param_named(major, capi_major, uint, 0);

L
Linus Torvalds 已提交
59
#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
60
#define CAPINC_NR_PORTS		32
L
Linus Torvalds 已提交
61
#define CAPINC_MAX_PORTS	256
62

A
Adrian Bunk 已提交
63 64
static int capi_ttymajor = 191;
static int capi_ttyminors = CAPINC_NR_PORTS;
L
Linus Torvalds 已提交
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81

module_param_named(ttymajor, capi_ttymajor, uint, 0);
module_param_named(ttyminors, capi_ttyminors, uint, 0);
#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */

/* -------- defines ------------------------------------------------- */

#define CAPINC_MAX_RECVQUEUE	10
#define CAPINC_MAX_SENDQUEUE	10
#define CAPI_MAX_BLKSIZE	2048

/* -------- data structures ----------------------------------------- */

struct capidev;
struct capincci;
struct capiminor;

82 83 84 85 86
struct datahandle_queue {
	struct list_head	list;
	u16			datahandle;
};

L
Linus Torvalds 已提交
87 88 89 90
struct capiminor {
	struct list_head list;
	struct capincci  *nccip;
	unsigned int      minor;
J
Jan Kiszka 已提交
91
	struct dentry *capifs_dentry;
L
Linus Torvalds 已提交
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109

	struct capi20_appl *ap;
	u32		 ncci;
	u16		 datahandle;
	u16		 msgid;

	struct tty_struct *tty;
	int                ttyinstop;
	int                ttyoutstop;
	struct sk_buff    *ttyskb;
	atomic_t           ttyopencount;

	struct sk_buff_head inqueue;
	int                 inbytes;
	struct sk_buff_head outqueue;
	int                 outbytes;

	/* transmit path */
110
	struct list_head ackqueue;
L
Linus Torvalds 已提交
111
	int nack;
112
	spinlock_t ackqlock;
L
Linus Torvalds 已提交
113 114
};

115 116 117 118 119 120 121 122 123
/* FIXME: The following lock is a sledgehammer-workaround to a
 * locking issue with the capiminor (and maybe other) data structure(s).
 * Access to this data is done in a racy way and crashes the machine with
 * a FritzCard DSL driver; sooner or later. This is a workaround
 * which trades scalability vs stability, so it doesn't crash the kernel anymore.
 * The correct (and scalable) fix for the issue seems to require
 * an API change to the drivers... . */
static DEFINE_SPINLOCK(workaround_lock);

L
Linus Torvalds 已提交
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
struct capincci {
	struct capincci *next;
	u32		 ncci;
	struct capidev	*cdev;
#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
	struct capiminor *minorp;
#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
};

struct capidev {
	struct list_head list;
	struct capi20_appl ap;
	u16		errcode;
	unsigned        userflags;

	struct sk_buff_head recvqueue;
	wait_queue_head_t recvwait;

	struct capincci *nccis;

144
	struct mutex ncci_list_mtx;
L
Linus Torvalds 已提交
145 146 147 148 149 150 151 152
};

/* -------- global variables ---------------------------------------- */

static DEFINE_RWLOCK(capidev_list_lock);
static LIST_HEAD(capidev_list);

#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
153

L
Linus Torvalds 已提交
154 155 156 157 158
static DEFINE_RWLOCK(capiminor_list_lock);
static LIST_HEAD(capiminor_list);

/* -------- datahandles --------------------------------------------- */

159
static int capiminor_add_ack(struct capiminor *mp, u16 datahandle)
L
Linus Torvalds 已提交
160
{
161 162
	struct datahandle_queue *n;
	unsigned long flags;
L
Linus Torvalds 已提交
163 164

	n = kmalloc(sizeof(*n), GFP_ATOMIC);
165 166 167
	if (unlikely(!n)) {
		printk(KERN_ERR "capi: alloc datahandle failed\n");
		return -1;
L
Linus Torvalds 已提交
168 169
	}
	n->datahandle = datahandle;
170 171 172
	INIT_LIST_HEAD(&n->list);
	spin_lock_irqsave(&mp->ackqlock, flags);
	list_add_tail(&n->list, &mp->ackqueue);
L
Linus Torvalds 已提交
173
	mp->nack++;
174
	spin_unlock_irqrestore(&mp->ackqlock, flags);
L
Linus Torvalds 已提交
175 176 177 178 179
	return 0;
}

static int capiminor_del_ack(struct capiminor *mp, u16 datahandle)
{
180 181
	struct datahandle_queue *p, *tmp;
	unsigned long flags;
L
Linus Torvalds 已提交
182

183 184 185 186
	spin_lock_irqsave(&mp->ackqlock, flags);
	list_for_each_entry_safe(p, tmp, &mp->ackqueue, list) {
 		if (p->datahandle == datahandle) {
			list_del(&p->list);
L
Linus Torvalds 已提交
187 188
			kfree(p);
			mp->nack--;
189
			spin_unlock_irqrestore(&mp->ackqlock, flags);
L
Linus Torvalds 已提交
190 191 192
			return 0;
		}
	}
193
	spin_unlock_irqrestore(&mp->ackqlock, flags);
L
Linus Torvalds 已提交
194 195 196 197 198
	return -1;
}

static void capiminor_del_all_ack(struct capiminor *mp)
{
199 200
	struct datahandle_queue *p, *tmp;
	unsigned long flags;
L
Linus Torvalds 已提交
201

202 203 204
	spin_lock_irqsave(&mp->ackqlock, flags);
	list_for_each_entry_safe(p, tmp, &mp->ackqueue, list) {
		list_del(&p->list);
L
Linus Torvalds 已提交
205 206 207
		kfree(p);
		mp->nack--;
	}
208
	spin_unlock_irqrestore(&mp->ackqlock, flags);
L
Linus Torvalds 已提交
209 210 211 212 213 214 215 216 217 218 219
}


/* -------- struct capiminor ---------------------------------------- */

static struct capiminor *capiminor_alloc(struct capi20_appl *ap, u32 ncci)
{
	struct capiminor *mp, *p;
	unsigned int minor = 0;
	unsigned long flags;

220
	mp = kzalloc(sizeof(*mp), GFP_ATOMIC);
L
Linus Torvalds 已提交
221 222 223 224 225 226 227 228 229
  	if (!mp) {
  		printk(KERN_ERR "capi: can't alloc capiminor\n");
		return NULL;
	}

	mp->ap = ap;
	mp->ncci = ncci;
	mp->msgid = 0;
	atomic_set(&mp->ttyopencount,0);
230 231
	INIT_LIST_HEAD(&mp->ackqueue);
	spin_lock_init(&mp->ackqlock);
L
Linus Torvalds 已提交
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271

	skb_queue_head_init(&mp->inqueue);
	skb_queue_head_init(&mp->outqueue);

	/* Allocate the least unused minor number.
	 */
	write_lock_irqsave(&capiminor_list_lock, flags);
	if (list_empty(&capiminor_list))
		list_add(&mp->list, &capiminor_list);
	else {
		list_for_each_entry(p, &capiminor_list, list) {
			if (p->minor > minor)
				break;
			minor++;
		}
		
		if (minor < capi_ttyminors) {
			mp->minor = minor;
			list_add(&mp->list, p->list.prev);
		}
	}
		write_unlock_irqrestore(&capiminor_list_lock, flags);

	if (!(minor < capi_ttyminors)) {
		printk(KERN_NOTICE "capi: out of minors\n");
			kfree(mp);
		return NULL;
	}

	return mp;
}

static void capiminor_free(struct capiminor *mp)
{
	unsigned long flags;

	write_lock_irqsave(&capiminor_list_lock, flags);
	list_del(&mp->list);
	write_unlock_irqrestore(&capiminor_list_lock, flags);

272
	kfree_skb(mp->ttyskb);
L
Linus Torvalds 已提交
273 274 275 276 277 278 279
	mp->ttyskb = NULL;
	skb_queue_purge(&mp->inqueue);
	skb_queue_purge(&mp->outqueue);
	capiminor_del_all_ack(mp);
	kfree(mp);
}

A
Adrian Bunk 已提交
280
static struct capiminor *capiminor_find(unsigned int minor)
L
Linus Torvalds 已提交
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
{
	struct list_head *l;
	struct capiminor *p = NULL;

	read_lock(&capiminor_list_lock);
	list_for_each(l, &capiminor_list) {
		p = list_entry(l, struct capiminor, list);
		if (p->minor == minor)
			break;
	}
	read_unlock(&capiminor_list_lock);
	if (l == &capiminor_list)
		return NULL;

	return p;
}

/* -------- struct capincci ----------------------------------------- */

300
static void capincci_alloc_minor(struct capidev *cdev, struct capincci *np)
L
Linus Torvalds 已提交
301
{
302
	struct capiminor *mp;
L
Linus Torvalds 已提交
303

304 305 306 307
	if (!(cdev->userflags & CAPIFLAG_HIGHJACKING))
		return;

	mp = np->minorp = capiminor_alloc(&cdev->ap, np->ncci);
L
Linus Torvalds 已提交
308 309 310 311 312
	if (mp) {
		mp->nccip = np;
#ifdef _DEBUG_REFCOUNT
		printk(KERN_DEBUG "set mp->nccip\n");
#endif
J
Jan Kiszka 已提交
313 314 315
		mp->capifs_dentry =
			capifs_new_ncci(mp->minor,
					MKDEV(capi_ttymajor, mp->minor));
L
Linus Torvalds 已提交
316
	}
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
}

static void capincci_free_minor(struct capincci *np)
{
	struct capiminor *mp = np->minorp;

	if (mp) {
		capifs_free_ncci(mp->capifs_dentry);
		if (mp->tty) {
			mp->nccip = NULL;
#ifdef _DEBUG_REFCOUNT
			printk(KERN_DEBUG "reset mp->nccip\n");
#endif
			tty_hangup(mp->tty);
		} else {
			capiminor_free(mp);
		}
	}
}

static inline unsigned int capincci_minor_opencount(struct capincci *np)
{
	struct capiminor *mp = np->minorp;

	return mp ? atomic_read(&mp->ttyopencount) : 0;
}

#else /* !CONFIG_ISDN_CAPI_MIDDLEWARE */

static inline void
capincci_alloc_minor(struct capidev *cdev, struct capincci *np) { }
static inline void capincci_free_minor(struct capincci *np) { }

static inline unsigned int capincci_minor_opencount(struct capincci *np)
{
	return 0;
}

#endif /* !CONFIG_ISDN_CAPI_MIDDLEWARE */

static struct capincci *capincci_alloc(struct capidev *cdev, u32 ncci)
{
	struct capincci *np, **pp;

	np = kzalloc(sizeof(*np), GFP_ATOMIC);
	if (!np)
		return NULL;
	np->ncci = ncci;
	np->cdev = cdev;

	capincci_alloc_minor(cdev, np);

L
Linus Torvalds 已提交
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
	for (pp=&cdev->nccis; *pp; pp = &(*pp)->next)
		;
	*pp = np;
        return np;
}

static void capincci_free(struct capidev *cdev, u32 ncci)
{
	struct capincci *np, **pp;

	pp=&cdev->nccis;
	while (*pp) {
		np = *pp;
		if (ncci == 0xffffffff || np->ncci == ncci) {
			*pp = (*pp)->next;
384
			capincci_free_minor(np);
L
Linus Torvalds 已提交
385
			kfree(np);
386
			if (*pp == NULL) return;
L
Linus Torvalds 已提交
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
		} else {
			pp = &(*pp)->next;
		}
	}
}

static struct capincci *capincci_find(struct capidev *cdev, u32 ncci)
{
	struct capincci *p;

	for (p=cdev->nccis; p ; p = p->next) {
		if (p->ncci == ncci)
			break;
	}
	return p;
}

/* -------- struct capidev ------------------------------------------ */

static struct capidev *capidev_alloc(void)
{
	struct capidev *cdev;
	unsigned long flags;

411
	cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
L
Linus Torvalds 已提交
412 413 414
	if (!cdev)
		return NULL;

415
	mutex_init(&cdev->ncci_list_mtx);
L
Linus Torvalds 已提交
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
	skb_queue_head_init(&cdev->recvqueue);
	init_waitqueue_head(&cdev->recvwait);
	write_lock_irqsave(&capidev_list_lock, flags);
	list_add_tail(&cdev->list, &capidev_list);
	write_unlock_irqrestore(&capidev_list_lock, flags);
        return cdev;
}

static void capidev_free(struct capidev *cdev)
{
	unsigned long flags;

	if (cdev->ap.applid) {
		capi20_release(&cdev->ap);
		cdev->ap.applid = 0;
	}
	skb_queue_purge(&cdev->recvqueue);

434
	mutex_lock(&cdev->ncci_list_mtx);
L
Linus Torvalds 已提交
435
	capincci_free(cdev, 0xffffffff);
436
	mutex_unlock(&cdev->ncci_list_mtx);
L
Linus Torvalds 已提交
437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484

	write_lock_irqsave(&capidev_list_lock, flags);
	list_del(&cdev->list);
	write_unlock_irqrestore(&capidev_list_lock, flags);
	kfree(cdev);
}

#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
/* -------- handle data queue --------------------------------------- */

static struct sk_buff *
gen_data_b3_resp_for(struct capiminor *mp, struct sk_buff *skb)
{
	struct sk_buff *nskb;
	nskb = alloc_skb(CAPI_DATA_B3_RESP_LEN, GFP_ATOMIC);
	if (nskb) {
		u16 datahandle = CAPIMSG_U16(skb->data,CAPIMSG_BASELEN+4+4+2);
		unsigned char *s = skb_put(nskb, CAPI_DATA_B3_RESP_LEN);
		capimsg_setu16(s, 0, CAPI_DATA_B3_RESP_LEN);
		capimsg_setu16(s, 2, mp->ap->applid);
		capimsg_setu8 (s, 4, CAPI_DATA_B3);
		capimsg_setu8 (s, 5, CAPI_RESP);
		capimsg_setu16(s, 6, mp->msgid++);
		capimsg_setu32(s, 8, mp->ncci);
		capimsg_setu16(s, 12, datahandle);
	}
	return nskb;
}

static int handle_recv_skb(struct capiminor *mp, struct sk_buff *skb)
{
	struct sk_buff *nskb;
	int datalen;
	u16 errcode, datahandle;
	struct tty_ldisc *ld;
	
	datalen = skb->len - CAPIMSG_LEN(skb->data);
	if (mp->tty == NULL)
	{
#ifdef _DEBUG_DATAFLOW
		printk(KERN_DEBUG "capi: currently no receiver\n");
#endif
		return -1;
	}
	
	ld = tty_ldisc_ref(mp->tty);
	if (ld == NULL)
		return -1;
A
Alan Cox 已提交
485
	if (ld->ops->receive_buf == NULL) {
L
Linus Torvalds 已提交
486 487 488 489 490 491 492 493 494 495 496
#if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
		printk(KERN_DEBUG "capi: ldisc has no receive_buf function\n");
#endif
		goto bad;
	}
	if (mp->ttyinstop) {
#if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
		printk(KERN_DEBUG "capi: recv tty throttled\n");
#endif
		goto bad;
	}
A
Alan Cox 已提交
497
	if (mp->tty->receive_room < datalen) {
L
Linus Torvalds 已提交
498 499 500 501 502
#if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
		printk(KERN_DEBUG "capi: no room in tty\n");
#endif
		goto bad;
	}
503
	if ((nskb = gen_data_b3_resp_for(mp, skb)) == NULL) {
L
Linus Torvalds 已提交
504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519
		printk(KERN_ERR "capi: gen_data_b3_resp failed\n");
		goto bad;
	}
	datahandle = CAPIMSG_U16(skb->data,CAPIMSG_BASELEN+4);
	errcode = capi20_put_message(mp->ap, nskb);
	if (errcode != CAPI_NOERROR) {
		printk(KERN_ERR "capi: send DATA_B3_RESP failed=%x\n",
				errcode);
		kfree_skb(nskb);
		goto bad;
	}
	(void)skb_pull(skb, CAPIMSG_LEN(skb->data));
#ifdef _DEBUG_DATAFLOW
	printk(KERN_DEBUG "capi: DATA_B3_RESP %u len=%d => ldisc\n",
				datahandle, skb->len);
#endif
A
Alan Cox 已提交
520
	ld->ops->receive_buf(mp->tty, skb->data, NULL, skb->len);
L
Linus Torvalds 已提交
521 522 523 524 525 526 527 528 529 530 531
	kfree_skb(skb);
	tty_ldisc_deref(ld);
	return 0;
bad:
	tty_ldisc_deref(ld);
	return -1;
}

static void handle_minor_recv(struct capiminor *mp)
{
	struct sk_buff *skb;
532
	while ((skb = skb_dequeue(&mp->inqueue)) != NULL) {
L
Linus Torvalds 已提交
533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557
		unsigned int len = skb->len;
		mp->inbytes -= len;
		if (handle_recv_skb(mp, skb) < 0) {
			skb_queue_head(&mp->inqueue, skb);
			mp->inbytes += len;
			return;
		}
	}
}

static int handle_minor_send(struct capiminor *mp)
{
	struct sk_buff *skb;
	u16 len;
	int count = 0;
	u16 errcode;
	u16 datahandle;

	if (mp->tty && mp->ttyoutstop) {
#if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
		printk(KERN_DEBUG "capi: send: tty stopped\n");
#endif
		return 0;
	}

558
	while ((skb = skb_dequeue(&mp->outqueue)) != NULL) {
L
Linus Torvalds 已提交
559 560 561 562 563 564 565 566 567 568
		datahandle = mp->datahandle;
		len = (u16)skb->len;
		skb_push(skb, CAPI_DATA_B3_REQ_LEN);
		memset(skb->data, 0, CAPI_DATA_B3_REQ_LEN);
		capimsg_setu16(skb->data, 0, CAPI_DATA_B3_REQ_LEN);
		capimsg_setu16(skb->data, 2, mp->ap->applid);
		capimsg_setu8 (skb->data, 4, CAPI_DATA_B3);
		capimsg_setu8 (skb->data, 5, CAPI_REQ);
		capimsg_setu16(skb->data, 6, mp->msgid++);
		capimsg_setu32(skb->data, 8, mp->ncci);	/* NCCI */
A
Andrew Morton 已提交
569
		capimsg_setu32(skb->data, 12, (u32)(long)skb->data);/* Data32 */
L
Linus Torvalds 已提交
570 571 572 573
		capimsg_setu16(skb->data, 16, len);	/* Data length */
		capimsg_setu16(skb->data, 18, datahandle);
		capimsg_setu16(skb->data, 20, 0);	/* Flags */

574
		if (capiminor_add_ack(mp, datahandle) < 0) {
L
Linus Torvalds 已提交
575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617
			skb_pull(skb, CAPI_DATA_B3_REQ_LEN);
			skb_queue_head(&mp->outqueue, skb);
			return count;
		}
		errcode = capi20_put_message(mp->ap, skb);
		if (errcode == CAPI_NOERROR) {
			mp->datahandle++;
			count++;
			mp->outbytes -= len;
#ifdef _DEBUG_DATAFLOW
			printk(KERN_DEBUG "capi: DATA_B3_REQ %u len=%u\n",
							datahandle, len);
#endif
			continue;
		}
		capiminor_del_ack(mp, datahandle);

		if (errcode == CAPI_SENDQUEUEFULL) {
			skb_pull(skb, CAPI_DATA_B3_REQ_LEN);
			skb_queue_head(&mp->outqueue, skb);
			break;
		}

		/* ups, drop packet */
		printk(KERN_ERR "capi: put_message = %x\n", errcode);
		mp->outbytes -= len;
		kfree_skb(skb);
	}
	return count;
}

#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
/* -------- function called by lower level -------------------------- */

static void capi_recv_message(struct capi20_appl *ap, struct sk_buff *skb)
{
	struct capidev *cdev = ap->private;
#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
	struct capiminor *mp;
	u16 datahandle;
#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
	struct capincci *np;
	u32 ncci;
618
	unsigned long flags;
L
Linus Torvalds 已提交
619 620 621

	if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_CONF) {
		u16 info = CAPIMSG_U16(skb->data, 12); // Info field
622
		if ((info & 0xff00) == 0) {
623
			mutex_lock(&cdev->ncci_list_mtx);
L
Linus Torvalds 已提交
624
			capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
625
			mutex_unlock(&cdev->ncci_list_mtx);
L
Linus Torvalds 已提交
626 627 628
		}
	}
	if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_IND) {
629
		mutex_lock(&cdev->ncci_list_mtx);
L
Linus Torvalds 已提交
630
		capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
631
		mutex_unlock(&cdev->ncci_list_mtx);
L
Linus Torvalds 已提交
632
	}
633
	spin_lock_irqsave(&workaround_lock, flags);
L
Linus Torvalds 已提交
634 635 636
	if (CAPIMSG_COMMAND(skb->data) != CAPI_DATA_B3) {
		skb_queue_tail(&cdev->recvqueue, skb);
		wake_up_interruptible(&cdev->recvwait);
637
		spin_unlock_irqrestore(&workaround_lock, flags);
L
Linus Torvalds 已提交
638 639 640 641 642 643 644 645 646
		return;
	}
	ncci = CAPIMSG_CONTROL(skb->data);
	for (np = cdev->nccis; np && np->ncci != ncci; np = np->next)
		;
	if (!np) {
		printk(KERN_ERR "BUG: capi_signal: ncci not found\n");
		skb_queue_tail(&cdev->recvqueue, skb);
		wake_up_interruptible(&cdev->recvwait);
647
		spin_unlock_irqrestore(&workaround_lock, flags);
L
Linus Torvalds 已提交
648 649
		return;
	}
650

L
Linus Torvalds 已提交
651 652 653
#ifndef CONFIG_ISDN_CAPI_MIDDLEWARE
	skb_queue_tail(&cdev->recvqueue, skb);
	wake_up_interruptible(&cdev->recvwait);
654

L
Linus Torvalds 已提交
655
#else /* CONFIG_ISDN_CAPI_MIDDLEWARE */
656

L
Linus Torvalds 已提交
657 658 659 660
	mp = np->minorp;
	if (!mp) {
		skb_queue_tail(&cdev->recvqueue, skb);
		wake_up_interruptible(&cdev->recvwait);
661
		spin_unlock_irqrestore(&workaround_lock, flags);
L
Linus Torvalds 已提交
662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693
		return;
	}
	if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_IND) {
		datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4+4+2);
#ifdef _DEBUG_DATAFLOW
		printk(KERN_DEBUG "capi_signal: DATA_B3_IND %u len=%d\n",
				datahandle, skb->len-CAPIMSG_LEN(skb->data));
#endif
		skb_queue_tail(&mp->inqueue, skb);
		mp->inbytes += skb->len;
		handle_minor_recv(mp);

	} else if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_CONF) {

		datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4);
#ifdef _DEBUG_DATAFLOW
		printk(KERN_DEBUG "capi_signal: DATA_B3_CONF %u 0x%x\n",
				datahandle,
				CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4+2));
#endif
		kfree_skb(skb);
		(void)capiminor_del_ack(mp, datahandle);
		if (mp->tty)
			tty_wakeup(mp->tty);
		(void)handle_minor_send(mp);

	} else {
		/* ups, let capi application handle it :-) */
		skb_queue_tail(&cdev->recvqueue, skb);
		wake_up_interruptible(&cdev->recvwait);
	}
#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
694

695
	spin_unlock_irqrestore(&workaround_lock, flags);
L
Linus Torvalds 已提交
696 697 698 699 700 701 702 703 704 705 706 707 708 709
}

/* -------- file_operations for capidev ----------------------------- */

static ssize_t
capi_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
{
	struct capidev *cdev = (struct capidev *)file->private_data;
	struct sk_buff *skb;
	size_t copied;

	if (!cdev->ap.applid)
		return -ENODEV;

710
	if ((skb = skb_dequeue(&cdev->recvqueue)) == NULL) {
L
Linus Torvalds 已提交
711 712 713 714 715 716

		if (file->f_flags & O_NONBLOCK)
			return -EAGAIN;

		for (;;) {
			interruptible_sleep_on(&cdev->recvwait);
717
			if ((skb = skb_dequeue(&cdev->recvqueue)) != NULL)
L
Linus Torvalds 已提交
718 719 720 721
				break;
			if (signal_pending(current))
				break;
		}
722
		if (skb == NULL)
L
Linus Torvalds 已提交
723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772
			return -ERESTARTNOHAND;
	}
	if (skb->len > count) {
		skb_queue_head(&cdev->recvqueue, skb);
		return -EMSGSIZE;
	}
	if (copy_to_user(buf, skb->data, skb->len)) {
		skb_queue_head(&cdev->recvqueue, skb);
		return -EFAULT;
	}
	copied = skb->len;

	kfree_skb(skb);

	return copied;
}

static ssize_t
capi_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
{
	struct capidev *cdev = (struct capidev *)file->private_data;
	struct sk_buff *skb;
	u16 mlen;

	if (!cdev->ap.applid)
		return -ENODEV;

	skb = alloc_skb(count, GFP_USER);
	if (!skb)
		return -ENOMEM;

	if (copy_from_user(skb_put(skb, count), buf, count)) {
		kfree_skb(skb);
		return -EFAULT;
	}
	mlen = CAPIMSG_LEN(skb->data);
	if (CAPIMSG_CMD(skb->data) == CAPI_DATA_B3_REQ) {
		if ((size_t)(mlen + CAPIMSG_DATALEN(skb->data)) != count) {
			kfree_skb(skb);
			return -EINVAL;
		}
	} else {
		if (mlen != count) {
			kfree_skb(skb);
			return -EINVAL;
		}
	}
	CAPIMSG_SETAPPID(skb->data, cdev->ap.applid);

	if (CAPIMSG_CMD(skb->data) == CAPI_DISCONNECT_B3_RESP) {
773
		mutex_lock(&cdev->ncci_list_mtx);
L
Linus Torvalds 已提交
774
		capincci_free(cdev, CAPIMSG_NCCI(skb->data));
775
		mutex_unlock(&cdev->ncci_list_mtx);
L
Linus Torvalds 已提交
776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956
	}

	cdev->errcode = capi20_put_message(&cdev->ap, skb);

	if (cdev->errcode) {
		kfree_skb(skb);
		return -EIO;
	}
	return count;
}

static unsigned int
capi_poll(struct file *file, poll_table * wait)
{
	struct capidev *cdev = (struct capidev *)file->private_data;
	unsigned int mask = 0;

	if (!cdev->ap.applid)
		return POLLERR;

	poll_wait(file, &(cdev->recvwait), wait);
	mask = POLLOUT | POLLWRNORM;
	if (!skb_queue_empty(&cdev->recvqueue))
		mask |= POLLIN | POLLRDNORM;
	return mask;
}

static int
capi_ioctl(struct inode *inode, struct file *file,
	   unsigned int cmd, unsigned long arg)
{
	struct capidev *cdev = file->private_data;
	struct capi20_appl *ap = &cdev->ap;
	capi_ioctl_struct data;
	int retval = -EINVAL;
	void __user *argp = (void __user *)arg;

	switch (cmd) {
	case CAPI_REGISTER:
		{
			if (ap->applid)
				return -EEXIST;

			if (copy_from_user(&cdev->ap.rparam, argp,
					   sizeof(struct capi_register_params)))
				return -EFAULT;
			
			cdev->ap.private = cdev;
			cdev->ap.recv_message = capi_recv_message;
			cdev->errcode = capi20_register(ap);
			if (cdev->errcode) {
				ap->applid = 0;
				return -EIO;
			}
		}
		return (int)ap->applid;

	case CAPI_GET_VERSION:
		{
			if (copy_from_user(&data.contr, argp,
						sizeof(data.contr)))
				return -EFAULT;
		        cdev->errcode = capi20_get_version(data.contr, &data.version);
			if (cdev->errcode)
				return -EIO;
			if (copy_to_user(argp, &data.version,
					 sizeof(data.version)))
				return -EFAULT;
		}
		return 0;

	case CAPI_GET_SERIAL:
		{
			if (copy_from_user(&data.contr, argp,
					   sizeof(data.contr)))
				return -EFAULT;
			cdev->errcode = capi20_get_serial (data.contr, data.serial);
			if (cdev->errcode)
				return -EIO;
			if (copy_to_user(argp, data.serial,
					 sizeof(data.serial)))
				return -EFAULT;
		}
		return 0;
	case CAPI_GET_PROFILE:
		{
			if (copy_from_user(&data.contr, argp,
					   sizeof(data.contr)))
				return -EFAULT;

			if (data.contr == 0) {
				cdev->errcode = capi20_get_profile(data.contr, &data.profile);
				if (cdev->errcode)
					return -EIO;

				retval = copy_to_user(argp,
				      &data.profile.ncontroller,
				       sizeof(data.profile.ncontroller));

			} else {
				cdev->errcode = capi20_get_profile(data.contr, &data.profile);
				if (cdev->errcode)
					return -EIO;

				retval = copy_to_user(argp, &data.profile,
						   sizeof(data.profile));
			}
			if (retval)
				return -EFAULT;
		}
		return 0;

	case CAPI_GET_MANUFACTURER:
		{
			if (copy_from_user(&data.contr, argp,
					   sizeof(data.contr)))
				return -EFAULT;
			cdev->errcode = capi20_get_manufacturer(data.contr, data.manufacturer);
			if (cdev->errcode)
				return -EIO;

			if (copy_to_user(argp, data.manufacturer,
					 sizeof(data.manufacturer)))
				return -EFAULT;

		}
		return 0;
	case CAPI_GET_ERRCODE:
		data.errcode = cdev->errcode;
		cdev->errcode = CAPI_NOERROR;
		if (arg) {
			if (copy_to_user(argp, &data.errcode,
					 sizeof(data.errcode)))
				return -EFAULT;
		}
		return data.errcode;

	case CAPI_INSTALLED:
		if (capi20_isinstalled() == CAPI_NOERROR)
			return 0;
		return -ENXIO;

	case CAPI_MANUFACTURER_CMD:
		{
			struct capi_manufacturer_cmd mcmd;
			if (!capable(CAP_SYS_ADMIN))
				return -EPERM;
			if (copy_from_user(&mcmd, argp, sizeof(mcmd)))
				return -EFAULT;
			return capi20_manufacturer(mcmd.cmd, mcmd.data);
		}
		return 0;

	case CAPI_SET_FLAGS:
	case CAPI_CLR_FLAGS:
		{
			unsigned userflags;
			if (copy_from_user(&userflags, argp,
					   sizeof(userflags)))
				return -EFAULT;
			if (cmd == CAPI_SET_FLAGS)
				cdev->userflags |= userflags;
			else
				cdev->userflags &= ~userflags;
		}
		return 0;

	case CAPI_GET_FLAGS:
		if (copy_to_user(argp, &cdev->userflags,
				 sizeof(cdev->userflags)))
			return -EFAULT;
		return 0;

	case CAPI_NCCI_OPENCOUNT:
		{
			struct capincci *nccip;
			unsigned ncci;
			int count = 0;
			if (copy_from_user(&ncci, argp, sizeof(ncci)))
				return -EFAULT;

957
			mutex_lock(&cdev->ncci_list_mtx);
958
			if ((nccip = capincci_find(cdev, (u32) ncci)) == NULL) {
959
				mutex_unlock(&cdev->ncci_list_mtx);
L
Linus Torvalds 已提交
960 961
				return 0;
			}
962
			count += capincci_minor_opencount(nccip);
963
			mutex_unlock(&cdev->ncci_list_mtx);
L
Linus Torvalds 已提交
964 965 966 967 968 969 970 971 972 973 974 975 976 977
			return count;
		}
		return 0;

#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
	case CAPI_NCCI_GETUNIT:
		{
			struct capincci *nccip;
			struct capiminor *mp;
			unsigned ncci;
			int unit = 0;
			if (copy_from_user(&ncci, argp,
					   sizeof(ncci)))
				return -EFAULT;
978
			mutex_lock(&cdev->ncci_list_mtx);
L
Linus Torvalds 已提交
979
			nccip = capincci_find(cdev, (u32) ncci);
980
			if (!nccip || (mp = nccip->minorp) == NULL) {
981
				mutex_unlock(&cdev->ncci_list_mtx);
L
Linus Torvalds 已提交
982 983 984
				return -ESRCH;
			}
			unit = mp->minor;
985
			mutex_unlock(&cdev->ncci_list_mtx);
L
Linus Torvalds 已提交
986 987 988 989 990 991 992 993 994 995 996
			return unit;
		}
		return 0;
#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
	}
	return -EINVAL;
}

static int
capi_open(struct inode *inode, struct file *file)
{
J
Jonathan Corbet 已提交
997 998 999
	int ret;
	
	lock_kernel();
L
Linus Torvalds 已提交
1000
	if (file->private_data)
J
Jonathan Corbet 已提交
1001 1002 1003 1004 1005 1006 1007
		ret = -EEXIST;
	else if ((file->private_data = capidev_alloc()) == NULL)
		ret = -ENOMEM;
	else
		ret = nonseekable_open(inode, file);
	unlock_kernel();
	return ret;
L
Linus Torvalds 已提交
1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
}

static int
capi_release(struct inode *inode, struct file *file)
{
	struct capidev *cdev = (struct capidev *)file->private_data;

	capidev_free(cdev);
	file->private_data = NULL;
	
	return 0;
}

1021
static const struct file_operations capi_fops =
L
Linus Torvalds 已提交
1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038
{
	.owner		= THIS_MODULE,
	.llseek		= no_llseek,
	.read		= capi_read,
	.write		= capi_write,
	.poll		= capi_poll,
	.ioctl		= capi_ioctl,
	.open		= capi_open,
	.release	= capi_release,
};

#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
/* -------- tty_operations for capincci ----------------------------- */

static int capinc_tty_open(struct tty_struct * tty, struct file * file)
{
	struct capiminor *mp;
1039
	unsigned long flags;
L
Linus Torvalds 已提交
1040

1041
	if ((mp = capiminor_find(iminor(file->f_path.dentry->d_inode))) == NULL)
L
Linus Torvalds 已提交
1042
		return -ENXIO;
1043
	if (mp->nccip == NULL)
L
Linus Torvalds 已提交
1044 1045 1046 1047
		return -ENXIO;

	tty->driver_data = (void *)mp;

1048
	spin_lock_irqsave(&workaround_lock, flags);
L
Linus Torvalds 已提交
1049 1050 1051 1052 1053 1054 1055
	if (atomic_read(&mp->ttyopencount) == 0)
		mp->tty = tty;
	atomic_inc(&mp->ttyopencount);
#ifdef _DEBUG_REFCOUNT
	printk(KERN_DEBUG "capinc_tty_open ocount=%d\n", atomic_read(&mp->ttyopencount));
#endif
	handle_minor_recv(mp);
1056
	spin_unlock_irqrestore(&workaround_lock, flags);
L
Linus Torvalds 已提交
1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075
	return 0;
}

static void capinc_tty_close(struct tty_struct * tty, struct file * file)
{
	struct capiminor *mp;

	mp = (struct capiminor *)tty->driver_data;
	if (mp)	{
		if (atomic_dec_and_test(&mp->ttyopencount)) {
#ifdef _DEBUG_REFCOUNT
			printk(KERN_DEBUG "capinc_tty_close lastclose\n");
#endif
			tty->driver_data = NULL;
			mp->tty = NULL;
		}
#ifdef _DEBUG_REFCOUNT
		printk(KERN_DEBUG "capinc_tty_close ocount=%d\n", atomic_read(&mp->ttyopencount));
#endif
1076
		if (mp->nccip == NULL)
L
Linus Torvalds 已提交
1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089
			capiminor_free(mp);
	}

#ifdef _DEBUG_REFCOUNT
	printk(KERN_DEBUG "capinc_tty_close\n");
#endif
}

static int capinc_tty_write(struct tty_struct * tty,
			    const unsigned char *buf, int count)
{
	struct capiminor *mp = (struct capiminor *)tty->driver_data;
	struct sk_buff *skb;
1090
	unsigned long flags;
L
Linus Torvalds 已提交
1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102

#ifdef _DEBUG_TTYFUNCS
	printk(KERN_DEBUG "capinc_tty_write(count=%d)\n", count);
#endif

	if (!mp || !mp->nccip) {
#ifdef _DEBUG_TTYFUNCS
		printk(KERN_DEBUG "capinc_tty_write: mp or mp->ncci NULL\n");
#endif
		return 0;
	}

1103
	spin_lock_irqsave(&workaround_lock, flags);
L
Linus Torvalds 已提交
1104 1105 1106 1107 1108 1109 1110 1111 1112 1113
	skb = mp->ttyskb;
	if (skb) {
		mp->ttyskb = NULL;
		skb_queue_tail(&mp->outqueue, skb);
		mp->outbytes += skb->len;
	}

	skb = alloc_skb(CAPI_DATA_B3_REQ_LEN+count, GFP_ATOMIC);
	if (!skb) {
		printk(KERN_ERR "capinc_tty_write: alloc_skb failed\n");
1114
		spin_unlock_irqrestore(&workaround_lock, flags);
L
Linus Torvalds 已提交
1115 1116 1117 1118 1119 1120 1121 1122 1123 1124
		return -ENOMEM;
	}

	skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
	memcpy(skb_put(skb, count), buf, count);

	skb_queue_tail(&mp->outqueue, skb);
	mp->outbytes += skb->len;
	(void)handle_minor_send(mp);
	(void)handle_minor_recv(mp);
1125
	spin_unlock_irqrestore(&workaround_lock, flags);
L
Linus Torvalds 已提交
1126 1127 1128
	return count;
}

A
Alan Cox 已提交
1129
static int capinc_tty_put_char(struct tty_struct *tty, unsigned char ch)
L
Linus Torvalds 已提交
1130 1131 1132
{
	struct capiminor *mp = (struct capiminor *)tty->driver_data;
	struct sk_buff *skb;
1133
	unsigned long flags;
A
Alan Cox 已提交
1134
	int ret = 1;
L
Linus Torvalds 已提交
1135 1136 1137 1138 1139 1140 1141 1142 1143

#ifdef _DEBUG_TTYFUNCS
	printk(KERN_DEBUG "capinc_put_char(%u)\n", ch);
#endif

	if (!mp || !mp->nccip) {
#ifdef _DEBUG_TTYFUNCS
		printk(KERN_DEBUG "capinc_tty_put_char: mp or mp->ncci NULL\n");
#endif
A
Alan Cox 已提交
1144
		return 0;
L
Linus Torvalds 已提交
1145 1146
	}

1147
	spin_lock_irqsave(&workaround_lock, flags);
L
Linus Torvalds 已提交
1148 1149 1150 1151
	skb = mp->ttyskb;
	if (skb) {
		if (skb_tailroom(skb) > 0) {
			*(skb_put(skb, 1)) = ch;
1152
			spin_unlock_irqrestore(&workaround_lock, flags);
A
Alan Cox 已提交
1153
			return 1;
L
Linus Torvalds 已提交
1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166
		}
		mp->ttyskb = NULL;
		skb_queue_tail(&mp->outqueue, skb);
		mp->outbytes += skb->len;
		(void)handle_minor_send(mp);
	}
	skb = alloc_skb(CAPI_DATA_B3_REQ_LEN+CAPI_MAX_BLKSIZE, GFP_ATOMIC);
	if (skb) {
		skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
		*(skb_put(skb, 1)) = ch;
		mp->ttyskb = skb;
	} else {
		printk(KERN_ERR "capinc_put_char: char %u lost\n", ch);
A
Alan Cox 已提交
1167
		ret = 0;
L
Linus Torvalds 已提交
1168
	}
1169
	spin_unlock_irqrestore(&workaround_lock, flags);
A
Alan Cox 已提交
1170
	return ret;
L
Linus Torvalds 已提交
1171 1172 1173 1174 1175 1176
}

static void capinc_tty_flush_chars(struct tty_struct *tty)
{
	struct capiminor *mp = (struct capiminor *)tty->driver_data;
	struct sk_buff *skb;
1177
	unsigned long flags;
L
Linus Torvalds 已提交
1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189

#ifdef _DEBUG_TTYFUNCS
	printk(KERN_DEBUG "capinc_tty_flush_chars\n");
#endif

	if (!mp || !mp->nccip) {
#ifdef _DEBUG_TTYFUNCS
		printk(KERN_DEBUG "capinc_tty_flush_chars: mp or mp->ncci NULL\n");
#endif
		return;
	}

1190
	spin_lock_irqsave(&workaround_lock, flags);
L
Linus Torvalds 已提交
1191 1192 1193 1194 1195 1196 1197 1198
	skb = mp->ttyskb;
	if (skb) {
		mp->ttyskb = NULL;
		skb_queue_tail(&mp->outqueue, skb);
		mp->outbytes += skb->len;
		(void)handle_minor_send(mp);
	}
	(void)handle_minor_recv(mp);
1199
	spin_unlock_irqrestore(&workaround_lock, flags);
L
Linus Torvalds 已提交
1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219
}

static int capinc_tty_write_room(struct tty_struct *tty)
{
	struct capiminor *mp = (struct capiminor *)tty->driver_data;
	int room;
	if (!mp || !mp->nccip) {
#ifdef _DEBUG_TTYFUNCS
		printk(KERN_DEBUG "capinc_tty_write_room: mp or mp->ncci NULL\n");
#endif
		return 0;
	}
	room = CAPINC_MAX_SENDQUEUE-skb_queue_len(&mp->outqueue);
	room *= CAPI_MAX_BLKSIZE;
#ifdef _DEBUG_TTYFUNCS
	printk(KERN_DEBUG "capinc_tty_write_room = %d\n", room);
#endif
	return room;
}

A
Adrian Bunk 已提交
1220
static int capinc_tty_chars_in_buffer(struct tty_struct *tty)
L
Linus Torvalds 已提交
1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243
{
	struct capiminor *mp = (struct capiminor *)tty->driver_data;
	if (!mp || !mp->nccip) {
#ifdef _DEBUG_TTYFUNCS
		printk(KERN_DEBUG "capinc_tty_chars_in_buffer: mp or mp->ncci NULL\n");
#endif
		return 0;
	}
#ifdef _DEBUG_TTYFUNCS
	printk(KERN_DEBUG "capinc_tty_chars_in_buffer = %d nack=%d sq=%d rq=%d\n",
			mp->outbytes, mp->nack,
			skb_queue_len(&mp->outqueue),
			skb_queue_len(&mp->inqueue));
#endif
	return mp->outbytes;
}

static int capinc_tty_ioctl(struct tty_struct *tty, struct file * file,
		    unsigned int cmd, unsigned long arg)
{
	int error = 0;
	switch (cmd) {
	default:
1244
		error = n_tty_ioctl_helper(tty, file, cmd, arg);
L
Linus Torvalds 已提交
1245 1246 1247 1248 1249
		break;
	}
	return error;
}

A
Alan Cox 已提交
1250
static void capinc_tty_set_termios(struct tty_struct *tty, struct ktermios * old)
L
Linus Torvalds 已提交
1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269
{
#ifdef _DEBUG_TTYFUNCS
	printk(KERN_DEBUG "capinc_tty_set_termios\n");
#endif
}

static void capinc_tty_throttle(struct tty_struct * tty)
{
	struct capiminor *mp = (struct capiminor *)tty->driver_data;
#ifdef _DEBUG_TTYFUNCS
	printk(KERN_DEBUG "capinc_tty_throttle\n");
#endif
	if (mp)
		mp->ttyinstop = 1;
}

static void capinc_tty_unthrottle(struct tty_struct * tty)
{
	struct capiminor *mp = (struct capiminor *)tty->driver_data;
1270
	unsigned long flags;
L
Linus Torvalds 已提交
1271 1272 1273 1274
#ifdef _DEBUG_TTYFUNCS
	printk(KERN_DEBUG "capinc_tty_unthrottle\n");
#endif
	if (mp) {
1275
		spin_lock_irqsave(&workaround_lock, flags);
L
Linus Torvalds 已提交
1276 1277
		mp->ttyinstop = 0;
		handle_minor_recv(mp);
1278
		spin_unlock_irqrestore(&workaround_lock, flags);
L
Linus Torvalds 已提交
1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295
	}
}

static void capinc_tty_stop(struct tty_struct *tty)
{
	struct capiminor *mp = (struct capiminor *)tty->driver_data;
#ifdef _DEBUG_TTYFUNCS
	printk(KERN_DEBUG "capinc_tty_stop\n");
#endif
	if (mp) {
		mp->ttyoutstop = 1;
	}
}

static void capinc_tty_start(struct tty_struct *tty)
{
	struct capiminor *mp = (struct capiminor *)tty->driver_data;
1296
	unsigned long flags;
L
Linus Torvalds 已提交
1297 1298 1299 1300
#ifdef _DEBUG_TTYFUNCS
	printk(KERN_DEBUG "capinc_tty_start\n");
#endif
	if (mp) {
1301
		spin_lock_irqsave(&workaround_lock, flags);
L
Linus Torvalds 已提交
1302 1303
		mp->ttyoutstop = 0;
		(void)handle_minor_send(mp);
1304
		spin_unlock_irqrestore(&workaround_lock, flags);
L
Linus Torvalds 已提交
1305 1306 1307 1308 1309 1310 1311 1312 1313 1314
	}
}

static void capinc_tty_hangup(struct tty_struct *tty)
{
#ifdef _DEBUG_TTYFUNCS
	printk(KERN_DEBUG "capinc_tty_hangup\n");
#endif
}

A
Alan Cox 已提交
1315
static int capinc_tty_break_ctl(struct tty_struct *tty, int state)
L
Linus Torvalds 已提交
1316 1317 1318 1319
{
#ifdef _DEBUG_TTYFUNCS
	printk(KERN_DEBUG "capinc_tty_break_ctl(%d)\n", state);
#endif
A
Alan Cox 已提交
1320
	return 0;
L
Linus Torvalds 已提交
1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345
}

static void capinc_tty_flush_buffer(struct tty_struct *tty)
{
#ifdef _DEBUG_TTYFUNCS
	printk(KERN_DEBUG "capinc_tty_flush_buffer\n");
#endif
}

static void capinc_tty_set_ldisc(struct tty_struct *tty)
{
#ifdef _DEBUG_TTYFUNCS
	printk(KERN_DEBUG "capinc_tty_set_ldisc\n");
#endif
}

static void capinc_tty_send_xchar(struct tty_struct *tty, char ch)
{
#ifdef _DEBUG_TTYFUNCS
	printk(KERN_DEBUG "capinc_tty_send_xchar(%d)\n", ch);
#endif
}

static struct tty_driver *capinc_tty_driver;

J
Jeff Dike 已提交
1346
static const struct tty_operations capinc_ops = {
L
Linus Torvalds 已提交
1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411
	.open = capinc_tty_open,
	.close = capinc_tty_close,
	.write = capinc_tty_write,
	.put_char = capinc_tty_put_char,
	.flush_chars = capinc_tty_flush_chars,
	.write_room = capinc_tty_write_room,
	.chars_in_buffer = capinc_tty_chars_in_buffer,
	.ioctl = capinc_tty_ioctl,
	.set_termios = capinc_tty_set_termios,
	.throttle = capinc_tty_throttle,
	.unthrottle = capinc_tty_unthrottle,
	.stop = capinc_tty_stop,
	.start = capinc_tty_start,
	.hangup = capinc_tty_hangup,
	.break_ctl = capinc_tty_break_ctl,
	.flush_buffer = capinc_tty_flush_buffer,
	.set_ldisc = capinc_tty_set_ldisc,
	.send_xchar = capinc_tty_send_xchar,
};

static int capinc_tty_init(void)
{
	struct tty_driver *drv;
	
	if (capi_ttyminors > CAPINC_MAX_PORTS)
		capi_ttyminors = CAPINC_MAX_PORTS;
	if (capi_ttyminors <= 0)
		capi_ttyminors = CAPINC_NR_PORTS;

	drv = alloc_tty_driver(capi_ttyminors);
	if (!drv)
		return -ENOMEM;

	drv->owner = THIS_MODULE;
	drv->driver_name = "capi_nc";
	drv->name = "capi";
	drv->major = capi_ttymajor;
	drv->minor_start = 0;
	drv->type = TTY_DRIVER_TYPE_SERIAL;
	drv->subtype = SERIAL_TYPE_NORMAL;
	drv->init_termios = tty_std_termios;
	drv->init_termios.c_iflag = ICRNL;
	drv->init_termios.c_oflag = OPOST | ONLCR;
	drv->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
	drv->init_termios.c_lflag = 0;
	drv->flags = TTY_DRIVER_REAL_RAW|TTY_DRIVER_RESET_TERMIOS;
	tty_set_operations(drv, &capinc_ops);
	if (tty_register_driver(drv)) {
		put_tty_driver(drv);
		printk(KERN_ERR "Couldn't register capi_nc driver\n");
		return -1;
	}
	capinc_tty_driver = drv;
	return 0;
}

static void capinc_tty_exit(void)
{
	struct tty_driver *drv = capinc_tty_driver;
	int retval;
	if ((retval = tty_unregister_driver(drv)))
		printk(KERN_ERR "capi: failed to unregister capi_nc driver (%d)\n", retval);
	put_tty_driver(drv);
}

1412 1413 1414 1415 1416 1417 1418 1419 1420 1421
#else /* !CONFIG_ISDN_CAPI_MIDDLEWARE */

static inline int capinc_tty_init(void)
{
	return 0;
}

static inline void capinc_tty_exit(void) { }

#endif /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
L
Linus Torvalds 已提交
1422 1423 1424 1425 1426 1427 1428

/* -------- /proc functions ----------------------------------------- */

/*
 * /proc/capi/capi20:
 *  minor applid nrecvctlpkt nrecvdatapkt nsendctlpkt nsenddatapkt
 */
1429
static int capi20_proc_show(struct seq_file *m, void *v)
L
Linus Torvalds 已提交
1430 1431 1432 1433 1434 1435 1436
{
        struct capidev *cdev;
	struct list_head *l;

	read_lock(&capidev_list_lock);
	list_for_each(l, &capidev_list) {
		cdev = list_entry(l, struct capidev, list);
1437
		seq_printf(m, "0 %d %lu %lu %lu %lu\n",
L
Linus Torvalds 已提交
1438 1439 1440 1441 1442 1443 1444
			cdev->ap.applid,
			cdev->ap.nrecvctlpkt,
			cdev->ap.nrecvdatapkt,
			cdev->ap.nsentctlpkt,
			cdev->ap.nsentdatapkt);
	}
	read_unlock(&capidev_list_lock);
1445
	return 0;
L
Linus Torvalds 已提交
1446 1447
}

1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460
static int capi20_proc_open(struct inode *inode, struct file *file)
{
	return single_open(file, capi20_proc_show, NULL);
}

static const struct file_operations capi20_proc_fops = {
	.owner		= THIS_MODULE,
	.open		= capi20_proc_open,
	.read		= seq_read,
	.llseek		= seq_lseek,
	.release	= single_release,
};

L
Linus Torvalds 已提交
1461 1462 1463 1464
/*
 * /proc/capi/capi20ncci:
 *  applid ncci
 */
1465
static int capi20ncci_proc_show(struct seq_file *m, void *v)
L
Linus Torvalds 已提交
1466 1467 1468 1469 1470 1471 1472 1473 1474
{
        struct capidev *cdev;
        struct capincci *np;
	struct list_head *l;

	read_lock(&capidev_list_lock);
	list_for_each(l, &capidev_list) {
		cdev = list_entry(l, struct capidev, list);
		for (np=cdev->nccis; np; np = np->next) {
1475
			seq_printf(m, "%d 0x%x\n",
L
Linus Torvalds 已提交
1476 1477 1478 1479 1480
				       cdev->ap.applid,
				       np->ncci);
		}
	}
	read_unlock(&capidev_list_lock);
1481
	return 0;
L
Linus Torvalds 已提交
1482 1483
}

1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494
static int capi20ncci_proc_open(struct inode *inode, struct file *file)
{
	return single_open(file, capi20ncci_proc_show, NULL);
}

static const struct file_operations capi20ncci_proc_fops = {
	.owner		= THIS_MODULE,
	.open		= capi20ncci_proc_open,
	.read		= seq_read,
	.llseek		= seq_lseek,
	.release	= single_release,
L
Linus Torvalds 已提交
1495 1496 1497 1498
};

static void __init proc_init(void)
{
1499 1500
	proc_create("capi/capi20", 0, NULL, &capi20_proc_fops);
	proc_create("capi/capi20ncci", 0, NULL, &capi20ncci_proc_fops);
L
Linus Torvalds 已提交
1501 1502 1503 1504
}

static void __exit proc_exit(void)
{
1505 1506
	remove_proc_entry("capi/capi20", NULL);
	remove_proc_entry("capi/capi20ncci", NULL);
L
Linus Torvalds 已提交
1507 1508 1509 1510 1511 1512 1513
}

/* -------- init function and module interface ---------------------- */


static int __init capi_init(void)
{
1514
	const char *compileinfo;
1515
	int major_ret;
L
Linus Torvalds 已提交
1516

1517 1518
	major_ret = register_chrdev(capi_major, "capi20", &capi_fops);
	if (major_ret < 0) {
L
Linus Torvalds 已提交
1519
		printk(KERN_ERR "capi20: unable to get major %d\n", capi_major);
1520
		return major_ret;
L
Linus Torvalds 已提交
1521
	}
1522
	capi_class = class_create(THIS_MODULE, "capi");
L
Linus Torvalds 已提交
1523 1524 1525 1526 1527
	if (IS_ERR(capi_class)) {
		unregister_chrdev(capi_major, "capi20");
		return PTR_ERR(capi_class);
	}

1528
	device_create(capi_class, NULL, MKDEV(capi_major, 0), NULL, "capi");
L
Linus Torvalds 已提交
1529 1530

	if (capinc_tty_init() < 0) {
1531
		device_destroy(capi_class, MKDEV(capi_major, 0));
1532
		class_destroy(capi_class);
L
Linus Torvalds 已提交
1533 1534 1535 1536 1537 1538 1539 1540
		unregister_chrdev(capi_major, "capi20");
		return -ENOMEM;
	}

	proc_init();

#if defined(CONFIG_ISDN_CAPI_CAPIFS) || defined(CONFIG_ISDN_CAPI_CAPIFS_MODULE)
        compileinfo = " (middleware+capifs)";
1541
#elif defined(CONFIG_ISDN_CAPI_MIDDLEWARE)
L
Linus Torvalds 已提交
1542 1543 1544 1545
        compileinfo = " (no capifs)";
#else
        compileinfo = " (no middleware)";
#endif
1546 1547
	printk(KERN_NOTICE "CAPI 2.0 started up with major %d%s\n",
	       capi_major, compileinfo);
L
Linus Torvalds 已提交
1548 1549 1550 1551 1552 1553 1554 1555

	return 0;
}

static void __exit capi_exit(void)
{
	proc_exit();

1556
	device_destroy(capi_class, MKDEV(capi_major, 0));
1557
	class_destroy(capi_class);
L
Linus Torvalds 已提交
1558 1559 1560 1561 1562 1563 1564
	unregister_chrdev(capi_major, "capi20");

	capinc_tty_exit();
}

module_init(capi_init);
module_exit(capi_exit);