capi.c 34.0 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 23 24 25 26 27 28 29 30
#include <linux/mm.h>
#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>
31
#include <linux/seq_file.h>
L
Linus Torvalds 已提交
32 33 34 35 36 37 38 39
#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 已提交
40

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

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

#undef _DEBUG_TTYFUNCS		/* call to tty_driver */
#undef _DEBUG_DATAFLOW		/* data flow */

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

52
static DEFINE_MUTEX(capi_mutex);
53
static struct class *capi_class;
A
Adrian Bunk 已提交
54
static int capi_major = 68;		/* allocated */
55 56 57

module_param_named(major, capi_major, uint, 0);

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

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

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;

79
struct ackqueue_entry {
80 81 82 83
	struct list_head	list;
	u16			datahandle;
};

L
Linus Torvalds 已提交
84
struct capiminor {
J
Jan Kiszka 已提交
85 86
	struct kref kref;

L
Linus Torvalds 已提交
87
	unsigned int      minor;
J
Jan Kiszka 已提交
88
	struct dentry *capifs_dentry;
L
Linus Torvalds 已提交
89

90 91 92 93
	struct capi20_appl	*ap;
	u32			ncci;
	atomic_t		datahandle;
	atomic_t		msgid;
L
Linus Torvalds 已提交
94

95
	struct tty_port port;
L
Linus Torvalds 已提交
96 97 98
	int                ttyinstop;
	int                ttyoutstop;

99 100 101 102 103 104
	struct sk_buff_head	inqueue;

	struct sk_buff_head	outqueue;
	int			outbytes;
	struct sk_buff		*outskb;
	spinlock_t		outlock;
L
Linus Torvalds 已提交
105 106

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

struct capincci {
113
	struct list_head list;
L
Linus Torvalds 已提交
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
	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;

130
	struct list_head nccis;
L
Linus Torvalds 已提交
131

132
	struct mutex lock;
L
Linus Torvalds 已提交
133 134 135 136
};

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

137
static DEFINE_MUTEX(capidev_list_lock);
L
Linus Torvalds 已提交
138 139 140
static LIST_HEAD(capidev_list);

#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
141

J
Jan Kiszka 已提交
142
static DEFINE_SPINLOCK(capiminors_lock);
143
static struct capiminor **capiminors;
L
Linus Torvalds 已提交
144

145 146
static struct tty_driver *capinc_tty_driver;

L
Linus Torvalds 已提交
147 148
/* -------- datahandles --------------------------------------------- */

149
static int capiminor_add_ack(struct capiminor *mp, u16 datahandle)
L
Linus Torvalds 已提交
150
{
151
	struct ackqueue_entry *n;
L
Linus Torvalds 已提交
152 153

	n = kmalloc(sizeof(*n), GFP_ATOMIC);
154 155 156
	if (unlikely(!n)) {
		printk(KERN_ERR "capi: alloc datahandle failed\n");
		return -1;
L
Linus Torvalds 已提交
157 158
	}
	n->datahandle = datahandle;
159
	INIT_LIST_HEAD(&n->list);
J
Jan Kiszka 已提交
160
	spin_lock_bh(&mp->ackqlock);
161
	list_add_tail(&n->list, &mp->ackqueue);
L
Linus Torvalds 已提交
162
	mp->nack++;
J
Jan Kiszka 已提交
163
	spin_unlock_bh(&mp->ackqlock);
L
Linus Torvalds 已提交
164 165 166 167 168
	return 0;
}

static int capiminor_del_ack(struct capiminor *mp, u16 datahandle)
{
169
	struct ackqueue_entry *p, *tmp;
L
Linus Torvalds 已提交
170

J
Jan Kiszka 已提交
171
	spin_lock_bh(&mp->ackqlock);
172 173 174
	list_for_each_entry_safe(p, tmp, &mp->ackqueue, list) {
 		if (p->datahandle == datahandle) {
			list_del(&p->list);
L
Linus Torvalds 已提交
175
			mp->nack--;
J
Jan Kiszka 已提交
176 177
			spin_unlock_bh(&mp->ackqlock);
			kfree(p);
L
Linus Torvalds 已提交
178 179 180
			return 0;
		}
	}
J
Jan Kiszka 已提交
181
	spin_unlock_bh(&mp->ackqlock);
L
Linus Torvalds 已提交
182 183 184 185 186
	return -1;
}

static void capiminor_del_all_ack(struct capiminor *mp)
{
187
	struct ackqueue_entry *p, *tmp;
L
Linus Torvalds 已提交
188

189 190
	list_for_each_entry_safe(p, tmp, &mp->ackqueue, list) {
		list_del(&p->list);
L
Linus Torvalds 已提交
191 192 193 194 195 196 197 198
		kfree(p);
		mp->nack--;
	}
}


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

199 200
static const struct tty_port_operations capiminor_port_ops; /* we have none */

L
Linus Torvalds 已提交
201 202
static struct capiminor *capiminor_alloc(struct capi20_appl *ap, u32 ncci)
{
203
	struct capiminor *mp;
204
	struct device *dev;
205
	unsigned int minor;
L
Linus Torvalds 已提交
206

207
	mp = kzalloc(sizeof(*mp), GFP_KERNEL);
L
Linus Torvalds 已提交
208 209 210 211 212
  	if (!mp) {
  		printk(KERN_ERR "capi: can't alloc capiminor\n");
		return NULL;
	}

J
Jan Kiszka 已提交
213 214
	kref_init(&mp->kref);

L
Linus Torvalds 已提交
215 216
	mp->ap = ap;
	mp->ncci = ncci;
217 218
	INIT_LIST_HEAD(&mp->ackqueue);
	spin_lock_init(&mp->ackqlock);
L
Linus Torvalds 已提交
219 220 221

	skb_queue_head_init(&mp->inqueue);
	skb_queue_head_init(&mp->outqueue);
222
	spin_lock_init(&mp->outlock);
L
Linus Torvalds 已提交
223

224 225 226
	tty_port_init(&mp->port);
	mp->port.ops = &capiminor_port_ops;

227
	/* Allocate the least unused minor number. */
J
Jan Kiszka 已提交
228
	spin_lock(&capiminors_lock);
229 230 231 232
	for (minor = 0; minor < capi_ttyminors; minor++)
		if (!capiminors[minor]) {
			capiminors[minor] = mp;
			break;
L
Linus Torvalds 已提交
233
		}
J
Jan Kiszka 已提交
234
	spin_unlock(&capiminors_lock);
L
Linus Torvalds 已提交
235

236
	if (minor == capi_ttyminors) {
L
Linus Torvalds 已提交
237
		printk(KERN_NOTICE "capi: out of minors\n");
238
		goto err_out1;
L
Linus Torvalds 已提交
239 240
	}

241 242
	mp->minor = minor;

243 244 245 246
	dev = tty_register_device(capinc_tty_driver, minor, NULL);
	if (IS_ERR(dev))
		goto err_out2;

L
Linus Torvalds 已提交
247
	return mp;
248 249

err_out2:
J
Jan Kiszka 已提交
250
	spin_lock(&capiminors_lock);
251
	capiminors[minor] = NULL;
J
Jan Kiszka 已提交
252
	spin_unlock(&capiminors_lock);
253 254 255 256

err_out1:
	kfree(mp);
	return NULL;
L
Linus Torvalds 已提交
257 258
}

J
Jan Kiszka 已提交
259
static void capiminor_destroy(struct kref *kref)
L
Linus Torvalds 已提交
260
{
J
Jan Kiszka 已提交
261
	struct capiminor *mp = container_of(kref, struct capiminor, kref);
L
Linus Torvalds 已提交
262

263
	kfree_skb(mp->outskb);
L
Linus Torvalds 已提交
264 265 266 267 268 269
	skb_queue_purge(&mp->inqueue);
	skb_queue_purge(&mp->outqueue);
	capiminor_del_all_ack(mp);
	kfree(mp);
}

270
static struct capiminor *capiminor_get(unsigned int minor)
L
Linus Torvalds 已提交
271
{
272
	struct capiminor *mp;
L
Linus Torvalds 已提交
273

J
Jan Kiszka 已提交
274
	spin_lock(&capiminors_lock);
275
	mp = capiminors[minor];
J
Jan Kiszka 已提交
276 277
	if (mp)
		kref_get(&mp->kref);
J
Jan Kiszka 已提交
278
	spin_unlock(&capiminors_lock);
279 280

	return mp;
L
Linus Torvalds 已提交
281 282
}

J
Jan Kiszka 已提交
283 284 285 286 287 288 289 290 291
static inline void capiminor_put(struct capiminor *mp)
{
	kref_put(&mp->kref, capiminor_destroy);
}

static void capiminor_free(struct capiminor *mp)
{
	tty_unregister_device(capinc_tty_driver, mp->minor);

J
Jan Kiszka 已提交
292
	spin_lock(&capiminors_lock);
J
Jan Kiszka 已提交
293
	capiminors[mp->minor] = NULL;
J
Jan Kiszka 已提交
294
	spin_unlock(&capiminors_lock);
J
Jan Kiszka 已提交
295 296 297 298

	capiminor_put(mp);
}

L
Linus Torvalds 已提交
299 300
/* -------- struct capincci ----------------------------------------- */

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

306 307 308 309
	if (!(cdev->userflags & CAPIFLAG_HIGHJACKING))
		return;

	mp = np->minorp = capiminor_alloc(&cdev->ap, np->ncci);
L
Linus Torvalds 已提交
310
	if (mp) {
311 312
		device = MKDEV(capinc_tty_driver->major, mp->minor);
		mp->capifs_dentry = capifs_new_ncci(mp->minor, device);
L
Linus Torvalds 已提交
313
	}
314 315 316 317 318
}

static void capincci_free_minor(struct capincci *np)
{
	struct capiminor *mp = np->minorp;
319
	struct tty_struct *tty;
320 321 322

	if (mp) {
		capifs_free_ncci(mp->capifs_dentry);
323 324 325

		tty = tty_port_tty_get(&mp->port);
		if (tty) {
326
			tty_vhangup(tty);
327
			tty_kref_put(tty);
328
		}
329 330

		capiminor_free(mp);
331 332 333 334 335 336
	}
}

static inline unsigned int capincci_minor_opencount(struct capincci *np)
{
	struct capiminor *mp = np->minorp;
J
Jan Kiszka 已提交
337 338
	unsigned int count = 0;
	struct tty_struct *tty;
339

J
Jan Kiszka 已提交
340 341 342 343 344 345 346 347
	if (mp) {
		tty = tty_port_tty_get(&mp->port);
		if (tty) {
			count = tty->count;
			tty_kref_put(tty);
		}
	}
	return count;
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
}

#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)
{
365
	struct capincci *np;
366

367
	np = kzalloc(sizeof(*np), GFP_KERNEL);
368 369 370 371 372 373 374
	if (!np)
		return NULL;
	np->ncci = ncci;
	np->cdev = cdev;

	capincci_alloc_minor(cdev, np);

375 376 377
	list_add_tail(&np->list, &cdev->nccis);

	return np;
L
Linus Torvalds 已提交
378 379 380 381
}

static void capincci_free(struct capidev *cdev, u32 ncci)
{
382
	struct capincci *np, *tmp;
L
Linus Torvalds 已提交
383

384
	list_for_each_entry_safe(np, tmp, &cdev->nccis, list)
L
Linus Torvalds 已提交
385
		if (ncci == 0xffffffff || np->ncci == ncci) {
386
			capincci_free_minor(np);
387
			list_del(&np->list);
L
Linus Torvalds 已提交
388 389 390 391 392 393
			kfree(np);
		}
}

static struct capincci *capincci_find(struct capidev *cdev, u32 ncci)
{
394
	struct capincci *np;
L
Linus Torvalds 已提交
395

396 397 398 399
	list_for_each_entry(np, &cdev->nccis, list)
		if (np->ncci == ncci)
			return np;
	return NULL;
L
Linus Torvalds 已提交
400 401 402 403 404 405 406 407 408
}

#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;
409
	nskb = alloc_skb(CAPI_DATA_B3_RESP_LEN, GFP_KERNEL);
L
Linus Torvalds 已提交
410 411 412 413 414 415 416
	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);
417
		capimsg_setu16(s, 6, atomic_inc_return(&mp->msgid));
L
Linus Torvalds 已提交
418 419 420 421 422 423 424 425
		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)
{
J
Jan Kiszka 已提交
426
	unsigned int datalen = skb->len - CAPIMSG_LEN(skb->data);
427
	struct tty_struct *tty;
L
Linus Torvalds 已提交
428 429 430
	struct sk_buff *nskb;
	u16 errcode, datahandle;
	struct tty_ldisc *ld;
431 432 433 434
	int ret = -1;

	tty = tty_port_tty_get(&mp->port);
	if (!tty) {
L
Linus Torvalds 已提交
435 436 437 438 439 440
#ifdef _DEBUG_DATAFLOW
		printk(KERN_DEBUG "capi: currently no receiver\n");
#endif
		return -1;
	}
	
441
	ld = tty_ldisc_ref(tty);
J
Jan Kiszka 已提交
442 443 444 445 446 447
	if (!ld) {
		/* fatal error, do not requeue */
		ret = 0;
		kfree_skb(skb);
		goto deref_tty;
	}
448

A
Alan Cox 已提交
449
	if (ld->ops->receive_buf == NULL) {
L
Linus Torvalds 已提交
450 451 452
#if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
		printk(KERN_DEBUG "capi: ldisc has no receive_buf function\n");
#endif
J
Jan Kiszka 已提交
453 454
		/* fatal error, do not requeue */
		goto free_skb;
L
Linus Torvalds 已提交
455 456 457 458 459
	}
	if (mp->ttyinstop) {
#if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
		printk(KERN_DEBUG "capi: recv tty throttled\n");
#endif
J
Jan Kiszka 已提交
460
		goto deref_ldisc;
L
Linus Torvalds 已提交
461
	}
J
Jan Kiszka 已提交
462

463
	if (tty->receive_room < datalen) {
L
Linus Torvalds 已提交
464 465 466
#if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
		printk(KERN_DEBUG "capi: no room in tty\n");
#endif
J
Jan Kiszka 已提交
467
		goto deref_ldisc;
L
Linus Torvalds 已提交
468
	}
J
Jan Kiszka 已提交
469 470 471

	nskb = gen_data_b3_resp_for(mp, skb);
	if (!nskb) {
L
Linus Torvalds 已提交
472
		printk(KERN_ERR "capi: gen_data_b3_resp failed\n");
J
Jan Kiszka 已提交
473
		goto deref_ldisc;
L
Linus Torvalds 已提交
474
	}
J
Jan Kiszka 已提交
475 476 477

	datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN + 4);

L
Linus Torvalds 已提交
478
	errcode = capi20_put_message(mp->ap, nskb);
J
Jan Kiszka 已提交
479 480 481 482 483 484 485 486 487

	if (errcode == CAPI_NOERROR) {
		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
		ld->ops->receive_buf(tty, skb->data, NULL, skb->len);
	} else {
L
Linus Torvalds 已提交
488 489 490
		printk(KERN_ERR "capi: send DATA_B3_RESP failed=%x\n",
				errcode);
		kfree_skb(nskb);
J
Jan Kiszka 已提交
491 492 493

		if (errcode == CAPI_SENDQUEUEFULL)
			goto deref_ldisc;
L
Linus Torvalds 已提交
494
	}
J
Jan Kiszka 已提交
495 496

free_skb:
497
	ret = 0;
J
Jan Kiszka 已提交
498 499 500
	kfree_skb(skb);

deref_ldisc:
L
Linus Torvalds 已提交
501
	tty_ldisc_deref(ld);
J
Jan Kiszka 已提交
502 503

deref_tty:
504 505
	tty_kref_put(tty);
	return ret;
L
Linus Torvalds 已提交
506 507 508 509 510
}

static void handle_minor_recv(struct capiminor *mp)
{
	struct sk_buff *skb;
511 512

	while ((skb = skb_dequeue(&mp->inqueue)) != NULL)
L
Linus Torvalds 已提交
513 514 515 516 517 518
		if (handle_recv_skb(mp, skb) < 0) {
			skb_queue_head(&mp->inqueue, skb);
			return;
		}
}

519
static void handle_minor_send(struct capiminor *mp)
L
Linus Torvalds 已提交
520
{
521
	struct tty_struct *tty;
L
Linus Torvalds 已提交
522 523 524 525 526
	struct sk_buff *skb;
	u16 len;
	u16 errcode;
	u16 datahandle;

527 528
	tty = tty_port_tty_get(&mp->port);
	if (!tty)
529
		return;
530 531

	if (mp->ttyoutstop) {
L
Linus Torvalds 已提交
532 533 534
#if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
		printk(KERN_DEBUG "capi: send: tty stopped\n");
#endif
535
		tty_kref_put(tty);
536
		return;
L
Linus Torvalds 已提交
537 538
	}

539 540 541 542 543 544 545
	while (1) {
		spin_lock_bh(&mp->outlock);
		skb = __skb_dequeue(&mp->outqueue);
		if (!skb) {
			spin_unlock_bh(&mp->outlock);
			break;
		}
L
Linus Torvalds 已提交
546
		len = (u16)skb->len;
547 548 549 550
		mp->outbytes -= len;
		spin_unlock_bh(&mp->outlock);

		datahandle = atomic_inc_return(&mp->datahandle);
L
Linus Torvalds 已提交
551 552 553 554 555 556
		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);
557
		capimsg_setu16(skb->data, 6, atomic_inc_return(&mp->msgid));
L
Linus Torvalds 已提交
558
		capimsg_setu32(skb->data, 8, mp->ncci);	/* NCCI */
A
Andrew Morton 已提交
559
		capimsg_setu32(skb->data, 12, (u32)(long)skb->data);/* Data32 */
L
Linus Torvalds 已提交
560 561 562 563
		capimsg_setu16(skb->data, 16, len);	/* Data length */
		capimsg_setu16(skb->data, 18, datahandle);
		capimsg_setu16(skb->data, 20, 0);	/* Flags */

564
		if (capiminor_add_ack(mp, datahandle) < 0) {
L
Linus Torvalds 已提交
565
			skb_pull(skb, CAPI_DATA_B3_REQ_LEN);
566 567 568 569 570 571

			spin_lock_bh(&mp->outlock);
			__skb_queue_head(&mp->outqueue, skb);
			mp->outbytes += len;
			spin_unlock_bh(&mp->outlock);

572
			break;
L
Linus Torvalds 已提交
573 574 575 576 577 578 579 580 581 582 583 584 585
		}
		errcode = capi20_put_message(mp->ap, skb);
		if (errcode == CAPI_NOERROR) {
#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);
586 587 588 589 590 591

			spin_lock_bh(&mp->outlock);
			__skb_queue_head(&mp->outqueue, skb);
			mp->outbytes += len;
			spin_unlock_bh(&mp->outlock);

L
Linus Torvalds 已提交
592 593 594 595 596 597 598
			break;
		}

		/* ups, drop packet */
		printk(KERN_ERR "capi: put_message = %x\n", errcode);
		kfree_skb(skb);
	}
599
	tty_kref_put(tty);
L
Linus Torvalds 已提交
600 601 602 603 604 605 606 607 608
}

#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
609
	struct tty_struct *tty;
L
Linus Torvalds 已提交
610 611 612 613 614
	struct capiminor *mp;
	u16 datahandle;
#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
	struct capincci *np;

615 616
	mutex_lock(&cdev->lock);

L
Linus Torvalds 已提交
617 618
	if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_CONF) {
		u16 info = CAPIMSG_U16(skb->data, 12); // Info field
619
		if ((info & 0xff00) == 0)
L
Linus Torvalds 已提交
620 621
			capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
	}
622
	if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_IND)
L
Linus Torvalds 已提交
623
		capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
624

L
Linus Torvalds 已提交
625 626 627
	if (CAPIMSG_COMMAND(skb->data) != CAPI_DATA_B3) {
		skb_queue_tail(&cdev->recvqueue, skb);
		wake_up_interruptible(&cdev->recvwait);
628
		goto unlock_out;
L
Linus Torvalds 已提交
629
	}
630 631

	np = capincci_find(cdev, CAPIMSG_CONTROL(skb->data));
L
Linus Torvalds 已提交
632 633 634 635
	if (!np) {
		printk(KERN_ERR "BUG: capi_signal: ncci not found\n");
		skb_queue_tail(&cdev->recvqueue, skb);
		wake_up_interruptible(&cdev->recvwait);
636
		goto unlock_out;
L
Linus Torvalds 已提交
637
	}
638

L
Linus Torvalds 已提交
639 640 641
#ifndef CONFIG_ISDN_CAPI_MIDDLEWARE
	skb_queue_tail(&cdev->recvqueue, skb);
	wake_up_interruptible(&cdev->recvwait);
642

L
Linus Torvalds 已提交
643
#else /* CONFIG_ISDN_CAPI_MIDDLEWARE */
644

L
Linus Torvalds 已提交
645 646 647 648
	mp = np->minorp;
	if (!mp) {
		skb_queue_tail(&cdev->recvqueue, skb);
		wake_up_interruptible(&cdev->recvwait);
649
		goto unlock_out;
L
Linus Torvalds 已提交
650 651 652 653 654 655 656 657
	}
	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);
658

L
Linus Torvalds 已提交
659 660 661 662 663 664 665 666 667 668 669
		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);
J
Jan Kiszka 已提交
670
		capiminor_del_ack(mp, datahandle);
671 672 673 674 675
		tty = tty_port_tty_get(&mp->port);
		if (tty) {
			tty_wakeup(tty);
			tty_kref_put(tty);
		}
676
		handle_minor_send(mp);
L
Linus Torvalds 已提交
677 678 679 680 681 682 683

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

685 686
unlock_out:
	mutex_unlock(&cdev->lock);
L
Linus Torvalds 已提交
687 688 689 690 691 692 693
}

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

static ssize_t
capi_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
{
694
	struct capidev *cdev = file->private_data;
L
Linus Torvalds 已提交
695 696
	struct sk_buff *skb;
	size_t copied;
J
Jan Kiszka 已提交
697
	int err;
L
Linus Torvalds 已提交
698 699 700 701

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

J
Jan Kiszka 已提交
702 703
	skb = skb_dequeue(&cdev->recvqueue);
	if (!skb) {
L
Linus Torvalds 已提交
704 705
		if (file->f_flags & O_NONBLOCK)
			return -EAGAIN;
J
Jan Kiszka 已提交
706 707 708 709
		err = wait_event_interruptible(cdev->recvwait,
				(skb = skb_dequeue(&cdev->recvqueue)));
		if (err)
			return err;
L
Linus Torvalds 已提交
710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728
	}
	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)
{
729
	struct capidev *cdev = file->private_data;
L
Linus Torvalds 已提交
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
	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) {
759
		mutex_lock(&cdev->lock);
L
Linus Torvalds 已提交
760
		capincci_free(cdev, CAPIMSG_NCCI(skb->data));
761
		mutex_unlock(&cdev->lock);
L
Linus Torvalds 已提交
762 763 764 765 766 767 768 769 770 771 772 773 774 775
	}

	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)
{
776
	struct capidev *cdev = file->private_data;
L
Linus Torvalds 已提交
777 778 779 780 781 782 783 784 785 786 787 788 789
	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
790
capi_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
L
Linus Torvalds 已提交
791 792 793 794 795 796 797 798
{
	struct capidev *cdev = file->private_data;
	capi_ioctl_struct data;
	int retval = -EINVAL;
	void __user *argp = (void __user *)arg;

	switch (cmd) {
	case CAPI_REGISTER:
799
		mutex_lock(&cdev->lock);
L
Linus Torvalds 已提交
800

801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816
		if (cdev->ap.applid) {
			retval = -EEXIST;
			goto register_out;
		}
		if (copy_from_user(&cdev->ap.rparam, argp,
				   sizeof(struct capi_register_params))) {
			retval = -EFAULT;
			goto register_out;
		}
		cdev->ap.private = cdev;
		cdev->ap.recv_message = capi_recv_message;
		cdev->errcode = capi20_register(&cdev->ap);
		retval = (int)cdev->ap.applid;
		if (cdev->errcode) {
			cdev->ap.applid = 0;
			retval = -EIO;
L
Linus Torvalds 已提交
817
		}
818 819 820 821

register_out:
		mutex_unlock(&cdev->lock);
		return retval;
L
Linus Torvalds 已提交
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

	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:
920 921 922 923 924
	case CAPI_CLR_FLAGS: {
		unsigned userflags;

		if (copy_from_user(&userflags, argp, sizeof(userflags)))
			return -EFAULT;
L
Linus Torvalds 已提交
925

926 927 928 929 930 931 932 933
		mutex_lock(&cdev->lock);
		if (cmd == CAPI_SET_FLAGS)
			cdev->userflags |= userflags;
		else
			cdev->userflags &= ~userflags;
		mutex_unlock(&cdev->lock);
		return 0;
	}
L
Linus Torvalds 已提交
934 935 936 937 938 939
	case CAPI_GET_FLAGS:
		if (copy_to_user(argp, &cdev->userflags,
				 sizeof(cdev->userflags)))
			return -EFAULT;
		return 0;

940 941 942 943
	case CAPI_NCCI_OPENCOUNT: {
		struct capincci *nccip;
		unsigned ncci;
		int count = 0;
L
Linus Torvalds 已提交
944

945 946 947 948 949 950 951 952 953 954
		if (copy_from_user(&ncci, argp, sizeof(ncci)))
			return -EFAULT;

		mutex_lock(&cdev->lock);
		nccip = capincci_find(cdev, (u32)ncci);
		if (nccip)
			count = capincci_minor_opencount(nccip);
		mutex_unlock(&cdev->lock);
		return count;
	}
L
Linus Torvalds 已提交
955 956

#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
957 958 959 960 961 962 963 964 965 966 967 968 969 970 971
	case CAPI_NCCI_GETUNIT: {
		struct capincci *nccip;
		struct capiminor *mp;
		unsigned ncci;
		int unit = -ESRCH;

		if (copy_from_user(&ncci, argp, sizeof(ncci)))
			return -EFAULT;

		mutex_lock(&cdev->lock);
		nccip = capincci_find(cdev, (u32)ncci);
		if (nccip) {
			mp = nccip->minorp;
			if (mp)
				unit = mp->minor;
L
Linus Torvalds 已提交
972
		}
973 974 975
		mutex_unlock(&cdev->lock);
		return unit;
	}
L
Linus Torvalds 已提交
976
#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
977 978 979

	default:
		return -EINVAL;
L
Linus Torvalds 已提交
980 981 982
	}
}

983 984 985 986 987
static long
capi_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
	int ret;

988
	mutex_lock(&capi_mutex);
989
	ret = capi_ioctl(file, cmd, arg);
990
	mutex_unlock(&capi_mutex);
991 992 993 994

	return ret;
}

J
Jan Kiszka 已提交
995
static int capi_open(struct inode *inode, struct file *file)
L
Linus Torvalds 已提交
996
{
J
Jan Kiszka 已提交
997 998 999 1000 1001 1002
	struct capidev *cdev;

	cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
	if (!cdev)
		return -ENOMEM;

1003
	mutex_init(&cdev->lock);
J
Jan Kiszka 已提交
1004 1005
	skb_queue_head_init(&cdev->recvqueue);
	init_waitqueue_head(&cdev->recvwait);
1006
	INIT_LIST_HEAD(&cdev->nccis);
J
Jan Kiszka 已提交
1007 1008 1009 1010 1011 1012 1013
	file->private_data = cdev;

	mutex_lock(&capidev_list_lock);
	list_add_tail(&cdev->list, &capidev_list);
	mutex_unlock(&capidev_list_lock);

	return nonseekable_open(inode, file);
L
Linus Torvalds 已提交
1014 1015
}

J
Jan Kiszka 已提交
1016
static int capi_release(struct inode *inode, struct file *file)
L
Linus Torvalds 已提交
1017
{
J
Jan Kiszka 已提交
1018
	struct capidev *cdev = file->private_data;
L
Linus Torvalds 已提交
1019

J
Jan Kiszka 已提交
1020 1021 1022 1023
	mutex_lock(&capidev_list_lock);
	list_del(&cdev->list);
	mutex_unlock(&capidev_list_lock);

1024
	if (cdev->ap.applid)
J
Jan Kiszka 已提交
1025 1026 1027 1028 1029
		capi20_release(&cdev->ap);
	skb_queue_purge(&cdev->recvqueue);
	capincci_free(cdev, 0xffffffff);

	kfree(cdev);
L
Linus Torvalds 已提交
1030 1031 1032
	return 0;
}

1033
static const struct file_operations capi_fops =
L
Linus Torvalds 已提交
1034 1035 1036 1037 1038 1039
{
	.owner		= THIS_MODULE,
	.llseek		= no_llseek,
	.read		= capi_read,
	.write		= capi_write,
	.poll		= capi_poll,
1040
	.unlocked_ioctl	= capi_unlocked_ioctl,
L
Linus Torvalds 已提交
1041 1042 1043 1044 1045 1046 1047
	.open		= capi_open,
	.release	= capi_release,
};

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

1048 1049
static int
capinc_tty_install(struct tty_driver *driver, struct tty_struct *tty)
L
Linus Torvalds 已提交
1050
{
1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063
	int idx = tty->index;
	struct capiminor *mp = capiminor_get(idx);
	int ret = tty_init_termios(tty);

	if (ret == 0) {
		tty_driver_kref_get(driver);
		tty->count++;
		tty->driver_data = mp;
		driver->ttys[idx] = tty;
	} else
		capiminor_put(mp);
	return ret;
}
L
Linus Torvalds 已提交
1064

1065 1066 1067 1068 1069 1070
static void capinc_tty_cleanup(struct tty_struct *tty)
{
	struct capiminor *mp = tty->driver_data;
	tty->driver_data = NULL;
	capiminor_put(mp);
}
L
Linus Torvalds 已提交
1071

1072
static int capinc_tty_open(struct tty_struct *tty, struct file *filp)
1073 1074
{
	struct capiminor *mp = tty->driver_data;
1075 1076 1077 1078 1079
	int err;

	err = tty_port_open(&mp->port, tty, filp);
	if (err)
		return err;
L
Linus Torvalds 已提交
1080 1081 1082 1083 1084

	handle_minor_recv(mp);
	return 0;
}

1085
static void capinc_tty_close(struct tty_struct *tty, struct file *filp)
L
Linus Torvalds 已提交
1086
{
1087
	struct capiminor *mp = tty->driver_data;
L
Linus Torvalds 已提交
1088

1089
	tty_port_close(&mp->port, tty, filp);
L
Linus Torvalds 已提交
1090 1091
}

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

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

1102 1103
	spin_lock_bh(&mp->outlock);
	skb = mp->outskb;
L
Linus Torvalds 已提交
1104
	if (skb) {
1105 1106
		mp->outskb = NULL;
		__skb_queue_tail(&mp->outqueue, skb);
L
Linus Torvalds 已提交
1107 1108 1109 1110 1111 1112
		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");
1113
		spin_unlock_bh(&mp->outlock);
L
Linus Torvalds 已提交
1114 1115 1116 1117 1118 1119
		return -ENOMEM;
	}

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

1120
	__skb_queue_tail(&mp->outqueue, skb);
L
Linus Torvalds 已提交
1121
	mp->outbytes += skb->len;
1122 1123
	spin_unlock_bh(&mp->outlock);

1124
	handle_minor_send(mp);
1125

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
	struct capiminor *mp = tty->driver_data;
1132
	bool invoke_send = false;
L
Linus Torvalds 已提交
1133
	struct sk_buff *skb;
A
Alan Cox 已提交
1134
	int ret = 1;
L
Linus Torvalds 已提交
1135 1136 1137 1138 1139

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

1140 1141
	spin_lock_bh(&mp->outlock);
	skb = mp->outskb;
L
Linus Torvalds 已提交
1142 1143 1144
	if (skb) {
		if (skb_tailroom(skb) > 0) {
			*(skb_put(skb, 1)) = ch;
1145
			goto unlock_out;
L
Linus Torvalds 已提交
1146
		}
1147 1148
		mp->outskb = NULL;
		__skb_queue_tail(&mp->outqueue, skb);
L
Linus Torvalds 已提交
1149
		mp->outbytes += skb->len;
1150
		invoke_send = true;
L
Linus Torvalds 已提交
1151
	}
1152

L
Linus Torvalds 已提交
1153 1154 1155 1156
	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;
1157
		mp->outskb = skb;
L
Linus Torvalds 已提交
1158 1159
	} else {
		printk(KERN_ERR "capinc_put_char: char %u lost\n", ch);
A
Alan Cox 已提交
1160
		ret = 0;
L
Linus Torvalds 已提交
1161
	}
1162 1163 1164 1165 1166

unlock_out:
	spin_unlock_bh(&mp->outlock);

	if (invoke_send)
1167
		handle_minor_send(mp);
1168

A
Alan Cox 已提交
1169
	return ret;
L
Linus Torvalds 已提交
1170 1171 1172 1173
}

static void capinc_tty_flush_chars(struct tty_struct *tty)
{
1174
	struct capiminor *mp = tty->driver_data;
L
Linus Torvalds 已提交
1175 1176 1177 1178 1179 1180
	struct sk_buff *skb;

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

1181 1182
	spin_lock_bh(&mp->outlock);
	skb = mp->outskb;
L
Linus Torvalds 已提交
1183
	if (skb) {
1184 1185
		mp->outskb = NULL;
		__skb_queue_tail(&mp->outqueue, skb);
L
Linus Torvalds 已提交
1186
		mp->outbytes += skb->len;
1187 1188
		spin_unlock_bh(&mp->outlock);

1189
		handle_minor_send(mp);
1190 1191 1192 1193
	} else
		spin_unlock_bh(&mp->outlock);

	handle_minor_recv(mp);
L
Linus Torvalds 已提交
1194 1195 1196 1197
}

static int capinc_tty_write_room(struct tty_struct *tty)
{
1198
	struct capiminor *mp = tty->driver_data;
L
Linus Torvalds 已提交
1199
	int room;
1200

L
Linus Torvalds 已提交
1201 1202 1203 1204 1205 1206 1207 1208
	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 已提交
1209
static int capinc_tty_chars_in_buffer(struct tty_struct *tty)
L
Linus Torvalds 已提交
1210
{
1211 1212
	struct capiminor *mp = tty->driver_data;

L
Linus Torvalds 已提交
1213 1214 1215 1216 1217 1218 1219 1220 1221
#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;
}

1222
static int capinc_tty_ioctl(struct tty_struct *tty,
L
Linus Torvalds 已提交
1223 1224
		    unsigned int cmd, unsigned long arg)
{
1225
	return -ENOIOCTLCMD;
L
Linus Torvalds 已提交
1226 1227
}

A
Alan Cox 已提交
1228
static void capinc_tty_set_termios(struct tty_struct *tty, struct ktermios * old)
L
Linus Torvalds 已提交
1229 1230 1231 1232 1233 1234
{
#ifdef _DEBUG_TTYFUNCS
	printk(KERN_DEBUG "capinc_tty_set_termios\n");
#endif
}

1235
static void capinc_tty_throttle(struct tty_struct *tty)
L
Linus Torvalds 已提交
1236
{
1237
	struct capiminor *mp = tty->driver_data;
L
Linus Torvalds 已提交
1238 1239 1240
#ifdef _DEBUG_TTYFUNCS
	printk(KERN_DEBUG "capinc_tty_throttle\n");
#endif
1241
	mp->ttyinstop = 1;
L
Linus Torvalds 已提交
1242 1243
}

1244
static void capinc_tty_unthrottle(struct tty_struct *tty)
L
Linus Torvalds 已提交
1245
{
1246 1247
	struct capiminor *mp = tty->driver_data;

L
Linus Torvalds 已提交
1248 1249 1250
#ifdef _DEBUG_TTYFUNCS
	printk(KERN_DEBUG "capinc_tty_unthrottle\n");
#endif
1251 1252
	mp->ttyinstop = 0;
	handle_minor_recv(mp);
L
Linus Torvalds 已提交
1253 1254 1255 1256
}

static void capinc_tty_stop(struct tty_struct *tty)
{
1257 1258
	struct capiminor *mp = tty->driver_data;

L
Linus Torvalds 已提交
1259 1260 1261
#ifdef _DEBUG_TTYFUNCS
	printk(KERN_DEBUG "capinc_tty_stop\n");
#endif
1262
	mp->ttyoutstop = 1;
L
Linus Torvalds 已提交
1263 1264 1265 1266
}

static void capinc_tty_start(struct tty_struct *tty)
{
1267 1268
	struct capiminor *mp = tty->driver_data;

L
Linus Torvalds 已提交
1269 1270 1271
#ifdef _DEBUG_TTYFUNCS
	printk(KERN_DEBUG "capinc_tty_start\n");
#endif
1272
	mp->ttyoutstop = 0;
1273
	handle_minor_send(mp);
L
Linus Torvalds 已提交
1274 1275 1276 1277
}

static void capinc_tty_hangup(struct tty_struct *tty)
{
1278 1279
	struct capiminor *mp = tty->driver_data;

L
Linus Torvalds 已提交
1280 1281 1282
#ifdef _DEBUG_TTYFUNCS
	printk(KERN_DEBUG "capinc_tty_hangup\n");
#endif
1283
	tty_port_hangup(&mp->port);
L
Linus Torvalds 已提交
1284 1285
}

A
Alan Cox 已提交
1286
static int capinc_tty_break_ctl(struct tty_struct *tty, int state)
L
Linus Torvalds 已提交
1287 1288 1289 1290
{
#ifdef _DEBUG_TTYFUNCS
	printk(KERN_DEBUG "capinc_tty_break_ctl(%d)\n", state);
#endif
A
Alan Cox 已提交
1291
	return 0;
L
Linus Torvalds 已提交
1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314
}

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
}

J
Jeff Dike 已提交
1315
static const struct tty_operations capinc_ops = {
L
Linus Torvalds 已提交
1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333
	.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,
1334 1335
	.install = capinc_tty_install,
	.cleanup = capinc_tty_cleanup,
L
Linus Torvalds 已提交
1336 1337
};

J
Jan Kiszka 已提交
1338
static int __init capinc_tty_init(void)
L
Linus Torvalds 已提交
1339 1340
{
	struct tty_driver *drv;
J
Jan Kiszka 已提交
1341 1342
	int err;

L
Linus Torvalds 已提交
1343 1344 1345 1346 1347
	if (capi_ttyminors > CAPINC_MAX_PORTS)
		capi_ttyminors = CAPINC_MAX_PORTS;
	if (capi_ttyminors <= 0)
		capi_ttyminors = CAPINC_NR_PORTS;

1348 1349 1350
	capiminors = kzalloc(sizeof(struct capi_minor *) * capi_ttyminors,
			     GFP_KERNEL);
	if (!capiminors)
L
Linus Torvalds 已提交
1351 1352
		return -ENOMEM;

1353 1354 1355 1356 1357
	drv = alloc_tty_driver(capi_ttyminors);
	if (!drv) {
		kfree(capiminors);
		return -ENOMEM;
	}
L
Linus Torvalds 已提交
1358 1359 1360
	drv->owner = THIS_MODULE;
	drv->driver_name = "capi_nc";
	drv->name = "capi";
1361
	drv->major = 0;
L
Linus Torvalds 已提交
1362 1363 1364 1365 1366 1367 1368 1369
	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;
1370 1371 1372
	drv->flags =
		TTY_DRIVER_REAL_RAW | TTY_DRIVER_RESET_TERMIOS |
		TTY_DRIVER_DYNAMIC_DEV;
L
Linus Torvalds 已提交
1373
	tty_set_operations(drv, &capinc_ops);
J
Jan Kiszka 已提交
1374 1375 1376

	err = tty_register_driver(drv);
	if (err) {
L
Linus Torvalds 已提交
1377
		put_tty_driver(drv);
1378
		kfree(capiminors);
L
Linus Torvalds 已提交
1379
		printk(KERN_ERR "Couldn't register capi_nc driver\n");
J
Jan Kiszka 已提交
1380
		return err;
L
Linus Torvalds 已提交
1381 1382 1383 1384 1385
	}
	capinc_tty_driver = drv;
	return 0;
}

J
Jan Kiszka 已提交
1386
static void __exit capinc_tty_exit(void)
L
Linus Torvalds 已提交
1387
{
J
Jan Kiszka 已提交
1388 1389
	tty_unregister_driver(capinc_tty_driver);
	put_tty_driver(capinc_tty_driver);
1390
	kfree(capiminors);
L
Linus Torvalds 已提交
1391 1392
}

1393 1394 1395 1396 1397 1398 1399 1400 1401 1402
#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 已提交
1403 1404 1405 1406 1407 1408 1409

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

/*
 * /proc/capi/capi20:
 *  minor applid nrecvctlpkt nrecvdatapkt nsendctlpkt nsenddatapkt
 */
1410
static int capi20_proc_show(struct seq_file *m, void *v)
L
Linus Torvalds 已提交
1411 1412 1413 1414
{
        struct capidev *cdev;
	struct list_head *l;

1415
	mutex_lock(&capidev_list_lock);
L
Linus Torvalds 已提交
1416 1417
	list_for_each(l, &capidev_list) {
		cdev = list_entry(l, struct capidev, list);
1418
		seq_printf(m, "0 %d %lu %lu %lu %lu\n",
L
Linus Torvalds 已提交
1419 1420 1421 1422 1423 1424
			cdev->ap.applid,
			cdev->ap.nrecvctlpkt,
			cdev->ap.nrecvdatapkt,
			cdev->ap.nsentctlpkt,
			cdev->ap.nsentdatapkt);
	}
1425
	mutex_unlock(&capidev_list_lock);
1426
	return 0;
L
Linus Torvalds 已提交
1427 1428
}

1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441
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 已提交
1442 1443 1444 1445
/*
 * /proc/capi/capi20ncci:
 *  applid ncci
 */
1446
static int capi20ncci_proc_show(struct seq_file *m, void *v)
L
Linus Torvalds 已提交
1447
{
1448 1449
	struct capidev *cdev;
	struct capincci *np;
L
Linus Torvalds 已提交
1450

1451
	mutex_lock(&capidev_list_lock);
1452
	list_for_each_entry(cdev, &capidev_list, list) {
1453
		mutex_lock(&cdev->lock);
1454 1455
		list_for_each_entry(np, &cdev->nccis, list)
			seq_printf(m, "%d 0x%x\n", cdev->ap.applid, np->ncci);
1456
		mutex_unlock(&cdev->lock);
L
Linus Torvalds 已提交
1457
	}
1458
	mutex_unlock(&capidev_list_lock);
1459
	return 0;
L
Linus Torvalds 已提交
1460 1461
}

1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472
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 已提交
1473 1474 1475 1476
};

static void __init proc_init(void)
{
1477 1478
	proc_create("capi/capi20", 0, NULL, &capi20_proc_fops);
	proc_create("capi/capi20ncci", 0, NULL, &capi20ncci_proc_fops);
L
Linus Torvalds 已提交
1479 1480 1481 1482
}

static void __exit proc_exit(void)
{
1483 1484
	remove_proc_entry("capi/capi20", NULL);
	remove_proc_entry("capi/capi20ncci", NULL);
L
Linus Torvalds 已提交
1485 1486 1487 1488 1489 1490 1491
}

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


static int __init capi_init(void)
{
1492
	const char *compileinfo;
1493
	int major_ret;
L
Linus Torvalds 已提交
1494

1495 1496
	major_ret = register_chrdev(capi_major, "capi20", &capi_fops);
	if (major_ret < 0) {
L
Linus Torvalds 已提交
1497
		printk(KERN_ERR "capi20: unable to get major %d\n", capi_major);
1498
		return major_ret;
L
Linus Torvalds 已提交
1499
	}
1500
	capi_class = class_create(THIS_MODULE, "capi");
L
Linus Torvalds 已提交
1501 1502 1503 1504 1505
	if (IS_ERR(capi_class)) {
		unregister_chrdev(capi_major, "capi20");
		return PTR_ERR(capi_class);
	}

1506
	device_create(capi_class, NULL, MKDEV(capi_major, 0), NULL, "capi");
L
Linus Torvalds 已提交
1507 1508

	if (capinc_tty_init() < 0) {
1509
		device_destroy(capi_class, MKDEV(capi_major, 0));
1510
		class_destroy(capi_class);
L
Linus Torvalds 已提交
1511 1512 1513 1514 1515 1516 1517 1518
		unregister_chrdev(capi_major, "capi20");
		return -ENOMEM;
	}

	proc_init();

#if defined(CONFIG_ISDN_CAPI_CAPIFS) || defined(CONFIG_ISDN_CAPI_CAPIFS_MODULE)
        compileinfo = " (middleware+capifs)";
1519
#elif defined(CONFIG_ISDN_CAPI_MIDDLEWARE)
L
Linus Torvalds 已提交
1520 1521 1522 1523
        compileinfo = " (no capifs)";
#else
        compileinfo = " (no middleware)";
#endif
1524 1525
	printk(KERN_NOTICE "CAPI 2.0 started up with major %d%s\n",
	       capi_major, compileinfo);
L
Linus Torvalds 已提交
1526 1527 1528 1529 1530 1531 1532 1533

	return 0;
}

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

1534
	device_destroy(capi_class, MKDEV(capi_major, 0));
1535
	class_destroy(capi_class);
L
Linus Torvalds 已提交
1536 1537 1538 1539 1540 1541 1542
	unregister_chrdev(capi_major, "capi20");

	capinc_tty_exit();
}

module_init(capi_init);
module_exit(capi_exit);