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