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