blob: 867589a5916a1bbe5e17fedd2ab12fbd5f914191 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#undef _DEBUG_TTYFUNCS /* call to tty_driver */
49#undef _DEBUG_DATAFLOW /* data flow */
50
51/* -------- driver information -------------------------------------- */
52
gregkh@suse.de56b22932005-03-23 10:01:41 -080053static struct class *capi_class;
Adrian Bunk408b6642005-05-01 08:59:29 -070054static int capi_major = 68; /* allocated */
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56module_param_named(major, capi_major, uint, 0);
Jan Kiszka501c87a2010-02-08 10:12:16 +000057
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
Jan Kiszka501c87a2010-02-08 10:12:16 +000059#define CAPINC_NR_PORTS 32
60#define CAPINC_MAX_PORTS 256
61
Jan Kiszka501c87a2010-02-08 10:12:16 +000062static int capi_ttyminors = CAPINC_NR_PORTS;
63
Linus Torvalds1da177e2005-04-16 15:20:36 -070064module_param_named(ttyminors, capi_ttyminors, uint, 0);
65#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
66
67/* -------- defines ------------------------------------------------- */
68
69#define CAPINC_MAX_RECVQUEUE 10
70#define CAPINC_MAX_SENDQUEUE 10
71#define CAPI_MAX_BLKSIZE 2048
72
73/* -------- data structures ----------------------------------------- */
74
75struct capidev;
76struct capincci;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077struct capiminor;
78
Michael Buesch6aa65472006-06-26 00:25:30 -070079struct datahandle_queue {
80 struct list_head list;
81 u16 datahandle;
82};
83
Linus Torvalds1da177e2005-04-16 15:20:36 -070084struct capiminor {
Jan Kiszka0159d542010-02-08 10:12:27 +000085 struct kref kref;
86
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 unsigned int minor;
Jan Kiszka90926f02010-02-08 10:12:06 +000088 struct dentry *capifs_dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
90 struct capi20_appl *ap;
91 u32 ncci;
92 u16 datahandle;
93 u16 msgid;
94
Jan Kiszkafb4b4882010-02-08 10:12:29 +000095 struct tty_port port;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 int ttyinstop;
97 int ttyoutstop;
98 struct sk_buff *ttyskb;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
100 struct sk_buff_head inqueue;
101 int inbytes;
102 struct sk_buff_head outqueue;
103 int outbytes;
104
105 /* transmit path */
Michael Buesch6aa65472006-06-26 00:25:30 -0700106 struct list_head ackqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 int nack;
Michael Buesch6aa65472006-06-26 00:25:30 -0700108 spinlock_t ackqlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Michael Buesch053b47f2007-02-12 00:53:26 -0800111/* FIXME: The following lock is a sledgehammer-workaround to a
112 * locking issue with the capiminor (and maybe other) data structure(s).
113 * Access to this data is done in a racy way and crashes the machine with
114 * a FritzCard DSL driver; sooner or later. This is a workaround
115 * which trades scalability vs stability, so it doesn't crash the kernel anymore.
116 * The correct (and scalable) fix for the issue seems to require
117 * an API change to the drivers... . */
118static DEFINE_SPINLOCK(workaround_lock);
119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120struct capincci {
Jan Kiszka884f5c42010-02-08 10:12:22 +0000121 struct list_head list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 u32 ncci;
123 struct capidev *cdev;
124#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
125 struct capiminor *minorp;
126#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
127};
128
129struct capidev {
130 struct list_head list;
131 struct capi20_appl ap;
132 u16 errcode;
133 unsigned userflags;
134
135 struct sk_buff_head recvqueue;
136 wait_queue_head_t recvwait;
137
Jan Kiszka884f5c42010-02-08 10:12:22 +0000138 struct list_head nccis;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Jan Kiszka05b41492010-02-08 10:12:19 +0000140 struct mutex lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141};
142
143/* -------- global variables ---------------------------------------- */
144
Jan Kiszkab8f433d2010-02-08 10:12:17 +0000145static DEFINE_MUTEX(capidev_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146static LIST_HEAD(capidev_list);
147
148#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
Jan Kiszka501c87a2010-02-08 10:12:16 +0000149
Jan Kiszka3d5d30f2010-02-08 10:12:33 +0000150static DEFINE_SPINLOCK(capiminors_lock);
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000151static struct capiminor **capiminors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
Jan Kiszka40fb2d02010-02-08 10:12:25 +0000153static struct tty_driver *capinc_tty_driver;
154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155/* -------- datahandles --------------------------------------------- */
156
Jan Kiszka501c87a2010-02-08 10:12:16 +0000157static int capiminor_add_ack(struct capiminor *mp, u16 datahandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158{
Michael Buesch6aa65472006-06-26 00:25:30 -0700159 struct datahandle_queue *n;
160 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
162 n = kmalloc(sizeof(*n), GFP_ATOMIC);
Michael Buesch6aa65472006-06-26 00:25:30 -0700163 if (unlikely(!n)) {
164 printk(KERN_ERR "capi: alloc datahandle failed\n");
165 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 n->datahandle = datahandle;
Michael Buesch6aa65472006-06-26 00:25:30 -0700168 INIT_LIST_HEAD(&n->list);
169 spin_lock_irqsave(&mp->ackqlock, flags);
170 list_add_tail(&n->list, &mp->ackqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 mp->nack++;
Michael Buesch6aa65472006-06-26 00:25:30 -0700172 spin_unlock_irqrestore(&mp->ackqlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 return 0;
174}
175
176static int capiminor_del_ack(struct capiminor *mp, u16 datahandle)
177{
Michael Buesch6aa65472006-06-26 00:25:30 -0700178 struct datahandle_queue *p, *tmp;
179 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
Michael Buesch6aa65472006-06-26 00:25:30 -0700181 spin_lock_irqsave(&mp->ackqlock, flags);
182 list_for_each_entry_safe(p, tmp, &mp->ackqueue, list) {
183 if (p->datahandle == datahandle) {
184 list_del(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 kfree(p);
186 mp->nack--;
Michael Buesch6aa65472006-06-26 00:25:30 -0700187 spin_unlock_irqrestore(&mp->ackqlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 return 0;
189 }
190 }
Michael Buesch6aa65472006-06-26 00:25:30 -0700191 spin_unlock_irqrestore(&mp->ackqlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 return -1;
193}
194
195static void capiminor_del_all_ack(struct capiminor *mp)
196{
Michael Buesch6aa65472006-06-26 00:25:30 -0700197 struct datahandle_queue *p, *tmp;
198 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
Michael Buesch6aa65472006-06-26 00:25:30 -0700200 spin_lock_irqsave(&mp->ackqlock, flags);
201 list_for_each_entry_safe(p, tmp, &mp->ackqueue, list) {
202 list_del(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 kfree(p);
204 mp->nack--;
205 }
Michael Buesch6aa65472006-06-26 00:25:30 -0700206 spin_unlock_irqrestore(&mp->ackqlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207}
208
209
210/* -------- struct capiminor ---------------------------------------- */
211
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000212static const struct tty_port_operations capiminor_port_ops; /* we have none */
213
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214static struct capiminor *capiminor_alloc(struct capi20_appl *ap, u32 ncci)
215{
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000216 struct capiminor *mp;
Jan Kiszka40fb2d02010-02-08 10:12:25 +0000217 struct device *dev;
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000218 unsigned int minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Jan Kiszka54f0fad2010-02-08 10:12:20 +0000220 mp = kzalloc(sizeof(*mp), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 if (!mp) {
222 printk(KERN_ERR "capi: can't alloc capiminor\n");
223 return NULL;
224 }
225
Jan Kiszka0159d542010-02-08 10:12:27 +0000226 kref_init(&mp->kref);
227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 mp->ap = ap;
229 mp->ncci = ncci;
230 mp->msgid = 0;
Michael Buesch6aa65472006-06-26 00:25:30 -0700231 INIT_LIST_HEAD(&mp->ackqueue);
232 spin_lock_init(&mp->ackqlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
234 skb_queue_head_init(&mp->inqueue);
235 skb_queue_head_init(&mp->outqueue);
236
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000237 tty_port_init(&mp->port);
238 mp->port.ops = &capiminor_port_ops;
239
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000240 /* Allocate the least unused minor number. */
Jan Kiszka3d5d30f2010-02-08 10:12:33 +0000241 spin_lock(&capiminors_lock);
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000242 for (minor = 0; minor < capi_ttyminors; minor++)
243 if (!capiminors[minor]) {
244 capiminors[minor] = mp;
245 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 }
Jan Kiszka3d5d30f2010-02-08 10:12:33 +0000247 spin_unlock(&capiminors_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000249 if (minor == capi_ttyminors) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 printk(KERN_NOTICE "capi: out of minors\n");
Jan Kiszka40fb2d02010-02-08 10:12:25 +0000251 goto err_out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 }
253
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000254 mp->minor = minor;
255
Jan Kiszka40fb2d02010-02-08 10:12:25 +0000256 dev = tty_register_device(capinc_tty_driver, minor, NULL);
257 if (IS_ERR(dev))
258 goto err_out2;
259
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 return mp;
Jan Kiszka40fb2d02010-02-08 10:12:25 +0000261
262err_out2:
Jan Kiszka3d5d30f2010-02-08 10:12:33 +0000263 spin_lock(&capiminors_lock);
Jan Kiszka40fb2d02010-02-08 10:12:25 +0000264 capiminors[minor] = NULL;
Jan Kiszka3d5d30f2010-02-08 10:12:33 +0000265 spin_unlock(&capiminors_lock);
Jan Kiszka40fb2d02010-02-08 10:12:25 +0000266
267err_out1:
268 kfree(mp);
269 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270}
271
Jan Kiszka0159d542010-02-08 10:12:27 +0000272static void capiminor_destroy(struct kref *kref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273{
Jan Kiszka0159d542010-02-08 10:12:27 +0000274 struct capiminor *mp = container_of(kref, struct capiminor, kref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
Wei Yongjund46604e2009-02-25 00:12:09 +0000276 kfree_skb(mp->ttyskb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 skb_queue_purge(&mp->inqueue);
278 skb_queue_purge(&mp->outqueue);
279 capiminor_del_all_ack(mp);
280 kfree(mp);
281}
282
Jan Kiszka40fb2d02010-02-08 10:12:25 +0000283static struct capiminor *capiminor_get(unsigned int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284{
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000285 struct capiminor *mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
Jan Kiszka3d5d30f2010-02-08 10:12:33 +0000287 spin_lock(&capiminors_lock);
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000288 mp = capiminors[minor];
Jan Kiszka0159d542010-02-08 10:12:27 +0000289 if (mp)
290 kref_get(&mp->kref);
Jan Kiszka3d5d30f2010-02-08 10:12:33 +0000291 spin_unlock(&capiminors_lock);
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000292
293 return mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
Jan Kiszka0159d542010-02-08 10:12:27 +0000296static inline void capiminor_put(struct capiminor *mp)
297{
298 kref_put(&mp->kref, capiminor_destroy);
299}
300
301static void capiminor_free(struct capiminor *mp)
302{
Jan Kiszka0159d542010-02-08 10:12:27 +0000303 tty_unregister_device(capinc_tty_driver, mp->minor);
304
Jan Kiszka3d5d30f2010-02-08 10:12:33 +0000305 spin_lock(&capiminors_lock);
Jan Kiszka0159d542010-02-08 10:12:27 +0000306 capiminors[mp->minor] = NULL;
Jan Kiszka3d5d30f2010-02-08 10:12:33 +0000307 spin_unlock(&capiminors_lock);
Jan Kiszka0159d542010-02-08 10:12:27 +0000308
309 capiminor_put(mp);
310}
311
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312/* -------- struct capincci ----------------------------------------- */
313
Jan Kiszka501c87a2010-02-08 10:12:16 +0000314static void capincci_alloc_minor(struct capidev *cdev, struct capincci *np)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315{
Jan Kiszka501c87a2010-02-08 10:12:16 +0000316 struct capiminor *mp;
Jan Kiszkae95ac142010-02-08 10:12:26 +0000317 dev_t device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
Jan Kiszka501c87a2010-02-08 10:12:16 +0000319 if (!(cdev->userflags & CAPIFLAG_HIGHJACKING))
320 return;
321
322 mp = np->minorp = capiminor_alloc(&cdev->ap, np->ncci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 if (mp) {
Jan Kiszkae95ac142010-02-08 10:12:26 +0000324 device = MKDEV(capinc_tty_driver->major, mp->minor);
325 mp->capifs_dentry = capifs_new_ncci(mp->minor, device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 }
Jan Kiszka501c87a2010-02-08 10:12:16 +0000327}
328
329static void capincci_free_minor(struct capincci *np)
330{
331 struct capiminor *mp = np->minorp;
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000332 struct tty_struct *tty;
Jan Kiszka501c87a2010-02-08 10:12:16 +0000333
334 if (mp) {
335 capifs_free_ncci(mp->capifs_dentry);
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000336
337 tty = tty_port_tty_get(&mp->port);
338 if (tty) {
Jan Kiszka30bced92010-02-08 10:12:31 +0000339 tty_vhangup(tty);
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000340 tty_kref_put(tty);
Jan Kiszka501c87a2010-02-08 10:12:16 +0000341 }
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000342
343 capiminor_free(mp);
Jan Kiszka501c87a2010-02-08 10:12:16 +0000344 }
345}
346
347static inline unsigned int capincci_minor_opencount(struct capincci *np)
348{
349 struct capiminor *mp = np->minorp;
Jan Kiszkaa84fdf42010-02-08 10:12:34 +0000350 unsigned int count = 0;
351 struct tty_struct *tty;
Jan Kiszka501c87a2010-02-08 10:12:16 +0000352
Jan Kiszkaa84fdf42010-02-08 10:12:34 +0000353 if (mp) {
354 tty = tty_port_tty_get(&mp->port);
355 if (tty) {
356 count = tty->count;
357 tty_kref_put(tty);
358 }
359 }
360 return count;
Jan Kiszka501c87a2010-02-08 10:12:16 +0000361}
362
363#else /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
364
365static inline void
366capincci_alloc_minor(struct capidev *cdev, struct capincci *np) { }
367static inline void capincci_free_minor(struct capincci *np) { }
368
369static inline unsigned int capincci_minor_opencount(struct capincci *np)
370{
371 return 0;
372}
373
374#endif /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
375
376static struct capincci *capincci_alloc(struct capidev *cdev, u32 ncci)
377{
Jan Kiszka884f5c42010-02-08 10:12:22 +0000378 struct capincci *np;
Jan Kiszka501c87a2010-02-08 10:12:16 +0000379
Jan Kiszka54f0fad2010-02-08 10:12:20 +0000380 np = kzalloc(sizeof(*np), GFP_KERNEL);
Jan Kiszka501c87a2010-02-08 10:12:16 +0000381 if (!np)
382 return NULL;
383 np->ncci = ncci;
384 np->cdev = cdev;
385
386 capincci_alloc_minor(cdev, np);
387
Jan Kiszka884f5c42010-02-08 10:12:22 +0000388 list_add_tail(&np->list, &cdev->nccis);
389
390 return np;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391}
392
393static void capincci_free(struct capidev *cdev, u32 ncci)
394{
Jan Kiszka884f5c42010-02-08 10:12:22 +0000395 struct capincci *np, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
Jan Kiszka884f5c42010-02-08 10:12:22 +0000397 list_for_each_entry_safe(np, tmp, &cdev->nccis, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 if (ncci == 0xffffffff || np->ncci == ncci) {
Jan Kiszka501c87a2010-02-08 10:12:16 +0000399 capincci_free_minor(np);
Jan Kiszka884f5c42010-02-08 10:12:22 +0000400 list_del(&np->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 kfree(np);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403}
404
405static struct capincci *capincci_find(struct capidev *cdev, u32 ncci)
406{
Jan Kiszka884f5c42010-02-08 10:12:22 +0000407 struct capincci *np;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Jan Kiszka884f5c42010-02-08 10:12:22 +0000409 list_for_each_entry(np, &cdev->nccis, list)
410 if (np->ncci == ncci)
411 return np;
412 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413}
414
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
416/* -------- handle data queue --------------------------------------- */
417
418static struct sk_buff *
419gen_data_b3_resp_for(struct capiminor *mp, struct sk_buff *skb)
420{
421 struct sk_buff *nskb;
422 nskb = alloc_skb(CAPI_DATA_B3_RESP_LEN, GFP_ATOMIC);
423 if (nskb) {
424 u16 datahandle = CAPIMSG_U16(skb->data,CAPIMSG_BASELEN+4+4+2);
425 unsigned char *s = skb_put(nskb, CAPI_DATA_B3_RESP_LEN);
426 capimsg_setu16(s, 0, CAPI_DATA_B3_RESP_LEN);
427 capimsg_setu16(s, 2, mp->ap->applid);
428 capimsg_setu8 (s, 4, CAPI_DATA_B3);
429 capimsg_setu8 (s, 5, CAPI_RESP);
430 capimsg_setu16(s, 6, mp->msgid++);
431 capimsg_setu32(s, 8, mp->ncci);
432 capimsg_setu16(s, 12, datahandle);
433 }
434 return nskb;
435}
436
437static int handle_recv_skb(struct capiminor *mp, struct sk_buff *skb)
438{
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000439 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 struct sk_buff *nskb;
441 int datalen;
442 u16 errcode, datahandle;
443 struct tty_ldisc *ld;
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000444 int ret = -1;
445
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 datalen = skb->len - CAPIMSG_LEN(skb->data);
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000447
448 tty = tty_port_tty_get(&mp->port);
449 if (!tty) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450#ifdef _DEBUG_DATAFLOW
451 printk(KERN_DEBUG "capi: currently no receiver\n");
452#endif
453 return -1;
454 }
455
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000456 ld = tty_ldisc_ref(tty);
457 if (!ld)
458 goto out1;
459
Alan Coxa352def2008-07-16 21:53:12 +0100460 if (ld->ops->receive_buf == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461#if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
462 printk(KERN_DEBUG "capi: ldisc has no receive_buf function\n");
463#endif
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000464 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 }
466 if (mp->ttyinstop) {
467#if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
468 printk(KERN_DEBUG "capi: recv tty throttled\n");
469#endif
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000470 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 }
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000472 if (tty->receive_room < datalen) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473#if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
474 printk(KERN_DEBUG "capi: no room in tty\n");
475#endif
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000476 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 }
Harvey Harrison2f9e9b62008-04-28 02:14:37 -0700478 if ((nskb = gen_data_b3_resp_for(mp, skb)) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 printk(KERN_ERR "capi: gen_data_b3_resp failed\n");
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000480 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 }
482 datahandle = CAPIMSG_U16(skb->data,CAPIMSG_BASELEN+4);
483 errcode = capi20_put_message(mp->ap, nskb);
484 if (errcode != CAPI_NOERROR) {
485 printk(KERN_ERR "capi: send DATA_B3_RESP failed=%x\n",
486 errcode);
487 kfree_skb(nskb);
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000488 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 }
490 (void)skb_pull(skb, CAPIMSG_LEN(skb->data));
491#ifdef _DEBUG_DATAFLOW
492 printk(KERN_DEBUG "capi: DATA_B3_RESP %u len=%d => ldisc\n",
493 datahandle, skb->len);
494#endif
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000495 ld->ops->receive_buf(tty, skb->data, NULL, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 kfree_skb(skb);
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000497 ret = 0;
498out2:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 tty_ldisc_deref(ld);
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000500out1:
501 tty_kref_put(tty);
502 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503}
504
505static void handle_minor_recv(struct capiminor *mp)
506{
507 struct sk_buff *skb;
Harvey Harrison2f9e9b62008-04-28 02:14:37 -0700508 while ((skb = skb_dequeue(&mp->inqueue)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 unsigned int len = skb->len;
510 mp->inbytes -= len;
511 if (handle_recv_skb(mp, skb) < 0) {
512 skb_queue_head(&mp->inqueue, skb);
513 mp->inbytes += len;
514 return;
515 }
516 }
517}
518
519static int handle_minor_send(struct capiminor *mp)
520{
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000521 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 struct sk_buff *skb;
523 u16 len;
524 int count = 0;
525 u16 errcode;
526 u16 datahandle;
527
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000528 tty = tty_port_tty_get(&mp->port);
529 if (!tty)
530 return 0;
531
532 if (mp->ttyoutstop) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533#if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
534 printk(KERN_DEBUG "capi: send: tty stopped\n");
535#endif
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000536 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 return 0;
538 }
539
Harvey Harrison2f9e9b62008-04-28 02:14:37 -0700540 while ((skb = skb_dequeue(&mp->outqueue)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 datahandle = mp->datahandle;
542 len = (u16)skb->len;
543 skb_push(skb, CAPI_DATA_B3_REQ_LEN);
544 memset(skb->data, 0, CAPI_DATA_B3_REQ_LEN);
545 capimsg_setu16(skb->data, 0, CAPI_DATA_B3_REQ_LEN);
546 capimsg_setu16(skb->data, 2, mp->ap->applid);
547 capimsg_setu8 (skb->data, 4, CAPI_DATA_B3);
548 capimsg_setu8 (skb->data, 5, CAPI_REQ);
549 capimsg_setu16(skb->data, 6, mp->msgid++);
550 capimsg_setu32(skb->data, 8, mp->ncci); /* NCCI */
Andrew Morton5e6c20a2007-07-17 04:04:22 -0700551 capimsg_setu32(skb->data, 12, (u32)(long)skb->data);/* Data32 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 capimsg_setu16(skb->data, 16, len); /* Data length */
553 capimsg_setu16(skb->data, 18, datahandle);
554 capimsg_setu16(skb->data, 20, 0); /* Flags */
555
Jan Kiszka501c87a2010-02-08 10:12:16 +0000556 if (capiminor_add_ack(mp, datahandle) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 skb_pull(skb, CAPI_DATA_B3_REQ_LEN);
558 skb_queue_head(&mp->outqueue, skb);
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000559 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 return count;
561 }
562 errcode = capi20_put_message(mp->ap, skb);
563 if (errcode == CAPI_NOERROR) {
564 mp->datahandle++;
565 count++;
566 mp->outbytes -= len;
567#ifdef _DEBUG_DATAFLOW
568 printk(KERN_DEBUG "capi: DATA_B3_REQ %u len=%u\n",
569 datahandle, len);
570#endif
571 continue;
572 }
573 capiminor_del_ack(mp, datahandle);
574
575 if (errcode == CAPI_SENDQUEUEFULL) {
576 skb_pull(skb, CAPI_DATA_B3_REQ_LEN);
577 skb_queue_head(&mp->outqueue, skb);
578 break;
579 }
580
581 /* ups, drop packet */
582 printk(KERN_ERR "capi: put_message = %x\n", errcode);
583 mp->outbytes -= len;
584 kfree_skb(skb);
585 }
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000586 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 return count;
588}
589
590#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
591/* -------- function called by lower level -------------------------- */
592
593static void capi_recv_message(struct capi20_appl *ap, struct sk_buff *skb)
594{
595 struct capidev *cdev = ap->private;
596#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000597 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 struct capiminor *mp;
599 u16 datahandle;
600#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
601 struct capincci *np;
Michael Buesch053b47f2007-02-12 00:53:26 -0800602 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
Jan Kiszka05b41492010-02-08 10:12:19 +0000604 mutex_lock(&cdev->lock);
605
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_CONF) {
607 u16 info = CAPIMSG_U16(skb->data, 12); // Info field
Jan Kiszka05b41492010-02-08 10:12:19 +0000608 if ((info & 0xff00) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 }
Jan Kiszka05b41492010-02-08 10:12:19 +0000611 if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_IND)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
Jan Kiszka05b41492010-02-08 10:12:19 +0000613
Michael Buesch053b47f2007-02-12 00:53:26 -0800614 spin_lock_irqsave(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 if (CAPIMSG_COMMAND(skb->data) != CAPI_DATA_B3) {
616 skb_queue_tail(&cdev->recvqueue, skb);
617 wake_up_interruptible(&cdev->recvwait);
Jan Kiszka05b41492010-02-08 10:12:19 +0000618 goto unlock_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 }
Jan Kiszka05b41492010-02-08 10:12:19 +0000620
621 np = capincci_find(cdev, CAPIMSG_CONTROL(skb->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 if (!np) {
623 printk(KERN_ERR "BUG: capi_signal: ncci not found\n");
624 skb_queue_tail(&cdev->recvqueue, skb);
625 wake_up_interruptible(&cdev->recvwait);
Jan Kiszka05b41492010-02-08 10:12:19 +0000626 goto unlock_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 }
Jan Kiszka501c87a2010-02-08 10:12:16 +0000628
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629#ifndef CONFIG_ISDN_CAPI_MIDDLEWARE
630 skb_queue_tail(&cdev->recvqueue, skb);
631 wake_up_interruptible(&cdev->recvwait);
Jan Kiszka501c87a2010-02-08 10:12:16 +0000632
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633#else /* CONFIG_ISDN_CAPI_MIDDLEWARE */
Jan Kiszka501c87a2010-02-08 10:12:16 +0000634
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 mp = np->minorp;
636 if (!mp) {
637 skb_queue_tail(&cdev->recvqueue, skb);
638 wake_up_interruptible(&cdev->recvwait);
Jan Kiszka05b41492010-02-08 10:12:19 +0000639 goto unlock_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_IND) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4+4+2);
643#ifdef _DEBUG_DATAFLOW
644 printk(KERN_DEBUG "capi_signal: DATA_B3_IND %u len=%d\n",
645 datahandle, skb->len-CAPIMSG_LEN(skb->data));
646#endif
647 skb_queue_tail(&mp->inqueue, skb);
648 mp->inbytes += skb->len;
649 handle_minor_recv(mp);
650
651 } else if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_CONF) {
652
653 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4);
654#ifdef _DEBUG_DATAFLOW
655 printk(KERN_DEBUG "capi_signal: DATA_B3_CONF %u 0x%x\n",
656 datahandle,
657 CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4+2));
658#endif
659 kfree_skb(skb);
660 (void)capiminor_del_ack(mp, datahandle);
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000661 tty = tty_port_tty_get(&mp->port);
662 if (tty) {
663 tty_wakeup(tty);
664 tty_kref_put(tty);
665 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 (void)handle_minor_send(mp);
667
668 } else {
669 /* ups, let capi application handle it :-) */
670 skb_queue_tail(&cdev->recvqueue, skb);
671 wake_up_interruptible(&cdev->recvwait);
672 }
673#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
Jan Kiszka501c87a2010-02-08 10:12:16 +0000674
Jan Kiszka05b41492010-02-08 10:12:19 +0000675unlock_out:
Michael Buesch053b47f2007-02-12 00:53:26 -0800676 spin_unlock_irqrestore(&workaround_lock, flags);
Jan Kiszka05b41492010-02-08 10:12:19 +0000677 mutex_unlock(&cdev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678}
679
680/* -------- file_operations for capidev ----------------------------- */
681
682static ssize_t
683capi_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
684{
685 struct capidev *cdev = (struct capidev *)file->private_data;
686 struct sk_buff *skb;
687 size_t copied;
Jan Kiszka28a1dbb2010-02-08 10:12:21 +0000688 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
690 if (!cdev->ap.applid)
691 return -ENODEV;
692
Jan Kiszka28a1dbb2010-02-08 10:12:21 +0000693 skb = skb_dequeue(&cdev->recvqueue);
694 if (!skb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 if (file->f_flags & O_NONBLOCK)
696 return -EAGAIN;
Jan Kiszka28a1dbb2010-02-08 10:12:21 +0000697 err = wait_event_interruptible(cdev->recvwait,
698 (skb = skb_dequeue(&cdev->recvqueue)));
699 if (err)
700 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 }
702 if (skb->len > count) {
703 skb_queue_head(&cdev->recvqueue, skb);
704 return -EMSGSIZE;
705 }
706 if (copy_to_user(buf, skb->data, skb->len)) {
707 skb_queue_head(&cdev->recvqueue, skb);
708 return -EFAULT;
709 }
710 copied = skb->len;
711
712 kfree_skb(skb);
713
714 return copied;
715}
716
717static ssize_t
718capi_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
719{
720 struct capidev *cdev = (struct capidev *)file->private_data;
721 struct sk_buff *skb;
722 u16 mlen;
723
724 if (!cdev->ap.applid)
725 return -ENODEV;
726
727 skb = alloc_skb(count, GFP_USER);
728 if (!skb)
729 return -ENOMEM;
730
731 if (copy_from_user(skb_put(skb, count), buf, count)) {
732 kfree_skb(skb);
733 return -EFAULT;
734 }
735 mlen = CAPIMSG_LEN(skb->data);
736 if (CAPIMSG_CMD(skb->data) == CAPI_DATA_B3_REQ) {
737 if ((size_t)(mlen + CAPIMSG_DATALEN(skb->data)) != count) {
738 kfree_skb(skb);
739 return -EINVAL;
740 }
741 } else {
742 if (mlen != count) {
743 kfree_skb(skb);
744 return -EINVAL;
745 }
746 }
747 CAPIMSG_SETAPPID(skb->data, cdev->ap.applid);
748
749 if (CAPIMSG_CMD(skb->data) == CAPI_DISCONNECT_B3_RESP) {
Jan Kiszka05b41492010-02-08 10:12:19 +0000750 mutex_lock(&cdev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 capincci_free(cdev, CAPIMSG_NCCI(skb->data));
Jan Kiszka05b41492010-02-08 10:12:19 +0000752 mutex_unlock(&cdev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 }
754
755 cdev->errcode = capi20_put_message(&cdev->ap, skb);
756
757 if (cdev->errcode) {
758 kfree_skb(skb);
759 return -EIO;
760 }
761 return count;
762}
763
764static unsigned int
765capi_poll(struct file *file, poll_table * wait)
766{
767 struct capidev *cdev = (struct capidev *)file->private_data;
768 unsigned int mask = 0;
769
770 if (!cdev->ap.applid)
771 return POLLERR;
772
773 poll_wait(file, &(cdev->recvwait), wait);
774 mask = POLLOUT | POLLWRNORM;
775 if (!skb_queue_empty(&cdev->recvqueue))
776 mask |= POLLIN | POLLRDNORM;
777 return mask;
778}
779
780static int
781capi_ioctl(struct inode *inode, struct file *file,
782 unsigned int cmd, unsigned long arg)
783{
784 struct capidev *cdev = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 capi_ioctl_struct data;
786 int retval = -EINVAL;
787 void __user *argp = (void __user *)arg;
788
789 switch (cmd) {
790 case CAPI_REGISTER:
Jan Kiszka05b41492010-02-08 10:12:19 +0000791 mutex_lock(&cdev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
Jan Kiszka05b41492010-02-08 10:12:19 +0000793 if (cdev->ap.applid) {
794 retval = -EEXIST;
795 goto register_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 }
Jan Kiszka05b41492010-02-08 10:12:19 +0000797 if (copy_from_user(&cdev->ap.rparam, argp,
798 sizeof(struct capi_register_params))) {
799 retval = -EFAULT;
800 goto register_out;
801 }
802 cdev->ap.private = cdev;
803 cdev->ap.recv_message = capi_recv_message;
804 cdev->errcode = capi20_register(&cdev->ap);
805 retval = (int)cdev->ap.applid;
806 if (cdev->errcode) {
807 cdev->ap.applid = 0;
808 retval = -EIO;
809 }
810
811register_out:
812 mutex_unlock(&cdev->lock);
813 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
815 case CAPI_GET_VERSION:
816 {
817 if (copy_from_user(&data.contr, argp,
818 sizeof(data.contr)))
819 return -EFAULT;
820 cdev->errcode = capi20_get_version(data.contr, &data.version);
821 if (cdev->errcode)
822 return -EIO;
823 if (copy_to_user(argp, &data.version,
824 sizeof(data.version)))
825 return -EFAULT;
826 }
827 return 0;
828
829 case CAPI_GET_SERIAL:
830 {
831 if (copy_from_user(&data.contr, argp,
832 sizeof(data.contr)))
833 return -EFAULT;
834 cdev->errcode = capi20_get_serial (data.contr, data.serial);
835 if (cdev->errcode)
836 return -EIO;
837 if (copy_to_user(argp, data.serial,
838 sizeof(data.serial)))
839 return -EFAULT;
840 }
841 return 0;
842 case CAPI_GET_PROFILE:
843 {
844 if (copy_from_user(&data.contr, argp,
845 sizeof(data.contr)))
846 return -EFAULT;
847
848 if (data.contr == 0) {
849 cdev->errcode = capi20_get_profile(data.contr, &data.profile);
850 if (cdev->errcode)
851 return -EIO;
852
853 retval = copy_to_user(argp,
854 &data.profile.ncontroller,
855 sizeof(data.profile.ncontroller));
856
857 } else {
858 cdev->errcode = capi20_get_profile(data.contr, &data.profile);
859 if (cdev->errcode)
860 return -EIO;
861
862 retval = copy_to_user(argp, &data.profile,
863 sizeof(data.profile));
864 }
865 if (retval)
866 return -EFAULT;
867 }
868 return 0;
869
870 case CAPI_GET_MANUFACTURER:
871 {
872 if (copy_from_user(&data.contr, argp,
873 sizeof(data.contr)))
874 return -EFAULT;
875 cdev->errcode = capi20_get_manufacturer(data.contr, data.manufacturer);
876 if (cdev->errcode)
877 return -EIO;
878
879 if (copy_to_user(argp, data.manufacturer,
880 sizeof(data.manufacturer)))
881 return -EFAULT;
882
883 }
884 return 0;
885 case CAPI_GET_ERRCODE:
886 data.errcode = cdev->errcode;
887 cdev->errcode = CAPI_NOERROR;
888 if (arg) {
889 if (copy_to_user(argp, &data.errcode,
890 sizeof(data.errcode)))
891 return -EFAULT;
892 }
893 return data.errcode;
894
895 case CAPI_INSTALLED:
896 if (capi20_isinstalled() == CAPI_NOERROR)
897 return 0;
898 return -ENXIO;
899
900 case CAPI_MANUFACTURER_CMD:
901 {
902 struct capi_manufacturer_cmd mcmd;
903 if (!capable(CAP_SYS_ADMIN))
904 return -EPERM;
905 if (copy_from_user(&mcmd, argp, sizeof(mcmd)))
906 return -EFAULT;
907 return capi20_manufacturer(mcmd.cmd, mcmd.data);
908 }
909 return 0;
910
911 case CAPI_SET_FLAGS:
Jan Kiszka05b41492010-02-08 10:12:19 +0000912 case CAPI_CLR_FLAGS: {
913 unsigned userflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
Jan Kiszka05b41492010-02-08 10:12:19 +0000915 if (copy_from_user(&userflags, argp, sizeof(userflags)))
916 return -EFAULT;
917
918 mutex_lock(&cdev->lock);
919 if (cmd == CAPI_SET_FLAGS)
920 cdev->userflags |= userflags;
921 else
922 cdev->userflags &= ~userflags;
923 mutex_unlock(&cdev->lock);
924 return 0;
925 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 case CAPI_GET_FLAGS:
927 if (copy_to_user(argp, &cdev->userflags,
928 sizeof(cdev->userflags)))
929 return -EFAULT;
930 return 0;
931
Jan Kiszka05b41492010-02-08 10:12:19 +0000932 case CAPI_NCCI_OPENCOUNT: {
933 struct capincci *nccip;
934 unsigned ncci;
935 int count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
Jan Kiszka05b41492010-02-08 10:12:19 +0000937 if (copy_from_user(&ncci, argp, sizeof(ncci)))
938 return -EFAULT;
939
940 mutex_lock(&cdev->lock);
941 nccip = capincci_find(cdev, (u32)ncci);
942 if (nccip)
943 count = capincci_minor_opencount(nccip);
944 mutex_unlock(&cdev->lock);
945 return count;
946 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
948#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
Jan Kiszka05b41492010-02-08 10:12:19 +0000949 case CAPI_NCCI_GETUNIT: {
950 struct capincci *nccip;
951 struct capiminor *mp;
952 unsigned ncci;
953 int unit = -ESRCH;
954
955 if (copy_from_user(&ncci, argp, sizeof(ncci)))
956 return -EFAULT;
957
958 mutex_lock(&cdev->lock);
959 nccip = capincci_find(cdev, (u32)ncci);
960 if (nccip) {
961 mp = nccip->minorp;
962 if (mp)
963 unit = mp->minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 }
Jan Kiszka05b41492010-02-08 10:12:19 +0000965 mutex_unlock(&cdev->lock);
966 return unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 }
Jan Kiszka05b41492010-02-08 10:12:19 +0000968#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
969
970 default:
971 return -EINVAL;
972 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973}
974
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000975static int capi_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976{
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000977 struct capidev *cdev;
978
979 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
980 if (!cdev)
981 return -ENOMEM;
982
Jan Kiszka05b41492010-02-08 10:12:19 +0000983 mutex_init(&cdev->lock);
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000984 skb_queue_head_init(&cdev->recvqueue);
985 init_waitqueue_head(&cdev->recvwait);
Jan Kiszka884f5c42010-02-08 10:12:22 +0000986 INIT_LIST_HEAD(&cdev->nccis);
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000987 file->private_data = cdev;
988
989 mutex_lock(&capidev_list_lock);
990 list_add_tail(&cdev->list, &capidev_list);
991 mutex_unlock(&capidev_list_lock);
992
993 return nonseekable_open(inode, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994}
995
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000996static int capi_release(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997{
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000998 struct capidev *cdev = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
Jan Kiszkaeca39dd2010-02-08 10:12:18 +00001000 mutex_lock(&capidev_list_lock);
1001 list_del(&cdev->list);
1002 mutex_unlock(&capidev_list_lock);
1003
Jan Kiszka05b41492010-02-08 10:12:19 +00001004 if (cdev->ap.applid)
Jan Kiszkaeca39dd2010-02-08 10:12:18 +00001005 capi20_release(&cdev->ap);
Jan Kiszkaeca39dd2010-02-08 10:12:18 +00001006 skb_queue_purge(&cdev->recvqueue);
Jan Kiszkaeca39dd2010-02-08 10:12:18 +00001007 capincci_free(cdev, 0xffffffff);
Jan Kiszkaeca39dd2010-02-08 10:12:18 +00001008
1009 kfree(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 return 0;
1011}
1012
Arjan van de Ven2b8693c2007-02-12 00:55:32 -08001013static const struct file_operations capi_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014{
1015 .owner = THIS_MODULE,
1016 .llseek = no_llseek,
1017 .read = capi_read,
1018 .write = capi_write,
1019 .poll = capi_poll,
1020 .ioctl = capi_ioctl,
1021 .open = capi_open,
1022 .release = capi_release,
1023};
1024
1025#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1026/* -------- tty_operations for capincci ----------------------------- */
1027
Jan Kiszka46324512010-02-08 10:12:28 +00001028static int
1029capinc_tty_install(struct tty_driver *driver, struct tty_struct *tty)
1030{
1031 int idx = tty->index;
1032 struct capiminor *mp = capiminor_get(idx);
1033 int ret = tty_init_termios(tty);
1034
1035 if (ret == 0) {
1036 tty_driver_kref_get(driver);
1037 tty->count++;
1038 tty->driver_data = mp;
1039 driver->ttys[idx] = tty;
1040 } else
1041 capiminor_put(mp);
1042 return ret;
1043}
1044
1045static void capinc_tty_cleanup(struct tty_struct *tty)
1046{
1047 struct capiminor *mp = tty->driver_data;
1048 tty->driver_data = NULL;
1049 capiminor_put(mp);
1050}
1051
Jan Kiszkafb4b4882010-02-08 10:12:29 +00001052static int capinc_tty_open(struct tty_struct *tty, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053{
Jan Kiszka46324512010-02-08 10:12:28 +00001054 struct capiminor *mp = tty->driver_data;
Michael Buesch053b47f2007-02-12 00:53:26 -08001055 unsigned long flags;
Jan Kiszkafb4b4882010-02-08 10:12:29 +00001056 int err;
1057
1058 err = tty_port_open(&mp->port, tty, filp);
1059 if (err)
1060 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
Michael Buesch053b47f2007-02-12 00:53:26 -08001062 spin_lock_irqsave(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 handle_minor_recv(mp);
Michael Buesch053b47f2007-02-12 00:53:26 -08001064 spin_unlock_irqrestore(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 return 0;
1066}
1067
Jan Kiszkafb4b4882010-02-08 10:12:29 +00001068static void capinc_tty_close(struct tty_struct *tty, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069{
Jan Kiszka46324512010-02-08 10:12:28 +00001070 struct capiminor *mp = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
Jan Kiszkafb4b4882010-02-08 10:12:29 +00001072 tty_port_close(&mp->port, tty, filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073}
1074
Jan Kiszka6576c282010-02-08 10:12:32 +00001075static int capinc_tty_write(struct tty_struct *tty,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 const unsigned char *buf, int count)
1077{
Jan Kiszka2c8df722010-02-08 10:12:30 +00001078 struct capiminor *mp = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 struct sk_buff *skb;
Michael Buesch053b47f2007-02-12 00:53:26 -08001080 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
1082#ifdef _DEBUG_TTYFUNCS
1083 printk(KERN_DEBUG "capinc_tty_write(count=%d)\n", count);
1084#endif
1085
Michael Buesch053b47f2007-02-12 00:53:26 -08001086 spin_lock_irqsave(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 skb = mp->ttyskb;
1088 if (skb) {
1089 mp->ttyskb = NULL;
1090 skb_queue_tail(&mp->outqueue, skb);
1091 mp->outbytes += skb->len;
1092 }
1093
1094 skb = alloc_skb(CAPI_DATA_B3_REQ_LEN+count, GFP_ATOMIC);
1095 if (!skb) {
1096 printk(KERN_ERR "capinc_tty_write: alloc_skb failed\n");
Michael Buesch053b47f2007-02-12 00:53:26 -08001097 spin_unlock_irqrestore(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 return -ENOMEM;
1099 }
1100
1101 skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
1102 memcpy(skb_put(skb, count), buf, count);
1103
1104 skb_queue_tail(&mp->outqueue, skb);
1105 mp->outbytes += skb->len;
1106 (void)handle_minor_send(mp);
1107 (void)handle_minor_recv(mp);
Michael Buesch053b47f2007-02-12 00:53:26 -08001108 spin_unlock_irqrestore(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 return count;
1110}
1111
Alan Coxf2545a72008-04-30 00:54:09 -07001112static int capinc_tty_put_char(struct tty_struct *tty, unsigned char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113{
Jan Kiszka2c8df722010-02-08 10:12:30 +00001114 struct capiminor *mp = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 struct sk_buff *skb;
Michael Buesch053b47f2007-02-12 00:53:26 -08001116 unsigned long flags;
Alan Coxf2545a72008-04-30 00:54:09 -07001117 int ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
1119#ifdef _DEBUG_TTYFUNCS
1120 printk(KERN_DEBUG "capinc_put_char(%u)\n", ch);
1121#endif
1122
Michael Buesch053b47f2007-02-12 00:53:26 -08001123 spin_lock_irqsave(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 skb = mp->ttyskb;
1125 if (skb) {
1126 if (skb_tailroom(skb) > 0) {
1127 *(skb_put(skb, 1)) = ch;
Michael Buesch053b47f2007-02-12 00:53:26 -08001128 spin_unlock_irqrestore(&workaround_lock, flags);
Alan Coxf2545a72008-04-30 00:54:09 -07001129 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 }
1131 mp->ttyskb = NULL;
1132 skb_queue_tail(&mp->outqueue, skb);
1133 mp->outbytes += skb->len;
1134 (void)handle_minor_send(mp);
1135 }
1136 skb = alloc_skb(CAPI_DATA_B3_REQ_LEN+CAPI_MAX_BLKSIZE, GFP_ATOMIC);
1137 if (skb) {
1138 skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
1139 *(skb_put(skb, 1)) = ch;
1140 mp->ttyskb = skb;
1141 } else {
1142 printk(KERN_ERR "capinc_put_char: char %u lost\n", ch);
Alan Coxf2545a72008-04-30 00:54:09 -07001143 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 }
Michael Buesch053b47f2007-02-12 00:53:26 -08001145 spin_unlock_irqrestore(&workaround_lock, flags);
Alan Coxf2545a72008-04-30 00:54:09 -07001146 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147}
1148
1149static void capinc_tty_flush_chars(struct tty_struct *tty)
1150{
Jan Kiszka2c8df722010-02-08 10:12:30 +00001151 struct capiminor *mp = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 struct sk_buff *skb;
Michael Buesch053b47f2007-02-12 00:53:26 -08001153 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154
1155#ifdef _DEBUG_TTYFUNCS
1156 printk(KERN_DEBUG "capinc_tty_flush_chars\n");
1157#endif
1158
Michael Buesch053b47f2007-02-12 00:53:26 -08001159 spin_lock_irqsave(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 skb = mp->ttyskb;
1161 if (skb) {
1162 mp->ttyskb = NULL;
1163 skb_queue_tail(&mp->outqueue, skb);
1164 mp->outbytes += skb->len;
1165 (void)handle_minor_send(mp);
1166 }
1167 (void)handle_minor_recv(mp);
Michael Buesch053b47f2007-02-12 00:53:26 -08001168 spin_unlock_irqrestore(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169}
1170
1171static int capinc_tty_write_room(struct tty_struct *tty)
1172{
Jan Kiszka2c8df722010-02-08 10:12:30 +00001173 struct capiminor *mp = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 int room;
Jan Kiszka2c8df722010-02-08 10:12:30 +00001175
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 room = CAPINC_MAX_SENDQUEUE-skb_queue_len(&mp->outqueue);
1177 room *= CAPI_MAX_BLKSIZE;
1178#ifdef _DEBUG_TTYFUNCS
1179 printk(KERN_DEBUG "capinc_tty_write_room = %d\n", room);
1180#endif
1181 return room;
1182}
1183
Adrian Bunk408b6642005-05-01 08:59:29 -07001184static int capinc_tty_chars_in_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185{
Jan Kiszka2c8df722010-02-08 10:12:30 +00001186 struct capiminor *mp = tty->driver_data;
1187
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188#ifdef _DEBUG_TTYFUNCS
1189 printk(KERN_DEBUG "capinc_tty_chars_in_buffer = %d nack=%d sq=%d rq=%d\n",
1190 mp->outbytes, mp->nack,
1191 skb_queue_len(&mp->outqueue),
1192 skb_queue_len(&mp->inqueue));
1193#endif
1194 return mp->outbytes;
1195}
1196
1197static int capinc_tty_ioctl(struct tty_struct *tty, struct file * file,
1198 unsigned int cmd, unsigned long arg)
1199{
1200 int error = 0;
1201 switch (cmd) {
1202 default:
Stephen Rothwell53e86312008-10-13 10:44:33 +01001203 error = n_tty_ioctl_helper(tty, file, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 break;
1205 }
1206 return error;
1207}
1208
Alan Cox606d0992006-12-08 02:38:45 -08001209static void capinc_tty_set_termios(struct tty_struct *tty, struct ktermios * old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210{
1211#ifdef _DEBUG_TTYFUNCS
1212 printk(KERN_DEBUG "capinc_tty_set_termios\n");
1213#endif
1214}
1215
Jan Kiszka2c8df722010-02-08 10:12:30 +00001216static void capinc_tty_throttle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217{
Jan Kiszka2c8df722010-02-08 10:12:30 +00001218 struct capiminor *mp = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219#ifdef _DEBUG_TTYFUNCS
1220 printk(KERN_DEBUG "capinc_tty_throttle\n");
1221#endif
Jan Kiszka2c8df722010-02-08 10:12:30 +00001222 mp->ttyinstop = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223}
1224
Jan Kiszka2c8df722010-02-08 10:12:30 +00001225static void capinc_tty_unthrottle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226{
Jan Kiszka2c8df722010-02-08 10:12:30 +00001227 struct capiminor *mp = tty->driver_data;
Michael Buesch053b47f2007-02-12 00:53:26 -08001228 unsigned long flags;
Jan Kiszka2c8df722010-02-08 10:12:30 +00001229
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230#ifdef _DEBUG_TTYFUNCS
1231 printk(KERN_DEBUG "capinc_tty_unthrottle\n");
1232#endif
Jan Kiszka2c8df722010-02-08 10:12:30 +00001233 spin_lock_irqsave(&workaround_lock, flags);
1234 mp->ttyinstop = 0;
1235 handle_minor_recv(mp);
1236 spin_unlock_irqrestore(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237}
1238
1239static void capinc_tty_stop(struct tty_struct *tty)
1240{
Jan Kiszka2c8df722010-02-08 10:12:30 +00001241 struct capiminor *mp = tty->driver_data;
1242
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243#ifdef _DEBUG_TTYFUNCS
1244 printk(KERN_DEBUG "capinc_tty_stop\n");
1245#endif
Jan Kiszka2c8df722010-02-08 10:12:30 +00001246 mp->ttyoutstop = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247}
1248
1249static void capinc_tty_start(struct tty_struct *tty)
1250{
Jan Kiszka2c8df722010-02-08 10:12:30 +00001251 struct capiminor *mp = tty->driver_data;
Michael Buesch053b47f2007-02-12 00:53:26 -08001252 unsigned long flags;
Jan Kiszka2c8df722010-02-08 10:12:30 +00001253
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254#ifdef _DEBUG_TTYFUNCS
1255 printk(KERN_DEBUG "capinc_tty_start\n");
1256#endif
Jan Kiszka2c8df722010-02-08 10:12:30 +00001257 spin_lock_irqsave(&workaround_lock, flags);
1258 mp->ttyoutstop = 0;
1259 (void)handle_minor_send(mp);
1260 spin_unlock_irqrestore(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261}
1262
1263static void capinc_tty_hangup(struct tty_struct *tty)
1264{
Jan Kiszkafb4b4882010-02-08 10:12:29 +00001265 struct capiminor *mp = tty->driver_data;
1266
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267#ifdef _DEBUG_TTYFUNCS
1268 printk(KERN_DEBUG "capinc_tty_hangup\n");
1269#endif
Jan Kiszkafb4b4882010-02-08 10:12:29 +00001270 tty_port_hangup(&mp->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271}
1272
Alan Cox9e989662008-07-22 11:18:03 +01001273static int capinc_tty_break_ctl(struct tty_struct *tty, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274{
1275#ifdef _DEBUG_TTYFUNCS
1276 printk(KERN_DEBUG "capinc_tty_break_ctl(%d)\n", state);
1277#endif
Alan Cox9e989662008-07-22 11:18:03 +01001278 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279}
1280
1281static void capinc_tty_flush_buffer(struct tty_struct *tty)
1282{
1283#ifdef _DEBUG_TTYFUNCS
1284 printk(KERN_DEBUG "capinc_tty_flush_buffer\n");
1285#endif
1286}
1287
1288static void capinc_tty_set_ldisc(struct tty_struct *tty)
1289{
1290#ifdef _DEBUG_TTYFUNCS
1291 printk(KERN_DEBUG "capinc_tty_set_ldisc\n");
1292#endif
1293}
1294
1295static void capinc_tty_send_xchar(struct tty_struct *tty, char ch)
1296{
1297#ifdef _DEBUG_TTYFUNCS
1298 printk(KERN_DEBUG "capinc_tty_send_xchar(%d)\n", ch);
1299#endif
1300}
1301
Jeff Dikeb68e31d2006-10-02 02:17:18 -07001302static const struct tty_operations capinc_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 .open = capinc_tty_open,
1304 .close = capinc_tty_close,
1305 .write = capinc_tty_write,
1306 .put_char = capinc_tty_put_char,
1307 .flush_chars = capinc_tty_flush_chars,
1308 .write_room = capinc_tty_write_room,
1309 .chars_in_buffer = capinc_tty_chars_in_buffer,
1310 .ioctl = capinc_tty_ioctl,
1311 .set_termios = capinc_tty_set_termios,
1312 .throttle = capinc_tty_throttle,
1313 .unthrottle = capinc_tty_unthrottle,
1314 .stop = capinc_tty_stop,
1315 .start = capinc_tty_start,
1316 .hangup = capinc_tty_hangup,
1317 .break_ctl = capinc_tty_break_ctl,
1318 .flush_buffer = capinc_tty_flush_buffer,
1319 .set_ldisc = capinc_tty_set_ldisc,
1320 .send_xchar = capinc_tty_send_xchar,
Jan Kiszka46324512010-02-08 10:12:28 +00001321 .install = capinc_tty_install,
1322 .cleanup = capinc_tty_cleanup,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323};
1324
Jan Kiszkae76b1542010-02-08 10:12:24 +00001325static int __init capinc_tty_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326{
1327 struct tty_driver *drv;
Jan Kiszkae76b1542010-02-08 10:12:24 +00001328 int err;
1329
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 if (capi_ttyminors > CAPINC_MAX_PORTS)
1331 capi_ttyminors = CAPINC_MAX_PORTS;
1332 if (capi_ttyminors <= 0)
1333 capi_ttyminors = CAPINC_NR_PORTS;
1334
Jan Kiszka81d17fe2010-02-08 10:12:23 +00001335 capiminors = kzalloc(sizeof(struct capi_minor *) * capi_ttyminors,
1336 GFP_KERNEL);
1337 if (!capiminors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 return -ENOMEM;
1339
Jan Kiszka81d17fe2010-02-08 10:12:23 +00001340 drv = alloc_tty_driver(capi_ttyminors);
1341 if (!drv) {
1342 kfree(capiminors);
1343 return -ENOMEM;
1344 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 drv->owner = THIS_MODULE;
1346 drv->driver_name = "capi_nc";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 drv->name = "capi";
Jan Kiszkae95ac142010-02-08 10:12:26 +00001348 drv->major = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 drv->minor_start = 0;
1350 drv->type = TTY_DRIVER_TYPE_SERIAL;
1351 drv->subtype = SERIAL_TYPE_NORMAL;
1352 drv->init_termios = tty_std_termios;
1353 drv->init_termios.c_iflag = ICRNL;
1354 drv->init_termios.c_oflag = OPOST | ONLCR;
1355 drv->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1356 drv->init_termios.c_lflag = 0;
Jan Kiszka40fb2d02010-02-08 10:12:25 +00001357 drv->flags =
1358 TTY_DRIVER_REAL_RAW | TTY_DRIVER_RESET_TERMIOS |
1359 TTY_DRIVER_DYNAMIC_DEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 tty_set_operations(drv, &capinc_ops);
Jan Kiszkae76b1542010-02-08 10:12:24 +00001361
1362 err = tty_register_driver(drv);
1363 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 put_tty_driver(drv);
Jan Kiszka81d17fe2010-02-08 10:12:23 +00001365 kfree(capiminors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 printk(KERN_ERR "Couldn't register capi_nc driver\n");
Jan Kiszkae76b1542010-02-08 10:12:24 +00001367 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 }
1369 capinc_tty_driver = drv;
1370 return 0;
1371}
1372
Jan Kiszkae76b1542010-02-08 10:12:24 +00001373static void __exit capinc_tty_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374{
Jan Kiszkae76b1542010-02-08 10:12:24 +00001375 tty_unregister_driver(capinc_tty_driver);
1376 put_tty_driver(capinc_tty_driver);
Jan Kiszka81d17fe2010-02-08 10:12:23 +00001377 kfree(capiminors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378}
1379
Jan Kiszka501c87a2010-02-08 10:12:16 +00001380#else /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
1381
1382static inline int capinc_tty_init(void)
1383{
1384 return 0;
1385}
1386
1387static inline void capinc_tty_exit(void) { }
1388
1389#endif /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
1391/* -------- /proc functions ----------------------------------------- */
1392
1393/*
1394 * /proc/capi/capi20:
1395 * minor applid nrecvctlpkt nrecvdatapkt nsendctlpkt nsenddatapkt
1396 */
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001397static int capi20_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398{
1399 struct capidev *cdev;
1400 struct list_head *l;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
Jan Kiszkab8f433d2010-02-08 10:12:17 +00001402 mutex_lock(&capidev_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 list_for_each(l, &capidev_list) {
1404 cdev = list_entry(l, struct capidev, list);
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001405 seq_printf(m, "0 %d %lu %lu %lu %lu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 cdev->ap.applid,
1407 cdev->ap.nrecvctlpkt,
1408 cdev->ap.nrecvdatapkt,
1409 cdev->ap.nsentctlpkt,
1410 cdev->ap.nsentdatapkt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 }
Jan Kiszkab8f433d2010-02-08 10:12:17 +00001412 mutex_unlock(&capidev_list_lock);
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001413 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414}
1415
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001416static int capi20_proc_open(struct inode *inode, struct file *file)
1417{
1418 return single_open(file, capi20_proc_show, NULL);
1419}
1420
1421static const struct file_operations capi20_proc_fops = {
1422 .owner = THIS_MODULE,
1423 .open = capi20_proc_open,
1424 .read = seq_read,
1425 .llseek = seq_lseek,
1426 .release = single_release,
1427};
1428
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429/*
1430 * /proc/capi/capi20ncci:
1431 * applid ncci
1432 */
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001433static int capi20ncci_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434{
Jan Kiszka884f5c42010-02-08 10:12:22 +00001435 struct capidev *cdev;
1436 struct capincci *np;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
Jan Kiszkab8f433d2010-02-08 10:12:17 +00001438 mutex_lock(&capidev_list_lock);
Jan Kiszka884f5c42010-02-08 10:12:22 +00001439 list_for_each_entry(cdev, &capidev_list, list) {
Jan Kiszka05b41492010-02-08 10:12:19 +00001440 mutex_lock(&cdev->lock);
Jan Kiszka884f5c42010-02-08 10:12:22 +00001441 list_for_each_entry(np, &cdev->nccis, list)
1442 seq_printf(m, "%d 0x%x\n", cdev->ap.applid, np->ncci);
Jan Kiszka05b41492010-02-08 10:12:19 +00001443 mutex_unlock(&cdev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 }
Jan Kiszkab8f433d2010-02-08 10:12:17 +00001445 mutex_unlock(&capidev_list_lock);
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001446 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447}
1448
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001449static int capi20ncci_proc_open(struct inode *inode, struct file *file)
1450{
1451 return single_open(file, capi20ncci_proc_show, NULL);
1452}
1453
1454static const struct file_operations capi20ncci_proc_fops = {
1455 .owner = THIS_MODULE,
1456 .open = capi20ncci_proc_open,
1457 .read = seq_read,
1458 .llseek = seq_lseek,
1459 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460};
1461
1462static void __init proc_init(void)
1463{
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001464 proc_create("capi/capi20", 0, NULL, &capi20_proc_fops);
1465 proc_create("capi/capi20ncci", 0, NULL, &capi20ncci_proc_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466}
1467
1468static void __exit proc_exit(void)
1469{
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001470 remove_proc_entry("capi/capi20", NULL);
1471 remove_proc_entry("capi/capi20ncci", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472}
1473
1474/* -------- init function and module interface ---------------------- */
1475
1476
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477static int __init capi_init(void)
1478{
Jan Kiszka88549d62010-02-08 10:12:09 +00001479 const char *compileinfo;
Andrew Morton6d9eac32006-03-28 01:56:19 -08001480 int major_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481
Andrew Morton6d9eac32006-03-28 01:56:19 -08001482 major_ret = register_chrdev(capi_major, "capi20", &capi_fops);
1483 if (major_ret < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 printk(KERN_ERR "capi20: unable to get major %d\n", capi_major);
Andrew Morton6d9eac32006-03-28 01:56:19 -08001485 return major_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 }
gregkh@suse.de56b22932005-03-23 10:01:41 -08001487 capi_class = class_create(THIS_MODULE, "capi");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 if (IS_ERR(capi_class)) {
1489 unregister_chrdev(capi_major, "capi20");
1490 return PTR_ERR(capi_class);
1491 }
1492
Greg Kroah-Hartmana9b12612008-07-21 20:03:34 -07001493 device_create(capi_class, NULL, MKDEV(capi_major, 0), NULL, "capi");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 if (capinc_tty_init() < 0) {
Tony Jonesd78b0362007-09-25 02:03:03 +02001496 device_destroy(capi_class, MKDEV(capi_major, 0));
gregkh@suse.de56b22932005-03-23 10:01:41 -08001497 class_destroy(capi_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 unregister_chrdev(capi_major, "capi20");
1499 return -ENOMEM;
1500 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
1502 proc_init();
1503
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504#if defined(CONFIG_ISDN_CAPI_CAPIFS) || defined(CONFIG_ISDN_CAPI_CAPIFS_MODULE)
1505 compileinfo = " (middleware+capifs)";
Jan Kiszka501c87a2010-02-08 10:12:16 +00001506#elif defined(CONFIG_ISDN_CAPI_MIDDLEWARE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 compileinfo = " (no capifs)";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508#else
1509 compileinfo = " (no middleware)";
1510#endif
Jan Kiszka88549d62010-02-08 10:12:09 +00001511 printk(KERN_NOTICE "CAPI 2.0 started up with major %d%s\n",
1512 capi_major, compileinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513
1514 return 0;
1515}
1516
1517static void __exit capi_exit(void)
1518{
1519 proc_exit();
1520
Tony Jonesd78b0362007-09-25 02:03:03 +02001521 device_destroy(capi_class, MKDEV(capi_major, 0));
gregkh@suse.de56b22932005-03-23 10:01:41 -08001522 class_destroy(capi_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 unregister_chrdev(capi_major, "capi20");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 capinc_tty_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526}
1527
1528module_init(capi_init);
1529module_exit(capi_exit);