Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* $Id: kcapi.c,v 1.1.2.8 2004/03/26 19:57:20 armin Exp $ |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 2 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * Kernel CAPI 2.0 Module |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 4 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | * Copyright 1999 by Carsten Paeth <calle@calle.de> |
| 6 | * Copyright 2002 by Kai Germaschewski <kai@germaschewski.name> |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 7 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | * This software may be used and distributed according to the terms |
| 9 | * of the GNU General Public License, incorporated herein by reference. |
| 10 | * |
| 11 | */ |
| 12 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include "kcapi.h" |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/mm.h> |
| 16 | #include <linux/interrupt.h> |
| 17 | #include <linux/ioport.h> |
| 18 | #include <linux/proc_fs.h> |
Ingo Molnar | 174cd4b | 2017-02-02 19:15:33 +0100 | [diff] [blame] | 19 | #include <linux/sched/signal.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <linux/seq_file.h> |
| 21 | #include <linux/skbuff.h> |
| 22 | #include <linux/workqueue.h> |
| 23 | #include <linux/capi.h> |
| 24 | #include <linux/kernelcapi.h> |
| 25 | #include <linux/init.h> |
| 26 | #include <linux/moduleparam.h> |
| 27 | #include <linux/delay.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 28 | #include <linux/slab.h> |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 29 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | #include <linux/isdn/capicmd.h> |
| 31 | #include <linux/isdn/capiutil.h> |
Arjan van de Ven | 9cdf182 | 2006-03-23 03:00:21 -0800 | [diff] [blame] | 32 | #include <linux/mutex.h> |
Jan Kiszka | 88c896e | 2010-02-08 10:12:15 +0000 | [diff] [blame] | 33 | #include <linux/rcupdate.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | static int showcapimsgs = 0; |
Tejun Heo | 158fa67 | 2010-12-24 15:59:06 +0100 | [diff] [blame] | 36 | static struct workqueue_struct *kcapi_wq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | module_param(showcapimsgs, uint, 0); |
| 39 | |
| 40 | /* ------------------------------------------------------------- */ |
| 41 | |
Jan Kiszka | ef69bb2 | 2010-02-08 10:12:13 +0000 | [diff] [blame] | 42 | struct capictr_event { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | struct work_struct work; |
Jan Kiszka | ef69bb2 | 2010-02-08 10:12:13 +0000 | [diff] [blame] | 44 | unsigned int type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | u32 controller; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | }; |
| 47 | |
| 48 | /* ------------------------------------------------------------- */ |
| 49 | |
Bhumika Goyal | 733a707 | 2017-08-06 22:39:06 +0530 | [diff] [blame] | 50 | static const struct capi_version driver_version = {2, 0, 1, 1 << 4}; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | static char driver_serial[CAPI_SERIAL_LEN] = "0004711"; |
| 52 | static char capi_manufakturer[64] = "AVM Berlin"; |
| 53 | |
| 54 | #define NCCI2CTRL(ncci) (((ncci) >> 24) & 0x7f) |
| 55 | |
Jan Kiszka | 0ca3a01 | 2010-02-08 10:12:14 +0000 | [diff] [blame] | 56 | struct capi_ctr *capi_controller[CAPI_MAXCONTR]; |
| 57 | DEFINE_MUTEX(capi_controller_lock); |
| 58 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | struct capi20_appl *capi_applications[CAPI_MAXAPPL]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | |
Jan Kiszka | 5225303 | 2010-02-08 10:12:10 +0000 | [diff] [blame] | 61 | static int ncontrollers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | |
| 63 | /* -------- controller ref counting -------------------------------------- */ |
| 64 | |
| 65 | static inline struct capi_ctr * |
Jan Kiszka | 5225303 | 2010-02-08 10:12:10 +0000 | [diff] [blame] | 66 | capi_ctr_get(struct capi_ctr *ctr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | { |
Jan Kiszka | 5225303 | 2010-02-08 10:12:10 +0000 | [diff] [blame] | 68 | if (!try_module_get(ctr->owner)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | return NULL; |
Jan Kiszka | 5225303 | 2010-02-08 10:12:10 +0000 | [diff] [blame] | 70 | return ctr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | static inline void |
Jan Kiszka | 5225303 | 2010-02-08 10:12:10 +0000 | [diff] [blame] | 74 | capi_ctr_put(struct capi_ctr *ctr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | { |
Jan Kiszka | 5225303 | 2010-02-08 10:12:10 +0000 | [diff] [blame] | 76 | module_put(ctr->owner); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | /* ------------------------------------------------------------- */ |
| 80 | |
| 81 | static inline struct capi_ctr *get_capi_ctr_by_nr(u16 contr) |
| 82 | { |
Dan Carpenter | 25dff94 | 2013-05-19 08:36:36 +0000 | [diff] [blame] | 83 | if (contr < 1 || contr - 1 >= CAPI_MAXCONTR) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | return NULL; |
| 85 | |
Jan Kiszka | 5225303 | 2010-02-08 10:12:10 +0000 | [diff] [blame] | 86 | return capi_controller[contr - 1]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | } |
| 88 | |
Jan Kiszka | b003f4e | 2010-10-17 05:18:15 +0000 | [diff] [blame] | 89 | static inline struct capi20_appl *__get_capi_appl_by_nr(u16 applid) |
| 90 | { |
| 91 | lockdep_assert_held(&capi_controller_lock); |
| 92 | |
Dan Carpenter | 25dff94 | 2013-05-19 08:36:36 +0000 | [diff] [blame] | 93 | if (applid < 1 || applid - 1 >= CAPI_MAXAPPL) |
Jan Kiszka | b003f4e | 2010-10-17 05:18:15 +0000 | [diff] [blame] | 94 | return NULL; |
| 95 | |
| 96 | return capi_applications[applid - 1]; |
| 97 | } |
| 98 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | static inline struct capi20_appl *get_capi_appl_by_nr(u16 applid) |
| 100 | { |
Dan Carpenter | 25dff94 | 2013-05-19 08:36:36 +0000 | [diff] [blame] | 101 | if (applid < 1 || applid - 1 >= CAPI_MAXAPPL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | return NULL; |
| 103 | |
Jan Kiszka | 88c896e | 2010-02-08 10:12:15 +0000 | [diff] [blame] | 104 | return rcu_dereference(capi_applications[applid - 1]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | /* -------- util functions ------------------------------------ */ |
| 108 | |
| 109 | static inline int capi_cmd_valid(u8 cmd) |
| 110 | { |
| 111 | switch (cmd) { |
| 112 | case CAPI_ALERT: |
| 113 | case CAPI_CONNECT: |
| 114 | case CAPI_CONNECT_ACTIVE: |
| 115 | case CAPI_CONNECT_B3_ACTIVE: |
| 116 | case CAPI_CONNECT_B3: |
| 117 | case CAPI_CONNECT_B3_T90_ACTIVE: |
| 118 | case CAPI_DATA_B3: |
| 119 | case CAPI_DISCONNECT_B3: |
| 120 | case CAPI_DISCONNECT: |
| 121 | case CAPI_FACILITY: |
| 122 | case CAPI_INFO: |
| 123 | case CAPI_LISTEN: |
| 124 | case CAPI_MANUFACTURER: |
| 125 | case CAPI_RESET_B3: |
| 126 | case CAPI_SELECT_B_PROTOCOL: |
| 127 | return 1; |
| 128 | } |
| 129 | return 0; |
| 130 | } |
| 131 | |
| 132 | static inline int capi_subcmd_valid(u8 subcmd) |
| 133 | { |
| 134 | switch (subcmd) { |
| 135 | case CAPI_REQ: |
| 136 | case CAPI_CONF: |
| 137 | case CAPI_IND: |
| 138 | case CAPI_RESP: |
| 139 | return 1; |
| 140 | } |
| 141 | return 0; |
| 142 | } |
| 143 | |
| 144 | /* ------------------------------------------------------------ */ |
| 145 | |
Jan Kiszka | 5225303 | 2010-02-08 10:12:10 +0000 | [diff] [blame] | 146 | static void |
| 147 | register_appl(struct capi_ctr *ctr, u16 applid, capi_register_params *rparam) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | { |
Jan Kiszka | 5225303 | 2010-02-08 10:12:10 +0000 | [diff] [blame] | 149 | ctr = capi_ctr_get(ctr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | |
Jan Kiszka | 5225303 | 2010-02-08 10:12:10 +0000 | [diff] [blame] | 151 | if (ctr) |
| 152 | ctr->register_appl(ctr, applid, rparam); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | else |
Jan Kiszka | 5225303 | 2010-02-08 10:12:10 +0000 | [diff] [blame] | 154 | printk(KERN_WARNING "%s: cannot get controller resources\n", |
| 155 | __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | |
Jan Kiszka | 5225303 | 2010-02-08 10:12:10 +0000 | [diff] [blame] | 159 | static void release_appl(struct capi_ctr *ctr, u16 applid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | { |
| 161 | DBG("applid %#x", applid); |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 162 | |
Jan Kiszka | 5225303 | 2010-02-08 10:12:10 +0000 | [diff] [blame] | 163 | ctr->release_appl(ctr, applid); |
| 164 | capi_ctr_put(ctr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | } |
| 166 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | static void notify_up(u32 contr) |
| 168 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | struct capi20_appl *ap; |
Jan Kiszka | 3efecf7 | 2010-02-08 10:12:12 +0000 | [diff] [blame] | 170 | struct capi_ctr *ctr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | u16 applid; |
| 172 | |
Jan Kiszka | 0ca3a01 | 2010-02-08 10:12:14 +0000 | [diff] [blame] | 173 | mutex_lock(&capi_controller_lock); |
| 174 | |
Jan Kiszka | 3efecf7 | 2010-02-08 10:12:12 +0000 | [diff] [blame] | 175 | if (showcapimsgs & 1) |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 176 | printk(KERN_DEBUG "kcapi: notify up contr %d\n", contr); |
Jan Kiszka | 3efecf7 | 2010-02-08 10:12:12 +0000 | [diff] [blame] | 177 | |
| 178 | ctr = get_capi_ctr_by_nr(contr); |
| 179 | if (ctr) { |
| 180 | if (ctr->state == CAPI_CTR_RUNNING) |
Jan Kiszka | 0ca3a01 | 2010-02-08 10:12:14 +0000 | [diff] [blame] | 181 | goto unlock_out; |
Jan Kiszka | 3efecf7 | 2010-02-08 10:12:12 +0000 | [diff] [blame] | 182 | |
| 183 | ctr->state = CAPI_CTR_RUNNING; |
| 184 | |
| 185 | for (applid = 1; applid <= CAPI_MAXAPPL; applid++) { |
Jan Kiszka | b003f4e | 2010-10-17 05:18:15 +0000 | [diff] [blame] | 186 | ap = __get_capi_appl_by_nr(applid); |
| 187 | if (ap) |
| 188 | register_appl(ctr, applid, &ap->rparam); |
Jan Kiszka | 3efecf7 | 2010-02-08 10:12:12 +0000 | [diff] [blame] | 189 | } |
| 190 | } else |
Harvey Harrison | 156f1ed | 2008-04-28 02:14:40 -0700 | [diff] [blame] | 191 | printk(KERN_WARNING "%s: invalid contr %d\n", __func__, contr); |
Jan Kiszka | 0ca3a01 | 2010-02-08 10:12:14 +0000 | [diff] [blame] | 192 | |
| 193 | unlock_out: |
| 194 | mutex_unlock(&capi_controller_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | } |
| 196 | |
Jan Kiszka | 0ca3a01 | 2010-02-08 10:12:14 +0000 | [diff] [blame] | 197 | static void ctr_down(struct capi_ctr *ctr, int new_state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | { |
| 199 | struct capi20_appl *ap; |
| 200 | u16 applid; |
| 201 | |
Jan Kiszka | 0ca3a01 | 2010-02-08 10:12:14 +0000 | [diff] [blame] | 202 | if (ctr->state == CAPI_CTR_DETECTED || ctr->state == CAPI_CTR_DETACHED) |
Jan Kiszka | 3efecf7 | 2010-02-08 10:12:12 +0000 | [diff] [blame] | 203 | return; |
| 204 | |
Jan Kiszka | 0ca3a01 | 2010-02-08 10:12:14 +0000 | [diff] [blame] | 205 | ctr->state = new_state; |
Jan Kiszka | 3efecf7 | 2010-02-08 10:12:12 +0000 | [diff] [blame] | 206 | |
| 207 | memset(ctr->manu, 0, sizeof(ctr->manu)); |
| 208 | memset(&ctr->version, 0, sizeof(ctr->version)); |
| 209 | memset(&ctr->profile, 0, sizeof(ctr->profile)); |
| 210 | memset(ctr->serial, 0, sizeof(ctr->serial)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | |
| 212 | for (applid = 1; applid <= CAPI_MAXAPPL; applid++) { |
Jan Kiszka | b003f4e | 2010-10-17 05:18:15 +0000 | [diff] [blame] | 213 | ap = __get_capi_appl_by_nr(applid); |
Jan Kiszka | 88c896e | 2010-02-08 10:12:15 +0000 | [diff] [blame] | 214 | if (ap) |
Jan Kiszka | 3efecf7 | 2010-02-08 10:12:12 +0000 | [diff] [blame] | 215 | capi_ctr_put(ctr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | } |
| 217 | } |
| 218 | |
Jan Kiszka | 3efecf7 | 2010-02-08 10:12:12 +0000 | [diff] [blame] | 219 | static void notify_down(u32 contr) |
| 220 | { |
| 221 | struct capi_ctr *ctr; |
| 222 | |
Jan Kiszka | 0ca3a01 | 2010-02-08 10:12:14 +0000 | [diff] [blame] | 223 | mutex_lock(&capi_controller_lock); |
| 224 | |
Jan Kiszka | 3efecf7 | 2010-02-08 10:12:12 +0000 | [diff] [blame] | 225 | if (showcapimsgs & 1) |
| 226 | printk(KERN_DEBUG "kcapi: notify down contr %d\n", contr); |
| 227 | |
| 228 | ctr = get_capi_ctr_by_nr(contr); |
| 229 | if (ctr) |
Jan Kiszka | 0ca3a01 | 2010-02-08 10:12:14 +0000 | [diff] [blame] | 230 | ctr_down(ctr, CAPI_CTR_DETECTED); |
Jan Kiszka | 3efecf7 | 2010-02-08 10:12:12 +0000 | [diff] [blame] | 231 | else |
| 232 | printk(KERN_WARNING "%s: invalid contr %d\n", __func__, contr); |
Jan Kiszka | 0ca3a01 | 2010-02-08 10:12:14 +0000 | [diff] [blame] | 233 | |
| 234 | mutex_unlock(&capi_controller_lock); |
Jan Kiszka | 3efecf7 | 2010-02-08 10:12:12 +0000 | [diff] [blame] | 235 | } |
| 236 | |
Jan Kiszka | ef69bb2 | 2010-02-08 10:12:13 +0000 | [diff] [blame] | 237 | static void do_notify_work(struct work_struct *work) |
| 238 | { |
| 239 | struct capictr_event *event = |
| 240 | container_of(work, struct capictr_event, work); |
| 241 | |
Arnd Bergmann | f59aba2 | 2019-12-10 21:59:16 +0100 | [diff] [blame] | 242 | switch (event->type) { |
| 243 | case CAPICTR_UP: |
| 244 | notify_up(event->controller); |
| 245 | break; |
| 246 | case CAPICTR_DOWN: |
| 247 | notify_down(event->controller); |
| 248 | break; |
| 249 | } |
| 250 | |
Jan Kiszka | ef69bb2 | 2010-02-08 10:12:13 +0000 | [diff] [blame] | 251 | kfree(event); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | } |
| 253 | |
Jan Kiszka | ef69bb2 | 2010-02-08 10:12:13 +0000 | [diff] [blame] | 254 | static int notify_push(unsigned int event_type, u32 controller) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | { |
Jan Kiszka | ef69bb2 | 2010-02-08 10:12:13 +0000 | [diff] [blame] | 256 | struct capictr_event *event = kmalloc(sizeof(*event), GFP_ATOMIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | |
Jan Kiszka | ef69bb2 | 2010-02-08 10:12:13 +0000 | [diff] [blame] | 258 | if (!event) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | return -ENOMEM; |
| 260 | |
Jan Kiszka | ef69bb2 | 2010-02-08 10:12:13 +0000 | [diff] [blame] | 261 | INIT_WORK(&event->work, do_notify_work); |
| 262 | event->type = event_type; |
| 263 | event->controller = controller; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | |
Tejun Heo | 158fa67 | 2010-12-24 15:59:06 +0100 | [diff] [blame] | 265 | queue_work(kcapi_wq, &event->work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | return 0; |
| 267 | } |
| 268 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | /* -------- Receiver ------------------------------------------ */ |
| 270 | |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 271 | static void recv_handler(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | { |
| 273 | struct sk_buff *skb; |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 274 | struct capi20_appl *ap = |
| 275 | container_of(work, struct capi20_appl, recv_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | |
| 277 | if ((!ap) || (ap->release_in_progress)) |
| 278 | return; |
| 279 | |
Matthias Kaehlcke | 67837f2 | 2007-07-17 04:04:16 -0700 | [diff] [blame] | 280 | mutex_lock(&ap->recv_mtx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | while ((skb = skb_dequeue(&ap->recv_queue))) { |
| 282 | if (CAPIMSG_CMD(skb->data) == CAPI_DATA_B3_IND) |
| 283 | ap->nrecvdatapkt++; |
| 284 | else |
| 285 | ap->nrecvctlpkt++; |
| 286 | |
| 287 | ap->recv_message(ap, skb); |
| 288 | } |
Matthias Kaehlcke | 67837f2 | 2007-07-17 04:04:16 -0700 | [diff] [blame] | 289 | mutex_unlock(&ap->recv_mtx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | } |
| 291 | |
Tilman Schmidt | 554f200 | 2009-04-23 02:24:21 +0000 | [diff] [blame] | 292 | /** |
| 293 | * capi_ctr_handle_message() - handle incoming CAPI message |
Jan Kiszka | 5225303 | 2010-02-08 10:12:10 +0000 | [diff] [blame] | 294 | * @ctr: controller descriptor structure. |
Tilman Schmidt | 554f200 | 2009-04-23 02:24:21 +0000 | [diff] [blame] | 295 | * @appl: application ID. |
| 296 | * @skb: message. |
| 297 | * |
| 298 | * Called by hardware driver to pass a CAPI message to the application. |
| 299 | */ |
| 300 | |
Jan Kiszka | 5225303 | 2010-02-08 10:12:10 +0000 | [diff] [blame] | 301 | void capi_ctr_handle_message(struct capi_ctr *ctr, u16 appl, |
| 302 | struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | { |
| 304 | struct capi20_appl *ap; |
| 305 | int showctl = 0; |
| 306 | u8 cmd, subcmd; |
Karsten Keil | 17f0cd2 | 2007-02-28 20:13:50 -0800 | [diff] [blame] | 307 | _cdebbuf *cdb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | |
Jan Kiszka | 5225303 | 2010-02-08 10:12:10 +0000 | [diff] [blame] | 309 | if (ctr->state != CAPI_CTR_RUNNING) { |
Karsten Keil | 17f0cd2 | 2007-02-28 20:13:50 -0800 | [diff] [blame] | 310 | cdb = capi_message2str(skb->data); |
| 311 | if (cdb) { |
| 312 | printk(KERN_INFO "kcapi: controller [%03d] not active, got: %s", |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 313 | ctr->cnr, cdb->buf); |
Karsten Keil | 17f0cd2 | 2007-02-28 20:13:50 -0800 | [diff] [blame] | 314 | cdebbuf_free(cdb); |
| 315 | } else |
| 316 | printk(KERN_INFO "kcapi: controller [%03d] not active, cannot trace\n", |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 317 | ctr->cnr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | goto error; |
| 319 | } |
| 320 | |
| 321 | cmd = CAPIMSG_COMMAND(skb->data); |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 322 | subcmd = CAPIMSG_SUBCOMMAND(skb->data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | if (cmd == CAPI_DATA_B3 && subcmd == CAPI_IND) { |
Jan Kiszka | 5225303 | 2010-02-08 10:12:10 +0000 | [diff] [blame] | 324 | ctr->nrecvdatapkt++; |
| 325 | if (ctr->traceflag > 2) |
| 326 | showctl |= 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | } else { |
Jan Kiszka | 5225303 | 2010-02-08 10:12:10 +0000 | [diff] [blame] | 328 | ctr->nrecvctlpkt++; |
| 329 | if (ctr->traceflag) |
| 330 | showctl |= 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | } |
Jan Kiszka | 5225303 | 2010-02-08 10:12:10 +0000 | [diff] [blame] | 332 | showctl |= (ctr->traceflag & 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | if (showctl & 2) { |
| 334 | if (showctl & 1) { |
Karsten Keil | 17f0cd2 | 2007-02-28 20:13:50 -0800 | [diff] [blame] | 335 | printk(KERN_DEBUG "kcapi: got [%03d] id#%d %s len=%u\n", |
Jan Kiszka | 5225303 | 2010-02-08 10:12:10 +0000 | [diff] [blame] | 336 | ctr->cnr, CAPIMSG_APPID(skb->data), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | capi_cmd2str(cmd, subcmd), |
| 338 | CAPIMSG_LEN(skb->data)); |
| 339 | } else { |
Karsten Keil | 17f0cd2 | 2007-02-28 20:13:50 -0800 | [diff] [blame] | 340 | cdb = capi_message2str(skb->data); |
| 341 | if (cdb) { |
| 342 | printk(KERN_DEBUG "kcapi: got [%03d] %s\n", |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 343 | ctr->cnr, cdb->buf); |
Karsten Keil | 17f0cd2 | 2007-02-28 20:13:50 -0800 | [diff] [blame] | 344 | cdebbuf_free(cdb); |
| 345 | } else |
| 346 | printk(KERN_DEBUG "kcapi: got [%03d] id#%d %s len=%u, cannot trace\n", |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 347 | ctr->cnr, CAPIMSG_APPID(skb->data), |
| 348 | capi_cmd2str(cmd, subcmd), |
| 349 | CAPIMSG_LEN(skb->data)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | } |
| 353 | |
Jan Kiszka | 88c896e | 2010-02-08 10:12:15 +0000 | [diff] [blame] | 354 | rcu_read_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | ap = get_capi_appl_by_nr(CAPIMSG_APPID(skb->data)); |
Jan Kiszka | 88c896e | 2010-02-08 10:12:15 +0000 | [diff] [blame] | 356 | if (!ap) { |
| 357 | rcu_read_unlock(); |
Karsten Keil | 17f0cd2 | 2007-02-28 20:13:50 -0800 | [diff] [blame] | 358 | cdb = capi_message2str(skb->data); |
| 359 | if (cdb) { |
| 360 | printk(KERN_ERR "kcapi: handle_message: applid %d state released (%s)\n", |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 361 | CAPIMSG_APPID(skb->data), cdb->buf); |
Karsten Keil | 17f0cd2 | 2007-02-28 20:13:50 -0800 | [diff] [blame] | 362 | cdebbuf_free(cdb); |
| 363 | } else |
| 364 | printk(KERN_ERR "kcapi: handle_message: applid %d state released (%s) cannot trace\n", |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 365 | CAPIMSG_APPID(skb->data), |
| 366 | capi_cmd2str(cmd, subcmd)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | goto error; |
| 368 | } |
| 369 | skb_queue_tail(&ap->recv_queue, skb); |
Tejun Heo | 158fa67 | 2010-12-24 15:59:06 +0100 | [diff] [blame] | 370 | queue_work(kcapi_wq, &ap->recv_work); |
Jan Kiszka | 88c896e | 2010-02-08 10:12:15 +0000 | [diff] [blame] | 371 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | |
| 373 | return; |
| 374 | |
| 375 | error: |
| 376 | kfree_skb(skb); |
| 377 | } |
| 378 | |
| 379 | EXPORT_SYMBOL(capi_ctr_handle_message); |
| 380 | |
Tilman Schmidt | 554f200 | 2009-04-23 02:24:21 +0000 | [diff] [blame] | 381 | /** |
| 382 | * capi_ctr_ready() - signal CAPI controller ready |
Jan Kiszka | 5225303 | 2010-02-08 10:12:10 +0000 | [diff] [blame] | 383 | * @ctr: controller descriptor structure. |
Tilman Schmidt | 554f200 | 2009-04-23 02:24:21 +0000 | [diff] [blame] | 384 | * |
| 385 | * Called by hardware driver to signal that the controller is up and running. |
| 386 | */ |
| 387 | |
Jan Kiszka | 5225303 | 2010-02-08 10:12:10 +0000 | [diff] [blame] | 388 | void capi_ctr_ready(struct capi_ctr *ctr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | { |
Jan Kiszka | 5225303 | 2010-02-08 10:12:10 +0000 | [diff] [blame] | 390 | printk(KERN_NOTICE "kcapi: controller [%03d] \"%s\" ready.\n", |
| 391 | ctr->cnr, ctr->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | |
Jan Kiszka | ef69bb2 | 2010-02-08 10:12:13 +0000 | [diff] [blame] | 393 | notify_push(CAPICTR_UP, ctr->cnr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | EXPORT_SYMBOL(capi_ctr_ready); |
| 397 | |
Tilman Schmidt | 554f200 | 2009-04-23 02:24:21 +0000 | [diff] [blame] | 398 | /** |
Tilman Schmidt | 4e32997 | 2009-06-07 09:09:23 +0000 | [diff] [blame] | 399 | * capi_ctr_down() - signal CAPI controller not ready |
Jan Kiszka | 5225303 | 2010-02-08 10:12:10 +0000 | [diff] [blame] | 400 | * @ctr: controller descriptor structure. |
Tilman Schmidt | 554f200 | 2009-04-23 02:24:21 +0000 | [diff] [blame] | 401 | * |
| 402 | * Called by hardware driver to signal that the controller is down and |
| 403 | * unavailable for use. |
| 404 | */ |
| 405 | |
Jan Kiszka | 5225303 | 2010-02-08 10:12:10 +0000 | [diff] [blame] | 406 | void capi_ctr_down(struct capi_ctr *ctr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | { |
Jan Kiszka | 5225303 | 2010-02-08 10:12:10 +0000 | [diff] [blame] | 408 | printk(KERN_NOTICE "kcapi: controller [%03d] down.\n", ctr->cnr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | |
Jan Kiszka | ef69bb2 | 2010-02-08 10:12:13 +0000 | [diff] [blame] | 410 | notify_push(CAPICTR_DOWN, ctr->cnr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | } |
| 412 | |
Tilman Schmidt | 4e32997 | 2009-06-07 09:09:23 +0000 | [diff] [blame] | 413 | EXPORT_SYMBOL(capi_ctr_down); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | /* ------------------------------------------------------------- */ |
| 416 | |
Tilman Schmidt | 554f200 | 2009-04-23 02:24:21 +0000 | [diff] [blame] | 417 | /** |
| 418 | * attach_capi_ctr() - register CAPI controller |
Jan Kiszka | 5225303 | 2010-02-08 10:12:10 +0000 | [diff] [blame] | 419 | * @ctr: controller descriptor structure. |
Tilman Schmidt | 554f200 | 2009-04-23 02:24:21 +0000 | [diff] [blame] | 420 | * |
| 421 | * Called by hardware driver to register a controller with the CAPI subsystem. |
| 422 | * Return value: 0 on success, error code < 0 on error |
| 423 | */ |
| 424 | |
Jan Kiszka | 5225303 | 2010-02-08 10:12:10 +0000 | [diff] [blame] | 425 | int attach_capi_ctr(struct capi_ctr *ctr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | { |
| 427 | int i; |
| 428 | |
Jan Kiszka | 0ca3a01 | 2010-02-08 10:12:14 +0000 | [diff] [blame] | 429 | mutex_lock(&capi_controller_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | |
| 431 | for (i = 0; i < CAPI_MAXCONTR; i++) { |
Jan Kiszka | 5225303 | 2010-02-08 10:12:10 +0000 | [diff] [blame] | 432 | if (!capi_controller[i]) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | break; |
| 434 | } |
| 435 | if (i == CAPI_MAXCONTR) { |
Jan Kiszka | 0ca3a01 | 2010-02-08 10:12:14 +0000 | [diff] [blame] | 436 | mutex_unlock(&capi_controller_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | printk(KERN_ERR "kcapi: out of controller slots\n"); |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 438 | return -EBUSY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | } |
Jan Kiszka | 5225303 | 2010-02-08 10:12:10 +0000 | [diff] [blame] | 440 | capi_controller[i] = ctr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | |
Jan Kiszka | 5225303 | 2010-02-08 10:12:10 +0000 | [diff] [blame] | 442 | ctr->nrecvctlpkt = 0; |
| 443 | ctr->nrecvdatapkt = 0; |
| 444 | ctr->nsentctlpkt = 0; |
| 445 | ctr->nsentdatapkt = 0; |
| 446 | ctr->cnr = i + 1; |
| 447 | ctr->state = CAPI_CTR_DETECTED; |
| 448 | ctr->blocked = 0; |
| 449 | ctr->traceflag = showcapimsgs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | |
Jan Kiszka | 5225303 | 2010-02-08 10:12:10 +0000 | [diff] [blame] | 451 | sprintf(ctr->procfn, "capi/controllers/%d", ctr->cnr); |
Christoph Hellwig | 2cd1f0d | 2018-04-11 18:39:29 +0200 | [diff] [blame] | 452 | ctr->procent = proc_create_single_data(ctr->procfn, 0, NULL, |
| 453 | ctr->proc_show, ctr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | |
Jan Kiszka | 5225303 | 2010-02-08 10:12:10 +0000 | [diff] [blame] | 455 | ncontrollers++; |
Jan Kiszka | 0ca3a01 | 2010-02-08 10:12:14 +0000 | [diff] [blame] | 456 | |
| 457 | mutex_unlock(&capi_controller_lock); |
| 458 | |
Jan Kiszka | 5225303 | 2010-02-08 10:12:10 +0000 | [diff] [blame] | 459 | printk(KERN_NOTICE "kcapi: controller [%03d]: %s attached\n", |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 460 | ctr->cnr, ctr->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | return 0; |
| 462 | } |
| 463 | |
| 464 | EXPORT_SYMBOL(attach_capi_ctr); |
| 465 | |
Tilman Schmidt | 554f200 | 2009-04-23 02:24:21 +0000 | [diff] [blame] | 466 | /** |
| 467 | * detach_capi_ctr() - unregister CAPI controller |
Jan Kiszka | 5225303 | 2010-02-08 10:12:10 +0000 | [diff] [blame] | 468 | * @ctr: controller descriptor structure. |
Tilman Schmidt | 554f200 | 2009-04-23 02:24:21 +0000 | [diff] [blame] | 469 | * |
| 470 | * Called by hardware driver to remove the registration of a controller |
| 471 | * with the CAPI subsystem. |
| 472 | * Return value: 0 on success, error code < 0 on error |
| 473 | */ |
| 474 | |
Jan Kiszka | 5225303 | 2010-02-08 10:12:10 +0000 | [diff] [blame] | 475 | int detach_capi_ctr(struct capi_ctr *ctr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | { |
Jan Kiszka | 0ca3a01 | 2010-02-08 10:12:14 +0000 | [diff] [blame] | 477 | int err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | |
Jan Kiszka | 0ca3a01 | 2010-02-08 10:12:14 +0000 | [diff] [blame] | 479 | mutex_lock(&capi_controller_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | |
Jan Kiszka | 0ca3a01 | 2010-02-08 10:12:14 +0000 | [diff] [blame] | 481 | ctr_down(ctr, CAPI_CTR_DETACHED); |
| 482 | |
Xiaolong Huang | 1f3e2e9 | 2021-10-08 14:58:30 +0800 | [diff] [blame] | 483 | if (ctr->cnr < 1 || ctr->cnr - 1 >= CAPI_MAXCONTR) { |
| 484 | err = -EINVAL; |
| 485 | goto unlock_out; |
| 486 | } |
| 487 | |
Jan Kiszka | 0ca3a01 | 2010-02-08 10:12:14 +0000 | [diff] [blame] | 488 | if (capi_controller[ctr->cnr - 1] != ctr) { |
| 489 | err = -EINVAL; |
| 490 | goto unlock_out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | } |
Jan Kiszka | 5225303 | 2010-02-08 10:12:10 +0000 | [diff] [blame] | 492 | capi_controller[ctr->cnr - 1] = NULL; |
Jan Kiszka | 0ca3a01 | 2010-02-08 10:12:14 +0000 | [diff] [blame] | 493 | ncontrollers--; |
| 494 | |
| 495 | if (ctr->procent) |
| 496 | remove_proc_entry(ctr->procfn, NULL); |
| 497 | |
Jan Kiszka | 5225303 | 2010-02-08 10:12:10 +0000 | [diff] [blame] | 498 | printk(KERN_NOTICE "kcapi: controller [%03d]: %s unregistered\n", |
| 499 | ctr->cnr, ctr->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | |
Jan Kiszka | 0ca3a01 | 2010-02-08 10:12:14 +0000 | [diff] [blame] | 501 | unlock_out: |
| 502 | mutex_unlock(&capi_controller_lock); |
| 503 | |
| 504 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | EXPORT_SYMBOL(detach_capi_ctr); |
| 508 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | /* ------------------------------------------------------------- */ |
| 510 | /* -------- CAPI2.0 Interface ---------------------------------- */ |
| 511 | /* ------------------------------------------------------------- */ |
| 512 | |
Tilman Schmidt | 554f200 | 2009-04-23 02:24:21 +0000 | [diff] [blame] | 513 | /** |
| 514 | * capi20_isinstalled() - CAPI 2.0 operation CAPI_INSTALLED |
| 515 | * |
| 516 | * Return value: CAPI result code (CAPI_NOERROR if at least one ISDN controller |
| 517 | * is ready for use, CAPI_REGNOTINSTALLED otherwise) |
| 518 | */ |
| 519 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | u16 capi20_isinstalled(void) |
| 521 | { |
Jan Kiszka | 0ca3a01 | 2010-02-08 10:12:14 +0000 | [diff] [blame] | 522 | u16 ret = CAPI_REGNOTINSTALLED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | int i; |
Jan Kiszka | 0ca3a01 | 2010-02-08 10:12:14 +0000 | [diff] [blame] | 524 | |
| 525 | mutex_lock(&capi_controller_lock); |
| 526 | |
| 527 | for (i = 0; i < CAPI_MAXCONTR; i++) |
Jan Kiszka | 5225303 | 2010-02-08 10:12:10 +0000 | [diff] [blame] | 528 | if (capi_controller[i] && |
Jan Kiszka | 0ca3a01 | 2010-02-08 10:12:14 +0000 | [diff] [blame] | 529 | capi_controller[i]->state == CAPI_CTR_RUNNING) { |
| 530 | ret = CAPI_NOERROR; |
| 531 | break; |
| 532 | } |
| 533 | |
| 534 | mutex_unlock(&capi_controller_lock); |
| 535 | |
| 536 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | } |
| 538 | |
Tilman Schmidt | 554f200 | 2009-04-23 02:24:21 +0000 | [diff] [blame] | 539 | /** |
| 540 | * capi20_register() - CAPI 2.0 operation CAPI_REGISTER |
| 541 | * @ap: CAPI application descriptor structure. |
| 542 | * |
| 543 | * Register an application's presence with CAPI. |
| 544 | * A unique application ID is assigned and stored in @ap->applid. |
| 545 | * After this function returns successfully, the message receive |
| 546 | * callback function @ap->recv_message() may be called at any time |
| 547 | * until capi20_release() has been called for the same @ap. |
| 548 | * Return value: CAPI result code |
| 549 | */ |
| 550 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | u16 capi20_register(struct capi20_appl *ap) |
| 552 | { |
| 553 | int i; |
| 554 | u16 applid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | |
| 556 | DBG(""); |
| 557 | |
| 558 | if (ap->rparam.datablklen < 128) |
| 559 | return CAPI_LOGBLKSIZETOSMALL; |
| 560 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | ap->nrecvctlpkt = 0; |
| 562 | ap->nrecvdatapkt = 0; |
| 563 | ap->nsentctlpkt = 0; |
| 564 | ap->nsentdatapkt = 0; |
Matthias Kaehlcke | 67837f2 | 2007-07-17 04:04:16 -0700 | [diff] [blame] | 565 | mutex_init(&ap->recv_mtx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 566 | skb_queue_head_init(&ap->recv_queue); |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 567 | INIT_WORK(&ap->recv_work, recv_handler); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | ap->release_in_progress = 0; |
| 569 | |
Jan Kiszka | 0ca3a01 | 2010-02-08 10:12:14 +0000 | [diff] [blame] | 570 | mutex_lock(&capi_controller_lock); |
| 571 | |
Jan Kiszka | 88c896e | 2010-02-08 10:12:15 +0000 | [diff] [blame] | 572 | for (applid = 1; applid <= CAPI_MAXAPPL; applid++) { |
| 573 | if (capi_applications[applid - 1] == NULL) |
| 574 | break; |
| 575 | } |
| 576 | if (applid > CAPI_MAXAPPL) { |
| 577 | mutex_unlock(&capi_controller_lock); |
| 578 | return CAPI_TOOMANYAPPLS; |
| 579 | } |
| 580 | |
| 581 | ap->applid = applid; |
| 582 | capi_applications[applid - 1] = ap; |
| 583 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 584 | for (i = 0; i < CAPI_MAXCONTR; i++) { |
Jan Kiszka | 5225303 | 2010-02-08 10:12:10 +0000 | [diff] [blame] | 585 | if (!capi_controller[i] || |
| 586 | capi_controller[i]->state != CAPI_CTR_RUNNING) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 587 | continue; |
Jan Kiszka | 5225303 | 2010-02-08 10:12:10 +0000 | [diff] [blame] | 588 | register_appl(capi_controller[i], applid, &ap->rparam); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 589 | } |
Jan Kiszka | 0ca3a01 | 2010-02-08 10:12:14 +0000 | [diff] [blame] | 590 | |
| 591 | mutex_unlock(&capi_controller_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 592 | |
| 593 | if (showcapimsgs & 1) { |
| 594 | printk(KERN_DEBUG "kcapi: appl %d up\n", applid); |
| 595 | } |
| 596 | |
| 597 | return CAPI_NOERROR; |
| 598 | } |
| 599 | |
Tilman Schmidt | 554f200 | 2009-04-23 02:24:21 +0000 | [diff] [blame] | 600 | /** |
| 601 | * capi20_release() - CAPI 2.0 operation CAPI_RELEASE |
| 602 | * @ap: CAPI application descriptor structure. |
| 603 | * |
| 604 | * Terminate an application's registration with CAPI. |
| 605 | * After this function returns successfully, the message receive |
| 606 | * callback function @ap->recv_message() will no longer be called. |
| 607 | * Return value: CAPI result code |
| 608 | */ |
| 609 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | u16 capi20_release(struct capi20_appl *ap) |
| 611 | { |
| 612 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | |
| 614 | DBG("applid %#x", ap->applid); |
| 615 | |
Jan Kiszka | 88c896e | 2010-02-08 10:12:15 +0000 | [diff] [blame] | 616 | mutex_lock(&capi_controller_lock); |
| 617 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | ap->release_in_progress = 1; |
| 619 | capi_applications[ap->applid - 1] = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 620 | |
Jan Kiszka | 88c896e | 2010-02-08 10:12:15 +0000 | [diff] [blame] | 621 | synchronize_rcu(); |
Jan Kiszka | 0ca3a01 | 2010-02-08 10:12:14 +0000 | [diff] [blame] | 622 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | for (i = 0; i < CAPI_MAXCONTR; i++) { |
Jan Kiszka | 5225303 | 2010-02-08 10:12:10 +0000 | [diff] [blame] | 624 | if (!capi_controller[i] || |
| 625 | capi_controller[i]->state != CAPI_CTR_RUNNING) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | continue; |
Jan Kiszka | 5225303 | 2010-02-08 10:12:10 +0000 | [diff] [blame] | 627 | release_appl(capi_controller[i], ap->applid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | } |
Jan Kiszka | 0ca3a01 | 2010-02-08 10:12:14 +0000 | [diff] [blame] | 629 | |
| 630 | mutex_unlock(&capi_controller_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 | |
Tejun Heo | 158fa67 | 2010-12-24 15:59:06 +0100 | [diff] [blame] | 632 | flush_workqueue(kcapi_wq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 633 | skb_queue_purge(&ap->recv_queue); |
| 634 | |
| 635 | if (showcapimsgs & 1) { |
| 636 | printk(KERN_DEBUG "kcapi: appl %d down\n", ap->applid); |
| 637 | } |
| 638 | |
| 639 | return CAPI_NOERROR; |
| 640 | } |
| 641 | |
Tilman Schmidt | 554f200 | 2009-04-23 02:24:21 +0000 | [diff] [blame] | 642 | /** |
| 643 | * capi20_put_message() - CAPI 2.0 operation CAPI_PUT_MESSAGE |
| 644 | * @ap: CAPI application descriptor structure. |
| 645 | * @skb: CAPI message. |
| 646 | * |
| 647 | * Transfer a single message to CAPI. |
| 648 | * Return value: CAPI result code |
| 649 | */ |
| 650 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 651 | u16 capi20_put_message(struct capi20_appl *ap, struct sk_buff *skb) |
| 652 | { |
Jan Kiszka | 5225303 | 2010-02-08 10:12:10 +0000 | [diff] [blame] | 653 | struct capi_ctr *ctr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 654 | int showctl = 0; |
| 655 | u8 cmd, subcmd; |
| 656 | |
| 657 | DBG("applid %#x", ap->applid); |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 658 | |
Jan Kiszka | 5225303 | 2010-02-08 10:12:10 +0000 | [diff] [blame] | 659 | if (ncontrollers == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 | return CAPI_REGNOTINSTALLED; |
| 661 | if ((ap->applid == 0) || ap->release_in_progress) |
| 662 | return CAPI_ILLAPPNR; |
| 663 | if (skb->len < 12 |
| 664 | || !capi_cmd_valid(CAPIMSG_COMMAND(skb->data)) |
| 665 | || !capi_subcmd_valid(CAPIMSG_SUBCOMMAND(skb->data))) |
| 666 | return CAPI_ILLCMDORSUBCMDORMSGTOSMALL; |
Jan Kiszka | 0ca3a01 | 2010-02-08 10:12:14 +0000 | [diff] [blame] | 667 | |
| 668 | /* |
| 669 | * The controller reference is protected by the existence of the |
| 670 | * application passed to us. We assume that the caller properly |
| 671 | * synchronizes this service with capi20_release. |
| 672 | */ |
Jan Kiszka | 5225303 | 2010-02-08 10:12:10 +0000 | [diff] [blame] | 673 | ctr = get_capi_ctr_by_nr(CAPIMSG_CONTROLLER(skb->data)); |
Jan Kiszka | c6af043 | 2010-02-08 10:12:43 +0000 | [diff] [blame] | 674 | if (!ctr || ctr->state != CAPI_CTR_RUNNING) |
| 675 | return CAPI_REGNOTINSTALLED; |
Jan Kiszka | 5225303 | 2010-02-08 10:12:10 +0000 | [diff] [blame] | 676 | if (ctr->blocked) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | return CAPI_SENDQUEUEFULL; |
| 678 | |
| 679 | cmd = CAPIMSG_COMMAND(skb->data); |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 680 | subcmd = CAPIMSG_SUBCOMMAND(skb->data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 682 | if (cmd == CAPI_DATA_B3 && subcmd == CAPI_REQ) { |
Jan Kiszka | 5225303 | 2010-02-08 10:12:10 +0000 | [diff] [blame] | 683 | ctr->nsentdatapkt++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 684 | ap->nsentdatapkt++; |
Jan Kiszka | 5225303 | 2010-02-08 10:12:10 +0000 | [diff] [blame] | 685 | if (ctr->traceflag > 2) |
| 686 | showctl |= 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 687 | } else { |
Jan Kiszka | 5225303 | 2010-02-08 10:12:10 +0000 | [diff] [blame] | 688 | ctr->nsentctlpkt++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | ap->nsentctlpkt++; |
Jan Kiszka | 5225303 | 2010-02-08 10:12:10 +0000 | [diff] [blame] | 690 | if (ctr->traceflag) |
| 691 | showctl |= 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 692 | } |
Jan Kiszka | 5225303 | 2010-02-08 10:12:10 +0000 | [diff] [blame] | 693 | showctl |= (ctr->traceflag & 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | if (showctl & 2) { |
| 695 | if (showctl & 1) { |
Karsten Keil | 17f0cd2 | 2007-02-28 20:13:50 -0800 | [diff] [blame] | 696 | printk(KERN_DEBUG "kcapi: put [%03d] id#%d %s len=%u\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 697 | CAPIMSG_CONTROLLER(skb->data), |
| 698 | CAPIMSG_APPID(skb->data), |
| 699 | capi_cmd2str(cmd, subcmd), |
| 700 | CAPIMSG_LEN(skb->data)); |
| 701 | } else { |
Karsten Keil | 17f0cd2 | 2007-02-28 20:13:50 -0800 | [diff] [blame] | 702 | _cdebbuf *cdb = capi_message2str(skb->data); |
| 703 | if (cdb) { |
| 704 | printk(KERN_DEBUG "kcapi: put [%03d] %s\n", |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 705 | CAPIMSG_CONTROLLER(skb->data), |
| 706 | cdb->buf); |
Karsten Keil | 17f0cd2 | 2007-02-28 20:13:50 -0800 | [diff] [blame] | 707 | cdebbuf_free(cdb); |
| 708 | } else |
| 709 | printk(KERN_DEBUG "kcapi: put [%03d] id#%d %s len=%u cannot trace\n", |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 710 | CAPIMSG_CONTROLLER(skb->data), |
| 711 | CAPIMSG_APPID(skb->data), |
| 712 | capi_cmd2str(cmd, subcmd), |
| 713 | CAPIMSG_LEN(skb->data)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 714 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 715 | } |
Jan Kiszka | 5225303 | 2010-02-08 10:12:10 +0000 | [diff] [blame] | 716 | return ctr->send_message(ctr, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 | } |
| 718 | |
Tilman Schmidt | 554f200 | 2009-04-23 02:24:21 +0000 | [diff] [blame] | 719 | /** |
| 720 | * capi20_get_manufacturer() - CAPI 2.0 operation CAPI_GET_MANUFACTURER |
| 721 | * @contr: controller number. |
| 722 | * @buf: result buffer (64 bytes). |
| 723 | * |
| 724 | * Retrieve information about the manufacturer of the specified ISDN controller |
| 725 | * or (for @contr == 0) the driver itself. |
| 726 | * Return value: CAPI result code |
| 727 | */ |
| 728 | |
Arnd Bergmann | 5ee7d4c | 2021-03-22 17:44:29 +0100 | [diff] [blame] | 729 | u16 capi20_get_manufacturer(u32 contr, u8 buf[CAPI_MANUFACTURER_LEN]) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 730 | { |
Jan Kiszka | 5225303 | 2010-02-08 10:12:10 +0000 | [diff] [blame] | 731 | struct capi_ctr *ctr; |
Jan Kiszka | 0ca3a01 | 2010-02-08 10:12:14 +0000 | [diff] [blame] | 732 | u16 ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 733 | |
| 734 | if (contr == 0) { |
Eric Dumazet | d63967e | 2019-01-02 09:20:27 -0800 | [diff] [blame] | 735 | strncpy(buf, capi_manufakturer, CAPI_MANUFACTURER_LEN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 736 | return CAPI_NOERROR; |
| 737 | } |
Jan Kiszka | 0ca3a01 | 2010-02-08 10:12:14 +0000 | [diff] [blame] | 738 | |
| 739 | mutex_lock(&capi_controller_lock); |
| 740 | |
Jan Kiszka | 5225303 | 2010-02-08 10:12:10 +0000 | [diff] [blame] | 741 | ctr = get_capi_ctr_by_nr(contr); |
Jan Kiszka | 0ca3a01 | 2010-02-08 10:12:14 +0000 | [diff] [blame] | 742 | if (ctr && ctr->state == CAPI_CTR_RUNNING) { |
Eric Dumazet | d63967e | 2019-01-02 09:20:27 -0800 | [diff] [blame] | 743 | strncpy(buf, ctr->manu, CAPI_MANUFACTURER_LEN); |
Jan Kiszka | 0ca3a01 | 2010-02-08 10:12:14 +0000 | [diff] [blame] | 744 | ret = CAPI_NOERROR; |
| 745 | } else |
| 746 | ret = CAPI_REGNOTINSTALLED; |
| 747 | |
| 748 | mutex_unlock(&capi_controller_lock); |
| 749 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 750 | } |
| 751 | |
Tilman Schmidt | 554f200 | 2009-04-23 02:24:21 +0000 | [diff] [blame] | 752 | /** |
| 753 | * capi20_get_version() - CAPI 2.0 operation CAPI_GET_VERSION |
| 754 | * @contr: controller number. |
| 755 | * @verp: result structure. |
| 756 | * |
| 757 | * Retrieve version information for the specified ISDN controller |
| 758 | * or (for @contr == 0) the driver itself. |
| 759 | * Return value: CAPI result code |
| 760 | */ |
| 761 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | u16 capi20_get_version(u32 contr, struct capi_version *verp) |
| 763 | { |
Jan Kiszka | 5225303 | 2010-02-08 10:12:10 +0000 | [diff] [blame] | 764 | struct capi_ctr *ctr; |
Jan Kiszka | 0ca3a01 | 2010-02-08 10:12:14 +0000 | [diff] [blame] | 765 | u16 ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 766 | |
| 767 | if (contr == 0) { |
| 768 | *verp = driver_version; |
| 769 | return CAPI_NOERROR; |
| 770 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 771 | |
Jan Kiszka | 0ca3a01 | 2010-02-08 10:12:14 +0000 | [diff] [blame] | 772 | mutex_lock(&capi_controller_lock); |
| 773 | |
| 774 | ctr = get_capi_ctr_by_nr(contr); |
| 775 | if (ctr && ctr->state == CAPI_CTR_RUNNING) { |
| 776 | memcpy(verp, &ctr->version, sizeof(capi_version)); |
| 777 | ret = CAPI_NOERROR; |
| 778 | } else |
| 779 | ret = CAPI_REGNOTINSTALLED; |
| 780 | |
| 781 | mutex_unlock(&capi_controller_lock); |
| 782 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 783 | } |
| 784 | |
Tilman Schmidt | 554f200 | 2009-04-23 02:24:21 +0000 | [diff] [blame] | 785 | /** |
| 786 | * capi20_get_serial() - CAPI 2.0 operation CAPI_GET_SERIAL_NUMBER |
| 787 | * @contr: controller number. |
| 788 | * @serial: result buffer (8 bytes). |
| 789 | * |
| 790 | * Retrieve the serial number of the specified ISDN controller |
| 791 | * or (for @contr == 0) the driver itself. |
| 792 | * Return value: CAPI result code |
| 793 | */ |
| 794 | |
Arnd Bergmann | 5ee7d4c | 2021-03-22 17:44:29 +0100 | [diff] [blame] | 795 | u16 capi20_get_serial(u32 contr, u8 serial[CAPI_SERIAL_LEN]) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 796 | { |
Jan Kiszka | 5225303 | 2010-02-08 10:12:10 +0000 | [diff] [blame] | 797 | struct capi_ctr *ctr; |
Jan Kiszka | 0ca3a01 | 2010-02-08 10:12:14 +0000 | [diff] [blame] | 798 | u16 ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 799 | |
| 800 | if (contr == 0) { |
| 801 | strlcpy(serial, driver_serial, CAPI_SERIAL_LEN); |
| 802 | return CAPI_NOERROR; |
| 803 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 804 | |
Jan Kiszka | 0ca3a01 | 2010-02-08 10:12:14 +0000 | [diff] [blame] | 805 | mutex_lock(&capi_controller_lock); |
| 806 | |
| 807 | ctr = get_capi_ctr_by_nr(contr); |
| 808 | if (ctr && ctr->state == CAPI_CTR_RUNNING) { |
| 809 | strlcpy(serial, ctr->serial, CAPI_SERIAL_LEN); |
| 810 | ret = CAPI_NOERROR; |
| 811 | } else |
| 812 | ret = CAPI_REGNOTINSTALLED; |
| 813 | |
| 814 | mutex_unlock(&capi_controller_lock); |
| 815 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 816 | } |
| 817 | |
Tilman Schmidt | 554f200 | 2009-04-23 02:24:21 +0000 | [diff] [blame] | 818 | /** |
| 819 | * capi20_get_profile() - CAPI 2.0 operation CAPI_GET_PROFILE |
| 820 | * @contr: controller number. |
| 821 | * @profp: result structure. |
| 822 | * |
| 823 | * Retrieve capability information for the specified ISDN controller |
| 824 | * or (for @contr == 0) the number of installed controllers. |
| 825 | * Return value: CAPI result code |
| 826 | */ |
| 827 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 828 | u16 capi20_get_profile(u32 contr, struct capi_profile *profp) |
| 829 | { |
Jan Kiszka | 5225303 | 2010-02-08 10:12:10 +0000 | [diff] [blame] | 830 | struct capi_ctr *ctr; |
Jan Kiszka | 0ca3a01 | 2010-02-08 10:12:14 +0000 | [diff] [blame] | 831 | u16 ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 832 | |
| 833 | if (contr == 0) { |
Jan Kiszka | 5225303 | 2010-02-08 10:12:10 +0000 | [diff] [blame] | 834 | profp->ncontroller = ncontrollers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 835 | return CAPI_NOERROR; |
| 836 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 837 | |
Jan Kiszka | 0ca3a01 | 2010-02-08 10:12:14 +0000 | [diff] [blame] | 838 | mutex_lock(&capi_controller_lock); |
| 839 | |
| 840 | ctr = get_capi_ctr_by_nr(contr); |
| 841 | if (ctr && ctr->state == CAPI_CTR_RUNNING) { |
| 842 | memcpy(profp, &ctr->profile, sizeof(struct capi_profile)); |
| 843 | ret = CAPI_NOERROR; |
| 844 | } else |
| 845 | ret = CAPI_REGNOTINSTALLED; |
| 846 | |
| 847 | mutex_unlock(&capi_controller_lock); |
| 848 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 849 | } |
| 850 | |
Tilman Schmidt | 554f200 | 2009-04-23 02:24:21 +0000 | [diff] [blame] | 851 | /** |
| 852 | * capi20_manufacturer() - CAPI 2.0 operation CAPI_MANUFACTURER |
| 853 | * @cmd: command. |
| 854 | * @data: parameter. |
| 855 | * |
| 856 | * Perform manufacturer specific command. |
| 857 | * Return value: CAPI result code |
| 858 | */ |
| 859 | |
Tilman Schmidt | 9ea8aa8 | 2014-10-11 13:46:30 +0200 | [diff] [blame] | 860 | int capi20_manufacturer(unsigned long cmd, void __user *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 861 | { |
Jan Kiszka | 5225303 | 2010-02-08 10:12:10 +0000 | [diff] [blame] | 862 | struct capi_ctr *ctr; |
Jan Kiszka | 0ca3a01 | 2010-02-08 10:12:14 +0000 | [diff] [blame] | 863 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 864 | |
| 865 | switch (cmd) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 866 | case KCAPI_CMD_TRACE: |
| 867 | { |
| 868 | kcapi_flagdef fdef; |
| 869 | |
| 870 | if (copy_from_user(&fdef, data, sizeof(kcapi_flagdef))) |
| 871 | return -EFAULT; |
| 872 | |
Jan Kiszka | 0ca3a01 | 2010-02-08 10:12:14 +0000 | [diff] [blame] | 873 | mutex_lock(&capi_controller_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 874 | |
Jan Kiszka | 0ca3a01 | 2010-02-08 10:12:14 +0000 | [diff] [blame] | 875 | ctr = get_capi_ctr_by_nr(fdef.contr); |
| 876 | if (ctr) { |
| 877 | ctr->traceflag = fdef.flag; |
| 878 | printk(KERN_INFO "kcapi: contr [%03d] set trace=%d\n", |
| 879 | ctr->cnr, ctr->traceflag); |
| 880 | retval = 0; |
| 881 | } else |
| 882 | retval = -ESRCH; |
| 883 | |
| 884 | mutex_unlock(&capi_controller_lock); |
| 885 | |
| 886 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 887 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 888 | |
| 889 | default: |
Tilman Schmidt | 9ea8aa8 | 2014-10-11 13:46:30 +0200 | [diff] [blame] | 890 | printk(KERN_ERR "kcapi: manufacturer command %lu unknown.\n", |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 891 | cmd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 892 | break; |
| 893 | |
| 894 | } |
| 895 | return -EINVAL; |
| 896 | } |
| 897 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 898 | /* ------------------------------------------------------------- */ |
| 899 | /* -------- Init & Cleanup ------------------------------------- */ |
| 900 | /* ------------------------------------------------------------- */ |
| 901 | |
| 902 | /* |
| 903 | * init / exit functions |
| 904 | */ |
| 905 | |
Arnd Bergmann | f59aba2 | 2019-12-10 21:59:16 +0100 | [diff] [blame] | 906 | int __init kcapi_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 907 | { |
Jan Kiszka | 88549d6 | 2010-02-08 10:12:09 +0000 | [diff] [blame] | 908 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 909 | |
Tejun Heo | 158fa67 | 2010-12-24 15:59:06 +0100 | [diff] [blame] | 910 | kcapi_wq = alloc_workqueue("kcapi", 0, 0); |
| 911 | if (!kcapi_wq) |
| 912 | return -ENOMEM; |
| 913 | |
Jan Kiszka | 88549d6 | 2010-02-08 10:12:09 +0000 | [diff] [blame] | 914 | err = cdebug_init(); |
Tejun Heo | 158fa67 | 2010-12-24 15:59:06 +0100 | [diff] [blame] | 915 | if (err) { |
Tejun Heo | 158fa67 | 2010-12-24 15:59:06 +0100 | [diff] [blame] | 916 | destroy_workqueue(kcapi_wq); |
| 917 | return err; |
| 918 | } |
| 919 | |
| 920 | kcapi_proc_init(); |
| 921 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 922 | } |
| 923 | |
Arnd Bergmann | f59aba2 | 2019-12-10 21:59:16 +0100 | [diff] [blame] | 924 | void kcapi_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 925 | { |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 926 | kcapi_proc_exit(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 927 | |
Karsten Keil | 17f0cd2 | 2007-02-28 20:13:50 -0800 | [diff] [blame] | 928 | cdebug_exit(); |
Tejun Heo | 158fa67 | 2010-12-24 15:59:06 +0100 | [diff] [blame] | 929 | destroy_workqueue(kcapi_wq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 930 | } |