blob: cf5e996ac448b2a0767145cc689865a1c17ecb75 [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
Jan Kiszka501c87a2010-02-08 10:12:16 +000063static int capi_ttyminors = CAPINC_NR_PORTS;
64
Linus Torvalds1da177e2005-04-16 15:20:36 -070065module_param_named(ttyminors, capi_ttyminors, uint, 0);
66#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
67
68/* -------- defines ------------------------------------------------- */
69
70#define CAPINC_MAX_RECVQUEUE 10
71#define CAPINC_MAX_SENDQUEUE 10
72#define CAPI_MAX_BLKSIZE 2048
73
74/* -------- data structures ----------------------------------------- */
75
76struct capidev;
77struct capincci;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078struct capiminor;
79
Michael Buesch6aa65472006-06-26 00:25:30 -070080struct datahandle_queue {
81 struct list_head list;
82 u16 datahandle;
83};
84
Linus Torvalds1da177e2005-04-16 15:20:36 -070085struct capiminor {
Jan Kiszka0159d542010-02-08 10:12:27 +000086 struct kref kref;
87
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 unsigned int minor;
Jan Kiszka90926f02010-02-08 10:12:06 +000089 struct dentry *capifs_dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
91 struct capi20_appl *ap;
92 u32 ncci;
93 u16 datahandle;
94 u16 msgid;
95
Jan Kiszkafb4b4882010-02-08 10:12:29 +000096 struct tty_port port;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 int ttyinstop;
98 int ttyoutstop;
99 struct sk_buff *ttyskb;
100 atomic_t ttyopencount;
101
102 struct sk_buff_head inqueue;
103 int inbytes;
104 struct sk_buff_head outqueue;
105 int outbytes;
106
107 /* transmit path */
Michael Buesch6aa65472006-06-26 00:25:30 -0700108 struct list_head ackqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 int nack;
Michael Buesch6aa65472006-06-26 00:25:30 -0700110 spinlock_t ackqlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Michael Buesch053b47f2007-02-12 00:53:26 -0800113/* FIXME: The following lock is a sledgehammer-workaround to a
114 * locking issue with the capiminor (and maybe other) data structure(s).
115 * Access to this data is done in a racy way and crashes the machine with
116 * a FritzCard DSL driver; sooner or later. This is a workaround
117 * which trades scalability vs stability, so it doesn't crash the kernel anymore.
118 * The correct (and scalable) fix for the issue seems to require
119 * an API change to the drivers... . */
120static DEFINE_SPINLOCK(workaround_lock);
121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122struct capincci {
Jan Kiszka884f5c42010-02-08 10:12:22 +0000123 struct list_head list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 u32 ncci;
125 struct capidev *cdev;
126#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
127 struct capiminor *minorp;
128#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
129};
130
131struct capidev {
132 struct list_head list;
133 struct capi20_appl ap;
134 u16 errcode;
135 unsigned userflags;
136
137 struct sk_buff_head recvqueue;
138 wait_queue_head_t recvwait;
139
Jan Kiszka884f5c42010-02-08 10:12:22 +0000140 struct list_head nccis;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Jan Kiszka05b41492010-02-08 10:12:19 +0000142 struct mutex lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143};
144
145/* -------- global variables ---------------------------------------- */
146
Jan Kiszkab8f433d2010-02-08 10:12:17 +0000147static DEFINE_MUTEX(capidev_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148static LIST_HEAD(capidev_list);
149
150#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
Jan Kiszka501c87a2010-02-08 10:12:16 +0000151
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000152static DEFINE_RWLOCK(capiminors_lock);
153static struct capiminor **capiminors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Jan Kiszka40fb2d02010-02-08 10:12:25 +0000155static struct tty_driver *capinc_tty_driver;
156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157/* -------- datahandles --------------------------------------------- */
158
Jan Kiszka501c87a2010-02-08 10:12:16 +0000159static int capiminor_add_ack(struct capiminor *mp, u16 datahandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160{
Michael Buesch6aa65472006-06-26 00:25:30 -0700161 struct datahandle_queue *n;
162 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
164 n = kmalloc(sizeof(*n), GFP_ATOMIC);
Michael Buesch6aa65472006-06-26 00:25:30 -0700165 if (unlikely(!n)) {
166 printk(KERN_ERR "capi: alloc datahandle failed\n");
167 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 n->datahandle = datahandle;
Michael Buesch6aa65472006-06-26 00:25:30 -0700170 INIT_LIST_HEAD(&n->list);
171 spin_lock_irqsave(&mp->ackqlock, flags);
172 list_add_tail(&n->list, &mp->ackqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 mp->nack++;
Michael Buesch6aa65472006-06-26 00:25:30 -0700174 spin_unlock_irqrestore(&mp->ackqlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 return 0;
176}
177
178static int capiminor_del_ack(struct capiminor *mp, u16 datahandle)
179{
Michael Buesch6aa65472006-06-26 00:25:30 -0700180 struct datahandle_queue *p, *tmp;
181 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Michael Buesch6aa65472006-06-26 00:25:30 -0700183 spin_lock_irqsave(&mp->ackqlock, flags);
184 list_for_each_entry_safe(p, tmp, &mp->ackqueue, list) {
185 if (p->datahandle == datahandle) {
186 list_del(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 kfree(p);
188 mp->nack--;
Michael Buesch6aa65472006-06-26 00:25:30 -0700189 spin_unlock_irqrestore(&mp->ackqlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 return 0;
191 }
192 }
Michael Buesch6aa65472006-06-26 00:25:30 -0700193 spin_unlock_irqrestore(&mp->ackqlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 return -1;
195}
196
197static void capiminor_del_all_ack(struct capiminor *mp)
198{
Michael Buesch6aa65472006-06-26 00:25:30 -0700199 struct datahandle_queue *p, *tmp;
200 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
Michael Buesch6aa65472006-06-26 00:25:30 -0700202 spin_lock_irqsave(&mp->ackqlock, flags);
203 list_for_each_entry_safe(p, tmp, &mp->ackqueue, list) {
204 list_del(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 kfree(p);
206 mp->nack--;
207 }
Michael Buesch6aa65472006-06-26 00:25:30 -0700208 spin_unlock_irqrestore(&mp->ackqlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209}
210
211
212/* -------- struct capiminor ---------------------------------------- */
213
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000214static const struct tty_port_operations capiminor_port_ops; /* we have none */
215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216static struct capiminor *capiminor_alloc(struct capi20_appl *ap, u32 ncci)
217{
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000218 struct capiminor *mp;
Jan Kiszka40fb2d02010-02-08 10:12:25 +0000219 struct device *dev;
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000220 unsigned int minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 unsigned long flags;
222
Jan Kiszka54f0fad2010-02-08 10:12:20 +0000223 mp = kzalloc(sizeof(*mp), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 if (!mp) {
225 printk(KERN_ERR "capi: can't alloc capiminor\n");
226 return NULL;
227 }
228
Jan Kiszka0159d542010-02-08 10:12:27 +0000229 kref_init(&mp->kref);
230
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 mp->ap = ap;
232 mp->ncci = ncci;
233 mp->msgid = 0;
234 atomic_set(&mp->ttyopencount,0);
Michael Buesch6aa65472006-06-26 00:25:30 -0700235 INIT_LIST_HEAD(&mp->ackqueue);
236 spin_lock_init(&mp->ackqlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
238 skb_queue_head_init(&mp->inqueue);
239 skb_queue_head_init(&mp->outqueue);
240
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000241 tty_port_init(&mp->port);
242 mp->port.ops = &capiminor_port_ops;
243
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000244 /* Allocate the least unused minor number. */
245 write_lock_irqsave(&capiminors_lock, flags);
246 for (minor = 0; minor < capi_ttyminors; minor++)
247 if (!capiminors[minor]) {
248 capiminors[minor] = mp;
249 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 }
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000251 write_unlock_irqrestore(&capiminors_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000253 if (minor == capi_ttyminors) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 printk(KERN_NOTICE "capi: out of minors\n");
Jan Kiszka40fb2d02010-02-08 10:12:25 +0000255 goto err_out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 }
257
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000258 mp->minor = minor;
259
Jan Kiszka40fb2d02010-02-08 10:12:25 +0000260 dev = tty_register_device(capinc_tty_driver, minor, NULL);
261 if (IS_ERR(dev))
262 goto err_out2;
263
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 return mp;
Jan Kiszka40fb2d02010-02-08 10:12:25 +0000265
266err_out2:
267 write_lock_irqsave(&capiminors_lock, flags);
268 capiminors[minor] = NULL;
269 write_unlock_irqrestore(&capiminors_lock, flags);
270
271err_out1:
272 kfree(mp);
273 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274}
275
Jan Kiszka0159d542010-02-08 10:12:27 +0000276static void capiminor_destroy(struct kref *kref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277{
Jan Kiszka0159d542010-02-08 10:12:27 +0000278 struct capiminor *mp = container_of(kref, struct capiminor, kref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Wei Yongjund46604e2009-02-25 00:12:09 +0000280 kfree_skb(mp->ttyskb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 skb_queue_purge(&mp->inqueue);
282 skb_queue_purge(&mp->outqueue);
283 capiminor_del_all_ack(mp);
284 kfree(mp);
285}
286
Jan Kiszka40fb2d02010-02-08 10:12:25 +0000287static struct capiminor *capiminor_get(unsigned int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288{
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000289 struct capiminor *mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000291 read_lock(&capiminors_lock);
292 mp = capiminors[minor];
Jan Kiszka0159d542010-02-08 10:12:27 +0000293 if (mp)
294 kref_get(&mp->kref);
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000295 read_unlock(&capiminors_lock);
296
297 return mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
Jan Kiszka0159d542010-02-08 10:12:27 +0000300static inline void capiminor_put(struct capiminor *mp)
301{
302 kref_put(&mp->kref, capiminor_destroy);
303}
304
305static void capiminor_free(struct capiminor *mp)
306{
307 unsigned long flags;
308
309 tty_unregister_device(capinc_tty_driver, mp->minor);
310
311 write_lock_irqsave(&capiminors_lock, flags);
312 capiminors[mp->minor] = NULL;
313 write_unlock_irqrestore(&capiminors_lock, flags);
314
315 capiminor_put(mp);
316}
317
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318/* -------- struct capincci ----------------------------------------- */
319
Jan Kiszka501c87a2010-02-08 10:12:16 +0000320static void capincci_alloc_minor(struct capidev *cdev, struct capincci *np)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321{
Jan Kiszka501c87a2010-02-08 10:12:16 +0000322 struct capiminor *mp;
Jan Kiszkae95ac142010-02-08 10:12:26 +0000323 dev_t device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
Jan Kiszka501c87a2010-02-08 10:12:16 +0000325 if (!(cdev->userflags & CAPIFLAG_HIGHJACKING))
326 return;
327
328 mp = np->minorp = capiminor_alloc(&cdev->ap, np->ncci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 if (mp) {
Jan Kiszkae95ac142010-02-08 10:12:26 +0000330 device = MKDEV(capinc_tty_driver->major, mp->minor);
331 mp->capifs_dentry = capifs_new_ncci(mp->minor, device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 }
Jan Kiszka501c87a2010-02-08 10:12:16 +0000333}
334
335static void capincci_free_minor(struct capincci *np)
336{
337 struct capiminor *mp = np->minorp;
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000338 struct tty_struct *tty;
Jan Kiszka501c87a2010-02-08 10:12:16 +0000339
340 if (mp) {
341 capifs_free_ncci(mp->capifs_dentry);
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000342
343 tty = tty_port_tty_get(&mp->port);
344 if (tty) {
Jan Kiszka30bced92010-02-08 10:12:31 +0000345 tty_vhangup(tty);
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000346 tty_kref_put(tty);
Jan Kiszka501c87a2010-02-08 10:12:16 +0000347 }
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000348
349 capiminor_free(mp);
Jan Kiszka501c87a2010-02-08 10:12:16 +0000350 }
351}
352
353static inline unsigned int capincci_minor_opencount(struct capincci *np)
354{
355 struct capiminor *mp = np->minorp;
356
357 return mp ? atomic_read(&mp->ttyopencount) : 0;
358}
359
360#else /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
361
362static inline void
363capincci_alloc_minor(struct capidev *cdev, struct capincci *np) { }
364static inline void capincci_free_minor(struct capincci *np) { }
365
366static inline unsigned int capincci_minor_opencount(struct capincci *np)
367{
368 return 0;
369}
370
371#endif /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
372
373static struct capincci *capincci_alloc(struct capidev *cdev, u32 ncci)
374{
Jan Kiszka884f5c42010-02-08 10:12:22 +0000375 struct capincci *np;
Jan Kiszka501c87a2010-02-08 10:12:16 +0000376
Jan Kiszka54f0fad2010-02-08 10:12:20 +0000377 np = kzalloc(sizeof(*np), GFP_KERNEL);
Jan Kiszka501c87a2010-02-08 10:12:16 +0000378 if (!np)
379 return NULL;
380 np->ncci = ncci;
381 np->cdev = cdev;
382
383 capincci_alloc_minor(cdev, np);
384
Jan Kiszka884f5c42010-02-08 10:12:22 +0000385 list_add_tail(&np->list, &cdev->nccis);
386
387 return np;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388}
389
390static void capincci_free(struct capidev *cdev, u32 ncci)
391{
Jan Kiszka884f5c42010-02-08 10:12:22 +0000392 struct capincci *np, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Jan Kiszka884f5c42010-02-08 10:12:22 +0000394 list_for_each_entry_safe(np, tmp, &cdev->nccis, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 if (ncci == 0xffffffff || np->ncci == ncci) {
Jan Kiszka501c87a2010-02-08 10:12:16 +0000396 capincci_free_minor(np);
Jan Kiszka884f5c42010-02-08 10:12:22 +0000397 list_del(&np->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 kfree(np);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400}
401
402static struct capincci *capincci_find(struct capidev *cdev, u32 ncci)
403{
Jan Kiszka884f5c42010-02-08 10:12:22 +0000404 struct capincci *np;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
Jan Kiszka884f5c42010-02-08 10:12:22 +0000406 list_for_each_entry(np, &cdev->nccis, list)
407 if (np->ncci == ncci)
408 return np;
409 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410}
411
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
413/* -------- handle data queue --------------------------------------- */
414
415static struct sk_buff *
416gen_data_b3_resp_for(struct capiminor *mp, struct sk_buff *skb)
417{
418 struct sk_buff *nskb;
419 nskb = alloc_skb(CAPI_DATA_B3_RESP_LEN, GFP_ATOMIC);
420 if (nskb) {
421 u16 datahandle = CAPIMSG_U16(skb->data,CAPIMSG_BASELEN+4+4+2);
422 unsigned char *s = skb_put(nskb, CAPI_DATA_B3_RESP_LEN);
423 capimsg_setu16(s, 0, CAPI_DATA_B3_RESP_LEN);
424 capimsg_setu16(s, 2, mp->ap->applid);
425 capimsg_setu8 (s, 4, CAPI_DATA_B3);
426 capimsg_setu8 (s, 5, CAPI_RESP);
427 capimsg_setu16(s, 6, mp->msgid++);
428 capimsg_setu32(s, 8, mp->ncci);
429 capimsg_setu16(s, 12, datahandle);
430 }
431 return nskb;
432}
433
434static int handle_recv_skb(struct capiminor *mp, struct sk_buff *skb)
435{
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000436 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 struct sk_buff *nskb;
438 int datalen;
439 u16 errcode, datahandle;
440 struct tty_ldisc *ld;
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000441 int ret = -1;
442
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 datalen = skb->len - CAPIMSG_LEN(skb->data);
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000444
445 tty = tty_port_tty_get(&mp->port);
446 if (!tty) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447#ifdef _DEBUG_DATAFLOW
448 printk(KERN_DEBUG "capi: currently no receiver\n");
449#endif
450 return -1;
451 }
452
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000453 ld = tty_ldisc_ref(tty);
454 if (!ld)
455 goto out1;
456
Alan Coxa352def2008-07-16 21:53:12 +0100457 if (ld->ops->receive_buf == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458#if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
459 printk(KERN_DEBUG "capi: ldisc has no receive_buf function\n");
460#endif
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000461 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 }
463 if (mp->ttyinstop) {
464#if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
465 printk(KERN_DEBUG "capi: recv tty throttled\n");
466#endif
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000467 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 }
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000469 if (tty->receive_room < datalen) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470#if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
471 printk(KERN_DEBUG "capi: no room in tty\n");
472#endif
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000473 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 }
Harvey Harrison2f9e9b62008-04-28 02:14:37 -0700475 if ((nskb = gen_data_b3_resp_for(mp, skb)) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 printk(KERN_ERR "capi: gen_data_b3_resp failed\n");
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000477 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 }
479 datahandle = CAPIMSG_U16(skb->data,CAPIMSG_BASELEN+4);
480 errcode = capi20_put_message(mp->ap, nskb);
481 if (errcode != CAPI_NOERROR) {
482 printk(KERN_ERR "capi: send DATA_B3_RESP failed=%x\n",
483 errcode);
484 kfree_skb(nskb);
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000485 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 }
487 (void)skb_pull(skb, CAPIMSG_LEN(skb->data));
488#ifdef _DEBUG_DATAFLOW
489 printk(KERN_DEBUG "capi: DATA_B3_RESP %u len=%d => ldisc\n",
490 datahandle, skb->len);
491#endif
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000492 ld->ops->receive_buf(tty, skb->data, NULL, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 kfree_skb(skb);
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000494 ret = 0;
495out2:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 tty_ldisc_deref(ld);
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000497out1:
498 tty_kref_put(tty);
499 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500}
501
502static void handle_minor_recv(struct capiminor *mp)
503{
504 struct sk_buff *skb;
Harvey Harrison2f9e9b62008-04-28 02:14:37 -0700505 while ((skb = skb_dequeue(&mp->inqueue)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 unsigned int len = skb->len;
507 mp->inbytes -= len;
508 if (handle_recv_skb(mp, skb) < 0) {
509 skb_queue_head(&mp->inqueue, skb);
510 mp->inbytes += len;
511 return;
512 }
513 }
514}
515
516static int handle_minor_send(struct capiminor *mp)
517{
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000518 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 struct sk_buff *skb;
520 u16 len;
521 int count = 0;
522 u16 errcode;
523 u16 datahandle;
524
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000525 tty = tty_port_tty_get(&mp->port);
526 if (!tty)
527 return 0;
528
529 if (mp->ttyoutstop) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530#if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
531 printk(KERN_DEBUG "capi: send: tty stopped\n");
532#endif
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000533 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 return 0;
535 }
536
Harvey Harrison2f9e9b62008-04-28 02:14:37 -0700537 while ((skb = skb_dequeue(&mp->outqueue)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 datahandle = mp->datahandle;
539 len = (u16)skb->len;
540 skb_push(skb, CAPI_DATA_B3_REQ_LEN);
541 memset(skb->data, 0, CAPI_DATA_B3_REQ_LEN);
542 capimsg_setu16(skb->data, 0, CAPI_DATA_B3_REQ_LEN);
543 capimsg_setu16(skb->data, 2, mp->ap->applid);
544 capimsg_setu8 (skb->data, 4, CAPI_DATA_B3);
545 capimsg_setu8 (skb->data, 5, CAPI_REQ);
546 capimsg_setu16(skb->data, 6, mp->msgid++);
547 capimsg_setu32(skb->data, 8, mp->ncci); /* NCCI */
Andrew Morton5e6c20a2007-07-17 04:04:22 -0700548 capimsg_setu32(skb->data, 12, (u32)(long)skb->data);/* Data32 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 capimsg_setu16(skb->data, 16, len); /* Data length */
550 capimsg_setu16(skb->data, 18, datahandle);
551 capimsg_setu16(skb->data, 20, 0); /* Flags */
552
Jan Kiszka501c87a2010-02-08 10:12:16 +0000553 if (capiminor_add_ack(mp, datahandle) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 skb_pull(skb, CAPI_DATA_B3_REQ_LEN);
555 skb_queue_head(&mp->outqueue, skb);
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000556 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 return count;
558 }
559 errcode = capi20_put_message(mp->ap, skb);
560 if (errcode == CAPI_NOERROR) {
561 mp->datahandle++;
562 count++;
563 mp->outbytes -= len;
564#ifdef _DEBUG_DATAFLOW
565 printk(KERN_DEBUG "capi: DATA_B3_REQ %u len=%u\n",
566 datahandle, len);
567#endif
568 continue;
569 }
570 capiminor_del_ack(mp, datahandle);
571
572 if (errcode == CAPI_SENDQUEUEFULL) {
573 skb_pull(skb, CAPI_DATA_B3_REQ_LEN);
574 skb_queue_head(&mp->outqueue, skb);
575 break;
576 }
577
578 /* ups, drop packet */
579 printk(KERN_ERR "capi: put_message = %x\n", errcode);
580 mp->outbytes -= len;
581 kfree_skb(skb);
582 }
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000583 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 return count;
585}
586
587#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
588/* -------- function called by lower level -------------------------- */
589
590static void capi_recv_message(struct capi20_appl *ap, struct sk_buff *skb)
591{
592 struct capidev *cdev = ap->private;
593#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000594 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 struct capiminor *mp;
596 u16 datahandle;
597#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
598 struct capincci *np;
Michael Buesch053b47f2007-02-12 00:53:26 -0800599 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
Jan Kiszka05b41492010-02-08 10:12:19 +0000601 mutex_lock(&cdev->lock);
602
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_CONF) {
604 u16 info = CAPIMSG_U16(skb->data, 12); // Info field
Jan Kiszka05b41492010-02-08 10:12:19 +0000605 if ((info & 0xff00) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 }
Jan Kiszka05b41492010-02-08 10:12:19 +0000608 if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_IND)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
Jan Kiszka05b41492010-02-08 10:12:19 +0000610
Michael Buesch053b47f2007-02-12 00:53:26 -0800611 spin_lock_irqsave(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 if (CAPIMSG_COMMAND(skb->data) != CAPI_DATA_B3) {
613 skb_queue_tail(&cdev->recvqueue, skb);
614 wake_up_interruptible(&cdev->recvwait);
Jan Kiszka05b41492010-02-08 10:12:19 +0000615 goto unlock_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 }
Jan Kiszka05b41492010-02-08 10:12:19 +0000617
618 np = capincci_find(cdev, CAPIMSG_CONTROL(skb->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 if (!np) {
620 printk(KERN_ERR "BUG: capi_signal: ncci not found\n");
621 skb_queue_tail(&cdev->recvqueue, skb);
622 wake_up_interruptible(&cdev->recvwait);
Jan Kiszka05b41492010-02-08 10:12:19 +0000623 goto unlock_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 }
Jan Kiszka501c87a2010-02-08 10:12:16 +0000625
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626#ifndef CONFIG_ISDN_CAPI_MIDDLEWARE
627 skb_queue_tail(&cdev->recvqueue, skb);
628 wake_up_interruptible(&cdev->recvwait);
Jan Kiszka501c87a2010-02-08 10:12:16 +0000629
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630#else /* CONFIG_ISDN_CAPI_MIDDLEWARE */
Jan Kiszka501c87a2010-02-08 10:12:16 +0000631
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 mp = np->minorp;
633 if (!mp) {
634 skb_queue_tail(&cdev->recvqueue, skb);
635 wake_up_interruptible(&cdev->recvwait);
Jan Kiszka05b41492010-02-08 10:12:19 +0000636 goto unlock_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_IND) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4+4+2);
640#ifdef _DEBUG_DATAFLOW
641 printk(KERN_DEBUG "capi_signal: DATA_B3_IND %u len=%d\n",
642 datahandle, skb->len-CAPIMSG_LEN(skb->data));
643#endif
644 skb_queue_tail(&mp->inqueue, skb);
645 mp->inbytes += skb->len;
646 handle_minor_recv(mp);
647
648 } else if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_CONF) {
649
650 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4);
651#ifdef _DEBUG_DATAFLOW
652 printk(KERN_DEBUG "capi_signal: DATA_B3_CONF %u 0x%x\n",
653 datahandle,
654 CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4+2));
655#endif
656 kfree_skb(skb);
657 (void)capiminor_del_ack(mp, datahandle);
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000658 tty = tty_port_tty_get(&mp->port);
659 if (tty) {
660 tty_wakeup(tty);
661 tty_kref_put(tty);
662 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 (void)handle_minor_send(mp);
664
665 } else {
666 /* ups, let capi application handle it :-) */
667 skb_queue_tail(&cdev->recvqueue, skb);
668 wake_up_interruptible(&cdev->recvwait);
669 }
670#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
Jan Kiszka501c87a2010-02-08 10:12:16 +0000671
Jan Kiszka05b41492010-02-08 10:12:19 +0000672unlock_out:
Michael Buesch053b47f2007-02-12 00:53:26 -0800673 spin_unlock_irqrestore(&workaround_lock, flags);
Jan Kiszka05b41492010-02-08 10:12:19 +0000674 mutex_unlock(&cdev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675}
676
677/* -------- file_operations for capidev ----------------------------- */
678
679static ssize_t
680capi_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
681{
682 struct capidev *cdev = (struct capidev *)file->private_data;
683 struct sk_buff *skb;
684 size_t copied;
Jan Kiszka28a1dbb2010-02-08 10:12:21 +0000685 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
687 if (!cdev->ap.applid)
688 return -ENODEV;
689
Jan Kiszka28a1dbb2010-02-08 10:12:21 +0000690 skb = skb_dequeue(&cdev->recvqueue);
691 if (!skb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 if (file->f_flags & O_NONBLOCK)
693 return -EAGAIN;
Jan Kiszka28a1dbb2010-02-08 10:12:21 +0000694 err = wait_event_interruptible(cdev->recvwait,
695 (skb = skb_dequeue(&cdev->recvqueue)));
696 if (err)
697 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 }
699 if (skb->len > count) {
700 skb_queue_head(&cdev->recvqueue, skb);
701 return -EMSGSIZE;
702 }
703 if (copy_to_user(buf, skb->data, skb->len)) {
704 skb_queue_head(&cdev->recvqueue, skb);
705 return -EFAULT;
706 }
707 copied = skb->len;
708
709 kfree_skb(skb);
710
711 return copied;
712}
713
714static ssize_t
715capi_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
716{
717 struct capidev *cdev = (struct capidev *)file->private_data;
718 struct sk_buff *skb;
719 u16 mlen;
720
721 if (!cdev->ap.applid)
722 return -ENODEV;
723
724 skb = alloc_skb(count, GFP_USER);
725 if (!skb)
726 return -ENOMEM;
727
728 if (copy_from_user(skb_put(skb, count), buf, count)) {
729 kfree_skb(skb);
730 return -EFAULT;
731 }
732 mlen = CAPIMSG_LEN(skb->data);
733 if (CAPIMSG_CMD(skb->data) == CAPI_DATA_B3_REQ) {
734 if ((size_t)(mlen + CAPIMSG_DATALEN(skb->data)) != count) {
735 kfree_skb(skb);
736 return -EINVAL;
737 }
738 } else {
739 if (mlen != count) {
740 kfree_skb(skb);
741 return -EINVAL;
742 }
743 }
744 CAPIMSG_SETAPPID(skb->data, cdev->ap.applid);
745
746 if (CAPIMSG_CMD(skb->data) == CAPI_DISCONNECT_B3_RESP) {
Jan Kiszka05b41492010-02-08 10:12:19 +0000747 mutex_lock(&cdev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 capincci_free(cdev, CAPIMSG_NCCI(skb->data));
Jan Kiszka05b41492010-02-08 10:12:19 +0000749 mutex_unlock(&cdev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 }
751
752 cdev->errcode = capi20_put_message(&cdev->ap, skb);
753
754 if (cdev->errcode) {
755 kfree_skb(skb);
756 return -EIO;
757 }
758 return count;
759}
760
761static unsigned int
762capi_poll(struct file *file, poll_table * wait)
763{
764 struct capidev *cdev = (struct capidev *)file->private_data;
765 unsigned int mask = 0;
766
767 if (!cdev->ap.applid)
768 return POLLERR;
769
770 poll_wait(file, &(cdev->recvwait), wait);
771 mask = POLLOUT | POLLWRNORM;
772 if (!skb_queue_empty(&cdev->recvqueue))
773 mask |= POLLIN | POLLRDNORM;
774 return mask;
775}
776
777static int
778capi_ioctl(struct inode *inode, struct file *file,
779 unsigned int cmd, unsigned long arg)
780{
781 struct capidev *cdev = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 capi_ioctl_struct data;
783 int retval = -EINVAL;
784 void __user *argp = (void __user *)arg;
785
786 switch (cmd) {
787 case CAPI_REGISTER:
Jan Kiszka05b41492010-02-08 10:12:19 +0000788 mutex_lock(&cdev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
Jan Kiszka05b41492010-02-08 10:12:19 +0000790 if (cdev->ap.applid) {
791 retval = -EEXIST;
792 goto register_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 }
Jan Kiszka05b41492010-02-08 10:12:19 +0000794 if (copy_from_user(&cdev->ap.rparam, argp,
795 sizeof(struct capi_register_params))) {
796 retval = -EFAULT;
797 goto register_out;
798 }
799 cdev->ap.private = cdev;
800 cdev->ap.recv_message = capi_recv_message;
801 cdev->errcode = capi20_register(&cdev->ap);
802 retval = (int)cdev->ap.applid;
803 if (cdev->errcode) {
804 cdev->ap.applid = 0;
805 retval = -EIO;
806 }
807
808register_out:
809 mutex_unlock(&cdev->lock);
810 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
812 case CAPI_GET_VERSION:
813 {
814 if (copy_from_user(&data.contr, argp,
815 sizeof(data.contr)))
816 return -EFAULT;
817 cdev->errcode = capi20_get_version(data.contr, &data.version);
818 if (cdev->errcode)
819 return -EIO;
820 if (copy_to_user(argp, &data.version,
821 sizeof(data.version)))
822 return -EFAULT;
823 }
824 return 0;
825
826 case CAPI_GET_SERIAL:
827 {
828 if (copy_from_user(&data.contr, argp,
829 sizeof(data.contr)))
830 return -EFAULT;
831 cdev->errcode = capi20_get_serial (data.contr, data.serial);
832 if (cdev->errcode)
833 return -EIO;
834 if (copy_to_user(argp, data.serial,
835 sizeof(data.serial)))
836 return -EFAULT;
837 }
838 return 0;
839 case CAPI_GET_PROFILE:
840 {
841 if (copy_from_user(&data.contr, argp,
842 sizeof(data.contr)))
843 return -EFAULT;
844
845 if (data.contr == 0) {
846 cdev->errcode = capi20_get_profile(data.contr, &data.profile);
847 if (cdev->errcode)
848 return -EIO;
849
850 retval = copy_to_user(argp,
851 &data.profile.ncontroller,
852 sizeof(data.profile.ncontroller));
853
854 } else {
855 cdev->errcode = capi20_get_profile(data.contr, &data.profile);
856 if (cdev->errcode)
857 return -EIO;
858
859 retval = copy_to_user(argp, &data.profile,
860 sizeof(data.profile));
861 }
862 if (retval)
863 return -EFAULT;
864 }
865 return 0;
866
867 case CAPI_GET_MANUFACTURER:
868 {
869 if (copy_from_user(&data.contr, argp,
870 sizeof(data.contr)))
871 return -EFAULT;
872 cdev->errcode = capi20_get_manufacturer(data.contr, data.manufacturer);
873 if (cdev->errcode)
874 return -EIO;
875
876 if (copy_to_user(argp, data.manufacturer,
877 sizeof(data.manufacturer)))
878 return -EFAULT;
879
880 }
881 return 0;
882 case CAPI_GET_ERRCODE:
883 data.errcode = cdev->errcode;
884 cdev->errcode = CAPI_NOERROR;
885 if (arg) {
886 if (copy_to_user(argp, &data.errcode,
887 sizeof(data.errcode)))
888 return -EFAULT;
889 }
890 return data.errcode;
891
892 case CAPI_INSTALLED:
893 if (capi20_isinstalled() == CAPI_NOERROR)
894 return 0;
895 return -ENXIO;
896
897 case CAPI_MANUFACTURER_CMD:
898 {
899 struct capi_manufacturer_cmd mcmd;
900 if (!capable(CAP_SYS_ADMIN))
901 return -EPERM;
902 if (copy_from_user(&mcmd, argp, sizeof(mcmd)))
903 return -EFAULT;
904 return capi20_manufacturer(mcmd.cmd, mcmd.data);
905 }
906 return 0;
907
908 case CAPI_SET_FLAGS:
Jan Kiszka05b41492010-02-08 10:12:19 +0000909 case CAPI_CLR_FLAGS: {
910 unsigned userflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
Jan Kiszka05b41492010-02-08 10:12:19 +0000912 if (copy_from_user(&userflags, argp, sizeof(userflags)))
913 return -EFAULT;
914
915 mutex_lock(&cdev->lock);
916 if (cmd == CAPI_SET_FLAGS)
917 cdev->userflags |= userflags;
918 else
919 cdev->userflags &= ~userflags;
920 mutex_unlock(&cdev->lock);
921 return 0;
922 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 case CAPI_GET_FLAGS:
924 if (copy_to_user(argp, &cdev->userflags,
925 sizeof(cdev->userflags)))
926 return -EFAULT;
927 return 0;
928
Jan Kiszka05b41492010-02-08 10:12:19 +0000929 case CAPI_NCCI_OPENCOUNT: {
930 struct capincci *nccip;
931 unsigned ncci;
932 int count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
Jan Kiszka05b41492010-02-08 10:12:19 +0000934 if (copy_from_user(&ncci, argp, sizeof(ncci)))
935 return -EFAULT;
936
937 mutex_lock(&cdev->lock);
938 nccip = capincci_find(cdev, (u32)ncci);
939 if (nccip)
940 count = capincci_minor_opencount(nccip);
941 mutex_unlock(&cdev->lock);
942 return count;
943 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
945#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
Jan Kiszka05b41492010-02-08 10:12:19 +0000946 case CAPI_NCCI_GETUNIT: {
947 struct capincci *nccip;
948 struct capiminor *mp;
949 unsigned ncci;
950 int unit = -ESRCH;
951
952 if (copy_from_user(&ncci, argp, sizeof(ncci)))
953 return -EFAULT;
954
955 mutex_lock(&cdev->lock);
956 nccip = capincci_find(cdev, (u32)ncci);
957 if (nccip) {
958 mp = nccip->minorp;
959 if (mp)
960 unit = mp->minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 }
Jan Kiszka05b41492010-02-08 10:12:19 +0000962 mutex_unlock(&cdev->lock);
963 return unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 }
Jan Kiszka05b41492010-02-08 10:12:19 +0000965#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
966
967 default:
968 return -EINVAL;
969 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970}
971
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000972static int capi_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973{
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000974 struct capidev *cdev;
975
976 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
977 if (!cdev)
978 return -ENOMEM;
979
Jan Kiszka05b41492010-02-08 10:12:19 +0000980 mutex_init(&cdev->lock);
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000981 skb_queue_head_init(&cdev->recvqueue);
982 init_waitqueue_head(&cdev->recvwait);
Jan Kiszka884f5c42010-02-08 10:12:22 +0000983 INIT_LIST_HEAD(&cdev->nccis);
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000984 file->private_data = cdev;
985
986 mutex_lock(&capidev_list_lock);
987 list_add_tail(&cdev->list, &capidev_list);
988 mutex_unlock(&capidev_list_lock);
989
990 return nonseekable_open(inode, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991}
992
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000993static int capi_release(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994{
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000995 struct capidev *cdev = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000997 mutex_lock(&capidev_list_lock);
998 list_del(&cdev->list);
999 mutex_unlock(&capidev_list_lock);
1000
Jan Kiszka05b41492010-02-08 10:12:19 +00001001 if (cdev->ap.applid)
Jan Kiszkaeca39dd2010-02-08 10:12:18 +00001002 capi20_release(&cdev->ap);
Jan Kiszkaeca39dd2010-02-08 10:12:18 +00001003 skb_queue_purge(&cdev->recvqueue);
Jan Kiszkaeca39dd2010-02-08 10:12:18 +00001004 capincci_free(cdev, 0xffffffff);
Jan Kiszkaeca39dd2010-02-08 10:12:18 +00001005
1006 kfree(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 return 0;
1008}
1009
Arjan van de Ven2b8693c2007-02-12 00:55:32 -08001010static const struct file_operations capi_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011{
1012 .owner = THIS_MODULE,
1013 .llseek = no_llseek,
1014 .read = capi_read,
1015 .write = capi_write,
1016 .poll = capi_poll,
1017 .ioctl = capi_ioctl,
1018 .open = capi_open,
1019 .release = capi_release,
1020};
1021
1022#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1023/* -------- tty_operations for capincci ----------------------------- */
1024
Jan Kiszka46324512010-02-08 10:12:28 +00001025static int
1026capinc_tty_install(struct tty_driver *driver, struct tty_struct *tty)
1027{
1028 int idx = tty->index;
1029 struct capiminor *mp = capiminor_get(idx);
1030 int ret = tty_init_termios(tty);
1031
1032 if (ret == 0) {
1033 tty_driver_kref_get(driver);
1034 tty->count++;
1035 tty->driver_data = mp;
1036 driver->ttys[idx] = tty;
1037 } else
1038 capiminor_put(mp);
1039 return ret;
1040}
1041
1042static void capinc_tty_cleanup(struct tty_struct *tty)
1043{
1044 struct capiminor *mp = tty->driver_data;
1045 tty->driver_data = NULL;
1046 capiminor_put(mp);
1047}
1048
Jan Kiszkafb4b4882010-02-08 10:12:29 +00001049static int capinc_tty_open(struct tty_struct *tty, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050{
Jan Kiszka46324512010-02-08 10:12:28 +00001051 struct capiminor *mp = tty->driver_data;
Michael Buesch053b47f2007-02-12 00:53:26 -08001052 unsigned long flags;
Jan Kiszkafb4b4882010-02-08 10:12:29 +00001053 int err;
1054
1055 err = tty_port_open(&mp->port, tty, filp);
1056 if (err)
1057 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
Michael Buesch053b47f2007-02-12 00:53:26 -08001059 spin_lock_irqsave(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 atomic_inc(&mp->ttyopencount);
1061#ifdef _DEBUG_REFCOUNT
1062 printk(KERN_DEBUG "capinc_tty_open ocount=%d\n", atomic_read(&mp->ttyopencount));
1063#endif
1064 handle_minor_recv(mp);
Michael Buesch053b47f2007-02-12 00:53:26 -08001065 spin_unlock_irqrestore(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 return 0;
1067}
1068
Jan Kiszkafb4b4882010-02-08 10:12:29 +00001069static void capinc_tty_close(struct tty_struct *tty, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070{
Jan Kiszka46324512010-02-08 10:12:28 +00001071 struct capiminor *mp = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 if (atomic_dec_and_test(&mp->ttyopencount)) {
1074#ifdef _DEBUG_REFCOUNT
1075 printk(KERN_DEBUG "capinc_tty_close lastclose\n");
1076#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 }
1078#ifdef _DEBUG_REFCOUNT
1079 printk(KERN_DEBUG "capinc_tty_close ocount=%d\n", atomic_read(&mp->ttyopencount));
1080#endif
Jan Kiszka0159d542010-02-08 10:12:27 +00001081
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082#ifdef _DEBUG_REFCOUNT
1083 printk(KERN_DEBUG "capinc_tty_close\n");
1084#endif
Jan Kiszkafb4b4882010-02-08 10:12:29 +00001085 tty_port_close(&mp->port, tty, filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086}
1087
Jan Kiszka6576c282010-02-08 10:12:32 +00001088static int capinc_tty_write(struct tty_struct *tty,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 const unsigned char *buf, int count)
1090{
Jan Kiszka2c8df722010-02-08 10:12:30 +00001091 struct capiminor *mp = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 struct sk_buff *skb;
Michael Buesch053b47f2007-02-12 00:53:26 -08001093 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
1095#ifdef _DEBUG_TTYFUNCS
1096 printk(KERN_DEBUG "capinc_tty_write(count=%d)\n", count);
1097#endif
1098
Michael Buesch053b47f2007-02-12 00:53:26 -08001099 spin_lock_irqsave(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 skb = mp->ttyskb;
1101 if (skb) {
1102 mp->ttyskb = NULL;
1103 skb_queue_tail(&mp->outqueue, skb);
1104 mp->outbytes += skb->len;
1105 }
1106
1107 skb = alloc_skb(CAPI_DATA_B3_REQ_LEN+count, GFP_ATOMIC);
1108 if (!skb) {
1109 printk(KERN_ERR "capinc_tty_write: alloc_skb failed\n");
Michael Buesch053b47f2007-02-12 00:53:26 -08001110 spin_unlock_irqrestore(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 return -ENOMEM;
1112 }
1113
1114 skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
1115 memcpy(skb_put(skb, count), buf, count);
1116
1117 skb_queue_tail(&mp->outqueue, skb);
1118 mp->outbytes += skb->len;
1119 (void)handle_minor_send(mp);
1120 (void)handle_minor_recv(mp);
Michael Buesch053b47f2007-02-12 00:53:26 -08001121 spin_unlock_irqrestore(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 return count;
1123}
1124
Alan Coxf2545a72008-04-30 00:54:09 -07001125static int capinc_tty_put_char(struct tty_struct *tty, unsigned char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126{
Jan Kiszka2c8df722010-02-08 10:12:30 +00001127 struct capiminor *mp = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 struct sk_buff *skb;
Michael Buesch053b47f2007-02-12 00:53:26 -08001129 unsigned long flags;
Alan Coxf2545a72008-04-30 00:54:09 -07001130 int ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131
1132#ifdef _DEBUG_TTYFUNCS
1133 printk(KERN_DEBUG "capinc_put_char(%u)\n", ch);
1134#endif
1135
Michael Buesch053b47f2007-02-12 00:53:26 -08001136 spin_lock_irqsave(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 skb = mp->ttyskb;
1138 if (skb) {
1139 if (skb_tailroom(skb) > 0) {
1140 *(skb_put(skb, 1)) = ch;
Michael Buesch053b47f2007-02-12 00:53:26 -08001141 spin_unlock_irqrestore(&workaround_lock, flags);
Alan Coxf2545a72008-04-30 00:54:09 -07001142 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 }
1144 mp->ttyskb = NULL;
1145 skb_queue_tail(&mp->outqueue, skb);
1146 mp->outbytes += skb->len;
1147 (void)handle_minor_send(mp);
1148 }
1149 skb = alloc_skb(CAPI_DATA_B3_REQ_LEN+CAPI_MAX_BLKSIZE, GFP_ATOMIC);
1150 if (skb) {
1151 skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
1152 *(skb_put(skb, 1)) = ch;
1153 mp->ttyskb = skb;
1154 } else {
1155 printk(KERN_ERR "capinc_put_char: char %u lost\n", ch);
Alan Coxf2545a72008-04-30 00:54:09 -07001156 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 }
Michael Buesch053b47f2007-02-12 00:53:26 -08001158 spin_unlock_irqrestore(&workaround_lock, flags);
Alan Coxf2545a72008-04-30 00:54:09 -07001159 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160}
1161
1162static void capinc_tty_flush_chars(struct tty_struct *tty)
1163{
Jan Kiszka2c8df722010-02-08 10:12:30 +00001164 struct capiminor *mp = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 struct sk_buff *skb;
Michael Buesch053b47f2007-02-12 00:53:26 -08001166 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
1168#ifdef _DEBUG_TTYFUNCS
1169 printk(KERN_DEBUG "capinc_tty_flush_chars\n");
1170#endif
1171
Michael Buesch053b47f2007-02-12 00:53:26 -08001172 spin_lock_irqsave(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 skb = mp->ttyskb;
1174 if (skb) {
1175 mp->ttyskb = NULL;
1176 skb_queue_tail(&mp->outqueue, skb);
1177 mp->outbytes += skb->len;
1178 (void)handle_minor_send(mp);
1179 }
1180 (void)handle_minor_recv(mp);
Michael Buesch053b47f2007-02-12 00:53:26 -08001181 spin_unlock_irqrestore(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182}
1183
1184static int capinc_tty_write_room(struct tty_struct *tty)
1185{
Jan Kiszka2c8df722010-02-08 10:12:30 +00001186 struct capiminor *mp = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 int room;
Jan Kiszka2c8df722010-02-08 10:12:30 +00001188
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 room = CAPINC_MAX_SENDQUEUE-skb_queue_len(&mp->outqueue);
1190 room *= CAPI_MAX_BLKSIZE;
1191#ifdef _DEBUG_TTYFUNCS
1192 printk(KERN_DEBUG "capinc_tty_write_room = %d\n", room);
1193#endif
1194 return room;
1195}
1196
Adrian Bunk408b6642005-05-01 08:59:29 -07001197static int capinc_tty_chars_in_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198{
Jan Kiszka2c8df722010-02-08 10:12:30 +00001199 struct capiminor *mp = tty->driver_data;
1200
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201#ifdef _DEBUG_TTYFUNCS
1202 printk(KERN_DEBUG "capinc_tty_chars_in_buffer = %d nack=%d sq=%d rq=%d\n",
1203 mp->outbytes, mp->nack,
1204 skb_queue_len(&mp->outqueue),
1205 skb_queue_len(&mp->inqueue));
1206#endif
1207 return mp->outbytes;
1208}
1209
1210static int capinc_tty_ioctl(struct tty_struct *tty, struct file * file,
1211 unsigned int cmd, unsigned long arg)
1212{
1213 int error = 0;
1214 switch (cmd) {
1215 default:
Stephen Rothwell53e86312008-10-13 10:44:33 +01001216 error = n_tty_ioctl_helper(tty, file, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 break;
1218 }
1219 return error;
1220}
1221
Alan Cox606d0992006-12-08 02:38:45 -08001222static void capinc_tty_set_termios(struct tty_struct *tty, struct ktermios * old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223{
1224#ifdef _DEBUG_TTYFUNCS
1225 printk(KERN_DEBUG "capinc_tty_set_termios\n");
1226#endif
1227}
1228
Jan Kiszka2c8df722010-02-08 10:12:30 +00001229static void capinc_tty_throttle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230{
Jan Kiszka2c8df722010-02-08 10:12:30 +00001231 struct capiminor *mp = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232#ifdef _DEBUG_TTYFUNCS
1233 printk(KERN_DEBUG "capinc_tty_throttle\n");
1234#endif
Jan Kiszka2c8df722010-02-08 10:12:30 +00001235 mp->ttyinstop = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236}
1237
Jan Kiszka2c8df722010-02-08 10:12:30 +00001238static void capinc_tty_unthrottle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239{
Jan Kiszka2c8df722010-02-08 10:12:30 +00001240 struct capiminor *mp = tty->driver_data;
Michael Buesch053b47f2007-02-12 00:53:26 -08001241 unsigned long flags;
Jan Kiszka2c8df722010-02-08 10:12:30 +00001242
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243#ifdef _DEBUG_TTYFUNCS
1244 printk(KERN_DEBUG "capinc_tty_unthrottle\n");
1245#endif
Jan Kiszka2c8df722010-02-08 10:12:30 +00001246 spin_lock_irqsave(&workaround_lock, flags);
1247 mp->ttyinstop = 0;
1248 handle_minor_recv(mp);
1249 spin_unlock_irqrestore(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250}
1251
1252static void capinc_tty_stop(struct tty_struct *tty)
1253{
Jan Kiszka2c8df722010-02-08 10:12:30 +00001254 struct capiminor *mp = tty->driver_data;
1255
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256#ifdef _DEBUG_TTYFUNCS
1257 printk(KERN_DEBUG "capinc_tty_stop\n");
1258#endif
Jan Kiszka2c8df722010-02-08 10:12:30 +00001259 mp->ttyoutstop = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260}
1261
1262static void capinc_tty_start(struct tty_struct *tty)
1263{
Jan Kiszka2c8df722010-02-08 10:12:30 +00001264 struct capiminor *mp = tty->driver_data;
Michael Buesch053b47f2007-02-12 00:53:26 -08001265 unsigned long flags;
Jan Kiszka2c8df722010-02-08 10:12:30 +00001266
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267#ifdef _DEBUG_TTYFUNCS
1268 printk(KERN_DEBUG "capinc_tty_start\n");
1269#endif
Jan Kiszka2c8df722010-02-08 10:12:30 +00001270 spin_lock_irqsave(&workaround_lock, flags);
1271 mp->ttyoutstop = 0;
1272 (void)handle_minor_send(mp);
1273 spin_unlock_irqrestore(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274}
1275
1276static void capinc_tty_hangup(struct tty_struct *tty)
1277{
Jan Kiszkafb4b4882010-02-08 10:12:29 +00001278 struct capiminor *mp = tty->driver_data;
1279
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280#ifdef _DEBUG_TTYFUNCS
1281 printk(KERN_DEBUG "capinc_tty_hangup\n");
1282#endif
Jan Kiszkafb4b4882010-02-08 10:12:29 +00001283 tty_port_hangup(&mp->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284}
1285
Alan Cox9e989662008-07-22 11:18:03 +01001286static int capinc_tty_break_ctl(struct tty_struct *tty, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287{
1288#ifdef _DEBUG_TTYFUNCS
1289 printk(KERN_DEBUG "capinc_tty_break_ctl(%d)\n", state);
1290#endif
Alan Cox9e989662008-07-22 11:18:03 +01001291 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292}
1293
1294static void capinc_tty_flush_buffer(struct tty_struct *tty)
1295{
1296#ifdef _DEBUG_TTYFUNCS
1297 printk(KERN_DEBUG "capinc_tty_flush_buffer\n");
1298#endif
1299}
1300
1301static void capinc_tty_set_ldisc(struct tty_struct *tty)
1302{
1303#ifdef _DEBUG_TTYFUNCS
1304 printk(KERN_DEBUG "capinc_tty_set_ldisc\n");
1305#endif
1306}
1307
1308static void capinc_tty_send_xchar(struct tty_struct *tty, char ch)
1309{
1310#ifdef _DEBUG_TTYFUNCS
1311 printk(KERN_DEBUG "capinc_tty_send_xchar(%d)\n", ch);
1312#endif
1313}
1314
Jeff Dikeb68e31d2006-10-02 02:17:18 -07001315static const struct tty_operations capinc_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 .open = capinc_tty_open,
1317 .close = capinc_tty_close,
1318 .write = capinc_tty_write,
1319 .put_char = capinc_tty_put_char,
1320 .flush_chars = capinc_tty_flush_chars,
1321 .write_room = capinc_tty_write_room,
1322 .chars_in_buffer = capinc_tty_chars_in_buffer,
1323 .ioctl = capinc_tty_ioctl,
1324 .set_termios = capinc_tty_set_termios,
1325 .throttle = capinc_tty_throttle,
1326 .unthrottle = capinc_tty_unthrottle,
1327 .stop = capinc_tty_stop,
1328 .start = capinc_tty_start,
1329 .hangup = capinc_tty_hangup,
1330 .break_ctl = capinc_tty_break_ctl,
1331 .flush_buffer = capinc_tty_flush_buffer,
1332 .set_ldisc = capinc_tty_set_ldisc,
1333 .send_xchar = capinc_tty_send_xchar,
Jan Kiszka46324512010-02-08 10:12:28 +00001334 .install = capinc_tty_install,
1335 .cleanup = capinc_tty_cleanup,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336};
1337
Jan Kiszkae76b1542010-02-08 10:12:24 +00001338static int __init capinc_tty_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339{
1340 struct tty_driver *drv;
Jan Kiszkae76b1542010-02-08 10:12:24 +00001341 int err;
1342
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 if (capi_ttyminors > CAPINC_MAX_PORTS)
1344 capi_ttyminors = CAPINC_MAX_PORTS;
1345 if (capi_ttyminors <= 0)
1346 capi_ttyminors = CAPINC_NR_PORTS;
1347
Jan Kiszka81d17fe2010-02-08 10:12:23 +00001348 capiminors = kzalloc(sizeof(struct capi_minor *) * capi_ttyminors,
1349 GFP_KERNEL);
1350 if (!capiminors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 return -ENOMEM;
1352
Jan Kiszka81d17fe2010-02-08 10:12:23 +00001353 drv = alloc_tty_driver(capi_ttyminors);
1354 if (!drv) {
1355 kfree(capiminors);
1356 return -ENOMEM;
1357 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 drv->owner = THIS_MODULE;
1359 drv->driver_name = "capi_nc";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 drv->name = "capi";
Jan Kiszkae95ac142010-02-08 10:12:26 +00001361 drv->major = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 drv->minor_start = 0;
1363 drv->type = TTY_DRIVER_TYPE_SERIAL;
1364 drv->subtype = SERIAL_TYPE_NORMAL;
1365 drv->init_termios = tty_std_termios;
1366 drv->init_termios.c_iflag = ICRNL;
1367 drv->init_termios.c_oflag = OPOST | ONLCR;
1368 drv->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1369 drv->init_termios.c_lflag = 0;
Jan Kiszka40fb2d02010-02-08 10:12:25 +00001370 drv->flags =
1371 TTY_DRIVER_REAL_RAW | TTY_DRIVER_RESET_TERMIOS |
1372 TTY_DRIVER_DYNAMIC_DEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 tty_set_operations(drv, &capinc_ops);
Jan Kiszkae76b1542010-02-08 10:12:24 +00001374
1375 err = tty_register_driver(drv);
1376 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 put_tty_driver(drv);
Jan Kiszka81d17fe2010-02-08 10:12:23 +00001378 kfree(capiminors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 printk(KERN_ERR "Couldn't register capi_nc driver\n");
Jan Kiszkae76b1542010-02-08 10:12:24 +00001380 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 }
1382 capinc_tty_driver = drv;
1383 return 0;
1384}
1385
Jan Kiszkae76b1542010-02-08 10:12:24 +00001386static void __exit capinc_tty_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387{
Jan Kiszkae76b1542010-02-08 10:12:24 +00001388 tty_unregister_driver(capinc_tty_driver);
1389 put_tty_driver(capinc_tty_driver);
Jan Kiszka81d17fe2010-02-08 10:12:23 +00001390 kfree(capiminors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391}
1392
Jan Kiszka501c87a2010-02-08 10:12:16 +00001393#else /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
1394
1395static inline int capinc_tty_init(void)
1396{
1397 return 0;
1398}
1399
1400static inline void capinc_tty_exit(void) { }
1401
1402#endif /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
1404/* -------- /proc functions ----------------------------------------- */
1405
1406/*
1407 * /proc/capi/capi20:
1408 * minor applid nrecvctlpkt nrecvdatapkt nsendctlpkt nsenddatapkt
1409 */
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001410static int capi20_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411{
1412 struct capidev *cdev;
1413 struct list_head *l;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414
Jan Kiszkab8f433d2010-02-08 10:12:17 +00001415 mutex_lock(&capidev_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 list_for_each(l, &capidev_list) {
1417 cdev = list_entry(l, struct capidev, list);
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001418 seq_printf(m, "0 %d %lu %lu %lu %lu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 cdev->ap.applid,
1420 cdev->ap.nrecvctlpkt,
1421 cdev->ap.nrecvdatapkt,
1422 cdev->ap.nsentctlpkt,
1423 cdev->ap.nsentdatapkt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 }
Jan Kiszkab8f433d2010-02-08 10:12:17 +00001425 mutex_unlock(&capidev_list_lock);
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001426 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427}
1428
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001429static int capi20_proc_open(struct inode *inode, struct file *file)
1430{
1431 return single_open(file, capi20_proc_show, NULL);
1432}
1433
1434static const struct file_operations capi20_proc_fops = {
1435 .owner = THIS_MODULE,
1436 .open = capi20_proc_open,
1437 .read = seq_read,
1438 .llseek = seq_lseek,
1439 .release = single_release,
1440};
1441
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442/*
1443 * /proc/capi/capi20ncci:
1444 * applid ncci
1445 */
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001446static int capi20ncci_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447{
Jan Kiszka884f5c42010-02-08 10:12:22 +00001448 struct capidev *cdev;
1449 struct capincci *np;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
Jan Kiszkab8f433d2010-02-08 10:12:17 +00001451 mutex_lock(&capidev_list_lock);
Jan Kiszka884f5c42010-02-08 10:12:22 +00001452 list_for_each_entry(cdev, &capidev_list, list) {
Jan Kiszka05b41492010-02-08 10:12:19 +00001453 mutex_lock(&cdev->lock);
Jan Kiszka884f5c42010-02-08 10:12:22 +00001454 list_for_each_entry(np, &cdev->nccis, list)
1455 seq_printf(m, "%d 0x%x\n", cdev->ap.applid, np->ncci);
Jan Kiszka05b41492010-02-08 10:12:19 +00001456 mutex_unlock(&cdev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 }
Jan Kiszkab8f433d2010-02-08 10:12:17 +00001458 mutex_unlock(&capidev_list_lock);
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001459 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460}
1461
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001462static int capi20ncci_proc_open(struct inode *inode, struct file *file)
1463{
1464 return single_open(file, capi20ncci_proc_show, NULL);
1465}
1466
1467static const struct file_operations capi20ncci_proc_fops = {
1468 .owner = THIS_MODULE,
1469 .open = capi20ncci_proc_open,
1470 .read = seq_read,
1471 .llseek = seq_lseek,
1472 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473};
1474
1475static void __init proc_init(void)
1476{
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001477 proc_create("capi/capi20", 0, NULL, &capi20_proc_fops);
1478 proc_create("capi/capi20ncci", 0, NULL, &capi20ncci_proc_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479}
1480
1481static void __exit proc_exit(void)
1482{
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001483 remove_proc_entry("capi/capi20", NULL);
1484 remove_proc_entry("capi/capi20ncci", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485}
1486
1487/* -------- init function and module interface ---------------------- */
1488
1489
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490static int __init capi_init(void)
1491{
Jan Kiszka88549d62010-02-08 10:12:09 +00001492 const char *compileinfo;
Andrew Morton6d9eac32006-03-28 01:56:19 -08001493 int major_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494
Andrew Morton6d9eac32006-03-28 01:56:19 -08001495 major_ret = register_chrdev(capi_major, "capi20", &capi_fops);
1496 if (major_ret < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 printk(KERN_ERR "capi20: unable to get major %d\n", capi_major);
Andrew Morton6d9eac32006-03-28 01:56:19 -08001498 return major_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 }
gregkh@suse.de56b22932005-03-23 10:01:41 -08001500 capi_class = class_create(THIS_MODULE, "capi");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 if (IS_ERR(capi_class)) {
1502 unregister_chrdev(capi_major, "capi20");
1503 return PTR_ERR(capi_class);
1504 }
1505
Greg Kroah-Hartmana9b12612008-07-21 20:03:34 -07001506 device_create(capi_class, NULL, MKDEV(capi_major, 0), NULL, "capi");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 if (capinc_tty_init() < 0) {
Tony Jonesd78b0362007-09-25 02:03:03 +02001509 device_destroy(capi_class, MKDEV(capi_major, 0));
gregkh@suse.de56b22932005-03-23 10:01:41 -08001510 class_destroy(capi_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 unregister_chrdev(capi_major, "capi20");
1512 return -ENOMEM;
1513 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514
1515 proc_init();
1516
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517#if defined(CONFIG_ISDN_CAPI_CAPIFS) || defined(CONFIG_ISDN_CAPI_CAPIFS_MODULE)
1518 compileinfo = " (middleware+capifs)";
Jan Kiszka501c87a2010-02-08 10:12:16 +00001519#elif defined(CONFIG_ISDN_CAPI_MIDDLEWARE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 compileinfo = " (no capifs)";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521#else
1522 compileinfo = " (no middleware)";
1523#endif
Jan Kiszka88549d62010-02-08 10:12:09 +00001524 printk(KERN_NOTICE "CAPI 2.0 started up with major %d%s\n",
1525 capi_major, compileinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526
1527 return 0;
1528}
1529
1530static void __exit capi_exit(void)
1531{
1532 proc_exit();
1533
Tony Jonesd78b0362007-09-25 02:03:03 +02001534 device_destroy(capi_class, MKDEV(capi_major, 0));
gregkh@suse.de56b22932005-03-23 10:01:41 -08001535 class_destroy(capi_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 unregister_chrdev(capi_major, "capi20");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 capinc_tty_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539}
1540
1541module_init(capi_init);
1542module_exit(capi_exit);