blob: cb0afe8971623668b232173e38dc7aad63d887c4 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* $Id: kcapi.c,v 1.1.2.8 2004/03/26 19:57:20 armin Exp $
Joe Perches475be4d2012-02-19 19:52:38 -08002 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Kernel CAPI 2.0 Module
Joe Perches475be4d2012-02-19 19:52:38 -08004 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Copyright 1999 by Carsten Paeth <calle@calle.de>
6 * Copyright 2002 by Kai Germaschewski <kai@germaschewski.name>
Joe Perches475be4d2012-02-19 19:52:38 -08007 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * 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 Torvalds1da177e2005-04-16 15:20:36 -070013#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 Molnar174cd4b2017-02-02 19:15:33 +010019#include <linux/sched/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#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 Heo5a0e3ad2010-03-24 17:04:11 +090028#include <linux/slab.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080029#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/isdn/capicmd.h>
31#include <linux/isdn/capiutil.h>
Arjan van de Ven9cdf1822006-03-23 03:00:21 -080032#include <linux/mutex.h>
Jan Kiszka88c896e2010-02-08 10:12:15 +000033#include <linux/rcupdate.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Linus Torvalds1da177e2005-04-16 15:20:36 -070035static int showcapimsgs = 0;
Tejun Heo158fa672010-12-24 15:59:06 +010036static struct workqueue_struct *kcapi_wq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Linus Torvalds1da177e2005-04-16 15:20:36 -070038module_param(showcapimsgs, uint, 0);
39
40/* ------------------------------------------------------------- */
41
Jan Kiszkaef69bb22010-02-08 10:12:13 +000042struct capictr_event {
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 struct work_struct work;
Jan Kiszkaef69bb22010-02-08 10:12:13 +000044 unsigned int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 u32 controller;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046};
47
48/* ------------------------------------------------------------- */
49
Bhumika Goyal733a7072017-08-06 22:39:06 +053050static const struct capi_version driver_version = {2, 0, 1, 1 << 4};
Linus Torvalds1da177e2005-04-16 15:20:36 -070051static char driver_serial[CAPI_SERIAL_LEN] = "0004711";
52static char capi_manufakturer[64] = "AVM Berlin";
53
54#define NCCI2CTRL(ncci) (((ncci) >> 24) & 0x7f)
55
Jan Kiszka0ca3a012010-02-08 10:12:14 +000056struct capi_ctr *capi_controller[CAPI_MAXCONTR];
57DEFINE_MUTEX(capi_controller_lock);
58
Linus Torvalds1da177e2005-04-16 15:20:36 -070059struct capi20_appl *capi_applications[CAPI_MAXAPPL];
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Jan Kiszka52253032010-02-08 10:12:10 +000061static int ncontrollers;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63/* -------- controller ref counting -------------------------------------- */
64
65static inline struct capi_ctr *
Jan Kiszka52253032010-02-08 10:12:10 +000066capi_ctr_get(struct capi_ctr *ctr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070067{
Jan Kiszka52253032010-02-08 10:12:10 +000068 if (!try_module_get(ctr->owner))
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 return NULL;
Jan Kiszka52253032010-02-08 10:12:10 +000070 return ctr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071}
72
73static inline void
Jan Kiszka52253032010-02-08 10:12:10 +000074capi_ctr_put(struct capi_ctr *ctr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075{
Jan Kiszka52253032010-02-08 10:12:10 +000076 module_put(ctr->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077}
78
79/* ------------------------------------------------------------- */
80
81static inline struct capi_ctr *get_capi_ctr_by_nr(u16 contr)
82{
Dan Carpenter25dff942013-05-19 08:36:36 +000083 if (contr < 1 || contr - 1 >= CAPI_MAXCONTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 return NULL;
85
Jan Kiszka52253032010-02-08 10:12:10 +000086 return capi_controller[contr - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -070087}
88
Jan Kiszkab003f4e2010-10-17 05:18:15 +000089static inline struct capi20_appl *__get_capi_appl_by_nr(u16 applid)
90{
91 lockdep_assert_held(&capi_controller_lock);
92
Dan Carpenter25dff942013-05-19 08:36:36 +000093 if (applid < 1 || applid - 1 >= CAPI_MAXAPPL)
Jan Kiszkab003f4e2010-10-17 05:18:15 +000094 return NULL;
95
96 return capi_applications[applid - 1];
97}
98
Linus Torvalds1da177e2005-04-16 15:20:36 -070099static inline struct capi20_appl *get_capi_appl_by_nr(u16 applid)
100{
Dan Carpenter25dff942013-05-19 08:36:36 +0000101 if (applid < 1 || applid - 1 >= CAPI_MAXAPPL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 return NULL;
103
Jan Kiszka88c896e2010-02-08 10:12:15 +0000104 return rcu_dereference(capi_applications[applid - 1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105}
106
107/* -------- util functions ------------------------------------ */
108
109static 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
132static 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 Kiszka52253032010-02-08 10:12:10 +0000146static void
147register_appl(struct capi_ctr *ctr, u16 applid, capi_register_params *rparam)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148{
Jan Kiszka52253032010-02-08 10:12:10 +0000149 ctr = capi_ctr_get(ctr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
Jan Kiszka52253032010-02-08 10:12:10 +0000151 if (ctr)
152 ctr->register_appl(ctr, applid, rparam);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 else
Jan Kiszka52253032010-02-08 10:12:10 +0000154 printk(KERN_WARNING "%s: cannot get controller resources\n",
155 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156}
157
158
Jan Kiszka52253032010-02-08 10:12:10 +0000159static void release_appl(struct capi_ctr *ctr, u16 applid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160{
161 DBG("applid %#x", applid);
Joe Perches475be4d2012-02-19 19:52:38 -0800162
Jan Kiszka52253032010-02-08 10:12:10 +0000163 ctr->release_appl(ctr, applid);
164 capi_ctr_put(ctr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165}
166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167static void notify_up(u32 contr)
168{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 struct capi20_appl *ap;
Jan Kiszka3efecf72010-02-08 10:12:12 +0000170 struct capi_ctr *ctr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 u16 applid;
172
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000173 mutex_lock(&capi_controller_lock);
174
Jan Kiszka3efecf72010-02-08 10:12:12 +0000175 if (showcapimsgs & 1)
Joe Perches475be4d2012-02-19 19:52:38 -0800176 printk(KERN_DEBUG "kcapi: notify up contr %d\n", contr);
Jan Kiszka3efecf72010-02-08 10:12:12 +0000177
178 ctr = get_capi_ctr_by_nr(contr);
179 if (ctr) {
180 if (ctr->state == CAPI_CTR_RUNNING)
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000181 goto unlock_out;
Jan Kiszka3efecf72010-02-08 10:12:12 +0000182
183 ctr->state = CAPI_CTR_RUNNING;
184
185 for (applid = 1; applid <= CAPI_MAXAPPL; applid++) {
Jan Kiszkab003f4e2010-10-17 05:18:15 +0000186 ap = __get_capi_appl_by_nr(applid);
187 if (ap)
188 register_appl(ctr, applid, &ap->rparam);
Jan Kiszka3efecf72010-02-08 10:12:12 +0000189 }
190 } else
Harvey Harrison156f1ed2008-04-28 02:14:40 -0700191 printk(KERN_WARNING "%s: invalid contr %d\n", __func__, contr);
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000192
193unlock_out:
194 mutex_unlock(&capi_controller_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195}
196
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000197static void ctr_down(struct capi_ctr *ctr, int new_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198{
199 struct capi20_appl *ap;
200 u16 applid;
201
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000202 if (ctr->state == CAPI_CTR_DETECTED || ctr->state == CAPI_CTR_DETACHED)
Jan Kiszka3efecf72010-02-08 10:12:12 +0000203 return;
204
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000205 ctr->state = new_state;
Jan Kiszka3efecf72010-02-08 10:12:12 +0000206
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 Torvalds1da177e2005-04-16 15:20:36 -0700211
212 for (applid = 1; applid <= CAPI_MAXAPPL; applid++) {
Jan Kiszkab003f4e2010-10-17 05:18:15 +0000213 ap = __get_capi_appl_by_nr(applid);
Jan Kiszka88c896e2010-02-08 10:12:15 +0000214 if (ap)
Jan Kiszka3efecf72010-02-08 10:12:12 +0000215 capi_ctr_put(ctr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 }
217}
218
Jan Kiszka3efecf72010-02-08 10:12:12 +0000219static void notify_down(u32 contr)
220{
221 struct capi_ctr *ctr;
222
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000223 mutex_lock(&capi_controller_lock);
224
Jan Kiszka3efecf72010-02-08 10:12:12 +0000225 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 Kiszka0ca3a012010-02-08 10:12:14 +0000230 ctr_down(ctr, CAPI_CTR_DETECTED);
Jan Kiszka3efecf72010-02-08 10:12:12 +0000231 else
232 printk(KERN_WARNING "%s: invalid contr %d\n", __func__, contr);
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000233
234 mutex_unlock(&capi_controller_lock);
Jan Kiszka3efecf72010-02-08 10:12:12 +0000235}
236
Jan Kiszkaef69bb22010-02-08 10:12:13 +0000237static void do_notify_work(struct work_struct *work)
238{
239 struct capictr_event *event =
240 container_of(work, struct capictr_event, work);
241
Arnd Bergmannf59aba22019-12-10 21:59:16 +0100242 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 Kiszkaef69bb22010-02-08 10:12:13 +0000251 kfree(event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252}
253
Jan Kiszkaef69bb22010-02-08 10:12:13 +0000254static int notify_push(unsigned int event_type, u32 controller)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255{
Jan Kiszkaef69bb22010-02-08 10:12:13 +0000256 struct capictr_event *event = kmalloc(sizeof(*event), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
Jan Kiszkaef69bb22010-02-08 10:12:13 +0000258 if (!event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 return -ENOMEM;
260
Jan Kiszkaef69bb22010-02-08 10:12:13 +0000261 INIT_WORK(&event->work, do_notify_work);
262 event->type = event_type;
263 event->controller = controller;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
Tejun Heo158fa672010-12-24 15:59:06 +0100265 queue_work(kcapi_wq, &event->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 return 0;
267}
268
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269/* -------- Receiver ------------------------------------------ */
270
David Howellsc4028952006-11-22 14:57:56 +0000271static void recv_handler(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272{
273 struct sk_buff *skb;
David Howellsc4028952006-11-22 14:57:56 +0000274 struct capi20_appl *ap =
275 container_of(work, struct capi20_appl, recv_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
277 if ((!ap) || (ap->release_in_progress))
278 return;
279
Matthias Kaehlcke67837f22007-07-17 04:04:16 -0700280 mutex_lock(&ap->recv_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 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 Kaehlcke67837f22007-07-17 04:04:16 -0700289 mutex_unlock(&ap->recv_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290}
291
Tilman Schmidt554f2002009-04-23 02:24:21 +0000292/**
293 * capi_ctr_handle_message() - handle incoming CAPI message
Jan Kiszka52253032010-02-08 10:12:10 +0000294 * @ctr: controller descriptor structure.
Tilman Schmidt554f2002009-04-23 02:24:21 +0000295 * @appl: application ID.
296 * @skb: message.
297 *
298 * Called by hardware driver to pass a CAPI message to the application.
299 */
300
Jan Kiszka52253032010-02-08 10:12:10 +0000301void capi_ctr_handle_message(struct capi_ctr *ctr, u16 appl,
302 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303{
304 struct capi20_appl *ap;
305 int showctl = 0;
306 u8 cmd, subcmd;
Karsten Keil17f0cd22007-02-28 20:13:50 -0800307 _cdebbuf *cdb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
Jan Kiszka52253032010-02-08 10:12:10 +0000309 if (ctr->state != CAPI_CTR_RUNNING) {
Karsten Keil17f0cd22007-02-28 20:13:50 -0800310 cdb = capi_message2str(skb->data);
311 if (cdb) {
312 printk(KERN_INFO "kcapi: controller [%03d] not active, got: %s",
Joe Perches475be4d2012-02-19 19:52:38 -0800313 ctr->cnr, cdb->buf);
Karsten Keil17f0cd22007-02-28 20:13:50 -0800314 cdebbuf_free(cdb);
315 } else
316 printk(KERN_INFO "kcapi: controller [%03d] not active, cannot trace\n",
Joe Perches475be4d2012-02-19 19:52:38 -0800317 ctr->cnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 goto error;
319 }
320
321 cmd = CAPIMSG_COMMAND(skb->data);
Joe Perches475be4d2012-02-19 19:52:38 -0800322 subcmd = CAPIMSG_SUBCOMMAND(skb->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 if (cmd == CAPI_DATA_B3 && subcmd == CAPI_IND) {
Jan Kiszka52253032010-02-08 10:12:10 +0000324 ctr->nrecvdatapkt++;
325 if (ctr->traceflag > 2)
326 showctl |= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 } else {
Jan Kiszka52253032010-02-08 10:12:10 +0000328 ctr->nrecvctlpkt++;
329 if (ctr->traceflag)
330 showctl |= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 }
Jan Kiszka52253032010-02-08 10:12:10 +0000332 showctl |= (ctr->traceflag & 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 if (showctl & 2) {
334 if (showctl & 1) {
Karsten Keil17f0cd22007-02-28 20:13:50 -0800335 printk(KERN_DEBUG "kcapi: got [%03d] id#%d %s len=%u\n",
Jan Kiszka52253032010-02-08 10:12:10 +0000336 ctr->cnr, CAPIMSG_APPID(skb->data),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 capi_cmd2str(cmd, subcmd),
338 CAPIMSG_LEN(skb->data));
339 } else {
Karsten Keil17f0cd22007-02-28 20:13:50 -0800340 cdb = capi_message2str(skb->data);
341 if (cdb) {
342 printk(KERN_DEBUG "kcapi: got [%03d] %s\n",
Joe Perches475be4d2012-02-19 19:52:38 -0800343 ctr->cnr, cdb->buf);
Karsten Keil17f0cd22007-02-28 20:13:50 -0800344 cdebbuf_free(cdb);
345 } else
346 printk(KERN_DEBUG "kcapi: got [%03d] id#%d %s len=%u, cannot trace\n",
Joe Perches475be4d2012-02-19 19:52:38 -0800347 ctr->cnr, CAPIMSG_APPID(skb->data),
348 capi_cmd2str(cmd, subcmd),
349 CAPIMSG_LEN(skb->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 }
351
352 }
353
Jan Kiszka88c896e2010-02-08 10:12:15 +0000354 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 ap = get_capi_appl_by_nr(CAPIMSG_APPID(skb->data));
Jan Kiszka88c896e2010-02-08 10:12:15 +0000356 if (!ap) {
357 rcu_read_unlock();
Karsten Keil17f0cd22007-02-28 20:13:50 -0800358 cdb = capi_message2str(skb->data);
359 if (cdb) {
360 printk(KERN_ERR "kcapi: handle_message: applid %d state released (%s)\n",
Joe Perches475be4d2012-02-19 19:52:38 -0800361 CAPIMSG_APPID(skb->data), cdb->buf);
Karsten Keil17f0cd22007-02-28 20:13:50 -0800362 cdebbuf_free(cdb);
363 } else
364 printk(KERN_ERR "kcapi: handle_message: applid %d state released (%s) cannot trace\n",
Joe Perches475be4d2012-02-19 19:52:38 -0800365 CAPIMSG_APPID(skb->data),
366 capi_cmd2str(cmd, subcmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 goto error;
368 }
369 skb_queue_tail(&ap->recv_queue, skb);
Tejun Heo158fa672010-12-24 15:59:06 +0100370 queue_work(kcapi_wq, &ap->recv_work);
Jan Kiszka88c896e2010-02-08 10:12:15 +0000371 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
373 return;
374
375error:
376 kfree_skb(skb);
377}
378
379EXPORT_SYMBOL(capi_ctr_handle_message);
380
Tilman Schmidt554f2002009-04-23 02:24:21 +0000381/**
382 * capi_ctr_ready() - signal CAPI controller ready
Jan Kiszka52253032010-02-08 10:12:10 +0000383 * @ctr: controller descriptor structure.
Tilman Schmidt554f2002009-04-23 02:24:21 +0000384 *
385 * Called by hardware driver to signal that the controller is up and running.
386 */
387
Jan Kiszka52253032010-02-08 10:12:10 +0000388void capi_ctr_ready(struct capi_ctr *ctr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389{
Jan Kiszka52253032010-02-08 10:12:10 +0000390 printk(KERN_NOTICE "kcapi: controller [%03d] \"%s\" ready.\n",
391 ctr->cnr, ctr->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
Jan Kiszkaef69bb22010-02-08 10:12:13 +0000393 notify_push(CAPICTR_UP, ctr->cnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394}
395
396EXPORT_SYMBOL(capi_ctr_ready);
397
Tilman Schmidt554f2002009-04-23 02:24:21 +0000398/**
Tilman Schmidt4e329972009-06-07 09:09:23 +0000399 * capi_ctr_down() - signal CAPI controller not ready
Jan Kiszka52253032010-02-08 10:12:10 +0000400 * @ctr: controller descriptor structure.
Tilman Schmidt554f2002009-04-23 02:24:21 +0000401 *
402 * Called by hardware driver to signal that the controller is down and
403 * unavailable for use.
404 */
405
Jan Kiszka52253032010-02-08 10:12:10 +0000406void capi_ctr_down(struct capi_ctr *ctr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407{
Jan Kiszka52253032010-02-08 10:12:10 +0000408 printk(KERN_NOTICE "kcapi: controller [%03d] down.\n", ctr->cnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
Jan Kiszkaef69bb22010-02-08 10:12:13 +0000410 notify_push(CAPICTR_DOWN, ctr->cnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411}
412
Tilman Schmidt4e329972009-06-07 09:09:23 +0000413EXPORT_SYMBOL(capi_ctr_down);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415/* ------------------------------------------------------------- */
416
Tilman Schmidt554f2002009-04-23 02:24:21 +0000417/**
418 * attach_capi_ctr() - register CAPI controller
Jan Kiszka52253032010-02-08 10:12:10 +0000419 * @ctr: controller descriptor structure.
Tilman Schmidt554f2002009-04-23 02:24:21 +0000420 *
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 Kiszka52253032010-02-08 10:12:10 +0000425int attach_capi_ctr(struct capi_ctr *ctr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426{
427 int i;
428
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000429 mutex_lock(&capi_controller_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
431 for (i = 0; i < CAPI_MAXCONTR; i++) {
Jan Kiszka52253032010-02-08 10:12:10 +0000432 if (!capi_controller[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 break;
434 }
435 if (i == CAPI_MAXCONTR) {
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000436 mutex_unlock(&capi_controller_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 printk(KERN_ERR "kcapi: out of controller slots\n");
Joe Perches475be4d2012-02-19 19:52:38 -0800438 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 }
Jan Kiszka52253032010-02-08 10:12:10 +0000440 capi_controller[i] = ctr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
Jan Kiszka52253032010-02-08 10:12:10 +0000442 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 Torvalds1da177e2005-04-16 15:20:36 -0700450
Jan Kiszka52253032010-02-08 10:12:10 +0000451 sprintf(ctr->procfn, "capi/controllers/%d", ctr->cnr);
Christoph Hellwig2cd1f0d2018-04-11 18:39:29 +0200452 ctr->procent = proc_create_single_data(ctr->procfn, 0, NULL,
453 ctr->proc_show, ctr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
Jan Kiszka52253032010-02-08 10:12:10 +0000455 ncontrollers++;
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000456
457 mutex_unlock(&capi_controller_lock);
458
Jan Kiszka52253032010-02-08 10:12:10 +0000459 printk(KERN_NOTICE "kcapi: controller [%03d]: %s attached\n",
Joe Perches475be4d2012-02-19 19:52:38 -0800460 ctr->cnr, ctr->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 return 0;
462}
463
464EXPORT_SYMBOL(attach_capi_ctr);
465
Tilman Schmidt554f2002009-04-23 02:24:21 +0000466/**
467 * detach_capi_ctr() - unregister CAPI controller
Jan Kiszka52253032010-02-08 10:12:10 +0000468 * @ctr: controller descriptor structure.
Tilman Schmidt554f2002009-04-23 02:24:21 +0000469 *
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 Kiszka52253032010-02-08 10:12:10 +0000475int detach_capi_ctr(struct capi_ctr *ctr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476{
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000477 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000479 mutex_lock(&capi_controller_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000481 ctr_down(ctr, CAPI_CTR_DETACHED);
482
483 if (capi_controller[ctr->cnr - 1] != ctr) {
484 err = -EINVAL;
485 goto unlock_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 }
Jan Kiszka52253032010-02-08 10:12:10 +0000487 capi_controller[ctr->cnr - 1] = NULL;
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000488 ncontrollers--;
489
490 if (ctr->procent)
491 remove_proc_entry(ctr->procfn, NULL);
492
Jan Kiszka52253032010-02-08 10:12:10 +0000493 printk(KERN_NOTICE "kcapi: controller [%03d]: %s unregistered\n",
494 ctr->cnr, ctr->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000496unlock_out:
497 mutex_unlock(&capi_controller_lock);
498
499 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500}
501
502EXPORT_SYMBOL(detach_capi_ctr);
503
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504/* ------------------------------------------------------------- */
505/* -------- CAPI2.0 Interface ---------------------------------- */
506/* ------------------------------------------------------------- */
507
Tilman Schmidt554f2002009-04-23 02:24:21 +0000508/**
509 * capi20_isinstalled() - CAPI 2.0 operation CAPI_INSTALLED
510 *
511 * Return value: CAPI result code (CAPI_NOERROR if at least one ISDN controller
512 * is ready for use, CAPI_REGNOTINSTALLED otherwise)
513 */
514
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515u16 capi20_isinstalled(void)
516{
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000517 u16 ret = CAPI_REGNOTINSTALLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 int i;
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000519
520 mutex_lock(&capi_controller_lock);
521
522 for (i = 0; i < CAPI_MAXCONTR; i++)
Jan Kiszka52253032010-02-08 10:12:10 +0000523 if (capi_controller[i] &&
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000524 capi_controller[i]->state == CAPI_CTR_RUNNING) {
525 ret = CAPI_NOERROR;
526 break;
527 }
528
529 mutex_unlock(&capi_controller_lock);
530
531 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532}
533
Tilman Schmidt554f2002009-04-23 02:24:21 +0000534/**
535 * capi20_register() - CAPI 2.0 operation CAPI_REGISTER
536 * @ap: CAPI application descriptor structure.
537 *
538 * Register an application's presence with CAPI.
539 * A unique application ID is assigned and stored in @ap->applid.
540 * After this function returns successfully, the message receive
541 * callback function @ap->recv_message() may be called at any time
542 * until capi20_release() has been called for the same @ap.
543 * Return value: CAPI result code
544 */
545
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546u16 capi20_register(struct capi20_appl *ap)
547{
548 int i;
549 u16 applid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
551 DBG("");
552
553 if (ap->rparam.datablklen < 128)
554 return CAPI_LOGBLKSIZETOSMALL;
555
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 ap->nrecvctlpkt = 0;
557 ap->nrecvdatapkt = 0;
558 ap->nsentctlpkt = 0;
559 ap->nsentdatapkt = 0;
Matthias Kaehlcke67837f22007-07-17 04:04:16 -0700560 mutex_init(&ap->recv_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 skb_queue_head_init(&ap->recv_queue);
David Howellsc4028952006-11-22 14:57:56 +0000562 INIT_WORK(&ap->recv_work, recv_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 ap->release_in_progress = 0;
564
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000565 mutex_lock(&capi_controller_lock);
566
Jan Kiszka88c896e2010-02-08 10:12:15 +0000567 for (applid = 1; applid <= CAPI_MAXAPPL; applid++) {
568 if (capi_applications[applid - 1] == NULL)
569 break;
570 }
571 if (applid > CAPI_MAXAPPL) {
572 mutex_unlock(&capi_controller_lock);
573 return CAPI_TOOMANYAPPLS;
574 }
575
576 ap->applid = applid;
577 capi_applications[applid - 1] = ap;
578
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 for (i = 0; i < CAPI_MAXCONTR; i++) {
Jan Kiszka52253032010-02-08 10:12:10 +0000580 if (!capi_controller[i] ||
581 capi_controller[i]->state != CAPI_CTR_RUNNING)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 continue;
Jan Kiszka52253032010-02-08 10:12:10 +0000583 register_appl(capi_controller[i], applid, &ap->rparam);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 }
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000585
586 mutex_unlock(&capi_controller_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
588 if (showcapimsgs & 1) {
589 printk(KERN_DEBUG "kcapi: appl %d up\n", applid);
590 }
591
592 return CAPI_NOERROR;
593}
594
Tilman Schmidt554f2002009-04-23 02:24:21 +0000595/**
596 * capi20_release() - CAPI 2.0 operation CAPI_RELEASE
597 * @ap: CAPI application descriptor structure.
598 *
599 * Terminate an application's registration with CAPI.
600 * After this function returns successfully, the message receive
601 * callback function @ap->recv_message() will no longer be called.
602 * Return value: CAPI result code
603 */
604
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605u16 capi20_release(struct capi20_appl *ap)
606{
607 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
609 DBG("applid %#x", ap->applid);
610
Jan Kiszka88c896e2010-02-08 10:12:15 +0000611 mutex_lock(&capi_controller_lock);
612
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 ap->release_in_progress = 1;
614 capi_applications[ap->applid - 1] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
Jan Kiszka88c896e2010-02-08 10:12:15 +0000616 synchronize_rcu();
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000617
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 for (i = 0; i < CAPI_MAXCONTR; i++) {
Jan Kiszka52253032010-02-08 10:12:10 +0000619 if (!capi_controller[i] ||
620 capi_controller[i]->state != CAPI_CTR_RUNNING)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 continue;
Jan Kiszka52253032010-02-08 10:12:10 +0000622 release_appl(capi_controller[i], ap->applid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 }
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000624
625 mutex_unlock(&capi_controller_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
Tejun Heo158fa672010-12-24 15:59:06 +0100627 flush_workqueue(kcapi_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 skb_queue_purge(&ap->recv_queue);
629
630 if (showcapimsgs & 1) {
631 printk(KERN_DEBUG "kcapi: appl %d down\n", ap->applid);
632 }
633
634 return CAPI_NOERROR;
635}
636
Tilman Schmidt554f2002009-04-23 02:24:21 +0000637/**
638 * capi20_put_message() - CAPI 2.0 operation CAPI_PUT_MESSAGE
639 * @ap: CAPI application descriptor structure.
640 * @skb: CAPI message.
641 *
642 * Transfer a single message to CAPI.
643 * Return value: CAPI result code
644 */
645
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646u16 capi20_put_message(struct capi20_appl *ap, struct sk_buff *skb)
647{
Jan Kiszka52253032010-02-08 10:12:10 +0000648 struct capi_ctr *ctr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 int showctl = 0;
650 u8 cmd, subcmd;
651
652 DBG("applid %#x", ap->applid);
Joe Perches475be4d2012-02-19 19:52:38 -0800653
Jan Kiszka52253032010-02-08 10:12:10 +0000654 if (ncontrollers == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 return CAPI_REGNOTINSTALLED;
656 if ((ap->applid == 0) || ap->release_in_progress)
657 return CAPI_ILLAPPNR;
658 if (skb->len < 12
659 || !capi_cmd_valid(CAPIMSG_COMMAND(skb->data))
660 || !capi_subcmd_valid(CAPIMSG_SUBCOMMAND(skb->data)))
661 return CAPI_ILLCMDORSUBCMDORMSGTOSMALL;
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000662
663 /*
664 * The controller reference is protected by the existence of the
665 * application passed to us. We assume that the caller properly
666 * synchronizes this service with capi20_release.
667 */
Jan Kiszka52253032010-02-08 10:12:10 +0000668 ctr = get_capi_ctr_by_nr(CAPIMSG_CONTROLLER(skb->data));
Jan Kiszkac6af0432010-02-08 10:12:43 +0000669 if (!ctr || ctr->state != CAPI_CTR_RUNNING)
670 return CAPI_REGNOTINSTALLED;
Jan Kiszka52253032010-02-08 10:12:10 +0000671 if (ctr->blocked)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 return CAPI_SENDQUEUEFULL;
673
674 cmd = CAPIMSG_COMMAND(skb->data);
Joe Perches475be4d2012-02-19 19:52:38 -0800675 subcmd = CAPIMSG_SUBCOMMAND(skb->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
Joe Perches475be4d2012-02-19 19:52:38 -0800677 if (cmd == CAPI_DATA_B3 && subcmd == CAPI_REQ) {
Jan Kiszka52253032010-02-08 10:12:10 +0000678 ctr->nsentdatapkt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 ap->nsentdatapkt++;
Jan Kiszka52253032010-02-08 10:12:10 +0000680 if (ctr->traceflag > 2)
681 showctl |= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 } else {
Jan Kiszka52253032010-02-08 10:12:10 +0000683 ctr->nsentctlpkt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 ap->nsentctlpkt++;
Jan Kiszka52253032010-02-08 10:12:10 +0000685 if (ctr->traceflag)
686 showctl |= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 }
Jan Kiszka52253032010-02-08 10:12:10 +0000688 showctl |= (ctr->traceflag & 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 if (showctl & 2) {
690 if (showctl & 1) {
Karsten Keil17f0cd22007-02-28 20:13:50 -0800691 printk(KERN_DEBUG "kcapi: put [%03d] id#%d %s len=%u\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 CAPIMSG_CONTROLLER(skb->data),
693 CAPIMSG_APPID(skb->data),
694 capi_cmd2str(cmd, subcmd),
695 CAPIMSG_LEN(skb->data));
696 } else {
Karsten Keil17f0cd22007-02-28 20:13:50 -0800697 _cdebbuf *cdb = capi_message2str(skb->data);
698 if (cdb) {
699 printk(KERN_DEBUG "kcapi: put [%03d] %s\n",
Joe Perches475be4d2012-02-19 19:52:38 -0800700 CAPIMSG_CONTROLLER(skb->data),
701 cdb->buf);
Karsten Keil17f0cd22007-02-28 20:13:50 -0800702 cdebbuf_free(cdb);
703 } else
704 printk(KERN_DEBUG "kcapi: put [%03d] id#%d %s len=%u cannot trace\n",
Joe Perches475be4d2012-02-19 19:52:38 -0800705 CAPIMSG_CONTROLLER(skb->data),
706 CAPIMSG_APPID(skb->data),
707 capi_cmd2str(cmd, subcmd),
708 CAPIMSG_LEN(skb->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 }
Jan Kiszka52253032010-02-08 10:12:10 +0000711 return ctr->send_message(ctr, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712}
713
Tilman Schmidt554f2002009-04-23 02:24:21 +0000714/**
715 * capi20_get_manufacturer() - CAPI 2.0 operation CAPI_GET_MANUFACTURER
716 * @contr: controller number.
717 * @buf: result buffer (64 bytes).
718 *
719 * Retrieve information about the manufacturer of the specified ISDN controller
720 * or (for @contr == 0) the driver itself.
721 * Return value: CAPI result code
722 */
723
Arnd Bergmann5ee7d4c2021-03-22 17:44:29 +0100724u16 capi20_get_manufacturer(u32 contr, u8 buf[CAPI_MANUFACTURER_LEN])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725{
Jan Kiszka52253032010-02-08 10:12:10 +0000726 struct capi_ctr *ctr;
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000727 u16 ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
729 if (contr == 0) {
Eric Dumazetd63967e2019-01-02 09:20:27 -0800730 strncpy(buf, capi_manufakturer, CAPI_MANUFACTURER_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 return CAPI_NOERROR;
732 }
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000733
734 mutex_lock(&capi_controller_lock);
735
Jan Kiszka52253032010-02-08 10:12:10 +0000736 ctr = get_capi_ctr_by_nr(contr);
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000737 if (ctr && ctr->state == CAPI_CTR_RUNNING) {
Eric Dumazetd63967e2019-01-02 09:20:27 -0800738 strncpy(buf, ctr->manu, CAPI_MANUFACTURER_LEN);
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000739 ret = CAPI_NOERROR;
740 } else
741 ret = CAPI_REGNOTINSTALLED;
742
743 mutex_unlock(&capi_controller_lock);
744 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745}
746
Tilman Schmidt554f2002009-04-23 02:24:21 +0000747/**
748 * capi20_get_version() - CAPI 2.0 operation CAPI_GET_VERSION
749 * @contr: controller number.
750 * @verp: result structure.
751 *
752 * Retrieve version information for the specified ISDN controller
753 * or (for @contr == 0) the driver itself.
754 * Return value: CAPI result code
755 */
756
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757u16 capi20_get_version(u32 contr, struct capi_version *verp)
758{
Jan Kiszka52253032010-02-08 10:12:10 +0000759 struct capi_ctr *ctr;
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000760 u16 ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
762 if (contr == 0) {
763 *verp = driver_version;
764 return CAPI_NOERROR;
765 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000767 mutex_lock(&capi_controller_lock);
768
769 ctr = get_capi_ctr_by_nr(contr);
770 if (ctr && ctr->state == CAPI_CTR_RUNNING) {
771 memcpy(verp, &ctr->version, sizeof(capi_version));
772 ret = CAPI_NOERROR;
773 } else
774 ret = CAPI_REGNOTINSTALLED;
775
776 mutex_unlock(&capi_controller_lock);
777 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778}
779
Tilman Schmidt554f2002009-04-23 02:24:21 +0000780/**
781 * capi20_get_serial() - CAPI 2.0 operation CAPI_GET_SERIAL_NUMBER
782 * @contr: controller number.
783 * @serial: result buffer (8 bytes).
784 *
785 * Retrieve the serial number of the specified ISDN controller
786 * or (for @contr == 0) the driver itself.
787 * Return value: CAPI result code
788 */
789
Arnd Bergmann5ee7d4c2021-03-22 17:44:29 +0100790u16 capi20_get_serial(u32 contr, u8 serial[CAPI_SERIAL_LEN])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791{
Jan Kiszka52253032010-02-08 10:12:10 +0000792 struct capi_ctr *ctr;
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000793 u16 ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
795 if (contr == 0) {
796 strlcpy(serial, driver_serial, CAPI_SERIAL_LEN);
797 return CAPI_NOERROR;
798 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000800 mutex_lock(&capi_controller_lock);
801
802 ctr = get_capi_ctr_by_nr(contr);
803 if (ctr && ctr->state == CAPI_CTR_RUNNING) {
804 strlcpy(serial, ctr->serial, CAPI_SERIAL_LEN);
805 ret = CAPI_NOERROR;
806 } else
807 ret = CAPI_REGNOTINSTALLED;
808
809 mutex_unlock(&capi_controller_lock);
810 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811}
812
Tilman Schmidt554f2002009-04-23 02:24:21 +0000813/**
814 * capi20_get_profile() - CAPI 2.0 operation CAPI_GET_PROFILE
815 * @contr: controller number.
816 * @profp: result structure.
817 *
818 * Retrieve capability information for the specified ISDN controller
819 * or (for @contr == 0) the number of installed controllers.
820 * Return value: CAPI result code
821 */
822
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823u16 capi20_get_profile(u32 contr, struct capi_profile *profp)
824{
Jan Kiszka52253032010-02-08 10:12:10 +0000825 struct capi_ctr *ctr;
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000826 u16 ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
828 if (contr == 0) {
Jan Kiszka52253032010-02-08 10:12:10 +0000829 profp->ncontroller = ncontrollers;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 return CAPI_NOERROR;
831 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000833 mutex_lock(&capi_controller_lock);
834
835 ctr = get_capi_ctr_by_nr(contr);
836 if (ctr && ctr->state == CAPI_CTR_RUNNING) {
837 memcpy(profp, &ctr->profile, sizeof(struct capi_profile));
838 ret = CAPI_NOERROR;
839 } else
840 ret = CAPI_REGNOTINSTALLED;
841
842 mutex_unlock(&capi_controller_lock);
843 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844}
845
Tilman Schmidt554f2002009-04-23 02:24:21 +0000846/**
847 * capi20_manufacturer() - CAPI 2.0 operation CAPI_MANUFACTURER
848 * @cmd: command.
849 * @data: parameter.
850 *
851 * Perform manufacturer specific command.
852 * Return value: CAPI result code
853 */
854
Tilman Schmidt9ea8aa82014-10-11 13:46:30 +0200855int capi20_manufacturer(unsigned long cmd, void __user *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856{
Jan Kiszka52253032010-02-08 10:12:10 +0000857 struct capi_ctr *ctr;
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000858 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859
860 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 case KCAPI_CMD_TRACE:
862 {
863 kcapi_flagdef fdef;
864
865 if (copy_from_user(&fdef, data, sizeof(kcapi_flagdef)))
866 return -EFAULT;
867
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000868 mutex_lock(&capi_controller_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000870 ctr = get_capi_ctr_by_nr(fdef.contr);
871 if (ctr) {
872 ctr->traceflag = fdef.flag;
873 printk(KERN_INFO "kcapi: contr [%03d] set trace=%d\n",
874 ctr->cnr, ctr->traceflag);
875 retval = 0;
876 } else
877 retval = -ESRCH;
878
879 mutex_unlock(&capi_controller_lock);
880
881 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
884 default:
Tilman Schmidt9ea8aa82014-10-11 13:46:30 +0200885 printk(KERN_ERR "kcapi: manufacturer command %lu unknown.\n",
Joe Perches475be4d2012-02-19 19:52:38 -0800886 cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 break;
888
889 }
890 return -EINVAL;
891}
892
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893/* ------------------------------------------------------------- */
894/* -------- Init & Cleanup ------------------------------------- */
895/* ------------------------------------------------------------- */
896
897/*
898 * init / exit functions
899 */
900
Arnd Bergmannf59aba22019-12-10 21:59:16 +0100901int __init kcapi_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902{
Jan Kiszka88549d62010-02-08 10:12:09 +0000903 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
Tejun Heo158fa672010-12-24 15:59:06 +0100905 kcapi_wq = alloc_workqueue("kcapi", 0, 0);
906 if (!kcapi_wq)
907 return -ENOMEM;
908
Jan Kiszka88549d62010-02-08 10:12:09 +0000909 err = cdebug_init();
Tejun Heo158fa672010-12-24 15:59:06 +0100910 if (err) {
Tejun Heo158fa672010-12-24 15:59:06 +0100911 destroy_workqueue(kcapi_wq);
912 return err;
913 }
914
915 kcapi_proc_init();
916 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917}
918
Arnd Bergmannf59aba22019-12-10 21:59:16 +0100919void kcapi_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920{
Joe Perches475be4d2012-02-19 19:52:38 -0800921 kcapi_proc_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
Karsten Keil17f0cd22007-02-28 20:13:50 -0800923 cdebug_exit();
Tejun Heo158fa672010-12-24 15:59:06 +0100924 destroy_workqueue(kcapi_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925}