blob: 81661b8bd3a865cf1f83c09a00b9b81355a5a909 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* $Id: capi.c,v 1.1.2.7 2004/04/28 09:48:59 armin Exp $
2 *
3 * CAPI 2.0 Interface for Linux
4 *
5 * Copyright 1996 by Carsten Paeth <calle@calle.de>
6 *
7 * This software may be used and distributed according to the terms
8 * of the GNU General Public License, incorporated herein by reference.
9 *
10 */
11
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/module.h>
13#include <linux/errno.h>
14#include <linux/kernel.h>
15#include <linux/major.h>
16#include <linux/sched.h>
17#include <linux/slab.h>
18#include <linux/fcntl.h>
19#include <linux/fs.h>
20#include <linux/signal.h>
Matthias Kaehlcke9ea6e5d2007-05-08 00:32:43 -070021#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/timer.h>
24#include <linux/wait.h>
25#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
26#include <linux/tty.h>
27#ifdef CONFIG_PPP
28#include <linux/netdevice.h>
29#include <linux/ppp_defs.h>
30#include <linux/if_ppp.h>
31#endif /* CONFIG_PPP */
32#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
33#include <linux/skbuff.h>
34#include <linux/proc_fs.h>
35#include <linux/poll.h>
36#include <linux/capi.h>
37#include <linux/kernelcapi.h>
38#include <linux/init.h>
39#include <linux/device.h>
40#include <linux/moduleparam.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/isdn/capiutil.h>
42#include <linux/isdn/capicmd.h>
43#if defined(CONFIG_ISDN_CAPI_CAPIFS) || defined(CONFIG_ISDN_CAPI_CAPIFS_MODULE)
44#include "capifs.h"
45#endif
46
47static char *revision = "$Revision: 1.1.2.7 $";
48
49MODULE_DESCRIPTION("CAPI4Linux: Userspace /dev/capi20 interface");
50MODULE_AUTHOR("Carsten Paeth");
51MODULE_LICENSE("GPL");
52
53#undef _DEBUG_REFCOUNT /* alloc/free and open/close debug */
54#undef _DEBUG_TTYFUNCS /* call to tty_driver */
55#undef _DEBUG_DATAFLOW /* data flow */
56
57/* -------- driver information -------------------------------------- */
58
gregkh@suse.de56b22932005-03-23 10:01:41 -080059static struct class *capi_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Adrian Bunk408b6642005-05-01 08:59:29 -070061static int capi_major = 68; /* allocated */
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
63#define CAPINC_NR_PORTS 32
64#define CAPINC_MAX_PORTS 256
Adrian Bunk408b6642005-05-01 08:59:29 -070065static int capi_ttymajor = 191;
66static int capi_ttyminors = CAPINC_NR_PORTS;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
68
69module_param_named(major, capi_major, uint, 0);
70#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
71module_param_named(ttymajor, capi_ttymajor, uint, 0);
72module_param_named(ttyminors, capi_ttyminors, uint, 0);
73#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
74
75/* -------- defines ------------------------------------------------- */
76
77#define CAPINC_MAX_RECVQUEUE 10
78#define CAPINC_MAX_SENDQUEUE 10
79#define CAPI_MAX_BLKSIZE 2048
80
81/* -------- data structures ----------------------------------------- */
82
83struct capidev;
84struct capincci;
85#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
86struct capiminor;
87
Michael Buesch6aa65472006-06-26 00:25:30 -070088struct datahandle_queue {
89 struct list_head list;
90 u16 datahandle;
91};
92
Linus Torvalds1da177e2005-04-16 15:20:36 -070093struct capiminor {
94 struct list_head list;
95 struct capincci *nccip;
96 unsigned int minor;
97
98 struct capi20_appl *ap;
99 u32 ncci;
100 u16 datahandle;
101 u16 msgid;
102
103 struct tty_struct *tty;
104 int ttyinstop;
105 int ttyoutstop;
106 struct sk_buff *ttyskb;
107 atomic_t ttyopencount;
108
109 struct sk_buff_head inqueue;
110 int inbytes;
111 struct sk_buff_head outqueue;
112 int outbytes;
113
114 /* transmit path */
Michael Buesch6aa65472006-06-26 00:25:30 -0700115 struct list_head ackqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 int nack;
Michael Buesch6aa65472006-06-26 00:25:30 -0700117 spinlock_t ackqlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118};
119#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
120
Michael Buesch053b47f2007-02-12 00:53:26 -0800121/* FIXME: The following lock is a sledgehammer-workaround to a
122 * locking issue with the capiminor (and maybe other) data structure(s).
123 * Access to this data is done in a racy way and crashes the machine with
124 * a FritzCard DSL driver; sooner or later. This is a workaround
125 * which trades scalability vs stability, so it doesn't crash the kernel anymore.
126 * The correct (and scalable) fix for the issue seems to require
127 * an API change to the drivers... . */
128static DEFINE_SPINLOCK(workaround_lock);
129
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130struct capincci {
131 struct capincci *next;
132 u32 ncci;
133 struct capidev *cdev;
134#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
135 struct capiminor *minorp;
136#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
137};
138
139struct capidev {
140 struct list_head list;
141 struct capi20_appl ap;
142 u16 errcode;
143 unsigned userflags;
144
145 struct sk_buff_head recvqueue;
146 wait_queue_head_t recvwait;
147
148 struct capincci *nccis;
149
Matthias Kaehlcke9ea6e5d2007-05-08 00:32:43 -0700150 struct mutex ncci_list_mtx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151};
152
153/* -------- global variables ---------------------------------------- */
154
155static DEFINE_RWLOCK(capidev_list_lock);
156static LIST_HEAD(capidev_list);
157
158#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
159static DEFINE_RWLOCK(capiminor_list_lock);
160static LIST_HEAD(capiminor_list);
161#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
162
163#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
164/* -------- datahandles --------------------------------------------- */
165
166static int capincci_add_ack(struct capiminor *mp, u16 datahandle)
167{
Michael Buesch6aa65472006-06-26 00:25:30 -0700168 struct datahandle_queue *n;
169 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
171 n = kmalloc(sizeof(*n), GFP_ATOMIC);
Michael Buesch6aa65472006-06-26 00:25:30 -0700172 if (unlikely(!n)) {
173 printk(KERN_ERR "capi: alloc datahandle failed\n");
174 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 n->datahandle = datahandle;
Michael Buesch6aa65472006-06-26 00:25:30 -0700177 INIT_LIST_HEAD(&n->list);
178 spin_lock_irqsave(&mp->ackqlock, flags);
179 list_add_tail(&n->list, &mp->ackqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 mp->nack++;
Michael Buesch6aa65472006-06-26 00:25:30 -0700181 spin_unlock_irqrestore(&mp->ackqlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 return 0;
183}
184
185static int capiminor_del_ack(struct capiminor *mp, u16 datahandle)
186{
Michael Buesch6aa65472006-06-26 00:25:30 -0700187 struct datahandle_queue *p, *tmp;
188 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
Michael Buesch6aa65472006-06-26 00:25:30 -0700190 spin_lock_irqsave(&mp->ackqlock, flags);
191 list_for_each_entry_safe(p, tmp, &mp->ackqueue, list) {
192 if (p->datahandle == datahandle) {
193 list_del(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 kfree(p);
195 mp->nack--;
Michael Buesch6aa65472006-06-26 00:25:30 -0700196 spin_unlock_irqrestore(&mp->ackqlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 return 0;
198 }
199 }
Michael Buesch6aa65472006-06-26 00:25:30 -0700200 spin_unlock_irqrestore(&mp->ackqlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 return -1;
202}
203
204static void capiminor_del_all_ack(struct capiminor *mp)
205{
Michael Buesch6aa65472006-06-26 00:25:30 -0700206 struct datahandle_queue *p, *tmp;
207 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Michael Buesch6aa65472006-06-26 00:25:30 -0700209 spin_lock_irqsave(&mp->ackqlock, flags);
210 list_for_each_entry_safe(p, tmp, &mp->ackqueue, list) {
211 list_del(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 kfree(p);
213 mp->nack--;
214 }
Michael Buesch6aa65472006-06-26 00:25:30 -0700215 spin_unlock_irqrestore(&mp->ackqlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216}
217
218
219/* -------- struct capiminor ---------------------------------------- */
220
221static struct capiminor *capiminor_alloc(struct capi20_appl *ap, u32 ncci)
222{
223 struct capiminor *mp, *p;
224 unsigned int minor = 0;
225 unsigned long flags;
226
Burman Yan41f96932006-12-08 02:39:35 -0800227 mp = kzalloc(sizeof(*mp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 if (!mp) {
229 printk(KERN_ERR "capi: can't alloc capiminor\n");
230 return NULL;
231 }
232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 mp->ap = ap;
234 mp->ncci = ncci;
235 mp->msgid = 0;
236 atomic_set(&mp->ttyopencount,0);
Michael Buesch6aa65472006-06-26 00:25:30 -0700237 INIT_LIST_HEAD(&mp->ackqueue);
238 spin_lock_init(&mp->ackqlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
240 skb_queue_head_init(&mp->inqueue);
241 skb_queue_head_init(&mp->outqueue);
242
243 /* Allocate the least unused minor number.
244 */
245 write_lock_irqsave(&capiminor_list_lock, flags);
246 if (list_empty(&capiminor_list))
247 list_add(&mp->list, &capiminor_list);
248 else {
249 list_for_each_entry(p, &capiminor_list, list) {
250 if (p->minor > minor)
251 break;
252 minor++;
253 }
254
255 if (minor < capi_ttyminors) {
256 mp->minor = minor;
257 list_add(&mp->list, p->list.prev);
258 }
259 }
260 write_unlock_irqrestore(&capiminor_list_lock, flags);
261
262 if (!(minor < capi_ttyminors)) {
263 printk(KERN_NOTICE "capi: out of minors\n");
264 kfree(mp);
265 return NULL;
266 }
267
268 return mp;
269}
270
271static void capiminor_free(struct capiminor *mp)
272{
273 unsigned long flags;
274
275 write_lock_irqsave(&capiminor_list_lock, flags);
276 list_del(&mp->list);
277 write_unlock_irqrestore(&capiminor_list_lock, flags);
278
279 if (mp->ttyskb) kfree_skb(mp->ttyskb);
280 mp->ttyskb = NULL;
281 skb_queue_purge(&mp->inqueue);
282 skb_queue_purge(&mp->outqueue);
283 capiminor_del_all_ack(mp);
284 kfree(mp);
285}
286
Adrian Bunk408b6642005-05-01 08:59:29 -0700287static struct capiminor *capiminor_find(unsigned int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288{
289 struct list_head *l;
290 struct capiminor *p = NULL;
291
292 read_lock(&capiminor_list_lock);
293 list_for_each(l, &capiminor_list) {
294 p = list_entry(l, struct capiminor, list);
295 if (p->minor == minor)
296 break;
297 }
298 read_unlock(&capiminor_list_lock);
299 if (l == &capiminor_list)
300 return NULL;
301
302 return p;
303}
304#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
305
306/* -------- struct capincci ----------------------------------------- */
307
308static struct capincci *capincci_alloc(struct capidev *cdev, u32 ncci)
309{
310 struct capincci *np, **pp;
311#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
312 struct capiminor *mp = NULL;
313#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
314
Burman Yan41f96932006-12-08 02:39:35 -0800315 np = kzalloc(sizeof(*np), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 if (!np)
317 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 np->ncci = ncci;
319 np->cdev = cdev;
320#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
321 mp = NULL;
322 if (cdev->userflags & CAPIFLAG_HIGHJACKING)
323 mp = np->minorp = capiminor_alloc(&cdev->ap, ncci);
324 if (mp) {
325 mp->nccip = np;
326#ifdef _DEBUG_REFCOUNT
327 printk(KERN_DEBUG "set mp->nccip\n");
328#endif
329#if defined(CONFIG_ISDN_CAPI_CAPIFS) || defined(CONFIG_ISDN_CAPI_CAPIFS_MODULE)
330 capifs_new_ncci(mp->minor, MKDEV(capi_ttymajor, mp->minor));
331#endif
332 }
333#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
334 for (pp=&cdev->nccis; *pp; pp = &(*pp)->next)
335 ;
336 *pp = np;
337 return np;
338}
339
340static void capincci_free(struct capidev *cdev, u32 ncci)
341{
342 struct capincci *np, **pp;
343#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
344 struct capiminor *mp;
345#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
346
347 pp=&cdev->nccis;
348 while (*pp) {
349 np = *pp;
350 if (ncci == 0xffffffff || np->ncci == ncci) {
351 *pp = (*pp)->next;
352#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
353 if ((mp = np->minorp) != 0) {
354#if defined(CONFIG_ISDN_CAPI_CAPIFS) || defined(CONFIG_ISDN_CAPI_CAPIFS_MODULE)
355 capifs_free_ncci(mp->minor);
356#endif
357 if (mp->tty) {
358 mp->nccip = NULL;
359#ifdef _DEBUG_REFCOUNT
360 printk(KERN_DEBUG "reset mp->nccip\n");
361#endif
362 tty_hangup(mp->tty);
363 } else {
364 capiminor_free(mp);
365 }
366 }
367#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
368 kfree(np);
369 if (*pp == 0) return;
370 } else {
371 pp = &(*pp)->next;
372 }
373 }
374}
375
376static struct capincci *capincci_find(struct capidev *cdev, u32 ncci)
377{
378 struct capincci *p;
379
380 for (p=cdev->nccis; p ; p = p->next) {
381 if (p->ncci == ncci)
382 break;
383 }
384 return p;
385}
386
387/* -------- struct capidev ------------------------------------------ */
388
389static struct capidev *capidev_alloc(void)
390{
391 struct capidev *cdev;
392 unsigned long flags;
393
Burman Yan41f96932006-12-08 02:39:35 -0800394 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 if (!cdev)
396 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
Matthias Kaehlcke9ea6e5d2007-05-08 00:32:43 -0700398 mutex_init(&cdev->ncci_list_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 skb_queue_head_init(&cdev->recvqueue);
400 init_waitqueue_head(&cdev->recvwait);
401 write_lock_irqsave(&capidev_list_lock, flags);
402 list_add_tail(&cdev->list, &capidev_list);
403 write_unlock_irqrestore(&capidev_list_lock, flags);
404 return cdev;
405}
406
407static void capidev_free(struct capidev *cdev)
408{
409 unsigned long flags;
410
411 if (cdev->ap.applid) {
412 capi20_release(&cdev->ap);
413 cdev->ap.applid = 0;
414 }
415 skb_queue_purge(&cdev->recvqueue);
416
Matthias Kaehlcke9ea6e5d2007-05-08 00:32:43 -0700417 mutex_lock(&cdev->ncci_list_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 capincci_free(cdev, 0xffffffff);
Matthias Kaehlcke9ea6e5d2007-05-08 00:32:43 -0700419 mutex_unlock(&cdev->ncci_list_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
421 write_lock_irqsave(&capidev_list_lock, flags);
422 list_del(&cdev->list);
423 write_unlock_irqrestore(&capidev_list_lock, flags);
424 kfree(cdev);
425}
426
427#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
428/* -------- handle data queue --------------------------------------- */
429
430static struct sk_buff *
431gen_data_b3_resp_for(struct capiminor *mp, struct sk_buff *skb)
432{
433 struct sk_buff *nskb;
434 nskb = alloc_skb(CAPI_DATA_B3_RESP_LEN, GFP_ATOMIC);
435 if (nskb) {
436 u16 datahandle = CAPIMSG_U16(skb->data,CAPIMSG_BASELEN+4+4+2);
437 unsigned char *s = skb_put(nskb, CAPI_DATA_B3_RESP_LEN);
438 capimsg_setu16(s, 0, CAPI_DATA_B3_RESP_LEN);
439 capimsg_setu16(s, 2, mp->ap->applid);
440 capimsg_setu8 (s, 4, CAPI_DATA_B3);
441 capimsg_setu8 (s, 5, CAPI_RESP);
442 capimsg_setu16(s, 6, mp->msgid++);
443 capimsg_setu32(s, 8, mp->ncci);
444 capimsg_setu16(s, 12, datahandle);
445 }
446 return nskb;
447}
448
449static int handle_recv_skb(struct capiminor *mp, struct sk_buff *skb)
450{
451 struct sk_buff *nskb;
452 int datalen;
453 u16 errcode, datahandle;
454 struct tty_ldisc *ld;
455
456 datalen = skb->len - CAPIMSG_LEN(skb->data);
457 if (mp->tty == NULL)
458 {
459#ifdef _DEBUG_DATAFLOW
460 printk(KERN_DEBUG "capi: currently no receiver\n");
461#endif
462 return -1;
463 }
464
465 ld = tty_ldisc_ref(mp->tty);
466 if (ld == NULL)
467 return -1;
468 if (ld->receive_buf == NULL) {
469#if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
470 printk(KERN_DEBUG "capi: ldisc has no receive_buf function\n");
471#endif
472 goto bad;
473 }
474 if (mp->ttyinstop) {
475#if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
476 printk(KERN_DEBUG "capi: recv tty throttled\n");
477#endif
478 goto bad;
479 }
Alan Cox33f0f882006-01-09 20:54:13 -0800480 if (mp->tty->receive_room < datalen) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481#if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
482 printk(KERN_DEBUG "capi: no room in tty\n");
483#endif
484 goto bad;
485 }
486 if ((nskb = gen_data_b3_resp_for(mp, skb)) == 0) {
487 printk(KERN_ERR "capi: gen_data_b3_resp failed\n");
488 goto bad;
489 }
490 datahandle = CAPIMSG_U16(skb->data,CAPIMSG_BASELEN+4);
491 errcode = capi20_put_message(mp->ap, nskb);
492 if (errcode != CAPI_NOERROR) {
493 printk(KERN_ERR "capi: send DATA_B3_RESP failed=%x\n",
494 errcode);
495 kfree_skb(nskb);
496 goto bad;
497 }
498 (void)skb_pull(skb, CAPIMSG_LEN(skb->data));
499#ifdef _DEBUG_DATAFLOW
500 printk(KERN_DEBUG "capi: DATA_B3_RESP %u len=%d => ldisc\n",
501 datahandle, skb->len);
502#endif
503 ld->receive_buf(mp->tty, skb->data, NULL, skb->len);
504 kfree_skb(skb);
505 tty_ldisc_deref(ld);
506 return 0;
507bad:
508 tty_ldisc_deref(ld);
509 return -1;
510}
511
512static void handle_minor_recv(struct capiminor *mp)
513{
514 struct sk_buff *skb;
515 while ((skb = skb_dequeue(&mp->inqueue)) != 0) {
516 unsigned int len = skb->len;
517 mp->inbytes -= len;
518 if (handle_recv_skb(mp, skb) < 0) {
519 skb_queue_head(&mp->inqueue, skb);
520 mp->inbytes += len;
521 return;
522 }
523 }
524}
525
526static int handle_minor_send(struct capiminor *mp)
527{
528 struct sk_buff *skb;
529 u16 len;
530 int count = 0;
531 u16 errcode;
532 u16 datahandle;
533
534 if (mp->tty && mp->ttyoutstop) {
535#if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
536 printk(KERN_DEBUG "capi: send: tty stopped\n");
537#endif
538 return 0;
539 }
540
541 while ((skb = skb_dequeue(&mp->outqueue)) != 0) {
542 datahandle = mp->datahandle;
543 len = (u16)skb->len;
544 skb_push(skb, CAPI_DATA_B3_REQ_LEN);
545 memset(skb->data, 0, CAPI_DATA_B3_REQ_LEN);
546 capimsg_setu16(skb->data, 0, CAPI_DATA_B3_REQ_LEN);
547 capimsg_setu16(skb->data, 2, mp->ap->applid);
548 capimsg_setu8 (skb->data, 4, CAPI_DATA_B3);
549 capimsg_setu8 (skb->data, 5, CAPI_REQ);
550 capimsg_setu16(skb->data, 6, mp->msgid++);
551 capimsg_setu32(skb->data, 8, mp->ncci); /* NCCI */
552 capimsg_setu32(skb->data, 12, (u32) skb->data); /* Data32 */
553 capimsg_setu16(skb->data, 16, len); /* Data length */
554 capimsg_setu16(skb->data, 18, datahandle);
555 capimsg_setu16(skb->data, 20, 0); /* Flags */
556
557 if (capincci_add_ack(mp, datahandle) < 0) {
558 skb_pull(skb, CAPI_DATA_B3_REQ_LEN);
559 skb_queue_head(&mp->outqueue, skb);
560 return count;
561 }
562 errcode = capi20_put_message(mp->ap, skb);
563 if (errcode == CAPI_NOERROR) {
564 mp->datahandle++;
565 count++;
566 mp->outbytes -= len;
567#ifdef _DEBUG_DATAFLOW
568 printk(KERN_DEBUG "capi: DATA_B3_REQ %u len=%u\n",
569 datahandle, len);
570#endif
571 continue;
572 }
573 capiminor_del_ack(mp, datahandle);
574
575 if (errcode == CAPI_SENDQUEUEFULL) {
576 skb_pull(skb, CAPI_DATA_B3_REQ_LEN);
577 skb_queue_head(&mp->outqueue, skb);
578 break;
579 }
580
581 /* ups, drop packet */
582 printk(KERN_ERR "capi: put_message = %x\n", errcode);
583 mp->outbytes -= len;
584 kfree_skb(skb);
585 }
586 return count;
587}
588
589#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
590/* -------- function called by lower level -------------------------- */
591
592static void capi_recv_message(struct capi20_appl *ap, struct sk_buff *skb)
593{
594 struct capidev *cdev = ap->private;
595#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
596 struct capiminor *mp;
597 u16 datahandle;
598#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
599 struct capincci *np;
600 u32 ncci;
Michael Buesch053b47f2007-02-12 00:53:26 -0800601 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
603 if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_CONF) {
604 u16 info = CAPIMSG_U16(skb->data, 12); // Info field
605 if (info == 0) {
Matthias Kaehlcke9ea6e5d2007-05-08 00:32:43 -0700606 mutex_lock(&cdev->ncci_list_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
Matthias Kaehlcke9ea6e5d2007-05-08 00:32:43 -0700608 mutex_unlock(&cdev->ncci_list_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 }
610 }
611 if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_IND) {
Matthias Kaehlcke9ea6e5d2007-05-08 00:32:43 -0700612 mutex_lock(&cdev->ncci_list_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
Matthias Kaehlcke9ea6e5d2007-05-08 00:32:43 -0700614 mutex_unlock(&cdev->ncci_list_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 }
Michael Buesch053b47f2007-02-12 00:53:26 -0800616 spin_lock_irqsave(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 if (CAPIMSG_COMMAND(skb->data) != CAPI_DATA_B3) {
618 skb_queue_tail(&cdev->recvqueue, skb);
619 wake_up_interruptible(&cdev->recvwait);
Michael Buesch053b47f2007-02-12 00:53:26 -0800620 spin_unlock_irqrestore(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 return;
622 }
623 ncci = CAPIMSG_CONTROL(skb->data);
624 for (np = cdev->nccis; np && np->ncci != ncci; np = np->next)
625 ;
626 if (!np) {
627 printk(KERN_ERR "BUG: capi_signal: ncci not found\n");
628 skb_queue_tail(&cdev->recvqueue, skb);
629 wake_up_interruptible(&cdev->recvwait);
Michael Buesch053b47f2007-02-12 00:53:26 -0800630 spin_unlock_irqrestore(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 return;
632 }
633#ifndef CONFIG_ISDN_CAPI_MIDDLEWARE
634 skb_queue_tail(&cdev->recvqueue, skb);
635 wake_up_interruptible(&cdev->recvwait);
636#else /* CONFIG_ISDN_CAPI_MIDDLEWARE */
637 mp = np->minorp;
638 if (!mp) {
639 skb_queue_tail(&cdev->recvqueue, skb);
640 wake_up_interruptible(&cdev->recvwait);
Michael Buesch053b47f2007-02-12 00:53:26 -0800641 spin_unlock_irqrestore(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 return;
643 }
644
645
646 if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_IND) {
647
648 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4+4+2);
649#ifdef _DEBUG_DATAFLOW
650 printk(KERN_DEBUG "capi_signal: DATA_B3_IND %u len=%d\n",
651 datahandle, skb->len-CAPIMSG_LEN(skb->data));
652#endif
653 skb_queue_tail(&mp->inqueue, skb);
654 mp->inbytes += skb->len;
655 handle_minor_recv(mp);
656
657 } else if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_CONF) {
658
659 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4);
660#ifdef _DEBUG_DATAFLOW
661 printk(KERN_DEBUG "capi_signal: DATA_B3_CONF %u 0x%x\n",
662 datahandle,
663 CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4+2));
664#endif
665 kfree_skb(skb);
666 (void)capiminor_del_ack(mp, datahandle);
667 if (mp->tty)
668 tty_wakeup(mp->tty);
669 (void)handle_minor_send(mp);
670
671 } else {
672 /* ups, let capi application handle it :-) */
673 skb_queue_tail(&cdev->recvqueue, skb);
674 wake_up_interruptible(&cdev->recvwait);
675 }
676#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
Michael Buesch053b47f2007-02-12 00:53:26 -0800677 spin_unlock_irqrestore(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678}
679
680/* -------- file_operations for capidev ----------------------------- */
681
682static ssize_t
683capi_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
684{
685 struct capidev *cdev = (struct capidev *)file->private_data;
686 struct sk_buff *skb;
687 size_t copied;
688
689 if (!cdev->ap.applid)
690 return -ENODEV;
691
692 if ((skb = skb_dequeue(&cdev->recvqueue)) == 0) {
693
694 if (file->f_flags & O_NONBLOCK)
695 return -EAGAIN;
696
697 for (;;) {
698 interruptible_sleep_on(&cdev->recvwait);
699 if ((skb = skb_dequeue(&cdev->recvqueue)) != 0)
700 break;
701 if (signal_pending(current))
702 break;
703 }
704 if (skb == 0)
705 return -ERESTARTNOHAND;
706 }
707 if (skb->len > count) {
708 skb_queue_head(&cdev->recvqueue, skb);
709 return -EMSGSIZE;
710 }
711 if (copy_to_user(buf, skb->data, skb->len)) {
712 skb_queue_head(&cdev->recvqueue, skb);
713 return -EFAULT;
714 }
715 copied = skb->len;
716
717 kfree_skb(skb);
718
719 return copied;
720}
721
722static ssize_t
723capi_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
724{
725 struct capidev *cdev = (struct capidev *)file->private_data;
726 struct sk_buff *skb;
727 u16 mlen;
728
729 if (!cdev->ap.applid)
730 return -ENODEV;
731
732 skb = alloc_skb(count, GFP_USER);
733 if (!skb)
734 return -ENOMEM;
735
736 if (copy_from_user(skb_put(skb, count), buf, count)) {
737 kfree_skb(skb);
738 return -EFAULT;
739 }
740 mlen = CAPIMSG_LEN(skb->data);
741 if (CAPIMSG_CMD(skb->data) == CAPI_DATA_B3_REQ) {
742 if ((size_t)(mlen + CAPIMSG_DATALEN(skb->data)) != count) {
743 kfree_skb(skb);
744 return -EINVAL;
745 }
746 } else {
747 if (mlen != count) {
748 kfree_skb(skb);
749 return -EINVAL;
750 }
751 }
752 CAPIMSG_SETAPPID(skb->data, cdev->ap.applid);
753
754 if (CAPIMSG_CMD(skb->data) == CAPI_DISCONNECT_B3_RESP) {
Matthias Kaehlcke9ea6e5d2007-05-08 00:32:43 -0700755 mutex_lock(&cdev->ncci_list_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 capincci_free(cdev, CAPIMSG_NCCI(skb->data));
Matthias Kaehlcke9ea6e5d2007-05-08 00:32:43 -0700757 mutex_unlock(&cdev->ncci_list_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 }
759
760 cdev->errcode = capi20_put_message(&cdev->ap, skb);
761
762 if (cdev->errcode) {
763 kfree_skb(skb);
764 return -EIO;
765 }
766 return count;
767}
768
769static unsigned int
770capi_poll(struct file *file, poll_table * wait)
771{
772 struct capidev *cdev = (struct capidev *)file->private_data;
773 unsigned int mask = 0;
774
775 if (!cdev->ap.applid)
776 return POLLERR;
777
778 poll_wait(file, &(cdev->recvwait), wait);
779 mask = POLLOUT | POLLWRNORM;
780 if (!skb_queue_empty(&cdev->recvqueue))
781 mask |= POLLIN | POLLRDNORM;
782 return mask;
783}
784
785static int
786capi_ioctl(struct inode *inode, struct file *file,
787 unsigned int cmd, unsigned long arg)
788{
789 struct capidev *cdev = file->private_data;
790 struct capi20_appl *ap = &cdev->ap;
791 capi_ioctl_struct data;
792 int retval = -EINVAL;
793 void __user *argp = (void __user *)arg;
794
795 switch (cmd) {
796 case CAPI_REGISTER:
797 {
798 if (ap->applid)
799 return -EEXIST;
800
801 if (copy_from_user(&cdev->ap.rparam, argp,
802 sizeof(struct capi_register_params)))
803 return -EFAULT;
804
805 cdev->ap.private = cdev;
806 cdev->ap.recv_message = capi_recv_message;
807 cdev->errcode = capi20_register(ap);
808 if (cdev->errcode) {
809 ap->applid = 0;
810 return -EIO;
811 }
812 }
813 return (int)ap->applid;
814
815 case CAPI_GET_VERSION:
816 {
817 if (copy_from_user(&data.contr, argp,
818 sizeof(data.contr)))
819 return -EFAULT;
820 cdev->errcode = capi20_get_version(data.contr, &data.version);
821 if (cdev->errcode)
822 return -EIO;
823 if (copy_to_user(argp, &data.version,
824 sizeof(data.version)))
825 return -EFAULT;
826 }
827 return 0;
828
829 case CAPI_GET_SERIAL:
830 {
831 if (copy_from_user(&data.contr, argp,
832 sizeof(data.contr)))
833 return -EFAULT;
834 cdev->errcode = capi20_get_serial (data.contr, data.serial);
835 if (cdev->errcode)
836 return -EIO;
837 if (copy_to_user(argp, data.serial,
838 sizeof(data.serial)))
839 return -EFAULT;
840 }
841 return 0;
842 case CAPI_GET_PROFILE:
843 {
844 if (copy_from_user(&data.contr, argp,
845 sizeof(data.contr)))
846 return -EFAULT;
847
848 if (data.contr == 0) {
849 cdev->errcode = capi20_get_profile(data.contr, &data.profile);
850 if (cdev->errcode)
851 return -EIO;
852
853 retval = copy_to_user(argp,
854 &data.profile.ncontroller,
855 sizeof(data.profile.ncontroller));
856
857 } else {
858 cdev->errcode = capi20_get_profile(data.contr, &data.profile);
859 if (cdev->errcode)
860 return -EIO;
861
862 retval = copy_to_user(argp, &data.profile,
863 sizeof(data.profile));
864 }
865 if (retval)
866 return -EFAULT;
867 }
868 return 0;
869
870 case CAPI_GET_MANUFACTURER:
871 {
872 if (copy_from_user(&data.contr, argp,
873 sizeof(data.contr)))
874 return -EFAULT;
875 cdev->errcode = capi20_get_manufacturer(data.contr, data.manufacturer);
876 if (cdev->errcode)
877 return -EIO;
878
879 if (copy_to_user(argp, data.manufacturer,
880 sizeof(data.manufacturer)))
881 return -EFAULT;
882
883 }
884 return 0;
885 case CAPI_GET_ERRCODE:
886 data.errcode = cdev->errcode;
887 cdev->errcode = CAPI_NOERROR;
888 if (arg) {
889 if (copy_to_user(argp, &data.errcode,
890 sizeof(data.errcode)))
891 return -EFAULT;
892 }
893 return data.errcode;
894
895 case CAPI_INSTALLED:
896 if (capi20_isinstalled() == CAPI_NOERROR)
897 return 0;
898 return -ENXIO;
899
900 case CAPI_MANUFACTURER_CMD:
901 {
902 struct capi_manufacturer_cmd mcmd;
903 if (!capable(CAP_SYS_ADMIN))
904 return -EPERM;
905 if (copy_from_user(&mcmd, argp, sizeof(mcmd)))
906 return -EFAULT;
907 return capi20_manufacturer(mcmd.cmd, mcmd.data);
908 }
909 return 0;
910
911 case CAPI_SET_FLAGS:
912 case CAPI_CLR_FLAGS:
913 {
914 unsigned userflags;
915 if (copy_from_user(&userflags, argp,
916 sizeof(userflags)))
917 return -EFAULT;
918 if (cmd == CAPI_SET_FLAGS)
919 cdev->userflags |= userflags;
920 else
921 cdev->userflags &= ~userflags;
922 }
923 return 0;
924
925 case CAPI_GET_FLAGS:
926 if (copy_to_user(argp, &cdev->userflags,
927 sizeof(cdev->userflags)))
928 return -EFAULT;
929 return 0;
930
931 case CAPI_NCCI_OPENCOUNT:
932 {
933 struct capincci *nccip;
934#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
935 struct capiminor *mp;
936#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
937 unsigned ncci;
938 int count = 0;
939 if (copy_from_user(&ncci, argp, sizeof(ncci)))
940 return -EFAULT;
941
Matthias Kaehlcke9ea6e5d2007-05-08 00:32:43 -0700942 mutex_lock(&cdev->ncci_list_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 if ((nccip = capincci_find(cdev, (u32) ncci)) == 0) {
Matthias Kaehlcke9ea6e5d2007-05-08 00:32:43 -0700944 mutex_unlock(&cdev->ncci_list_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 return 0;
946 }
947#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
948 if ((mp = nccip->minorp) != 0) {
949 count += atomic_read(&mp->ttyopencount);
950 }
951#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
Matthias Kaehlcke9ea6e5d2007-05-08 00:32:43 -0700952 mutex_unlock(&cdev->ncci_list_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 return count;
954 }
955 return 0;
956
957#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
958 case CAPI_NCCI_GETUNIT:
959 {
960 struct capincci *nccip;
961 struct capiminor *mp;
962 unsigned ncci;
963 int unit = 0;
964 if (copy_from_user(&ncci, argp,
965 sizeof(ncci)))
966 return -EFAULT;
Matthias Kaehlcke9ea6e5d2007-05-08 00:32:43 -0700967 mutex_lock(&cdev->ncci_list_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 nccip = capincci_find(cdev, (u32) ncci);
969 if (!nccip || (mp = nccip->minorp) == 0) {
Matthias Kaehlcke9ea6e5d2007-05-08 00:32:43 -0700970 mutex_unlock(&cdev->ncci_list_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 return -ESRCH;
972 }
973 unit = mp->minor;
Matthias Kaehlcke9ea6e5d2007-05-08 00:32:43 -0700974 mutex_unlock(&cdev->ncci_list_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 return unit;
976 }
977 return 0;
978#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
979 }
980 return -EINVAL;
981}
982
983static int
984capi_open(struct inode *inode, struct file *file)
985{
986 if (file->private_data)
987 return -EEXIST;
988
989 if ((file->private_data = capidev_alloc()) == 0)
990 return -ENOMEM;
991
992 return nonseekable_open(inode, file);
993}
994
995static int
996capi_release(struct inode *inode, struct file *file)
997{
998 struct capidev *cdev = (struct capidev *)file->private_data;
999
1000 capidev_free(cdev);
1001 file->private_data = NULL;
1002
1003 return 0;
1004}
1005
Arjan van de Ven2b8693c2007-02-12 00:55:32 -08001006static const struct file_operations capi_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007{
1008 .owner = THIS_MODULE,
1009 .llseek = no_llseek,
1010 .read = capi_read,
1011 .write = capi_write,
1012 .poll = capi_poll,
1013 .ioctl = capi_ioctl,
1014 .open = capi_open,
1015 .release = capi_release,
1016};
1017
1018#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1019/* -------- tty_operations for capincci ----------------------------- */
1020
1021static int capinc_tty_open(struct tty_struct * tty, struct file * file)
1022{
1023 struct capiminor *mp;
Michael Buesch053b47f2007-02-12 00:53:26 -08001024 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
Josef Sipek4482dfa2006-12-08 02:37:13 -08001026 if ((mp = capiminor_find(iminor(file->f_path.dentry->d_inode))) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 return -ENXIO;
1028 if (mp->nccip == 0)
1029 return -ENXIO;
1030
1031 tty->driver_data = (void *)mp;
1032
Michael Buesch053b47f2007-02-12 00:53:26 -08001033 spin_lock_irqsave(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 if (atomic_read(&mp->ttyopencount) == 0)
1035 mp->tty = tty;
1036 atomic_inc(&mp->ttyopencount);
1037#ifdef _DEBUG_REFCOUNT
1038 printk(KERN_DEBUG "capinc_tty_open ocount=%d\n", atomic_read(&mp->ttyopencount));
1039#endif
1040 handle_minor_recv(mp);
Michael Buesch053b47f2007-02-12 00:53:26 -08001041 spin_unlock_irqrestore(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 return 0;
1043}
1044
1045static void capinc_tty_close(struct tty_struct * tty, struct file * file)
1046{
1047 struct capiminor *mp;
1048
1049 mp = (struct capiminor *)tty->driver_data;
1050 if (mp) {
1051 if (atomic_dec_and_test(&mp->ttyopencount)) {
1052#ifdef _DEBUG_REFCOUNT
1053 printk(KERN_DEBUG "capinc_tty_close lastclose\n");
1054#endif
1055 tty->driver_data = NULL;
1056 mp->tty = NULL;
1057 }
1058#ifdef _DEBUG_REFCOUNT
1059 printk(KERN_DEBUG "capinc_tty_close ocount=%d\n", atomic_read(&mp->ttyopencount));
1060#endif
1061 if (mp->nccip == 0)
1062 capiminor_free(mp);
1063 }
1064
1065#ifdef _DEBUG_REFCOUNT
1066 printk(KERN_DEBUG "capinc_tty_close\n");
1067#endif
1068}
1069
1070static int capinc_tty_write(struct tty_struct * tty,
1071 const unsigned char *buf, int count)
1072{
1073 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1074 struct sk_buff *skb;
Michael Buesch053b47f2007-02-12 00:53:26 -08001075 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
1077#ifdef _DEBUG_TTYFUNCS
1078 printk(KERN_DEBUG "capinc_tty_write(count=%d)\n", count);
1079#endif
1080
1081 if (!mp || !mp->nccip) {
1082#ifdef _DEBUG_TTYFUNCS
1083 printk(KERN_DEBUG "capinc_tty_write: mp or mp->ncci NULL\n");
1084#endif
1085 return 0;
1086 }
1087
Michael Buesch053b47f2007-02-12 00:53:26 -08001088 spin_lock_irqsave(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 skb = mp->ttyskb;
1090 if (skb) {
1091 mp->ttyskb = NULL;
1092 skb_queue_tail(&mp->outqueue, skb);
1093 mp->outbytes += skb->len;
1094 }
1095
1096 skb = alloc_skb(CAPI_DATA_B3_REQ_LEN+count, GFP_ATOMIC);
1097 if (!skb) {
1098 printk(KERN_ERR "capinc_tty_write: alloc_skb failed\n");
Michael Buesch053b47f2007-02-12 00:53:26 -08001099 spin_unlock_irqrestore(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 return -ENOMEM;
1101 }
1102
1103 skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
1104 memcpy(skb_put(skb, count), buf, count);
1105
1106 skb_queue_tail(&mp->outqueue, skb);
1107 mp->outbytes += skb->len;
1108 (void)handle_minor_send(mp);
1109 (void)handle_minor_recv(mp);
Michael Buesch053b47f2007-02-12 00:53:26 -08001110 spin_unlock_irqrestore(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 return count;
1112}
1113
1114static void capinc_tty_put_char(struct tty_struct *tty, unsigned char ch)
1115{
1116 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1117 struct sk_buff *skb;
Michael Buesch053b47f2007-02-12 00:53:26 -08001118 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
1120#ifdef _DEBUG_TTYFUNCS
1121 printk(KERN_DEBUG "capinc_put_char(%u)\n", ch);
1122#endif
1123
1124 if (!mp || !mp->nccip) {
1125#ifdef _DEBUG_TTYFUNCS
1126 printk(KERN_DEBUG "capinc_tty_put_char: mp or mp->ncci NULL\n");
1127#endif
1128 return;
1129 }
1130
Michael Buesch053b47f2007-02-12 00:53:26 -08001131 spin_lock_irqsave(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 skb = mp->ttyskb;
1133 if (skb) {
1134 if (skb_tailroom(skb) > 0) {
1135 *(skb_put(skb, 1)) = ch;
Michael Buesch053b47f2007-02-12 00:53:26 -08001136 spin_unlock_irqrestore(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 return;
1138 }
1139 mp->ttyskb = NULL;
1140 skb_queue_tail(&mp->outqueue, skb);
1141 mp->outbytes += skb->len;
1142 (void)handle_minor_send(mp);
1143 }
1144 skb = alloc_skb(CAPI_DATA_B3_REQ_LEN+CAPI_MAX_BLKSIZE, GFP_ATOMIC);
1145 if (skb) {
1146 skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
1147 *(skb_put(skb, 1)) = ch;
1148 mp->ttyskb = skb;
1149 } else {
1150 printk(KERN_ERR "capinc_put_char: char %u lost\n", ch);
1151 }
Michael Buesch053b47f2007-02-12 00:53:26 -08001152 spin_unlock_irqrestore(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153}
1154
1155static void capinc_tty_flush_chars(struct tty_struct *tty)
1156{
1157 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1158 struct sk_buff *skb;
Michael Buesch053b47f2007-02-12 00:53:26 -08001159 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
1161#ifdef _DEBUG_TTYFUNCS
1162 printk(KERN_DEBUG "capinc_tty_flush_chars\n");
1163#endif
1164
1165 if (!mp || !mp->nccip) {
1166#ifdef _DEBUG_TTYFUNCS
1167 printk(KERN_DEBUG "capinc_tty_flush_chars: mp or mp->ncci NULL\n");
1168#endif
1169 return;
1170 }
1171
Michael Buesch053b47f2007-02-12 00:53:26 -08001172 spin_lock_irqsave(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 skb = mp->ttyskb;
1174 if (skb) {
1175 mp->ttyskb = NULL;
1176 skb_queue_tail(&mp->outqueue, skb);
1177 mp->outbytes += skb->len;
1178 (void)handle_minor_send(mp);
1179 }
1180 (void)handle_minor_recv(mp);
Michael Buesch053b47f2007-02-12 00:53:26 -08001181 spin_unlock_irqrestore(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182}
1183
1184static int capinc_tty_write_room(struct tty_struct *tty)
1185{
1186 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1187 int room;
1188 if (!mp || !mp->nccip) {
1189#ifdef _DEBUG_TTYFUNCS
1190 printk(KERN_DEBUG "capinc_tty_write_room: mp or mp->ncci NULL\n");
1191#endif
1192 return 0;
1193 }
1194 room = CAPINC_MAX_SENDQUEUE-skb_queue_len(&mp->outqueue);
1195 room *= CAPI_MAX_BLKSIZE;
1196#ifdef _DEBUG_TTYFUNCS
1197 printk(KERN_DEBUG "capinc_tty_write_room = %d\n", room);
1198#endif
1199 return room;
1200}
1201
Adrian Bunk408b6642005-05-01 08:59:29 -07001202static int capinc_tty_chars_in_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203{
1204 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1205 if (!mp || !mp->nccip) {
1206#ifdef _DEBUG_TTYFUNCS
1207 printk(KERN_DEBUG "capinc_tty_chars_in_buffer: mp or mp->ncci NULL\n");
1208#endif
1209 return 0;
1210 }
1211#ifdef _DEBUG_TTYFUNCS
1212 printk(KERN_DEBUG "capinc_tty_chars_in_buffer = %d nack=%d sq=%d rq=%d\n",
1213 mp->outbytes, mp->nack,
1214 skb_queue_len(&mp->outqueue),
1215 skb_queue_len(&mp->inqueue));
1216#endif
1217 return mp->outbytes;
1218}
1219
1220static int capinc_tty_ioctl(struct tty_struct *tty, struct file * file,
1221 unsigned int cmd, unsigned long arg)
1222{
1223 int error = 0;
1224 switch (cmd) {
1225 default:
1226 error = n_tty_ioctl (tty, file, cmd, arg);
1227 break;
1228 }
1229 return error;
1230}
1231
Alan Cox606d0992006-12-08 02:38:45 -08001232static void capinc_tty_set_termios(struct tty_struct *tty, struct ktermios * old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233{
1234#ifdef _DEBUG_TTYFUNCS
1235 printk(KERN_DEBUG "capinc_tty_set_termios\n");
1236#endif
1237}
1238
1239static void capinc_tty_throttle(struct tty_struct * tty)
1240{
1241 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1242#ifdef _DEBUG_TTYFUNCS
1243 printk(KERN_DEBUG "capinc_tty_throttle\n");
1244#endif
1245 if (mp)
1246 mp->ttyinstop = 1;
1247}
1248
1249static void capinc_tty_unthrottle(struct tty_struct * tty)
1250{
1251 struct capiminor *mp = (struct capiminor *)tty->driver_data;
Michael Buesch053b47f2007-02-12 00:53:26 -08001252 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253#ifdef _DEBUG_TTYFUNCS
1254 printk(KERN_DEBUG "capinc_tty_unthrottle\n");
1255#endif
1256 if (mp) {
Michael Buesch053b47f2007-02-12 00:53:26 -08001257 spin_lock_irqsave(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 mp->ttyinstop = 0;
1259 handle_minor_recv(mp);
Michael Buesch053b47f2007-02-12 00:53:26 -08001260 spin_unlock_irqrestore(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 }
1262}
1263
1264static void capinc_tty_stop(struct tty_struct *tty)
1265{
1266 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1267#ifdef _DEBUG_TTYFUNCS
1268 printk(KERN_DEBUG "capinc_tty_stop\n");
1269#endif
1270 if (mp) {
1271 mp->ttyoutstop = 1;
1272 }
1273}
1274
1275static void capinc_tty_start(struct tty_struct *tty)
1276{
1277 struct capiminor *mp = (struct capiminor *)tty->driver_data;
Michael Buesch053b47f2007-02-12 00:53:26 -08001278 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279#ifdef _DEBUG_TTYFUNCS
1280 printk(KERN_DEBUG "capinc_tty_start\n");
1281#endif
1282 if (mp) {
Michael Buesch053b47f2007-02-12 00:53:26 -08001283 spin_lock_irqsave(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 mp->ttyoutstop = 0;
1285 (void)handle_minor_send(mp);
Michael Buesch053b47f2007-02-12 00:53:26 -08001286 spin_unlock_irqrestore(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 }
1288}
1289
1290static void capinc_tty_hangup(struct tty_struct *tty)
1291{
1292#ifdef _DEBUG_TTYFUNCS
1293 printk(KERN_DEBUG "capinc_tty_hangup\n");
1294#endif
1295}
1296
1297static void capinc_tty_break_ctl(struct tty_struct *tty, int state)
1298{
1299#ifdef _DEBUG_TTYFUNCS
1300 printk(KERN_DEBUG "capinc_tty_break_ctl(%d)\n", state);
1301#endif
1302}
1303
1304static void capinc_tty_flush_buffer(struct tty_struct *tty)
1305{
1306#ifdef _DEBUG_TTYFUNCS
1307 printk(KERN_DEBUG "capinc_tty_flush_buffer\n");
1308#endif
1309}
1310
1311static void capinc_tty_set_ldisc(struct tty_struct *tty)
1312{
1313#ifdef _DEBUG_TTYFUNCS
1314 printk(KERN_DEBUG "capinc_tty_set_ldisc\n");
1315#endif
1316}
1317
1318static void capinc_tty_send_xchar(struct tty_struct *tty, char ch)
1319{
1320#ifdef _DEBUG_TTYFUNCS
1321 printk(KERN_DEBUG "capinc_tty_send_xchar(%d)\n", ch);
1322#endif
1323}
1324
1325static int capinc_tty_read_proc(char *page, char **start, off_t off,
1326 int count, int *eof, void *data)
1327{
1328 return 0;
1329}
1330
1331static struct tty_driver *capinc_tty_driver;
1332
Jeff Dikeb68e31d2006-10-02 02:17:18 -07001333static const struct tty_operations capinc_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 .open = capinc_tty_open,
1335 .close = capinc_tty_close,
1336 .write = capinc_tty_write,
1337 .put_char = capinc_tty_put_char,
1338 .flush_chars = capinc_tty_flush_chars,
1339 .write_room = capinc_tty_write_room,
1340 .chars_in_buffer = capinc_tty_chars_in_buffer,
1341 .ioctl = capinc_tty_ioctl,
1342 .set_termios = capinc_tty_set_termios,
1343 .throttle = capinc_tty_throttle,
1344 .unthrottle = capinc_tty_unthrottle,
1345 .stop = capinc_tty_stop,
1346 .start = capinc_tty_start,
1347 .hangup = capinc_tty_hangup,
1348 .break_ctl = capinc_tty_break_ctl,
1349 .flush_buffer = capinc_tty_flush_buffer,
1350 .set_ldisc = capinc_tty_set_ldisc,
1351 .send_xchar = capinc_tty_send_xchar,
1352 .read_proc = capinc_tty_read_proc,
1353};
1354
1355static int capinc_tty_init(void)
1356{
1357 struct tty_driver *drv;
1358
1359 if (capi_ttyminors > CAPINC_MAX_PORTS)
1360 capi_ttyminors = CAPINC_MAX_PORTS;
1361 if (capi_ttyminors <= 0)
1362 capi_ttyminors = CAPINC_NR_PORTS;
1363
1364 drv = alloc_tty_driver(capi_ttyminors);
1365 if (!drv)
1366 return -ENOMEM;
1367
1368 drv->owner = THIS_MODULE;
1369 drv->driver_name = "capi_nc";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 drv->name = "capi";
1371 drv->major = capi_ttymajor;
1372 drv->minor_start = 0;
1373 drv->type = TTY_DRIVER_TYPE_SERIAL;
1374 drv->subtype = SERIAL_TYPE_NORMAL;
1375 drv->init_termios = tty_std_termios;
1376 drv->init_termios.c_iflag = ICRNL;
1377 drv->init_termios.c_oflag = OPOST | ONLCR;
1378 drv->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1379 drv->init_termios.c_lflag = 0;
1380 drv->flags = TTY_DRIVER_REAL_RAW|TTY_DRIVER_RESET_TERMIOS;
1381 tty_set_operations(drv, &capinc_ops);
1382 if (tty_register_driver(drv)) {
1383 put_tty_driver(drv);
1384 printk(KERN_ERR "Couldn't register capi_nc driver\n");
1385 return -1;
1386 }
1387 capinc_tty_driver = drv;
1388 return 0;
1389}
1390
1391static void capinc_tty_exit(void)
1392{
1393 struct tty_driver *drv = capinc_tty_driver;
1394 int retval;
1395 if ((retval = tty_unregister_driver(drv)))
1396 printk(KERN_ERR "capi: failed to unregister capi_nc driver (%d)\n", retval);
1397 put_tty_driver(drv);
1398}
1399
1400#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
1401
1402/* -------- /proc functions ----------------------------------------- */
1403
1404/*
1405 * /proc/capi/capi20:
1406 * minor applid nrecvctlpkt nrecvdatapkt nsendctlpkt nsenddatapkt
1407 */
1408static int proc_capidev_read_proc(char *page, char **start, off_t off,
1409 int count, int *eof, void *data)
1410{
1411 struct capidev *cdev;
1412 struct list_head *l;
1413 int len = 0;
1414
1415 read_lock(&capidev_list_lock);
1416 list_for_each(l, &capidev_list) {
1417 cdev = list_entry(l, struct capidev, list);
1418 len += sprintf(page+len, "0 %d %lu %lu %lu %lu\n",
1419 cdev->ap.applid,
1420 cdev->ap.nrecvctlpkt,
1421 cdev->ap.nrecvdatapkt,
1422 cdev->ap.nsentctlpkt,
1423 cdev->ap.nsentdatapkt);
1424 if (len <= off) {
1425 off -= len;
1426 len = 0;
1427 } else {
1428 if (len-off > count)
1429 goto endloop;
1430 }
1431 }
1432
1433endloop:
1434 read_unlock(&capidev_list_lock);
1435 if (len < count)
1436 *eof = 1;
1437 if (len > count) len = count;
1438 if (len < 0) len = 0;
1439 return len;
1440}
1441
1442/*
1443 * /proc/capi/capi20ncci:
1444 * applid ncci
1445 */
1446static int proc_capincci_read_proc(char *page, char **start, off_t off,
1447 int count, int *eof, void *data)
1448{
1449 struct capidev *cdev;
1450 struct capincci *np;
1451 struct list_head *l;
1452 int len = 0;
1453
1454 read_lock(&capidev_list_lock);
1455 list_for_each(l, &capidev_list) {
1456 cdev = list_entry(l, struct capidev, list);
1457 for (np=cdev->nccis; np; np = np->next) {
1458 len += sprintf(page+len, "%d 0x%x\n",
1459 cdev->ap.applid,
1460 np->ncci);
1461 if (len <= off) {
1462 off -= len;
1463 len = 0;
1464 } else {
1465 if (len-off > count)
1466 goto endloop;
1467 }
1468 }
1469 }
1470endloop:
1471 read_unlock(&capidev_list_lock);
1472 *start = page+off;
1473 if (len < count)
1474 *eof = 1;
1475 if (len>count) len = count;
1476 if (len<0) len = 0;
1477 return len;
1478}
1479
1480static struct procfsentries {
1481 char *name;
1482 mode_t mode;
1483 int (*read_proc)(char *page, char **start, off_t off,
1484 int count, int *eof, void *data);
1485 struct proc_dir_entry *procent;
1486} procfsentries[] = {
1487 /* { "capi", S_IFDIR, 0 }, */
1488 { "capi/capi20", 0 , proc_capidev_read_proc },
1489 { "capi/capi20ncci", 0 , proc_capincci_read_proc },
1490};
1491
1492static void __init proc_init(void)
1493{
Ahmed S. Darwishfd863db2007-02-12 00:53:19 -08001494 int nelem = ARRAY_SIZE(procfsentries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 int i;
1496
1497 for (i=0; i < nelem; i++) {
1498 struct procfsentries *p = procfsentries + i;
1499 p->procent = create_proc_entry(p->name, p->mode, NULL);
1500 if (p->procent) p->procent->read_proc = p->read_proc;
1501 }
1502}
1503
1504static void __exit proc_exit(void)
1505{
Ahmed S. Darwishfd863db2007-02-12 00:53:19 -08001506 int nelem = ARRAY_SIZE(procfsentries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 int i;
1508
1509 for (i=nelem-1; i >= 0; i--) {
1510 struct procfsentries *p = procfsentries + i;
1511 if (p->procent) {
1512 remove_proc_entry(p->name, NULL);
1513 p->procent = NULL;
1514 }
1515 }
1516}
1517
1518/* -------- init function and module interface ---------------------- */
1519
1520
1521static char rev[32];
1522
1523static int __init capi_init(void)
1524{
1525 char *p;
1526 char *compileinfo;
Andrew Morton6d9eac32006-03-28 01:56:19 -08001527 int major_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
1529 if ((p = strchr(revision, ':')) != 0 && p[1]) {
1530 strlcpy(rev, p + 2, sizeof(rev));
1531 if ((p = strchr(rev, '$')) != 0 && p > rev)
1532 *(p-1) = 0;
1533 } else
1534 strcpy(rev, "1.0");
1535
Andrew Morton6d9eac32006-03-28 01:56:19 -08001536 major_ret = register_chrdev(capi_major, "capi20", &capi_fops);
1537 if (major_ret < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 printk(KERN_ERR "capi20: unable to get major %d\n", capi_major);
Andrew Morton6d9eac32006-03-28 01:56:19 -08001539 return major_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 }
gregkh@suse.de56b22932005-03-23 10:01:41 -08001541 capi_class = class_create(THIS_MODULE, "capi");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 if (IS_ERR(capi_class)) {
1543 unregister_chrdev(capi_major, "capi20");
1544 return PTR_ERR(capi_class);
1545 }
1546
Greg Kroah-Hartman53f46542005-10-27 22:25:43 -07001547 class_device_create(capi_class, NULL, MKDEV(capi_major, 0), NULL, "capi");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548
1549#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1550 if (capinc_tty_init() < 0) {
gregkh@suse.de56b22932005-03-23 10:01:41 -08001551 class_device_destroy(capi_class, MKDEV(capi_major, 0));
1552 class_destroy(capi_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 unregister_chrdev(capi_major, "capi20");
1554 return -ENOMEM;
1555 }
1556#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
1557
1558 proc_init();
1559
1560#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1561#if defined(CONFIG_ISDN_CAPI_CAPIFS) || defined(CONFIG_ISDN_CAPI_CAPIFS_MODULE)
1562 compileinfo = " (middleware+capifs)";
1563#else
1564 compileinfo = " (no capifs)";
1565#endif
1566#else
1567 compileinfo = " (no middleware)";
1568#endif
1569 printk(KERN_NOTICE "capi20: Rev %s: started up with major %d%s\n",
1570 rev, capi_major, compileinfo);
1571
1572 return 0;
1573}
1574
1575static void __exit capi_exit(void)
1576{
1577 proc_exit();
1578
gregkh@suse.de56b22932005-03-23 10:01:41 -08001579 class_device_destroy(capi_class, MKDEV(capi_major, 0));
1580 class_destroy(capi_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 unregister_chrdev(capi_major, "capi20");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582
1583#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1584 capinc_tty_exit();
1585#endif
1586 printk(KERN_NOTICE "capi: Rev %s: unloaded\n", rev);
1587}
1588
1589module_init(capi_init);
1590module_exit(capi_exit);