blob: 074496fae8da7b714edc9fdc64eba3c0b8d1f6c0 [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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Jan Kiszkadfbb84f2010-02-08 10:12:40 +000099 struct sk_buff_head inqueue;
100
101 struct sk_buff_head outqueue;
102 int outbytes;
103 struct sk_buff *outskb;
104 spinlock_t outlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
106 /* transmit path */
Michael Buesch6aa65472006-06-26 00:25:30 -0700107 struct list_head ackqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 int nack;
Michael Buesch6aa65472006-06-26 00:25:30 -0700109 spinlock_t ackqlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
112struct capincci {
Jan Kiszka884f5c42010-02-08 10:12:22 +0000113 struct list_head list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 u32 ncci;
115 struct capidev *cdev;
116#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
117 struct capiminor *minorp;
118#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
119};
120
121struct capidev {
122 struct list_head list;
123 struct capi20_appl ap;
124 u16 errcode;
125 unsigned userflags;
126
127 struct sk_buff_head recvqueue;
128 wait_queue_head_t recvwait;
129
Jan Kiszka884f5c42010-02-08 10:12:22 +0000130 struct list_head nccis;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Jan Kiszka05b41492010-02-08 10:12:19 +0000132 struct mutex lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133};
134
135/* -------- global variables ---------------------------------------- */
136
Jan Kiszkab8f433d2010-02-08 10:12:17 +0000137static DEFINE_MUTEX(capidev_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138static LIST_HEAD(capidev_list);
139
140#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
Jan Kiszka501c87a2010-02-08 10:12:16 +0000141
Jan Kiszka3d5d30f2010-02-08 10:12:33 +0000142static DEFINE_SPINLOCK(capiminors_lock);
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000143static struct capiminor **capiminors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Jan Kiszka40fb2d02010-02-08 10:12:25 +0000145static struct tty_driver *capinc_tty_driver;
146
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147/* -------- datahandles --------------------------------------------- */
148
Jan Kiszka501c87a2010-02-08 10:12:16 +0000149static int capiminor_add_ack(struct capiminor *mp, u16 datahandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150{
Jan Kiszka42651b52010-02-08 10:12:37 +0000151 struct ackqueue_entry *n;
Michael Buesch6aa65472006-06-26 00:25:30 -0700152 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
154 n = kmalloc(sizeof(*n), GFP_ATOMIC);
Michael Buesch6aa65472006-06-26 00:25:30 -0700155 if (unlikely(!n)) {
156 printk(KERN_ERR "capi: alloc datahandle failed\n");
157 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 n->datahandle = datahandle;
Michael Buesch6aa65472006-06-26 00:25:30 -0700160 INIT_LIST_HEAD(&n->list);
161 spin_lock_irqsave(&mp->ackqlock, flags);
162 list_add_tail(&n->list, &mp->ackqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 mp->nack++;
Michael Buesch6aa65472006-06-26 00:25:30 -0700164 spin_unlock_irqrestore(&mp->ackqlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 return 0;
166}
167
168static int capiminor_del_ack(struct capiminor *mp, u16 datahandle)
169{
Jan Kiszka42651b52010-02-08 10:12:37 +0000170 struct ackqueue_entry *p, *tmp;
Michael Buesch6aa65472006-06-26 00:25:30 -0700171 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Michael Buesch6aa65472006-06-26 00:25:30 -0700173 spin_lock_irqsave(&mp->ackqlock, flags);
174 list_for_each_entry_safe(p, tmp, &mp->ackqueue, list) {
175 if (p->datahandle == datahandle) {
176 list_del(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 kfree(p);
178 mp->nack--;
Michael Buesch6aa65472006-06-26 00:25:30 -0700179 spin_unlock_irqrestore(&mp->ackqlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 return 0;
181 }
182 }
Michael Buesch6aa65472006-06-26 00:25:30 -0700183 spin_unlock_irqrestore(&mp->ackqlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 return -1;
185}
186
187static void capiminor_del_all_ack(struct capiminor *mp)
188{
Jan Kiszka42651b52010-02-08 10:12:37 +0000189 struct ackqueue_entry *p, *tmp;
Michael Buesch6aa65472006-06-26 00:25:30 -0700190 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
Michael Buesch6aa65472006-06-26 00:25:30 -0700192 spin_lock_irqsave(&mp->ackqlock, flags);
193 list_for_each_entry_safe(p, tmp, &mp->ackqueue, list) {
194 list_del(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 kfree(p);
196 mp->nack--;
197 }
Michael Buesch6aa65472006-06-26 00:25:30 -0700198 spin_unlock_irqrestore(&mp->ackqlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199}
200
201
202/* -------- struct capiminor ---------------------------------------- */
203
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000204static const struct tty_port_operations capiminor_port_ops; /* we have none */
205
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206static struct capiminor *capiminor_alloc(struct capi20_appl *ap, u32 ncci)
207{
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000208 struct capiminor *mp;
Jan Kiszka40fb2d02010-02-08 10:12:25 +0000209 struct device *dev;
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000210 unsigned int minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Jan Kiszka54f0fad2010-02-08 10:12:20 +0000212 mp = kzalloc(sizeof(*mp), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 if (!mp) {
214 printk(KERN_ERR "capi: can't alloc capiminor\n");
215 return NULL;
216 }
217
Jan Kiszka0159d542010-02-08 10:12:27 +0000218 kref_init(&mp->kref);
219
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 mp->ap = ap;
221 mp->ncci = ncci;
Michael Buesch6aa65472006-06-26 00:25:30 -0700222 INIT_LIST_HEAD(&mp->ackqueue);
223 spin_lock_init(&mp->ackqlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
225 skb_queue_head_init(&mp->inqueue);
226 skb_queue_head_init(&mp->outqueue);
Jan Kiszkadfbb84f2010-02-08 10:12:40 +0000227 spin_lock_init(&mp->outlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000229 tty_port_init(&mp->port);
230 mp->port.ops = &capiminor_port_ops;
231
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000232 /* Allocate the least unused minor number. */
Jan Kiszka3d5d30f2010-02-08 10:12:33 +0000233 spin_lock(&capiminors_lock);
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000234 for (minor = 0; minor < capi_ttyminors; minor++)
235 if (!capiminors[minor]) {
236 capiminors[minor] = mp;
237 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 }
Jan Kiszka3d5d30f2010-02-08 10:12:33 +0000239 spin_unlock(&capiminors_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000241 if (minor == capi_ttyminors) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 printk(KERN_NOTICE "capi: out of minors\n");
Jan Kiszka40fb2d02010-02-08 10:12:25 +0000243 goto err_out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 }
245
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000246 mp->minor = minor;
247
Jan Kiszka40fb2d02010-02-08 10:12:25 +0000248 dev = tty_register_device(capinc_tty_driver, minor, NULL);
249 if (IS_ERR(dev))
250 goto err_out2;
251
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 return mp;
Jan Kiszka40fb2d02010-02-08 10:12:25 +0000253
254err_out2:
Jan Kiszka3d5d30f2010-02-08 10:12:33 +0000255 spin_lock(&capiminors_lock);
Jan Kiszka40fb2d02010-02-08 10:12:25 +0000256 capiminors[minor] = NULL;
Jan Kiszka3d5d30f2010-02-08 10:12:33 +0000257 spin_unlock(&capiminors_lock);
Jan Kiszka40fb2d02010-02-08 10:12:25 +0000258
259err_out1:
260 kfree(mp);
261 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262}
263
Jan Kiszka0159d542010-02-08 10:12:27 +0000264static void capiminor_destroy(struct kref *kref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{
Jan Kiszka0159d542010-02-08 10:12:27 +0000266 struct capiminor *mp = container_of(kref, struct capiminor, kref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Jan Kiszkadfbb84f2010-02-08 10:12:40 +0000268 kfree_skb(mp->outskb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 skb_queue_purge(&mp->inqueue);
270 skb_queue_purge(&mp->outqueue);
271 capiminor_del_all_ack(mp);
272 kfree(mp);
273}
274
Jan Kiszka40fb2d02010-02-08 10:12:25 +0000275static struct capiminor *capiminor_get(unsigned int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276{
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000277 struct capiminor *mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
Jan Kiszka3d5d30f2010-02-08 10:12:33 +0000279 spin_lock(&capiminors_lock);
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000280 mp = capiminors[minor];
Jan Kiszka0159d542010-02-08 10:12:27 +0000281 if (mp)
282 kref_get(&mp->kref);
Jan Kiszka3d5d30f2010-02-08 10:12:33 +0000283 spin_unlock(&capiminors_lock);
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000284
285 return mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
Jan Kiszka0159d542010-02-08 10:12:27 +0000288static inline void capiminor_put(struct capiminor *mp)
289{
290 kref_put(&mp->kref, capiminor_destroy);
291}
292
293static void capiminor_free(struct capiminor *mp)
294{
Jan Kiszka0159d542010-02-08 10:12:27 +0000295 tty_unregister_device(capinc_tty_driver, mp->minor);
296
Jan Kiszka3d5d30f2010-02-08 10:12:33 +0000297 spin_lock(&capiminors_lock);
Jan Kiszka0159d542010-02-08 10:12:27 +0000298 capiminors[mp->minor] = NULL;
Jan Kiszka3d5d30f2010-02-08 10:12:33 +0000299 spin_unlock(&capiminors_lock);
Jan Kiszka0159d542010-02-08 10:12:27 +0000300
301 capiminor_put(mp);
302}
303
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304/* -------- struct capincci ----------------------------------------- */
305
Jan Kiszka501c87a2010-02-08 10:12:16 +0000306static void capincci_alloc_minor(struct capidev *cdev, struct capincci *np)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307{
Jan Kiszka501c87a2010-02-08 10:12:16 +0000308 struct capiminor *mp;
Jan Kiszkae95ac142010-02-08 10:12:26 +0000309 dev_t device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Jan Kiszka501c87a2010-02-08 10:12:16 +0000311 if (!(cdev->userflags & CAPIFLAG_HIGHJACKING))
312 return;
313
314 mp = np->minorp = capiminor_alloc(&cdev->ap, np->ncci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 if (mp) {
Jan Kiszkae95ac142010-02-08 10:12:26 +0000316 device = MKDEV(capinc_tty_driver->major, mp->minor);
317 mp->capifs_dentry = capifs_new_ncci(mp->minor, device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 }
Jan Kiszka501c87a2010-02-08 10:12:16 +0000319}
320
321static void capincci_free_minor(struct capincci *np)
322{
323 struct capiminor *mp = np->minorp;
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000324 struct tty_struct *tty;
Jan Kiszka501c87a2010-02-08 10:12:16 +0000325
326 if (mp) {
327 capifs_free_ncci(mp->capifs_dentry);
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000328
329 tty = tty_port_tty_get(&mp->port);
330 if (tty) {
Jan Kiszka30bced92010-02-08 10:12:31 +0000331 tty_vhangup(tty);
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000332 tty_kref_put(tty);
Jan Kiszka501c87a2010-02-08 10:12:16 +0000333 }
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000334
335 capiminor_free(mp);
Jan Kiszka501c87a2010-02-08 10:12:16 +0000336 }
337}
338
339static inline unsigned int capincci_minor_opencount(struct capincci *np)
340{
341 struct capiminor *mp = np->minorp;
Jan Kiszkaa84fdf42010-02-08 10:12:34 +0000342 unsigned int count = 0;
343 struct tty_struct *tty;
Jan Kiszka501c87a2010-02-08 10:12:16 +0000344
Jan Kiszkaa84fdf42010-02-08 10:12:34 +0000345 if (mp) {
346 tty = tty_port_tty_get(&mp->port);
347 if (tty) {
348 count = tty->count;
349 tty_kref_put(tty);
350 }
351 }
352 return count;
Jan Kiszka501c87a2010-02-08 10:12:16 +0000353}
354
355#else /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
356
357static inline void
358capincci_alloc_minor(struct capidev *cdev, struct capincci *np) { }
359static inline void capincci_free_minor(struct capincci *np) { }
360
361static inline unsigned int capincci_minor_opencount(struct capincci *np)
362{
363 return 0;
364}
365
366#endif /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
367
368static struct capincci *capincci_alloc(struct capidev *cdev, u32 ncci)
369{
Jan Kiszka884f5c42010-02-08 10:12:22 +0000370 struct capincci *np;
Jan Kiszka501c87a2010-02-08 10:12:16 +0000371
Jan Kiszka54f0fad2010-02-08 10:12:20 +0000372 np = kzalloc(sizeof(*np), GFP_KERNEL);
Jan Kiszka501c87a2010-02-08 10:12:16 +0000373 if (!np)
374 return NULL;
375 np->ncci = ncci;
376 np->cdev = cdev;
377
378 capincci_alloc_minor(cdev, np);
379
Jan Kiszka884f5c42010-02-08 10:12:22 +0000380 list_add_tail(&np->list, &cdev->nccis);
381
382 return np;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383}
384
385static void capincci_free(struct capidev *cdev, u32 ncci)
386{
Jan Kiszka884f5c42010-02-08 10:12:22 +0000387 struct capincci *np, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
Jan Kiszka884f5c42010-02-08 10:12:22 +0000389 list_for_each_entry_safe(np, tmp, &cdev->nccis, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 if (ncci == 0xffffffff || np->ncci == ncci) {
Jan Kiszka501c87a2010-02-08 10:12:16 +0000391 capincci_free_minor(np);
Jan Kiszka884f5c42010-02-08 10:12:22 +0000392 list_del(&np->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 kfree(np);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395}
396
397static struct capincci *capincci_find(struct capidev *cdev, u32 ncci)
398{
Jan Kiszka884f5c42010-02-08 10:12:22 +0000399 struct capincci *np;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
Jan Kiszka884f5c42010-02-08 10:12:22 +0000401 list_for_each_entry(np, &cdev->nccis, list)
402 if (np->ncci == ncci)
403 return np;
404 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405}
406
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
408/* -------- handle data queue --------------------------------------- */
409
410static struct sk_buff *
411gen_data_b3_resp_for(struct capiminor *mp, struct sk_buff *skb)
412{
413 struct sk_buff *nskb;
Jan Kiszkadfbb84f2010-02-08 10:12:40 +0000414 nskb = alloc_skb(CAPI_DATA_B3_RESP_LEN, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 if (nskb) {
416 u16 datahandle = CAPIMSG_U16(skb->data,CAPIMSG_BASELEN+4+4+2);
417 unsigned char *s = skb_put(nskb, CAPI_DATA_B3_RESP_LEN);
418 capimsg_setu16(s, 0, CAPI_DATA_B3_RESP_LEN);
419 capimsg_setu16(s, 2, mp->ap->applid);
420 capimsg_setu8 (s, 4, CAPI_DATA_B3);
421 capimsg_setu8 (s, 5, CAPI_RESP);
Jan Kiszka42792712010-02-08 10:12:38 +0000422 capimsg_setu16(s, 6, atomic_inc_return(&mp->msgid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 capimsg_setu32(s, 8, mp->ncci);
424 capimsg_setu16(s, 12, datahandle);
425 }
426 return nskb;
427}
428
429static int handle_recv_skb(struct capiminor *mp, struct sk_buff *skb)
430{
Jan Kiszkaa11ef7b2010-02-08 10:12:36 +0000431 unsigned int datalen = skb->len - CAPIMSG_LEN(skb->data);
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000432 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 struct sk_buff *nskb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 u16 errcode, datahandle;
435 struct tty_ldisc *ld;
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000436 int ret = -1;
437
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000438 tty = tty_port_tty_get(&mp->port);
439 if (!tty) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440#ifdef _DEBUG_DATAFLOW
441 printk(KERN_DEBUG "capi: currently no receiver\n");
442#endif
443 return -1;
444 }
445
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000446 ld = tty_ldisc_ref(tty);
Jan Kiszkaa11ef7b2010-02-08 10:12:36 +0000447 if (!ld) {
448 /* fatal error, do not requeue */
449 ret = 0;
450 kfree_skb(skb);
451 goto deref_tty;
452 }
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000453
Alan Coxa352def2008-07-16 21:53:12 +0100454 if (ld->ops->receive_buf == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455#if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
456 printk(KERN_DEBUG "capi: ldisc has no receive_buf function\n");
457#endif
Jan Kiszkaa11ef7b2010-02-08 10:12:36 +0000458 /* fatal error, do not requeue */
459 goto free_skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 }
461 if (mp->ttyinstop) {
462#if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
463 printk(KERN_DEBUG "capi: recv tty throttled\n");
464#endif
Jan Kiszkaa11ef7b2010-02-08 10:12:36 +0000465 goto deref_ldisc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 }
Jan Kiszkaa11ef7b2010-02-08 10:12:36 +0000467
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000468 if (tty->receive_room < datalen) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469#if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
470 printk(KERN_DEBUG "capi: no room in tty\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
475 nskb = gen_data_b3_resp_for(mp, skb);
476 if (!nskb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 printk(KERN_ERR "capi: gen_data_b3_resp failed\n");
Jan Kiszkaa11ef7b2010-02-08 10:12:36 +0000478 goto deref_ldisc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 }
Jan Kiszkaa11ef7b2010-02-08 10:12:36 +0000480
481 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN + 4);
482
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 errcode = capi20_put_message(mp->ap, nskb);
Jan Kiszkaa11ef7b2010-02-08 10:12:36 +0000484
485 if (errcode == CAPI_NOERROR) {
486 skb_pull(skb, CAPIMSG_LEN(skb->data));
487#ifdef _DEBUG_DATAFLOW
488 printk(KERN_DEBUG "capi: DATA_B3_RESP %u len=%d => ldisc\n",
489 datahandle, skb->len);
490#endif
491 ld->ops->receive_buf(tty, skb->data, NULL, skb->len);
492 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 printk(KERN_ERR "capi: send DATA_B3_RESP failed=%x\n",
494 errcode);
495 kfree_skb(nskb);
Jan Kiszkaa11ef7b2010-02-08 10:12:36 +0000496
497 if (errcode == CAPI_SENDQUEUEFULL)
498 goto deref_ldisc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 }
Jan Kiszkaa11ef7b2010-02-08 10:12:36 +0000500
501free_skb:
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000502 ret = 0;
Jan Kiszkaa11ef7b2010-02-08 10:12:36 +0000503 kfree_skb(skb);
504
505deref_ldisc:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 tty_ldisc_deref(ld);
Jan Kiszkaa11ef7b2010-02-08 10:12:36 +0000507
508deref_tty:
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000509 tty_kref_put(tty);
510 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511}
512
513static void handle_minor_recv(struct capiminor *mp)
514{
515 struct sk_buff *skb;
Jan Kiszka68d73472010-02-08 10:12:39 +0000516
517 while ((skb = skb_dequeue(&mp->inqueue)) != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 if (handle_recv_skb(mp, skb) < 0) {
519 skb_queue_head(&mp->inqueue, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 return;
521 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522}
523
524static int handle_minor_send(struct capiminor *mp)
525{
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000526 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 struct sk_buff *skb;
528 u16 len;
529 int count = 0;
530 u16 errcode;
531 u16 datahandle;
532
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000533 tty = tty_port_tty_get(&mp->port);
534 if (!tty)
535 return 0;
536
537 if (mp->ttyoutstop) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538#if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
539 printk(KERN_DEBUG "capi: send: tty stopped\n");
540#endif
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000541 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 return 0;
543 }
544
Jan Kiszkadfbb84f2010-02-08 10:12:40 +0000545 while (1) {
546 spin_lock_bh(&mp->outlock);
547 skb = __skb_dequeue(&mp->outqueue);
548 if (!skb) {
549 spin_unlock_bh(&mp->outlock);
550 break;
551 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 len = (u16)skb->len;
Jan Kiszkadfbb84f2010-02-08 10:12:40 +0000553 mp->outbytes -= len;
554 spin_unlock_bh(&mp->outlock);
555
556 datahandle = atomic_inc_return(&mp->datahandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 skb_push(skb, CAPI_DATA_B3_REQ_LEN);
558 memset(skb->data, 0, CAPI_DATA_B3_REQ_LEN);
559 capimsg_setu16(skb->data, 0, CAPI_DATA_B3_REQ_LEN);
560 capimsg_setu16(skb->data, 2, mp->ap->applid);
561 capimsg_setu8 (skb->data, 4, CAPI_DATA_B3);
562 capimsg_setu8 (skb->data, 5, CAPI_REQ);
Jan Kiszka42792712010-02-08 10:12:38 +0000563 capimsg_setu16(skb->data, 6, atomic_inc_return(&mp->msgid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 capimsg_setu32(skb->data, 8, mp->ncci); /* NCCI */
Andrew Morton5e6c20a2007-07-17 04:04:22 -0700565 capimsg_setu32(skb->data, 12, (u32)(long)skb->data);/* Data32 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 capimsg_setu16(skb->data, 16, len); /* Data length */
567 capimsg_setu16(skb->data, 18, datahandle);
568 capimsg_setu16(skb->data, 20, 0); /* Flags */
569
Jan Kiszka501c87a2010-02-08 10:12:16 +0000570 if (capiminor_add_ack(mp, datahandle) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 skb_pull(skb, CAPI_DATA_B3_REQ_LEN);
Jan Kiszkadfbb84f2010-02-08 10:12:40 +0000572
573 spin_lock_bh(&mp->outlock);
574 __skb_queue_head(&mp->outqueue, skb);
575 mp->outbytes += len;
576 spin_unlock_bh(&mp->outlock);
577
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000578 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 return count;
580 }
581 errcode = capi20_put_message(mp->ap, skb);
582 if (errcode == CAPI_NOERROR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584#ifdef _DEBUG_DATAFLOW
585 printk(KERN_DEBUG "capi: DATA_B3_REQ %u len=%u\n",
586 datahandle, len);
587#endif
588 continue;
589 }
590 capiminor_del_ack(mp, datahandle);
591
592 if (errcode == CAPI_SENDQUEUEFULL) {
593 skb_pull(skb, CAPI_DATA_B3_REQ_LEN);
Jan Kiszkadfbb84f2010-02-08 10:12:40 +0000594
595 spin_lock_bh(&mp->outlock);
596 __skb_queue_head(&mp->outqueue, skb);
597 mp->outbytes += len;
598 spin_unlock_bh(&mp->outlock);
599
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 break;
601 }
602
603 /* ups, drop packet */
604 printk(KERN_ERR "capi: put_message = %x\n", errcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 kfree_skb(skb);
606 }
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000607 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 return count;
609}
610
611#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
612/* -------- function called by lower level -------------------------- */
613
614static void capi_recv_message(struct capi20_appl *ap, struct sk_buff *skb)
615{
616 struct capidev *cdev = ap->private;
617#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000618 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 struct capiminor *mp;
620 u16 datahandle;
621#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
622 struct capincci *np;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
Jan Kiszka05b41492010-02-08 10:12:19 +0000624 mutex_lock(&cdev->lock);
625
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_CONF) {
627 u16 info = CAPIMSG_U16(skb->data, 12); // Info field
Jan Kiszka05b41492010-02-08 10:12:19 +0000628 if ((info & 0xff00) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 }
Jan Kiszka05b41492010-02-08 10:12:19 +0000631 if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_IND)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
Jan Kiszka05b41492010-02-08 10:12:19 +0000633
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 if (CAPIMSG_COMMAND(skb->data) != CAPI_DATA_B3) {
635 skb_queue_tail(&cdev->recvqueue, skb);
636 wake_up_interruptible(&cdev->recvwait);
Jan Kiszka05b41492010-02-08 10:12:19 +0000637 goto unlock_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 }
Jan Kiszka05b41492010-02-08 10:12:19 +0000639
640 np = capincci_find(cdev, CAPIMSG_CONTROL(skb->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 if (!np) {
642 printk(KERN_ERR "BUG: capi_signal: ncci not found\n");
643 skb_queue_tail(&cdev->recvqueue, skb);
644 wake_up_interruptible(&cdev->recvwait);
Jan Kiszka05b41492010-02-08 10:12:19 +0000645 goto unlock_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 }
Jan Kiszka501c87a2010-02-08 10:12:16 +0000647
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648#ifndef CONFIG_ISDN_CAPI_MIDDLEWARE
649 skb_queue_tail(&cdev->recvqueue, skb);
650 wake_up_interruptible(&cdev->recvwait);
Jan Kiszka501c87a2010-02-08 10:12:16 +0000651
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652#else /* CONFIG_ISDN_CAPI_MIDDLEWARE */
Jan Kiszka501c87a2010-02-08 10:12:16 +0000653
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 mp = np->minorp;
655 if (!mp) {
656 skb_queue_tail(&cdev->recvqueue, skb);
657 wake_up_interruptible(&cdev->recvwait);
Jan Kiszka05b41492010-02-08 10:12:19 +0000658 goto unlock_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_IND) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4+4+2);
662#ifdef _DEBUG_DATAFLOW
663 printk(KERN_DEBUG "capi_signal: DATA_B3_IND %u len=%d\n",
664 datahandle, skb->len-CAPIMSG_LEN(skb->data));
665#endif
666 skb_queue_tail(&mp->inqueue, skb);
Jan Kiszka68d73472010-02-08 10:12:39 +0000667
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 handle_minor_recv(mp);
669
670 } else if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_CONF) {
671
672 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4);
673#ifdef _DEBUG_DATAFLOW
674 printk(KERN_DEBUG "capi_signal: DATA_B3_CONF %u 0x%x\n",
675 datahandle,
676 CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4+2));
677#endif
678 kfree_skb(skb);
679 (void)capiminor_del_ack(mp, datahandle);
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000680 tty = tty_port_tty_get(&mp->port);
681 if (tty) {
682 tty_wakeup(tty);
683 tty_kref_put(tty);
684 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 (void)handle_minor_send(mp);
686
687 } else {
688 /* ups, let capi application handle it :-) */
689 skb_queue_tail(&cdev->recvqueue, skb);
690 wake_up_interruptible(&cdev->recvwait);
691 }
692#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
Jan Kiszka501c87a2010-02-08 10:12:16 +0000693
Jan Kiszka05b41492010-02-08 10:12:19 +0000694unlock_out:
Jan Kiszka05b41492010-02-08 10:12:19 +0000695 mutex_unlock(&cdev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696}
697
698/* -------- file_operations for capidev ----------------------------- */
699
700static ssize_t
701capi_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
702{
703 struct capidev *cdev = (struct capidev *)file->private_data;
704 struct sk_buff *skb;
705 size_t copied;
Jan Kiszka28a1dbb2010-02-08 10:12:21 +0000706 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
708 if (!cdev->ap.applid)
709 return -ENODEV;
710
Jan Kiszka28a1dbb2010-02-08 10:12:21 +0000711 skb = skb_dequeue(&cdev->recvqueue);
712 if (!skb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 if (file->f_flags & O_NONBLOCK)
714 return -EAGAIN;
Jan Kiszka28a1dbb2010-02-08 10:12:21 +0000715 err = wait_event_interruptible(cdev->recvwait,
716 (skb = skb_dequeue(&cdev->recvqueue)));
717 if (err)
718 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 }
720 if (skb->len > count) {
721 skb_queue_head(&cdev->recvqueue, skb);
722 return -EMSGSIZE;
723 }
724 if (copy_to_user(buf, skb->data, skb->len)) {
725 skb_queue_head(&cdev->recvqueue, skb);
726 return -EFAULT;
727 }
728 copied = skb->len;
729
730 kfree_skb(skb);
731
732 return copied;
733}
734
735static ssize_t
736capi_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
737{
738 struct capidev *cdev = (struct capidev *)file->private_data;
739 struct sk_buff *skb;
740 u16 mlen;
741
742 if (!cdev->ap.applid)
743 return -ENODEV;
744
745 skb = alloc_skb(count, GFP_USER);
746 if (!skb)
747 return -ENOMEM;
748
749 if (copy_from_user(skb_put(skb, count), buf, count)) {
750 kfree_skb(skb);
751 return -EFAULT;
752 }
753 mlen = CAPIMSG_LEN(skb->data);
754 if (CAPIMSG_CMD(skb->data) == CAPI_DATA_B3_REQ) {
755 if ((size_t)(mlen + CAPIMSG_DATALEN(skb->data)) != count) {
756 kfree_skb(skb);
757 return -EINVAL;
758 }
759 } else {
760 if (mlen != count) {
761 kfree_skb(skb);
762 return -EINVAL;
763 }
764 }
765 CAPIMSG_SETAPPID(skb->data, cdev->ap.applid);
766
767 if (CAPIMSG_CMD(skb->data) == CAPI_DISCONNECT_B3_RESP) {
Jan Kiszka05b41492010-02-08 10:12:19 +0000768 mutex_lock(&cdev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 capincci_free(cdev, CAPIMSG_NCCI(skb->data));
Jan Kiszka05b41492010-02-08 10:12:19 +0000770 mutex_unlock(&cdev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 }
772
773 cdev->errcode = capi20_put_message(&cdev->ap, skb);
774
775 if (cdev->errcode) {
776 kfree_skb(skb);
777 return -EIO;
778 }
779 return count;
780}
781
782static unsigned int
783capi_poll(struct file *file, poll_table * wait)
784{
785 struct capidev *cdev = (struct capidev *)file->private_data;
786 unsigned int mask = 0;
787
788 if (!cdev->ap.applid)
789 return POLLERR;
790
791 poll_wait(file, &(cdev->recvwait), wait);
792 mask = POLLOUT | POLLWRNORM;
793 if (!skb_queue_empty(&cdev->recvqueue))
794 mask |= POLLIN | POLLRDNORM;
795 return mask;
796}
797
798static int
799capi_ioctl(struct inode *inode, struct file *file,
800 unsigned int cmd, unsigned long arg)
801{
802 struct capidev *cdev = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 capi_ioctl_struct data;
804 int retval = -EINVAL;
805 void __user *argp = (void __user *)arg;
806
807 switch (cmd) {
808 case CAPI_REGISTER:
Jan Kiszka05b41492010-02-08 10:12:19 +0000809 mutex_lock(&cdev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
Jan Kiszka05b41492010-02-08 10:12:19 +0000811 if (cdev->ap.applid) {
812 retval = -EEXIST;
813 goto register_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 }
Jan Kiszka05b41492010-02-08 10:12:19 +0000815 if (copy_from_user(&cdev->ap.rparam, argp,
816 sizeof(struct capi_register_params))) {
817 retval = -EFAULT;
818 goto register_out;
819 }
820 cdev->ap.private = cdev;
821 cdev->ap.recv_message = capi_recv_message;
822 cdev->errcode = capi20_register(&cdev->ap);
823 retval = (int)cdev->ap.applid;
824 if (cdev->errcode) {
825 cdev->ap.applid = 0;
826 retval = -EIO;
827 }
828
829register_out:
830 mutex_unlock(&cdev->lock);
831 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
833 case CAPI_GET_VERSION:
834 {
835 if (copy_from_user(&data.contr, argp,
836 sizeof(data.contr)))
837 return -EFAULT;
838 cdev->errcode = capi20_get_version(data.contr, &data.version);
839 if (cdev->errcode)
840 return -EIO;
841 if (copy_to_user(argp, &data.version,
842 sizeof(data.version)))
843 return -EFAULT;
844 }
845 return 0;
846
847 case CAPI_GET_SERIAL:
848 {
849 if (copy_from_user(&data.contr, argp,
850 sizeof(data.contr)))
851 return -EFAULT;
852 cdev->errcode = capi20_get_serial (data.contr, data.serial);
853 if (cdev->errcode)
854 return -EIO;
855 if (copy_to_user(argp, data.serial,
856 sizeof(data.serial)))
857 return -EFAULT;
858 }
859 return 0;
860 case CAPI_GET_PROFILE:
861 {
862 if (copy_from_user(&data.contr, argp,
863 sizeof(data.contr)))
864 return -EFAULT;
865
866 if (data.contr == 0) {
867 cdev->errcode = capi20_get_profile(data.contr, &data.profile);
868 if (cdev->errcode)
869 return -EIO;
870
871 retval = copy_to_user(argp,
872 &data.profile.ncontroller,
873 sizeof(data.profile.ncontroller));
874
875 } else {
876 cdev->errcode = capi20_get_profile(data.contr, &data.profile);
877 if (cdev->errcode)
878 return -EIO;
879
880 retval = copy_to_user(argp, &data.profile,
881 sizeof(data.profile));
882 }
883 if (retval)
884 return -EFAULT;
885 }
886 return 0;
887
888 case CAPI_GET_MANUFACTURER:
889 {
890 if (copy_from_user(&data.contr, argp,
891 sizeof(data.contr)))
892 return -EFAULT;
893 cdev->errcode = capi20_get_manufacturer(data.contr, data.manufacturer);
894 if (cdev->errcode)
895 return -EIO;
896
897 if (copy_to_user(argp, data.manufacturer,
898 sizeof(data.manufacturer)))
899 return -EFAULT;
900
901 }
902 return 0;
903 case CAPI_GET_ERRCODE:
904 data.errcode = cdev->errcode;
905 cdev->errcode = CAPI_NOERROR;
906 if (arg) {
907 if (copy_to_user(argp, &data.errcode,
908 sizeof(data.errcode)))
909 return -EFAULT;
910 }
911 return data.errcode;
912
913 case CAPI_INSTALLED:
914 if (capi20_isinstalled() == CAPI_NOERROR)
915 return 0;
916 return -ENXIO;
917
918 case CAPI_MANUFACTURER_CMD:
919 {
920 struct capi_manufacturer_cmd mcmd;
921 if (!capable(CAP_SYS_ADMIN))
922 return -EPERM;
923 if (copy_from_user(&mcmd, argp, sizeof(mcmd)))
924 return -EFAULT;
925 return capi20_manufacturer(mcmd.cmd, mcmd.data);
926 }
927 return 0;
928
929 case CAPI_SET_FLAGS:
Jan Kiszka05b41492010-02-08 10:12:19 +0000930 case CAPI_CLR_FLAGS: {
931 unsigned userflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
Jan Kiszka05b41492010-02-08 10:12:19 +0000933 if (copy_from_user(&userflags, argp, sizeof(userflags)))
934 return -EFAULT;
935
936 mutex_lock(&cdev->lock);
937 if (cmd == CAPI_SET_FLAGS)
938 cdev->userflags |= userflags;
939 else
940 cdev->userflags &= ~userflags;
941 mutex_unlock(&cdev->lock);
942 return 0;
943 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 case CAPI_GET_FLAGS:
945 if (copy_to_user(argp, &cdev->userflags,
946 sizeof(cdev->userflags)))
947 return -EFAULT;
948 return 0;
949
Jan Kiszka05b41492010-02-08 10:12:19 +0000950 case CAPI_NCCI_OPENCOUNT: {
951 struct capincci *nccip;
952 unsigned ncci;
953 int count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
Jan Kiszka05b41492010-02-08 10:12:19 +0000955 if (copy_from_user(&ncci, argp, sizeof(ncci)))
956 return -EFAULT;
957
958 mutex_lock(&cdev->lock);
959 nccip = capincci_find(cdev, (u32)ncci);
960 if (nccip)
961 count = capincci_minor_opencount(nccip);
962 mutex_unlock(&cdev->lock);
963 return count;
964 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
966#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
Jan Kiszka05b41492010-02-08 10:12:19 +0000967 case CAPI_NCCI_GETUNIT: {
968 struct capincci *nccip;
969 struct capiminor *mp;
970 unsigned ncci;
971 int unit = -ESRCH;
972
973 if (copy_from_user(&ncci, argp, sizeof(ncci)))
974 return -EFAULT;
975
976 mutex_lock(&cdev->lock);
977 nccip = capincci_find(cdev, (u32)ncci);
978 if (nccip) {
979 mp = nccip->minorp;
980 if (mp)
981 unit = mp->minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 }
Jan Kiszka05b41492010-02-08 10:12:19 +0000983 mutex_unlock(&cdev->lock);
984 return unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 }
Jan Kiszka05b41492010-02-08 10:12:19 +0000986#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
987
988 default:
989 return -EINVAL;
990 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991}
992
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000993static int capi_open(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;
996
997 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
998 if (!cdev)
999 return -ENOMEM;
1000
Jan Kiszka05b41492010-02-08 10:12:19 +00001001 mutex_init(&cdev->lock);
Jan Kiszkaeca39dd2010-02-08 10:12:18 +00001002 skb_queue_head_init(&cdev->recvqueue);
1003 init_waitqueue_head(&cdev->recvwait);
Jan Kiszka884f5c42010-02-08 10:12:22 +00001004 INIT_LIST_HEAD(&cdev->nccis);
Jan Kiszkaeca39dd2010-02-08 10:12:18 +00001005 file->private_data = cdev;
1006
1007 mutex_lock(&capidev_list_lock);
1008 list_add_tail(&cdev->list, &capidev_list);
1009 mutex_unlock(&capidev_list_lock);
1010
1011 return nonseekable_open(inode, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012}
1013
Jan Kiszkaeca39dd2010-02-08 10:12:18 +00001014static int capi_release(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015{
Jan Kiszkaeca39dd2010-02-08 10:12:18 +00001016 struct capidev *cdev = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017
Jan Kiszkaeca39dd2010-02-08 10:12:18 +00001018 mutex_lock(&capidev_list_lock);
1019 list_del(&cdev->list);
1020 mutex_unlock(&capidev_list_lock);
1021
Jan Kiszka05b41492010-02-08 10:12:19 +00001022 if (cdev->ap.applid)
Jan Kiszkaeca39dd2010-02-08 10:12:18 +00001023 capi20_release(&cdev->ap);
Jan Kiszkaeca39dd2010-02-08 10:12:18 +00001024 skb_queue_purge(&cdev->recvqueue);
Jan Kiszkaeca39dd2010-02-08 10:12:18 +00001025 capincci_free(cdev, 0xffffffff);
Jan Kiszkaeca39dd2010-02-08 10:12:18 +00001026
1027 kfree(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 return 0;
1029}
1030
Arjan van de Ven2b8693c2007-02-12 00:55:32 -08001031static const struct file_operations capi_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032{
1033 .owner = THIS_MODULE,
1034 .llseek = no_llseek,
1035 .read = capi_read,
1036 .write = capi_write,
1037 .poll = capi_poll,
1038 .ioctl = capi_ioctl,
1039 .open = capi_open,
1040 .release = capi_release,
1041};
1042
1043#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1044/* -------- tty_operations for capincci ----------------------------- */
1045
Jan Kiszka46324512010-02-08 10:12:28 +00001046static int
1047capinc_tty_install(struct tty_driver *driver, struct tty_struct *tty)
1048{
1049 int idx = tty->index;
1050 struct capiminor *mp = capiminor_get(idx);
1051 int ret = tty_init_termios(tty);
1052
1053 if (ret == 0) {
1054 tty_driver_kref_get(driver);
1055 tty->count++;
1056 tty->driver_data = mp;
1057 driver->ttys[idx] = tty;
1058 } else
1059 capiminor_put(mp);
1060 return ret;
1061}
1062
1063static void capinc_tty_cleanup(struct tty_struct *tty)
1064{
1065 struct capiminor *mp = tty->driver_data;
1066 tty->driver_data = NULL;
1067 capiminor_put(mp);
1068}
1069
Jan Kiszkafb4b4882010-02-08 10:12:29 +00001070static int capinc_tty_open(struct tty_struct *tty, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071{
Jan Kiszka46324512010-02-08 10:12:28 +00001072 struct capiminor *mp = tty->driver_data;
Jan Kiszkafb4b4882010-02-08 10:12:29 +00001073 int err;
1074
1075 err = tty_port_open(&mp->port, tty, filp);
1076 if (err)
1077 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 handle_minor_recv(mp);
1080 return 0;
1081}
1082
Jan Kiszkafb4b4882010-02-08 10:12:29 +00001083static void capinc_tty_close(struct tty_struct *tty, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084{
Jan Kiszka46324512010-02-08 10:12:28 +00001085 struct capiminor *mp = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
Jan Kiszkafb4b4882010-02-08 10:12:29 +00001087 tty_port_close(&mp->port, tty, filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088}
1089
Jan Kiszka6576c282010-02-08 10:12:32 +00001090static int capinc_tty_write(struct tty_struct *tty,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 const unsigned char *buf, int count)
1092{
Jan Kiszka2c8df722010-02-08 10:12:30 +00001093 struct capiminor *mp = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 struct sk_buff *skb;
1095
1096#ifdef _DEBUG_TTYFUNCS
1097 printk(KERN_DEBUG "capinc_tty_write(count=%d)\n", count);
1098#endif
1099
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001100 spin_lock_bh(&mp->outlock);
1101 skb = mp->outskb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 if (skb) {
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001103 mp->outskb = NULL;
1104 __skb_queue_tail(&mp->outqueue, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 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");
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001111 spin_unlock_bh(&mp->outlock);
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
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001118 __skb_queue_tail(&mp->outqueue, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 mp->outbytes += skb->len;
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001120 spin_unlock_bh(&mp->outlock);
1121
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 (void)handle_minor_send(mp);
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001123
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 return count;
1125}
1126
Alan Coxf2545a72008-04-30 00:54:09 -07001127static int capinc_tty_put_char(struct tty_struct *tty, unsigned char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128{
Jan Kiszka2c8df722010-02-08 10:12:30 +00001129 struct capiminor *mp = tty->driver_data;
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001130 bool invoke_send = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 struct sk_buff *skb;
Alan Coxf2545a72008-04-30 00:54:09 -07001132 int ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
1134#ifdef _DEBUG_TTYFUNCS
1135 printk(KERN_DEBUG "capinc_put_char(%u)\n", ch);
1136#endif
1137
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001138 spin_lock_bh(&mp->outlock);
1139 skb = mp->outskb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 if (skb) {
1141 if (skb_tailroom(skb) > 0) {
1142 *(skb_put(skb, 1)) = ch;
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001143 goto unlock_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 }
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001145 mp->outskb = NULL;
1146 __skb_queue_tail(&mp->outqueue, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 mp->outbytes += skb->len;
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001148 invoke_send = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 }
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001150
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 skb = alloc_skb(CAPI_DATA_B3_REQ_LEN+CAPI_MAX_BLKSIZE, GFP_ATOMIC);
1152 if (skb) {
1153 skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
1154 *(skb_put(skb, 1)) = ch;
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001155 mp->outskb = skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 } else {
1157 printk(KERN_ERR "capinc_put_char: char %u lost\n", ch);
Alan Coxf2545a72008-04-30 00:54:09 -07001158 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 }
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001160
1161unlock_out:
1162 spin_unlock_bh(&mp->outlock);
1163
1164 if (invoke_send)
1165 (void)handle_minor_send(mp);
1166
Alan Coxf2545a72008-04-30 00:54:09 -07001167 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168}
1169
1170static void capinc_tty_flush_chars(struct tty_struct *tty)
1171{
Jan Kiszka2c8df722010-02-08 10:12:30 +00001172 struct capiminor *mp = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 struct sk_buff *skb;
1174
1175#ifdef _DEBUG_TTYFUNCS
1176 printk(KERN_DEBUG "capinc_tty_flush_chars\n");
1177#endif
1178
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001179 spin_lock_bh(&mp->outlock);
1180 skb = mp->outskb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 if (skb) {
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001182 mp->outskb = NULL;
1183 __skb_queue_tail(&mp->outqueue, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 mp->outbytes += skb->len;
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001185 spin_unlock_bh(&mp->outlock);
1186
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 (void)handle_minor_send(mp);
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001188 } else
1189 spin_unlock_bh(&mp->outlock);
1190
1191 handle_minor_recv(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192}
1193
1194static int capinc_tty_write_room(struct tty_struct *tty)
1195{
Jan Kiszka2c8df722010-02-08 10:12:30 +00001196 struct capiminor *mp = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 int room;
Jan Kiszka2c8df722010-02-08 10:12:30 +00001198
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 room = CAPINC_MAX_SENDQUEUE-skb_queue_len(&mp->outqueue);
1200 room *= CAPI_MAX_BLKSIZE;
1201#ifdef _DEBUG_TTYFUNCS
1202 printk(KERN_DEBUG "capinc_tty_write_room = %d\n", room);
1203#endif
1204 return room;
1205}
1206
Adrian Bunk408b6642005-05-01 08:59:29 -07001207static int capinc_tty_chars_in_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208{
Jan Kiszka2c8df722010-02-08 10:12:30 +00001209 struct capiminor *mp = tty->driver_data;
1210
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211#ifdef _DEBUG_TTYFUNCS
1212 printk(KERN_DEBUG "capinc_tty_chars_in_buffer = %d nack=%d sq=%d rq=%d\n",
1213 mp->outbytes, mp->nack,
1214 skb_queue_len(&mp->outqueue),
1215 skb_queue_len(&mp->inqueue));
1216#endif
1217 return mp->outbytes;
1218}
1219
1220static int capinc_tty_ioctl(struct tty_struct *tty, struct file * file,
1221 unsigned int cmd, unsigned long arg)
1222{
1223 int error = 0;
1224 switch (cmd) {
1225 default:
Stephen Rothwell53e86312008-10-13 10:44:33 +01001226 error = n_tty_ioctl_helper(tty, file, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 break;
1228 }
1229 return error;
1230}
1231
Alan Cox606d0992006-12-08 02:38:45 -08001232static void capinc_tty_set_termios(struct tty_struct *tty, struct ktermios * old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233{
1234#ifdef _DEBUG_TTYFUNCS
1235 printk(KERN_DEBUG "capinc_tty_set_termios\n");
1236#endif
1237}
1238
Jan Kiszka2c8df722010-02-08 10:12:30 +00001239static void capinc_tty_throttle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240{
Jan Kiszka2c8df722010-02-08 10:12:30 +00001241 struct capiminor *mp = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242#ifdef _DEBUG_TTYFUNCS
1243 printk(KERN_DEBUG "capinc_tty_throttle\n");
1244#endif
Jan Kiszka2c8df722010-02-08 10:12:30 +00001245 mp->ttyinstop = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246}
1247
Jan Kiszka2c8df722010-02-08 10:12:30 +00001248static void capinc_tty_unthrottle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249{
Jan Kiszka2c8df722010-02-08 10:12:30 +00001250 struct capiminor *mp = tty->driver_data;
Jan Kiszka2c8df722010-02-08 10:12:30 +00001251
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252#ifdef _DEBUG_TTYFUNCS
1253 printk(KERN_DEBUG "capinc_tty_unthrottle\n");
1254#endif
Jan Kiszka2c8df722010-02-08 10:12:30 +00001255 mp->ttyinstop = 0;
1256 handle_minor_recv(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257}
1258
1259static void capinc_tty_stop(struct tty_struct *tty)
1260{
Jan Kiszka2c8df722010-02-08 10:12:30 +00001261 struct capiminor *mp = tty->driver_data;
1262
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263#ifdef _DEBUG_TTYFUNCS
1264 printk(KERN_DEBUG "capinc_tty_stop\n");
1265#endif
Jan Kiszka2c8df722010-02-08 10:12:30 +00001266 mp->ttyoutstop = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267}
1268
1269static void capinc_tty_start(struct tty_struct *tty)
1270{
Jan Kiszka2c8df722010-02-08 10:12:30 +00001271 struct capiminor *mp = tty->driver_data;
Jan Kiszka2c8df722010-02-08 10:12:30 +00001272
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273#ifdef _DEBUG_TTYFUNCS
1274 printk(KERN_DEBUG "capinc_tty_start\n");
1275#endif
Jan Kiszka2c8df722010-02-08 10:12:30 +00001276 mp->ttyoutstop = 0;
1277 (void)handle_minor_send(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278}
1279
1280static void capinc_tty_hangup(struct tty_struct *tty)
1281{
Jan Kiszkafb4b4882010-02-08 10:12:29 +00001282 struct capiminor *mp = tty->driver_data;
1283
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284#ifdef _DEBUG_TTYFUNCS
1285 printk(KERN_DEBUG "capinc_tty_hangup\n");
1286#endif
Jan Kiszkafb4b4882010-02-08 10:12:29 +00001287 tty_port_hangup(&mp->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288}
1289
Alan Cox9e989662008-07-22 11:18:03 +01001290static int capinc_tty_break_ctl(struct tty_struct *tty, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291{
1292#ifdef _DEBUG_TTYFUNCS
1293 printk(KERN_DEBUG "capinc_tty_break_ctl(%d)\n", state);
1294#endif
Alan Cox9e989662008-07-22 11:18:03 +01001295 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296}
1297
1298static void capinc_tty_flush_buffer(struct tty_struct *tty)
1299{
1300#ifdef _DEBUG_TTYFUNCS
1301 printk(KERN_DEBUG "capinc_tty_flush_buffer\n");
1302#endif
1303}
1304
1305static void capinc_tty_set_ldisc(struct tty_struct *tty)
1306{
1307#ifdef _DEBUG_TTYFUNCS
1308 printk(KERN_DEBUG "capinc_tty_set_ldisc\n");
1309#endif
1310}
1311
1312static void capinc_tty_send_xchar(struct tty_struct *tty, char ch)
1313{
1314#ifdef _DEBUG_TTYFUNCS
1315 printk(KERN_DEBUG "capinc_tty_send_xchar(%d)\n", ch);
1316#endif
1317}
1318
Jeff Dikeb68e31d2006-10-02 02:17:18 -07001319static const struct tty_operations capinc_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 .open = capinc_tty_open,
1321 .close = capinc_tty_close,
1322 .write = capinc_tty_write,
1323 .put_char = capinc_tty_put_char,
1324 .flush_chars = capinc_tty_flush_chars,
1325 .write_room = capinc_tty_write_room,
1326 .chars_in_buffer = capinc_tty_chars_in_buffer,
1327 .ioctl = capinc_tty_ioctl,
1328 .set_termios = capinc_tty_set_termios,
1329 .throttle = capinc_tty_throttle,
1330 .unthrottle = capinc_tty_unthrottle,
1331 .stop = capinc_tty_stop,
1332 .start = capinc_tty_start,
1333 .hangup = capinc_tty_hangup,
1334 .break_ctl = capinc_tty_break_ctl,
1335 .flush_buffer = capinc_tty_flush_buffer,
1336 .set_ldisc = capinc_tty_set_ldisc,
1337 .send_xchar = capinc_tty_send_xchar,
Jan Kiszka46324512010-02-08 10:12:28 +00001338 .install = capinc_tty_install,
1339 .cleanup = capinc_tty_cleanup,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340};
1341
Jan Kiszkae76b1542010-02-08 10:12:24 +00001342static int __init capinc_tty_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343{
1344 struct tty_driver *drv;
Jan Kiszkae76b1542010-02-08 10:12:24 +00001345 int err;
1346
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 if (capi_ttyminors > CAPINC_MAX_PORTS)
1348 capi_ttyminors = CAPINC_MAX_PORTS;
1349 if (capi_ttyminors <= 0)
1350 capi_ttyminors = CAPINC_NR_PORTS;
1351
Jan Kiszka81d17fe2010-02-08 10:12:23 +00001352 capiminors = kzalloc(sizeof(struct capi_minor *) * capi_ttyminors,
1353 GFP_KERNEL);
1354 if (!capiminors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 return -ENOMEM;
1356
Jan Kiszka81d17fe2010-02-08 10:12:23 +00001357 drv = alloc_tty_driver(capi_ttyminors);
1358 if (!drv) {
1359 kfree(capiminors);
1360 return -ENOMEM;
1361 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 drv->owner = THIS_MODULE;
1363 drv->driver_name = "capi_nc";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 drv->name = "capi";
Jan Kiszkae95ac142010-02-08 10:12:26 +00001365 drv->major = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 drv->minor_start = 0;
1367 drv->type = TTY_DRIVER_TYPE_SERIAL;
1368 drv->subtype = SERIAL_TYPE_NORMAL;
1369 drv->init_termios = tty_std_termios;
1370 drv->init_termios.c_iflag = ICRNL;
1371 drv->init_termios.c_oflag = OPOST | ONLCR;
1372 drv->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1373 drv->init_termios.c_lflag = 0;
Jan Kiszka40fb2d02010-02-08 10:12:25 +00001374 drv->flags =
1375 TTY_DRIVER_REAL_RAW | TTY_DRIVER_RESET_TERMIOS |
1376 TTY_DRIVER_DYNAMIC_DEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 tty_set_operations(drv, &capinc_ops);
Jan Kiszkae76b1542010-02-08 10:12:24 +00001378
1379 err = tty_register_driver(drv);
1380 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 put_tty_driver(drv);
Jan Kiszka81d17fe2010-02-08 10:12:23 +00001382 kfree(capiminors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 printk(KERN_ERR "Couldn't register capi_nc driver\n");
Jan Kiszkae76b1542010-02-08 10:12:24 +00001384 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 }
1386 capinc_tty_driver = drv;
1387 return 0;
1388}
1389
Jan Kiszkae76b1542010-02-08 10:12:24 +00001390static void __exit capinc_tty_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391{
Jan Kiszkae76b1542010-02-08 10:12:24 +00001392 tty_unregister_driver(capinc_tty_driver);
1393 put_tty_driver(capinc_tty_driver);
Jan Kiszka81d17fe2010-02-08 10:12:23 +00001394 kfree(capiminors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395}
1396
Jan Kiszka501c87a2010-02-08 10:12:16 +00001397#else /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
1398
1399static inline int capinc_tty_init(void)
1400{
1401 return 0;
1402}
1403
1404static inline void capinc_tty_exit(void) { }
1405
1406#endif /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
1408/* -------- /proc functions ----------------------------------------- */
1409
1410/*
1411 * /proc/capi/capi20:
1412 * minor applid nrecvctlpkt nrecvdatapkt nsendctlpkt nsenddatapkt
1413 */
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001414static int capi20_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415{
1416 struct capidev *cdev;
1417 struct list_head *l;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
Jan Kiszkab8f433d2010-02-08 10:12:17 +00001419 mutex_lock(&capidev_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 list_for_each(l, &capidev_list) {
1421 cdev = list_entry(l, struct capidev, list);
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001422 seq_printf(m, "0 %d %lu %lu %lu %lu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 cdev->ap.applid,
1424 cdev->ap.nrecvctlpkt,
1425 cdev->ap.nrecvdatapkt,
1426 cdev->ap.nsentctlpkt,
1427 cdev->ap.nsentdatapkt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 }
Jan Kiszkab8f433d2010-02-08 10:12:17 +00001429 mutex_unlock(&capidev_list_lock);
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001430 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431}
1432
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001433static int capi20_proc_open(struct inode *inode, struct file *file)
1434{
1435 return single_open(file, capi20_proc_show, NULL);
1436}
1437
1438static const struct file_operations capi20_proc_fops = {
1439 .owner = THIS_MODULE,
1440 .open = capi20_proc_open,
1441 .read = seq_read,
1442 .llseek = seq_lseek,
1443 .release = single_release,
1444};
1445
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446/*
1447 * /proc/capi/capi20ncci:
1448 * applid ncci
1449 */
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001450static int capi20ncci_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451{
Jan Kiszka884f5c42010-02-08 10:12:22 +00001452 struct capidev *cdev;
1453 struct capincci *np;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
Jan Kiszkab8f433d2010-02-08 10:12:17 +00001455 mutex_lock(&capidev_list_lock);
Jan Kiszka884f5c42010-02-08 10:12:22 +00001456 list_for_each_entry(cdev, &capidev_list, list) {
Jan Kiszka05b41492010-02-08 10:12:19 +00001457 mutex_lock(&cdev->lock);
Jan Kiszka884f5c42010-02-08 10:12:22 +00001458 list_for_each_entry(np, &cdev->nccis, list)
1459 seq_printf(m, "%d 0x%x\n", cdev->ap.applid, np->ncci);
Jan Kiszka05b41492010-02-08 10:12:19 +00001460 mutex_unlock(&cdev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 }
Jan Kiszkab8f433d2010-02-08 10:12:17 +00001462 mutex_unlock(&capidev_list_lock);
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001463 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464}
1465
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001466static int capi20ncci_proc_open(struct inode *inode, struct file *file)
1467{
1468 return single_open(file, capi20ncci_proc_show, NULL);
1469}
1470
1471static const struct file_operations capi20ncci_proc_fops = {
1472 .owner = THIS_MODULE,
1473 .open = capi20ncci_proc_open,
1474 .read = seq_read,
1475 .llseek = seq_lseek,
1476 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477};
1478
1479static void __init proc_init(void)
1480{
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001481 proc_create("capi/capi20", 0, NULL, &capi20_proc_fops);
1482 proc_create("capi/capi20ncci", 0, NULL, &capi20ncci_proc_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483}
1484
1485static void __exit proc_exit(void)
1486{
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001487 remove_proc_entry("capi/capi20", NULL);
1488 remove_proc_entry("capi/capi20ncci", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489}
1490
1491/* -------- init function and module interface ---------------------- */
1492
1493
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494static int __init capi_init(void)
1495{
Jan Kiszka88549d62010-02-08 10:12:09 +00001496 const char *compileinfo;
Andrew Morton6d9eac32006-03-28 01:56:19 -08001497 int major_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498
Andrew Morton6d9eac32006-03-28 01:56:19 -08001499 major_ret = register_chrdev(capi_major, "capi20", &capi_fops);
1500 if (major_ret < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 printk(KERN_ERR "capi20: unable to get major %d\n", capi_major);
Andrew Morton6d9eac32006-03-28 01:56:19 -08001502 return major_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 }
gregkh@suse.de56b22932005-03-23 10:01:41 -08001504 capi_class = class_create(THIS_MODULE, "capi");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 if (IS_ERR(capi_class)) {
1506 unregister_chrdev(capi_major, "capi20");
1507 return PTR_ERR(capi_class);
1508 }
1509
Greg Kroah-Hartmana9b12612008-07-21 20:03:34 -07001510 device_create(capi_class, NULL, MKDEV(capi_major, 0), NULL, "capi");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 if (capinc_tty_init() < 0) {
Tony Jonesd78b0362007-09-25 02:03:03 +02001513 device_destroy(capi_class, MKDEV(capi_major, 0));
gregkh@suse.de56b22932005-03-23 10:01:41 -08001514 class_destroy(capi_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 unregister_chrdev(capi_major, "capi20");
1516 return -ENOMEM;
1517 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518
1519 proc_init();
1520
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521#if defined(CONFIG_ISDN_CAPI_CAPIFS) || defined(CONFIG_ISDN_CAPI_CAPIFS_MODULE)
1522 compileinfo = " (middleware+capifs)";
Jan Kiszka501c87a2010-02-08 10:12:16 +00001523#elif defined(CONFIG_ISDN_CAPI_MIDDLEWARE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 compileinfo = " (no capifs)";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525#else
1526 compileinfo = " (no middleware)";
1527#endif
Jan Kiszka88549d62010-02-08 10:12:09 +00001528 printk(KERN_NOTICE "CAPI 2.0 started up with major %d%s\n",
1529 capi_major, compileinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530
1531 return 0;
1532}
1533
1534static void __exit capi_exit(void)
1535{
1536 proc_exit();
1537
Tony Jonesd78b0362007-09-25 02:03:03 +02001538 device_destroy(capi_class, MKDEV(capi_major, 0));
gregkh@suse.de56b22932005-03-23 10:01:41 -08001539 class_destroy(capi_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 unregister_chrdev(capi_major, "capi20");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 capinc_tty_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543}
1544
1545module_init(capi_init);
1546module_exit(capi_exit);