blob: acc811bfe8e0df467753f7ff379e818e2b6bcf1d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* $Id: capi.c,v 1.1.2.7 2004/04/28 09:48:59 armin Exp $
2 *
3 * CAPI 2.0 Interface for Linux
4 *
5 * Copyright 1996 by Carsten Paeth <calle@calle.de>
6 *
7 * This software may be used and distributed according to the terms
8 * of the GNU General Public License, incorporated herein by reference.
9 *
10 */
11
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/module.h>
13#include <linux/errno.h>
14#include <linux/kernel.h>
15#include <linux/major.h>
16#include <linux/sched.h>
17#include <linux/slab.h>
18#include <linux/fcntl.h>
19#include <linux/fs.h>
20#include <linux/signal.h>
Matthias Kaehlcke9ea6e5d2007-05-08 00:32:43 -070021#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/mm.h>
Jonathan Corbeta237f3b2008-05-16 14:15:33 -060023#include <linux/smp_lock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/timer.h>
25#include <linux/wait.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/tty.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/netdevice.h>
28#include <linux/ppp_defs.h>
29#include <linux/if_ppp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/skbuff.h>
31#include <linux/proc_fs.h>
Alexey Dobriyan9a58a802010-01-14 03:10:54 -080032#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/poll.h>
34#include <linux/capi.h>
35#include <linux/kernelcapi.h>
36#include <linux/init.h>
37#include <linux/device.h>
38#include <linux/moduleparam.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/isdn/capiutil.h>
40#include <linux/isdn/capicmd.h>
Jan Kiszka90926f02010-02-08 10:12:06 +000041
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include "capifs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Linus Torvalds1da177e2005-04-16 15:20:36 -070044MODULE_DESCRIPTION("CAPI4Linux: Userspace /dev/capi20 interface");
45MODULE_AUTHOR("Carsten Paeth");
46MODULE_LICENSE("GPL");
47
48#undef _DEBUG_REFCOUNT /* alloc/free and open/close debug */
49#undef _DEBUG_TTYFUNCS /* call to tty_driver */
50#undef _DEBUG_DATAFLOW /* data flow */
51
52/* -------- driver information -------------------------------------- */
53
gregkh@suse.de56b22932005-03-23 10:01:41 -080054static struct class *capi_class;
Adrian Bunk408b6642005-05-01 08:59:29 -070055static int capi_major = 68; /* allocated */
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57module_param_named(major, capi_major, uint, 0);
Jan Kiszka501c87a2010-02-08 10:12:16 +000058
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
Jan Kiszka501c87a2010-02-08 10:12:16 +000060#define CAPINC_NR_PORTS 32
61#define CAPINC_MAX_PORTS 256
62
Jan Kiszka501c87a2010-02-08 10:12:16 +000063static int capi_ttyminors = CAPINC_NR_PORTS;
64
Linus Torvalds1da177e2005-04-16 15:20:36 -070065module_param_named(ttyminors, capi_ttyminors, uint, 0);
66#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
67
68/* -------- defines ------------------------------------------------- */
69
70#define CAPINC_MAX_RECVQUEUE 10
71#define CAPINC_MAX_SENDQUEUE 10
72#define CAPI_MAX_BLKSIZE 2048
73
74/* -------- data structures ----------------------------------------- */
75
76struct capidev;
77struct capincci;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078struct capiminor;
79
Michael Buesch6aa65472006-06-26 00:25:30 -070080struct datahandle_queue {
81 struct list_head list;
82 u16 datahandle;
83};
84
Linus Torvalds1da177e2005-04-16 15:20:36 -070085struct capiminor {
Jan Kiszka0159d542010-02-08 10:12:27 +000086 struct kref kref;
87
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 struct capincci *nccip;
89 unsigned int minor;
Jan Kiszka90926f02010-02-08 10:12:06 +000090 struct dentry *capifs_dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92 struct capi20_appl *ap;
93 u32 ncci;
94 u16 datahandle;
95 u16 msgid;
96
Jan Kiszkafb4b4882010-02-08 10:12:29 +000097 struct tty_port port;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 int ttyinstop;
99 int ttyoutstop;
100 struct sk_buff *ttyskb;
101 atomic_t ttyopencount;
102
103 struct sk_buff_head inqueue;
104 int inbytes;
105 struct sk_buff_head outqueue;
106 int outbytes;
107
108 /* transmit path */
Michael Buesch6aa65472006-06-26 00:25:30 -0700109 struct list_head ackqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 int nack;
Michael Buesch6aa65472006-06-26 00:25:30 -0700111 spinlock_t ackqlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Michael Buesch053b47f2007-02-12 00:53:26 -0800114/* FIXME: The following lock is a sledgehammer-workaround to a
115 * locking issue with the capiminor (and maybe other) data structure(s).
116 * Access to this data is done in a racy way and crashes the machine with
117 * a FritzCard DSL driver; sooner or later. This is a workaround
118 * which trades scalability vs stability, so it doesn't crash the kernel anymore.
119 * The correct (and scalable) fix for the issue seems to require
120 * an API change to the drivers... . */
121static DEFINE_SPINLOCK(workaround_lock);
122
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123struct capincci {
Jan Kiszka884f5c42010-02-08 10:12:22 +0000124 struct list_head list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 u32 ncci;
126 struct capidev *cdev;
127#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
128 struct capiminor *minorp;
129#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
130};
131
132struct capidev {
133 struct list_head list;
134 struct capi20_appl ap;
135 u16 errcode;
136 unsigned userflags;
137
138 struct sk_buff_head recvqueue;
139 wait_queue_head_t recvwait;
140
Jan Kiszka884f5c42010-02-08 10:12:22 +0000141 struct list_head nccis;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Jan Kiszka05b41492010-02-08 10:12:19 +0000143 struct mutex lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144};
145
146/* -------- global variables ---------------------------------------- */
147
Jan Kiszkab8f433d2010-02-08 10:12:17 +0000148static DEFINE_MUTEX(capidev_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149static LIST_HEAD(capidev_list);
150
151#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
Jan Kiszka501c87a2010-02-08 10:12:16 +0000152
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000153static DEFINE_RWLOCK(capiminors_lock);
154static struct capiminor **capiminors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
Jan Kiszka40fb2d02010-02-08 10:12:25 +0000156static struct tty_driver *capinc_tty_driver;
157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158/* -------- datahandles --------------------------------------------- */
159
Jan Kiszka501c87a2010-02-08 10:12:16 +0000160static int capiminor_add_ack(struct capiminor *mp, u16 datahandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161{
Michael Buesch6aa65472006-06-26 00:25:30 -0700162 struct datahandle_queue *n;
163 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
165 n = kmalloc(sizeof(*n), GFP_ATOMIC);
Michael Buesch6aa65472006-06-26 00:25:30 -0700166 if (unlikely(!n)) {
167 printk(KERN_ERR "capi: alloc datahandle failed\n");
168 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 n->datahandle = datahandle;
Michael Buesch6aa65472006-06-26 00:25:30 -0700171 INIT_LIST_HEAD(&n->list);
172 spin_lock_irqsave(&mp->ackqlock, flags);
173 list_add_tail(&n->list, &mp->ackqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 mp->nack++;
Michael Buesch6aa65472006-06-26 00:25:30 -0700175 spin_unlock_irqrestore(&mp->ackqlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 return 0;
177}
178
179static int capiminor_del_ack(struct capiminor *mp, u16 datahandle)
180{
Michael Buesch6aa65472006-06-26 00:25:30 -0700181 struct datahandle_queue *p, *tmp;
182 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
Michael Buesch6aa65472006-06-26 00:25:30 -0700184 spin_lock_irqsave(&mp->ackqlock, flags);
185 list_for_each_entry_safe(p, tmp, &mp->ackqueue, list) {
186 if (p->datahandle == datahandle) {
187 list_del(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 kfree(p);
189 mp->nack--;
Michael Buesch6aa65472006-06-26 00:25:30 -0700190 spin_unlock_irqrestore(&mp->ackqlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 return 0;
192 }
193 }
Michael Buesch6aa65472006-06-26 00:25:30 -0700194 spin_unlock_irqrestore(&mp->ackqlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 return -1;
196}
197
198static void capiminor_del_all_ack(struct capiminor *mp)
199{
Michael Buesch6aa65472006-06-26 00:25:30 -0700200 struct datahandle_queue *p, *tmp;
201 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Michael Buesch6aa65472006-06-26 00:25:30 -0700203 spin_lock_irqsave(&mp->ackqlock, flags);
204 list_for_each_entry_safe(p, tmp, &mp->ackqueue, list) {
205 list_del(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 kfree(p);
207 mp->nack--;
208 }
Michael Buesch6aa65472006-06-26 00:25:30 -0700209 spin_unlock_irqrestore(&mp->ackqlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210}
211
212
213/* -------- struct capiminor ---------------------------------------- */
214
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000215static const struct tty_port_operations capiminor_port_ops; /* we have none */
216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217static struct capiminor *capiminor_alloc(struct capi20_appl *ap, u32 ncci)
218{
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000219 struct capiminor *mp;
Jan Kiszka40fb2d02010-02-08 10:12:25 +0000220 struct device *dev;
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000221 unsigned int minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 unsigned long flags;
223
Jan Kiszka54f0fad2010-02-08 10:12:20 +0000224 mp = kzalloc(sizeof(*mp), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 if (!mp) {
226 printk(KERN_ERR "capi: can't alloc capiminor\n");
227 return NULL;
228 }
229
Jan Kiszka0159d542010-02-08 10:12:27 +0000230 kref_init(&mp->kref);
231
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 mp->ap = ap;
233 mp->ncci = ncci;
234 mp->msgid = 0;
235 atomic_set(&mp->ttyopencount,0);
Michael Buesch6aa65472006-06-26 00:25:30 -0700236 INIT_LIST_HEAD(&mp->ackqueue);
237 spin_lock_init(&mp->ackqlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
239 skb_queue_head_init(&mp->inqueue);
240 skb_queue_head_init(&mp->outqueue);
241
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000242 tty_port_init(&mp->port);
243 mp->port.ops = &capiminor_port_ops;
244
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000245 /* Allocate the least unused minor number. */
246 write_lock_irqsave(&capiminors_lock, flags);
247 for (minor = 0; minor < capi_ttyminors; minor++)
248 if (!capiminors[minor]) {
249 capiminors[minor] = mp;
250 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 }
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000252 write_unlock_irqrestore(&capiminors_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000254 if (minor == capi_ttyminors) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 printk(KERN_NOTICE "capi: out of minors\n");
Jan Kiszka40fb2d02010-02-08 10:12:25 +0000256 goto err_out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 }
258
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000259 mp->minor = minor;
260
Jan Kiszka40fb2d02010-02-08 10:12:25 +0000261 dev = tty_register_device(capinc_tty_driver, minor, NULL);
262 if (IS_ERR(dev))
263 goto err_out2;
264
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 return mp;
Jan Kiszka40fb2d02010-02-08 10:12:25 +0000266
267err_out2:
268 write_lock_irqsave(&capiminors_lock, flags);
269 capiminors[minor] = NULL;
270 write_unlock_irqrestore(&capiminors_lock, flags);
271
272err_out1:
273 kfree(mp);
274 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275}
276
Jan Kiszka0159d542010-02-08 10:12:27 +0000277static void capiminor_destroy(struct kref *kref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278{
Jan Kiszka0159d542010-02-08 10:12:27 +0000279 struct capiminor *mp = container_of(kref, struct capiminor, kref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
Wei Yongjund46604e2009-02-25 00:12:09 +0000281 kfree_skb(mp->ttyskb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 skb_queue_purge(&mp->inqueue);
283 skb_queue_purge(&mp->outqueue);
284 capiminor_del_all_ack(mp);
285 kfree(mp);
286}
287
Jan Kiszka40fb2d02010-02-08 10:12:25 +0000288static struct capiminor *capiminor_get(unsigned int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289{
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000290 struct capiminor *mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000292 read_lock(&capiminors_lock);
293 mp = capiminors[minor];
Jan Kiszka0159d542010-02-08 10:12:27 +0000294 if (mp)
295 kref_get(&mp->kref);
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000296 read_unlock(&capiminors_lock);
297
298 return mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
Jan Kiszka0159d542010-02-08 10:12:27 +0000301static inline void capiminor_put(struct capiminor *mp)
302{
303 kref_put(&mp->kref, capiminor_destroy);
304}
305
306static void capiminor_free(struct capiminor *mp)
307{
308 unsigned long flags;
309
310 tty_unregister_device(capinc_tty_driver, mp->minor);
311
312 write_lock_irqsave(&capiminors_lock, flags);
313 capiminors[mp->minor] = NULL;
314 write_unlock_irqrestore(&capiminors_lock, flags);
315
316 capiminor_put(mp);
317}
318
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319/* -------- struct capincci ----------------------------------------- */
320
Jan Kiszka501c87a2010-02-08 10:12:16 +0000321static void capincci_alloc_minor(struct capidev *cdev, struct capincci *np)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322{
Jan Kiszka501c87a2010-02-08 10:12:16 +0000323 struct capiminor *mp;
Jan Kiszkae95ac142010-02-08 10:12:26 +0000324 dev_t device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
Jan Kiszka501c87a2010-02-08 10:12:16 +0000326 if (!(cdev->userflags & CAPIFLAG_HIGHJACKING))
327 return;
328
329 mp = np->minorp = capiminor_alloc(&cdev->ap, np->ncci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 if (mp) {
331 mp->nccip = np;
332#ifdef _DEBUG_REFCOUNT
333 printk(KERN_DEBUG "set mp->nccip\n");
334#endif
Jan Kiszkae95ac142010-02-08 10:12:26 +0000335 device = MKDEV(capinc_tty_driver->major, mp->minor);
336 mp->capifs_dentry = capifs_new_ncci(mp->minor, device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 }
Jan Kiszka501c87a2010-02-08 10:12:16 +0000338}
339
340static void capincci_free_minor(struct capincci *np)
341{
342 struct capiminor *mp = np->minorp;
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000343 struct tty_struct *tty;
Jan Kiszka501c87a2010-02-08 10:12:16 +0000344
345 if (mp) {
346 capifs_free_ncci(mp->capifs_dentry);
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000347
348 tty = tty_port_tty_get(&mp->port);
349 if (tty) {
Jan Kiszka501c87a2010-02-08 10:12:16 +0000350 mp->nccip = NULL;
351#ifdef _DEBUG_REFCOUNT
352 printk(KERN_DEBUG "reset mp->nccip\n");
353#endif
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000354 tty_hangup(tty);
355 tty_kref_put(tty);
Jan Kiszka501c87a2010-02-08 10:12:16 +0000356 }
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000357
358 capiminor_free(mp);
Jan Kiszka501c87a2010-02-08 10:12:16 +0000359 }
360}
361
362static inline unsigned int capincci_minor_opencount(struct capincci *np)
363{
364 struct capiminor *mp = np->minorp;
365
366 return mp ? atomic_read(&mp->ttyopencount) : 0;
367}
368
369#else /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
370
371static inline void
372capincci_alloc_minor(struct capidev *cdev, struct capincci *np) { }
373static inline void capincci_free_minor(struct capincci *np) { }
374
375static inline unsigned int capincci_minor_opencount(struct capincci *np)
376{
377 return 0;
378}
379
380#endif /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
381
382static struct capincci *capincci_alloc(struct capidev *cdev, u32 ncci)
383{
Jan Kiszka884f5c42010-02-08 10:12:22 +0000384 struct capincci *np;
Jan Kiszka501c87a2010-02-08 10:12:16 +0000385
Jan Kiszka54f0fad2010-02-08 10:12:20 +0000386 np = kzalloc(sizeof(*np), GFP_KERNEL);
Jan Kiszka501c87a2010-02-08 10:12:16 +0000387 if (!np)
388 return NULL;
389 np->ncci = ncci;
390 np->cdev = cdev;
391
392 capincci_alloc_minor(cdev, np);
393
Jan Kiszka884f5c42010-02-08 10:12:22 +0000394 list_add_tail(&np->list, &cdev->nccis);
395
396 return np;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397}
398
399static void capincci_free(struct capidev *cdev, u32 ncci)
400{
Jan Kiszka884f5c42010-02-08 10:12:22 +0000401 struct capincci *np, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
Jan Kiszka884f5c42010-02-08 10:12:22 +0000403 list_for_each_entry_safe(np, tmp, &cdev->nccis, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 if (ncci == 0xffffffff || np->ncci == ncci) {
Jan Kiszka501c87a2010-02-08 10:12:16 +0000405 capincci_free_minor(np);
Jan Kiszka884f5c42010-02-08 10:12:22 +0000406 list_del(&np->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 kfree(np);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409}
410
411static struct capincci *capincci_find(struct capidev *cdev, u32 ncci)
412{
Jan Kiszka884f5c42010-02-08 10:12:22 +0000413 struct capincci *np;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
Jan Kiszka884f5c42010-02-08 10:12:22 +0000415 list_for_each_entry(np, &cdev->nccis, list)
416 if (np->ncci == ncci)
417 return np;
418 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419}
420
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
422/* -------- handle data queue --------------------------------------- */
423
424static struct sk_buff *
425gen_data_b3_resp_for(struct capiminor *mp, struct sk_buff *skb)
426{
427 struct sk_buff *nskb;
428 nskb = alloc_skb(CAPI_DATA_B3_RESP_LEN, GFP_ATOMIC);
429 if (nskb) {
430 u16 datahandle = CAPIMSG_U16(skb->data,CAPIMSG_BASELEN+4+4+2);
431 unsigned char *s = skb_put(nskb, CAPI_DATA_B3_RESP_LEN);
432 capimsg_setu16(s, 0, CAPI_DATA_B3_RESP_LEN);
433 capimsg_setu16(s, 2, mp->ap->applid);
434 capimsg_setu8 (s, 4, CAPI_DATA_B3);
435 capimsg_setu8 (s, 5, CAPI_RESP);
436 capimsg_setu16(s, 6, mp->msgid++);
437 capimsg_setu32(s, 8, mp->ncci);
438 capimsg_setu16(s, 12, datahandle);
439 }
440 return nskb;
441}
442
443static int handle_recv_skb(struct capiminor *mp, struct sk_buff *skb)
444{
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000445 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 struct sk_buff *nskb;
447 int datalen;
448 u16 errcode, datahandle;
449 struct tty_ldisc *ld;
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000450 int ret = -1;
451
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 datalen = skb->len - CAPIMSG_LEN(skb->data);
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000453
454 tty = tty_port_tty_get(&mp->port);
455 if (!tty) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456#ifdef _DEBUG_DATAFLOW
457 printk(KERN_DEBUG "capi: currently no receiver\n");
458#endif
459 return -1;
460 }
461
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000462 ld = tty_ldisc_ref(tty);
463 if (!ld)
464 goto out1;
465
Alan Coxa352def2008-07-16 21:53:12 +0100466 if (ld->ops->receive_buf == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467#if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
468 printk(KERN_DEBUG "capi: ldisc has no receive_buf function\n");
469#endif
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000470 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 }
472 if (mp->ttyinstop) {
473#if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
474 printk(KERN_DEBUG "capi: recv tty throttled\n");
475#endif
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000476 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 }
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000478 if (tty->receive_room < datalen) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479#if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
480 printk(KERN_DEBUG "capi: no room in tty\n");
481#endif
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000482 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 }
Harvey Harrison2f9e9b62008-04-28 02:14:37 -0700484 if ((nskb = gen_data_b3_resp_for(mp, skb)) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 printk(KERN_ERR "capi: gen_data_b3_resp failed\n");
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000486 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 }
488 datahandle = CAPIMSG_U16(skb->data,CAPIMSG_BASELEN+4);
489 errcode = capi20_put_message(mp->ap, nskb);
490 if (errcode != CAPI_NOERROR) {
491 printk(KERN_ERR "capi: send DATA_B3_RESP failed=%x\n",
492 errcode);
493 kfree_skb(nskb);
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000494 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 }
496 (void)skb_pull(skb, CAPIMSG_LEN(skb->data));
497#ifdef _DEBUG_DATAFLOW
498 printk(KERN_DEBUG "capi: DATA_B3_RESP %u len=%d => ldisc\n",
499 datahandle, skb->len);
500#endif
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000501 ld->ops->receive_buf(tty, skb->data, NULL, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 kfree_skb(skb);
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000503 ret = 0;
504out2:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 tty_ldisc_deref(ld);
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000506out1:
507 tty_kref_put(tty);
508 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509}
510
511static void handle_minor_recv(struct capiminor *mp)
512{
513 struct sk_buff *skb;
Harvey Harrison2f9e9b62008-04-28 02:14:37 -0700514 while ((skb = skb_dequeue(&mp->inqueue)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 unsigned int len = skb->len;
516 mp->inbytes -= len;
517 if (handle_recv_skb(mp, skb) < 0) {
518 skb_queue_head(&mp->inqueue, skb);
519 mp->inbytes += len;
520 return;
521 }
522 }
523}
524
525static int handle_minor_send(struct capiminor *mp)
526{
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000527 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 struct sk_buff *skb;
529 u16 len;
530 int count = 0;
531 u16 errcode;
532 u16 datahandle;
533
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000534 tty = tty_port_tty_get(&mp->port);
535 if (!tty)
536 return 0;
537
538 if (mp->ttyoutstop) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539#if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
540 printk(KERN_DEBUG "capi: send: tty stopped\n");
541#endif
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000542 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 return 0;
544 }
545
Harvey Harrison2f9e9b62008-04-28 02:14:37 -0700546 while ((skb = skb_dequeue(&mp->outqueue)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 datahandle = mp->datahandle;
548 len = (u16)skb->len;
549 skb_push(skb, CAPI_DATA_B3_REQ_LEN);
550 memset(skb->data, 0, CAPI_DATA_B3_REQ_LEN);
551 capimsg_setu16(skb->data, 0, CAPI_DATA_B3_REQ_LEN);
552 capimsg_setu16(skb->data, 2, mp->ap->applid);
553 capimsg_setu8 (skb->data, 4, CAPI_DATA_B3);
554 capimsg_setu8 (skb->data, 5, CAPI_REQ);
555 capimsg_setu16(skb->data, 6, mp->msgid++);
556 capimsg_setu32(skb->data, 8, mp->ncci); /* NCCI */
Andrew Morton5e6c20a2007-07-17 04:04:22 -0700557 capimsg_setu32(skb->data, 12, (u32)(long)skb->data);/* Data32 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 capimsg_setu16(skb->data, 16, len); /* Data length */
559 capimsg_setu16(skb->data, 18, datahandle);
560 capimsg_setu16(skb->data, 20, 0); /* Flags */
561
Jan Kiszka501c87a2010-02-08 10:12:16 +0000562 if (capiminor_add_ack(mp, datahandle) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 skb_pull(skb, CAPI_DATA_B3_REQ_LEN);
564 skb_queue_head(&mp->outqueue, skb);
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000565 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 return count;
567 }
568 errcode = capi20_put_message(mp->ap, skb);
569 if (errcode == CAPI_NOERROR) {
570 mp->datahandle++;
571 count++;
572 mp->outbytes -= len;
573#ifdef _DEBUG_DATAFLOW
574 printk(KERN_DEBUG "capi: DATA_B3_REQ %u len=%u\n",
575 datahandle, len);
576#endif
577 continue;
578 }
579 capiminor_del_ack(mp, datahandle);
580
581 if (errcode == CAPI_SENDQUEUEFULL) {
582 skb_pull(skb, CAPI_DATA_B3_REQ_LEN);
583 skb_queue_head(&mp->outqueue, skb);
584 break;
585 }
586
587 /* ups, drop packet */
588 printk(KERN_ERR "capi: put_message = %x\n", errcode);
589 mp->outbytes -= len;
590 kfree_skb(skb);
591 }
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000592 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 return count;
594}
595
596#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
597/* -------- function called by lower level -------------------------- */
598
599static void capi_recv_message(struct capi20_appl *ap, struct sk_buff *skb)
600{
601 struct capidev *cdev = ap->private;
602#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000603 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 struct capiminor *mp;
605 u16 datahandle;
606#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
607 struct capincci *np;
Michael Buesch053b47f2007-02-12 00:53:26 -0800608 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
Jan Kiszka05b41492010-02-08 10:12:19 +0000610 mutex_lock(&cdev->lock);
611
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_CONF) {
613 u16 info = CAPIMSG_U16(skb->data, 12); // Info field
Jan Kiszka05b41492010-02-08 10:12:19 +0000614 if ((info & 0xff00) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 }
Jan Kiszka05b41492010-02-08 10:12:19 +0000617 if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_IND)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
Jan Kiszka05b41492010-02-08 10:12:19 +0000619
Michael Buesch053b47f2007-02-12 00:53:26 -0800620 spin_lock_irqsave(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 if (CAPIMSG_COMMAND(skb->data) != CAPI_DATA_B3) {
622 skb_queue_tail(&cdev->recvqueue, skb);
623 wake_up_interruptible(&cdev->recvwait);
Jan Kiszka05b41492010-02-08 10:12:19 +0000624 goto unlock_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 }
Jan Kiszka05b41492010-02-08 10:12:19 +0000626
627 np = capincci_find(cdev, CAPIMSG_CONTROL(skb->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 if (!np) {
629 printk(KERN_ERR "BUG: capi_signal: ncci not found\n");
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 Kiszka501c87a2010-02-08 10:12:16 +0000634
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635#ifndef CONFIG_ISDN_CAPI_MIDDLEWARE
636 skb_queue_tail(&cdev->recvqueue, skb);
637 wake_up_interruptible(&cdev->recvwait);
Jan Kiszka501c87a2010-02-08 10:12:16 +0000638
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639#else /* CONFIG_ISDN_CAPI_MIDDLEWARE */
Jan Kiszka501c87a2010-02-08 10:12:16 +0000640
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 mp = np->minorp;
642 if (!mp) {
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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_IND) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4+4+2);
649#ifdef _DEBUG_DATAFLOW
650 printk(KERN_DEBUG "capi_signal: DATA_B3_IND %u len=%d\n",
651 datahandle, skb->len-CAPIMSG_LEN(skb->data));
652#endif
653 skb_queue_tail(&mp->inqueue, skb);
654 mp->inbytes += skb->len;
655 handle_minor_recv(mp);
656
657 } else if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_CONF) {
658
659 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4);
660#ifdef _DEBUG_DATAFLOW
661 printk(KERN_DEBUG "capi_signal: DATA_B3_CONF %u 0x%x\n",
662 datahandle,
663 CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4+2));
664#endif
665 kfree_skb(skb);
666 (void)capiminor_del_ack(mp, datahandle);
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000667 tty = tty_port_tty_get(&mp->port);
668 if (tty) {
669 tty_wakeup(tty);
670 tty_kref_put(tty);
671 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 (void)handle_minor_send(mp);
673
674 } else {
675 /* ups, let capi application handle it :-) */
676 skb_queue_tail(&cdev->recvqueue, skb);
677 wake_up_interruptible(&cdev->recvwait);
678 }
679#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
Jan Kiszka501c87a2010-02-08 10:12:16 +0000680
Jan Kiszka05b41492010-02-08 10:12:19 +0000681unlock_out:
Michael Buesch053b47f2007-02-12 00:53:26 -0800682 spin_unlock_irqrestore(&workaround_lock, flags);
Jan Kiszka05b41492010-02-08 10:12:19 +0000683 mutex_unlock(&cdev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684}
685
686/* -------- file_operations for capidev ----------------------------- */
687
688static ssize_t
689capi_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
690{
691 struct capidev *cdev = (struct capidev *)file->private_data;
692 struct sk_buff *skb;
693 size_t copied;
Jan Kiszka28a1dbb2010-02-08 10:12:21 +0000694 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
696 if (!cdev->ap.applid)
697 return -ENODEV;
698
Jan Kiszka28a1dbb2010-02-08 10:12:21 +0000699 skb = skb_dequeue(&cdev->recvqueue);
700 if (!skb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 if (file->f_flags & O_NONBLOCK)
702 return -EAGAIN;
Jan Kiszka28a1dbb2010-02-08 10:12:21 +0000703 err = wait_event_interruptible(cdev->recvwait,
704 (skb = skb_dequeue(&cdev->recvqueue)));
705 if (err)
706 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 }
708 if (skb->len > count) {
709 skb_queue_head(&cdev->recvqueue, skb);
710 return -EMSGSIZE;
711 }
712 if (copy_to_user(buf, skb->data, skb->len)) {
713 skb_queue_head(&cdev->recvqueue, skb);
714 return -EFAULT;
715 }
716 copied = skb->len;
717
718 kfree_skb(skb);
719
720 return copied;
721}
722
723static ssize_t
724capi_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
725{
726 struct capidev *cdev = (struct capidev *)file->private_data;
727 struct sk_buff *skb;
728 u16 mlen;
729
730 if (!cdev->ap.applid)
731 return -ENODEV;
732
733 skb = alloc_skb(count, GFP_USER);
734 if (!skb)
735 return -ENOMEM;
736
737 if (copy_from_user(skb_put(skb, count), buf, count)) {
738 kfree_skb(skb);
739 return -EFAULT;
740 }
741 mlen = CAPIMSG_LEN(skb->data);
742 if (CAPIMSG_CMD(skb->data) == CAPI_DATA_B3_REQ) {
743 if ((size_t)(mlen + CAPIMSG_DATALEN(skb->data)) != count) {
744 kfree_skb(skb);
745 return -EINVAL;
746 }
747 } else {
748 if (mlen != count) {
749 kfree_skb(skb);
750 return -EINVAL;
751 }
752 }
753 CAPIMSG_SETAPPID(skb->data, cdev->ap.applid);
754
755 if (CAPIMSG_CMD(skb->data) == CAPI_DISCONNECT_B3_RESP) {
Jan Kiszka05b41492010-02-08 10:12:19 +0000756 mutex_lock(&cdev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 capincci_free(cdev, CAPIMSG_NCCI(skb->data));
Jan Kiszka05b41492010-02-08 10:12:19 +0000758 mutex_unlock(&cdev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 }
760
761 cdev->errcode = capi20_put_message(&cdev->ap, skb);
762
763 if (cdev->errcode) {
764 kfree_skb(skb);
765 return -EIO;
766 }
767 return count;
768}
769
770static unsigned int
771capi_poll(struct file *file, poll_table * wait)
772{
773 struct capidev *cdev = (struct capidev *)file->private_data;
774 unsigned int mask = 0;
775
776 if (!cdev->ap.applid)
777 return POLLERR;
778
779 poll_wait(file, &(cdev->recvwait), wait);
780 mask = POLLOUT | POLLWRNORM;
781 if (!skb_queue_empty(&cdev->recvqueue))
782 mask |= POLLIN | POLLRDNORM;
783 return mask;
784}
785
786static int
787capi_ioctl(struct inode *inode, struct file *file,
788 unsigned int cmd, unsigned long arg)
789{
790 struct capidev *cdev = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 capi_ioctl_struct data;
792 int retval = -EINVAL;
793 void __user *argp = (void __user *)arg;
794
795 switch (cmd) {
796 case CAPI_REGISTER:
Jan Kiszka05b41492010-02-08 10:12:19 +0000797 mutex_lock(&cdev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
Jan Kiszka05b41492010-02-08 10:12:19 +0000799 if (cdev->ap.applid) {
800 retval = -EEXIST;
801 goto register_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 }
Jan Kiszka05b41492010-02-08 10:12:19 +0000803 if (copy_from_user(&cdev->ap.rparam, argp,
804 sizeof(struct capi_register_params))) {
805 retval = -EFAULT;
806 goto register_out;
807 }
808 cdev->ap.private = cdev;
809 cdev->ap.recv_message = capi_recv_message;
810 cdev->errcode = capi20_register(&cdev->ap);
811 retval = (int)cdev->ap.applid;
812 if (cdev->errcode) {
813 cdev->ap.applid = 0;
814 retval = -EIO;
815 }
816
817register_out:
818 mutex_unlock(&cdev->lock);
819 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
821 case CAPI_GET_VERSION:
822 {
823 if (copy_from_user(&data.contr, argp,
824 sizeof(data.contr)))
825 return -EFAULT;
826 cdev->errcode = capi20_get_version(data.contr, &data.version);
827 if (cdev->errcode)
828 return -EIO;
829 if (copy_to_user(argp, &data.version,
830 sizeof(data.version)))
831 return -EFAULT;
832 }
833 return 0;
834
835 case CAPI_GET_SERIAL:
836 {
837 if (copy_from_user(&data.contr, argp,
838 sizeof(data.contr)))
839 return -EFAULT;
840 cdev->errcode = capi20_get_serial (data.contr, data.serial);
841 if (cdev->errcode)
842 return -EIO;
843 if (copy_to_user(argp, data.serial,
844 sizeof(data.serial)))
845 return -EFAULT;
846 }
847 return 0;
848 case CAPI_GET_PROFILE:
849 {
850 if (copy_from_user(&data.contr, argp,
851 sizeof(data.contr)))
852 return -EFAULT;
853
854 if (data.contr == 0) {
855 cdev->errcode = capi20_get_profile(data.contr, &data.profile);
856 if (cdev->errcode)
857 return -EIO;
858
859 retval = copy_to_user(argp,
860 &data.profile.ncontroller,
861 sizeof(data.profile.ncontroller));
862
863 } else {
864 cdev->errcode = capi20_get_profile(data.contr, &data.profile);
865 if (cdev->errcode)
866 return -EIO;
867
868 retval = copy_to_user(argp, &data.profile,
869 sizeof(data.profile));
870 }
871 if (retval)
872 return -EFAULT;
873 }
874 return 0;
875
876 case CAPI_GET_MANUFACTURER:
877 {
878 if (copy_from_user(&data.contr, argp,
879 sizeof(data.contr)))
880 return -EFAULT;
881 cdev->errcode = capi20_get_manufacturer(data.contr, data.manufacturer);
882 if (cdev->errcode)
883 return -EIO;
884
885 if (copy_to_user(argp, data.manufacturer,
886 sizeof(data.manufacturer)))
887 return -EFAULT;
888
889 }
890 return 0;
891 case CAPI_GET_ERRCODE:
892 data.errcode = cdev->errcode;
893 cdev->errcode = CAPI_NOERROR;
894 if (arg) {
895 if (copy_to_user(argp, &data.errcode,
896 sizeof(data.errcode)))
897 return -EFAULT;
898 }
899 return data.errcode;
900
901 case CAPI_INSTALLED:
902 if (capi20_isinstalled() == CAPI_NOERROR)
903 return 0;
904 return -ENXIO;
905
906 case CAPI_MANUFACTURER_CMD:
907 {
908 struct capi_manufacturer_cmd mcmd;
909 if (!capable(CAP_SYS_ADMIN))
910 return -EPERM;
911 if (copy_from_user(&mcmd, argp, sizeof(mcmd)))
912 return -EFAULT;
913 return capi20_manufacturer(mcmd.cmd, mcmd.data);
914 }
915 return 0;
916
917 case CAPI_SET_FLAGS:
Jan Kiszka05b41492010-02-08 10:12:19 +0000918 case CAPI_CLR_FLAGS: {
919 unsigned userflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
Jan Kiszka05b41492010-02-08 10:12:19 +0000921 if (copy_from_user(&userflags, argp, sizeof(userflags)))
922 return -EFAULT;
923
924 mutex_lock(&cdev->lock);
925 if (cmd == CAPI_SET_FLAGS)
926 cdev->userflags |= userflags;
927 else
928 cdev->userflags &= ~userflags;
929 mutex_unlock(&cdev->lock);
930 return 0;
931 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 case CAPI_GET_FLAGS:
933 if (copy_to_user(argp, &cdev->userflags,
934 sizeof(cdev->userflags)))
935 return -EFAULT;
936 return 0;
937
Jan Kiszka05b41492010-02-08 10:12:19 +0000938 case CAPI_NCCI_OPENCOUNT: {
939 struct capincci *nccip;
940 unsigned ncci;
941 int count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
Jan Kiszka05b41492010-02-08 10:12:19 +0000943 if (copy_from_user(&ncci, argp, sizeof(ncci)))
944 return -EFAULT;
945
946 mutex_lock(&cdev->lock);
947 nccip = capincci_find(cdev, (u32)ncci);
948 if (nccip)
949 count = capincci_minor_opencount(nccip);
950 mutex_unlock(&cdev->lock);
951 return count;
952 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
954#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
Jan Kiszka05b41492010-02-08 10:12:19 +0000955 case CAPI_NCCI_GETUNIT: {
956 struct capincci *nccip;
957 struct capiminor *mp;
958 unsigned ncci;
959 int unit = -ESRCH;
960
961 if (copy_from_user(&ncci, argp, sizeof(ncci)))
962 return -EFAULT;
963
964 mutex_lock(&cdev->lock);
965 nccip = capincci_find(cdev, (u32)ncci);
966 if (nccip) {
967 mp = nccip->minorp;
968 if (mp)
969 unit = mp->minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 }
Jan Kiszka05b41492010-02-08 10:12:19 +0000971 mutex_unlock(&cdev->lock);
972 return unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 }
Jan Kiszka05b41492010-02-08 10:12:19 +0000974#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
975
976 default:
977 return -EINVAL;
978 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979}
980
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000981static int capi_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982{
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000983 struct capidev *cdev;
984
985 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
986 if (!cdev)
987 return -ENOMEM;
988
Jan Kiszka05b41492010-02-08 10:12:19 +0000989 mutex_init(&cdev->lock);
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000990 skb_queue_head_init(&cdev->recvqueue);
991 init_waitqueue_head(&cdev->recvwait);
Jan Kiszka884f5c42010-02-08 10:12:22 +0000992 INIT_LIST_HEAD(&cdev->nccis);
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000993 file->private_data = cdev;
994
995 mutex_lock(&capidev_list_lock);
996 list_add_tail(&cdev->list, &capidev_list);
997 mutex_unlock(&capidev_list_lock);
998
999 return nonseekable_open(inode, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000}
1001
Jan Kiszkaeca39dd2010-02-08 10:12:18 +00001002static int capi_release(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003{
Jan Kiszkaeca39dd2010-02-08 10:12:18 +00001004 struct capidev *cdev = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
Jan Kiszkaeca39dd2010-02-08 10:12:18 +00001006 mutex_lock(&capidev_list_lock);
1007 list_del(&cdev->list);
1008 mutex_unlock(&capidev_list_lock);
1009
Jan Kiszka05b41492010-02-08 10:12:19 +00001010 if (cdev->ap.applid)
Jan Kiszkaeca39dd2010-02-08 10:12:18 +00001011 capi20_release(&cdev->ap);
Jan Kiszkaeca39dd2010-02-08 10:12:18 +00001012 skb_queue_purge(&cdev->recvqueue);
Jan Kiszkaeca39dd2010-02-08 10:12:18 +00001013 capincci_free(cdev, 0xffffffff);
Jan Kiszkaeca39dd2010-02-08 10:12:18 +00001014
1015 kfree(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 return 0;
1017}
1018
Arjan van de Ven2b8693c2007-02-12 00:55:32 -08001019static const struct file_operations capi_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020{
1021 .owner = THIS_MODULE,
1022 .llseek = no_llseek,
1023 .read = capi_read,
1024 .write = capi_write,
1025 .poll = capi_poll,
1026 .ioctl = capi_ioctl,
1027 .open = capi_open,
1028 .release = capi_release,
1029};
1030
1031#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1032/* -------- tty_operations for capincci ----------------------------- */
1033
Jan Kiszka46324512010-02-08 10:12:28 +00001034static int
1035capinc_tty_install(struct tty_driver *driver, struct tty_struct *tty)
1036{
1037 int idx = tty->index;
1038 struct capiminor *mp = capiminor_get(idx);
1039 int ret = tty_init_termios(tty);
1040
1041 if (ret == 0) {
1042 tty_driver_kref_get(driver);
1043 tty->count++;
1044 tty->driver_data = mp;
1045 driver->ttys[idx] = tty;
1046 } else
1047 capiminor_put(mp);
1048 return ret;
1049}
1050
1051static void capinc_tty_cleanup(struct tty_struct *tty)
1052{
1053 struct capiminor *mp = tty->driver_data;
1054 tty->driver_data = NULL;
1055 capiminor_put(mp);
1056}
1057
Jan Kiszkafb4b4882010-02-08 10:12:29 +00001058static int capinc_tty_open(struct tty_struct *tty, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059{
Jan Kiszka46324512010-02-08 10:12:28 +00001060 struct capiminor *mp = tty->driver_data;
Michael Buesch053b47f2007-02-12 00:53:26 -08001061 unsigned long flags;
Jan Kiszkafb4b4882010-02-08 10:12:29 +00001062 int err;
1063
1064 err = tty_port_open(&mp->port, tty, filp);
1065 if (err)
1066 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
Michael Buesch053b47f2007-02-12 00:53:26 -08001068 spin_lock_irqsave(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 atomic_inc(&mp->ttyopencount);
1070#ifdef _DEBUG_REFCOUNT
1071 printk(KERN_DEBUG "capinc_tty_open ocount=%d\n", atomic_read(&mp->ttyopencount));
1072#endif
1073 handle_minor_recv(mp);
Michael Buesch053b47f2007-02-12 00:53:26 -08001074 spin_unlock_irqrestore(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 return 0;
1076}
1077
Jan Kiszkafb4b4882010-02-08 10:12:29 +00001078static void capinc_tty_close(struct tty_struct *tty, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079{
Jan Kiszka46324512010-02-08 10:12:28 +00001080 struct capiminor *mp = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 if (atomic_dec_and_test(&mp->ttyopencount)) {
1083#ifdef _DEBUG_REFCOUNT
1084 printk(KERN_DEBUG "capinc_tty_close lastclose\n");
1085#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 }
1087#ifdef _DEBUG_REFCOUNT
1088 printk(KERN_DEBUG "capinc_tty_close ocount=%d\n", atomic_read(&mp->ttyopencount));
1089#endif
Jan Kiszka0159d542010-02-08 10:12:27 +00001090
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091#ifdef _DEBUG_REFCOUNT
1092 printk(KERN_DEBUG "capinc_tty_close\n");
1093#endif
Jan Kiszkafb4b4882010-02-08 10:12:29 +00001094 tty_port_close(&mp->port, tty, filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095}
1096
1097static int capinc_tty_write(struct tty_struct * tty,
1098 const unsigned char *buf, int count)
1099{
Jan Kiszka2c8df722010-02-08 10:12:30 +00001100 struct capiminor *mp = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 struct sk_buff *skb;
Michael Buesch053b47f2007-02-12 00:53:26 -08001102 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
1104#ifdef _DEBUG_TTYFUNCS
1105 printk(KERN_DEBUG "capinc_tty_write(count=%d)\n", count);
1106#endif
1107
Jan Kiszka2c8df722010-02-08 10:12:30 +00001108 if (!mp->nccip) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109#ifdef _DEBUG_TTYFUNCS
1110 printk(KERN_DEBUG "capinc_tty_write: mp or mp->ncci NULL\n");
1111#endif
1112 return 0;
1113 }
1114
Michael Buesch053b47f2007-02-12 00:53:26 -08001115 spin_lock_irqsave(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 skb = mp->ttyskb;
1117 if (skb) {
1118 mp->ttyskb = NULL;
1119 skb_queue_tail(&mp->outqueue, skb);
1120 mp->outbytes += skb->len;
1121 }
1122
1123 skb = alloc_skb(CAPI_DATA_B3_REQ_LEN+count, GFP_ATOMIC);
1124 if (!skb) {
1125 printk(KERN_ERR "capinc_tty_write: alloc_skb failed\n");
Michael Buesch053b47f2007-02-12 00:53:26 -08001126 spin_unlock_irqrestore(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 return -ENOMEM;
1128 }
1129
1130 skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
1131 memcpy(skb_put(skb, count), buf, count);
1132
1133 skb_queue_tail(&mp->outqueue, skb);
1134 mp->outbytes += skb->len;
1135 (void)handle_minor_send(mp);
1136 (void)handle_minor_recv(mp);
Michael Buesch053b47f2007-02-12 00:53:26 -08001137 spin_unlock_irqrestore(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 return count;
1139}
1140
Alan Coxf2545a72008-04-30 00:54:09 -07001141static int capinc_tty_put_char(struct tty_struct *tty, unsigned char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142{
Jan Kiszka2c8df722010-02-08 10:12:30 +00001143 struct capiminor *mp = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 struct sk_buff *skb;
Michael Buesch053b47f2007-02-12 00:53:26 -08001145 unsigned long flags;
Alan Coxf2545a72008-04-30 00:54:09 -07001146 int ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
1148#ifdef _DEBUG_TTYFUNCS
1149 printk(KERN_DEBUG "capinc_put_char(%u)\n", ch);
1150#endif
1151
Jan Kiszka2c8df722010-02-08 10:12:30 +00001152 if (!mp->nccip) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153#ifdef _DEBUG_TTYFUNCS
1154 printk(KERN_DEBUG "capinc_tty_put_char: mp or mp->ncci NULL\n");
1155#endif
Alan Coxf2545a72008-04-30 00:54:09 -07001156 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 }
1158
Michael Buesch053b47f2007-02-12 00:53:26 -08001159 spin_lock_irqsave(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 skb = mp->ttyskb;
1161 if (skb) {
1162 if (skb_tailroom(skb) > 0) {
1163 *(skb_put(skb, 1)) = ch;
Michael Buesch053b47f2007-02-12 00:53:26 -08001164 spin_unlock_irqrestore(&workaround_lock, flags);
Alan Coxf2545a72008-04-30 00:54:09 -07001165 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 }
1167 mp->ttyskb = NULL;
1168 skb_queue_tail(&mp->outqueue, skb);
1169 mp->outbytes += skb->len;
1170 (void)handle_minor_send(mp);
1171 }
1172 skb = alloc_skb(CAPI_DATA_B3_REQ_LEN+CAPI_MAX_BLKSIZE, GFP_ATOMIC);
1173 if (skb) {
1174 skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
1175 *(skb_put(skb, 1)) = ch;
1176 mp->ttyskb = skb;
1177 } else {
1178 printk(KERN_ERR "capinc_put_char: char %u lost\n", ch);
Alan Coxf2545a72008-04-30 00:54:09 -07001179 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 }
Michael Buesch053b47f2007-02-12 00:53:26 -08001181 spin_unlock_irqrestore(&workaround_lock, flags);
Alan Coxf2545a72008-04-30 00:54:09 -07001182 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183}
1184
1185static void capinc_tty_flush_chars(struct tty_struct *tty)
1186{
Jan Kiszka2c8df722010-02-08 10:12:30 +00001187 struct capiminor *mp = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 struct sk_buff *skb;
Michael Buesch053b47f2007-02-12 00:53:26 -08001189 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
1191#ifdef _DEBUG_TTYFUNCS
1192 printk(KERN_DEBUG "capinc_tty_flush_chars\n");
1193#endif
1194
Jan Kiszka2c8df722010-02-08 10:12:30 +00001195 if (!mp->nccip) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196#ifdef _DEBUG_TTYFUNCS
1197 printk(KERN_DEBUG "capinc_tty_flush_chars: mp or mp->ncci NULL\n");
1198#endif
1199 return;
1200 }
1201
Michael Buesch053b47f2007-02-12 00:53:26 -08001202 spin_lock_irqsave(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 skb = mp->ttyskb;
1204 if (skb) {
1205 mp->ttyskb = NULL;
1206 skb_queue_tail(&mp->outqueue, skb);
1207 mp->outbytes += skb->len;
1208 (void)handle_minor_send(mp);
1209 }
1210 (void)handle_minor_recv(mp);
Michael Buesch053b47f2007-02-12 00:53:26 -08001211 spin_unlock_irqrestore(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212}
1213
1214static int capinc_tty_write_room(struct tty_struct *tty)
1215{
Jan Kiszka2c8df722010-02-08 10:12:30 +00001216 struct capiminor *mp = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 int room;
Jan Kiszka2c8df722010-02-08 10:12:30 +00001218
1219 if (!mp->nccip) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220#ifdef _DEBUG_TTYFUNCS
1221 printk(KERN_DEBUG "capinc_tty_write_room: mp or mp->ncci NULL\n");
1222#endif
1223 return 0;
1224 }
1225 room = CAPINC_MAX_SENDQUEUE-skb_queue_len(&mp->outqueue);
1226 room *= CAPI_MAX_BLKSIZE;
1227#ifdef _DEBUG_TTYFUNCS
1228 printk(KERN_DEBUG "capinc_tty_write_room = %d\n", room);
1229#endif
1230 return room;
1231}
1232
Adrian Bunk408b6642005-05-01 08:59:29 -07001233static int capinc_tty_chars_in_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234{
Jan Kiszka2c8df722010-02-08 10:12:30 +00001235 struct capiminor *mp = tty->driver_data;
1236
1237 if (!mp->nccip) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238#ifdef _DEBUG_TTYFUNCS
1239 printk(KERN_DEBUG "capinc_tty_chars_in_buffer: mp or mp->ncci NULL\n");
1240#endif
1241 return 0;
1242 }
1243#ifdef _DEBUG_TTYFUNCS
1244 printk(KERN_DEBUG "capinc_tty_chars_in_buffer = %d nack=%d sq=%d rq=%d\n",
1245 mp->outbytes, mp->nack,
1246 skb_queue_len(&mp->outqueue),
1247 skb_queue_len(&mp->inqueue));
1248#endif
1249 return mp->outbytes;
1250}
1251
1252static int capinc_tty_ioctl(struct tty_struct *tty, struct file * file,
1253 unsigned int cmd, unsigned long arg)
1254{
1255 int error = 0;
1256 switch (cmd) {
1257 default:
Stephen Rothwell53e86312008-10-13 10:44:33 +01001258 error = n_tty_ioctl_helper(tty, file, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 break;
1260 }
1261 return error;
1262}
1263
Alan Cox606d0992006-12-08 02:38:45 -08001264static void capinc_tty_set_termios(struct tty_struct *tty, struct ktermios * old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265{
1266#ifdef _DEBUG_TTYFUNCS
1267 printk(KERN_DEBUG "capinc_tty_set_termios\n");
1268#endif
1269}
1270
Jan Kiszka2c8df722010-02-08 10:12:30 +00001271static void capinc_tty_throttle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272{
Jan Kiszka2c8df722010-02-08 10:12:30 +00001273 struct capiminor *mp = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274#ifdef _DEBUG_TTYFUNCS
1275 printk(KERN_DEBUG "capinc_tty_throttle\n");
1276#endif
Jan Kiszka2c8df722010-02-08 10:12:30 +00001277 mp->ttyinstop = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278}
1279
Jan Kiszka2c8df722010-02-08 10:12:30 +00001280static void capinc_tty_unthrottle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281{
Jan Kiszka2c8df722010-02-08 10:12:30 +00001282 struct capiminor *mp = tty->driver_data;
Michael Buesch053b47f2007-02-12 00:53:26 -08001283 unsigned long flags;
Jan Kiszka2c8df722010-02-08 10:12:30 +00001284
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285#ifdef _DEBUG_TTYFUNCS
1286 printk(KERN_DEBUG "capinc_tty_unthrottle\n");
1287#endif
Jan Kiszka2c8df722010-02-08 10:12:30 +00001288 spin_lock_irqsave(&workaround_lock, flags);
1289 mp->ttyinstop = 0;
1290 handle_minor_recv(mp);
1291 spin_unlock_irqrestore(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292}
1293
1294static void capinc_tty_stop(struct tty_struct *tty)
1295{
Jan Kiszka2c8df722010-02-08 10:12:30 +00001296 struct capiminor *mp = tty->driver_data;
1297
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298#ifdef _DEBUG_TTYFUNCS
1299 printk(KERN_DEBUG "capinc_tty_stop\n");
1300#endif
Jan Kiszka2c8df722010-02-08 10:12:30 +00001301 mp->ttyoutstop = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302}
1303
1304static void capinc_tty_start(struct tty_struct *tty)
1305{
Jan Kiszka2c8df722010-02-08 10:12:30 +00001306 struct capiminor *mp = tty->driver_data;
Michael Buesch053b47f2007-02-12 00:53:26 -08001307 unsigned long flags;
Jan Kiszka2c8df722010-02-08 10:12:30 +00001308
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309#ifdef _DEBUG_TTYFUNCS
1310 printk(KERN_DEBUG "capinc_tty_start\n");
1311#endif
Jan Kiszka2c8df722010-02-08 10:12:30 +00001312 spin_lock_irqsave(&workaround_lock, flags);
1313 mp->ttyoutstop = 0;
1314 (void)handle_minor_send(mp);
1315 spin_unlock_irqrestore(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316}
1317
1318static void capinc_tty_hangup(struct tty_struct *tty)
1319{
Jan Kiszkafb4b4882010-02-08 10:12:29 +00001320 struct capiminor *mp = tty->driver_data;
1321
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322#ifdef _DEBUG_TTYFUNCS
1323 printk(KERN_DEBUG "capinc_tty_hangup\n");
1324#endif
Jan Kiszkafb4b4882010-02-08 10:12:29 +00001325 tty_port_hangup(&mp->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326}
1327
Alan Cox9e98966c2008-07-22 11:18:03 +01001328static int capinc_tty_break_ctl(struct tty_struct *tty, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329{
1330#ifdef _DEBUG_TTYFUNCS
1331 printk(KERN_DEBUG "capinc_tty_break_ctl(%d)\n", state);
1332#endif
Alan Cox9e98966c2008-07-22 11:18:03 +01001333 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334}
1335
1336static void capinc_tty_flush_buffer(struct tty_struct *tty)
1337{
1338#ifdef _DEBUG_TTYFUNCS
1339 printk(KERN_DEBUG "capinc_tty_flush_buffer\n");
1340#endif
1341}
1342
1343static void capinc_tty_set_ldisc(struct tty_struct *tty)
1344{
1345#ifdef _DEBUG_TTYFUNCS
1346 printk(KERN_DEBUG "capinc_tty_set_ldisc\n");
1347#endif
1348}
1349
1350static void capinc_tty_send_xchar(struct tty_struct *tty, char ch)
1351{
1352#ifdef _DEBUG_TTYFUNCS
1353 printk(KERN_DEBUG "capinc_tty_send_xchar(%d)\n", ch);
1354#endif
1355}
1356
Jeff Dikeb68e31d2006-10-02 02:17:18 -07001357static const struct tty_operations capinc_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 .open = capinc_tty_open,
1359 .close = capinc_tty_close,
1360 .write = capinc_tty_write,
1361 .put_char = capinc_tty_put_char,
1362 .flush_chars = capinc_tty_flush_chars,
1363 .write_room = capinc_tty_write_room,
1364 .chars_in_buffer = capinc_tty_chars_in_buffer,
1365 .ioctl = capinc_tty_ioctl,
1366 .set_termios = capinc_tty_set_termios,
1367 .throttle = capinc_tty_throttle,
1368 .unthrottle = capinc_tty_unthrottle,
1369 .stop = capinc_tty_stop,
1370 .start = capinc_tty_start,
1371 .hangup = capinc_tty_hangup,
1372 .break_ctl = capinc_tty_break_ctl,
1373 .flush_buffer = capinc_tty_flush_buffer,
1374 .set_ldisc = capinc_tty_set_ldisc,
1375 .send_xchar = capinc_tty_send_xchar,
Jan Kiszka46324512010-02-08 10:12:28 +00001376 .install = capinc_tty_install,
1377 .cleanup = capinc_tty_cleanup,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378};
1379
Jan Kiszkae76b1542010-02-08 10:12:24 +00001380static int __init capinc_tty_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381{
1382 struct tty_driver *drv;
Jan Kiszkae76b1542010-02-08 10:12:24 +00001383 int err;
1384
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 if (capi_ttyminors > CAPINC_MAX_PORTS)
1386 capi_ttyminors = CAPINC_MAX_PORTS;
1387 if (capi_ttyminors <= 0)
1388 capi_ttyminors = CAPINC_NR_PORTS;
1389
Jan Kiszka81d17fe2010-02-08 10:12:23 +00001390 capiminors = kzalloc(sizeof(struct capi_minor *) * capi_ttyminors,
1391 GFP_KERNEL);
1392 if (!capiminors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 return -ENOMEM;
1394
Jan Kiszka81d17fe2010-02-08 10:12:23 +00001395 drv = alloc_tty_driver(capi_ttyminors);
1396 if (!drv) {
1397 kfree(capiminors);
1398 return -ENOMEM;
1399 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 drv->owner = THIS_MODULE;
1401 drv->driver_name = "capi_nc";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 drv->name = "capi";
Jan Kiszkae95ac142010-02-08 10:12:26 +00001403 drv->major = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 drv->minor_start = 0;
1405 drv->type = TTY_DRIVER_TYPE_SERIAL;
1406 drv->subtype = SERIAL_TYPE_NORMAL;
1407 drv->init_termios = tty_std_termios;
1408 drv->init_termios.c_iflag = ICRNL;
1409 drv->init_termios.c_oflag = OPOST | ONLCR;
1410 drv->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1411 drv->init_termios.c_lflag = 0;
Jan Kiszka40fb2d02010-02-08 10:12:25 +00001412 drv->flags =
1413 TTY_DRIVER_REAL_RAW | TTY_DRIVER_RESET_TERMIOS |
1414 TTY_DRIVER_DYNAMIC_DEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 tty_set_operations(drv, &capinc_ops);
Jan Kiszkae76b1542010-02-08 10:12:24 +00001416
1417 err = tty_register_driver(drv);
1418 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 put_tty_driver(drv);
Jan Kiszka81d17fe2010-02-08 10:12:23 +00001420 kfree(capiminors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 printk(KERN_ERR "Couldn't register capi_nc driver\n");
Jan Kiszkae76b1542010-02-08 10:12:24 +00001422 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 }
1424 capinc_tty_driver = drv;
1425 return 0;
1426}
1427
Jan Kiszkae76b1542010-02-08 10:12:24 +00001428static void __exit capinc_tty_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429{
Jan Kiszkae76b1542010-02-08 10:12:24 +00001430 tty_unregister_driver(capinc_tty_driver);
1431 put_tty_driver(capinc_tty_driver);
Jan Kiszka81d17fe2010-02-08 10:12:23 +00001432 kfree(capiminors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433}
1434
Jan Kiszka501c87a2010-02-08 10:12:16 +00001435#else /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
1436
1437static inline int capinc_tty_init(void)
1438{
1439 return 0;
1440}
1441
1442static inline void capinc_tty_exit(void) { }
1443
1444#endif /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
1446/* -------- /proc functions ----------------------------------------- */
1447
1448/*
1449 * /proc/capi/capi20:
1450 * minor applid nrecvctlpkt nrecvdatapkt nsendctlpkt nsenddatapkt
1451 */
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001452static int capi20_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453{
1454 struct capidev *cdev;
1455 struct list_head *l;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
Jan Kiszkab8f433d2010-02-08 10:12:17 +00001457 mutex_lock(&capidev_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 list_for_each(l, &capidev_list) {
1459 cdev = list_entry(l, struct capidev, list);
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001460 seq_printf(m, "0 %d %lu %lu %lu %lu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 cdev->ap.applid,
1462 cdev->ap.nrecvctlpkt,
1463 cdev->ap.nrecvdatapkt,
1464 cdev->ap.nsentctlpkt,
1465 cdev->ap.nsentdatapkt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 }
Jan Kiszkab8f433d2010-02-08 10:12:17 +00001467 mutex_unlock(&capidev_list_lock);
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001468 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469}
1470
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001471static int capi20_proc_open(struct inode *inode, struct file *file)
1472{
1473 return single_open(file, capi20_proc_show, NULL);
1474}
1475
1476static const struct file_operations capi20_proc_fops = {
1477 .owner = THIS_MODULE,
1478 .open = capi20_proc_open,
1479 .read = seq_read,
1480 .llseek = seq_lseek,
1481 .release = single_release,
1482};
1483
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484/*
1485 * /proc/capi/capi20ncci:
1486 * applid ncci
1487 */
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001488static int capi20ncci_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489{
Jan Kiszka884f5c42010-02-08 10:12:22 +00001490 struct capidev *cdev;
1491 struct capincci *np;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492
Jan Kiszkab8f433d2010-02-08 10:12:17 +00001493 mutex_lock(&capidev_list_lock);
Jan Kiszka884f5c42010-02-08 10:12:22 +00001494 list_for_each_entry(cdev, &capidev_list, list) {
Jan Kiszka05b41492010-02-08 10:12:19 +00001495 mutex_lock(&cdev->lock);
Jan Kiszka884f5c42010-02-08 10:12:22 +00001496 list_for_each_entry(np, &cdev->nccis, list)
1497 seq_printf(m, "%d 0x%x\n", cdev->ap.applid, np->ncci);
Jan Kiszka05b41492010-02-08 10:12:19 +00001498 mutex_unlock(&cdev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 }
Jan Kiszkab8f433d2010-02-08 10:12:17 +00001500 mutex_unlock(&capidev_list_lock);
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001501 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502}
1503
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001504static int capi20ncci_proc_open(struct inode *inode, struct file *file)
1505{
1506 return single_open(file, capi20ncci_proc_show, NULL);
1507}
1508
1509static const struct file_operations capi20ncci_proc_fops = {
1510 .owner = THIS_MODULE,
1511 .open = capi20ncci_proc_open,
1512 .read = seq_read,
1513 .llseek = seq_lseek,
1514 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515};
1516
1517static void __init proc_init(void)
1518{
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001519 proc_create("capi/capi20", 0, NULL, &capi20_proc_fops);
1520 proc_create("capi/capi20ncci", 0, NULL, &capi20ncci_proc_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521}
1522
1523static void __exit proc_exit(void)
1524{
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001525 remove_proc_entry("capi/capi20", NULL);
1526 remove_proc_entry("capi/capi20ncci", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527}
1528
1529/* -------- init function and module interface ---------------------- */
1530
1531
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532static int __init capi_init(void)
1533{
Jan Kiszka88549d62010-02-08 10:12:09 +00001534 const char *compileinfo;
Andrew Morton6d9eac32006-03-28 01:56:19 -08001535 int major_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536
Andrew Morton6d9eac32006-03-28 01:56:19 -08001537 major_ret = register_chrdev(capi_major, "capi20", &capi_fops);
1538 if (major_ret < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 printk(KERN_ERR "capi20: unable to get major %d\n", capi_major);
Andrew Morton6d9eac32006-03-28 01:56:19 -08001540 return major_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 }
gregkh@suse.de56b22932005-03-23 10:01:41 -08001542 capi_class = class_create(THIS_MODULE, "capi");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 if (IS_ERR(capi_class)) {
1544 unregister_chrdev(capi_major, "capi20");
1545 return PTR_ERR(capi_class);
1546 }
1547
Greg Kroah-Hartmana9b12612008-07-21 20:03:34 -07001548 device_create(capi_class, NULL, MKDEV(capi_major, 0), NULL, "capi");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 if (capinc_tty_init() < 0) {
Tony Jonesd78b0362007-09-25 02:03:03 +02001551 device_destroy(capi_class, MKDEV(capi_major, 0));
gregkh@suse.de56b22932005-03-23 10:01:41 -08001552 class_destroy(capi_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 unregister_chrdev(capi_major, "capi20");
1554 return -ENOMEM;
1555 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556
1557 proc_init();
1558
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559#if defined(CONFIG_ISDN_CAPI_CAPIFS) || defined(CONFIG_ISDN_CAPI_CAPIFS_MODULE)
1560 compileinfo = " (middleware+capifs)";
Jan Kiszka501c87a2010-02-08 10:12:16 +00001561#elif defined(CONFIG_ISDN_CAPI_MIDDLEWARE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 compileinfo = " (no capifs)";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563#else
1564 compileinfo = " (no middleware)";
1565#endif
Jan Kiszka88549d62010-02-08 10:12:09 +00001566 printk(KERN_NOTICE "CAPI 2.0 started up with major %d%s\n",
1567 capi_major, compileinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568
1569 return 0;
1570}
1571
1572static void __exit capi_exit(void)
1573{
1574 proc_exit();
1575
Tony Jonesd78b0362007-09-25 02:03:03 +02001576 device_destroy(capi_class, MKDEV(capi_major, 0));
gregkh@suse.de56b22932005-03-23 10:01:41 -08001577 class_destroy(capi_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 unregister_chrdev(capi_major, "capi20");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 capinc_tty_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581}
1582
1583module_init(capi_init);
1584module_exit(capi_exit);