blob: 08d5a8aa2fecd3c9e663ce131cc4252e456134b5 [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
Jan Kiszka42651b52010-02-08 10:12:37 +000079struct ackqueue_entry {
Michael Buesch6aa65472006-06-26 00:25:30 -070080 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
Jan Kiszka42792712010-02-08 10:12:38 +000090 struct capi20_appl *ap;
91 u32 ncci;
92 atomic_t datahandle;
93 atomic_t msgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
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{
Jan Kiszka42651b52010-02-08 10:12:37 +0000159 struct ackqueue_entry *n;
Michael Buesch6aa65472006-06-26 00:25:30 -0700160 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{
Jan Kiszka42651b52010-02-08 10:12:37 +0000178 struct ackqueue_entry *p, *tmp;
Michael Buesch6aa65472006-06-26 00:25:30 -0700179 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{
Jan Kiszka42651b52010-02-08 10:12:37 +0000197 struct ackqueue_entry *p, *tmp;
Michael Buesch6aa65472006-06-26 00:25:30 -0700198 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;
Michael Buesch6aa65472006-06-26 00:25:30 -0700230 INIT_LIST_HEAD(&mp->ackqueue);
231 spin_lock_init(&mp->ackqlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
233 skb_queue_head_init(&mp->inqueue);
234 skb_queue_head_init(&mp->outqueue);
235
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000236 tty_port_init(&mp->port);
237 mp->port.ops = &capiminor_port_ops;
238
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000239 /* Allocate the least unused minor number. */
Jan Kiszka3d5d30f2010-02-08 10:12:33 +0000240 spin_lock(&capiminors_lock);
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000241 for (minor = 0; minor < capi_ttyminors; minor++)
242 if (!capiminors[minor]) {
243 capiminors[minor] = mp;
244 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 }
Jan Kiszka3d5d30f2010-02-08 10:12:33 +0000246 spin_unlock(&capiminors_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000248 if (minor == capi_ttyminors) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 printk(KERN_NOTICE "capi: out of minors\n");
Jan Kiszka40fb2d02010-02-08 10:12:25 +0000250 goto err_out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 }
252
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000253 mp->minor = minor;
254
Jan Kiszka40fb2d02010-02-08 10:12:25 +0000255 dev = tty_register_device(capinc_tty_driver, minor, NULL);
256 if (IS_ERR(dev))
257 goto err_out2;
258
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 return mp;
Jan Kiszka40fb2d02010-02-08 10:12:25 +0000260
261err_out2:
Jan Kiszka3d5d30f2010-02-08 10:12:33 +0000262 spin_lock(&capiminors_lock);
Jan Kiszka40fb2d02010-02-08 10:12:25 +0000263 capiminors[minor] = NULL;
Jan Kiszka3d5d30f2010-02-08 10:12:33 +0000264 spin_unlock(&capiminors_lock);
Jan Kiszka40fb2d02010-02-08 10:12:25 +0000265
266err_out1:
267 kfree(mp);
268 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269}
270
Jan Kiszka0159d542010-02-08 10:12:27 +0000271static void capiminor_destroy(struct kref *kref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272{
Jan Kiszka0159d542010-02-08 10:12:27 +0000273 struct capiminor *mp = container_of(kref, struct capiminor, kref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
Wei Yongjund46604e2009-02-25 00:12:09 +0000275 kfree_skb(mp->ttyskb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 skb_queue_purge(&mp->inqueue);
277 skb_queue_purge(&mp->outqueue);
278 capiminor_del_all_ack(mp);
279 kfree(mp);
280}
281
Jan Kiszka40fb2d02010-02-08 10:12:25 +0000282static struct capiminor *capiminor_get(unsigned int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283{
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000284 struct capiminor *mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
Jan Kiszka3d5d30f2010-02-08 10:12:33 +0000286 spin_lock(&capiminors_lock);
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000287 mp = capiminors[minor];
Jan Kiszka0159d542010-02-08 10:12:27 +0000288 if (mp)
289 kref_get(&mp->kref);
Jan Kiszka3d5d30f2010-02-08 10:12:33 +0000290 spin_unlock(&capiminors_lock);
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000291
292 return mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
Jan Kiszka0159d542010-02-08 10:12:27 +0000295static inline void capiminor_put(struct capiminor *mp)
296{
297 kref_put(&mp->kref, capiminor_destroy);
298}
299
300static void capiminor_free(struct capiminor *mp)
301{
Jan Kiszka0159d542010-02-08 10:12:27 +0000302 tty_unregister_device(capinc_tty_driver, mp->minor);
303
Jan Kiszka3d5d30f2010-02-08 10:12:33 +0000304 spin_lock(&capiminors_lock);
Jan Kiszka0159d542010-02-08 10:12:27 +0000305 capiminors[mp->minor] = NULL;
Jan Kiszka3d5d30f2010-02-08 10:12:33 +0000306 spin_unlock(&capiminors_lock);
Jan Kiszka0159d542010-02-08 10:12:27 +0000307
308 capiminor_put(mp);
309}
310
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311/* -------- struct capincci ----------------------------------------- */
312
Jan Kiszka501c87a2010-02-08 10:12:16 +0000313static void capincci_alloc_minor(struct capidev *cdev, struct capincci *np)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314{
Jan Kiszka501c87a2010-02-08 10:12:16 +0000315 struct capiminor *mp;
Jan Kiszkae95ac142010-02-08 10:12:26 +0000316 dev_t device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
Jan Kiszka501c87a2010-02-08 10:12:16 +0000318 if (!(cdev->userflags & CAPIFLAG_HIGHJACKING))
319 return;
320
321 mp = np->minorp = capiminor_alloc(&cdev->ap, np->ncci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 if (mp) {
Jan Kiszkae95ac142010-02-08 10:12:26 +0000323 device = MKDEV(capinc_tty_driver->major, mp->minor);
324 mp->capifs_dentry = capifs_new_ncci(mp->minor, device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 }
Jan Kiszka501c87a2010-02-08 10:12:16 +0000326}
327
328static void capincci_free_minor(struct capincci *np)
329{
330 struct capiminor *mp = np->minorp;
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000331 struct tty_struct *tty;
Jan Kiszka501c87a2010-02-08 10:12:16 +0000332
333 if (mp) {
334 capifs_free_ncci(mp->capifs_dentry);
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000335
336 tty = tty_port_tty_get(&mp->port);
337 if (tty) {
Jan Kiszka30bced92010-02-08 10:12:31 +0000338 tty_vhangup(tty);
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000339 tty_kref_put(tty);
Jan Kiszka501c87a2010-02-08 10:12:16 +0000340 }
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000341
342 capiminor_free(mp);
Jan Kiszka501c87a2010-02-08 10:12:16 +0000343 }
344}
345
346static inline unsigned int capincci_minor_opencount(struct capincci *np)
347{
348 struct capiminor *mp = np->minorp;
Jan Kiszkaa84fdf42010-02-08 10:12:34 +0000349 unsigned int count = 0;
350 struct tty_struct *tty;
Jan Kiszka501c87a2010-02-08 10:12:16 +0000351
Jan Kiszkaa84fdf42010-02-08 10:12:34 +0000352 if (mp) {
353 tty = tty_port_tty_get(&mp->port);
354 if (tty) {
355 count = tty->count;
356 tty_kref_put(tty);
357 }
358 }
359 return count;
Jan Kiszka501c87a2010-02-08 10:12:16 +0000360}
361
362#else /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
363
364static inline void
365capincci_alloc_minor(struct capidev *cdev, struct capincci *np) { }
366static inline void capincci_free_minor(struct capincci *np) { }
367
368static inline unsigned int capincci_minor_opencount(struct capincci *np)
369{
370 return 0;
371}
372
373#endif /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
374
375static struct capincci *capincci_alloc(struct capidev *cdev, u32 ncci)
376{
Jan Kiszka884f5c42010-02-08 10:12:22 +0000377 struct capincci *np;
Jan Kiszka501c87a2010-02-08 10:12:16 +0000378
Jan Kiszka54f0fad2010-02-08 10:12:20 +0000379 np = kzalloc(sizeof(*np), GFP_KERNEL);
Jan Kiszka501c87a2010-02-08 10:12:16 +0000380 if (!np)
381 return NULL;
382 np->ncci = ncci;
383 np->cdev = cdev;
384
385 capincci_alloc_minor(cdev, np);
386
Jan Kiszka884f5c42010-02-08 10:12:22 +0000387 list_add_tail(&np->list, &cdev->nccis);
388
389 return np;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390}
391
392static void capincci_free(struct capidev *cdev, u32 ncci)
393{
Jan Kiszka884f5c42010-02-08 10:12:22 +0000394 struct capincci *np, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
Jan Kiszka884f5c42010-02-08 10:12:22 +0000396 list_for_each_entry_safe(np, tmp, &cdev->nccis, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 if (ncci == 0xffffffff || np->ncci == ncci) {
Jan Kiszka501c87a2010-02-08 10:12:16 +0000398 capincci_free_minor(np);
Jan Kiszka884f5c42010-02-08 10:12:22 +0000399 list_del(&np->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 kfree(np);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402}
403
404static struct capincci *capincci_find(struct capidev *cdev, u32 ncci)
405{
Jan Kiszka884f5c42010-02-08 10:12:22 +0000406 struct capincci *np;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
Jan Kiszka884f5c42010-02-08 10:12:22 +0000408 list_for_each_entry(np, &cdev->nccis, list)
409 if (np->ncci == ncci)
410 return np;
411 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412}
413
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
415/* -------- handle data queue --------------------------------------- */
416
417static struct sk_buff *
418gen_data_b3_resp_for(struct capiminor *mp, struct sk_buff *skb)
419{
420 struct sk_buff *nskb;
421 nskb = alloc_skb(CAPI_DATA_B3_RESP_LEN, GFP_ATOMIC);
422 if (nskb) {
423 u16 datahandle = CAPIMSG_U16(skb->data,CAPIMSG_BASELEN+4+4+2);
424 unsigned char *s = skb_put(nskb, CAPI_DATA_B3_RESP_LEN);
425 capimsg_setu16(s, 0, CAPI_DATA_B3_RESP_LEN);
426 capimsg_setu16(s, 2, mp->ap->applid);
427 capimsg_setu8 (s, 4, CAPI_DATA_B3);
428 capimsg_setu8 (s, 5, CAPI_RESP);
Jan Kiszka42792712010-02-08 10:12:38 +0000429 capimsg_setu16(s, 6, atomic_inc_return(&mp->msgid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 capimsg_setu32(s, 8, mp->ncci);
431 capimsg_setu16(s, 12, datahandle);
432 }
433 return nskb;
434}
435
436static int handle_recv_skb(struct capiminor *mp, struct sk_buff *skb)
437{
Jan Kiszkaa11ef7b2010-02-08 10:12:36 +0000438 unsigned int datalen = skb->len - CAPIMSG_LEN(skb->data);
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000439 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 struct sk_buff *nskb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 u16 errcode, datahandle;
442 struct tty_ldisc *ld;
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000443 int ret = -1;
444
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000445 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);
Jan Kiszkaa11ef7b2010-02-08 10:12:36 +0000454 if (!ld) {
455 /* fatal error, do not requeue */
456 ret = 0;
457 kfree_skb(skb);
458 goto deref_tty;
459 }
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000460
Alan Coxa352def2008-07-16 21:53:12 +0100461 if (ld->ops->receive_buf == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462#if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
463 printk(KERN_DEBUG "capi: ldisc has no receive_buf function\n");
464#endif
Jan Kiszkaa11ef7b2010-02-08 10:12:36 +0000465 /* fatal error, do not requeue */
466 goto free_skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 }
468 if (mp->ttyinstop) {
469#if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
470 printk(KERN_DEBUG "capi: recv tty throttled\n");
471#endif
Jan Kiszkaa11ef7b2010-02-08 10:12:36 +0000472 goto deref_ldisc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 }
Jan Kiszkaa11ef7b2010-02-08 10:12:36 +0000474
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000475 if (tty->receive_room < datalen) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476#if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
477 printk(KERN_DEBUG "capi: no room in tty\n");
478#endif
Jan Kiszkaa11ef7b2010-02-08 10:12:36 +0000479 goto deref_ldisc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 }
Jan Kiszkaa11ef7b2010-02-08 10:12:36 +0000481
482 nskb = gen_data_b3_resp_for(mp, skb);
483 if (!nskb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 printk(KERN_ERR "capi: gen_data_b3_resp failed\n");
Jan Kiszkaa11ef7b2010-02-08 10:12:36 +0000485 goto deref_ldisc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 }
Jan Kiszkaa11ef7b2010-02-08 10:12:36 +0000487
488 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN + 4);
489
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 errcode = capi20_put_message(mp->ap, nskb);
Jan Kiszkaa11ef7b2010-02-08 10:12:36 +0000491
492 if (errcode == CAPI_NOERROR) {
493 skb_pull(skb, CAPIMSG_LEN(skb->data));
494#ifdef _DEBUG_DATAFLOW
495 printk(KERN_DEBUG "capi: DATA_B3_RESP %u len=%d => ldisc\n",
496 datahandle, skb->len);
497#endif
498 ld->ops->receive_buf(tty, skb->data, NULL, skb->len);
499 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 printk(KERN_ERR "capi: send DATA_B3_RESP failed=%x\n",
501 errcode);
502 kfree_skb(nskb);
Jan Kiszkaa11ef7b2010-02-08 10:12:36 +0000503
504 if (errcode == CAPI_SENDQUEUEFULL)
505 goto deref_ldisc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 }
Jan Kiszkaa11ef7b2010-02-08 10:12:36 +0000507
508free_skb:
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000509 ret = 0;
Jan Kiszkaa11ef7b2010-02-08 10:12:36 +0000510 kfree_skb(skb);
511
512deref_ldisc:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 tty_ldisc_deref(ld);
Jan Kiszkaa11ef7b2010-02-08 10:12:36 +0000514
515deref_tty:
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000516 tty_kref_put(tty);
517 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518}
519
520static void handle_minor_recv(struct capiminor *mp)
521{
522 struct sk_buff *skb;
Harvey Harrison2f9e9b62008-04-28 02:14:37 -0700523 while ((skb = skb_dequeue(&mp->inqueue)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 unsigned int len = skb->len;
525 mp->inbytes -= len;
526 if (handle_recv_skb(mp, skb) < 0) {
527 skb_queue_head(&mp->inqueue, skb);
528 mp->inbytes += len;
529 return;
530 }
531 }
532}
533
534static int handle_minor_send(struct capiminor *mp)
535{
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000536 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 struct sk_buff *skb;
538 u16 len;
539 int count = 0;
540 u16 errcode;
541 u16 datahandle;
542
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000543 tty = tty_port_tty_get(&mp->port);
544 if (!tty)
545 return 0;
546
547 if (mp->ttyoutstop) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548#if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
549 printk(KERN_DEBUG "capi: send: tty stopped\n");
550#endif
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000551 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 return 0;
553 }
554
Harvey Harrison2f9e9b62008-04-28 02:14:37 -0700555 while ((skb = skb_dequeue(&mp->outqueue)) != NULL) {
Jan Kiszka42792712010-02-08 10:12:38 +0000556 datahandle = atomic_inc_return(&mp->datahandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 len = (u16)skb->len;
558 skb_push(skb, CAPI_DATA_B3_REQ_LEN);
559 memset(skb->data, 0, CAPI_DATA_B3_REQ_LEN);
560 capimsg_setu16(skb->data, 0, CAPI_DATA_B3_REQ_LEN);
561 capimsg_setu16(skb->data, 2, mp->ap->applid);
562 capimsg_setu8 (skb->data, 4, CAPI_DATA_B3);
563 capimsg_setu8 (skb->data, 5, CAPI_REQ);
Jan Kiszka42792712010-02-08 10:12:38 +0000564 capimsg_setu16(skb->data, 6, atomic_inc_return(&mp->msgid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 capimsg_setu32(skb->data, 8, mp->ncci); /* NCCI */
Andrew Morton5e6c20a2007-07-17 04:04:22 -0700566 capimsg_setu32(skb->data, 12, (u32)(long)skb->data);/* Data32 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 capimsg_setu16(skb->data, 16, len); /* Data length */
568 capimsg_setu16(skb->data, 18, datahandle);
569 capimsg_setu16(skb->data, 20, 0); /* Flags */
570
Jan Kiszka501c87a2010-02-08 10:12:16 +0000571 if (capiminor_add_ack(mp, datahandle) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 skb_pull(skb, CAPI_DATA_B3_REQ_LEN);
573 skb_queue_head(&mp->outqueue, skb);
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000574 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 return count;
576 }
577 errcode = capi20_put_message(mp->ap, skb);
578 if (errcode == CAPI_NOERROR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 count++;
580 mp->outbytes -= len;
581#ifdef _DEBUG_DATAFLOW
582 printk(KERN_DEBUG "capi: DATA_B3_REQ %u len=%u\n",
583 datahandle, len);
584#endif
585 continue;
586 }
587 capiminor_del_ack(mp, datahandle);
588
589 if (errcode == CAPI_SENDQUEUEFULL) {
590 skb_pull(skb, CAPI_DATA_B3_REQ_LEN);
591 skb_queue_head(&mp->outqueue, skb);
592 break;
593 }
594
595 /* ups, drop packet */
596 printk(KERN_ERR "capi: put_message = %x\n", errcode);
597 mp->outbytes -= len;
598 kfree_skb(skb);
599 }
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000600 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 return count;
602}
603
604#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
605/* -------- function called by lower level -------------------------- */
606
607static void capi_recv_message(struct capi20_appl *ap, struct sk_buff *skb)
608{
609 struct capidev *cdev = ap->private;
610#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000611 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 struct capiminor *mp;
613 u16 datahandle;
614#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
615 struct capincci *np;
Michael Buesch053b47f2007-02-12 00:53:26 -0800616 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
Jan Kiszka05b41492010-02-08 10:12:19 +0000618 mutex_lock(&cdev->lock);
619
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_CONF) {
621 u16 info = CAPIMSG_U16(skb->data, 12); // Info field
Jan Kiszka05b41492010-02-08 10:12:19 +0000622 if ((info & 0xff00) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 }
Jan Kiszka05b41492010-02-08 10:12:19 +0000625 if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_IND)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
Jan Kiszka05b41492010-02-08 10:12:19 +0000627
Michael Buesch053b47f2007-02-12 00:53:26 -0800628 spin_lock_irqsave(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 if (CAPIMSG_COMMAND(skb->data) != CAPI_DATA_B3) {
630 skb_queue_tail(&cdev->recvqueue, skb);
631 wake_up_interruptible(&cdev->recvwait);
Jan Kiszka05b41492010-02-08 10:12:19 +0000632 goto unlock_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 }
Jan Kiszka05b41492010-02-08 10:12:19 +0000634
635 np = capincci_find(cdev, CAPIMSG_CONTROL(skb->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 if (!np) {
637 printk(KERN_ERR "BUG: capi_signal: ncci not found\n");
638 skb_queue_tail(&cdev->recvqueue, skb);
639 wake_up_interruptible(&cdev->recvwait);
Jan Kiszka05b41492010-02-08 10:12:19 +0000640 goto unlock_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 }
Jan Kiszka501c87a2010-02-08 10:12:16 +0000642
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643#ifndef CONFIG_ISDN_CAPI_MIDDLEWARE
644 skb_queue_tail(&cdev->recvqueue, skb);
645 wake_up_interruptible(&cdev->recvwait);
Jan Kiszka501c87a2010-02-08 10:12:16 +0000646
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647#else /* CONFIG_ISDN_CAPI_MIDDLEWARE */
Jan Kiszka501c87a2010-02-08 10:12:16 +0000648
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 mp = np->minorp;
650 if (!mp) {
651 skb_queue_tail(&cdev->recvqueue, skb);
652 wake_up_interruptible(&cdev->recvwait);
Jan Kiszka05b41492010-02-08 10:12:19 +0000653 goto unlock_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_IND) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4+4+2);
657#ifdef _DEBUG_DATAFLOW
658 printk(KERN_DEBUG "capi_signal: DATA_B3_IND %u len=%d\n",
659 datahandle, skb->len-CAPIMSG_LEN(skb->data));
660#endif
661 skb_queue_tail(&mp->inqueue, skb);
662 mp->inbytes += skb->len;
663 handle_minor_recv(mp);
664
665 } else if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_CONF) {
666
667 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4);
668#ifdef _DEBUG_DATAFLOW
669 printk(KERN_DEBUG "capi_signal: DATA_B3_CONF %u 0x%x\n",
670 datahandle,
671 CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4+2));
672#endif
673 kfree_skb(skb);
674 (void)capiminor_del_ack(mp, datahandle);
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000675 tty = tty_port_tty_get(&mp->port);
676 if (tty) {
677 tty_wakeup(tty);
678 tty_kref_put(tty);
679 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 (void)handle_minor_send(mp);
681
682 } else {
683 /* ups, let capi application handle it :-) */
684 skb_queue_tail(&cdev->recvqueue, skb);
685 wake_up_interruptible(&cdev->recvwait);
686 }
687#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
Jan Kiszka501c87a2010-02-08 10:12:16 +0000688
Jan Kiszka05b41492010-02-08 10:12:19 +0000689unlock_out:
Michael Buesch053b47f2007-02-12 00:53:26 -0800690 spin_unlock_irqrestore(&workaround_lock, flags);
Jan Kiszka05b41492010-02-08 10:12:19 +0000691 mutex_unlock(&cdev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692}
693
694/* -------- file_operations for capidev ----------------------------- */
695
696static ssize_t
697capi_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
698{
699 struct capidev *cdev = (struct capidev *)file->private_data;
700 struct sk_buff *skb;
701 size_t copied;
Jan Kiszka28a1dbb2010-02-08 10:12:21 +0000702 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
704 if (!cdev->ap.applid)
705 return -ENODEV;
706
Jan Kiszka28a1dbb2010-02-08 10:12:21 +0000707 skb = skb_dequeue(&cdev->recvqueue);
708 if (!skb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 if (file->f_flags & O_NONBLOCK)
710 return -EAGAIN;
Jan Kiszka28a1dbb2010-02-08 10:12:21 +0000711 err = wait_event_interruptible(cdev->recvwait,
712 (skb = skb_dequeue(&cdev->recvqueue)));
713 if (err)
714 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 }
716 if (skb->len > count) {
717 skb_queue_head(&cdev->recvqueue, skb);
718 return -EMSGSIZE;
719 }
720 if (copy_to_user(buf, skb->data, skb->len)) {
721 skb_queue_head(&cdev->recvqueue, skb);
722 return -EFAULT;
723 }
724 copied = skb->len;
725
726 kfree_skb(skb);
727
728 return copied;
729}
730
731static ssize_t
732capi_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
733{
734 struct capidev *cdev = (struct capidev *)file->private_data;
735 struct sk_buff *skb;
736 u16 mlen;
737
738 if (!cdev->ap.applid)
739 return -ENODEV;
740
741 skb = alloc_skb(count, GFP_USER);
742 if (!skb)
743 return -ENOMEM;
744
745 if (copy_from_user(skb_put(skb, count), buf, count)) {
746 kfree_skb(skb);
747 return -EFAULT;
748 }
749 mlen = CAPIMSG_LEN(skb->data);
750 if (CAPIMSG_CMD(skb->data) == CAPI_DATA_B3_REQ) {
751 if ((size_t)(mlen + CAPIMSG_DATALEN(skb->data)) != count) {
752 kfree_skb(skb);
753 return -EINVAL;
754 }
755 } else {
756 if (mlen != count) {
757 kfree_skb(skb);
758 return -EINVAL;
759 }
760 }
761 CAPIMSG_SETAPPID(skb->data, cdev->ap.applid);
762
763 if (CAPIMSG_CMD(skb->data) == CAPI_DISCONNECT_B3_RESP) {
Jan Kiszka05b41492010-02-08 10:12:19 +0000764 mutex_lock(&cdev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 capincci_free(cdev, CAPIMSG_NCCI(skb->data));
Jan Kiszka05b41492010-02-08 10:12:19 +0000766 mutex_unlock(&cdev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 }
768
769 cdev->errcode = capi20_put_message(&cdev->ap, skb);
770
771 if (cdev->errcode) {
772 kfree_skb(skb);
773 return -EIO;
774 }
775 return count;
776}
777
778static unsigned int
779capi_poll(struct file *file, poll_table * wait)
780{
781 struct capidev *cdev = (struct capidev *)file->private_data;
782 unsigned int mask = 0;
783
784 if (!cdev->ap.applid)
785 return POLLERR;
786
787 poll_wait(file, &(cdev->recvwait), wait);
788 mask = POLLOUT | POLLWRNORM;
789 if (!skb_queue_empty(&cdev->recvqueue))
790 mask |= POLLIN | POLLRDNORM;
791 return mask;
792}
793
794static int
795capi_ioctl(struct inode *inode, struct file *file,
796 unsigned int cmd, unsigned long arg)
797{
798 struct capidev *cdev = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 capi_ioctl_struct data;
800 int retval = -EINVAL;
801 void __user *argp = (void __user *)arg;
802
803 switch (cmd) {
804 case CAPI_REGISTER:
Jan Kiszka05b41492010-02-08 10:12:19 +0000805 mutex_lock(&cdev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
Jan Kiszka05b41492010-02-08 10:12:19 +0000807 if (cdev->ap.applid) {
808 retval = -EEXIST;
809 goto register_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 }
Jan Kiszka05b41492010-02-08 10:12:19 +0000811 if (copy_from_user(&cdev->ap.rparam, argp,
812 sizeof(struct capi_register_params))) {
813 retval = -EFAULT;
814 goto register_out;
815 }
816 cdev->ap.private = cdev;
817 cdev->ap.recv_message = capi_recv_message;
818 cdev->errcode = capi20_register(&cdev->ap);
819 retval = (int)cdev->ap.applid;
820 if (cdev->errcode) {
821 cdev->ap.applid = 0;
822 retval = -EIO;
823 }
824
825register_out:
826 mutex_unlock(&cdev->lock);
827 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
829 case CAPI_GET_VERSION:
830 {
831 if (copy_from_user(&data.contr, argp,
832 sizeof(data.contr)))
833 return -EFAULT;
834 cdev->errcode = capi20_get_version(data.contr, &data.version);
835 if (cdev->errcode)
836 return -EIO;
837 if (copy_to_user(argp, &data.version,
838 sizeof(data.version)))
839 return -EFAULT;
840 }
841 return 0;
842
843 case CAPI_GET_SERIAL:
844 {
845 if (copy_from_user(&data.contr, argp,
846 sizeof(data.contr)))
847 return -EFAULT;
848 cdev->errcode = capi20_get_serial (data.contr, data.serial);
849 if (cdev->errcode)
850 return -EIO;
851 if (copy_to_user(argp, data.serial,
852 sizeof(data.serial)))
853 return -EFAULT;
854 }
855 return 0;
856 case CAPI_GET_PROFILE:
857 {
858 if (copy_from_user(&data.contr, argp,
859 sizeof(data.contr)))
860 return -EFAULT;
861
862 if (data.contr == 0) {
863 cdev->errcode = capi20_get_profile(data.contr, &data.profile);
864 if (cdev->errcode)
865 return -EIO;
866
867 retval = copy_to_user(argp,
868 &data.profile.ncontroller,
869 sizeof(data.profile.ncontroller));
870
871 } else {
872 cdev->errcode = capi20_get_profile(data.contr, &data.profile);
873 if (cdev->errcode)
874 return -EIO;
875
876 retval = copy_to_user(argp, &data.profile,
877 sizeof(data.profile));
878 }
879 if (retval)
880 return -EFAULT;
881 }
882 return 0;
883
884 case CAPI_GET_MANUFACTURER:
885 {
886 if (copy_from_user(&data.contr, argp,
887 sizeof(data.contr)))
888 return -EFAULT;
889 cdev->errcode = capi20_get_manufacturer(data.contr, data.manufacturer);
890 if (cdev->errcode)
891 return -EIO;
892
893 if (copy_to_user(argp, data.manufacturer,
894 sizeof(data.manufacturer)))
895 return -EFAULT;
896
897 }
898 return 0;
899 case CAPI_GET_ERRCODE:
900 data.errcode = cdev->errcode;
901 cdev->errcode = CAPI_NOERROR;
902 if (arg) {
903 if (copy_to_user(argp, &data.errcode,
904 sizeof(data.errcode)))
905 return -EFAULT;
906 }
907 return data.errcode;
908
909 case CAPI_INSTALLED:
910 if (capi20_isinstalled() == CAPI_NOERROR)
911 return 0;
912 return -ENXIO;
913
914 case CAPI_MANUFACTURER_CMD:
915 {
916 struct capi_manufacturer_cmd mcmd;
917 if (!capable(CAP_SYS_ADMIN))
918 return -EPERM;
919 if (copy_from_user(&mcmd, argp, sizeof(mcmd)))
920 return -EFAULT;
921 return capi20_manufacturer(mcmd.cmd, mcmd.data);
922 }
923 return 0;
924
925 case CAPI_SET_FLAGS:
Jan Kiszka05b41492010-02-08 10:12:19 +0000926 case CAPI_CLR_FLAGS: {
927 unsigned userflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
Jan Kiszka05b41492010-02-08 10:12:19 +0000929 if (copy_from_user(&userflags, argp, sizeof(userflags)))
930 return -EFAULT;
931
932 mutex_lock(&cdev->lock);
933 if (cmd == CAPI_SET_FLAGS)
934 cdev->userflags |= userflags;
935 else
936 cdev->userflags &= ~userflags;
937 mutex_unlock(&cdev->lock);
938 return 0;
939 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 case CAPI_GET_FLAGS:
941 if (copy_to_user(argp, &cdev->userflags,
942 sizeof(cdev->userflags)))
943 return -EFAULT;
944 return 0;
945
Jan Kiszka05b41492010-02-08 10:12:19 +0000946 case CAPI_NCCI_OPENCOUNT: {
947 struct capincci *nccip;
948 unsigned ncci;
949 int count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
Jan Kiszka05b41492010-02-08 10:12:19 +0000951 if (copy_from_user(&ncci, argp, sizeof(ncci)))
952 return -EFAULT;
953
954 mutex_lock(&cdev->lock);
955 nccip = capincci_find(cdev, (u32)ncci);
956 if (nccip)
957 count = capincci_minor_opencount(nccip);
958 mutex_unlock(&cdev->lock);
959 return count;
960 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
962#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
Jan Kiszka05b41492010-02-08 10:12:19 +0000963 case CAPI_NCCI_GETUNIT: {
964 struct capincci *nccip;
965 struct capiminor *mp;
966 unsigned ncci;
967 int unit = -ESRCH;
968
969 if (copy_from_user(&ncci, argp, sizeof(ncci)))
970 return -EFAULT;
971
972 mutex_lock(&cdev->lock);
973 nccip = capincci_find(cdev, (u32)ncci);
974 if (nccip) {
975 mp = nccip->minorp;
976 if (mp)
977 unit = mp->minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 }
Jan Kiszka05b41492010-02-08 10:12:19 +0000979 mutex_unlock(&cdev->lock);
980 return unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 }
Jan Kiszka05b41492010-02-08 10:12:19 +0000982#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
983
984 default:
985 return -EINVAL;
986 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987}
988
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000989static int capi_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990{
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000991 struct capidev *cdev;
992
993 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
994 if (!cdev)
995 return -ENOMEM;
996
Jan Kiszka05b41492010-02-08 10:12:19 +0000997 mutex_init(&cdev->lock);
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000998 skb_queue_head_init(&cdev->recvqueue);
999 init_waitqueue_head(&cdev->recvwait);
Jan Kiszka884f5c42010-02-08 10:12:22 +00001000 INIT_LIST_HEAD(&cdev->nccis);
Jan Kiszkaeca39dd2010-02-08 10:12:18 +00001001 file->private_data = cdev;
1002
1003 mutex_lock(&capidev_list_lock);
1004 list_add_tail(&cdev->list, &capidev_list);
1005 mutex_unlock(&capidev_list_lock);
1006
1007 return nonseekable_open(inode, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008}
1009
Jan Kiszkaeca39dd2010-02-08 10:12:18 +00001010static int capi_release(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011{
Jan Kiszkaeca39dd2010-02-08 10:12:18 +00001012 struct capidev *cdev = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
Jan Kiszkaeca39dd2010-02-08 10:12:18 +00001014 mutex_lock(&capidev_list_lock);
1015 list_del(&cdev->list);
1016 mutex_unlock(&capidev_list_lock);
1017
Jan Kiszka05b41492010-02-08 10:12:19 +00001018 if (cdev->ap.applid)
Jan Kiszkaeca39dd2010-02-08 10:12:18 +00001019 capi20_release(&cdev->ap);
Jan Kiszkaeca39dd2010-02-08 10:12:18 +00001020 skb_queue_purge(&cdev->recvqueue);
Jan Kiszkaeca39dd2010-02-08 10:12:18 +00001021 capincci_free(cdev, 0xffffffff);
Jan Kiszkaeca39dd2010-02-08 10:12:18 +00001022
1023 kfree(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 return 0;
1025}
1026
Arjan van de Ven2b8693c2007-02-12 00:55:32 -08001027static const struct file_operations capi_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028{
1029 .owner = THIS_MODULE,
1030 .llseek = no_llseek,
1031 .read = capi_read,
1032 .write = capi_write,
1033 .poll = capi_poll,
1034 .ioctl = capi_ioctl,
1035 .open = capi_open,
1036 .release = capi_release,
1037};
1038
1039#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1040/* -------- tty_operations for capincci ----------------------------- */
1041
Jan Kiszka46324512010-02-08 10:12:28 +00001042static int
1043capinc_tty_install(struct tty_driver *driver, struct tty_struct *tty)
1044{
1045 int idx = tty->index;
1046 struct capiminor *mp = capiminor_get(idx);
1047 int ret = tty_init_termios(tty);
1048
1049 if (ret == 0) {
1050 tty_driver_kref_get(driver);
1051 tty->count++;
1052 tty->driver_data = mp;
1053 driver->ttys[idx] = tty;
1054 } else
1055 capiminor_put(mp);
1056 return ret;
1057}
1058
1059static void capinc_tty_cleanup(struct tty_struct *tty)
1060{
1061 struct capiminor *mp = tty->driver_data;
1062 tty->driver_data = NULL;
1063 capiminor_put(mp);
1064}
1065
Jan Kiszkafb4b4882010-02-08 10:12:29 +00001066static int capinc_tty_open(struct tty_struct *tty, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067{
Jan Kiszka46324512010-02-08 10:12:28 +00001068 struct capiminor *mp = tty->driver_data;
Michael Buesch053b47f2007-02-12 00:53:26 -08001069 unsigned long flags;
Jan Kiszkafb4b4882010-02-08 10:12:29 +00001070 int err;
1071
1072 err = tty_port_open(&mp->port, tty, filp);
1073 if (err)
1074 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
Michael Buesch053b47f2007-02-12 00:53:26 -08001076 spin_lock_irqsave(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 handle_minor_recv(mp);
Michael Buesch053b47f2007-02-12 00:53:26 -08001078 spin_unlock_irqrestore(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 return 0;
1080}
1081
Jan Kiszkafb4b4882010-02-08 10:12:29 +00001082static void capinc_tty_close(struct tty_struct *tty, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083{
Jan Kiszka46324512010-02-08 10:12:28 +00001084 struct capiminor *mp = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
Jan Kiszkafb4b4882010-02-08 10:12:29 +00001086 tty_port_close(&mp->port, tty, filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087}
1088
Jan Kiszka6576c282010-02-08 10:12:32 +00001089static int capinc_tty_write(struct tty_struct *tty,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 const unsigned char *buf, int count)
1091{
Jan Kiszka2c8df722010-02-08 10:12:30 +00001092 struct capiminor *mp = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 struct sk_buff *skb;
Michael Buesch053b47f2007-02-12 00:53:26 -08001094 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
1096#ifdef _DEBUG_TTYFUNCS
1097 printk(KERN_DEBUG "capinc_tty_write(count=%d)\n", count);
1098#endif
1099
Michael Buesch053b47f2007-02-12 00:53:26 -08001100 spin_lock_irqsave(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 skb = mp->ttyskb;
1102 if (skb) {
1103 mp->ttyskb = NULL;
1104 skb_queue_tail(&mp->outqueue, skb);
1105 mp->outbytes += skb->len;
1106 }
1107
1108 skb = alloc_skb(CAPI_DATA_B3_REQ_LEN+count, GFP_ATOMIC);
1109 if (!skb) {
1110 printk(KERN_ERR "capinc_tty_write: alloc_skb failed\n");
Michael Buesch053b47f2007-02-12 00:53:26 -08001111 spin_unlock_irqrestore(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 return -ENOMEM;
1113 }
1114
1115 skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
1116 memcpy(skb_put(skb, count), buf, count);
1117
1118 skb_queue_tail(&mp->outqueue, skb);
1119 mp->outbytes += skb->len;
1120 (void)handle_minor_send(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);