Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* $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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #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 Kaehlcke | 9ea6e5d | 2007-05-08 00:32:43 -0700 | [diff] [blame] | 21 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <linux/mm.h> |
Jonathan Corbet | a237f3b | 2008-05-16 14:15:33 -0600 | [diff] [blame] | 23 | #include <linux/smp_lock.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include <linux/timer.h> |
| 25 | #include <linux/wait.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include <linux/tty.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #include <linux/netdevice.h> |
| 28 | #include <linux/ppp_defs.h> |
| 29 | #include <linux/if_ppp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | #include <linux/skbuff.h> |
| 31 | #include <linux/proc_fs.h> |
Alexey Dobriyan | 9a58a80 | 2010-01-14 03:10:54 -0800 | [diff] [blame] | 32 | #include <linux/seq_file.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | #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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | #include <linux/isdn/capiutil.h> |
| 40 | #include <linux/isdn/capicmd.h> |
Jan Kiszka | 90926f0 | 2010-02-08 10:12:06 +0000 | [diff] [blame] | 41 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | #include "capifs.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | MODULE_DESCRIPTION("CAPI4Linux: Userspace /dev/capi20 interface"); |
| 45 | MODULE_AUTHOR("Carsten Paeth"); |
| 46 | MODULE_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.de | 56b2293 | 2005-03-23 10:01:41 -0800 | [diff] [blame] | 54 | static struct class *capi_class; |
Adrian Bunk | 408b664 | 2005-05-01 08:59:29 -0700 | [diff] [blame] | 55 | static int capi_major = 68; /* allocated */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | |
| 57 | module_param_named(major, capi_major, uint, 0); |
Jan Kiszka | 501c87a | 2010-02-08 10:12:16 +0000 | [diff] [blame] | 58 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE |
Jan Kiszka | 501c87a | 2010-02-08 10:12:16 +0000 | [diff] [blame] | 60 | #define CAPINC_NR_PORTS 32 |
| 61 | #define CAPINC_MAX_PORTS 256 |
| 62 | |
| 63 | static int capi_ttymajor = 191; |
| 64 | static int capi_ttyminors = CAPINC_NR_PORTS; |
| 65 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | module_param_named(ttymajor, capi_ttymajor, uint, 0); |
| 67 | module_param_named(ttyminors, capi_ttyminors, uint, 0); |
| 68 | #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */ |
| 69 | |
| 70 | /* -------- defines ------------------------------------------------- */ |
| 71 | |
| 72 | #define CAPINC_MAX_RECVQUEUE 10 |
| 73 | #define CAPINC_MAX_SENDQUEUE 10 |
| 74 | #define CAPI_MAX_BLKSIZE 2048 |
| 75 | |
| 76 | /* -------- data structures ----------------------------------------- */ |
| 77 | |
| 78 | struct capidev; |
| 79 | struct capincci; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | struct capiminor; |
| 81 | |
Michael Buesch | 6aa6547 | 2006-06-26 00:25:30 -0700 | [diff] [blame] | 82 | struct datahandle_queue { |
| 83 | struct list_head list; |
| 84 | u16 datahandle; |
| 85 | }; |
| 86 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | struct capiminor { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | struct capincci *nccip; |
| 89 | unsigned int minor; |
Jan Kiszka | 90926f0 | 2010-02-08 10:12:06 +0000 | [diff] [blame] | 90 | struct dentry *capifs_dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | |
| 92 | struct capi20_appl *ap; |
| 93 | u32 ncci; |
| 94 | u16 datahandle; |
| 95 | u16 msgid; |
| 96 | |
| 97 | struct tty_struct *tty; |
| 98 | 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 Buesch | 6aa6547 | 2006-06-26 00:25:30 -0700 | [diff] [blame] | 109 | struct list_head ackqueue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | int nack; |
Michael Buesch | 6aa6547 | 2006-06-26 00:25:30 -0700 | [diff] [blame] | 111 | spinlock_t ackqlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | |
Michael Buesch | 053b47f | 2007-02-12 00:53:26 -0800 | [diff] [blame] | 114 | /* 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... . */ |
| 121 | static DEFINE_SPINLOCK(workaround_lock); |
| 122 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | struct capincci { |
Jan Kiszka | 884f5c4 | 2010-02-08 10:12:22 +0000 | [diff] [blame] | 124 | struct list_head list; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | u32 ncci; |
| 126 | struct capidev *cdev; |
| 127 | #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE |
| 128 | struct capiminor *minorp; |
| 129 | #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */ |
| 130 | }; |
| 131 | |
| 132 | struct 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 Kiszka | 884f5c4 | 2010-02-08 10:12:22 +0000 | [diff] [blame] | 141 | struct list_head nccis; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | |
Jan Kiszka | 05b4149 | 2010-02-08 10:12:19 +0000 | [diff] [blame] | 143 | struct mutex lock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | }; |
| 145 | |
| 146 | /* -------- global variables ---------------------------------------- */ |
| 147 | |
Jan Kiszka | b8f433d | 2010-02-08 10:12:17 +0000 | [diff] [blame] | 148 | static DEFINE_MUTEX(capidev_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | static LIST_HEAD(capidev_list); |
| 150 | |
| 151 | #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE |
Jan Kiszka | 501c87a | 2010-02-08 10:12:16 +0000 | [diff] [blame] | 152 | |
Jan Kiszka | 81d17fe | 2010-02-08 10:12:23 +0000 | [diff] [blame] | 153 | static DEFINE_RWLOCK(capiminors_lock); |
| 154 | static struct capiminor **capiminors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | /* -------- datahandles --------------------------------------------- */ |
| 157 | |
Jan Kiszka | 501c87a | 2010-02-08 10:12:16 +0000 | [diff] [blame] | 158 | static int capiminor_add_ack(struct capiminor *mp, u16 datahandle) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | { |
Michael Buesch | 6aa6547 | 2006-06-26 00:25:30 -0700 | [diff] [blame] | 160 | struct datahandle_queue *n; |
| 161 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | |
| 163 | n = kmalloc(sizeof(*n), GFP_ATOMIC); |
Michael Buesch | 6aa6547 | 2006-06-26 00:25:30 -0700 | [diff] [blame] | 164 | if (unlikely(!n)) { |
| 165 | printk(KERN_ERR "capi: alloc datahandle failed\n"); |
| 166 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | n->datahandle = datahandle; |
Michael Buesch | 6aa6547 | 2006-06-26 00:25:30 -0700 | [diff] [blame] | 169 | INIT_LIST_HEAD(&n->list); |
| 170 | spin_lock_irqsave(&mp->ackqlock, flags); |
| 171 | list_add_tail(&n->list, &mp->ackqueue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | mp->nack++; |
Michael Buesch | 6aa6547 | 2006-06-26 00:25:30 -0700 | [diff] [blame] | 173 | spin_unlock_irqrestore(&mp->ackqlock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | return 0; |
| 175 | } |
| 176 | |
| 177 | static int capiminor_del_ack(struct capiminor *mp, u16 datahandle) |
| 178 | { |
Michael Buesch | 6aa6547 | 2006-06-26 00:25:30 -0700 | [diff] [blame] | 179 | struct datahandle_queue *p, *tmp; |
| 180 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | |
Michael Buesch | 6aa6547 | 2006-06-26 00:25:30 -0700 | [diff] [blame] | 182 | spin_lock_irqsave(&mp->ackqlock, flags); |
| 183 | list_for_each_entry_safe(p, tmp, &mp->ackqueue, list) { |
| 184 | if (p->datahandle == datahandle) { |
| 185 | list_del(&p->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | kfree(p); |
| 187 | mp->nack--; |
Michael Buesch | 6aa6547 | 2006-06-26 00:25:30 -0700 | [diff] [blame] | 188 | spin_unlock_irqrestore(&mp->ackqlock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | return 0; |
| 190 | } |
| 191 | } |
Michael Buesch | 6aa6547 | 2006-06-26 00:25:30 -0700 | [diff] [blame] | 192 | spin_unlock_irqrestore(&mp->ackqlock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | return -1; |
| 194 | } |
| 195 | |
| 196 | static void capiminor_del_all_ack(struct capiminor *mp) |
| 197 | { |
Michael Buesch | 6aa6547 | 2006-06-26 00:25:30 -0700 | [diff] [blame] | 198 | struct datahandle_queue *p, *tmp; |
| 199 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | |
Michael Buesch | 6aa6547 | 2006-06-26 00:25:30 -0700 | [diff] [blame] | 201 | spin_lock_irqsave(&mp->ackqlock, flags); |
| 202 | list_for_each_entry_safe(p, tmp, &mp->ackqueue, list) { |
| 203 | list_del(&p->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | kfree(p); |
| 205 | mp->nack--; |
| 206 | } |
Michael Buesch | 6aa6547 | 2006-06-26 00:25:30 -0700 | [diff] [blame] | 207 | spin_unlock_irqrestore(&mp->ackqlock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | |
| 211 | /* -------- struct capiminor ---------------------------------------- */ |
| 212 | |
| 213 | static struct capiminor *capiminor_alloc(struct capi20_appl *ap, u32 ncci) |
| 214 | { |
Jan Kiszka | 81d17fe | 2010-02-08 10:12:23 +0000 | [diff] [blame] | 215 | struct capiminor *mp; |
| 216 | unsigned int minor; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | unsigned long flags; |
| 218 | |
Jan Kiszka | 54f0fad | 2010-02-08 10:12:20 +0000 | [diff] [blame] | 219 | mp = kzalloc(sizeof(*mp), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | if (!mp) { |
| 221 | printk(KERN_ERR "capi: can't alloc capiminor\n"); |
| 222 | return NULL; |
| 223 | } |
| 224 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | mp->ap = ap; |
| 226 | mp->ncci = ncci; |
| 227 | mp->msgid = 0; |
| 228 | atomic_set(&mp->ttyopencount,0); |
Michael Buesch | 6aa6547 | 2006-06-26 00:25:30 -0700 | [diff] [blame] | 229 | INIT_LIST_HEAD(&mp->ackqueue); |
| 230 | spin_lock_init(&mp->ackqlock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | |
| 232 | skb_queue_head_init(&mp->inqueue); |
| 233 | skb_queue_head_init(&mp->outqueue); |
| 234 | |
Jan Kiszka | 81d17fe | 2010-02-08 10:12:23 +0000 | [diff] [blame] | 235 | /* Allocate the least unused minor number. */ |
| 236 | write_lock_irqsave(&capiminors_lock, flags); |
| 237 | for (minor = 0; minor < capi_ttyminors; minor++) |
| 238 | if (!capiminors[minor]) { |
| 239 | capiminors[minor] = mp; |
| 240 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | } |
Jan Kiszka | 81d17fe | 2010-02-08 10:12:23 +0000 | [diff] [blame] | 242 | write_unlock_irqrestore(&capiminors_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | |
Jan Kiszka | 81d17fe | 2010-02-08 10:12:23 +0000 | [diff] [blame] | 244 | if (minor == capi_ttyminors) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | printk(KERN_NOTICE "capi: out of minors\n"); |
Jan Kiszka | 81d17fe | 2010-02-08 10:12:23 +0000 | [diff] [blame] | 246 | kfree(mp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | return NULL; |
| 248 | } |
| 249 | |
Jan Kiszka | 81d17fe | 2010-02-08 10:12:23 +0000 | [diff] [blame] | 250 | mp->minor = minor; |
| 251 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | return mp; |
| 253 | } |
| 254 | |
| 255 | static void capiminor_free(struct capiminor *mp) |
| 256 | { |
| 257 | unsigned long flags; |
| 258 | |
Jan Kiszka | 81d17fe | 2010-02-08 10:12:23 +0000 | [diff] [blame] | 259 | write_lock_irqsave(&capiminors_lock, flags); |
| 260 | capiminors[mp->minor] = NULL; |
| 261 | write_unlock_irqrestore(&capiminors_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | |
Wei Yongjun | d46604e | 2009-02-25 00:12:09 +0000 | [diff] [blame] | 263 | kfree_skb(mp->ttyskb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | mp->ttyskb = NULL; |
| 265 | skb_queue_purge(&mp->inqueue); |
| 266 | skb_queue_purge(&mp->outqueue); |
| 267 | capiminor_del_all_ack(mp); |
| 268 | kfree(mp); |
| 269 | } |
| 270 | |
Adrian Bunk | 408b664 | 2005-05-01 08:59:29 -0700 | [diff] [blame] | 271 | static struct capiminor *capiminor_find(unsigned int minor) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | { |
Jan Kiszka | 81d17fe | 2010-02-08 10:12:23 +0000 | [diff] [blame] | 273 | struct capiminor *mp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | |
Jan Kiszka | 81d17fe | 2010-02-08 10:12:23 +0000 | [diff] [blame] | 275 | if (minor >= capi_ttyminors) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | return NULL; |
| 277 | |
Jan Kiszka | 81d17fe | 2010-02-08 10:12:23 +0000 | [diff] [blame] | 278 | read_lock(&capiminors_lock); |
| 279 | mp = capiminors[minor]; |
| 280 | read_unlock(&capiminors_lock); |
| 281 | |
| 282 | return mp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | |
| 285 | /* -------- struct capincci ----------------------------------------- */ |
| 286 | |
Jan Kiszka | 501c87a | 2010-02-08 10:12:16 +0000 | [diff] [blame] | 287 | static void capincci_alloc_minor(struct capidev *cdev, struct capincci *np) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | { |
Jan Kiszka | 501c87a | 2010-02-08 10:12:16 +0000 | [diff] [blame] | 289 | struct capiminor *mp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | |
Jan Kiszka | 501c87a | 2010-02-08 10:12:16 +0000 | [diff] [blame] | 291 | if (!(cdev->userflags & CAPIFLAG_HIGHJACKING)) |
| 292 | return; |
| 293 | |
| 294 | mp = np->minorp = capiminor_alloc(&cdev->ap, np->ncci); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | if (mp) { |
| 296 | mp->nccip = np; |
| 297 | #ifdef _DEBUG_REFCOUNT |
| 298 | printk(KERN_DEBUG "set mp->nccip\n"); |
| 299 | #endif |
Jan Kiszka | 90926f0 | 2010-02-08 10:12:06 +0000 | [diff] [blame] | 300 | mp->capifs_dentry = |
| 301 | capifs_new_ncci(mp->minor, |
| 302 | MKDEV(capi_ttymajor, mp->minor)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | } |
Jan Kiszka | 501c87a | 2010-02-08 10:12:16 +0000 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | static void capincci_free_minor(struct capincci *np) |
| 307 | { |
| 308 | struct capiminor *mp = np->minorp; |
| 309 | |
| 310 | if (mp) { |
| 311 | capifs_free_ncci(mp->capifs_dentry); |
| 312 | if (mp->tty) { |
| 313 | mp->nccip = NULL; |
| 314 | #ifdef _DEBUG_REFCOUNT |
| 315 | printk(KERN_DEBUG "reset mp->nccip\n"); |
| 316 | #endif |
| 317 | tty_hangup(mp->tty); |
| 318 | } else { |
| 319 | capiminor_free(mp); |
| 320 | } |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | static inline unsigned int capincci_minor_opencount(struct capincci *np) |
| 325 | { |
| 326 | struct capiminor *mp = np->minorp; |
| 327 | |
| 328 | return mp ? atomic_read(&mp->ttyopencount) : 0; |
| 329 | } |
| 330 | |
| 331 | #else /* !CONFIG_ISDN_CAPI_MIDDLEWARE */ |
| 332 | |
| 333 | static inline void |
| 334 | capincci_alloc_minor(struct capidev *cdev, struct capincci *np) { } |
| 335 | static inline void capincci_free_minor(struct capincci *np) { } |
| 336 | |
| 337 | static inline unsigned int capincci_minor_opencount(struct capincci *np) |
| 338 | { |
| 339 | return 0; |
| 340 | } |
| 341 | |
| 342 | #endif /* !CONFIG_ISDN_CAPI_MIDDLEWARE */ |
| 343 | |
| 344 | static struct capincci *capincci_alloc(struct capidev *cdev, u32 ncci) |
| 345 | { |
Jan Kiszka | 884f5c4 | 2010-02-08 10:12:22 +0000 | [diff] [blame] | 346 | struct capincci *np; |
Jan Kiszka | 501c87a | 2010-02-08 10:12:16 +0000 | [diff] [blame] | 347 | |
Jan Kiszka | 54f0fad | 2010-02-08 10:12:20 +0000 | [diff] [blame] | 348 | np = kzalloc(sizeof(*np), GFP_KERNEL); |
Jan Kiszka | 501c87a | 2010-02-08 10:12:16 +0000 | [diff] [blame] | 349 | if (!np) |
| 350 | return NULL; |
| 351 | np->ncci = ncci; |
| 352 | np->cdev = cdev; |
| 353 | |
| 354 | capincci_alloc_minor(cdev, np); |
| 355 | |
Jan Kiszka | 884f5c4 | 2010-02-08 10:12:22 +0000 | [diff] [blame] | 356 | list_add_tail(&np->list, &cdev->nccis); |
| 357 | |
| 358 | return np; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | static void capincci_free(struct capidev *cdev, u32 ncci) |
| 362 | { |
Jan Kiszka | 884f5c4 | 2010-02-08 10:12:22 +0000 | [diff] [blame] | 363 | struct capincci *np, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | |
Jan Kiszka | 884f5c4 | 2010-02-08 10:12:22 +0000 | [diff] [blame] | 365 | list_for_each_entry_safe(np, tmp, &cdev->nccis, list) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | if (ncci == 0xffffffff || np->ncci == ncci) { |
Jan Kiszka | 501c87a | 2010-02-08 10:12:16 +0000 | [diff] [blame] | 367 | capincci_free_minor(np); |
Jan Kiszka | 884f5c4 | 2010-02-08 10:12:22 +0000 | [diff] [blame] | 368 | list_del(&np->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | kfree(np); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | static struct capincci *capincci_find(struct capidev *cdev, u32 ncci) |
| 374 | { |
Jan Kiszka | 884f5c4 | 2010-02-08 10:12:22 +0000 | [diff] [blame] | 375 | struct capincci *np; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | |
Jan Kiszka | 884f5c4 | 2010-02-08 10:12:22 +0000 | [diff] [blame] | 377 | list_for_each_entry(np, &cdev->nccis, list) |
| 378 | if (np->ncci == ncci) |
| 379 | return np; |
| 380 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | } |
| 382 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE |
| 384 | /* -------- handle data queue --------------------------------------- */ |
| 385 | |
| 386 | static struct sk_buff * |
| 387 | gen_data_b3_resp_for(struct capiminor *mp, struct sk_buff *skb) |
| 388 | { |
| 389 | struct sk_buff *nskb; |
| 390 | nskb = alloc_skb(CAPI_DATA_B3_RESP_LEN, GFP_ATOMIC); |
| 391 | if (nskb) { |
| 392 | u16 datahandle = CAPIMSG_U16(skb->data,CAPIMSG_BASELEN+4+4+2); |
| 393 | unsigned char *s = skb_put(nskb, CAPI_DATA_B3_RESP_LEN); |
| 394 | capimsg_setu16(s, 0, CAPI_DATA_B3_RESP_LEN); |
| 395 | capimsg_setu16(s, 2, mp->ap->applid); |
| 396 | capimsg_setu8 (s, 4, CAPI_DATA_B3); |
| 397 | capimsg_setu8 (s, 5, CAPI_RESP); |
| 398 | capimsg_setu16(s, 6, mp->msgid++); |
| 399 | capimsg_setu32(s, 8, mp->ncci); |
| 400 | capimsg_setu16(s, 12, datahandle); |
| 401 | } |
| 402 | return nskb; |
| 403 | } |
| 404 | |
| 405 | static int handle_recv_skb(struct capiminor *mp, struct sk_buff *skb) |
| 406 | { |
| 407 | struct sk_buff *nskb; |
| 408 | int datalen; |
| 409 | u16 errcode, datahandle; |
| 410 | struct tty_ldisc *ld; |
| 411 | |
| 412 | datalen = skb->len - CAPIMSG_LEN(skb->data); |
| 413 | if (mp->tty == NULL) |
| 414 | { |
| 415 | #ifdef _DEBUG_DATAFLOW |
| 416 | printk(KERN_DEBUG "capi: currently no receiver\n"); |
| 417 | #endif |
| 418 | return -1; |
| 419 | } |
| 420 | |
| 421 | ld = tty_ldisc_ref(mp->tty); |
| 422 | if (ld == NULL) |
| 423 | return -1; |
Alan Cox | a352def | 2008-07-16 21:53:12 +0100 | [diff] [blame] | 424 | if (ld->ops->receive_buf == NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS) |
| 426 | printk(KERN_DEBUG "capi: ldisc has no receive_buf function\n"); |
| 427 | #endif |
| 428 | goto bad; |
| 429 | } |
| 430 | if (mp->ttyinstop) { |
| 431 | #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS) |
| 432 | printk(KERN_DEBUG "capi: recv tty throttled\n"); |
| 433 | #endif |
| 434 | goto bad; |
| 435 | } |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 436 | if (mp->tty->receive_room < datalen) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS) |
| 438 | printk(KERN_DEBUG "capi: no room in tty\n"); |
| 439 | #endif |
| 440 | goto bad; |
| 441 | } |
Harvey Harrison | 2f9e9b6 | 2008-04-28 02:14:37 -0700 | [diff] [blame] | 442 | if ((nskb = gen_data_b3_resp_for(mp, skb)) == NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | printk(KERN_ERR "capi: gen_data_b3_resp failed\n"); |
| 444 | goto bad; |
| 445 | } |
| 446 | datahandle = CAPIMSG_U16(skb->data,CAPIMSG_BASELEN+4); |
| 447 | errcode = capi20_put_message(mp->ap, nskb); |
| 448 | if (errcode != CAPI_NOERROR) { |
| 449 | printk(KERN_ERR "capi: send DATA_B3_RESP failed=%x\n", |
| 450 | errcode); |
| 451 | kfree_skb(nskb); |
| 452 | goto bad; |
| 453 | } |
| 454 | (void)skb_pull(skb, CAPIMSG_LEN(skb->data)); |
| 455 | #ifdef _DEBUG_DATAFLOW |
| 456 | printk(KERN_DEBUG "capi: DATA_B3_RESP %u len=%d => ldisc\n", |
| 457 | datahandle, skb->len); |
| 458 | #endif |
Alan Cox | a352def | 2008-07-16 21:53:12 +0100 | [diff] [blame] | 459 | ld->ops->receive_buf(mp->tty, skb->data, NULL, skb->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | kfree_skb(skb); |
| 461 | tty_ldisc_deref(ld); |
| 462 | return 0; |
| 463 | bad: |
| 464 | tty_ldisc_deref(ld); |
| 465 | return -1; |
| 466 | } |
| 467 | |
| 468 | static void handle_minor_recv(struct capiminor *mp) |
| 469 | { |
| 470 | struct sk_buff *skb; |
Harvey Harrison | 2f9e9b6 | 2008-04-28 02:14:37 -0700 | [diff] [blame] | 471 | while ((skb = skb_dequeue(&mp->inqueue)) != NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | unsigned int len = skb->len; |
| 473 | mp->inbytes -= len; |
| 474 | if (handle_recv_skb(mp, skb) < 0) { |
| 475 | skb_queue_head(&mp->inqueue, skb); |
| 476 | mp->inbytes += len; |
| 477 | return; |
| 478 | } |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | static int handle_minor_send(struct capiminor *mp) |
| 483 | { |
| 484 | struct sk_buff *skb; |
| 485 | u16 len; |
| 486 | int count = 0; |
| 487 | u16 errcode; |
| 488 | u16 datahandle; |
| 489 | |
| 490 | if (mp->tty && mp->ttyoutstop) { |
| 491 | #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS) |
| 492 | printk(KERN_DEBUG "capi: send: tty stopped\n"); |
| 493 | #endif |
| 494 | return 0; |
| 495 | } |
| 496 | |
Harvey Harrison | 2f9e9b6 | 2008-04-28 02:14:37 -0700 | [diff] [blame] | 497 | while ((skb = skb_dequeue(&mp->outqueue)) != NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | datahandle = mp->datahandle; |
| 499 | len = (u16)skb->len; |
| 500 | skb_push(skb, CAPI_DATA_B3_REQ_LEN); |
| 501 | memset(skb->data, 0, CAPI_DATA_B3_REQ_LEN); |
| 502 | capimsg_setu16(skb->data, 0, CAPI_DATA_B3_REQ_LEN); |
| 503 | capimsg_setu16(skb->data, 2, mp->ap->applid); |
| 504 | capimsg_setu8 (skb->data, 4, CAPI_DATA_B3); |
| 505 | capimsg_setu8 (skb->data, 5, CAPI_REQ); |
| 506 | capimsg_setu16(skb->data, 6, mp->msgid++); |
| 507 | capimsg_setu32(skb->data, 8, mp->ncci); /* NCCI */ |
Andrew Morton | 5e6c20a | 2007-07-17 04:04:22 -0700 | [diff] [blame] | 508 | capimsg_setu32(skb->data, 12, (u32)(long)skb->data);/* Data32 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | capimsg_setu16(skb->data, 16, len); /* Data length */ |
| 510 | capimsg_setu16(skb->data, 18, datahandle); |
| 511 | capimsg_setu16(skb->data, 20, 0); /* Flags */ |
| 512 | |
Jan Kiszka | 501c87a | 2010-02-08 10:12:16 +0000 | [diff] [blame] | 513 | if (capiminor_add_ack(mp, datahandle) < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | skb_pull(skb, CAPI_DATA_B3_REQ_LEN); |
| 515 | skb_queue_head(&mp->outqueue, skb); |
| 516 | return count; |
| 517 | } |
| 518 | errcode = capi20_put_message(mp->ap, skb); |
| 519 | if (errcode == CAPI_NOERROR) { |
| 520 | mp->datahandle++; |
| 521 | count++; |
| 522 | mp->outbytes -= len; |
| 523 | #ifdef _DEBUG_DATAFLOW |
| 524 | printk(KERN_DEBUG "capi: DATA_B3_REQ %u len=%u\n", |
| 525 | datahandle, len); |
| 526 | #endif |
| 527 | continue; |
| 528 | } |
| 529 | capiminor_del_ack(mp, datahandle); |
| 530 | |
| 531 | if (errcode == CAPI_SENDQUEUEFULL) { |
| 532 | skb_pull(skb, CAPI_DATA_B3_REQ_LEN); |
| 533 | skb_queue_head(&mp->outqueue, skb); |
| 534 | break; |
| 535 | } |
| 536 | |
| 537 | /* ups, drop packet */ |
| 538 | printk(KERN_ERR "capi: put_message = %x\n", errcode); |
| 539 | mp->outbytes -= len; |
| 540 | kfree_skb(skb); |
| 541 | } |
| 542 | return count; |
| 543 | } |
| 544 | |
| 545 | #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */ |
| 546 | /* -------- function called by lower level -------------------------- */ |
| 547 | |
| 548 | static void capi_recv_message(struct capi20_appl *ap, struct sk_buff *skb) |
| 549 | { |
| 550 | struct capidev *cdev = ap->private; |
| 551 | #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE |
| 552 | struct capiminor *mp; |
| 553 | u16 datahandle; |
| 554 | #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */ |
| 555 | struct capincci *np; |
Michael Buesch | 053b47f | 2007-02-12 00:53:26 -0800 | [diff] [blame] | 556 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | |
Jan Kiszka | 05b4149 | 2010-02-08 10:12:19 +0000 | [diff] [blame] | 558 | mutex_lock(&cdev->lock); |
| 559 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 560 | if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_CONF) { |
| 561 | u16 info = CAPIMSG_U16(skb->data, 12); // Info field |
Jan Kiszka | 05b4149 | 2010-02-08 10:12:19 +0000 | [diff] [blame] | 562 | if ((info & 0xff00) == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | capincci_alloc(cdev, CAPIMSG_NCCI(skb->data)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | } |
Jan Kiszka | 05b4149 | 2010-02-08 10:12:19 +0000 | [diff] [blame] | 565 | if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_IND) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 566 | capincci_alloc(cdev, CAPIMSG_NCCI(skb->data)); |
Jan Kiszka | 05b4149 | 2010-02-08 10:12:19 +0000 | [diff] [blame] | 567 | |
Michael Buesch | 053b47f | 2007-02-12 00:53:26 -0800 | [diff] [blame] | 568 | spin_lock_irqsave(&workaround_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | if (CAPIMSG_COMMAND(skb->data) != CAPI_DATA_B3) { |
| 570 | skb_queue_tail(&cdev->recvqueue, skb); |
| 571 | wake_up_interruptible(&cdev->recvwait); |
Jan Kiszka | 05b4149 | 2010-02-08 10:12:19 +0000 | [diff] [blame] | 572 | goto unlock_out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 573 | } |
Jan Kiszka | 05b4149 | 2010-02-08 10:12:19 +0000 | [diff] [blame] | 574 | |
| 575 | np = capincci_find(cdev, CAPIMSG_CONTROL(skb->data)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | if (!np) { |
| 577 | printk(KERN_ERR "BUG: capi_signal: ncci not found\n"); |
| 578 | skb_queue_tail(&cdev->recvqueue, skb); |
| 579 | wake_up_interruptible(&cdev->recvwait); |
Jan Kiszka | 05b4149 | 2010-02-08 10:12:19 +0000 | [diff] [blame] | 580 | goto unlock_out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 581 | } |
Jan Kiszka | 501c87a | 2010-02-08 10:12:16 +0000 | [diff] [blame] | 582 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 583 | #ifndef CONFIG_ISDN_CAPI_MIDDLEWARE |
| 584 | skb_queue_tail(&cdev->recvqueue, skb); |
| 585 | wake_up_interruptible(&cdev->recvwait); |
Jan Kiszka | 501c87a | 2010-02-08 10:12:16 +0000 | [diff] [blame] | 586 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 587 | #else /* CONFIG_ISDN_CAPI_MIDDLEWARE */ |
Jan Kiszka | 501c87a | 2010-02-08 10:12:16 +0000 | [diff] [blame] | 588 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 589 | mp = np->minorp; |
| 590 | if (!mp) { |
| 591 | skb_queue_tail(&cdev->recvqueue, skb); |
| 592 | wake_up_interruptible(&cdev->recvwait); |
Jan Kiszka | 05b4149 | 2010-02-08 10:12:19 +0000 | [diff] [blame] | 593 | goto unlock_out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 594 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_IND) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4+4+2); |
| 597 | #ifdef _DEBUG_DATAFLOW |
| 598 | printk(KERN_DEBUG "capi_signal: DATA_B3_IND %u len=%d\n", |
| 599 | datahandle, skb->len-CAPIMSG_LEN(skb->data)); |
| 600 | #endif |
| 601 | skb_queue_tail(&mp->inqueue, skb); |
| 602 | mp->inbytes += skb->len; |
| 603 | handle_minor_recv(mp); |
| 604 | |
| 605 | } else if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_CONF) { |
| 606 | |
| 607 | datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4); |
| 608 | #ifdef _DEBUG_DATAFLOW |
| 609 | printk(KERN_DEBUG "capi_signal: DATA_B3_CONF %u 0x%x\n", |
| 610 | datahandle, |
| 611 | CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4+2)); |
| 612 | #endif |
| 613 | kfree_skb(skb); |
| 614 | (void)capiminor_del_ack(mp, datahandle); |
| 615 | if (mp->tty) |
| 616 | tty_wakeup(mp->tty); |
| 617 | (void)handle_minor_send(mp); |
| 618 | |
| 619 | } else { |
| 620 | /* ups, let capi application handle it :-) */ |
| 621 | skb_queue_tail(&cdev->recvqueue, skb); |
| 622 | wake_up_interruptible(&cdev->recvwait); |
| 623 | } |
| 624 | #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */ |
Jan Kiszka | 501c87a | 2010-02-08 10:12:16 +0000 | [diff] [blame] | 625 | |
Jan Kiszka | 05b4149 | 2010-02-08 10:12:19 +0000 | [diff] [blame] | 626 | unlock_out: |
Michael Buesch | 053b47f | 2007-02-12 00:53:26 -0800 | [diff] [blame] | 627 | spin_unlock_irqrestore(&workaround_lock, flags); |
Jan Kiszka | 05b4149 | 2010-02-08 10:12:19 +0000 | [diff] [blame] | 628 | mutex_unlock(&cdev->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | } |
| 630 | |
| 631 | /* -------- file_operations for capidev ----------------------------- */ |
| 632 | |
| 633 | static ssize_t |
| 634 | capi_read(struct file *file, char __user *buf, size_t count, loff_t *ppos) |
| 635 | { |
| 636 | struct capidev *cdev = (struct capidev *)file->private_data; |
| 637 | struct sk_buff *skb; |
| 638 | size_t copied; |
Jan Kiszka | 28a1dbb | 2010-02-08 10:12:21 +0000 | [diff] [blame] | 639 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 640 | |
| 641 | if (!cdev->ap.applid) |
| 642 | return -ENODEV; |
| 643 | |
Jan Kiszka | 28a1dbb | 2010-02-08 10:12:21 +0000 | [diff] [blame] | 644 | skb = skb_dequeue(&cdev->recvqueue); |
| 645 | if (!skb) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | if (file->f_flags & O_NONBLOCK) |
| 647 | return -EAGAIN; |
Jan Kiszka | 28a1dbb | 2010-02-08 10:12:21 +0000 | [diff] [blame] | 648 | err = wait_event_interruptible(cdev->recvwait, |
| 649 | (skb = skb_dequeue(&cdev->recvqueue))); |
| 650 | if (err) |
| 651 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 652 | } |
| 653 | if (skb->len > count) { |
| 654 | skb_queue_head(&cdev->recvqueue, skb); |
| 655 | return -EMSGSIZE; |
| 656 | } |
| 657 | if (copy_to_user(buf, skb->data, skb->len)) { |
| 658 | skb_queue_head(&cdev->recvqueue, skb); |
| 659 | return -EFAULT; |
| 660 | } |
| 661 | copied = skb->len; |
| 662 | |
| 663 | kfree_skb(skb); |
| 664 | |
| 665 | return copied; |
| 666 | } |
| 667 | |
| 668 | static ssize_t |
| 669 | capi_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos) |
| 670 | { |
| 671 | struct capidev *cdev = (struct capidev *)file->private_data; |
| 672 | struct sk_buff *skb; |
| 673 | u16 mlen; |
| 674 | |
| 675 | if (!cdev->ap.applid) |
| 676 | return -ENODEV; |
| 677 | |
| 678 | skb = alloc_skb(count, GFP_USER); |
| 679 | if (!skb) |
| 680 | return -ENOMEM; |
| 681 | |
| 682 | if (copy_from_user(skb_put(skb, count), buf, count)) { |
| 683 | kfree_skb(skb); |
| 684 | return -EFAULT; |
| 685 | } |
| 686 | mlen = CAPIMSG_LEN(skb->data); |
| 687 | if (CAPIMSG_CMD(skb->data) == CAPI_DATA_B3_REQ) { |
| 688 | if ((size_t)(mlen + CAPIMSG_DATALEN(skb->data)) != count) { |
| 689 | kfree_skb(skb); |
| 690 | return -EINVAL; |
| 691 | } |
| 692 | } else { |
| 693 | if (mlen != count) { |
| 694 | kfree_skb(skb); |
| 695 | return -EINVAL; |
| 696 | } |
| 697 | } |
| 698 | CAPIMSG_SETAPPID(skb->data, cdev->ap.applid); |
| 699 | |
| 700 | if (CAPIMSG_CMD(skb->data) == CAPI_DISCONNECT_B3_RESP) { |
Jan Kiszka | 05b4149 | 2010-02-08 10:12:19 +0000 | [diff] [blame] | 701 | mutex_lock(&cdev->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 702 | capincci_free(cdev, CAPIMSG_NCCI(skb->data)); |
Jan Kiszka | 05b4149 | 2010-02-08 10:12:19 +0000 | [diff] [blame] | 703 | mutex_unlock(&cdev->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 704 | } |
| 705 | |
| 706 | cdev->errcode = capi20_put_message(&cdev->ap, skb); |
| 707 | |
| 708 | if (cdev->errcode) { |
| 709 | kfree_skb(skb); |
| 710 | return -EIO; |
| 711 | } |
| 712 | return count; |
| 713 | } |
| 714 | |
| 715 | static unsigned int |
| 716 | capi_poll(struct file *file, poll_table * wait) |
| 717 | { |
| 718 | struct capidev *cdev = (struct capidev *)file->private_data; |
| 719 | unsigned int mask = 0; |
| 720 | |
| 721 | if (!cdev->ap.applid) |
| 722 | return POLLERR; |
| 723 | |
| 724 | poll_wait(file, &(cdev->recvwait), wait); |
| 725 | mask = POLLOUT | POLLWRNORM; |
| 726 | if (!skb_queue_empty(&cdev->recvqueue)) |
| 727 | mask |= POLLIN | POLLRDNORM; |
| 728 | return mask; |
| 729 | } |
| 730 | |
| 731 | static int |
| 732 | capi_ioctl(struct inode *inode, struct file *file, |
| 733 | unsigned int cmd, unsigned long arg) |
| 734 | { |
| 735 | struct capidev *cdev = file->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 736 | capi_ioctl_struct data; |
| 737 | int retval = -EINVAL; |
| 738 | void __user *argp = (void __user *)arg; |
| 739 | |
| 740 | switch (cmd) { |
| 741 | case CAPI_REGISTER: |
Jan Kiszka | 05b4149 | 2010-02-08 10:12:19 +0000 | [diff] [blame] | 742 | mutex_lock(&cdev->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 743 | |
Jan Kiszka | 05b4149 | 2010-02-08 10:12:19 +0000 | [diff] [blame] | 744 | if (cdev->ap.applid) { |
| 745 | retval = -EEXIST; |
| 746 | goto register_out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 747 | } |
Jan Kiszka | 05b4149 | 2010-02-08 10:12:19 +0000 | [diff] [blame] | 748 | if (copy_from_user(&cdev->ap.rparam, argp, |
| 749 | sizeof(struct capi_register_params))) { |
| 750 | retval = -EFAULT; |
| 751 | goto register_out; |
| 752 | } |
| 753 | cdev->ap.private = cdev; |
| 754 | cdev->ap.recv_message = capi_recv_message; |
| 755 | cdev->errcode = capi20_register(&cdev->ap); |
| 756 | retval = (int)cdev->ap.applid; |
| 757 | if (cdev->errcode) { |
| 758 | cdev->ap.applid = 0; |
| 759 | retval = -EIO; |
| 760 | } |
| 761 | |
| 762 | register_out: |
| 763 | mutex_unlock(&cdev->lock); |
| 764 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 765 | |
| 766 | case CAPI_GET_VERSION: |
| 767 | { |
| 768 | if (copy_from_user(&data.contr, argp, |
| 769 | sizeof(data.contr))) |
| 770 | return -EFAULT; |
| 771 | cdev->errcode = capi20_get_version(data.contr, &data.version); |
| 772 | if (cdev->errcode) |
| 773 | return -EIO; |
| 774 | if (copy_to_user(argp, &data.version, |
| 775 | sizeof(data.version))) |
| 776 | return -EFAULT; |
| 777 | } |
| 778 | return 0; |
| 779 | |
| 780 | case CAPI_GET_SERIAL: |
| 781 | { |
| 782 | if (copy_from_user(&data.contr, argp, |
| 783 | sizeof(data.contr))) |
| 784 | return -EFAULT; |
| 785 | cdev->errcode = capi20_get_serial (data.contr, data.serial); |
| 786 | if (cdev->errcode) |
| 787 | return -EIO; |
| 788 | if (copy_to_user(argp, data.serial, |
| 789 | sizeof(data.serial))) |
| 790 | return -EFAULT; |
| 791 | } |
| 792 | return 0; |
| 793 | case CAPI_GET_PROFILE: |
| 794 | { |
| 795 | if (copy_from_user(&data.contr, argp, |
| 796 | sizeof(data.contr))) |
| 797 | return -EFAULT; |
| 798 | |
| 799 | if (data.contr == 0) { |
| 800 | cdev->errcode = capi20_get_profile(data.contr, &data.profile); |
| 801 | if (cdev->errcode) |
| 802 | return -EIO; |
| 803 | |
| 804 | retval = copy_to_user(argp, |
| 805 | &data.profile.ncontroller, |
| 806 | sizeof(data.profile.ncontroller)); |
| 807 | |
| 808 | } else { |
| 809 | cdev->errcode = capi20_get_profile(data.contr, &data.profile); |
| 810 | if (cdev->errcode) |
| 811 | return -EIO; |
| 812 | |
| 813 | retval = copy_to_user(argp, &data.profile, |
| 814 | sizeof(data.profile)); |
| 815 | } |
| 816 | if (retval) |
| 817 | return -EFAULT; |
| 818 | } |
| 819 | return 0; |
| 820 | |
| 821 | case CAPI_GET_MANUFACTURER: |
| 822 | { |
| 823 | if (copy_from_user(&data.contr, argp, |
| 824 | sizeof(data.contr))) |
| 825 | return -EFAULT; |
| 826 | cdev->errcode = capi20_get_manufacturer(data.contr, data.manufacturer); |
| 827 | if (cdev->errcode) |
| 828 | return -EIO; |
| 829 | |
| 830 | if (copy_to_user(argp, data.manufacturer, |
| 831 | sizeof(data.manufacturer))) |
| 832 | return -EFAULT; |
| 833 | |
| 834 | } |
| 835 | return 0; |
| 836 | case CAPI_GET_ERRCODE: |
| 837 | data.errcode = cdev->errcode; |
| 838 | cdev->errcode = CAPI_NOERROR; |
| 839 | if (arg) { |
| 840 | if (copy_to_user(argp, &data.errcode, |
| 841 | sizeof(data.errcode))) |
| 842 | return -EFAULT; |
| 843 | } |
| 844 | return data.errcode; |
| 845 | |
| 846 | case CAPI_INSTALLED: |
| 847 | if (capi20_isinstalled() == CAPI_NOERROR) |
| 848 | return 0; |
| 849 | return -ENXIO; |
| 850 | |
| 851 | case CAPI_MANUFACTURER_CMD: |
| 852 | { |
| 853 | struct capi_manufacturer_cmd mcmd; |
| 854 | if (!capable(CAP_SYS_ADMIN)) |
| 855 | return -EPERM; |
| 856 | if (copy_from_user(&mcmd, argp, sizeof(mcmd))) |
| 857 | return -EFAULT; |
| 858 | return capi20_manufacturer(mcmd.cmd, mcmd.data); |
| 859 | } |
| 860 | return 0; |
| 861 | |
| 862 | case CAPI_SET_FLAGS: |
Jan Kiszka | 05b4149 | 2010-02-08 10:12:19 +0000 | [diff] [blame] | 863 | case CAPI_CLR_FLAGS: { |
| 864 | unsigned userflags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 865 | |
Jan Kiszka | 05b4149 | 2010-02-08 10:12:19 +0000 | [diff] [blame] | 866 | if (copy_from_user(&userflags, argp, sizeof(userflags))) |
| 867 | return -EFAULT; |
| 868 | |
| 869 | mutex_lock(&cdev->lock); |
| 870 | if (cmd == CAPI_SET_FLAGS) |
| 871 | cdev->userflags |= userflags; |
| 872 | else |
| 873 | cdev->userflags &= ~userflags; |
| 874 | mutex_unlock(&cdev->lock); |
| 875 | return 0; |
| 876 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 877 | case CAPI_GET_FLAGS: |
| 878 | if (copy_to_user(argp, &cdev->userflags, |
| 879 | sizeof(cdev->userflags))) |
| 880 | return -EFAULT; |
| 881 | return 0; |
| 882 | |
Jan Kiszka | 05b4149 | 2010-02-08 10:12:19 +0000 | [diff] [blame] | 883 | case CAPI_NCCI_OPENCOUNT: { |
| 884 | struct capincci *nccip; |
| 885 | unsigned ncci; |
| 886 | int count = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 887 | |
Jan Kiszka | 05b4149 | 2010-02-08 10:12:19 +0000 | [diff] [blame] | 888 | if (copy_from_user(&ncci, argp, sizeof(ncci))) |
| 889 | return -EFAULT; |
| 890 | |
| 891 | mutex_lock(&cdev->lock); |
| 892 | nccip = capincci_find(cdev, (u32)ncci); |
| 893 | if (nccip) |
| 894 | count = capincci_minor_opencount(nccip); |
| 895 | mutex_unlock(&cdev->lock); |
| 896 | return count; |
| 897 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 898 | |
| 899 | #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE |
Jan Kiszka | 05b4149 | 2010-02-08 10:12:19 +0000 | [diff] [blame] | 900 | case CAPI_NCCI_GETUNIT: { |
| 901 | struct capincci *nccip; |
| 902 | struct capiminor *mp; |
| 903 | unsigned ncci; |
| 904 | int unit = -ESRCH; |
| 905 | |
| 906 | if (copy_from_user(&ncci, argp, sizeof(ncci))) |
| 907 | return -EFAULT; |
| 908 | |
| 909 | mutex_lock(&cdev->lock); |
| 910 | nccip = capincci_find(cdev, (u32)ncci); |
| 911 | if (nccip) { |
| 912 | mp = nccip->minorp; |
| 913 | if (mp) |
| 914 | unit = mp->minor; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 915 | } |
Jan Kiszka | 05b4149 | 2010-02-08 10:12:19 +0000 | [diff] [blame] | 916 | mutex_unlock(&cdev->lock); |
| 917 | return unit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 918 | } |
Jan Kiszka | 05b4149 | 2010-02-08 10:12:19 +0000 | [diff] [blame] | 919 | #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */ |
| 920 | |
| 921 | default: |
| 922 | return -EINVAL; |
| 923 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 924 | } |
| 925 | |
Jan Kiszka | eca39dd | 2010-02-08 10:12:18 +0000 | [diff] [blame] | 926 | static int capi_open(struct inode *inode, struct file *file) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 927 | { |
Jan Kiszka | eca39dd | 2010-02-08 10:12:18 +0000 | [diff] [blame] | 928 | struct capidev *cdev; |
| 929 | |
| 930 | cdev = kzalloc(sizeof(*cdev), GFP_KERNEL); |
| 931 | if (!cdev) |
| 932 | return -ENOMEM; |
| 933 | |
Jan Kiszka | 05b4149 | 2010-02-08 10:12:19 +0000 | [diff] [blame] | 934 | mutex_init(&cdev->lock); |
Jan Kiszka | eca39dd | 2010-02-08 10:12:18 +0000 | [diff] [blame] | 935 | skb_queue_head_init(&cdev->recvqueue); |
| 936 | init_waitqueue_head(&cdev->recvwait); |
Jan Kiszka | 884f5c4 | 2010-02-08 10:12:22 +0000 | [diff] [blame] | 937 | INIT_LIST_HEAD(&cdev->nccis); |
Jan Kiszka | eca39dd | 2010-02-08 10:12:18 +0000 | [diff] [blame] | 938 | file->private_data = cdev; |
| 939 | |
| 940 | mutex_lock(&capidev_list_lock); |
| 941 | list_add_tail(&cdev->list, &capidev_list); |
| 942 | mutex_unlock(&capidev_list_lock); |
| 943 | |
| 944 | return nonseekable_open(inode, file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 945 | } |
| 946 | |
Jan Kiszka | eca39dd | 2010-02-08 10:12:18 +0000 | [diff] [blame] | 947 | static int capi_release(struct inode *inode, struct file *file) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 948 | { |
Jan Kiszka | eca39dd | 2010-02-08 10:12:18 +0000 | [diff] [blame] | 949 | struct capidev *cdev = file->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 950 | |
Jan Kiszka | eca39dd | 2010-02-08 10:12:18 +0000 | [diff] [blame] | 951 | mutex_lock(&capidev_list_lock); |
| 952 | list_del(&cdev->list); |
| 953 | mutex_unlock(&capidev_list_lock); |
| 954 | |
Jan Kiszka | 05b4149 | 2010-02-08 10:12:19 +0000 | [diff] [blame] | 955 | if (cdev->ap.applid) |
Jan Kiszka | eca39dd | 2010-02-08 10:12:18 +0000 | [diff] [blame] | 956 | capi20_release(&cdev->ap); |
Jan Kiszka | eca39dd | 2010-02-08 10:12:18 +0000 | [diff] [blame] | 957 | skb_queue_purge(&cdev->recvqueue); |
Jan Kiszka | eca39dd | 2010-02-08 10:12:18 +0000 | [diff] [blame] | 958 | capincci_free(cdev, 0xffffffff); |
Jan Kiszka | eca39dd | 2010-02-08 10:12:18 +0000 | [diff] [blame] | 959 | |
| 960 | kfree(cdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 961 | return 0; |
| 962 | } |
| 963 | |
Arjan van de Ven | 2b8693c | 2007-02-12 00:55:32 -0800 | [diff] [blame] | 964 | static const struct file_operations capi_fops = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 965 | { |
| 966 | .owner = THIS_MODULE, |
| 967 | .llseek = no_llseek, |
| 968 | .read = capi_read, |
| 969 | .write = capi_write, |
| 970 | .poll = capi_poll, |
| 971 | .ioctl = capi_ioctl, |
| 972 | .open = capi_open, |
| 973 | .release = capi_release, |
| 974 | }; |
| 975 | |
| 976 | #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE |
| 977 | /* -------- tty_operations for capincci ----------------------------- */ |
| 978 | |
| 979 | static int capinc_tty_open(struct tty_struct * tty, struct file * file) |
| 980 | { |
| 981 | struct capiminor *mp; |
Michael Buesch | 053b47f | 2007-02-12 00:53:26 -0800 | [diff] [blame] | 982 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 983 | |
Harvey Harrison | 2f9e9b6 | 2008-04-28 02:14:37 -0700 | [diff] [blame] | 984 | if ((mp = capiminor_find(iminor(file->f_path.dentry->d_inode))) == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 985 | return -ENXIO; |
Harvey Harrison | 2f9e9b6 | 2008-04-28 02:14:37 -0700 | [diff] [blame] | 986 | if (mp->nccip == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 987 | return -ENXIO; |
| 988 | |
| 989 | tty->driver_data = (void *)mp; |
| 990 | |
Michael Buesch | 053b47f | 2007-02-12 00:53:26 -0800 | [diff] [blame] | 991 | spin_lock_irqsave(&workaround_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 992 | if (atomic_read(&mp->ttyopencount) == 0) |
| 993 | mp->tty = tty; |
| 994 | atomic_inc(&mp->ttyopencount); |
| 995 | #ifdef _DEBUG_REFCOUNT |
| 996 | printk(KERN_DEBUG "capinc_tty_open ocount=%d\n", atomic_read(&mp->ttyopencount)); |
| 997 | #endif |
| 998 | handle_minor_recv(mp); |
Michael Buesch | 053b47f | 2007-02-12 00:53:26 -0800 | [diff] [blame] | 999 | spin_unlock_irqrestore(&workaround_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1000 | return 0; |
| 1001 | } |
| 1002 | |
| 1003 | static void capinc_tty_close(struct tty_struct * tty, struct file * file) |
| 1004 | { |
| 1005 | struct capiminor *mp; |
| 1006 | |
| 1007 | mp = (struct capiminor *)tty->driver_data; |
| 1008 | if (mp) { |
| 1009 | if (atomic_dec_and_test(&mp->ttyopencount)) { |
| 1010 | #ifdef _DEBUG_REFCOUNT |
| 1011 | printk(KERN_DEBUG "capinc_tty_close lastclose\n"); |
| 1012 | #endif |
| 1013 | tty->driver_data = NULL; |
| 1014 | mp->tty = NULL; |
| 1015 | } |
| 1016 | #ifdef _DEBUG_REFCOUNT |
| 1017 | printk(KERN_DEBUG "capinc_tty_close ocount=%d\n", atomic_read(&mp->ttyopencount)); |
| 1018 | #endif |
Harvey Harrison | 2f9e9b6 | 2008-04-28 02:14:37 -0700 | [diff] [blame] | 1019 | if (mp->nccip == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1020 | capiminor_free(mp); |
| 1021 | } |
| 1022 | |
| 1023 | #ifdef _DEBUG_REFCOUNT |
| 1024 | printk(KERN_DEBUG "capinc_tty_close\n"); |
| 1025 | #endif |
| 1026 | } |
| 1027 | |
| 1028 | static int capinc_tty_write(struct tty_struct * tty, |
| 1029 | const unsigned char *buf, int count) |
| 1030 | { |
| 1031 | struct capiminor *mp = (struct capiminor *)tty->driver_data; |
| 1032 | struct sk_buff *skb; |
Michael Buesch | 053b47f | 2007-02-12 00:53:26 -0800 | [diff] [blame] | 1033 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1034 | |
| 1035 | #ifdef _DEBUG_TTYFUNCS |
| 1036 | printk(KERN_DEBUG "capinc_tty_write(count=%d)\n", count); |
| 1037 | #endif |
| 1038 | |
| 1039 | if (!mp || !mp->nccip) { |
| 1040 | #ifdef _DEBUG_TTYFUNCS |
| 1041 | printk(KERN_DEBUG "capinc_tty_write: mp or mp->ncci NULL\n"); |
| 1042 | #endif |
| 1043 | return 0; |
| 1044 | } |
| 1045 | |
Michael Buesch | 053b47f | 2007-02-12 00:53:26 -0800 | [diff] [blame] | 1046 | spin_lock_irqsave(&workaround_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1047 | skb = mp->ttyskb; |
| 1048 | if (skb) { |
| 1049 | mp->ttyskb = NULL; |
| 1050 | skb_queue_tail(&mp->outqueue, skb); |
| 1051 | mp->outbytes += skb->len; |
| 1052 | } |
| 1053 | |
| 1054 | skb = alloc_skb(CAPI_DATA_B3_REQ_LEN+count, GFP_ATOMIC); |
| 1055 | if (!skb) { |
| 1056 | printk(KERN_ERR "capinc_tty_write: alloc_skb failed\n"); |
Michael Buesch | 053b47f | 2007-02-12 00:53:26 -0800 | [diff] [blame] | 1057 | spin_unlock_irqrestore(&workaround_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1058 | return -ENOMEM; |
| 1059 | } |
| 1060 | |
| 1061 | skb_reserve(skb, CAPI_DATA_B3_REQ_LEN); |
| 1062 | memcpy(skb_put(skb, count), buf, count); |
| 1063 | |
| 1064 | skb_queue_tail(&mp->outqueue, skb); |
| 1065 | mp->outbytes += skb->len; |
| 1066 | (void)handle_minor_send(mp); |
| 1067 | (void)handle_minor_recv(mp); |
Michael Buesch | 053b47f | 2007-02-12 00:53:26 -0800 | [diff] [blame] | 1068 | spin_unlock_irqrestore(&workaround_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1069 | return count; |
| 1070 | } |
| 1071 | |
Alan Cox | f2545a7 | 2008-04-30 00:54:09 -0700 | [diff] [blame] | 1072 | static int capinc_tty_put_char(struct tty_struct *tty, unsigned char ch) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1073 | { |
| 1074 | struct capiminor *mp = (struct capiminor *)tty->driver_data; |
| 1075 | struct sk_buff *skb; |
Michael Buesch | 053b47f | 2007-02-12 00:53:26 -0800 | [diff] [blame] | 1076 | unsigned long flags; |
Alan Cox | f2545a7 | 2008-04-30 00:54:09 -0700 | [diff] [blame] | 1077 | int ret = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1078 | |
| 1079 | #ifdef _DEBUG_TTYFUNCS |
| 1080 | printk(KERN_DEBUG "capinc_put_char(%u)\n", ch); |
| 1081 | #endif |
| 1082 | |
| 1083 | if (!mp || !mp->nccip) { |
| 1084 | #ifdef _DEBUG_TTYFUNCS |
| 1085 | printk(KERN_DEBUG "capinc_tty_put_char: mp or mp->ncci NULL\n"); |
| 1086 | #endif |
Alan Cox | f2545a7 | 2008-04-30 00:54:09 -0700 | [diff] [blame] | 1087 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1088 | } |
| 1089 | |
Michael Buesch | 053b47f | 2007-02-12 00:53:26 -0800 | [diff] [blame] | 1090 | spin_lock_irqsave(&workaround_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1091 | skb = mp->ttyskb; |
| 1092 | if (skb) { |
| 1093 | if (skb_tailroom(skb) > 0) { |
| 1094 | *(skb_put(skb, 1)) = ch; |
Michael Buesch | 053b47f | 2007-02-12 00:53:26 -0800 | [diff] [blame] | 1095 | spin_unlock_irqrestore(&workaround_lock, flags); |
Alan Cox | f2545a7 | 2008-04-30 00:54:09 -0700 | [diff] [blame] | 1096 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1097 | } |
| 1098 | mp->ttyskb = NULL; |
| 1099 | skb_queue_tail(&mp->outqueue, skb); |
| 1100 | mp->outbytes += skb->len; |
| 1101 | (void)handle_minor_send(mp); |
| 1102 | } |
| 1103 | skb = alloc_skb(CAPI_DATA_B3_REQ_LEN+CAPI_MAX_BLKSIZE, GFP_ATOMIC); |
| 1104 | if (skb) { |
| 1105 | skb_reserve(skb, CAPI_DATA_B3_REQ_LEN); |
| 1106 | *(skb_put(skb, 1)) = ch; |
| 1107 | mp->ttyskb = skb; |
| 1108 | } else { |
| 1109 | printk(KERN_ERR "capinc_put_char: char %u lost\n", ch); |
Alan Cox | f2545a7 | 2008-04-30 00:54:09 -0700 | [diff] [blame] | 1110 | ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1111 | } |
Michael Buesch | 053b47f | 2007-02-12 00:53:26 -0800 | [diff] [blame] | 1112 | spin_unlock_irqrestore(&workaround_lock, flags); |
Alan Cox | f2545a7 | 2008-04-30 00:54:09 -0700 | [diff] [blame] | 1113 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1114 | } |
| 1115 | |
| 1116 | static void capinc_tty_flush_chars(struct tty_struct *tty) |
| 1117 | { |
| 1118 | struct capiminor *mp = (struct capiminor *)tty->driver_data; |
| 1119 | struct sk_buff *skb; |
Michael Buesch | 053b47f | 2007-02-12 00:53:26 -0800 | [diff] [blame] | 1120 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1121 | |
| 1122 | #ifdef _DEBUG_TTYFUNCS |
| 1123 | printk(KERN_DEBUG "capinc_tty_flush_chars\n"); |
| 1124 | #endif |
| 1125 | |
| 1126 | if (!mp || !mp->nccip) { |
| 1127 | #ifdef _DEBUG_TTYFUNCS |
| 1128 | printk(KERN_DEBUG "capinc_tty_flush_chars: mp or mp->ncci NULL\n"); |
| 1129 | #endif |
| 1130 | return; |
| 1131 | } |
| 1132 | |
Michael Buesch | 053b47f | 2007-02-12 00:53:26 -0800 | [diff] [blame] | 1133 | spin_lock_irqsave(&workaround_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1134 | skb = mp->ttyskb; |
| 1135 | if (skb) { |
| 1136 | mp->ttyskb = NULL; |
| 1137 | skb_queue_tail(&mp->outqueue, skb); |
| 1138 | mp->outbytes += skb->len; |
| 1139 | (void)handle_minor_send(mp); |
| 1140 | } |
| 1141 | (void)handle_minor_recv(mp); |
Michael Buesch | 053b47f | 2007-02-12 00:53:26 -0800 | [diff] [blame] | 1142 | spin_unlock_irqrestore(&workaround_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1143 | } |
| 1144 | |
| 1145 | static int capinc_tty_write_room(struct tty_struct *tty) |
| 1146 | { |
| 1147 | struct capiminor *mp = (struct capiminor *)tty->driver_data; |
| 1148 | int room; |
| 1149 | if (!mp || !mp->nccip) { |
| 1150 | #ifdef _DEBUG_TTYFUNCS |
| 1151 | printk(KERN_DEBUG "capinc_tty_write_room: mp or mp->ncci NULL\n"); |
| 1152 | #endif |
| 1153 | return 0; |
| 1154 | } |
| 1155 | room = CAPINC_MAX_SENDQUEUE-skb_queue_len(&mp->outqueue); |
| 1156 | room *= CAPI_MAX_BLKSIZE; |
| 1157 | #ifdef _DEBUG_TTYFUNCS |
| 1158 | printk(KERN_DEBUG "capinc_tty_write_room = %d\n", room); |
| 1159 | #endif |
| 1160 | return room; |
| 1161 | } |
| 1162 | |
Adrian Bunk | 408b664 | 2005-05-01 08:59:29 -0700 | [diff] [blame] | 1163 | static int capinc_tty_chars_in_buffer(struct tty_struct *tty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1164 | { |
| 1165 | struct capiminor *mp = (struct capiminor *)tty->driver_data; |
| 1166 | if (!mp || !mp->nccip) { |
| 1167 | #ifdef _DEBUG_TTYFUNCS |
| 1168 | printk(KERN_DEBUG "capinc_tty_chars_in_buffer: mp or mp->ncci NULL\n"); |
| 1169 | #endif |
| 1170 | return 0; |
| 1171 | } |
| 1172 | #ifdef _DEBUG_TTYFUNCS |
| 1173 | printk(KERN_DEBUG "capinc_tty_chars_in_buffer = %d nack=%d sq=%d rq=%d\n", |
| 1174 | mp->outbytes, mp->nack, |
| 1175 | skb_queue_len(&mp->outqueue), |
| 1176 | skb_queue_len(&mp->inqueue)); |
| 1177 | #endif |
| 1178 | return mp->outbytes; |
| 1179 | } |
| 1180 | |
| 1181 | static int capinc_tty_ioctl(struct tty_struct *tty, struct file * file, |
| 1182 | unsigned int cmd, unsigned long arg) |
| 1183 | { |
| 1184 | int error = 0; |
| 1185 | switch (cmd) { |
| 1186 | default: |
Stephen Rothwell | 53e8631 | 2008-10-13 10:44:33 +0100 | [diff] [blame] | 1187 | error = n_tty_ioctl_helper(tty, file, cmd, arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1188 | break; |
| 1189 | } |
| 1190 | return error; |
| 1191 | } |
| 1192 | |
Alan Cox | 606d099 | 2006-12-08 02:38:45 -0800 | [diff] [blame] | 1193 | static void capinc_tty_set_termios(struct tty_struct *tty, struct ktermios * old) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1194 | { |
| 1195 | #ifdef _DEBUG_TTYFUNCS |
| 1196 | printk(KERN_DEBUG "capinc_tty_set_termios\n"); |
| 1197 | #endif |
| 1198 | } |
| 1199 | |
| 1200 | static void capinc_tty_throttle(struct tty_struct * tty) |
| 1201 | { |
| 1202 | struct capiminor *mp = (struct capiminor *)tty->driver_data; |
| 1203 | #ifdef _DEBUG_TTYFUNCS |
| 1204 | printk(KERN_DEBUG "capinc_tty_throttle\n"); |
| 1205 | #endif |
| 1206 | if (mp) |
| 1207 | mp->ttyinstop = 1; |
| 1208 | } |
| 1209 | |
| 1210 | static void capinc_tty_unthrottle(struct tty_struct * tty) |
| 1211 | { |
| 1212 | struct capiminor *mp = (struct capiminor *)tty->driver_data; |
Michael Buesch | 053b47f | 2007-02-12 00:53:26 -0800 | [diff] [blame] | 1213 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1214 | #ifdef _DEBUG_TTYFUNCS |
| 1215 | printk(KERN_DEBUG "capinc_tty_unthrottle\n"); |
| 1216 | #endif |
| 1217 | if (mp) { |
Michael Buesch | 053b47f | 2007-02-12 00:53:26 -0800 | [diff] [blame] | 1218 | spin_lock_irqsave(&workaround_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1219 | mp->ttyinstop = 0; |
| 1220 | handle_minor_recv(mp); |
Michael Buesch | 053b47f | 2007-02-12 00:53:26 -0800 | [diff] [blame] | 1221 | spin_unlock_irqrestore(&workaround_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1222 | } |
| 1223 | } |
| 1224 | |
| 1225 | static void capinc_tty_stop(struct tty_struct *tty) |
| 1226 | { |
| 1227 | struct capiminor *mp = (struct capiminor *)tty->driver_data; |
| 1228 | #ifdef _DEBUG_TTYFUNCS |
| 1229 | printk(KERN_DEBUG "capinc_tty_stop\n"); |
| 1230 | #endif |
| 1231 | if (mp) { |
| 1232 | mp->ttyoutstop = 1; |
| 1233 | } |
| 1234 | } |
| 1235 | |
| 1236 | static void capinc_tty_start(struct tty_struct *tty) |
| 1237 | { |
| 1238 | struct capiminor *mp = (struct capiminor *)tty->driver_data; |
Michael Buesch | 053b47f | 2007-02-12 00:53:26 -0800 | [diff] [blame] | 1239 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1240 | #ifdef _DEBUG_TTYFUNCS |
| 1241 | printk(KERN_DEBUG "capinc_tty_start\n"); |
| 1242 | #endif |
| 1243 | if (mp) { |
Michael Buesch | 053b47f | 2007-02-12 00:53:26 -0800 | [diff] [blame] | 1244 | spin_lock_irqsave(&workaround_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1245 | mp->ttyoutstop = 0; |
| 1246 | (void)handle_minor_send(mp); |
Michael Buesch | 053b47f | 2007-02-12 00:53:26 -0800 | [diff] [blame] | 1247 | spin_unlock_irqrestore(&workaround_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1248 | } |
| 1249 | } |
| 1250 | |
| 1251 | static void capinc_tty_hangup(struct tty_struct *tty) |
| 1252 | { |
| 1253 | #ifdef _DEBUG_TTYFUNCS |
| 1254 | printk(KERN_DEBUG "capinc_tty_hangup\n"); |
| 1255 | #endif |
| 1256 | } |
| 1257 | |
Alan Cox | 9e98966c | 2008-07-22 11:18:03 +0100 | [diff] [blame] | 1258 | static int capinc_tty_break_ctl(struct tty_struct *tty, int state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1259 | { |
| 1260 | #ifdef _DEBUG_TTYFUNCS |
| 1261 | printk(KERN_DEBUG "capinc_tty_break_ctl(%d)\n", state); |
| 1262 | #endif |
Alan Cox | 9e98966c | 2008-07-22 11:18:03 +0100 | [diff] [blame] | 1263 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1264 | } |
| 1265 | |
| 1266 | static void capinc_tty_flush_buffer(struct tty_struct *tty) |
| 1267 | { |
| 1268 | #ifdef _DEBUG_TTYFUNCS |
| 1269 | printk(KERN_DEBUG "capinc_tty_flush_buffer\n"); |
| 1270 | #endif |
| 1271 | } |
| 1272 | |
| 1273 | static void capinc_tty_set_ldisc(struct tty_struct *tty) |
| 1274 | { |
| 1275 | #ifdef _DEBUG_TTYFUNCS |
| 1276 | printk(KERN_DEBUG "capinc_tty_set_ldisc\n"); |
| 1277 | #endif |
| 1278 | } |
| 1279 | |
| 1280 | static void capinc_tty_send_xchar(struct tty_struct *tty, char ch) |
| 1281 | { |
| 1282 | #ifdef _DEBUG_TTYFUNCS |
| 1283 | printk(KERN_DEBUG "capinc_tty_send_xchar(%d)\n", ch); |
| 1284 | #endif |
| 1285 | } |
| 1286 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1287 | static struct tty_driver *capinc_tty_driver; |
| 1288 | |
Jeff Dike | b68e31d | 2006-10-02 02:17:18 -0700 | [diff] [blame] | 1289 | static const struct tty_operations capinc_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1290 | .open = capinc_tty_open, |
| 1291 | .close = capinc_tty_close, |
| 1292 | .write = capinc_tty_write, |
| 1293 | .put_char = capinc_tty_put_char, |
| 1294 | .flush_chars = capinc_tty_flush_chars, |
| 1295 | .write_room = capinc_tty_write_room, |
| 1296 | .chars_in_buffer = capinc_tty_chars_in_buffer, |
| 1297 | .ioctl = capinc_tty_ioctl, |
| 1298 | .set_termios = capinc_tty_set_termios, |
| 1299 | .throttle = capinc_tty_throttle, |
| 1300 | .unthrottle = capinc_tty_unthrottle, |
| 1301 | .stop = capinc_tty_stop, |
| 1302 | .start = capinc_tty_start, |
| 1303 | .hangup = capinc_tty_hangup, |
| 1304 | .break_ctl = capinc_tty_break_ctl, |
| 1305 | .flush_buffer = capinc_tty_flush_buffer, |
| 1306 | .set_ldisc = capinc_tty_set_ldisc, |
| 1307 | .send_xchar = capinc_tty_send_xchar, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1308 | }; |
| 1309 | |
Jan Kiszka | e76b154 | 2010-02-08 10:12:24 +0000 | [diff] [blame^] | 1310 | static int __init capinc_tty_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1311 | { |
| 1312 | struct tty_driver *drv; |
Jan Kiszka | e76b154 | 2010-02-08 10:12:24 +0000 | [diff] [blame^] | 1313 | int err; |
| 1314 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1315 | if (capi_ttyminors > CAPINC_MAX_PORTS) |
| 1316 | capi_ttyminors = CAPINC_MAX_PORTS; |
| 1317 | if (capi_ttyminors <= 0) |
| 1318 | capi_ttyminors = CAPINC_NR_PORTS; |
| 1319 | |
Jan Kiszka | 81d17fe | 2010-02-08 10:12:23 +0000 | [diff] [blame] | 1320 | capiminors = kzalloc(sizeof(struct capi_minor *) * capi_ttyminors, |
| 1321 | GFP_KERNEL); |
| 1322 | if (!capiminors) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1323 | return -ENOMEM; |
| 1324 | |
Jan Kiszka | 81d17fe | 2010-02-08 10:12:23 +0000 | [diff] [blame] | 1325 | drv = alloc_tty_driver(capi_ttyminors); |
| 1326 | if (!drv) { |
| 1327 | kfree(capiminors); |
| 1328 | return -ENOMEM; |
| 1329 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1330 | drv->owner = THIS_MODULE; |
| 1331 | drv->driver_name = "capi_nc"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1332 | drv->name = "capi"; |
| 1333 | drv->major = capi_ttymajor; |
| 1334 | drv->minor_start = 0; |
| 1335 | drv->type = TTY_DRIVER_TYPE_SERIAL; |
| 1336 | drv->subtype = SERIAL_TYPE_NORMAL; |
| 1337 | drv->init_termios = tty_std_termios; |
| 1338 | drv->init_termios.c_iflag = ICRNL; |
| 1339 | drv->init_termios.c_oflag = OPOST | ONLCR; |
| 1340 | drv->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL; |
| 1341 | drv->init_termios.c_lflag = 0; |
| 1342 | drv->flags = TTY_DRIVER_REAL_RAW|TTY_DRIVER_RESET_TERMIOS; |
| 1343 | tty_set_operations(drv, &capinc_ops); |
Jan Kiszka | e76b154 | 2010-02-08 10:12:24 +0000 | [diff] [blame^] | 1344 | |
| 1345 | err = tty_register_driver(drv); |
| 1346 | if (err) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1347 | put_tty_driver(drv); |
Jan Kiszka | 81d17fe | 2010-02-08 10:12:23 +0000 | [diff] [blame] | 1348 | kfree(capiminors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1349 | printk(KERN_ERR "Couldn't register capi_nc driver\n"); |
Jan Kiszka | e76b154 | 2010-02-08 10:12:24 +0000 | [diff] [blame^] | 1350 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1351 | } |
| 1352 | capinc_tty_driver = drv; |
| 1353 | return 0; |
| 1354 | } |
| 1355 | |
Jan Kiszka | e76b154 | 2010-02-08 10:12:24 +0000 | [diff] [blame^] | 1356 | static void __exit capinc_tty_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1357 | { |
Jan Kiszka | e76b154 | 2010-02-08 10:12:24 +0000 | [diff] [blame^] | 1358 | tty_unregister_driver(capinc_tty_driver); |
| 1359 | put_tty_driver(capinc_tty_driver); |
Jan Kiszka | 81d17fe | 2010-02-08 10:12:23 +0000 | [diff] [blame] | 1360 | kfree(capiminors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1361 | } |
| 1362 | |
Jan Kiszka | 501c87a | 2010-02-08 10:12:16 +0000 | [diff] [blame] | 1363 | #else /* !CONFIG_ISDN_CAPI_MIDDLEWARE */ |
| 1364 | |
| 1365 | static inline int capinc_tty_init(void) |
| 1366 | { |
| 1367 | return 0; |
| 1368 | } |
| 1369 | |
| 1370 | static inline void capinc_tty_exit(void) { } |
| 1371 | |
| 1372 | #endif /* !CONFIG_ISDN_CAPI_MIDDLEWARE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1373 | |
| 1374 | /* -------- /proc functions ----------------------------------------- */ |
| 1375 | |
| 1376 | /* |
| 1377 | * /proc/capi/capi20: |
| 1378 | * minor applid nrecvctlpkt nrecvdatapkt nsendctlpkt nsenddatapkt |
| 1379 | */ |
Alexey Dobriyan | 9a58a80 | 2010-01-14 03:10:54 -0800 | [diff] [blame] | 1380 | static int capi20_proc_show(struct seq_file *m, void *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1381 | { |
| 1382 | struct capidev *cdev; |
| 1383 | struct list_head *l; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1384 | |
Jan Kiszka | b8f433d | 2010-02-08 10:12:17 +0000 | [diff] [blame] | 1385 | mutex_lock(&capidev_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1386 | list_for_each(l, &capidev_list) { |
| 1387 | cdev = list_entry(l, struct capidev, list); |
Alexey Dobriyan | 9a58a80 | 2010-01-14 03:10:54 -0800 | [diff] [blame] | 1388 | seq_printf(m, "0 %d %lu %lu %lu %lu\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1389 | cdev->ap.applid, |
| 1390 | cdev->ap.nrecvctlpkt, |
| 1391 | cdev->ap.nrecvdatapkt, |
| 1392 | cdev->ap.nsentctlpkt, |
| 1393 | cdev->ap.nsentdatapkt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1394 | } |
Jan Kiszka | b8f433d | 2010-02-08 10:12:17 +0000 | [diff] [blame] | 1395 | mutex_unlock(&capidev_list_lock); |
Alexey Dobriyan | 9a58a80 | 2010-01-14 03:10:54 -0800 | [diff] [blame] | 1396 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1397 | } |
| 1398 | |
Alexey Dobriyan | 9a58a80 | 2010-01-14 03:10:54 -0800 | [diff] [blame] | 1399 | static int capi20_proc_open(struct inode *inode, struct file *file) |
| 1400 | { |
| 1401 | return single_open(file, capi20_proc_show, NULL); |
| 1402 | } |
| 1403 | |
| 1404 | static const struct file_operations capi20_proc_fops = { |
| 1405 | .owner = THIS_MODULE, |
| 1406 | .open = capi20_proc_open, |
| 1407 | .read = seq_read, |
| 1408 | .llseek = seq_lseek, |
| 1409 | .release = single_release, |
| 1410 | }; |
| 1411 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1412 | /* |
| 1413 | * /proc/capi/capi20ncci: |
| 1414 | * applid ncci |
| 1415 | */ |
Alexey Dobriyan | 9a58a80 | 2010-01-14 03:10:54 -0800 | [diff] [blame] | 1416 | static int capi20ncci_proc_show(struct seq_file *m, void *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1417 | { |
Jan Kiszka | 884f5c4 | 2010-02-08 10:12:22 +0000 | [diff] [blame] | 1418 | struct capidev *cdev; |
| 1419 | struct capincci *np; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1420 | |
Jan Kiszka | b8f433d | 2010-02-08 10:12:17 +0000 | [diff] [blame] | 1421 | mutex_lock(&capidev_list_lock); |
Jan Kiszka | 884f5c4 | 2010-02-08 10:12:22 +0000 | [diff] [blame] | 1422 | list_for_each_entry(cdev, &capidev_list, list) { |
Jan Kiszka | 05b4149 | 2010-02-08 10:12:19 +0000 | [diff] [blame] | 1423 | mutex_lock(&cdev->lock); |
Jan Kiszka | 884f5c4 | 2010-02-08 10:12:22 +0000 | [diff] [blame] | 1424 | list_for_each_entry(np, &cdev->nccis, list) |
| 1425 | seq_printf(m, "%d 0x%x\n", cdev->ap.applid, np->ncci); |
Jan Kiszka | 05b4149 | 2010-02-08 10:12:19 +0000 | [diff] [blame] | 1426 | mutex_unlock(&cdev->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1427 | } |
Jan Kiszka | b8f433d | 2010-02-08 10:12:17 +0000 | [diff] [blame] | 1428 | mutex_unlock(&capidev_list_lock); |
Alexey Dobriyan | 9a58a80 | 2010-01-14 03:10:54 -0800 | [diff] [blame] | 1429 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1430 | } |
| 1431 | |
Alexey Dobriyan | 9a58a80 | 2010-01-14 03:10:54 -0800 | [diff] [blame] | 1432 | static int capi20ncci_proc_open(struct inode *inode, struct file *file) |
| 1433 | { |
| 1434 | return single_open(file, capi20ncci_proc_show, NULL); |
| 1435 | } |
| 1436 | |
| 1437 | static const struct file_operations capi20ncci_proc_fops = { |
| 1438 | .owner = THIS_MODULE, |
| 1439 | .open = capi20ncci_proc_open, |
| 1440 | .read = seq_read, |
| 1441 | .llseek = seq_lseek, |
| 1442 | .release = single_release, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1443 | }; |
| 1444 | |
| 1445 | static void __init proc_init(void) |
| 1446 | { |
Alexey Dobriyan | 9a58a80 | 2010-01-14 03:10:54 -0800 | [diff] [blame] | 1447 | proc_create("capi/capi20", 0, NULL, &capi20_proc_fops); |
| 1448 | proc_create("capi/capi20ncci", 0, NULL, &capi20ncci_proc_fops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1449 | } |
| 1450 | |
| 1451 | static void __exit proc_exit(void) |
| 1452 | { |
Alexey Dobriyan | 9a58a80 | 2010-01-14 03:10:54 -0800 | [diff] [blame] | 1453 | remove_proc_entry("capi/capi20", NULL); |
| 1454 | remove_proc_entry("capi/capi20ncci", NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1455 | } |
| 1456 | |
| 1457 | /* -------- init function and module interface ---------------------- */ |
| 1458 | |
| 1459 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1460 | static int __init capi_init(void) |
| 1461 | { |
Jan Kiszka | 88549d6 | 2010-02-08 10:12:09 +0000 | [diff] [blame] | 1462 | const char *compileinfo; |
Andrew Morton | 6d9eac3 | 2006-03-28 01:56:19 -0800 | [diff] [blame] | 1463 | int major_ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1464 | |
Andrew Morton | 6d9eac3 | 2006-03-28 01:56:19 -0800 | [diff] [blame] | 1465 | major_ret = register_chrdev(capi_major, "capi20", &capi_fops); |
| 1466 | if (major_ret < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1467 | printk(KERN_ERR "capi20: unable to get major %d\n", capi_major); |
Andrew Morton | 6d9eac3 | 2006-03-28 01:56:19 -0800 | [diff] [blame] | 1468 | return major_ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1469 | } |
gregkh@suse.de | 56b2293 | 2005-03-23 10:01:41 -0800 | [diff] [blame] | 1470 | capi_class = class_create(THIS_MODULE, "capi"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1471 | if (IS_ERR(capi_class)) { |
| 1472 | unregister_chrdev(capi_major, "capi20"); |
| 1473 | return PTR_ERR(capi_class); |
| 1474 | } |
| 1475 | |
Greg Kroah-Hartman | a9b1261 | 2008-07-21 20:03:34 -0700 | [diff] [blame] | 1476 | device_create(capi_class, NULL, MKDEV(capi_major, 0), NULL, "capi"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1477 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1478 | if (capinc_tty_init() < 0) { |
Tony Jones | d78b036 | 2007-09-25 02:03:03 +0200 | [diff] [blame] | 1479 | device_destroy(capi_class, MKDEV(capi_major, 0)); |
gregkh@suse.de | 56b2293 | 2005-03-23 10:01:41 -0800 | [diff] [blame] | 1480 | class_destroy(capi_class); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1481 | unregister_chrdev(capi_major, "capi20"); |
| 1482 | return -ENOMEM; |
| 1483 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1484 | |
| 1485 | proc_init(); |
| 1486 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1487 | #if defined(CONFIG_ISDN_CAPI_CAPIFS) || defined(CONFIG_ISDN_CAPI_CAPIFS_MODULE) |
| 1488 | compileinfo = " (middleware+capifs)"; |
Jan Kiszka | 501c87a | 2010-02-08 10:12:16 +0000 | [diff] [blame] | 1489 | #elif defined(CONFIG_ISDN_CAPI_MIDDLEWARE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1490 | compileinfo = " (no capifs)"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1491 | #else |
| 1492 | compileinfo = " (no middleware)"; |
| 1493 | #endif |
Jan Kiszka | 88549d6 | 2010-02-08 10:12:09 +0000 | [diff] [blame] | 1494 | printk(KERN_NOTICE "CAPI 2.0 started up with major %d%s\n", |
| 1495 | capi_major, compileinfo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1496 | |
| 1497 | return 0; |
| 1498 | } |
| 1499 | |
| 1500 | static void __exit capi_exit(void) |
| 1501 | { |
| 1502 | proc_exit(); |
| 1503 | |
Tony Jones | d78b036 | 2007-09-25 02:03:03 +0200 | [diff] [blame] | 1504 | device_destroy(capi_class, MKDEV(capi_major, 0)); |
gregkh@suse.de | 56b2293 | 2005-03-23 10:01:41 -0800 | [diff] [blame] | 1505 | class_destroy(capi_class); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1506 | unregister_chrdev(capi_major, "capi20"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1507 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1508 | capinc_tty_exit(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1509 | } |
| 1510 | |
| 1511 | module_init(capi_init); |
| 1512 | module_exit(capi_exit); |