blob: c22b34976c240142a6a9df45aeea8aeb88dd7472 [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>
Jonathan Corbeta237f3b2008-05-16 14:15:33 -060023#include <linux/smp_lock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/timer.h>
25#include <linux/wait.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/tty.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/netdevice.h>
28#include <linux/ppp_defs.h>
29#include <linux/if_ppp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/skbuff.h>
31#include <linux/proc_fs.h>
Alexey Dobriyan9a58a802010-01-14 03:10:54 -080032#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/poll.h>
34#include <linux/capi.h>
35#include <linux/kernelcapi.h>
36#include <linux/init.h>
37#include <linux/device.h>
38#include <linux/moduleparam.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/isdn/capiutil.h>
40#include <linux/isdn/capicmd.h>
Jan Kiszka90926f02010-02-08 10:12:06 +000041
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include "capifs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Linus Torvalds1da177e2005-04-16 15:20:36 -070044MODULE_DESCRIPTION("CAPI4Linux: Userspace /dev/capi20 interface");
45MODULE_AUTHOR("Carsten Paeth");
46MODULE_LICENSE("GPL");
47
48#undef _DEBUG_REFCOUNT /* alloc/free and open/close debug */
49#undef _DEBUG_TTYFUNCS /* call to tty_driver */
50#undef _DEBUG_DATAFLOW /* data flow */
51
52/* -------- driver information -------------------------------------- */
53
gregkh@suse.de56b22932005-03-23 10:01:41 -080054static struct class *capi_class;
Adrian Bunk408b6642005-05-01 08:59:29 -070055static int capi_major = 68; /* allocated */
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57module_param_named(major, capi_major, uint, 0);
Jan Kiszka501c87a2010-02-08 10:12:16 +000058
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
Jan Kiszka501c87a2010-02-08 10:12:16 +000060#define CAPINC_NR_PORTS 32
61#define CAPINC_MAX_PORTS 256
62
63static int capi_ttymajor = 191;
64static int capi_ttyminors = CAPINC_NR_PORTS;
65
Linus Torvalds1da177e2005-04-16 15:20:36 -070066module_param_named(ttymajor, capi_ttymajor, uint, 0);
67module_param_named(ttyminors, capi_ttyminors, uint, 0);
68#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
69
70/* -------- defines ------------------------------------------------- */
71
72#define CAPINC_MAX_RECVQUEUE 10
73#define CAPINC_MAX_SENDQUEUE 10
74#define CAPI_MAX_BLKSIZE 2048
75
76/* -------- data structures ----------------------------------------- */
77
78struct capidev;
79struct capincci;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080struct capiminor;
81
Michael Buesch6aa65472006-06-26 00:25:30 -070082struct datahandle_queue {
83 struct list_head list;
84 u16 datahandle;
85};
86
Linus Torvalds1da177e2005-04-16 15:20:36 -070087struct capiminor {
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 struct capincci *nccip;
89 unsigned int minor;
Jan Kiszka90926f02010-02-08 10:12:06 +000090 struct dentry *capifs_dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92 struct capi20_appl *ap;
93 u32 ncci;
94 u16 datahandle;
95 u16 msgid;
96
97 struct tty_struct *tty;
98 int ttyinstop;
99 int ttyoutstop;
100 struct sk_buff *ttyskb;
101 atomic_t ttyopencount;
102
103 struct sk_buff_head inqueue;
104 int inbytes;
105 struct sk_buff_head outqueue;
106 int outbytes;
107
108 /* transmit path */
Michael Buesch6aa65472006-06-26 00:25:30 -0700109 struct list_head ackqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 int nack;
Michael Buesch6aa65472006-06-26 00:25:30 -0700111 spinlock_t ackqlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Michael Buesch053b47f2007-02-12 00:53:26 -0800114/* FIXME: The following lock is a sledgehammer-workaround to a
115 * locking issue with the capiminor (and maybe other) data structure(s).
116 * Access to this data is done in a racy way and crashes the machine with
117 * a FritzCard DSL driver; sooner or later. This is a workaround
118 * which trades scalability vs stability, so it doesn't crash the kernel anymore.
119 * The correct (and scalable) fix for the issue seems to require
120 * an API change to the drivers... . */
121static DEFINE_SPINLOCK(workaround_lock);
122
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123struct capincci {
Jan Kiszka884f5c42010-02-08 10:12:22 +0000124 struct list_head list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 u32 ncci;
126 struct capidev *cdev;
127#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
128 struct capiminor *minorp;
129#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
130};
131
132struct 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
Jan Kiszka884f5c42010-02-08 10:12:22 +0000141 struct list_head nccis;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Jan Kiszka05b41492010-02-08 10:12:19 +0000143 struct mutex lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144};
145
146/* -------- global variables ---------------------------------------- */
147
Jan Kiszkab8f433d2010-02-08 10:12:17 +0000148static DEFINE_MUTEX(capidev_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149static LIST_HEAD(capidev_list);
150
151#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
Jan Kiszka501c87a2010-02-08 10:12:16 +0000152
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000153static DEFINE_RWLOCK(capiminors_lock);
154static struct capiminor **capiminors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156/* -------- datahandles --------------------------------------------- */
157
Jan Kiszka501c87a2010-02-08 10:12:16 +0000158static int capiminor_add_ack(struct capiminor *mp, u16 datahandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159{
Michael Buesch6aa65472006-06-26 00:25:30 -0700160 struct datahandle_queue *n;
161 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
163 n = kmalloc(sizeof(*n), GFP_ATOMIC);
Michael Buesch6aa65472006-06-26 00:25:30 -0700164 if (unlikely(!n)) {
165 printk(KERN_ERR "capi: alloc datahandle failed\n");
166 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 n->datahandle = datahandle;
Michael Buesch6aa65472006-06-26 00:25:30 -0700169 INIT_LIST_HEAD(&n->list);
170 spin_lock_irqsave(&mp->ackqlock, flags);
171 list_add_tail(&n->list, &mp->ackqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 mp->nack++;
Michael Buesch6aa65472006-06-26 00:25:30 -0700173 spin_unlock_irqrestore(&mp->ackqlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 return 0;
175}
176
177static int capiminor_del_ack(struct capiminor *mp, u16 datahandle)
178{
Michael Buesch6aa65472006-06-26 00:25:30 -0700179 struct datahandle_queue *p, *tmp;
180 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
Michael Buesch6aa65472006-06-26 00:25:30 -0700182 spin_lock_irqsave(&mp->ackqlock, flags);
183 list_for_each_entry_safe(p, tmp, &mp->ackqueue, list) {
184 if (p->datahandle == datahandle) {
185 list_del(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 kfree(p);
187 mp->nack--;
Michael Buesch6aa65472006-06-26 00:25:30 -0700188 spin_unlock_irqrestore(&mp->ackqlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 return 0;
190 }
191 }
Michael Buesch6aa65472006-06-26 00:25:30 -0700192 spin_unlock_irqrestore(&mp->ackqlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 return -1;
194}
195
196static void capiminor_del_all_ack(struct capiminor *mp)
197{
Michael Buesch6aa65472006-06-26 00:25:30 -0700198 struct datahandle_queue *p, *tmp;
199 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Michael Buesch6aa65472006-06-26 00:25:30 -0700201 spin_lock_irqsave(&mp->ackqlock, flags);
202 list_for_each_entry_safe(p, tmp, &mp->ackqueue, list) {
203 list_del(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 kfree(p);
205 mp->nack--;
206 }
Michael Buesch6aa65472006-06-26 00:25:30 -0700207 spin_unlock_irqrestore(&mp->ackqlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208}
209
210
211/* -------- struct capiminor ---------------------------------------- */
212
213static struct capiminor *capiminor_alloc(struct capi20_appl *ap, u32 ncci)
214{
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000215 struct capiminor *mp;
216 unsigned int minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 unsigned long flags;
218
Jan Kiszka54f0fad2010-02-08 10:12:20 +0000219 mp = kzalloc(sizeof(*mp), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 if (!mp) {
221 printk(KERN_ERR "capi: can't alloc capiminor\n");
222 return NULL;
223 }
224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 mp->ap = ap;
226 mp->ncci = ncci;
227 mp->msgid = 0;
228 atomic_set(&mp->ttyopencount,0);
Michael Buesch6aa65472006-06-26 00:25:30 -0700229 INIT_LIST_HEAD(&mp->ackqueue);
230 spin_lock_init(&mp->ackqlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
232 skb_queue_head_init(&mp->inqueue);
233 skb_queue_head_init(&mp->outqueue);
234
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000235 /* Allocate the least unused minor number. */
236 write_lock_irqsave(&capiminors_lock, flags);
237 for (minor = 0; minor < capi_ttyminors; minor++)
238 if (!capiminors[minor]) {
239 capiminors[minor] = mp;
240 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 }
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000242 write_unlock_irqrestore(&capiminors_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000244 if (minor == capi_ttyminors) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 printk(KERN_NOTICE "capi: out of minors\n");
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000246 kfree(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 return NULL;
248 }
249
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000250 mp->minor = minor;
251
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 return mp;
253}
254
255static void capiminor_free(struct capiminor *mp)
256{
257 unsigned long flags;
258
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000259 write_lock_irqsave(&capiminors_lock, flags);
260 capiminors[mp->minor] = NULL;
261 write_unlock_irqrestore(&capiminors_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
Wei Yongjund46604e2009-02-25 00:12:09 +0000263 kfree_skb(mp->ttyskb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 mp->ttyskb = NULL;
265 skb_queue_purge(&mp->inqueue);
266 skb_queue_purge(&mp->outqueue);
267 capiminor_del_all_ack(mp);
268 kfree(mp);
269}
270
Adrian Bunk408b6642005-05-01 08:59:29 -0700271static struct capiminor *capiminor_find(unsigned int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272{
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000273 struct capiminor *mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000275 if (minor >= capi_ttyminors)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 return NULL;
277
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000278 read_lock(&capiminors_lock);
279 mp = capiminors[minor];
280 read_unlock(&capiminors_lock);
281
282 return mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
285/* -------- struct capincci ----------------------------------------- */
286
Jan Kiszka501c87a2010-02-08 10:12:16 +0000287static void capincci_alloc_minor(struct capidev *cdev, struct capincci *np)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288{
Jan Kiszka501c87a2010-02-08 10:12:16 +0000289 struct capiminor *mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
Jan Kiszka501c87a2010-02-08 10:12:16 +0000291 if (!(cdev->userflags & CAPIFLAG_HIGHJACKING))
292 return;
293
294 mp = np->minorp = capiminor_alloc(&cdev->ap, np->ncci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 if (mp) {
296 mp->nccip = np;
297#ifdef _DEBUG_REFCOUNT
298 printk(KERN_DEBUG "set mp->nccip\n");
299#endif
Jan Kiszka90926f02010-02-08 10:12:06 +0000300 mp->capifs_dentry =
301 capifs_new_ncci(mp->minor,
302 MKDEV(capi_ttymajor, mp->minor));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 }
Jan Kiszka501c87a2010-02-08 10:12:16 +0000304}
305
306static void capincci_free_minor(struct capincci *np)
307{
308 struct capiminor *mp = np->minorp;
309
310 if (mp) {
311 capifs_free_ncci(mp->capifs_dentry);
312 if (mp->tty) {
313 mp->nccip = NULL;
314#ifdef _DEBUG_REFCOUNT
315 printk(KERN_DEBUG "reset mp->nccip\n");
316#endif
317 tty_hangup(mp->tty);
318 } else {
319 capiminor_free(mp);
320 }
321 }
322}
323
324static inline unsigned int capincci_minor_opencount(struct capincci *np)
325{
326 struct capiminor *mp = np->minorp;
327
328 return mp ? atomic_read(&mp->ttyopencount) : 0;
329}
330
331#else /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
332
333static inline void
334capincci_alloc_minor(struct capidev *cdev, struct capincci *np) { }
335static inline void capincci_free_minor(struct capincci *np) { }
336
337static inline unsigned int capincci_minor_opencount(struct capincci *np)
338{
339 return 0;
340}
341
342#endif /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
343
344static struct capincci *capincci_alloc(struct capidev *cdev, u32 ncci)
345{
Jan Kiszka884f5c42010-02-08 10:12:22 +0000346 struct capincci *np;
Jan Kiszka501c87a2010-02-08 10:12:16 +0000347
Jan Kiszka54f0fad2010-02-08 10:12:20 +0000348 np = kzalloc(sizeof(*np), GFP_KERNEL);
Jan Kiszka501c87a2010-02-08 10:12:16 +0000349 if (!np)
350 return NULL;
351 np->ncci = ncci;
352 np->cdev = cdev;
353
354 capincci_alloc_minor(cdev, np);
355
Jan Kiszka884f5c42010-02-08 10:12:22 +0000356 list_add_tail(&np->list, &cdev->nccis);
357
358 return np;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359}
360
361static void capincci_free(struct capidev *cdev, u32 ncci)
362{
Jan Kiszka884f5c42010-02-08 10:12:22 +0000363 struct capincci *np, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
Jan Kiszka884f5c42010-02-08 10:12:22 +0000365 list_for_each_entry_safe(np, tmp, &cdev->nccis, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 if (ncci == 0xffffffff || np->ncci == ncci) {
Jan Kiszka501c87a2010-02-08 10:12:16 +0000367 capincci_free_minor(np);
Jan Kiszka884f5c42010-02-08 10:12:22 +0000368 list_del(&np->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 kfree(np);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371}
372
373static struct capincci *capincci_find(struct capidev *cdev, u32 ncci)
374{
Jan Kiszka884f5c42010-02-08 10:12:22 +0000375 struct capincci *np;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Jan Kiszka884f5c42010-02-08 10:12:22 +0000377 list_for_each_entry(np, &cdev->nccis, list)
378 if (np->ncci == ncci)
379 return np;
380 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381}
382
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
384/* -------- handle data queue --------------------------------------- */
385
386static struct sk_buff *
387gen_data_b3_resp_for(struct capiminor *mp, struct sk_buff *skb)
388{
389 struct sk_buff *nskb;
390 nskb = alloc_skb(CAPI_DATA_B3_RESP_LEN, GFP_ATOMIC);
391 if (nskb) {
392 u16 datahandle = CAPIMSG_U16(skb->data,CAPIMSG_BASELEN+4+4+2);
393 unsigned char *s = skb_put(nskb, CAPI_DATA_B3_RESP_LEN);
394 capimsg_setu16(s, 0, CAPI_DATA_B3_RESP_LEN);
395 capimsg_setu16(s, 2, mp->ap->applid);
396 capimsg_setu8 (s, 4, CAPI_DATA_B3);
397 capimsg_setu8 (s, 5, CAPI_RESP);
398 capimsg_setu16(s, 6, mp->msgid++);
399 capimsg_setu32(s, 8, mp->ncci);
400 capimsg_setu16(s, 12, datahandle);
401 }
402 return nskb;
403}
404
405static int handle_recv_skb(struct capiminor *mp, struct sk_buff *skb)
406{
407 struct sk_buff *nskb;
408 int datalen;
409 u16 errcode, datahandle;
410 struct tty_ldisc *ld;
411
412 datalen = skb->len - CAPIMSG_LEN(skb->data);
413 if (mp->tty == NULL)
414 {
415#ifdef _DEBUG_DATAFLOW
416 printk(KERN_DEBUG "capi: currently no receiver\n");
417#endif
418 return -1;
419 }
420
421 ld = tty_ldisc_ref(mp->tty);
422 if (ld == NULL)
423 return -1;
Alan Coxa352def2008-07-16 21:53:12 +0100424 if (ld->ops->receive_buf == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425#if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
426 printk(KERN_DEBUG "capi: ldisc has no receive_buf function\n");
427#endif
428 goto bad;
429 }
430 if (mp->ttyinstop) {
431#if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
432 printk(KERN_DEBUG "capi: recv tty throttled\n");
433#endif
434 goto bad;
435 }
Alan Cox33f0f882006-01-09 20:54:13 -0800436 if (mp->tty->receive_room < datalen) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437#if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
438 printk(KERN_DEBUG "capi: no room in tty\n");
439#endif
440 goto bad;
441 }
Harvey Harrison2f9e9b62008-04-28 02:14:37 -0700442 if ((nskb = gen_data_b3_resp_for(mp, skb)) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 printk(KERN_ERR "capi: gen_data_b3_resp failed\n");
444 goto bad;
445 }
446 datahandle = CAPIMSG_U16(skb->data,CAPIMSG_BASELEN+4);
447 errcode = capi20_put_message(mp->ap, nskb);
448 if (errcode != CAPI_NOERROR) {
449 printk(KERN_ERR "capi: send DATA_B3_RESP failed=%x\n",
450 errcode);
451 kfree_skb(nskb);
452 goto bad;
453 }
454 (void)skb_pull(skb, CAPIMSG_LEN(skb->data));
455#ifdef _DEBUG_DATAFLOW
456 printk(KERN_DEBUG "capi: DATA_B3_RESP %u len=%d => ldisc\n",
457 datahandle, skb->len);
458#endif
Alan Coxa352def2008-07-16 21:53:12 +0100459 ld->ops->receive_buf(mp->tty, skb->data, NULL, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 kfree_skb(skb);
461 tty_ldisc_deref(ld);
462 return 0;
463bad:
464 tty_ldisc_deref(ld);
465 return -1;
466}
467
468static void handle_minor_recv(struct capiminor *mp)
469{
470 struct sk_buff *skb;
Harvey Harrison2f9e9b62008-04-28 02:14:37 -0700471 while ((skb = skb_dequeue(&mp->inqueue)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 unsigned int len = skb->len;
473 mp->inbytes -= len;
474 if (handle_recv_skb(mp, skb) < 0) {
475 skb_queue_head(&mp->inqueue, skb);
476 mp->inbytes += len;
477 return;
478 }
479 }
480}
481
482static int handle_minor_send(struct capiminor *mp)
483{
484 struct sk_buff *skb;
485 u16 len;
486 int count = 0;
487 u16 errcode;
488 u16 datahandle;
489
490 if (mp->tty && mp->ttyoutstop) {
491#if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
492 printk(KERN_DEBUG "capi: send: tty stopped\n");
493#endif
494 return 0;
495 }
496
Harvey Harrison2f9e9b62008-04-28 02:14:37 -0700497 while ((skb = skb_dequeue(&mp->outqueue)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 datahandle = mp->datahandle;
499 len = (u16)skb->len;
500 skb_push(skb, CAPI_DATA_B3_REQ_LEN);
501 memset(skb->data, 0, CAPI_DATA_B3_REQ_LEN);
502 capimsg_setu16(skb->data, 0, CAPI_DATA_B3_REQ_LEN);
503 capimsg_setu16(skb->data, 2, mp->ap->applid);
504 capimsg_setu8 (skb->data, 4, CAPI_DATA_B3);
505 capimsg_setu8 (skb->data, 5, CAPI_REQ);
506 capimsg_setu16(skb->data, 6, mp->msgid++);
507 capimsg_setu32(skb->data, 8, mp->ncci); /* NCCI */
Andrew Morton5e6c20a2007-07-17 04:04:22 -0700508 capimsg_setu32(skb->data, 12, (u32)(long)skb->data);/* Data32 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 capimsg_setu16(skb->data, 16, len); /* Data length */
510 capimsg_setu16(skb->data, 18, datahandle);
511 capimsg_setu16(skb->data, 20, 0); /* Flags */
512
Jan Kiszka501c87a2010-02-08 10:12:16 +0000513 if (capiminor_add_ack(mp, datahandle) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 skb_pull(skb, CAPI_DATA_B3_REQ_LEN);
515 skb_queue_head(&mp->outqueue, skb);
516 return count;
517 }
518 errcode = capi20_put_message(mp->ap, skb);
519 if (errcode == CAPI_NOERROR) {
520 mp->datahandle++;
521 count++;
522 mp->outbytes -= len;
523#ifdef _DEBUG_DATAFLOW
524 printk(KERN_DEBUG "capi: DATA_B3_REQ %u len=%u\n",
525 datahandle, len);
526#endif
527 continue;
528 }
529 capiminor_del_ack(mp, datahandle);
530
531 if (errcode == CAPI_SENDQUEUEFULL) {
532 skb_pull(skb, CAPI_DATA_B3_REQ_LEN);
533 skb_queue_head(&mp->outqueue, skb);
534 break;
535 }
536
537 /* ups, drop packet */
538 printk(KERN_ERR "capi: put_message = %x\n", errcode);
539 mp->outbytes -= len;
540 kfree_skb(skb);
541 }
542 return count;
543}
544
545#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
546/* -------- function called by lower level -------------------------- */
547
548static void capi_recv_message(struct capi20_appl *ap, struct sk_buff *skb)
549{
550 struct capidev *cdev = ap->private;
551#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
552 struct capiminor *mp;
553 u16 datahandle;
554#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
555 struct capincci *np;
Michael Buesch053b47f2007-02-12 00:53:26 -0800556 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
Jan Kiszka05b41492010-02-08 10:12:19 +0000558 mutex_lock(&cdev->lock);
559
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_CONF) {
561 u16 info = CAPIMSG_U16(skb->data, 12); // Info field
Jan Kiszka05b41492010-02-08 10:12:19 +0000562 if ((info & 0xff00) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 }
Jan Kiszka05b41492010-02-08 10:12:19 +0000565 if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_IND)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
Jan Kiszka05b41492010-02-08 10:12:19 +0000567
Michael Buesch053b47f2007-02-12 00:53:26 -0800568 spin_lock_irqsave(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 if (CAPIMSG_COMMAND(skb->data) != CAPI_DATA_B3) {
570 skb_queue_tail(&cdev->recvqueue, skb);
571 wake_up_interruptible(&cdev->recvwait);
Jan Kiszka05b41492010-02-08 10:12:19 +0000572 goto unlock_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 }
Jan Kiszka05b41492010-02-08 10:12:19 +0000574
575 np = capincci_find(cdev, CAPIMSG_CONTROL(skb->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 if (!np) {
577 printk(KERN_ERR "BUG: capi_signal: ncci not found\n");
578 skb_queue_tail(&cdev->recvqueue, skb);
579 wake_up_interruptible(&cdev->recvwait);
Jan Kiszka05b41492010-02-08 10:12:19 +0000580 goto unlock_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 }
Jan Kiszka501c87a2010-02-08 10:12:16 +0000582
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583#ifndef CONFIG_ISDN_CAPI_MIDDLEWARE
584 skb_queue_tail(&cdev->recvqueue, skb);
585 wake_up_interruptible(&cdev->recvwait);
Jan Kiszka501c87a2010-02-08 10:12:16 +0000586
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587#else /* CONFIG_ISDN_CAPI_MIDDLEWARE */
Jan Kiszka501c87a2010-02-08 10:12:16 +0000588
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 mp = np->minorp;
590 if (!mp) {
591 skb_queue_tail(&cdev->recvqueue, skb);
592 wake_up_interruptible(&cdev->recvwait);
Jan Kiszka05b41492010-02-08 10:12:19 +0000593 goto unlock_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_IND) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4+4+2);
597#ifdef _DEBUG_DATAFLOW
598 printk(KERN_DEBUG "capi_signal: DATA_B3_IND %u len=%d\n",
599 datahandle, skb->len-CAPIMSG_LEN(skb->data));
600#endif
601 skb_queue_tail(&mp->inqueue, skb);
602 mp->inbytes += skb->len;
603 handle_minor_recv(mp);
604
605 } else if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_CONF) {
606
607 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4);
608#ifdef _DEBUG_DATAFLOW
609 printk(KERN_DEBUG "capi_signal: DATA_B3_CONF %u 0x%x\n",
610 datahandle,
611 CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4+2));
612#endif
613 kfree_skb(skb);
614 (void)capiminor_del_ack(mp, datahandle);
615 if (mp->tty)
616 tty_wakeup(mp->tty);
617 (void)handle_minor_send(mp);
618
619 } else {
620 /* ups, let capi application handle it :-) */
621 skb_queue_tail(&cdev->recvqueue, skb);
622 wake_up_interruptible(&cdev->recvwait);
623 }
624#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
Jan Kiszka501c87a2010-02-08 10:12:16 +0000625
Jan Kiszka05b41492010-02-08 10:12:19 +0000626unlock_out:
Michael Buesch053b47f2007-02-12 00:53:26 -0800627 spin_unlock_irqrestore(&workaround_lock, flags);
Jan Kiszka05b41492010-02-08 10:12:19 +0000628 mutex_unlock(&cdev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629}
630
631/* -------- file_operations for capidev ----------------------------- */
632
633static ssize_t
634capi_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
635{
636 struct capidev *cdev = (struct capidev *)file->private_data;
637 struct sk_buff *skb;
638 size_t copied;
Jan Kiszka28a1dbb2010-02-08 10:12:21 +0000639 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
641 if (!cdev->ap.applid)
642 return -ENODEV;
643
Jan Kiszka28a1dbb2010-02-08 10:12:21 +0000644 skb = skb_dequeue(&cdev->recvqueue);
645 if (!skb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 if (file->f_flags & O_NONBLOCK)
647 return -EAGAIN;
Jan Kiszka28a1dbb2010-02-08 10:12:21 +0000648 err = wait_event_interruptible(cdev->recvwait,
649 (skb = skb_dequeue(&cdev->recvqueue)));
650 if (err)
651 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 }
653 if (skb->len > count) {
654 skb_queue_head(&cdev->recvqueue, skb);
655 return -EMSGSIZE;
656 }
657 if (copy_to_user(buf, skb->data, skb->len)) {
658 skb_queue_head(&cdev->recvqueue, skb);
659 return -EFAULT;
660 }
661 copied = skb->len;
662
663 kfree_skb(skb);
664
665 return copied;
666}
667
668static ssize_t
669capi_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
670{
671 struct capidev *cdev = (struct capidev *)file->private_data;
672 struct sk_buff *skb;
673 u16 mlen;
674
675 if (!cdev->ap.applid)
676 return -ENODEV;
677
678 skb = alloc_skb(count, GFP_USER);
679 if (!skb)
680 return -ENOMEM;
681
682 if (copy_from_user(skb_put(skb, count), buf, count)) {
683 kfree_skb(skb);
684 return -EFAULT;
685 }
686 mlen = CAPIMSG_LEN(skb->data);
687 if (CAPIMSG_CMD(skb->data) == CAPI_DATA_B3_REQ) {
688 if ((size_t)(mlen + CAPIMSG_DATALEN(skb->data)) != count) {
689 kfree_skb(skb);
690 return -EINVAL;
691 }
692 } else {
693 if (mlen != count) {
694 kfree_skb(skb);
695 return -EINVAL;
696 }
697 }
698 CAPIMSG_SETAPPID(skb->data, cdev->ap.applid);
699
700 if (CAPIMSG_CMD(skb->data) == CAPI_DISCONNECT_B3_RESP) {
Jan Kiszka05b41492010-02-08 10:12:19 +0000701 mutex_lock(&cdev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 capincci_free(cdev, CAPIMSG_NCCI(skb->data));
Jan Kiszka05b41492010-02-08 10:12:19 +0000703 mutex_unlock(&cdev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 }
705
706 cdev->errcode = capi20_put_message(&cdev->ap, skb);
707
708 if (cdev->errcode) {
709 kfree_skb(skb);
710 return -EIO;
711 }
712 return count;
713}
714
715static unsigned int
716capi_poll(struct file *file, poll_table * wait)
717{
718 struct capidev *cdev = (struct capidev *)file->private_data;
719 unsigned int mask = 0;
720
721 if (!cdev->ap.applid)
722 return POLLERR;
723
724 poll_wait(file, &(cdev->recvwait), wait);
725 mask = POLLOUT | POLLWRNORM;
726 if (!skb_queue_empty(&cdev->recvqueue))
727 mask |= POLLIN | POLLRDNORM;
728 return mask;
729}
730
731static int
732capi_ioctl(struct inode *inode, struct file *file,
733 unsigned int cmd, unsigned long arg)
734{
735 struct capidev *cdev = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 capi_ioctl_struct data;
737 int retval = -EINVAL;
738 void __user *argp = (void __user *)arg;
739
740 switch (cmd) {
741 case CAPI_REGISTER:
Jan Kiszka05b41492010-02-08 10:12:19 +0000742 mutex_lock(&cdev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
Jan Kiszka05b41492010-02-08 10:12:19 +0000744 if (cdev->ap.applid) {
745 retval = -EEXIST;
746 goto register_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 }
Jan Kiszka05b41492010-02-08 10:12:19 +0000748 if (copy_from_user(&cdev->ap.rparam, argp,
749 sizeof(struct capi_register_params))) {
750 retval = -EFAULT;
751 goto register_out;
752 }
753 cdev->ap.private = cdev;
754 cdev->ap.recv_message = capi_recv_message;
755 cdev->errcode = capi20_register(&cdev->ap);
756 retval = (int)cdev->ap.applid;
757 if (cdev->errcode) {
758 cdev->ap.applid = 0;
759 retval = -EIO;
760 }
761
762register_out:
763 mutex_unlock(&cdev->lock);
764 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
766 case CAPI_GET_VERSION:
767 {
768 if (copy_from_user(&data.contr, argp,
769 sizeof(data.contr)))
770 return -EFAULT;
771 cdev->errcode = capi20_get_version(data.contr, &data.version);
772 if (cdev->errcode)
773 return -EIO;
774 if (copy_to_user(argp, &data.version,
775 sizeof(data.version)))
776 return -EFAULT;
777 }
778 return 0;
779
780 case CAPI_GET_SERIAL:
781 {
782 if (copy_from_user(&data.contr, argp,
783 sizeof(data.contr)))
784 return -EFAULT;
785 cdev->errcode = capi20_get_serial (data.contr, data.serial);
786 if (cdev->errcode)
787 return -EIO;
788 if (copy_to_user(argp, data.serial,
789 sizeof(data.serial)))
790 return -EFAULT;
791 }
792 return 0;
793 case CAPI_GET_PROFILE:
794 {
795 if (copy_from_user(&data.contr, argp,
796 sizeof(data.contr)))
797 return -EFAULT;
798
799 if (data.contr == 0) {
800 cdev->errcode = capi20_get_profile(data.contr, &data.profile);
801 if (cdev->errcode)
802 return -EIO;
803
804 retval = copy_to_user(argp,
805 &data.profile.ncontroller,
806 sizeof(data.profile.ncontroller));
807
808 } else {
809 cdev->errcode = capi20_get_profile(data.contr, &data.profile);
810 if (cdev->errcode)
811 return -EIO;
812
813 retval = copy_to_user(argp, &data.profile,
814 sizeof(data.profile));
815 }
816 if (retval)
817 return -EFAULT;
818 }
819 return 0;
820
821 case CAPI_GET_MANUFACTURER:
822 {
823 if (copy_from_user(&data.contr, argp,
824 sizeof(data.contr)))
825 return -EFAULT;
826 cdev->errcode = capi20_get_manufacturer(data.contr, data.manufacturer);
827 if (cdev->errcode)
828 return -EIO;
829
830 if (copy_to_user(argp, data.manufacturer,
831 sizeof(data.manufacturer)))
832 return -EFAULT;
833
834 }
835 return 0;
836 case CAPI_GET_ERRCODE:
837 data.errcode = cdev->errcode;
838 cdev->errcode = CAPI_NOERROR;
839 if (arg) {
840 if (copy_to_user(argp, &data.errcode,
841 sizeof(data.errcode)))
842 return -EFAULT;
843 }
844 return data.errcode;
845
846 case CAPI_INSTALLED:
847 if (capi20_isinstalled() == CAPI_NOERROR)
848 return 0;
849 return -ENXIO;
850
851 case CAPI_MANUFACTURER_CMD:
852 {
853 struct capi_manufacturer_cmd mcmd;
854 if (!capable(CAP_SYS_ADMIN))
855 return -EPERM;
856 if (copy_from_user(&mcmd, argp, sizeof(mcmd)))
857 return -EFAULT;
858 return capi20_manufacturer(mcmd.cmd, mcmd.data);
859 }
860 return 0;
861
862 case CAPI_SET_FLAGS:
Jan Kiszka05b41492010-02-08 10:12:19 +0000863 case CAPI_CLR_FLAGS: {
864 unsigned userflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
Jan Kiszka05b41492010-02-08 10:12:19 +0000866 if (copy_from_user(&userflags, argp, sizeof(userflags)))
867 return -EFAULT;
868
869 mutex_lock(&cdev->lock);
870 if (cmd == CAPI_SET_FLAGS)
871 cdev->userflags |= userflags;
872 else
873 cdev->userflags &= ~userflags;
874 mutex_unlock(&cdev->lock);
875 return 0;
876 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 case CAPI_GET_FLAGS:
878 if (copy_to_user(argp, &cdev->userflags,
879 sizeof(cdev->userflags)))
880 return -EFAULT;
881 return 0;
882
Jan Kiszka05b41492010-02-08 10:12:19 +0000883 case CAPI_NCCI_OPENCOUNT: {
884 struct capincci *nccip;
885 unsigned ncci;
886 int count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
Jan Kiszka05b41492010-02-08 10:12:19 +0000888 if (copy_from_user(&ncci, argp, sizeof(ncci)))
889 return -EFAULT;
890
891 mutex_lock(&cdev->lock);
892 nccip = capincci_find(cdev, (u32)ncci);
893 if (nccip)
894 count = capincci_minor_opencount(nccip);
895 mutex_unlock(&cdev->lock);
896 return count;
897 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
899#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
Jan Kiszka05b41492010-02-08 10:12:19 +0000900 case CAPI_NCCI_GETUNIT: {
901 struct capincci *nccip;
902 struct capiminor *mp;
903 unsigned ncci;
904 int unit = -ESRCH;
905
906 if (copy_from_user(&ncci, argp, sizeof(ncci)))
907 return -EFAULT;
908
909 mutex_lock(&cdev->lock);
910 nccip = capincci_find(cdev, (u32)ncci);
911 if (nccip) {
912 mp = nccip->minorp;
913 if (mp)
914 unit = mp->minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 }
Jan Kiszka05b41492010-02-08 10:12:19 +0000916 mutex_unlock(&cdev->lock);
917 return unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 }
Jan Kiszka05b41492010-02-08 10:12:19 +0000919#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
920
921 default:
922 return -EINVAL;
923 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924}
925
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000926static int capi_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927{
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000928 struct capidev *cdev;
929
930 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
931 if (!cdev)
932 return -ENOMEM;
933
Jan Kiszka05b41492010-02-08 10:12:19 +0000934 mutex_init(&cdev->lock);
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000935 skb_queue_head_init(&cdev->recvqueue);
936 init_waitqueue_head(&cdev->recvwait);
Jan Kiszka884f5c42010-02-08 10:12:22 +0000937 INIT_LIST_HEAD(&cdev->nccis);
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000938 file->private_data = cdev;
939
940 mutex_lock(&capidev_list_lock);
941 list_add_tail(&cdev->list, &capidev_list);
942 mutex_unlock(&capidev_list_lock);
943
944 return nonseekable_open(inode, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945}
946
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000947static int capi_release(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948{
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000949 struct capidev *cdev = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000951 mutex_lock(&capidev_list_lock);
952 list_del(&cdev->list);
953 mutex_unlock(&capidev_list_lock);
954
Jan Kiszka05b41492010-02-08 10:12:19 +0000955 if (cdev->ap.applid)
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000956 capi20_release(&cdev->ap);
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000957 skb_queue_purge(&cdev->recvqueue);
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000958 capincci_free(cdev, 0xffffffff);
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000959
960 kfree(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 return 0;
962}
963
Arjan van de Ven2b8693c2007-02-12 00:55:32 -0800964static const struct file_operations capi_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965{
966 .owner = THIS_MODULE,
967 .llseek = no_llseek,
968 .read = capi_read,
969 .write = capi_write,
970 .poll = capi_poll,
971 .ioctl = capi_ioctl,
972 .open = capi_open,
973 .release = capi_release,
974};
975
976#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
977/* -------- tty_operations for capincci ----------------------------- */
978
979static int capinc_tty_open(struct tty_struct * tty, struct file * file)
980{
981 struct capiminor *mp;
Michael Buesch053b47f2007-02-12 00:53:26 -0800982 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
Harvey Harrison2f9e9b62008-04-28 02:14:37 -0700984 if ((mp = capiminor_find(iminor(file->f_path.dentry->d_inode))) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 return -ENXIO;
Harvey Harrison2f9e9b62008-04-28 02:14:37 -0700986 if (mp->nccip == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 return -ENXIO;
988
989 tty->driver_data = (void *)mp;
990
Michael Buesch053b47f2007-02-12 00:53:26 -0800991 spin_lock_irqsave(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 if (atomic_read(&mp->ttyopencount) == 0)
993 mp->tty = tty;
994 atomic_inc(&mp->ttyopencount);
995#ifdef _DEBUG_REFCOUNT
996 printk(KERN_DEBUG "capinc_tty_open ocount=%d\n", atomic_read(&mp->ttyopencount));
997#endif
998 handle_minor_recv(mp);
Michael Buesch053b47f2007-02-12 00:53:26 -0800999 spin_unlock_irqrestore(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 return 0;
1001}
1002
1003static void capinc_tty_close(struct tty_struct * tty, struct file * file)
1004{
1005 struct capiminor *mp;
1006
1007 mp = (struct capiminor *)tty->driver_data;
1008 if (mp) {
1009 if (atomic_dec_and_test(&mp->ttyopencount)) {
1010#ifdef _DEBUG_REFCOUNT
1011 printk(KERN_DEBUG "capinc_tty_close lastclose\n");
1012#endif
1013 tty->driver_data = NULL;
1014 mp->tty = NULL;
1015 }
1016#ifdef _DEBUG_REFCOUNT
1017 printk(KERN_DEBUG "capinc_tty_close ocount=%d\n", atomic_read(&mp->ttyopencount));
1018#endif
Harvey Harrison2f9e9b62008-04-28 02:14:37 -07001019 if (mp->nccip == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 capiminor_free(mp);
1021 }
1022
1023#ifdef _DEBUG_REFCOUNT
1024 printk(KERN_DEBUG "capinc_tty_close\n");
1025#endif
1026}
1027
1028static int capinc_tty_write(struct tty_struct * tty,
1029 const unsigned char *buf, int count)
1030{
1031 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1032 struct sk_buff *skb;
Michael Buesch053b47f2007-02-12 00:53:26 -08001033 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
1035#ifdef _DEBUG_TTYFUNCS
1036 printk(KERN_DEBUG "capinc_tty_write(count=%d)\n", count);
1037#endif
1038
1039 if (!mp || !mp->nccip) {
1040#ifdef _DEBUG_TTYFUNCS
1041 printk(KERN_DEBUG "capinc_tty_write: mp or mp->ncci NULL\n");
1042#endif
1043 return 0;
1044 }
1045
Michael Buesch053b47f2007-02-12 00:53:26 -08001046 spin_lock_irqsave(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 skb = mp->ttyskb;
1048 if (skb) {
1049 mp->ttyskb = NULL;
1050 skb_queue_tail(&mp->outqueue, skb);
1051 mp->outbytes += skb->len;
1052 }
1053
1054 skb = alloc_skb(CAPI_DATA_B3_REQ_LEN+count, GFP_ATOMIC);
1055 if (!skb) {
1056 printk(KERN_ERR "capinc_tty_write: alloc_skb failed\n");
Michael Buesch053b47f2007-02-12 00:53:26 -08001057 spin_unlock_irqrestore(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 return -ENOMEM;
1059 }
1060
1061 skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
1062 memcpy(skb_put(skb, count), buf, count);
1063
1064 skb_queue_tail(&mp->outqueue, skb);
1065 mp->outbytes += skb->len;
1066 (void)handle_minor_send(mp);
1067 (void)handle_minor_recv(mp);
Michael Buesch053b47f2007-02-12 00:53:26 -08001068 spin_unlock_irqrestore(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 return count;
1070}
1071
Alan Coxf2545a72008-04-30 00:54:09 -07001072static int capinc_tty_put_char(struct tty_struct *tty, unsigned char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073{
1074 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1075 struct sk_buff *skb;
Michael Buesch053b47f2007-02-12 00:53:26 -08001076 unsigned long flags;
Alan Coxf2545a72008-04-30 00:54:09 -07001077 int ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
1079#ifdef _DEBUG_TTYFUNCS
1080 printk(KERN_DEBUG "capinc_put_char(%u)\n", ch);
1081#endif
1082
1083 if (!mp || !mp->nccip) {
1084#ifdef _DEBUG_TTYFUNCS
1085 printk(KERN_DEBUG "capinc_tty_put_char: mp or mp->ncci NULL\n");
1086#endif
Alan Coxf2545a72008-04-30 00:54:09 -07001087 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 }
1089
Michael Buesch053b47f2007-02-12 00:53:26 -08001090 spin_lock_irqsave(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 skb = mp->ttyskb;
1092 if (skb) {
1093 if (skb_tailroom(skb) > 0) {
1094 *(skb_put(skb, 1)) = ch;
Michael Buesch053b47f2007-02-12 00:53:26 -08001095 spin_unlock_irqrestore(&workaround_lock, flags);
Alan Coxf2545a72008-04-30 00:54:09 -07001096 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 }
1098 mp->ttyskb = NULL;
1099 skb_queue_tail(&mp->outqueue, skb);
1100 mp->outbytes += skb->len;
1101 (void)handle_minor_send(mp);
1102 }
1103 skb = alloc_skb(CAPI_DATA_B3_REQ_LEN+CAPI_MAX_BLKSIZE, GFP_ATOMIC);
1104 if (skb) {
1105 skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
1106 *(skb_put(skb, 1)) = ch;
1107 mp->ttyskb = skb;
1108 } else {
1109 printk(KERN_ERR "capinc_put_char: char %u lost\n", ch);
Alan Coxf2545a72008-04-30 00:54:09 -07001110 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 }
Michael Buesch053b47f2007-02-12 00:53:26 -08001112 spin_unlock_irqrestore(&workaround_lock, flags);
Alan Coxf2545a72008-04-30 00:54:09 -07001113 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114}
1115
1116static void capinc_tty_flush_chars(struct tty_struct *tty)
1117{
1118 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1119 struct sk_buff *skb;
Michael Buesch053b47f2007-02-12 00:53:26 -08001120 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
1122#ifdef _DEBUG_TTYFUNCS
1123 printk(KERN_DEBUG "capinc_tty_flush_chars\n");
1124#endif
1125
1126 if (!mp || !mp->nccip) {
1127#ifdef _DEBUG_TTYFUNCS
1128 printk(KERN_DEBUG "capinc_tty_flush_chars: mp or mp->ncci NULL\n");
1129#endif
1130 return;
1131 }
1132
Michael Buesch053b47f2007-02-12 00:53:26 -08001133 spin_lock_irqsave(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 skb = mp->ttyskb;
1135 if (skb) {
1136 mp->ttyskb = NULL;
1137 skb_queue_tail(&mp->outqueue, skb);
1138 mp->outbytes += skb->len;
1139 (void)handle_minor_send(mp);
1140 }
1141 (void)handle_minor_recv(mp);
Michael Buesch053b47f2007-02-12 00:53:26 -08001142 spin_unlock_irqrestore(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143}
1144
1145static int capinc_tty_write_room(struct tty_struct *tty)
1146{
1147 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1148 int room;
1149 if (!mp || !mp->nccip) {
1150#ifdef _DEBUG_TTYFUNCS
1151 printk(KERN_DEBUG "capinc_tty_write_room: mp or mp->ncci NULL\n");
1152#endif
1153 return 0;
1154 }
1155 room = CAPINC_MAX_SENDQUEUE-skb_queue_len(&mp->outqueue);
1156 room *= CAPI_MAX_BLKSIZE;
1157#ifdef _DEBUG_TTYFUNCS
1158 printk(KERN_DEBUG "capinc_tty_write_room = %d\n", room);
1159#endif
1160 return room;
1161}
1162
Adrian Bunk408b6642005-05-01 08:59:29 -07001163static int capinc_tty_chars_in_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164{
1165 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1166 if (!mp || !mp->nccip) {
1167#ifdef _DEBUG_TTYFUNCS
1168 printk(KERN_DEBUG "capinc_tty_chars_in_buffer: mp or mp->ncci NULL\n");
1169#endif
1170 return 0;
1171 }
1172#ifdef _DEBUG_TTYFUNCS
1173 printk(KERN_DEBUG "capinc_tty_chars_in_buffer = %d nack=%d sq=%d rq=%d\n",
1174 mp->outbytes, mp->nack,
1175 skb_queue_len(&mp->outqueue),
1176 skb_queue_len(&mp->inqueue));
1177#endif
1178 return mp->outbytes;
1179}
1180
1181static int capinc_tty_ioctl(struct tty_struct *tty, struct file * file,
1182 unsigned int cmd, unsigned long arg)
1183{
1184 int error = 0;
1185 switch (cmd) {
1186 default:
Stephen Rothwell53e86312008-10-13 10:44:33 +01001187 error = n_tty_ioctl_helper(tty, file, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 break;
1189 }
1190 return error;
1191}
1192
Alan Cox606d0992006-12-08 02:38:45 -08001193static void capinc_tty_set_termios(struct tty_struct *tty, struct ktermios * old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194{
1195#ifdef _DEBUG_TTYFUNCS
1196 printk(KERN_DEBUG "capinc_tty_set_termios\n");
1197#endif
1198}
1199
1200static void capinc_tty_throttle(struct tty_struct * tty)
1201{
1202 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1203#ifdef _DEBUG_TTYFUNCS
1204 printk(KERN_DEBUG "capinc_tty_throttle\n");
1205#endif
1206 if (mp)
1207 mp->ttyinstop = 1;
1208}
1209
1210static void capinc_tty_unthrottle(struct tty_struct * tty)
1211{
1212 struct capiminor *mp = (struct capiminor *)tty->driver_data;
Michael Buesch053b47f2007-02-12 00:53:26 -08001213 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214#ifdef _DEBUG_TTYFUNCS
1215 printk(KERN_DEBUG "capinc_tty_unthrottle\n");
1216#endif
1217 if (mp) {
Michael Buesch053b47f2007-02-12 00:53:26 -08001218 spin_lock_irqsave(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 mp->ttyinstop = 0;
1220 handle_minor_recv(mp);
Michael Buesch053b47f2007-02-12 00:53:26 -08001221 spin_unlock_irqrestore(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 }
1223}
1224
1225static void capinc_tty_stop(struct tty_struct *tty)
1226{
1227 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1228#ifdef _DEBUG_TTYFUNCS
1229 printk(KERN_DEBUG "capinc_tty_stop\n");
1230#endif
1231 if (mp) {
1232 mp->ttyoutstop = 1;
1233 }
1234}
1235
1236static void capinc_tty_start(struct tty_struct *tty)
1237{
1238 struct capiminor *mp = (struct capiminor *)tty->driver_data;
Michael Buesch053b47f2007-02-12 00:53:26 -08001239 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240#ifdef _DEBUG_TTYFUNCS
1241 printk(KERN_DEBUG "capinc_tty_start\n");
1242#endif
1243 if (mp) {
Michael Buesch053b47f2007-02-12 00:53:26 -08001244 spin_lock_irqsave(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 mp->ttyoutstop = 0;
1246 (void)handle_minor_send(mp);
Michael Buesch053b47f2007-02-12 00:53:26 -08001247 spin_unlock_irqrestore(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 }
1249}
1250
1251static void capinc_tty_hangup(struct tty_struct *tty)
1252{
1253#ifdef _DEBUG_TTYFUNCS
1254 printk(KERN_DEBUG "capinc_tty_hangup\n");
1255#endif
1256}
1257
Alan Cox9e98966c2008-07-22 11:18:03 +01001258static int capinc_tty_break_ctl(struct tty_struct *tty, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259{
1260#ifdef _DEBUG_TTYFUNCS
1261 printk(KERN_DEBUG "capinc_tty_break_ctl(%d)\n", state);
1262#endif
Alan Cox9e98966c2008-07-22 11:18:03 +01001263 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264}
1265
1266static void capinc_tty_flush_buffer(struct tty_struct *tty)
1267{
1268#ifdef _DEBUG_TTYFUNCS
1269 printk(KERN_DEBUG "capinc_tty_flush_buffer\n");
1270#endif
1271}
1272
1273static void capinc_tty_set_ldisc(struct tty_struct *tty)
1274{
1275#ifdef _DEBUG_TTYFUNCS
1276 printk(KERN_DEBUG "capinc_tty_set_ldisc\n");
1277#endif
1278}
1279
1280static void capinc_tty_send_xchar(struct tty_struct *tty, char ch)
1281{
1282#ifdef _DEBUG_TTYFUNCS
1283 printk(KERN_DEBUG "capinc_tty_send_xchar(%d)\n", ch);
1284#endif
1285}
1286
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287static struct tty_driver *capinc_tty_driver;
1288
Jeff Dikeb68e31d2006-10-02 02:17:18 -07001289static const struct tty_operations capinc_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 .open = capinc_tty_open,
1291 .close = capinc_tty_close,
1292 .write = capinc_tty_write,
1293 .put_char = capinc_tty_put_char,
1294 .flush_chars = capinc_tty_flush_chars,
1295 .write_room = capinc_tty_write_room,
1296 .chars_in_buffer = capinc_tty_chars_in_buffer,
1297 .ioctl = capinc_tty_ioctl,
1298 .set_termios = capinc_tty_set_termios,
1299 .throttle = capinc_tty_throttle,
1300 .unthrottle = capinc_tty_unthrottle,
1301 .stop = capinc_tty_stop,
1302 .start = capinc_tty_start,
1303 .hangup = capinc_tty_hangup,
1304 .break_ctl = capinc_tty_break_ctl,
1305 .flush_buffer = capinc_tty_flush_buffer,
1306 .set_ldisc = capinc_tty_set_ldisc,
1307 .send_xchar = capinc_tty_send_xchar,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308};
1309
Jan Kiszkae76b1542010-02-08 10:12:24 +00001310static int __init capinc_tty_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311{
1312 struct tty_driver *drv;
Jan Kiszkae76b1542010-02-08 10:12:24 +00001313 int err;
1314
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 if (capi_ttyminors > CAPINC_MAX_PORTS)
1316 capi_ttyminors = CAPINC_MAX_PORTS;
1317 if (capi_ttyminors <= 0)
1318 capi_ttyminors = CAPINC_NR_PORTS;
1319
Jan Kiszka81d17fe2010-02-08 10:12:23 +00001320 capiminors = kzalloc(sizeof(struct capi_minor *) * capi_ttyminors,
1321 GFP_KERNEL);
1322 if (!capiminors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 return -ENOMEM;
1324
Jan Kiszka81d17fe2010-02-08 10:12:23 +00001325 drv = alloc_tty_driver(capi_ttyminors);
1326 if (!drv) {
1327 kfree(capiminors);
1328 return -ENOMEM;
1329 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 drv->owner = THIS_MODULE;
1331 drv->driver_name = "capi_nc";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 drv->name = "capi";
1333 drv->major = capi_ttymajor;
1334 drv->minor_start = 0;
1335 drv->type = TTY_DRIVER_TYPE_SERIAL;
1336 drv->subtype = SERIAL_TYPE_NORMAL;
1337 drv->init_termios = tty_std_termios;
1338 drv->init_termios.c_iflag = ICRNL;
1339 drv->init_termios.c_oflag = OPOST | ONLCR;
1340 drv->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1341 drv->init_termios.c_lflag = 0;
1342 drv->flags = TTY_DRIVER_REAL_RAW|TTY_DRIVER_RESET_TERMIOS;
1343 tty_set_operations(drv, &capinc_ops);
Jan Kiszkae76b1542010-02-08 10:12:24 +00001344
1345 err = tty_register_driver(drv);
1346 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 put_tty_driver(drv);
Jan Kiszka81d17fe2010-02-08 10:12:23 +00001348 kfree(capiminors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 printk(KERN_ERR "Couldn't register capi_nc driver\n");
Jan Kiszkae76b1542010-02-08 10:12:24 +00001350 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 }
1352 capinc_tty_driver = drv;
1353 return 0;
1354}
1355
Jan Kiszkae76b1542010-02-08 10:12:24 +00001356static void __exit capinc_tty_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357{
Jan Kiszkae76b1542010-02-08 10:12:24 +00001358 tty_unregister_driver(capinc_tty_driver);
1359 put_tty_driver(capinc_tty_driver);
Jan Kiszka81d17fe2010-02-08 10:12:23 +00001360 kfree(capiminors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361}
1362
Jan Kiszka501c87a2010-02-08 10:12:16 +00001363#else /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
1364
1365static inline int capinc_tty_init(void)
1366{
1367 return 0;
1368}
1369
1370static inline void capinc_tty_exit(void) { }
1371
1372#endif /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
1374/* -------- /proc functions ----------------------------------------- */
1375
1376/*
1377 * /proc/capi/capi20:
1378 * minor applid nrecvctlpkt nrecvdatapkt nsendctlpkt nsenddatapkt
1379 */
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001380static int capi20_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381{
1382 struct capidev *cdev;
1383 struct list_head *l;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
Jan Kiszkab8f433d2010-02-08 10:12:17 +00001385 mutex_lock(&capidev_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 list_for_each(l, &capidev_list) {
1387 cdev = list_entry(l, struct capidev, list);
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001388 seq_printf(m, "0 %d %lu %lu %lu %lu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 cdev->ap.applid,
1390 cdev->ap.nrecvctlpkt,
1391 cdev->ap.nrecvdatapkt,
1392 cdev->ap.nsentctlpkt,
1393 cdev->ap.nsentdatapkt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 }
Jan Kiszkab8f433d2010-02-08 10:12:17 +00001395 mutex_unlock(&capidev_list_lock);
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001396 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397}
1398
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001399static int capi20_proc_open(struct inode *inode, struct file *file)
1400{
1401 return single_open(file, capi20_proc_show, NULL);
1402}
1403
1404static const struct file_operations capi20_proc_fops = {
1405 .owner = THIS_MODULE,
1406 .open = capi20_proc_open,
1407 .read = seq_read,
1408 .llseek = seq_lseek,
1409 .release = single_release,
1410};
1411
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412/*
1413 * /proc/capi/capi20ncci:
1414 * applid ncci
1415 */
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001416static int capi20ncci_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417{
Jan Kiszka884f5c42010-02-08 10:12:22 +00001418 struct capidev *cdev;
1419 struct capincci *np;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420
Jan Kiszkab8f433d2010-02-08 10:12:17 +00001421 mutex_lock(&capidev_list_lock);
Jan Kiszka884f5c42010-02-08 10:12:22 +00001422 list_for_each_entry(cdev, &capidev_list, list) {
Jan Kiszka05b41492010-02-08 10:12:19 +00001423 mutex_lock(&cdev->lock);
Jan Kiszka884f5c42010-02-08 10:12:22 +00001424 list_for_each_entry(np, &cdev->nccis, list)
1425 seq_printf(m, "%d 0x%x\n", cdev->ap.applid, np->ncci);
Jan Kiszka05b41492010-02-08 10:12:19 +00001426 mutex_unlock(&cdev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 }
Jan Kiszkab8f433d2010-02-08 10:12:17 +00001428 mutex_unlock(&capidev_list_lock);
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001429 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430}
1431
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001432static int capi20ncci_proc_open(struct inode *inode, struct file *file)
1433{
1434 return single_open(file, capi20ncci_proc_show, NULL);
1435}
1436
1437static const struct file_operations capi20ncci_proc_fops = {
1438 .owner = THIS_MODULE,
1439 .open = capi20ncci_proc_open,
1440 .read = seq_read,
1441 .llseek = seq_lseek,
1442 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443};
1444
1445static void __init proc_init(void)
1446{
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001447 proc_create("capi/capi20", 0, NULL, &capi20_proc_fops);
1448 proc_create("capi/capi20ncci", 0, NULL, &capi20ncci_proc_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449}
1450
1451static void __exit proc_exit(void)
1452{
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001453 remove_proc_entry("capi/capi20", NULL);
1454 remove_proc_entry("capi/capi20ncci", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455}
1456
1457/* -------- init function and module interface ---------------------- */
1458
1459
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460static int __init capi_init(void)
1461{
Jan Kiszka88549d62010-02-08 10:12:09 +00001462 const char *compileinfo;
Andrew Morton6d9eac32006-03-28 01:56:19 -08001463 int major_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464
Andrew Morton6d9eac32006-03-28 01:56:19 -08001465 major_ret = register_chrdev(capi_major, "capi20", &capi_fops);
1466 if (major_ret < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 printk(KERN_ERR "capi20: unable to get major %d\n", capi_major);
Andrew Morton6d9eac32006-03-28 01:56:19 -08001468 return major_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 }
gregkh@suse.de56b22932005-03-23 10:01:41 -08001470 capi_class = class_create(THIS_MODULE, "capi");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 if (IS_ERR(capi_class)) {
1472 unregister_chrdev(capi_major, "capi20");
1473 return PTR_ERR(capi_class);
1474 }
1475
Greg Kroah-Hartmana9b12612008-07-21 20:03:34 -07001476 device_create(capi_class, NULL, MKDEV(capi_major, 0), NULL, "capi");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 if (capinc_tty_init() < 0) {
Tony Jonesd78b0362007-09-25 02:03:03 +02001479 device_destroy(capi_class, MKDEV(capi_major, 0));
gregkh@suse.de56b22932005-03-23 10:01:41 -08001480 class_destroy(capi_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 unregister_chrdev(capi_major, "capi20");
1482 return -ENOMEM;
1483 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484
1485 proc_init();
1486
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487#if defined(CONFIG_ISDN_CAPI_CAPIFS) || defined(CONFIG_ISDN_CAPI_CAPIFS_MODULE)
1488 compileinfo = " (middleware+capifs)";
Jan Kiszka501c87a2010-02-08 10:12:16 +00001489#elif defined(CONFIG_ISDN_CAPI_MIDDLEWARE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 compileinfo = " (no capifs)";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491#else
1492 compileinfo = " (no middleware)";
1493#endif
Jan Kiszka88549d62010-02-08 10:12:09 +00001494 printk(KERN_NOTICE "CAPI 2.0 started up with major %d%s\n",
1495 capi_major, compileinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496
1497 return 0;
1498}
1499
1500static void __exit capi_exit(void)
1501{
1502 proc_exit();
1503
Tony Jonesd78b0362007-09-25 02:03:03 +02001504 device_destroy(capi_class, MKDEV(capi_major, 0));
gregkh@suse.de56b22932005-03-23 10:01:41 -08001505 class_destroy(capi_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 unregister_chrdev(capi_major, "capi20");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 capinc_tty_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509}
1510
1511module_init(capi_init);
1512module_exit(capi_exit);