[PATCH] devfs: Remove devfs_remove() function from the kernel tree
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / isdn / capi / capi.c
1 /* $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
12 #include <linux/config.h>
13 #include <linux/module.h>
14 #include <linux/errno.h>
15 #include <linux/kernel.h>
16 #include <linux/major.h>
17 #include <linux/sched.h>
18 #include <linux/slab.h>
19 #include <linux/fcntl.h>
20 #include <linux/fs.h>
21 #include <linux/signal.h>
22 #include <linux/mm.h>
23 #include <linux/smp_lock.h>
24 #include <linux/timer.h>
25 #include <linux/wait.h>
26 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
27 #include <linux/tty.h>
28 #ifdef CONFIG_PPP
29 #include <linux/netdevice.h>
30 #include <linux/ppp_defs.h>
31 #include <linux/if_ppp.h>
32 #endif /* CONFIG_PPP */
33 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
34 #include <linux/skbuff.h>
35 #include <linux/proc_fs.h>
36 #include <linux/poll.h>
37 #include <linux/capi.h>
38 #include <linux/kernelcapi.h>
39 #include <linux/init.h>
40 #include <linux/device.h>
41 #include <linux/moduleparam.h>
42 #include <linux/devfs_fs_kernel.h>
43 #include <linux/isdn/capiutil.h>
44 #include <linux/isdn/capicmd.h>
45 #if defined(CONFIG_ISDN_CAPI_CAPIFS) || defined(CONFIG_ISDN_CAPI_CAPIFS_MODULE)
46 #include "capifs.h"
47 #endif
48
49 static char *revision = "$Revision: 1.1.2.7 $";
50
51 MODULE_DESCRIPTION("CAPI4Linux: Userspace /dev/capi20 interface");
52 MODULE_AUTHOR("Carsten Paeth");
53 MODULE_LICENSE("GPL");
54
55 #undef _DEBUG_REFCOUNT /* alloc/free and open/close debug */
56 #undef _DEBUG_TTYFUNCS /* call to tty_driver */
57 #undef _DEBUG_DATAFLOW /* data flow */
58
59 /* -------- driver information -------------------------------------- */
60
61 static struct class *capi_class;
62
63 static int capi_major = 68; /* allocated */
64 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
65 #define CAPINC_NR_PORTS 32
66 #define CAPINC_MAX_PORTS 256
67 static int capi_ttymajor = 191;
68 static int capi_ttyminors = CAPINC_NR_PORTS;
69 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
70
71 module_param_named(major, capi_major, uint, 0);
72 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
73 module_param_named(ttymajor, capi_ttymajor, uint, 0);
74 module_param_named(ttyminors, capi_ttyminors, uint, 0);
75 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
76
77 /* -------- defines ------------------------------------------------- */
78
79 #define CAPINC_MAX_RECVQUEUE 10
80 #define CAPINC_MAX_SENDQUEUE 10
81 #define CAPI_MAX_BLKSIZE 2048
82
83 /* -------- data structures ----------------------------------------- */
84
85 struct capidev;
86 struct capincci;
87 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
88 struct capiminor;
89
90 struct datahandle_queue {
91 struct list_head list;
92 u16 datahandle;
93 };
94
95 struct capiminor {
96 struct list_head list;
97 struct capincci *nccip;
98 unsigned int minor;
99
100 struct capi20_appl *ap;
101 u32 ncci;
102 u16 datahandle;
103 u16 msgid;
104
105 struct tty_struct *tty;
106 int ttyinstop;
107 int ttyoutstop;
108 struct sk_buff *ttyskb;
109 atomic_t ttyopencount;
110
111 struct sk_buff_head inqueue;
112 int inbytes;
113 struct sk_buff_head outqueue;
114 int outbytes;
115
116 /* transmit path */
117 struct list_head ackqueue;
118 int nack;
119 spinlock_t ackqlock;
120 };
121 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
122
123 struct capincci {
124 struct capincci *next;
125 u32 ncci;
126 struct capidev *cdev;
127 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
128 struct capiminor *minorp;
129 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
130 };
131
132 struct capidev {
133 struct list_head list;
134 struct capi20_appl ap;
135 u16 errcode;
136 unsigned userflags;
137
138 struct sk_buff_head recvqueue;
139 wait_queue_head_t recvwait;
140
141 struct capincci *nccis;
142
143 struct semaphore ncci_list_sem;
144 };
145
146 /* -------- global variables ---------------------------------------- */
147
148 static DEFINE_RWLOCK(capidev_list_lock);
149 static LIST_HEAD(capidev_list);
150
151 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
152 static DEFINE_RWLOCK(capiminor_list_lock);
153 static LIST_HEAD(capiminor_list);
154 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
155
156 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
157 /* -------- datahandles --------------------------------------------- */
158
159 static int capincci_add_ack(struct capiminor *mp, u16 datahandle)
160 {
161 struct datahandle_queue *n;
162 unsigned long flags;
163
164 n = kmalloc(sizeof(*n), GFP_ATOMIC);
165 if (unlikely(!n)) {
166 printk(KERN_ERR "capi: alloc datahandle failed\n");
167 return -1;
168 }
169 n->datahandle = datahandle;
170 INIT_LIST_HEAD(&n->list);
171 spin_lock_irqsave(&mp->ackqlock, flags);
172 list_add_tail(&n->list, &mp->ackqueue);
173 mp->nack++;
174 spin_unlock_irqrestore(&mp->ackqlock, flags);
175 return 0;
176 }
177
178 static int capiminor_del_ack(struct capiminor *mp, u16 datahandle)
179 {
180 struct datahandle_queue *p, *tmp;
181 unsigned long flags;
182
183 spin_lock_irqsave(&mp->ackqlock, flags);
184 list_for_each_entry_safe(p, tmp, &mp->ackqueue, list) {
185 if (p->datahandle == datahandle) {
186 list_del(&p->list);
187 kfree(p);
188 mp->nack--;
189 spin_unlock_irqrestore(&mp->ackqlock, flags);
190 return 0;
191 }
192 }
193 spin_unlock_irqrestore(&mp->ackqlock, flags);
194 return -1;
195 }
196
197 static void capiminor_del_all_ack(struct capiminor *mp)
198 {
199 struct datahandle_queue *p, *tmp;
200 unsigned long flags;
201
202 spin_lock_irqsave(&mp->ackqlock, flags);
203 list_for_each_entry_safe(p, tmp, &mp->ackqueue, list) {
204 list_del(&p->list);
205 kfree(p);
206 mp->nack--;
207 }
208 spin_unlock_irqrestore(&mp->ackqlock, flags);
209 }
210
211
212 /* -------- struct capiminor ---------------------------------------- */
213
214 static struct capiminor *capiminor_alloc(struct capi20_appl *ap, u32 ncci)
215 {
216 struct capiminor *mp, *p;
217 unsigned int minor = 0;
218 unsigned long flags;
219
220 mp = kmalloc(sizeof(*mp), GFP_ATOMIC);
221 if (!mp) {
222 printk(KERN_ERR "capi: can't alloc capiminor\n");
223 return NULL;
224 }
225
226 memset(mp, 0, sizeof(struct capiminor));
227 mp->ap = ap;
228 mp->ncci = ncci;
229 mp->msgid = 0;
230 atomic_set(&mp->ttyopencount,0);
231 INIT_LIST_HEAD(&mp->ackqueue);
232 spin_lock_init(&mp->ackqlock);
233
234 skb_queue_head_init(&mp->inqueue);
235 skb_queue_head_init(&mp->outqueue);
236
237 /* Allocate the least unused minor number.
238 */
239 write_lock_irqsave(&capiminor_list_lock, flags);
240 if (list_empty(&capiminor_list))
241 list_add(&mp->list, &capiminor_list);
242 else {
243 list_for_each_entry(p, &capiminor_list, list) {
244 if (p->minor > minor)
245 break;
246 minor++;
247 }
248
249 if (minor < capi_ttyminors) {
250 mp->minor = minor;
251 list_add(&mp->list, p->list.prev);
252 }
253 }
254 write_unlock_irqrestore(&capiminor_list_lock, flags);
255
256 if (!(minor < capi_ttyminors)) {
257 printk(KERN_NOTICE "capi: out of minors\n");
258 kfree(mp);
259 return NULL;
260 }
261
262 return mp;
263 }
264
265 static void capiminor_free(struct capiminor *mp)
266 {
267 unsigned long flags;
268
269 write_lock_irqsave(&capiminor_list_lock, flags);
270 list_del(&mp->list);
271 write_unlock_irqrestore(&capiminor_list_lock, flags);
272
273 if (mp->ttyskb) kfree_skb(mp->ttyskb);
274 mp->ttyskb = NULL;
275 skb_queue_purge(&mp->inqueue);
276 skb_queue_purge(&mp->outqueue);
277 capiminor_del_all_ack(mp);
278 kfree(mp);
279 }
280
281 static struct capiminor *capiminor_find(unsigned int minor)
282 {
283 struct list_head *l;
284 struct capiminor *p = NULL;
285
286 read_lock(&capiminor_list_lock);
287 list_for_each(l, &capiminor_list) {
288 p = list_entry(l, struct capiminor, list);
289 if (p->minor == minor)
290 break;
291 }
292 read_unlock(&capiminor_list_lock);
293 if (l == &capiminor_list)
294 return NULL;
295
296 return p;
297 }
298 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
299
300 /* -------- struct capincci ----------------------------------------- */
301
302 static struct capincci *capincci_alloc(struct capidev *cdev, u32 ncci)
303 {
304 struct capincci *np, **pp;
305 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
306 struct capiminor *mp = NULL;
307 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
308
309 np = kmalloc(sizeof(*np), GFP_ATOMIC);
310 if (!np)
311 return NULL;
312 memset(np, 0, sizeof(struct capincci));
313 np->ncci = ncci;
314 np->cdev = cdev;
315 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
316 mp = NULL;
317 if (cdev->userflags & CAPIFLAG_HIGHJACKING)
318 mp = np->minorp = capiminor_alloc(&cdev->ap, ncci);
319 if (mp) {
320 mp->nccip = np;
321 #ifdef _DEBUG_REFCOUNT
322 printk(KERN_DEBUG "set mp->nccip\n");
323 #endif
324 #if defined(CONFIG_ISDN_CAPI_CAPIFS) || defined(CONFIG_ISDN_CAPI_CAPIFS_MODULE)
325 capifs_new_ncci(mp->minor, MKDEV(capi_ttymajor, mp->minor));
326 #endif
327 }
328 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
329 for (pp=&cdev->nccis; *pp; pp = &(*pp)->next)
330 ;
331 *pp = np;
332 return np;
333 }
334
335 static void capincci_free(struct capidev *cdev, u32 ncci)
336 {
337 struct capincci *np, **pp;
338 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
339 struct capiminor *mp;
340 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
341
342 pp=&cdev->nccis;
343 while (*pp) {
344 np = *pp;
345 if (ncci == 0xffffffff || np->ncci == ncci) {
346 *pp = (*pp)->next;
347 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
348 if ((mp = np->minorp) != 0) {
349 #if defined(CONFIG_ISDN_CAPI_CAPIFS) || defined(CONFIG_ISDN_CAPI_CAPIFS_MODULE)
350 capifs_free_ncci(mp->minor);
351 #endif
352 if (mp->tty) {
353 mp->nccip = NULL;
354 #ifdef _DEBUG_REFCOUNT
355 printk(KERN_DEBUG "reset mp->nccip\n");
356 #endif
357 tty_hangup(mp->tty);
358 } else {
359 capiminor_free(mp);
360 }
361 }
362 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
363 kfree(np);
364 if (*pp == 0) return;
365 } else {
366 pp = &(*pp)->next;
367 }
368 }
369 }
370
371 static struct capincci *capincci_find(struct capidev *cdev, u32 ncci)
372 {
373 struct capincci *p;
374
375 for (p=cdev->nccis; p ; p = p->next) {
376 if (p->ncci == ncci)
377 break;
378 }
379 return p;
380 }
381
382 /* -------- struct capidev ------------------------------------------ */
383
384 static struct capidev *capidev_alloc(void)
385 {
386 struct capidev *cdev;
387 unsigned long flags;
388
389 cdev = kmalloc(sizeof(*cdev), GFP_KERNEL);
390 if (!cdev)
391 return NULL;
392 memset(cdev, 0, sizeof(struct capidev));
393
394 init_MUTEX(&cdev->ncci_list_sem);
395 skb_queue_head_init(&cdev->recvqueue);
396 init_waitqueue_head(&cdev->recvwait);
397 write_lock_irqsave(&capidev_list_lock, flags);
398 list_add_tail(&cdev->list, &capidev_list);
399 write_unlock_irqrestore(&capidev_list_lock, flags);
400 return cdev;
401 }
402
403 static void capidev_free(struct capidev *cdev)
404 {
405 unsigned long flags;
406
407 if (cdev->ap.applid) {
408 capi20_release(&cdev->ap);
409 cdev->ap.applid = 0;
410 }
411 skb_queue_purge(&cdev->recvqueue);
412
413 down(&cdev->ncci_list_sem);
414 capincci_free(cdev, 0xffffffff);
415 up(&cdev->ncci_list_sem);
416
417 write_lock_irqsave(&capidev_list_lock, flags);
418 list_del(&cdev->list);
419 write_unlock_irqrestore(&capidev_list_lock, flags);
420 kfree(cdev);
421 }
422
423 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
424 /* -------- handle data queue --------------------------------------- */
425
426 static struct sk_buff *
427 gen_data_b3_resp_for(struct capiminor *mp, struct sk_buff *skb)
428 {
429 struct sk_buff *nskb;
430 nskb = alloc_skb(CAPI_DATA_B3_RESP_LEN, GFP_ATOMIC);
431 if (nskb) {
432 u16 datahandle = CAPIMSG_U16(skb->data,CAPIMSG_BASELEN+4+4+2);
433 unsigned char *s = skb_put(nskb, CAPI_DATA_B3_RESP_LEN);
434 capimsg_setu16(s, 0, CAPI_DATA_B3_RESP_LEN);
435 capimsg_setu16(s, 2, mp->ap->applid);
436 capimsg_setu8 (s, 4, CAPI_DATA_B3);
437 capimsg_setu8 (s, 5, CAPI_RESP);
438 capimsg_setu16(s, 6, mp->msgid++);
439 capimsg_setu32(s, 8, mp->ncci);
440 capimsg_setu16(s, 12, datahandle);
441 }
442 return nskb;
443 }
444
445 static int handle_recv_skb(struct capiminor *mp, struct sk_buff *skb)
446 {
447 struct sk_buff *nskb;
448 int datalen;
449 u16 errcode, datahandle;
450 struct tty_ldisc *ld;
451
452 datalen = skb->len - CAPIMSG_LEN(skb->data);
453 if (mp->tty == NULL)
454 {
455 #ifdef _DEBUG_DATAFLOW
456 printk(KERN_DEBUG "capi: currently no receiver\n");
457 #endif
458 return -1;
459 }
460
461 ld = tty_ldisc_ref(mp->tty);
462 if (ld == NULL)
463 return -1;
464 if (ld->receive_buf == NULL) {
465 #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
466 printk(KERN_DEBUG "capi: ldisc has no receive_buf function\n");
467 #endif
468 goto bad;
469 }
470 if (mp->ttyinstop) {
471 #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
472 printk(KERN_DEBUG "capi: recv tty throttled\n");
473 #endif
474 goto bad;
475 }
476 if (mp->tty->receive_room < datalen) {
477 #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
478 printk(KERN_DEBUG "capi: no room in tty\n");
479 #endif
480 goto bad;
481 }
482 if ((nskb = gen_data_b3_resp_for(mp, skb)) == 0) {
483 printk(KERN_ERR "capi: gen_data_b3_resp failed\n");
484 goto bad;
485 }
486 datahandle = CAPIMSG_U16(skb->data,CAPIMSG_BASELEN+4);
487 errcode = capi20_put_message(mp->ap, nskb);
488 if (errcode != CAPI_NOERROR) {
489 printk(KERN_ERR "capi: send DATA_B3_RESP failed=%x\n",
490 errcode);
491 kfree_skb(nskb);
492 goto bad;
493 }
494 (void)skb_pull(skb, CAPIMSG_LEN(skb->data));
495 #ifdef _DEBUG_DATAFLOW
496 printk(KERN_DEBUG "capi: DATA_B3_RESP %u len=%d => ldisc\n",
497 datahandle, skb->len);
498 #endif
499 ld->receive_buf(mp->tty, skb->data, NULL, skb->len);
500 kfree_skb(skb);
501 tty_ldisc_deref(ld);
502 return 0;
503 bad:
504 tty_ldisc_deref(ld);
505 return -1;
506 }
507
508 static void handle_minor_recv(struct capiminor *mp)
509 {
510 struct sk_buff *skb;
511 while ((skb = skb_dequeue(&mp->inqueue)) != 0) {
512 unsigned int len = skb->len;
513 mp->inbytes -= len;
514 if (handle_recv_skb(mp, skb) < 0) {
515 skb_queue_head(&mp->inqueue, skb);
516 mp->inbytes += len;
517 return;
518 }
519 }
520 }
521
522 static int handle_minor_send(struct capiminor *mp)
523 {
524 struct sk_buff *skb;
525 u16 len;
526 int count = 0;
527 u16 errcode;
528 u16 datahandle;
529
530 if (mp->tty && mp->ttyoutstop) {
531 #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
532 printk(KERN_DEBUG "capi: send: tty stopped\n");
533 #endif
534 return 0;
535 }
536
537 while ((skb = skb_dequeue(&mp->outqueue)) != 0) {
538 datahandle = mp->datahandle;
539 len = (u16)skb->len;
540 skb_push(skb, CAPI_DATA_B3_REQ_LEN);
541 memset(skb->data, 0, CAPI_DATA_B3_REQ_LEN);
542 capimsg_setu16(skb->data, 0, CAPI_DATA_B3_REQ_LEN);
543 capimsg_setu16(skb->data, 2, mp->ap->applid);
544 capimsg_setu8 (skb->data, 4, CAPI_DATA_B3);
545 capimsg_setu8 (skb->data, 5, CAPI_REQ);
546 capimsg_setu16(skb->data, 6, mp->msgid++);
547 capimsg_setu32(skb->data, 8, mp->ncci); /* NCCI */
548 capimsg_setu32(skb->data, 12, (u32) skb->data); /* Data32 */
549 capimsg_setu16(skb->data, 16, len); /* Data length */
550 capimsg_setu16(skb->data, 18, datahandle);
551 capimsg_setu16(skb->data, 20, 0); /* Flags */
552
553 if (capincci_add_ack(mp, datahandle) < 0) {
554 skb_pull(skb, CAPI_DATA_B3_REQ_LEN);
555 skb_queue_head(&mp->outqueue, skb);
556 return count;
557 }
558 errcode = capi20_put_message(mp->ap, skb);
559 if (errcode == CAPI_NOERROR) {
560 mp->datahandle++;
561 count++;
562 mp->outbytes -= len;
563 #ifdef _DEBUG_DATAFLOW
564 printk(KERN_DEBUG "capi: DATA_B3_REQ %u len=%u\n",
565 datahandle, len);
566 #endif
567 continue;
568 }
569 capiminor_del_ack(mp, datahandle);
570
571 if (errcode == CAPI_SENDQUEUEFULL) {
572 skb_pull(skb, CAPI_DATA_B3_REQ_LEN);
573 skb_queue_head(&mp->outqueue, skb);
574 break;
575 }
576
577 /* ups, drop packet */
578 printk(KERN_ERR "capi: put_message = %x\n", errcode);
579 mp->outbytes -= len;
580 kfree_skb(skb);
581 }
582 return count;
583 }
584
585 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
586 /* -------- function called by lower level -------------------------- */
587
588 static void capi_recv_message(struct capi20_appl *ap, struct sk_buff *skb)
589 {
590 struct capidev *cdev = ap->private;
591 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
592 struct capiminor *mp;
593 u16 datahandle;
594 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
595 struct capincci *np;
596 u32 ncci;
597
598 if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_CONF) {
599 u16 info = CAPIMSG_U16(skb->data, 12); // Info field
600 if (info == 0) {
601 down(&cdev->ncci_list_sem);
602 capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
603 up(&cdev->ncci_list_sem);
604 }
605 }
606 if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_IND) {
607 down(&cdev->ncci_list_sem);
608 capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
609 up(&cdev->ncci_list_sem);
610 }
611 if (CAPIMSG_COMMAND(skb->data) != CAPI_DATA_B3) {
612 skb_queue_tail(&cdev->recvqueue, skb);
613 wake_up_interruptible(&cdev->recvwait);
614 return;
615 }
616 ncci = CAPIMSG_CONTROL(skb->data);
617 for (np = cdev->nccis; np && np->ncci != ncci; np = np->next)
618 ;
619 if (!np) {
620 printk(KERN_ERR "BUG: capi_signal: ncci not found\n");
621 skb_queue_tail(&cdev->recvqueue, skb);
622 wake_up_interruptible(&cdev->recvwait);
623 return;
624 }
625 #ifndef CONFIG_ISDN_CAPI_MIDDLEWARE
626 skb_queue_tail(&cdev->recvqueue, skb);
627 wake_up_interruptible(&cdev->recvwait);
628 #else /* CONFIG_ISDN_CAPI_MIDDLEWARE */
629 mp = np->minorp;
630 if (!mp) {
631 skb_queue_tail(&cdev->recvqueue, skb);
632 wake_up_interruptible(&cdev->recvwait);
633 return;
634 }
635
636
637 if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_IND) {
638
639 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4+4+2);
640 #ifdef _DEBUG_DATAFLOW
641 printk(KERN_DEBUG "capi_signal: DATA_B3_IND %u len=%d\n",
642 datahandle, skb->len-CAPIMSG_LEN(skb->data));
643 #endif
644 skb_queue_tail(&mp->inqueue, skb);
645 mp->inbytes += skb->len;
646 handle_minor_recv(mp);
647
648 } else if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_CONF) {
649
650 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4);
651 #ifdef _DEBUG_DATAFLOW
652 printk(KERN_DEBUG "capi_signal: DATA_B3_CONF %u 0x%x\n",
653 datahandle,
654 CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4+2));
655 #endif
656 kfree_skb(skb);
657 (void)capiminor_del_ack(mp, datahandle);
658 if (mp->tty)
659 tty_wakeup(mp->tty);
660 (void)handle_minor_send(mp);
661
662 } else {
663 /* ups, let capi application handle it :-) */
664 skb_queue_tail(&cdev->recvqueue, skb);
665 wake_up_interruptible(&cdev->recvwait);
666 }
667 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
668 }
669
670 /* -------- file_operations for capidev ----------------------------- */
671
672 static ssize_t
673 capi_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
674 {
675 struct capidev *cdev = (struct capidev *)file->private_data;
676 struct sk_buff *skb;
677 size_t copied;
678
679 if (!cdev->ap.applid)
680 return -ENODEV;
681
682 if ((skb = skb_dequeue(&cdev->recvqueue)) == 0) {
683
684 if (file->f_flags & O_NONBLOCK)
685 return -EAGAIN;
686
687 for (;;) {
688 interruptible_sleep_on(&cdev->recvwait);
689 if ((skb = skb_dequeue(&cdev->recvqueue)) != 0)
690 break;
691 if (signal_pending(current))
692 break;
693 }
694 if (skb == 0)
695 return -ERESTARTNOHAND;
696 }
697 if (skb->len > count) {
698 skb_queue_head(&cdev->recvqueue, skb);
699 return -EMSGSIZE;
700 }
701 if (copy_to_user(buf, skb->data, skb->len)) {
702 skb_queue_head(&cdev->recvqueue, skb);
703 return -EFAULT;
704 }
705 copied = skb->len;
706
707 kfree_skb(skb);
708
709 return copied;
710 }
711
712 static ssize_t
713 capi_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
714 {
715 struct capidev *cdev = (struct capidev *)file->private_data;
716 struct sk_buff *skb;
717 u16 mlen;
718
719 if (!cdev->ap.applid)
720 return -ENODEV;
721
722 skb = alloc_skb(count, GFP_USER);
723 if (!skb)
724 return -ENOMEM;
725
726 if (copy_from_user(skb_put(skb, count), buf, count)) {
727 kfree_skb(skb);
728 return -EFAULT;
729 }
730 mlen = CAPIMSG_LEN(skb->data);
731 if (CAPIMSG_CMD(skb->data) == CAPI_DATA_B3_REQ) {
732 if ((size_t)(mlen + CAPIMSG_DATALEN(skb->data)) != count) {
733 kfree_skb(skb);
734 return -EINVAL;
735 }
736 } else {
737 if (mlen != count) {
738 kfree_skb(skb);
739 return -EINVAL;
740 }
741 }
742 CAPIMSG_SETAPPID(skb->data, cdev->ap.applid);
743
744 if (CAPIMSG_CMD(skb->data) == CAPI_DISCONNECT_B3_RESP) {
745 down(&cdev->ncci_list_sem);
746 capincci_free(cdev, CAPIMSG_NCCI(skb->data));
747 up(&cdev->ncci_list_sem);
748 }
749
750 cdev->errcode = capi20_put_message(&cdev->ap, skb);
751
752 if (cdev->errcode) {
753 kfree_skb(skb);
754 return -EIO;
755 }
756 return count;
757 }
758
759 static unsigned int
760 capi_poll(struct file *file, poll_table * wait)
761 {
762 struct capidev *cdev = (struct capidev *)file->private_data;
763 unsigned int mask = 0;
764
765 if (!cdev->ap.applid)
766 return POLLERR;
767
768 poll_wait(file, &(cdev->recvwait), wait);
769 mask = POLLOUT | POLLWRNORM;
770 if (!skb_queue_empty(&cdev->recvqueue))
771 mask |= POLLIN | POLLRDNORM;
772 return mask;
773 }
774
775 static int
776 capi_ioctl(struct inode *inode, struct file *file,
777 unsigned int cmd, unsigned long arg)
778 {
779 struct capidev *cdev = file->private_data;
780 struct capi20_appl *ap = &cdev->ap;
781 capi_ioctl_struct data;
782 int retval = -EINVAL;
783 void __user *argp = (void __user *)arg;
784
785 switch (cmd) {
786 case CAPI_REGISTER:
787 {
788 if (ap->applid)
789 return -EEXIST;
790
791 if (copy_from_user(&cdev->ap.rparam, argp,
792 sizeof(struct capi_register_params)))
793 return -EFAULT;
794
795 cdev->ap.private = cdev;
796 cdev->ap.recv_message = capi_recv_message;
797 cdev->errcode = capi20_register(ap);
798 if (cdev->errcode) {
799 ap->applid = 0;
800 return -EIO;
801 }
802 }
803 return (int)ap->applid;
804
805 case CAPI_GET_VERSION:
806 {
807 if (copy_from_user(&data.contr, argp,
808 sizeof(data.contr)))
809 return -EFAULT;
810 cdev->errcode = capi20_get_version(data.contr, &data.version);
811 if (cdev->errcode)
812 return -EIO;
813 if (copy_to_user(argp, &data.version,
814 sizeof(data.version)))
815 return -EFAULT;
816 }
817 return 0;
818
819 case CAPI_GET_SERIAL:
820 {
821 if (copy_from_user(&data.contr, argp,
822 sizeof(data.contr)))
823 return -EFAULT;
824 cdev->errcode = capi20_get_serial (data.contr, data.serial);
825 if (cdev->errcode)
826 return -EIO;
827 if (copy_to_user(argp, data.serial,
828 sizeof(data.serial)))
829 return -EFAULT;
830 }
831 return 0;
832 case CAPI_GET_PROFILE:
833 {
834 if (copy_from_user(&data.contr, argp,
835 sizeof(data.contr)))
836 return -EFAULT;
837
838 if (data.contr == 0) {
839 cdev->errcode = capi20_get_profile(data.contr, &data.profile);
840 if (cdev->errcode)
841 return -EIO;
842
843 retval = copy_to_user(argp,
844 &data.profile.ncontroller,
845 sizeof(data.profile.ncontroller));
846
847 } else {
848 cdev->errcode = capi20_get_profile(data.contr, &data.profile);
849 if (cdev->errcode)
850 return -EIO;
851
852 retval = copy_to_user(argp, &data.profile,
853 sizeof(data.profile));
854 }
855 if (retval)
856 return -EFAULT;
857 }
858 return 0;
859
860 case CAPI_GET_MANUFACTURER:
861 {
862 if (copy_from_user(&data.contr, argp,
863 sizeof(data.contr)))
864 return -EFAULT;
865 cdev->errcode = capi20_get_manufacturer(data.contr, data.manufacturer);
866 if (cdev->errcode)
867 return -EIO;
868
869 if (copy_to_user(argp, data.manufacturer,
870 sizeof(data.manufacturer)))
871 return -EFAULT;
872
873 }
874 return 0;
875 case CAPI_GET_ERRCODE:
876 data.errcode = cdev->errcode;
877 cdev->errcode = CAPI_NOERROR;
878 if (arg) {
879 if (copy_to_user(argp, &data.errcode,
880 sizeof(data.errcode)))
881 return -EFAULT;
882 }
883 return data.errcode;
884
885 case CAPI_INSTALLED:
886 if (capi20_isinstalled() == CAPI_NOERROR)
887 return 0;
888 return -ENXIO;
889
890 case CAPI_MANUFACTURER_CMD:
891 {
892 struct capi_manufacturer_cmd mcmd;
893 if (!capable(CAP_SYS_ADMIN))
894 return -EPERM;
895 if (copy_from_user(&mcmd, argp, sizeof(mcmd)))
896 return -EFAULT;
897 return capi20_manufacturer(mcmd.cmd, mcmd.data);
898 }
899 return 0;
900
901 case CAPI_SET_FLAGS:
902 case CAPI_CLR_FLAGS:
903 {
904 unsigned userflags;
905 if (copy_from_user(&userflags, argp,
906 sizeof(userflags)))
907 return -EFAULT;
908 if (cmd == CAPI_SET_FLAGS)
909 cdev->userflags |= userflags;
910 else
911 cdev->userflags &= ~userflags;
912 }
913 return 0;
914
915 case CAPI_GET_FLAGS:
916 if (copy_to_user(argp, &cdev->userflags,
917 sizeof(cdev->userflags)))
918 return -EFAULT;
919 return 0;
920
921 case CAPI_NCCI_OPENCOUNT:
922 {
923 struct capincci *nccip;
924 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
925 struct capiminor *mp;
926 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
927 unsigned ncci;
928 int count = 0;
929 if (copy_from_user(&ncci, argp, sizeof(ncci)))
930 return -EFAULT;
931
932 down(&cdev->ncci_list_sem);
933 if ((nccip = capincci_find(cdev, (u32) ncci)) == 0) {
934 up(&cdev->ncci_list_sem);
935 return 0;
936 }
937 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
938 if ((mp = nccip->minorp) != 0) {
939 count += atomic_read(&mp->ttyopencount);
940 }
941 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
942 up(&cdev->ncci_list_sem);
943 return count;
944 }
945 return 0;
946
947 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
948 case CAPI_NCCI_GETUNIT:
949 {
950 struct capincci *nccip;
951 struct capiminor *mp;
952 unsigned ncci;
953 int unit = 0;
954 if (copy_from_user(&ncci, argp,
955 sizeof(ncci)))
956 return -EFAULT;
957 down(&cdev->ncci_list_sem);
958 nccip = capincci_find(cdev, (u32) ncci);
959 if (!nccip || (mp = nccip->minorp) == 0) {
960 up(&cdev->ncci_list_sem);
961 return -ESRCH;
962 }
963 unit = mp->minor;
964 up(&cdev->ncci_list_sem);
965 return unit;
966 }
967 return 0;
968 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
969 }
970 return -EINVAL;
971 }
972
973 static int
974 capi_open(struct inode *inode, struct file *file)
975 {
976 if (file->private_data)
977 return -EEXIST;
978
979 if ((file->private_data = capidev_alloc()) == 0)
980 return -ENOMEM;
981
982 return nonseekable_open(inode, file);
983 }
984
985 static int
986 capi_release(struct inode *inode, struct file *file)
987 {
988 struct capidev *cdev = (struct capidev *)file->private_data;
989
990 capidev_free(cdev);
991 file->private_data = NULL;
992
993 return 0;
994 }
995
996 static struct file_operations capi_fops =
997 {
998 .owner = THIS_MODULE,
999 .llseek = no_llseek,
1000 .read = capi_read,
1001 .write = capi_write,
1002 .poll = capi_poll,
1003 .ioctl = capi_ioctl,
1004 .open = capi_open,
1005 .release = capi_release,
1006 };
1007
1008 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1009 /* -------- tty_operations for capincci ----------------------------- */
1010
1011 static int capinc_tty_open(struct tty_struct * tty, struct file * file)
1012 {
1013 struct capiminor *mp;
1014
1015 if ((mp = capiminor_find(iminor(file->f_dentry->d_inode))) == 0)
1016 return -ENXIO;
1017 if (mp->nccip == 0)
1018 return -ENXIO;
1019
1020 tty->driver_data = (void *)mp;
1021
1022 if (atomic_read(&mp->ttyopencount) == 0)
1023 mp->tty = tty;
1024 atomic_inc(&mp->ttyopencount);
1025 #ifdef _DEBUG_REFCOUNT
1026 printk(KERN_DEBUG "capinc_tty_open ocount=%d\n", atomic_read(&mp->ttyopencount));
1027 #endif
1028 handle_minor_recv(mp);
1029 return 0;
1030 }
1031
1032 static void capinc_tty_close(struct tty_struct * tty, struct file * file)
1033 {
1034 struct capiminor *mp;
1035
1036 mp = (struct capiminor *)tty->driver_data;
1037 if (mp) {
1038 if (atomic_dec_and_test(&mp->ttyopencount)) {
1039 #ifdef _DEBUG_REFCOUNT
1040 printk(KERN_DEBUG "capinc_tty_close lastclose\n");
1041 #endif
1042 tty->driver_data = NULL;
1043 mp->tty = NULL;
1044 }
1045 #ifdef _DEBUG_REFCOUNT
1046 printk(KERN_DEBUG "capinc_tty_close ocount=%d\n", atomic_read(&mp->ttyopencount));
1047 #endif
1048 if (mp->nccip == 0)
1049 capiminor_free(mp);
1050 }
1051
1052 #ifdef _DEBUG_REFCOUNT
1053 printk(KERN_DEBUG "capinc_tty_close\n");
1054 #endif
1055 }
1056
1057 static int capinc_tty_write(struct tty_struct * tty,
1058 const unsigned char *buf, int count)
1059 {
1060 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1061 struct sk_buff *skb;
1062
1063 #ifdef _DEBUG_TTYFUNCS
1064 printk(KERN_DEBUG "capinc_tty_write(count=%d)\n", count);
1065 #endif
1066
1067 if (!mp || !mp->nccip) {
1068 #ifdef _DEBUG_TTYFUNCS
1069 printk(KERN_DEBUG "capinc_tty_write: mp or mp->ncci NULL\n");
1070 #endif
1071 return 0;
1072 }
1073
1074 skb = mp->ttyskb;
1075 if (skb) {
1076 mp->ttyskb = NULL;
1077 skb_queue_tail(&mp->outqueue, skb);
1078 mp->outbytes += skb->len;
1079 }
1080
1081 skb = alloc_skb(CAPI_DATA_B3_REQ_LEN+count, GFP_ATOMIC);
1082 if (!skb) {
1083 printk(KERN_ERR "capinc_tty_write: alloc_skb failed\n");
1084 return -ENOMEM;
1085 }
1086
1087 skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
1088 memcpy(skb_put(skb, count), buf, count);
1089
1090 skb_queue_tail(&mp->outqueue, skb);
1091 mp->outbytes += skb->len;
1092 (void)handle_minor_send(mp);
1093 (void)handle_minor_recv(mp);
1094 return count;
1095 }
1096
1097 static void capinc_tty_put_char(struct tty_struct *tty, unsigned char ch)
1098 {
1099 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1100 struct sk_buff *skb;
1101
1102 #ifdef _DEBUG_TTYFUNCS
1103 printk(KERN_DEBUG "capinc_put_char(%u)\n", ch);
1104 #endif
1105
1106 if (!mp || !mp->nccip) {
1107 #ifdef _DEBUG_TTYFUNCS
1108 printk(KERN_DEBUG "capinc_tty_put_char: mp or mp->ncci NULL\n");
1109 #endif
1110 return;
1111 }
1112
1113 skb = mp->ttyskb;
1114 if (skb) {
1115 if (skb_tailroom(skb) > 0) {
1116 *(skb_put(skb, 1)) = ch;
1117 return;
1118 }
1119 mp->ttyskb = NULL;
1120 skb_queue_tail(&mp->outqueue, skb);
1121 mp->outbytes += skb->len;
1122 (void)handle_minor_send(mp);
1123 }
1124 skb = alloc_skb(CAPI_DATA_B3_REQ_LEN+CAPI_MAX_BLKSIZE, GFP_ATOMIC);
1125 if (skb) {
1126 skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
1127 *(skb_put(skb, 1)) = ch;
1128 mp->ttyskb = skb;
1129 } else {
1130 printk(KERN_ERR "capinc_put_char: char %u lost\n", ch);
1131 }
1132 }
1133
1134 static void capinc_tty_flush_chars(struct tty_struct *tty)
1135 {
1136 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1137 struct sk_buff *skb;
1138
1139 #ifdef _DEBUG_TTYFUNCS
1140 printk(KERN_DEBUG "capinc_tty_flush_chars\n");
1141 #endif
1142
1143 if (!mp || !mp->nccip) {
1144 #ifdef _DEBUG_TTYFUNCS
1145 printk(KERN_DEBUG "capinc_tty_flush_chars: mp or mp->ncci NULL\n");
1146 #endif
1147 return;
1148 }
1149
1150 skb = mp->ttyskb;
1151 if (skb) {
1152 mp->ttyskb = NULL;
1153 skb_queue_tail(&mp->outqueue, skb);
1154 mp->outbytes += skb->len;
1155 (void)handle_minor_send(mp);
1156 }
1157 (void)handle_minor_recv(mp);
1158 }
1159
1160 static int capinc_tty_write_room(struct tty_struct *tty)
1161 {
1162 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1163 int room;
1164 if (!mp || !mp->nccip) {
1165 #ifdef _DEBUG_TTYFUNCS
1166 printk(KERN_DEBUG "capinc_tty_write_room: mp or mp->ncci NULL\n");
1167 #endif
1168 return 0;
1169 }
1170 room = CAPINC_MAX_SENDQUEUE-skb_queue_len(&mp->outqueue);
1171 room *= CAPI_MAX_BLKSIZE;
1172 #ifdef _DEBUG_TTYFUNCS
1173 printk(KERN_DEBUG "capinc_tty_write_room = %d\n", room);
1174 #endif
1175 return room;
1176 }
1177
1178 static int capinc_tty_chars_in_buffer(struct tty_struct *tty)
1179 {
1180 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1181 if (!mp || !mp->nccip) {
1182 #ifdef _DEBUG_TTYFUNCS
1183 printk(KERN_DEBUG "capinc_tty_chars_in_buffer: mp or mp->ncci NULL\n");
1184 #endif
1185 return 0;
1186 }
1187 #ifdef _DEBUG_TTYFUNCS
1188 printk(KERN_DEBUG "capinc_tty_chars_in_buffer = %d nack=%d sq=%d rq=%d\n",
1189 mp->outbytes, mp->nack,
1190 skb_queue_len(&mp->outqueue),
1191 skb_queue_len(&mp->inqueue));
1192 #endif
1193 return mp->outbytes;
1194 }
1195
1196 static int capinc_tty_ioctl(struct tty_struct *tty, struct file * file,
1197 unsigned int cmd, unsigned long arg)
1198 {
1199 int error = 0;
1200 switch (cmd) {
1201 default:
1202 error = n_tty_ioctl (tty, file, cmd, arg);
1203 break;
1204 }
1205 return error;
1206 }
1207
1208 static void capinc_tty_set_termios(struct tty_struct *tty, struct termios * old)
1209 {
1210 #ifdef _DEBUG_TTYFUNCS
1211 printk(KERN_DEBUG "capinc_tty_set_termios\n");
1212 #endif
1213 }
1214
1215 static void capinc_tty_throttle(struct tty_struct * tty)
1216 {
1217 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1218 #ifdef _DEBUG_TTYFUNCS
1219 printk(KERN_DEBUG "capinc_tty_throttle\n");
1220 #endif
1221 if (mp)
1222 mp->ttyinstop = 1;
1223 }
1224
1225 static void capinc_tty_unthrottle(struct tty_struct * tty)
1226 {
1227 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1228 #ifdef _DEBUG_TTYFUNCS
1229 printk(KERN_DEBUG "capinc_tty_unthrottle\n");
1230 #endif
1231 if (mp) {
1232 mp->ttyinstop = 0;
1233 handle_minor_recv(mp);
1234 }
1235 }
1236
1237 static void capinc_tty_stop(struct tty_struct *tty)
1238 {
1239 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1240 #ifdef _DEBUG_TTYFUNCS
1241 printk(KERN_DEBUG "capinc_tty_stop\n");
1242 #endif
1243 if (mp) {
1244 mp->ttyoutstop = 1;
1245 }
1246 }
1247
1248 static void capinc_tty_start(struct tty_struct *tty)
1249 {
1250 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1251 #ifdef _DEBUG_TTYFUNCS
1252 printk(KERN_DEBUG "capinc_tty_start\n");
1253 #endif
1254 if (mp) {
1255 mp->ttyoutstop = 0;
1256 (void)handle_minor_send(mp);
1257 }
1258 }
1259
1260 static void capinc_tty_hangup(struct tty_struct *tty)
1261 {
1262 #ifdef _DEBUG_TTYFUNCS
1263 printk(KERN_DEBUG "capinc_tty_hangup\n");
1264 #endif
1265 }
1266
1267 static void capinc_tty_break_ctl(struct tty_struct *tty, int state)
1268 {
1269 #ifdef _DEBUG_TTYFUNCS
1270 printk(KERN_DEBUG "capinc_tty_break_ctl(%d)\n", state);
1271 #endif
1272 }
1273
1274 static void capinc_tty_flush_buffer(struct tty_struct *tty)
1275 {
1276 #ifdef _DEBUG_TTYFUNCS
1277 printk(KERN_DEBUG "capinc_tty_flush_buffer\n");
1278 #endif
1279 }
1280
1281 static void capinc_tty_set_ldisc(struct tty_struct *tty)
1282 {
1283 #ifdef _DEBUG_TTYFUNCS
1284 printk(KERN_DEBUG "capinc_tty_set_ldisc\n");
1285 #endif
1286 }
1287
1288 static void capinc_tty_send_xchar(struct tty_struct *tty, char ch)
1289 {
1290 #ifdef _DEBUG_TTYFUNCS
1291 printk(KERN_DEBUG "capinc_tty_send_xchar(%d)\n", ch);
1292 #endif
1293 }
1294
1295 static int capinc_tty_read_proc(char *page, char **start, off_t off,
1296 int count, int *eof, void *data)
1297 {
1298 return 0;
1299 }
1300
1301 static struct tty_driver *capinc_tty_driver;
1302
1303 static struct tty_operations capinc_ops = {
1304 .open = capinc_tty_open,
1305 .close = capinc_tty_close,
1306 .write = capinc_tty_write,
1307 .put_char = capinc_tty_put_char,
1308 .flush_chars = capinc_tty_flush_chars,
1309 .write_room = capinc_tty_write_room,
1310 .chars_in_buffer = capinc_tty_chars_in_buffer,
1311 .ioctl = capinc_tty_ioctl,
1312 .set_termios = capinc_tty_set_termios,
1313 .throttle = capinc_tty_throttle,
1314 .unthrottle = capinc_tty_unthrottle,
1315 .stop = capinc_tty_stop,
1316 .start = capinc_tty_start,
1317 .hangup = capinc_tty_hangup,
1318 .break_ctl = capinc_tty_break_ctl,
1319 .flush_buffer = capinc_tty_flush_buffer,
1320 .set_ldisc = capinc_tty_set_ldisc,
1321 .send_xchar = capinc_tty_send_xchar,
1322 .read_proc = capinc_tty_read_proc,
1323 };
1324
1325 static int capinc_tty_init(void)
1326 {
1327 struct tty_driver *drv;
1328
1329 if (capi_ttyminors > CAPINC_MAX_PORTS)
1330 capi_ttyminors = CAPINC_MAX_PORTS;
1331 if (capi_ttyminors <= 0)
1332 capi_ttyminors = CAPINC_NR_PORTS;
1333
1334 drv = alloc_tty_driver(capi_ttyminors);
1335 if (!drv)
1336 return -ENOMEM;
1337
1338 drv->owner = THIS_MODULE;
1339 drv->driver_name = "capi_nc";
1340 drv->devfs_name = "capi/";
1341 drv->name = "capi";
1342 drv->major = capi_ttymajor;
1343 drv->minor_start = 0;
1344 drv->type = TTY_DRIVER_TYPE_SERIAL;
1345 drv->subtype = SERIAL_TYPE_NORMAL;
1346 drv->init_termios = tty_std_termios;
1347 drv->init_termios.c_iflag = ICRNL;
1348 drv->init_termios.c_oflag = OPOST | ONLCR;
1349 drv->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1350 drv->init_termios.c_lflag = 0;
1351 drv->flags = TTY_DRIVER_REAL_RAW|TTY_DRIVER_RESET_TERMIOS;
1352 tty_set_operations(drv, &capinc_ops);
1353 if (tty_register_driver(drv)) {
1354 put_tty_driver(drv);
1355 printk(KERN_ERR "Couldn't register capi_nc driver\n");
1356 return -1;
1357 }
1358 capinc_tty_driver = drv;
1359 return 0;
1360 }
1361
1362 static void capinc_tty_exit(void)
1363 {
1364 struct tty_driver *drv = capinc_tty_driver;
1365 int retval;
1366 if ((retval = tty_unregister_driver(drv)))
1367 printk(KERN_ERR "capi: failed to unregister capi_nc driver (%d)\n", retval);
1368 put_tty_driver(drv);
1369 }
1370
1371 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
1372
1373 /* -------- /proc functions ----------------------------------------- */
1374
1375 /*
1376 * /proc/capi/capi20:
1377 * minor applid nrecvctlpkt nrecvdatapkt nsendctlpkt nsenddatapkt
1378 */
1379 static int proc_capidev_read_proc(char *page, char **start, off_t off,
1380 int count, int *eof, void *data)
1381 {
1382 struct capidev *cdev;
1383 struct list_head *l;
1384 int len = 0;
1385
1386 read_lock(&capidev_list_lock);
1387 list_for_each(l, &capidev_list) {
1388 cdev = list_entry(l, struct capidev, list);
1389 len += sprintf(page+len, "0 %d %lu %lu %lu %lu\n",
1390 cdev->ap.applid,
1391 cdev->ap.nrecvctlpkt,
1392 cdev->ap.nrecvdatapkt,
1393 cdev->ap.nsentctlpkt,
1394 cdev->ap.nsentdatapkt);
1395 if (len <= off) {
1396 off -= len;
1397 len = 0;
1398 } else {
1399 if (len-off > count)
1400 goto endloop;
1401 }
1402 }
1403
1404 endloop:
1405 read_unlock(&capidev_list_lock);
1406 if (len < count)
1407 *eof = 1;
1408 if (len > count) len = count;
1409 if (len < 0) len = 0;
1410 return len;
1411 }
1412
1413 /*
1414 * /proc/capi/capi20ncci:
1415 * applid ncci
1416 */
1417 static int proc_capincci_read_proc(char *page, char **start, off_t off,
1418 int count, int *eof, void *data)
1419 {
1420 struct capidev *cdev;
1421 struct capincci *np;
1422 struct list_head *l;
1423 int len = 0;
1424
1425 read_lock(&capidev_list_lock);
1426 list_for_each(l, &capidev_list) {
1427 cdev = list_entry(l, struct capidev, list);
1428 for (np=cdev->nccis; np; np = np->next) {
1429 len += sprintf(page+len, "%d 0x%x\n",
1430 cdev->ap.applid,
1431 np->ncci);
1432 if (len <= off) {
1433 off -= len;
1434 len = 0;
1435 } else {
1436 if (len-off > count)
1437 goto endloop;
1438 }
1439 }
1440 }
1441 endloop:
1442 read_unlock(&capidev_list_lock);
1443 *start = page+off;
1444 if (len < count)
1445 *eof = 1;
1446 if (len>count) len = count;
1447 if (len<0) len = 0;
1448 return len;
1449 }
1450
1451 static struct procfsentries {
1452 char *name;
1453 mode_t mode;
1454 int (*read_proc)(char *page, char **start, off_t off,
1455 int count, int *eof, void *data);
1456 struct proc_dir_entry *procent;
1457 } procfsentries[] = {
1458 /* { "capi", S_IFDIR, 0 }, */
1459 { "capi/capi20", 0 , proc_capidev_read_proc },
1460 { "capi/capi20ncci", 0 , proc_capincci_read_proc },
1461 };
1462
1463 static void __init proc_init(void)
1464 {
1465 int nelem = sizeof(procfsentries)/sizeof(procfsentries[0]);
1466 int i;
1467
1468 for (i=0; i < nelem; i++) {
1469 struct procfsentries *p = procfsentries + i;
1470 p->procent = create_proc_entry(p->name, p->mode, NULL);
1471 if (p->procent) p->procent->read_proc = p->read_proc;
1472 }
1473 }
1474
1475 static void __exit proc_exit(void)
1476 {
1477 int nelem = sizeof(procfsentries)/sizeof(procfsentries[0]);
1478 int i;
1479
1480 for (i=nelem-1; i >= 0; i--) {
1481 struct procfsentries *p = procfsentries + i;
1482 if (p->procent) {
1483 remove_proc_entry(p->name, NULL);
1484 p->procent = NULL;
1485 }
1486 }
1487 }
1488
1489 /* -------- init function and module interface ---------------------- */
1490
1491
1492 static char rev[32];
1493
1494 static int __init capi_init(void)
1495 {
1496 char *p;
1497 char *compileinfo;
1498 int major_ret;
1499
1500 if ((p = strchr(revision, ':')) != 0 && p[1]) {
1501 strlcpy(rev, p + 2, sizeof(rev));
1502 if ((p = strchr(rev, '$')) != 0 && p > rev)
1503 *(p-1) = 0;
1504 } else
1505 strcpy(rev, "1.0");
1506
1507 major_ret = register_chrdev(capi_major, "capi20", &capi_fops);
1508 if (major_ret < 0) {
1509 printk(KERN_ERR "capi20: unable to get major %d\n", capi_major);
1510 return major_ret;
1511 }
1512 capi_class = class_create(THIS_MODULE, "capi");
1513 if (IS_ERR(capi_class)) {
1514 unregister_chrdev(capi_major, "capi20");
1515 return PTR_ERR(capi_class);
1516 }
1517
1518 class_device_create(capi_class, NULL, MKDEV(capi_major, 0), NULL, "capi");
1519
1520 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1521 if (capinc_tty_init() < 0) {
1522 class_device_destroy(capi_class, MKDEV(capi_major, 0));
1523 class_destroy(capi_class);
1524 unregister_chrdev(capi_major, "capi20");
1525 return -ENOMEM;
1526 }
1527 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
1528
1529 proc_init();
1530
1531 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1532 #if defined(CONFIG_ISDN_CAPI_CAPIFS) || defined(CONFIG_ISDN_CAPI_CAPIFS_MODULE)
1533 compileinfo = " (middleware+capifs)";
1534 #else
1535 compileinfo = " (no capifs)";
1536 #endif
1537 #else
1538 compileinfo = " (no middleware)";
1539 #endif
1540 printk(KERN_NOTICE "capi20: Rev %s: started up with major %d%s\n",
1541 rev, capi_major, compileinfo);
1542
1543 return 0;
1544 }
1545
1546 static void __exit capi_exit(void)
1547 {
1548 proc_exit();
1549
1550 class_device_destroy(capi_class, MKDEV(capi_major, 0));
1551 class_destroy(capi_class);
1552 unregister_chrdev(capi_major, "capi20");
1553
1554 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1555 capinc_tty_exit();
1556 #endif
1557 printk(KERN_NOTICE "capi: Rev %s: unloaded\n", rev);
1558 }
1559
1560 module_init(capi_init);
1561 module_exit(capi_exit);