blob: 40b81b460e81a1b19155a77f24bd2d96e6a2c165 [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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
153 n = kmalloc(sizeof(*n), GFP_ATOMIC);
Michael Buesch6aa65472006-06-26 00:25:30 -0700154 if (unlikely(!n)) {
155 printk(KERN_ERR "capi: alloc datahandle failed\n");
156 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 n->datahandle = datahandle;
Michael Buesch6aa65472006-06-26 00:25:30 -0700159 INIT_LIST_HEAD(&n->list);
Jan Kiszka2b72b5b2010-02-08 10:12:41 +0000160 spin_lock_bh(&mp->ackqlock);
Michael Buesch6aa65472006-06-26 00:25:30 -0700161 list_add_tail(&n->list, &mp->ackqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 mp->nack++;
Jan Kiszka2b72b5b2010-02-08 10:12:41 +0000163 spin_unlock_bh(&mp->ackqlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 return 0;
165}
166
167static int capiminor_del_ack(struct capiminor *mp, u16 datahandle)
168{
Jan Kiszka42651b52010-02-08 10:12:37 +0000169 struct ackqueue_entry *p, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Jan Kiszka2b72b5b2010-02-08 10:12:41 +0000171 spin_lock_bh(&mp->ackqlock);
Michael Buesch6aa65472006-06-26 00:25:30 -0700172 list_for_each_entry_safe(p, tmp, &mp->ackqueue, list) {
173 if (p->datahandle == datahandle) {
174 list_del(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 mp->nack--;
Jan Kiszka2b72b5b2010-02-08 10:12:41 +0000176 spin_unlock_bh(&mp->ackqlock);
177 kfree(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 return 0;
179 }
180 }
Jan Kiszka2b72b5b2010-02-08 10:12:41 +0000181 spin_unlock_bh(&mp->ackqlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 return -1;
183}
184
185static void capiminor_del_all_ack(struct capiminor *mp)
186{
Jan Kiszka42651b52010-02-08 10:12:37 +0000187 struct ackqueue_entry *p, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
Michael Buesch6aa65472006-06-26 00:25:30 -0700189 list_for_each_entry_safe(p, tmp, &mp->ackqueue, list) {
190 list_del(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 kfree(p);
192 mp->nack--;
193 }
194}
195
196
197/* -------- struct capiminor ---------------------------------------- */
198
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000199static const struct tty_port_operations capiminor_port_ops; /* we have none */
200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201static struct capiminor *capiminor_alloc(struct capi20_appl *ap, u32 ncci)
202{
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000203 struct capiminor *mp;
Jan Kiszka40fb2d02010-02-08 10:12:25 +0000204 struct device *dev;
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000205 unsigned int minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
Jan Kiszka54f0fad2010-02-08 10:12:20 +0000207 mp = kzalloc(sizeof(*mp), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 if (!mp) {
209 printk(KERN_ERR "capi: can't alloc capiminor\n");
210 return NULL;
211 }
212
Jan Kiszka0159d542010-02-08 10:12:27 +0000213 kref_init(&mp->kref);
214
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 mp->ap = ap;
216 mp->ncci = ncci;
Michael Buesch6aa65472006-06-26 00:25:30 -0700217 INIT_LIST_HEAD(&mp->ackqueue);
218 spin_lock_init(&mp->ackqlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
220 skb_queue_head_init(&mp->inqueue);
221 skb_queue_head_init(&mp->outqueue);
Jan Kiszkadfbb84f2010-02-08 10:12:40 +0000222 spin_lock_init(&mp->outlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000224 tty_port_init(&mp->port);
225 mp->port.ops = &capiminor_port_ops;
226
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000227 /* Allocate the least unused minor number. */
Jan Kiszka3d5d30f2010-02-08 10:12:33 +0000228 spin_lock(&capiminors_lock);
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000229 for (minor = 0; minor < capi_ttyminors; minor++)
230 if (!capiminors[minor]) {
231 capiminors[minor] = mp;
232 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 }
Jan Kiszka3d5d30f2010-02-08 10:12:33 +0000234 spin_unlock(&capiminors_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000236 if (minor == capi_ttyminors) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 printk(KERN_NOTICE "capi: out of minors\n");
Jan Kiszka40fb2d02010-02-08 10:12:25 +0000238 goto err_out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 }
240
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000241 mp->minor = minor;
242
Jan Kiszka40fb2d02010-02-08 10:12:25 +0000243 dev = tty_register_device(capinc_tty_driver, minor, NULL);
244 if (IS_ERR(dev))
245 goto err_out2;
246
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 return mp;
Jan Kiszka40fb2d02010-02-08 10:12:25 +0000248
249err_out2:
Jan Kiszka3d5d30f2010-02-08 10:12:33 +0000250 spin_lock(&capiminors_lock);
Jan Kiszka40fb2d02010-02-08 10:12:25 +0000251 capiminors[minor] = NULL;
Jan Kiszka3d5d30f2010-02-08 10:12:33 +0000252 spin_unlock(&capiminors_lock);
Jan Kiszka40fb2d02010-02-08 10:12:25 +0000253
254err_out1:
255 kfree(mp);
256 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257}
258
Jan Kiszka0159d542010-02-08 10:12:27 +0000259static void capiminor_destroy(struct kref *kref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260{
Jan Kiszka0159d542010-02-08 10:12:27 +0000261 struct capiminor *mp = container_of(kref, struct capiminor, kref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
Jan Kiszkadfbb84f2010-02-08 10:12:40 +0000263 kfree_skb(mp->outskb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 skb_queue_purge(&mp->inqueue);
265 skb_queue_purge(&mp->outqueue);
266 capiminor_del_all_ack(mp);
267 kfree(mp);
268}
269
Jan Kiszka40fb2d02010-02-08 10:12:25 +0000270static struct capiminor *capiminor_get(unsigned int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271{
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000272 struct capiminor *mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
Jan Kiszka3d5d30f2010-02-08 10:12:33 +0000274 spin_lock(&capiminors_lock);
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000275 mp = capiminors[minor];
Jan Kiszka0159d542010-02-08 10:12:27 +0000276 if (mp)
277 kref_get(&mp->kref);
Jan Kiszka3d5d30f2010-02-08 10:12:33 +0000278 spin_unlock(&capiminors_lock);
Jan Kiszka81d17fe2010-02-08 10:12:23 +0000279
280 return mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
Jan Kiszka0159d542010-02-08 10:12:27 +0000283static inline void capiminor_put(struct capiminor *mp)
284{
285 kref_put(&mp->kref, capiminor_destroy);
286}
287
288static void capiminor_free(struct capiminor *mp)
289{
Jan Kiszka0159d542010-02-08 10:12:27 +0000290 tty_unregister_device(capinc_tty_driver, mp->minor);
291
Jan Kiszka3d5d30f2010-02-08 10:12:33 +0000292 spin_lock(&capiminors_lock);
Jan Kiszka0159d542010-02-08 10:12:27 +0000293 capiminors[mp->minor] = NULL;
Jan Kiszka3d5d30f2010-02-08 10:12:33 +0000294 spin_unlock(&capiminors_lock);
Jan Kiszka0159d542010-02-08 10:12:27 +0000295
296 capiminor_put(mp);
297}
298
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299/* -------- struct capincci ----------------------------------------- */
300
Jan Kiszka501c87a2010-02-08 10:12:16 +0000301static void capincci_alloc_minor(struct capidev *cdev, struct capincci *np)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302{
Jan Kiszka501c87a2010-02-08 10:12:16 +0000303 struct capiminor *mp;
Jan Kiszkae95ac142010-02-08 10:12:26 +0000304 dev_t device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
Jan Kiszka501c87a2010-02-08 10:12:16 +0000306 if (!(cdev->userflags & CAPIFLAG_HIGHJACKING))
307 return;
308
309 mp = np->minorp = capiminor_alloc(&cdev->ap, np->ncci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 if (mp) {
Jan Kiszkae95ac142010-02-08 10:12:26 +0000311 device = MKDEV(capinc_tty_driver->major, mp->minor);
312 mp->capifs_dentry = capifs_new_ncci(mp->minor, device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 }
Jan Kiszka501c87a2010-02-08 10:12:16 +0000314}
315
316static void capincci_free_minor(struct capincci *np)
317{
318 struct capiminor *mp = np->minorp;
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000319 struct tty_struct *tty;
Jan Kiszka501c87a2010-02-08 10:12:16 +0000320
321 if (mp) {
322 capifs_free_ncci(mp->capifs_dentry);
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000323
324 tty = tty_port_tty_get(&mp->port);
325 if (tty) {
Jan Kiszka30bced92010-02-08 10:12:31 +0000326 tty_vhangup(tty);
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000327 tty_kref_put(tty);
Jan Kiszka501c87a2010-02-08 10:12:16 +0000328 }
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000329
330 capiminor_free(mp);
Jan Kiszka501c87a2010-02-08 10:12:16 +0000331 }
332}
333
334static inline unsigned int capincci_minor_opencount(struct capincci *np)
335{
336 struct capiminor *mp = np->minorp;
Jan Kiszkaa84fdf42010-02-08 10:12:34 +0000337 unsigned int count = 0;
338 struct tty_struct *tty;
Jan Kiszka501c87a2010-02-08 10:12:16 +0000339
Jan Kiszkaa84fdf42010-02-08 10:12:34 +0000340 if (mp) {
341 tty = tty_port_tty_get(&mp->port);
342 if (tty) {
343 count = tty->count;
344 tty_kref_put(tty);
345 }
346 }
347 return count;
Jan Kiszka501c87a2010-02-08 10:12:16 +0000348}
349
350#else /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
351
352static inline void
353capincci_alloc_minor(struct capidev *cdev, struct capincci *np) { }
354static inline void capincci_free_minor(struct capincci *np) { }
355
356static inline unsigned int capincci_minor_opencount(struct capincci *np)
357{
358 return 0;
359}
360
361#endif /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
362
363static struct capincci *capincci_alloc(struct capidev *cdev, u32 ncci)
364{
Jan Kiszka884f5c42010-02-08 10:12:22 +0000365 struct capincci *np;
Jan Kiszka501c87a2010-02-08 10:12:16 +0000366
Jan Kiszka54f0fad2010-02-08 10:12:20 +0000367 np = kzalloc(sizeof(*np), GFP_KERNEL);
Jan Kiszka501c87a2010-02-08 10:12:16 +0000368 if (!np)
369 return NULL;
370 np->ncci = ncci;
371 np->cdev = cdev;
372
373 capincci_alloc_minor(cdev, np);
374
Jan Kiszka884f5c42010-02-08 10:12:22 +0000375 list_add_tail(&np->list, &cdev->nccis);
376
377 return np;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378}
379
380static void capincci_free(struct capidev *cdev, u32 ncci)
381{
Jan Kiszka884f5c42010-02-08 10:12:22 +0000382 struct capincci *np, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
Jan Kiszka884f5c42010-02-08 10:12:22 +0000384 list_for_each_entry_safe(np, tmp, &cdev->nccis, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 if (ncci == 0xffffffff || np->ncci == ncci) {
Jan Kiszka501c87a2010-02-08 10:12:16 +0000386 capincci_free_minor(np);
Jan Kiszka884f5c42010-02-08 10:12:22 +0000387 list_del(&np->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 kfree(np);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390}
391
392static struct capincci *capincci_find(struct capidev *cdev, u32 ncci)
393{
Jan Kiszka884f5c42010-02-08 10:12:22 +0000394 struct capincci *np;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
Jan Kiszka884f5c42010-02-08 10:12:22 +0000396 list_for_each_entry(np, &cdev->nccis, list)
397 if (np->ncci == ncci)
398 return np;
399 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400}
401
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
403/* -------- handle data queue --------------------------------------- */
404
405static struct sk_buff *
406gen_data_b3_resp_for(struct capiminor *mp, struct sk_buff *skb)
407{
408 struct sk_buff *nskb;
Jan Kiszkadfbb84f2010-02-08 10:12:40 +0000409 nskb = alloc_skb(CAPI_DATA_B3_RESP_LEN, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 if (nskb) {
411 u16 datahandle = CAPIMSG_U16(skb->data,CAPIMSG_BASELEN+4+4+2);
412 unsigned char *s = skb_put(nskb, CAPI_DATA_B3_RESP_LEN);
413 capimsg_setu16(s, 0, CAPI_DATA_B3_RESP_LEN);
414 capimsg_setu16(s, 2, mp->ap->applid);
415 capimsg_setu8 (s, 4, CAPI_DATA_B3);
416 capimsg_setu8 (s, 5, CAPI_RESP);
Jan Kiszka42792712010-02-08 10:12:38 +0000417 capimsg_setu16(s, 6, atomic_inc_return(&mp->msgid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 capimsg_setu32(s, 8, mp->ncci);
419 capimsg_setu16(s, 12, datahandle);
420 }
421 return nskb;
422}
423
424static int handle_recv_skb(struct capiminor *mp, struct sk_buff *skb)
425{
Jan Kiszkaa11ef7b2010-02-08 10:12:36 +0000426 unsigned int datalen = skb->len - CAPIMSG_LEN(skb->data);
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000427 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 struct sk_buff *nskb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 u16 errcode, datahandle;
430 struct tty_ldisc *ld;
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000431 int ret = -1;
432
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000433 tty = tty_port_tty_get(&mp->port);
434 if (!tty) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435#ifdef _DEBUG_DATAFLOW
436 printk(KERN_DEBUG "capi: currently no receiver\n");
437#endif
438 return -1;
439 }
440
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000441 ld = tty_ldisc_ref(tty);
Jan Kiszkaa11ef7b2010-02-08 10:12:36 +0000442 if (!ld) {
443 /* fatal error, do not requeue */
444 ret = 0;
445 kfree_skb(skb);
446 goto deref_tty;
447 }
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000448
Alan Coxa352def2008-07-16 21:53:12 +0100449 if (ld->ops->receive_buf == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450#if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
451 printk(KERN_DEBUG "capi: ldisc has no receive_buf function\n");
452#endif
Jan Kiszkaa11ef7b2010-02-08 10:12:36 +0000453 /* fatal error, do not requeue */
454 goto free_skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 }
456 if (mp->ttyinstop) {
457#if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
458 printk(KERN_DEBUG "capi: recv tty throttled\n");
459#endif
Jan Kiszkaa11ef7b2010-02-08 10:12:36 +0000460 goto deref_ldisc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 }
Jan Kiszkaa11ef7b2010-02-08 10:12:36 +0000462
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000463 if (tty->receive_room < datalen) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464#if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
465 printk(KERN_DEBUG "capi: no room in tty\n");
466#endif
Jan Kiszkaa11ef7b2010-02-08 10:12:36 +0000467 goto deref_ldisc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 }
Jan Kiszkaa11ef7b2010-02-08 10:12:36 +0000469
470 nskb = gen_data_b3_resp_for(mp, skb);
471 if (!nskb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 printk(KERN_ERR "capi: gen_data_b3_resp failed\n");
Jan Kiszkaa11ef7b2010-02-08 10:12:36 +0000473 goto deref_ldisc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 }
Jan Kiszkaa11ef7b2010-02-08 10:12:36 +0000475
476 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN + 4);
477
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 errcode = capi20_put_message(mp->ap, nskb);
Jan Kiszkaa11ef7b2010-02-08 10:12:36 +0000479
480 if (errcode == CAPI_NOERROR) {
481 skb_pull(skb, CAPIMSG_LEN(skb->data));
482#ifdef _DEBUG_DATAFLOW
483 printk(KERN_DEBUG "capi: DATA_B3_RESP %u len=%d => ldisc\n",
484 datahandle, skb->len);
485#endif
486 ld->ops->receive_buf(tty, skb->data, NULL, skb->len);
487 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 printk(KERN_ERR "capi: send DATA_B3_RESP failed=%x\n",
489 errcode);
490 kfree_skb(nskb);
Jan Kiszkaa11ef7b2010-02-08 10:12:36 +0000491
492 if (errcode == CAPI_SENDQUEUEFULL)
493 goto deref_ldisc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 }
Jan Kiszkaa11ef7b2010-02-08 10:12:36 +0000495
496free_skb:
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000497 ret = 0;
Jan Kiszkaa11ef7b2010-02-08 10:12:36 +0000498 kfree_skb(skb);
499
500deref_ldisc:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 tty_ldisc_deref(ld);
Jan Kiszkaa11ef7b2010-02-08 10:12:36 +0000502
503deref_tty:
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000504 tty_kref_put(tty);
505 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506}
507
508static void handle_minor_recv(struct capiminor *mp)
509{
510 struct sk_buff *skb;
Jan Kiszka68d73472010-02-08 10:12:39 +0000511
512 while ((skb = skb_dequeue(&mp->inqueue)) != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 if (handle_recv_skb(mp, skb) < 0) {
514 skb_queue_head(&mp->inqueue, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 return;
516 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517}
518
519static int handle_minor_send(struct capiminor *mp)
520{
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000521 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 struct sk_buff *skb;
523 u16 len;
524 int count = 0;
525 u16 errcode;
526 u16 datahandle;
527
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000528 tty = tty_port_tty_get(&mp->port);
529 if (!tty)
530 return 0;
531
532 if (mp->ttyoutstop) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533#if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
534 printk(KERN_DEBUG "capi: send: tty stopped\n");
535#endif
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000536 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 return 0;
538 }
539
Jan Kiszkadfbb84f2010-02-08 10:12:40 +0000540 while (1) {
541 spin_lock_bh(&mp->outlock);
542 skb = __skb_dequeue(&mp->outqueue);
543 if (!skb) {
544 spin_unlock_bh(&mp->outlock);
545 break;
546 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 len = (u16)skb->len;
Jan Kiszkadfbb84f2010-02-08 10:12:40 +0000548 mp->outbytes -= len;
549 spin_unlock_bh(&mp->outlock);
550
551 datahandle = atomic_inc_return(&mp->datahandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 skb_push(skb, CAPI_DATA_B3_REQ_LEN);
553 memset(skb->data, 0, CAPI_DATA_B3_REQ_LEN);
554 capimsg_setu16(skb->data, 0, CAPI_DATA_B3_REQ_LEN);
555 capimsg_setu16(skb->data, 2, mp->ap->applid);
556 capimsg_setu8 (skb->data, 4, CAPI_DATA_B3);
557 capimsg_setu8 (skb->data, 5, CAPI_REQ);
Jan Kiszka42792712010-02-08 10:12:38 +0000558 capimsg_setu16(skb->data, 6, atomic_inc_return(&mp->msgid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 capimsg_setu32(skb->data, 8, mp->ncci); /* NCCI */
Andrew Morton5e6c20a2007-07-17 04:04:22 -0700560 capimsg_setu32(skb->data, 12, (u32)(long)skb->data);/* Data32 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 capimsg_setu16(skb->data, 16, len); /* Data length */
562 capimsg_setu16(skb->data, 18, datahandle);
563 capimsg_setu16(skb->data, 20, 0); /* Flags */
564
Jan Kiszka501c87a2010-02-08 10:12:16 +0000565 if (capiminor_add_ack(mp, datahandle) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 skb_pull(skb, CAPI_DATA_B3_REQ_LEN);
Jan Kiszkadfbb84f2010-02-08 10:12:40 +0000567
568 spin_lock_bh(&mp->outlock);
569 __skb_queue_head(&mp->outqueue, skb);
570 mp->outbytes += len;
571 spin_unlock_bh(&mp->outlock);
572
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000573 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 return count;
575 }
576 errcode = capi20_put_message(mp->ap, skb);
577 if (errcode == CAPI_NOERROR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579#ifdef _DEBUG_DATAFLOW
580 printk(KERN_DEBUG "capi: DATA_B3_REQ %u len=%u\n",
581 datahandle, len);
582#endif
583 continue;
584 }
585 capiminor_del_ack(mp, datahandle);
586
587 if (errcode == CAPI_SENDQUEUEFULL) {
588 skb_pull(skb, CAPI_DATA_B3_REQ_LEN);
Jan Kiszkadfbb84f2010-02-08 10:12:40 +0000589
590 spin_lock_bh(&mp->outlock);
591 __skb_queue_head(&mp->outqueue, skb);
592 mp->outbytes += len;
593 spin_unlock_bh(&mp->outlock);
594
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 break;
596 }
597
598 /* ups, drop packet */
599 printk(KERN_ERR "capi: put_message = %x\n", errcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 kfree_skb(skb);
601 }
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000602 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 return count;
604}
605
606#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
607/* -------- function called by lower level -------------------------- */
608
609static void capi_recv_message(struct capi20_appl *ap, struct sk_buff *skb)
610{
611 struct capidev *cdev = ap->private;
612#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000613 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 struct capiminor *mp;
615 u16 datahandle;
616#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
617 struct capincci *np;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
Jan Kiszka05b41492010-02-08 10:12:19 +0000619 mutex_lock(&cdev->lock);
620
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_CONF) {
622 u16 info = CAPIMSG_U16(skb->data, 12); // Info field
Jan Kiszka05b41492010-02-08 10:12:19 +0000623 if ((info & 0xff00) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 }
Jan Kiszka05b41492010-02-08 10:12:19 +0000626 if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_IND)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
Jan Kiszka05b41492010-02-08 10:12:19 +0000628
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 if (CAPIMSG_COMMAND(skb->data) != CAPI_DATA_B3) {
630 skb_queue_tail(&cdev->recvqueue, skb);
631 wake_up_interruptible(&cdev->recvwait);
Jan Kiszka05b41492010-02-08 10:12:19 +0000632 goto unlock_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 }
Jan Kiszka05b41492010-02-08 10:12:19 +0000634
635 np = capincci_find(cdev, CAPIMSG_CONTROL(skb->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 if (!np) {
637 printk(KERN_ERR "BUG: capi_signal: ncci not found\n");
638 skb_queue_tail(&cdev->recvqueue, skb);
639 wake_up_interruptible(&cdev->recvwait);
Jan Kiszka05b41492010-02-08 10:12:19 +0000640 goto unlock_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 }
Jan Kiszka501c87a2010-02-08 10:12:16 +0000642
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643#ifndef CONFIG_ISDN_CAPI_MIDDLEWARE
644 skb_queue_tail(&cdev->recvqueue, skb);
645 wake_up_interruptible(&cdev->recvwait);
Jan Kiszka501c87a2010-02-08 10:12:16 +0000646
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647#else /* CONFIG_ISDN_CAPI_MIDDLEWARE */
Jan Kiszka501c87a2010-02-08 10:12:16 +0000648
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 mp = np->minorp;
650 if (!mp) {
651 skb_queue_tail(&cdev->recvqueue, skb);
652 wake_up_interruptible(&cdev->recvwait);
Jan Kiszka05b41492010-02-08 10:12:19 +0000653 goto unlock_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_IND) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4+4+2);
657#ifdef _DEBUG_DATAFLOW
658 printk(KERN_DEBUG "capi_signal: DATA_B3_IND %u len=%d\n",
659 datahandle, skb->len-CAPIMSG_LEN(skb->data));
660#endif
661 skb_queue_tail(&mp->inqueue, skb);
Jan Kiszka68d73472010-02-08 10:12:39 +0000662
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 handle_minor_recv(mp);
664
665 } else if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_CONF) {
666
667 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4);
668#ifdef _DEBUG_DATAFLOW
669 printk(KERN_DEBUG "capi_signal: DATA_B3_CONF %u 0x%x\n",
670 datahandle,
671 CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4+2));
672#endif
673 kfree_skb(skb);
Jan Kiszka2b72b5b2010-02-08 10:12:41 +0000674 capiminor_del_ack(mp, datahandle);
Jan Kiszkafb4b4882010-02-08 10:12:29 +0000675 tty = tty_port_tty_get(&mp->port);
676 if (tty) {
677 tty_wakeup(tty);
678 tty_kref_put(tty);
679 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 (void)handle_minor_send(mp);
681
682 } else {
683 /* ups, let capi application handle it :-) */
684 skb_queue_tail(&cdev->recvqueue, skb);
685 wake_up_interruptible(&cdev->recvwait);
686 }
687#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
Jan Kiszka501c87a2010-02-08 10:12:16 +0000688
Jan Kiszka05b41492010-02-08 10:12:19 +0000689unlock_out:
Jan Kiszka05b41492010-02-08 10:12:19 +0000690 mutex_unlock(&cdev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691}
692
693/* -------- file_operations for capidev ----------------------------- */
694
695static ssize_t
696capi_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
697{
698 struct capidev *cdev = (struct capidev *)file->private_data;
699 struct sk_buff *skb;
700 size_t copied;
Jan Kiszka28a1dbb2010-02-08 10:12:21 +0000701 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
703 if (!cdev->ap.applid)
704 return -ENODEV;
705
Jan Kiszka28a1dbb2010-02-08 10:12:21 +0000706 skb = skb_dequeue(&cdev->recvqueue);
707 if (!skb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 if (file->f_flags & O_NONBLOCK)
709 return -EAGAIN;
Jan Kiszka28a1dbb2010-02-08 10:12:21 +0000710 err = wait_event_interruptible(cdev->recvwait,
711 (skb = skb_dequeue(&cdev->recvqueue)));
712 if (err)
713 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 }
715 if (skb->len > count) {
716 skb_queue_head(&cdev->recvqueue, skb);
717 return -EMSGSIZE;
718 }
719 if (copy_to_user(buf, skb->data, skb->len)) {
720 skb_queue_head(&cdev->recvqueue, skb);
721 return -EFAULT;
722 }
723 copied = skb->len;
724
725 kfree_skb(skb);
726
727 return copied;
728}
729
730static ssize_t
731capi_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
732{
733 struct capidev *cdev = (struct capidev *)file->private_data;
734 struct sk_buff *skb;
735 u16 mlen;
736
737 if (!cdev->ap.applid)
738 return -ENODEV;
739
740 skb = alloc_skb(count, GFP_USER);
741 if (!skb)
742 return -ENOMEM;
743
744 if (copy_from_user(skb_put(skb, count), buf, count)) {
745 kfree_skb(skb);
746 return -EFAULT;
747 }
748 mlen = CAPIMSG_LEN(skb->data);
749 if (CAPIMSG_CMD(skb->data) == CAPI_DATA_B3_REQ) {
750 if ((size_t)(mlen + CAPIMSG_DATALEN(skb->data)) != count) {
751 kfree_skb(skb);
752 return -EINVAL;
753 }
754 } else {
755 if (mlen != count) {
756 kfree_skb(skb);
757 return -EINVAL;
758 }
759 }
760 CAPIMSG_SETAPPID(skb->data, cdev->ap.applid);
761
762 if (CAPIMSG_CMD(skb->data) == CAPI_DISCONNECT_B3_RESP) {
Jan Kiszka05b41492010-02-08 10:12:19 +0000763 mutex_lock(&cdev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 capincci_free(cdev, CAPIMSG_NCCI(skb->data));
Jan Kiszka05b41492010-02-08 10:12:19 +0000765 mutex_unlock(&cdev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 }
767
768 cdev->errcode = capi20_put_message(&cdev->ap, skb);
769
770 if (cdev->errcode) {
771 kfree_skb(skb);
772 return -EIO;
773 }
774 return count;
775}
776
777static unsigned int
778capi_poll(struct file *file, poll_table * wait)
779{
780 struct capidev *cdev = (struct capidev *)file->private_data;
781 unsigned int mask = 0;
782
783 if (!cdev->ap.applid)
784 return POLLERR;
785
786 poll_wait(file, &(cdev->recvwait), wait);
787 mask = POLLOUT | POLLWRNORM;
788 if (!skb_queue_empty(&cdev->recvqueue))
789 mask |= POLLIN | POLLRDNORM;
790 return mask;
791}
792
793static int
794capi_ioctl(struct inode *inode, struct file *file,
795 unsigned int cmd, unsigned long arg)
796{
797 struct capidev *cdev = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 capi_ioctl_struct data;
799 int retval = -EINVAL;
800 void __user *argp = (void __user *)arg;
801
802 switch (cmd) {
803 case CAPI_REGISTER:
Jan Kiszka05b41492010-02-08 10:12:19 +0000804 mutex_lock(&cdev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
Jan Kiszka05b41492010-02-08 10:12:19 +0000806 if (cdev->ap.applid) {
807 retval = -EEXIST;
808 goto register_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 }
Jan Kiszka05b41492010-02-08 10:12:19 +0000810 if (copy_from_user(&cdev->ap.rparam, argp,
811 sizeof(struct capi_register_params))) {
812 retval = -EFAULT;
813 goto register_out;
814 }
815 cdev->ap.private = cdev;
816 cdev->ap.recv_message = capi_recv_message;
817 cdev->errcode = capi20_register(&cdev->ap);
818 retval = (int)cdev->ap.applid;
819 if (cdev->errcode) {
820 cdev->ap.applid = 0;
821 retval = -EIO;
822 }
823
824register_out:
825 mutex_unlock(&cdev->lock);
826 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
828 case CAPI_GET_VERSION:
829 {
830 if (copy_from_user(&data.contr, argp,
831 sizeof(data.contr)))
832 return -EFAULT;
833 cdev->errcode = capi20_get_version(data.contr, &data.version);
834 if (cdev->errcode)
835 return -EIO;
836 if (copy_to_user(argp, &data.version,
837 sizeof(data.version)))
838 return -EFAULT;
839 }
840 return 0;
841
842 case CAPI_GET_SERIAL:
843 {
844 if (copy_from_user(&data.contr, argp,
845 sizeof(data.contr)))
846 return -EFAULT;
847 cdev->errcode = capi20_get_serial (data.contr, data.serial);
848 if (cdev->errcode)
849 return -EIO;
850 if (copy_to_user(argp, data.serial,
851 sizeof(data.serial)))
852 return -EFAULT;
853 }
854 return 0;
855 case CAPI_GET_PROFILE:
856 {
857 if (copy_from_user(&data.contr, argp,
858 sizeof(data.contr)))
859 return -EFAULT;
860
861 if (data.contr == 0) {
862 cdev->errcode = capi20_get_profile(data.contr, &data.profile);
863 if (cdev->errcode)
864 return -EIO;
865
866 retval = copy_to_user(argp,
867 &data.profile.ncontroller,
868 sizeof(data.profile.ncontroller));
869
870 } else {
871 cdev->errcode = capi20_get_profile(data.contr, &data.profile);
872 if (cdev->errcode)
873 return -EIO;
874
875 retval = copy_to_user(argp, &data.profile,
876 sizeof(data.profile));
877 }
878 if (retval)
879 return -EFAULT;
880 }
881 return 0;
882
883 case CAPI_GET_MANUFACTURER:
884 {
885 if (copy_from_user(&data.contr, argp,
886 sizeof(data.contr)))
887 return -EFAULT;
888 cdev->errcode = capi20_get_manufacturer(data.contr, data.manufacturer);
889 if (cdev->errcode)
890 return -EIO;
891
892 if (copy_to_user(argp, data.manufacturer,
893 sizeof(data.manufacturer)))
894 return -EFAULT;
895
896 }
897 return 0;
898 case CAPI_GET_ERRCODE:
899 data.errcode = cdev->errcode;
900 cdev->errcode = CAPI_NOERROR;
901 if (arg) {
902 if (copy_to_user(argp, &data.errcode,
903 sizeof(data.errcode)))
904 return -EFAULT;
905 }
906 return data.errcode;
907
908 case CAPI_INSTALLED:
909 if (capi20_isinstalled() == CAPI_NOERROR)
910 return 0;
911 return -ENXIO;
912
913 case CAPI_MANUFACTURER_CMD:
914 {
915 struct capi_manufacturer_cmd mcmd;
916 if (!capable(CAP_SYS_ADMIN))
917 return -EPERM;
918 if (copy_from_user(&mcmd, argp, sizeof(mcmd)))
919 return -EFAULT;
920 return capi20_manufacturer(mcmd.cmd, mcmd.data);
921 }
922 return 0;
923
924 case CAPI_SET_FLAGS:
Jan Kiszka05b41492010-02-08 10:12:19 +0000925 case CAPI_CLR_FLAGS: {
926 unsigned userflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
Jan Kiszka05b41492010-02-08 10:12:19 +0000928 if (copy_from_user(&userflags, argp, sizeof(userflags)))
929 return -EFAULT;
930
931 mutex_lock(&cdev->lock);
932 if (cmd == CAPI_SET_FLAGS)
933 cdev->userflags |= userflags;
934 else
935 cdev->userflags &= ~userflags;
936 mutex_unlock(&cdev->lock);
937 return 0;
938 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 case CAPI_GET_FLAGS:
940 if (copy_to_user(argp, &cdev->userflags,
941 sizeof(cdev->userflags)))
942 return -EFAULT;
943 return 0;
944
Jan Kiszka05b41492010-02-08 10:12:19 +0000945 case CAPI_NCCI_OPENCOUNT: {
946 struct capincci *nccip;
947 unsigned ncci;
948 int count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
Jan Kiszka05b41492010-02-08 10:12:19 +0000950 if (copy_from_user(&ncci, argp, sizeof(ncci)))
951 return -EFAULT;
952
953 mutex_lock(&cdev->lock);
954 nccip = capincci_find(cdev, (u32)ncci);
955 if (nccip)
956 count = capincci_minor_opencount(nccip);
957 mutex_unlock(&cdev->lock);
958 return count;
959 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
961#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
Jan Kiszka05b41492010-02-08 10:12:19 +0000962 case CAPI_NCCI_GETUNIT: {
963 struct capincci *nccip;
964 struct capiminor *mp;
965 unsigned ncci;
966 int unit = -ESRCH;
967
968 if (copy_from_user(&ncci, argp, sizeof(ncci)))
969 return -EFAULT;
970
971 mutex_lock(&cdev->lock);
972 nccip = capincci_find(cdev, (u32)ncci);
973 if (nccip) {
974 mp = nccip->minorp;
975 if (mp)
976 unit = mp->minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 }
Jan Kiszka05b41492010-02-08 10:12:19 +0000978 mutex_unlock(&cdev->lock);
979 return unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 }
Jan Kiszka05b41492010-02-08 10:12:19 +0000981#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
982
983 default:
984 return -EINVAL;
985 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986}
987
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000988static int capi_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989{
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000990 struct capidev *cdev;
991
992 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
993 if (!cdev)
994 return -ENOMEM;
995
Jan Kiszka05b41492010-02-08 10:12:19 +0000996 mutex_init(&cdev->lock);
Jan Kiszkaeca39dd2010-02-08 10:12:18 +0000997 skb_queue_head_init(&cdev->recvqueue);
998 init_waitqueue_head(&cdev->recvwait);
Jan Kiszka884f5c42010-02-08 10:12:22 +0000999 INIT_LIST_HEAD(&cdev->nccis);
Jan Kiszkaeca39dd2010-02-08 10:12:18 +00001000 file->private_data = cdev;
1001
1002 mutex_lock(&capidev_list_lock);
1003 list_add_tail(&cdev->list, &capidev_list);
1004 mutex_unlock(&capidev_list_lock);
1005
1006 return nonseekable_open(inode, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007}
1008
Jan Kiszkaeca39dd2010-02-08 10:12:18 +00001009static int capi_release(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010{
Jan Kiszkaeca39dd2010-02-08 10:12:18 +00001011 struct capidev *cdev = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
Jan Kiszkaeca39dd2010-02-08 10:12:18 +00001013 mutex_lock(&capidev_list_lock);
1014 list_del(&cdev->list);
1015 mutex_unlock(&capidev_list_lock);
1016
Jan Kiszka05b41492010-02-08 10:12:19 +00001017 if (cdev->ap.applid)
Jan Kiszkaeca39dd2010-02-08 10:12:18 +00001018 capi20_release(&cdev->ap);
Jan Kiszkaeca39dd2010-02-08 10:12:18 +00001019 skb_queue_purge(&cdev->recvqueue);
Jan Kiszkaeca39dd2010-02-08 10:12:18 +00001020 capincci_free(cdev, 0xffffffff);
Jan Kiszkaeca39dd2010-02-08 10:12:18 +00001021
1022 kfree(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 return 0;
1024}
1025
Arjan van de Ven2b8693c2007-02-12 00:55:32 -08001026static const struct file_operations capi_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027{
1028 .owner = THIS_MODULE,
1029 .llseek = no_llseek,
1030 .read = capi_read,
1031 .write = capi_write,
1032 .poll = capi_poll,
1033 .ioctl = capi_ioctl,
1034 .open = capi_open,
1035 .release = capi_release,
1036};
1037
1038#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1039/* -------- tty_operations for capincci ----------------------------- */
1040
Jan Kiszka46324512010-02-08 10:12:28 +00001041static int
1042capinc_tty_install(struct tty_driver *driver, struct tty_struct *tty)
1043{
1044 int idx = tty->index;
1045 struct capiminor *mp = capiminor_get(idx);
1046 int ret = tty_init_termios(tty);
1047
1048 if (ret == 0) {
1049 tty_driver_kref_get(driver);
1050 tty->count++;
1051 tty->driver_data = mp;
1052 driver->ttys[idx] = tty;
1053 } else
1054 capiminor_put(mp);
1055 return ret;
1056}
1057
1058static void capinc_tty_cleanup(struct tty_struct *tty)
1059{
1060 struct capiminor *mp = tty->driver_data;
1061 tty->driver_data = NULL;
1062 capiminor_put(mp);
1063}
1064
Jan Kiszkafb4b4882010-02-08 10:12:29 +00001065static int capinc_tty_open(struct tty_struct *tty, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066{
Jan Kiszka46324512010-02-08 10:12:28 +00001067 struct capiminor *mp = tty->driver_data;
Jan Kiszkafb4b4882010-02-08 10:12:29 +00001068 int err;
1069
1070 err = tty_port_open(&mp->port, tty, filp);
1071 if (err)
1072 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 handle_minor_recv(mp);
1075 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
Jan Kiszkafb4b4882010-02-08 10:12:29 +00001082 tty_port_close(&mp->port, tty, filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083}
1084
Jan Kiszka6576c282010-02-08 10:12:32 +00001085static int capinc_tty_write(struct tty_struct *tty,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 const unsigned char *buf, int count)
1087{
Jan Kiszka2c8df722010-02-08 10:12:30 +00001088 struct capiminor *mp = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 struct sk_buff *skb;
1090
1091#ifdef _DEBUG_TTYFUNCS
1092 printk(KERN_DEBUG "capinc_tty_write(count=%d)\n", count);
1093#endif
1094
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001095 spin_lock_bh(&mp->outlock);
1096 skb = mp->outskb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 if (skb) {
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001098 mp->outskb = NULL;
1099 __skb_queue_tail(&mp->outqueue, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 mp->outbytes += skb->len;
1101 }
1102
1103 skb = alloc_skb(CAPI_DATA_B3_REQ_LEN+count, GFP_ATOMIC);
1104 if (!skb) {
1105 printk(KERN_ERR "capinc_tty_write: alloc_skb failed\n");
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001106 spin_unlock_bh(&mp->outlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 return -ENOMEM;
1108 }
1109
1110 skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
1111 memcpy(skb_put(skb, count), buf, count);
1112
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001113 __skb_queue_tail(&mp->outqueue, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 mp->outbytes += skb->len;
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001115 spin_unlock_bh(&mp->outlock);
1116
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 (void)handle_minor_send(mp);
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001118
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 return count;
1120}
1121
Alan Coxf2545a72008-04-30 00:54:09 -07001122static int capinc_tty_put_char(struct tty_struct *tty, unsigned char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123{
Jan Kiszka2c8df722010-02-08 10:12:30 +00001124 struct capiminor *mp = tty->driver_data;
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001125 bool invoke_send = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 struct sk_buff *skb;
Alan Coxf2545a72008-04-30 00:54:09 -07001127 int ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
1129#ifdef _DEBUG_TTYFUNCS
1130 printk(KERN_DEBUG "capinc_put_char(%u)\n", ch);
1131#endif
1132
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001133 spin_lock_bh(&mp->outlock);
1134 skb = mp->outskb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 if (skb) {
1136 if (skb_tailroom(skb) > 0) {
1137 *(skb_put(skb, 1)) = ch;
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001138 goto unlock_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 }
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001140 mp->outskb = NULL;
1141 __skb_queue_tail(&mp->outqueue, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 mp->outbytes += skb->len;
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001143 invoke_send = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 }
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001145
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 skb = alloc_skb(CAPI_DATA_B3_REQ_LEN+CAPI_MAX_BLKSIZE, GFP_ATOMIC);
1147 if (skb) {
1148 skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
1149 *(skb_put(skb, 1)) = ch;
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001150 mp->outskb = skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 } else {
1152 printk(KERN_ERR "capinc_put_char: char %u lost\n", ch);
Alan Coxf2545a72008-04-30 00:54:09 -07001153 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 }
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001155
1156unlock_out:
1157 spin_unlock_bh(&mp->outlock);
1158
1159 if (invoke_send)
1160 (void)handle_minor_send(mp);
1161
Alan Coxf2545a72008-04-30 00:54:09 -07001162 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163}
1164
1165static void capinc_tty_flush_chars(struct tty_struct *tty)
1166{
Jan Kiszka2c8df722010-02-08 10:12:30 +00001167 struct capiminor *mp = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 struct sk_buff *skb;
1169
1170#ifdef _DEBUG_TTYFUNCS
1171 printk(KERN_DEBUG "capinc_tty_flush_chars\n");
1172#endif
1173
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001174 spin_lock_bh(&mp->outlock);
1175 skb = mp->outskb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 if (skb) {
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001177 mp->outskb = NULL;
1178 __skb_queue_tail(&mp->outqueue, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 mp->outbytes += skb->len;
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001180 spin_unlock_bh(&mp->outlock);
1181
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 (void)handle_minor_send(mp);
Jan Kiszkadfbb84f2010-02-08 10:12:40 +00001183 } else
1184 spin_unlock_bh(&mp->outlock);
1185
1186 handle_minor_recv(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187}
1188
1189static int capinc_tty_write_room(struct tty_struct *tty)
1190{
Jan Kiszka2c8df722010-02-08 10:12:30 +00001191 struct capiminor *mp = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 int room;
Jan Kiszka2c8df722010-02-08 10:12:30 +00001193
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 room = CAPINC_MAX_SENDQUEUE-skb_queue_len(&mp->outqueue);
1195 room *= CAPI_MAX_BLKSIZE;
1196#ifdef _DEBUG_TTYFUNCS
1197 printk(KERN_DEBUG "capinc_tty_write_room = %d\n", room);
1198#endif
1199 return room;
1200}
1201
Adrian Bunk408b6642005-05-01 08:59:29 -07001202static int capinc_tty_chars_in_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203{
Jan Kiszka2c8df722010-02-08 10:12:30 +00001204 struct capiminor *mp = tty->driver_data;
1205
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206#ifdef _DEBUG_TTYFUNCS
1207 printk(KERN_DEBUG "capinc_tty_chars_in_buffer = %d nack=%d sq=%d rq=%d\n",
1208 mp->outbytes, mp->nack,
1209 skb_queue_len(&mp->outqueue),
1210 skb_queue_len(&mp->inqueue));
1211#endif
1212 return mp->outbytes;
1213}
1214
1215static int capinc_tty_ioctl(struct tty_struct *tty, struct file * file,
1216 unsigned int cmd, unsigned long arg)
1217{
1218 int error = 0;
1219 switch (cmd) {
1220 default:
Stephen Rothwell53e86312008-10-13 10:44:33 +01001221 error = n_tty_ioctl_helper(tty, file, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 break;
1223 }
1224 return error;
1225}
1226
Alan Cox606d0992006-12-08 02:38:45 -08001227static void capinc_tty_set_termios(struct tty_struct *tty, struct ktermios * old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228{
1229#ifdef _DEBUG_TTYFUNCS
1230 printk(KERN_DEBUG "capinc_tty_set_termios\n");
1231#endif
1232}
1233
Jan Kiszka2c8df722010-02-08 10:12:30 +00001234static void capinc_tty_throttle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235{
Jan Kiszka2c8df722010-02-08 10:12:30 +00001236 struct capiminor *mp = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237#ifdef _DEBUG_TTYFUNCS
1238 printk(KERN_DEBUG "capinc_tty_throttle\n");
1239#endif
Jan Kiszka2c8df722010-02-08 10:12:30 +00001240 mp->ttyinstop = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241}
1242
Jan Kiszka2c8df722010-02-08 10:12:30 +00001243static void capinc_tty_unthrottle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244{
Jan Kiszka2c8df722010-02-08 10:12:30 +00001245 struct capiminor *mp = tty->driver_data;
Jan Kiszka2c8df722010-02-08 10:12:30 +00001246
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247#ifdef _DEBUG_TTYFUNCS
1248 printk(KERN_DEBUG "capinc_tty_unthrottle\n");
1249#endif
Jan Kiszka2c8df722010-02-08 10:12:30 +00001250 mp->ttyinstop = 0;
1251 handle_minor_recv(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252}
1253
1254static void capinc_tty_stop(struct tty_struct *tty)
1255{
Jan Kiszka2c8df722010-02-08 10:12:30 +00001256 struct capiminor *mp = tty->driver_data;
1257
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258#ifdef _DEBUG_TTYFUNCS
1259 printk(KERN_DEBUG "capinc_tty_stop\n");
1260#endif
Jan Kiszka2c8df722010-02-08 10:12:30 +00001261 mp->ttyoutstop = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262}
1263
1264static void capinc_tty_start(struct tty_struct *tty)
1265{
Jan Kiszka2c8df722010-02-08 10:12:30 +00001266 struct capiminor *mp = tty->driver_data;
Jan Kiszka2c8df722010-02-08 10:12:30 +00001267
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268#ifdef _DEBUG_TTYFUNCS
1269 printk(KERN_DEBUG "capinc_tty_start\n");
1270#endif
Jan Kiszka2c8df722010-02-08 10:12:30 +00001271 mp->ttyoutstop = 0;
1272 (void)handle_minor_send(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273}
1274
1275static void capinc_tty_hangup(struct tty_struct *tty)
1276{
Jan Kiszkafb4b4882010-02-08 10:12:29 +00001277 struct capiminor *mp = tty->driver_data;
1278
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279#ifdef _DEBUG_TTYFUNCS
1280 printk(KERN_DEBUG "capinc_tty_hangup\n");
1281#endif
Jan Kiszkafb4b4882010-02-08 10:12:29 +00001282 tty_port_hangup(&mp->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283}
1284
Alan Cox9e989662008-07-22 11:18:03 +01001285static int capinc_tty_break_ctl(struct tty_struct *tty, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286{
1287#ifdef _DEBUG_TTYFUNCS
1288 printk(KERN_DEBUG "capinc_tty_break_ctl(%d)\n", state);
1289#endif
Alan Cox9e989662008-07-22 11:18:03 +01001290 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291}
1292
1293static void capinc_tty_flush_buffer(struct tty_struct *tty)
1294{
1295#ifdef _DEBUG_TTYFUNCS
1296 printk(KERN_DEBUG "capinc_tty_flush_buffer\n");
1297#endif
1298}
1299
1300static void capinc_tty_set_ldisc(struct tty_struct *tty)
1301{
1302#ifdef _DEBUG_TTYFUNCS
1303 printk(KERN_DEBUG "capinc_tty_set_ldisc\n");
1304#endif
1305}
1306
1307static void capinc_tty_send_xchar(struct tty_struct *tty, char ch)
1308{
1309#ifdef _DEBUG_TTYFUNCS
1310 printk(KERN_DEBUG "capinc_tty_send_xchar(%d)\n", ch);
1311#endif
1312}
1313
Jeff Dikeb68e31d2006-10-02 02:17:18 -07001314static const struct tty_operations capinc_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 .open = capinc_tty_open,
1316 .close = capinc_tty_close,
1317 .write = capinc_tty_write,
1318 .put_char = capinc_tty_put_char,
1319 .flush_chars = capinc_tty_flush_chars,
1320 .write_room = capinc_tty_write_room,
1321 .chars_in_buffer = capinc_tty_chars_in_buffer,
1322 .ioctl = capinc_tty_ioctl,
1323 .set_termios = capinc_tty_set_termios,
1324 .throttle = capinc_tty_throttle,
1325 .unthrottle = capinc_tty_unthrottle,
1326 .stop = capinc_tty_stop,
1327 .start = capinc_tty_start,
1328 .hangup = capinc_tty_hangup,
1329 .break_ctl = capinc_tty_break_ctl,
1330 .flush_buffer = capinc_tty_flush_buffer,
1331 .set_ldisc = capinc_tty_set_ldisc,
1332 .send_xchar = capinc_tty_send_xchar,
Jan Kiszka46324512010-02-08 10:12:28 +00001333 .install = capinc_tty_install,
1334 .cleanup = capinc_tty_cleanup,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335};
1336
Jan Kiszkae76b1542010-02-08 10:12:24 +00001337static int __init capinc_tty_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338{
1339 struct tty_driver *drv;
Jan Kiszkae76b1542010-02-08 10:12:24 +00001340 int err;
1341
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 if (capi_ttyminors > CAPINC_MAX_PORTS)
1343 capi_ttyminors = CAPINC_MAX_PORTS;
1344 if (capi_ttyminors <= 0)
1345 capi_ttyminors = CAPINC_NR_PORTS;
1346
Jan Kiszka81d17fe2010-02-08 10:12:23 +00001347 capiminors = kzalloc(sizeof(struct capi_minor *) * capi_ttyminors,
1348 GFP_KERNEL);
1349 if (!capiminors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 return -ENOMEM;
1351
Jan Kiszka81d17fe2010-02-08 10:12:23 +00001352 drv = alloc_tty_driver(capi_ttyminors);
1353 if (!drv) {
1354 kfree(capiminors);
1355 return -ENOMEM;
1356 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 drv->owner = THIS_MODULE;
1358 drv->driver_name = "capi_nc";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 drv->name = "capi";
Jan Kiszkae95ac142010-02-08 10:12:26 +00001360 drv->major = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 drv->minor_start = 0;
1362 drv->type = TTY_DRIVER_TYPE_SERIAL;
1363 drv->subtype = SERIAL_TYPE_NORMAL;
1364 drv->init_termios = tty_std_termios;
1365 drv->init_termios.c_iflag = ICRNL;
1366 drv->init_termios.c_oflag = OPOST | ONLCR;
1367 drv->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1368 drv->init_termios.c_lflag = 0;
Jan Kiszka40fb2d02010-02-08 10:12:25 +00001369 drv->flags =
1370 TTY_DRIVER_REAL_RAW | TTY_DRIVER_RESET_TERMIOS |
1371 TTY_DRIVER_DYNAMIC_DEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 tty_set_operations(drv, &capinc_ops);
Jan Kiszkae76b1542010-02-08 10:12:24 +00001373
1374 err = tty_register_driver(drv);
1375 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 put_tty_driver(drv);
Jan Kiszka81d17fe2010-02-08 10:12:23 +00001377 kfree(capiminors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 printk(KERN_ERR "Couldn't register capi_nc driver\n");
Jan Kiszkae76b1542010-02-08 10:12:24 +00001379 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 }
1381 capinc_tty_driver = drv;
1382 return 0;
1383}
1384
Jan Kiszkae76b1542010-02-08 10:12:24 +00001385static void __exit capinc_tty_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386{
Jan Kiszkae76b1542010-02-08 10:12:24 +00001387 tty_unregister_driver(capinc_tty_driver);
1388 put_tty_driver(capinc_tty_driver);
Jan Kiszka81d17fe2010-02-08 10:12:23 +00001389 kfree(capiminors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390}
1391
Jan Kiszka501c87a2010-02-08 10:12:16 +00001392#else /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
1393
1394static inline int capinc_tty_init(void)
1395{
1396 return 0;
1397}
1398
1399static inline void capinc_tty_exit(void) { }
1400
1401#endif /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
1403/* -------- /proc functions ----------------------------------------- */
1404
1405/*
1406 * /proc/capi/capi20:
1407 * minor applid nrecvctlpkt nrecvdatapkt nsendctlpkt nsenddatapkt
1408 */
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001409static int capi20_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410{
1411 struct capidev *cdev;
1412 struct list_head *l;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413
Jan Kiszkab8f433d2010-02-08 10:12:17 +00001414 mutex_lock(&capidev_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 list_for_each(l, &capidev_list) {
1416 cdev = list_entry(l, struct capidev, list);
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001417 seq_printf(m, "0 %d %lu %lu %lu %lu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 cdev->ap.applid,
1419 cdev->ap.nrecvctlpkt,
1420 cdev->ap.nrecvdatapkt,
1421 cdev->ap.nsentctlpkt,
1422 cdev->ap.nsentdatapkt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 }
Jan Kiszkab8f433d2010-02-08 10:12:17 +00001424 mutex_unlock(&capidev_list_lock);
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001425 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426}
1427
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001428static int capi20_proc_open(struct inode *inode, struct file *file)
1429{
1430 return single_open(file, capi20_proc_show, NULL);
1431}
1432
1433static const struct file_operations capi20_proc_fops = {
1434 .owner = THIS_MODULE,
1435 .open = capi20_proc_open,
1436 .read = seq_read,
1437 .llseek = seq_lseek,
1438 .release = single_release,
1439};
1440
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441/*
1442 * /proc/capi/capi20ncci:
1443 * applid ncci
1444 */
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001445static int capi20ncci_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446{
Jan Kiszka884f5c42010-02-08 10:12:22 +00001447 struct capidev *cdev;
1448 struct capincci *np;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
Jan Kiszkab8f433d2010-02-08 10:12:17 +00001450 mutex_lock(&capidev_list_lock);
Jan Kiszka884f5c42010-02-08 10:12:22 +00001451 list_for_each_entry(cdev, &capidev_list, list) {
Jan Kiszka05b41492010-02-08 10:12:19 +00001452 mutex_lock(&cdev->lock);
Jan Kiszka884f5c42010-02-08 10:12:22 +00001453 list_for_each_entry(np, &cdev->nccis, list)
1454 seq_printf(m, "%d 0x%x\n", cdev->ap.applid, np->ncci);
Jan Kiszka05b41492010-02-08 10:12:19 +00001455 mutex_unlock(&cdev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 }
Jan Kiszkab8f433d2010-02-08 10:12:17 +00001457 mutex_unlock(&capidev_list_lock);
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001458 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459}
1460
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001461static int capi20ncci_proc_open(struct inode *inode, struct file *file)
1462{
1463 return single_open(file, capi20ncci_proc_show, NULL);
1464}
1465
1466static const struct file_operations capi20ncci_proc_fops = {
1467 .owner = THIS_MODULE,
1468 .open = capi20ncci_proc_open,
1469 .read = seq_read,
1470 .llseek = seq_lseek,
1471 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472};
1473
1474static void __init proc_init(void)
1475{
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001476 proc_create("capi/capi20", 0, NULL, &capi20_proc_fops);
1477 proc_create("capi/capi20ncci", 0, NULL, &capi20ncci_proc_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478}
1479
1480static void __exit proc_exit(void)
1481{
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08001482 remove_proc_entry("capi/capi20", NULL);
1483 remove_proc_entry("capi/capi20ncci", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484}
1485
1486/* -------- init function and module interface ---------------------- */
1487
1488
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489static int __init capi_init(void)
1490{
Jan Kiszka88549d62010-02-08 10:12:09 +00001491 const char *compileinfo;
Andrew Morton6d9eac32006-03-28 01:56:19 -08001492 int major_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493
Andrew Morton6d9eac32006-03-28 01:56:19 -08001494 major_ret = register_chrdev(capi_major, "capi20", &capi_fops);
1495 if (major_ret < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 printk(KERN_ERR "capi20: unable to get major %d\n", capi_major);
Andrew Morton6d9eac32006-03-28 01:56:19 -08001497 return major_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 }
gregkh@suse.de56b22932005-03-23 10:01:41 -08001499 capi_class = class_create(THIS_MODULE, "capi");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 if (IS_ERR(capi_class)) {
1501 unregister_chrdev(capi_major, "capi20");
1502 return PTR_ERR(capi_class);
1503 }
1504
Greg Kroah-Hartmana9b12612008-07-21 20:03:34 -07001505 device_create(capi_class, NULL, MKDEV(capi_major, 0), NULL, "capi");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 if (capinc_tty_init() < 0) {
Tony Jonesd78b0362007-09-25 02:03:03 +02001508 device_destroy(capi_class, MKDEV(capi_major, 0));
gregkh@suse.de56b22932005-03-23 10:01:41 -08001509 class_destroy(capi_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 unregister_chrdev(capi_major, "capi20");
1511 return -ENOMEM;
1512 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513
1514 proc_init();
1515
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516#if defined(CONFIG_ISDN_CAPI_CAPIFS) || defined(CONFIG_ISDN_CAPI_CAPIFS_MODULE)
1517 compileinfo = " (middleware+capifs)";
Jan Kiszka501c87a2010-02-08 10:12:16 +00001518#elif defined(CONFIG_ISDN_CAPI_MIDDLEWARE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 compileinfo = " (no capifs)";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520#else
1521 compileinfo = " (no middleware)";
1522#endif
Jan Kiszka88549d62010-02-08 10:12:09 +00001523 printk(KERN_NOTICE "CAPI 2.0 started up with major %d%s\n",
1524 capi_major, compileinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525
1526 return 0;
1527}
1528
1529static void __exit capi_exit(void)
1530{
1531 proc_exit();
1532
Tony Jonesd78b0362007-09-25 02:03:03 +02001533 device_destroy(capi_class, MKDEV(capi_major, 0));
gregkh@suse.de56b22932005-03-23 10:01:41 -08001534 class_destroy(capi_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 unregister_chrdev(capi_major, "capi20");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 capinc_tty_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538}
1539
1540module_init(capi_init);
1541module_exit(capi_exit);