blob: a4ceb61c5b6035fef835759d0761c733dea3fde8 [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
Robert P. J. Day37772ac2008-04-28 02:14:42 -070013#define AVMB1_COMPAT
Linus Torvalds1da177e2005-04-16 15:20:36 -070014
15#include "kcapi.h"
16#include <linux/module.h>
17#include <linux/mm.h>
18#include <linux/interrupt.h>
19#include <linux/ioport.h>
20#include <linux/proc_fs.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010021#include <linux/sched/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/seq_file.h>
23#include <linux/skbuff.h>
24#include <linux/workqueue.h>
25#include <linux/capi.h>
26#include <linux/kernelcapi.h>
27#include <linux/init.h>
28#include <linux/moduleparam.h>
29#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/slab.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080031#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/isdn/capicmd.h>
33#include <linux/isdn/capiutil.h>
Robert P. J. Day37772ac2008-04-28 02:14:42 -070034#ifdef AVMB1_COMPAT
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/b1lli.h>
36#endif
Arjan van de Ven9cdf1822006-03-23 03:00:21 -080037#include <linux/mutex.h>
Jan Kiszka88c896e2010-02-08 10:12:15 +000038#include <linux/rcupdate.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Linus Torvalds1da177e2005-04-16 15:20:36 -070040static int showcapimsgs = 0;
Tejun Heo158fa672010-12-24 15:59:06 +010041static struct workqueue_struct *kcapi_wq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43MODULE_DESCRIPTION("CAPI4Linux: kernel CAPI layer");
44MODULE_AUTHOR("Carsten Paeth");
45MODULE_LICENSE("GPL");
46module_param(showcapimsgs, uint, 0);
47
48/* ------------------------------------------------------------- */
49
Jan Kiszkaef69bb22010-02-08 10:12:13 +000050struct capictr_event {
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 struct work_struct work;
Jan Kiszkaef69bb22010-02-08 10:12:13 +000052 unsigned int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 u32 controller;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054};
55
56/* ------------------------------------------------------------- */
57
Bhumika Goyal733a7072017-08-06 22:39:06 +053058static const struct capi_version driver_version = {2, 0, 1, 1 << 4};
Linus Torvalds1da177e2005-04-16 15:20:36 -070059static char driver_serial[CAPI_SERIAL_LEN] = "0004711";
60static char capi_manufakturer[64] = "AVM Berlin";
61
62#define NCCI2CTRL(ncci) (((ncci) >> 24) & 0x7f)
63
64LIST_HEAD(capi_drivers);
Jan Kiszka9717fb82010-02-08 10:12:11 +000065DEFINE_MUTEX(capi_drivers_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Jan Kiszka0ca3a012010-02-08 10:12:14 +000067struct capi_ctr *capi_controller[CAPI_MAXCONTR];
68DEFINE_MUTEX(capi_controller_lock);
69
Linus Torvalds1da177e2005-04-16 15:20:36 -070070struct capi20_appl *capi_applications[CAPI_MAXAPPL];
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Jan Kiszka52253032010-02-08 10:12:10 +000072static int ncontrollers;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Jan Kiszkaef69bb22010-02-08 10:12:13 +000074static BLOCKING_NOTIFIER_HEAD(ctr_notifier_list);
75
Linus Torvalds1da177e2005-04-16 15:20:36 -070076/* -------- controller ref counting -------------------------------------- */
77
78static inline struct capi_ctr *
Jan Kiszka52253032010-02-08 10:12:10 +000079capi_ctr_get(struct capi_ctr *ctr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080{
Jan Kiszka52253032010-02-08 10:12:10 +000081 if (!try_module_get(ctr->owner))
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 return NULL;
Jan Kiszka52253032010-02-08 10:12:10 +000083 return ctr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084}
85
86static inline void
Jan Kiszka52253032010-02-08 10:12:10 +000087capi_ctr_put(struct capi_ctr *ctr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088{
Jan Kiszka52253032010-02-08 10:12:10 +000089 module_put(ctr->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090}
91
92/* ------------------------------------------------------------- */
93
94static inline struct capi_ctr *get_capi_ctr_by_nr(u16 contr)
95{
Dan Carpenter25dff942013-05-19 08:36:36 +000096 if (contr < 1 || contr - 1 >= CAPI_MAXCONTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 return NULL;
98
Jan Kiszka52253032010-02-08 10:12:10 +000099 return capi_controller[contr - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100}
101
Jan Kiszkab003f4e2010-10-17 05:18:15 +0000102static inline struct capi20_appl *__get_capi_appl_by_nr(u16 applid)
103{
104 lockdep_assert_held(&capi_controller_lock);
105
Dan Carpenter25dff942013-05-19 08:36:36 +0000106 if (applid < 1 || applid - 1 >= CAPI_MAXAPPL)
Jan Kiszkab003f4e2010-10-17 05:18:15 +0000107 return NULL;
108
109 return capi_applications[applid - 1];
110}
111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112static inline struct capi20_appl *get_capi_appl_by_nr(u16 applid)
113{
Dan Carpenter25dff942013-05-19 08:36:36 +0000114 if (applid < 1 || applid - 1 >= CAPI_MAXAPPL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 return NULL;
116
Jan Kiszka88c896e2010-02-08 10:12:15 +0000117 return rcu_dereference(capi_applications[applid - 1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118}
119
120/* -------- util functions ------------------------------------ */
121
122static inline int capi_cmd_valid(u8 cmd)
123{
124 switch (cmd) {
125 case CAPI_ALERT:
126 case CAPI_CONNECT:
127 case CAPI_CONNECT_ACTIVE:
128 case CAPI_CONNECT_B3_ACTIVE:
129 case CAPI_CONNECT_B3:
130 case CAPI_CONNECT_B3_T90_ACTIVE:
131 case CAPI_DATA_B3:
132 case CAPI_DISCONNECT_B3:
133 case CAPI_DISCONNECT:
134 case CAPI_FACILITY:
135 case CAPI_INFO:
136 case CAPI_LISTEN:
137 case CAPI_MANUFACTURER:
138 case CAPI_RESET_B3:
139 case CAPI_SELECT_B_PROTOCOL:
140 return 1;
141 }
142 return 0;
143}
144
145static inline int capi_subcmd_valid(u8 subcmd)
146{
147 switch (subcmd) {
148 case CAPI_REQ:
149 case CAPI_CONF:
150 case CAPI_IND:
151 case CAPI_RESP:
152 return 1;
153 }
154 return 0;
155}
156
157/* ------------------------------------------------------------ */
158
Jan Kiszka52253032010-02-08 10:12:10 +0000159static void
160register_appl(struct capi_ctr *ctr, u16 applid, capi_register_params *rparam)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161{
Jan Kiszka52253032010-02-08 10:12:10 +0000162 ctr = capi_ctr_get(ctr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Jan Kiszka52253032010-02-08 10:12:10 +0000164 if (ctr)
165 ctr->register_appl(ctr, applid, rparam);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 else
Jan Kiszka52253032010-02-08 10:12:10 +0000167 printk(KERN_WARNING "%s: cannot get controller resources\n",
168 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169}
170
171
Jan Kiszka52253032010-02-08 10:12:10 +0000172static void release_appl(struct capi_ctr *ctr, u16 applid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173{
174 DBG("applid %#x", applid);
Joe Perches475be4d2012-02-19 19:52:38 -0800175
Jan Kiszka52253032010-02-08 10:12:10 +0000176 ctr->release_appl(ctr, applid);
177 capi_ctr_put(ctr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178}
179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180static void notify_up(u32 contr)
181{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 struct capi20_appl *ap;
Jan Kiszka3efecf72010-02-08 10:12:12 +0000183 struct capi_ctr *ctr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 u16 applid;
185
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000186 mutex_lock(&capi_controller_lock);
187
Jan Kiszka3efecf72010-02-08 10:12:12 +0000188 if (showcapimsgs & 1)
Joe Perches475be4d2012-02-19 19:52:38 -0800189 printk(KERN_DEBUG "kcapi: notify up contr %d\n", contr);
Jan Kiszka3efecf72010-02-08 10:12:12 +0000190
191 ctr = get_capi_ctr_by_nr(contr);
192 if (ctr) {
193 if (ctr->state == CAPI_CTR_RUNNING)
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000194 goto unlock_out;
Jan Kiszka3efecf72010-02-08 10:12:12 +0000195
196 ctr->state = CAPI_CTR_RUNNING;
197
198 for (applid = 1; applid <= CAPI_MAXAPPL; applid++) {
Jan Kiszkab003f4e2010-10-17 05:18:15 +0000199 ap = __get_capi_appl_by_nr(applid);
200 if (ap)
201 register_appl(ctr, applid, &ap->rparam);
Jan Kiszka3efecf72010-02-08 10:12:12 +0000202 }
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000203
204 wake_up_interruptible_all(&ctr->state_wait_queue);
Jan Kiszka3efecf72010-02-08 10:12:12 +0000205 } else
Harvey Harrison156f1ed2008-04-28 02:14:40 -0700206 printk(KERN_WARNING "%s: invalid contr %d\n", __func__, contr);
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000207
208unlock_out:
209 mutex_unlock(&capi_controller_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210}
211
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000212static void ctr_down(struct capi_ctr *ctr, int new_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213{
214 struct capi20_appl *ap;
215 u16 applid;
216
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000217 if (ctr->state == CAPI_CTR_DETECTED || ctr->state == CAPI_CTR_DETACHED)
Jan Kiszka3efecf72010-02-08 10:12:12 +0000218 return;
219
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000220 ctr->state = new_state;
Jan Kiszka3efecf72010-02-08 10:12:12 +0000221
222 memset(ctr->manu, 0, sizeof(ctr->manu));
223 memset(&ctr->version, 0, sizeof(ctr->version));
224 memset(&ctr->profile, 0, sizeof(ctr->profile));
225 memset(ctr->serial, 0, sizeof(ctr->serial));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
227 for (applid = 1; applid <= CAPI_MAXAPPL; applid++) {
Jan Kiszkab003f4e2010-10-17 05:18:15 +0000228 ap = __get_capi_appl_by_nr(applid);
Jan Kiszka88c896e2010-02-08 10:12:15 +0000229 if (ap)
Jan Kiszka3efecf72010-02-08 10:12:12 +0000230 capi_ctr_put(ctr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 }
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000232
233 wake_up_interruptible_all(&ctr->state_wait_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234}
235
Jan Kiszka3efecf72010-02-08 10:12:12 +0000236static void notify_down(u32 contr)
237{
238 struct capi_ctr *ctr;
239
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000240 mutex_lock(&capi_controller_lock);
241
Jan Kiszka3efecf72010-02-08 10:12:12 +0000242 if (showcapimsgs & 1)
243 printk(KERN_DEBUG "kcapi: notify down contr %d\n", contr);
244
245 ctr = get_capi_ctr_by_nr(contr);
246 if (ctr)
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000247 ctr_down(ctr, CAPI_CTR_DETECTED);
Jan Kiszka3efecf72010-02-08 10:12:12 +0000248 else
249 printk(KERN_WARNING "%s: invalid contr %d\n", __func__, contr);
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000250
251 mutex_unlock(&capi_controller_lock);
Jan Kiszka3efecf72010-02-08 10:12:12 +0000252}
253
Jan Kiszkaef69bb22010-02-08 10:12:13 +0000254static int
255notify_handler(struct notifier_block *nb, unsigned long val, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256{
Jan Kiszkaef69bb22010-02-08 10:12:13 +0000257 u32 contr = (long)v;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
Jan Kiszkaef69bb22010-02-08 10:12:13 +0000259 switch (val) {
260 case CAPICTR_UP:
261 notify_up(contr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 break;
Jan Kiszkaef69bb22010-02-08 10:12:13 +0000263 case CAPICTR_DOWN:
264 notify_down(contr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 break;
266 }
Jan Kiszkaef69bb22010-02-08 10:12:13 +0000267 return NOTIFY_OK;
268}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
Jan Kiszkaef69bb22010-02-08 10:12:13 +0000270static void do_notify_work(struct work_struct *work)
271{
272 struct capictr_event *event =
273 container_of(work, struct capictr_event, work);
274
275 blocking_notifier_call_chain(&ctr_notifier_list, event->type,
276 (void *)(long)event->controller);
277 kfree(event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278}
279
280/*
281 * The notifier will result in adding/deleteing of devices. Devices can
282 * only removed in user process, not in bh.
283 */
Jan Kiszkaef69bb22010-02-08 10:12:13 +0000284static int notify_push(unsigned int event_type, u32 controller)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285{
Jan Kiszkaef69bb22010-02-08 10:12:13 +0000286 struct capictr_event *event = kmalloc(sizeof(*event), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
Jan Kiszkaef69bb22010-02-08 10:12:13 +0000288 if (!event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 return -ENOMEM;
290
Jan Kiszkaef69bb22010-02-08 10:12:13 +0000291 INIT_WORK(&event->work, do_notify_work);
292 event->type = event_type;
293 event->controller = controller;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
Tejun Heo158fa672010-12-24 15:59:06 +0100295 queue_work(kcapi_wq, &event->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 return 0;
297}
298
Jan Kiszkaef69bb22010-02-08 10:12:13 +0000299int register_capictr_notifier(struct notifier_block *nb)
300{
301 return blocking_notifier_chain_register(&ctr_notifier_list, nb);
302}
303EXPORT_SYMBOL_GPL(register_capictr_notifier);
304
305int unregister_capictr_notifier(struct notifier_block *nb)
306{
307 return blocking_notifier_chain_unregister(&ctr_notifier_list, nb);
308}
309EXPORT_SYMBOL_GPL(unregister_capictr_notifier);
310
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311/* -------- Receiver ------------------------------------------ */
312
David Howellsc4028952006-11-22 14:57:56 +0000313static void recv_handler(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314{
315 struct sk_buff *skb;
David Howellsc4028952006-11-22 14:57:56 +0000316 struct capi20_appl *ap =
317 container_of(work, struct capi20_appl, recv_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
319 if ((!ap) || (ap->release_in_progress))
320 return;
321
Matthias Kaehlcke67837f22007-07-17 04:04:16 -0700322 mutex_lock(&ap->recv_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 while ((skb = skb_dequeue(&ap->recv_queue))) {
324 if (CAPIMSG_CMD(skb->data) == CAPI_DATA_B3_IND)
325 ap->nrecvdatapkt++;
326 else
327 ap->nrecvctlpkt++;
328
329 ap->recv_message(ap, skb);
330 }
Matthias Kaehlcke67837f22007-07-17 04:04:16 -0700331 mutex_unlock(&ap->recv_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332}
333
Tilman Schmidt554f2002009-04-23 02:24:21 +0000334/**
335 * capi_ctr_handle_message() - handle incoming CAPI message
Jan Kiszka52253032010-02-08 10:12:10 +0000336 * @ctr: controller descriptor structure.
Tilman Schmidt554f2002009-04-23 02:24:21 +0000337 * @appl: application ID.
338 * @skb: message.
339 *
340 * Called by hardware driver to pass a CAPI message to the application.
341 */
342
Jan Kiszka52253032010-02-08 10:12:10 +0000343void capi_ctr_handle_message(struct capi_ctr *ctr, u16 appl,
344 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345{
346 struct capi20_appl *ap;
347 int showctl = 0;
348 u8 cmd, subcmd;
Karsten Keil17f0cd22007-02-28 20:13:50 -0800349 _cdebbuf *cdb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
Jan Kiszka52253032010-02-08 10:12:10 +0000351 if (ctr->state != CAPI_CTR_RUNNING) {
Karsten Keil17f0cd22007-02-28 20:13:50 -0800352 cdb = capi_message2str(skb->data);
353 if (cdb) {
354 printk(KERN_INFO "kcapi: controller [%03d] not active, got: %s",
Joe Perches475be4d2012-02-19 19:52:38 -0800355 ctr->cnr, cdb->buf);
Karsten Keil17f0cd22007-02-28 20:13:50 -0800356 cdebbuf_free(cdb);
357 } else
358 printk(KERN_INFO "kcapi: controller [%03d] not active, cannot trace\n",
Joe Perches475be4d2012-02-19 19:52:38 -0800359 ctr->cnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 goto error;
361 }
362
363 cmd = CAPIMSG_COMMAND(skb->data);
Joe Perches475be4d2012-02-19 19:52:38 -0800364 subcmd = CAPIMSG_SUBCOMMAND(skb->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 if (cmd == CAPI_DATA_B3 && subcmd == CAPI_IND) {
Jan Kiszka52253032010-02-08 10:12:10 +0000366 ctr->nrecvdatapkt++;
367 if (ctr->traceflag > 2)
368 showctl |= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 } else {
Jan Kiszka52253032010-02-08 10:12:10 +0000370 ctr->nrecvctlpkt++;
371 if (ctr->traceflag)
372 showctl |= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 }
Jan Kiszka52253032010-02-08 10:12:10 +0000374 showctl |= (ctr->traceflag & 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 if (showctl & 2) {
376 if (showctl & 1) {
Karsten Keil17f0cd22007-02-28 20:13:50 -0800377 printk(KERN_DEBUG "kcapi: got [%03d] id#%d %s len=%u\n",
Jan Kiszka52253032010-02-08 10:12:10 +0000378 ctr->cnr, CAPIMSG_APPID(skb->data),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 capi_cmd2str(cmd, subcmd),
380 CAPIMSG_LEN(skb->data));
381 } else {
Karsten Keil17f0cd22007-02-28 20:13:50 -0800382 cdb = capi_message2str(skb->data);
383 if (cdb) {
384 printk(KERN_DEBUG "kcapi: got [%03d] %s\n",
Joe Perches475be4d2012-02-19 19:52:38 -0800385 ctr->cnr, cdb->buf);
Karsten Keil17f0cd22007-02-28 20:13:50 -0800386 cdebbuf_free(cdb);
387 } else
388 printk(KERN_DEBUG "kcapi: got [%03d] id#%d %s len=%u, cannot trace\n",
Joe Perches475be4d2012-02-19 19:52:38 -0800389 ctr->cnr, CAPIMSG_APPID(skb->data),
390 capi_cmd2str(cmd, subcmd),
391 CAPIMSG_LEN(skb->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 }
393
394 }
395
Jan Kiszka88c896e2010-02-08 10:12:15 +0000396 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 ap = get_capi_appl_by_nr(CAPIMSG_APPID(skb->data));
Jan Kiszka88c896e2010-02-08 10:12:15 +0000398 if (!ap) {
399 rcu_read_unlock();
Karsten Keil17f0cd22007-02-28 20:13:50 -0800400 cdb = capi_message2str(skb->data);
401 if (cdb) {
402 printk(KERN_ERR "kcapi: handle_message: applid %d state released (%s)\n",
Joe Perches475be4d2012-02-19 19:52:38 -0800403 CAPIMSG_APPID(skb->data), cdb->buf);
Karsten Keil17f0cd22007-02-28 20:13:50 -0800404 cdebbuf_free(cdb);
405 } else
406 printk(KERN_ERR "kcapi: handle_message: applid %d state released (%s) cannot trace\n",
Joe Perches475be4d2012-02-19 19:52:38 -0800407 CAPIMSG_APPID(skb->data),
408 capi_cmd2str(cmd, subcmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 goto error;
410 }
411 skb_queue_tail(&ap->recv_queue, skb);
Tejun Heo158fa672010-12-24 15:59:06 +0100412 queue_work(kcapi_wq, &ap->recv_work);
Jan Kiszka88c896e2010-02-08 10:12:15 +0000413 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
415 return;
416
417error:
418 kfree_skb(skb);
419}
420
421EXPORT_SYMBOL(capi_ctr_handle_message);
422
Tilman Schmidt554f2002009-04-23 02:24:21 +0000423/**
424 * capi_ctr_ready() - signal CAPI controller ready
Jan Kiszka52253032010-02-08 10:12:10 +0000425 * @ctr: controller descriptor structure.
Tilman Schmidt554f2002009-04-23 02:24:21 +0000426 *
427 * Called by hardware driver to signal that the controller is up and running.
428 */
429
Jan Kiszka52253032010-02-08 10:12:10 +0000430void capi_ctr_ready(struct capi_ctr *ctr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431{
Jan Kiszka52253032010-02-08 10:12:10 +0000432 printk(KERN_NOTICE "kcapi: controller [%03d] \"%s\" ready.\n",
433 ctr->cnr, ctr->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
Jan Kiszkaef69bb22010-02-08 10:12:13 +0000435 notify_push(CAPICTR_UP, ctr->cnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436}
437
438EXPORT_SYMBOL(capi_ctr_ready);
439
Tilman Schmidt554f2002009-04-23 02:24:21 +0000440/**
Tilman Schmidt4e329972009-06-07 09:09:23 +0000441 * capi_ctr_down() - signal CAPI controller not ready
Jan Kiszka52253032010-02-08 10:12:10 +0000442 * @ctr: controller descriptor structure.
Tilman Schmidt554f2002009-04-23 02:24:21 +0000443 *
444 * Called by hardware driver to signal that the controller is down and
445 * unavailable for use.
446 */
447
Jan Kiszka52253032010-02-08 10:12:10 +0000448void capi_ctr_down(struct capi_ctr *ctr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449{
Jan Kiszka52253032010-02-08 10:12:10 +0000450 printk(KERN_NOTICE "kcapi: controller [%03d] down.\n", ctr->cnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
Jan Kiszkaef69bb22010-02-08 10:12:13 +0000452 notify_push(CAPICTR_DOWN, ctr->cnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453}
454
Tilman Schmidt4e329972009-06-07 09:09:23 +0000455EXPORT_SYMBOL(capi_ctr_down);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
Tilman Schmidt554f2002009-04-23 02:24:21 +0000457/**
458 * capi_ctr_suspend_output() - suspend controller
Jan Kiszka52253032010-02-08 10:12:10 +0000459 * @ctr: controller descriptor structure.
Tilman Schmidt554f2002009-04-23 02:24:21 +0000460 *
461 * Called by hardware driver to stop data flow.
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000462 *
463 * Note: The caller is responsible for synchronizing concurrent state changes
464 * as well as invocations of capi_ctr_handle_message.
Tilman Schmidt554f2002009-04-23 02:24:21 +0000465 */
466
Jan Kiszka52253032010-02-08 10:12:10 +0000467void capi_ctr_suspend_output(struct capi_ctr *ctr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468{
Jan Kiszka52253032010-02-08 10:12:10 +0000469 if (!ctr->blocked) {
470 printk(KERN_DEBUG "kcapi: controller [%03d] suspend\n",
471 ctr->cnr);
472 ctr->blocked = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 }
474}
475
476EXPORT_SYMBOL(capi_ctr_suspend_output);
477
Tilman Schmidt554f2002009-04-23 02:24:21 +0000478/**
479 * capi_ctr_resume_output() - resume controller
Jan Kiszka52253032010-02-08 10:12:10 +0000480 * @ctr: controller descriptor structure.
Tilman Schmidt554f2002009-04-23 02:24:21 +0000481 *
482 * Called by hardware driver to resume data flow.
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000483 *
484 * Note: The caller is responsible for synchronizing concurrent state changes
485 * as well as invocations of capi_ctr_handle_message.
Tilman Schmidt554f2002009-04-23 02:24:21 +0000486 */
487
Jan Kiszka52253032010-02-08 10:12:10 +0000488void capi_ctr_resume_output(struct capi_ctr *ctr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489{
Jan Kiszka52253032010-02-08 10:12:10 +0000490 if (ctr->blocked) {
491 printk(KERN_DEBUG "kcapi: controller [%03d] resumed\n",
492 ctr->cnr);
493 ctr->blocked = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 }
495}
496
497EXPORT_SYMBOL(capi_ctr_resume_output);
498
499/* ------------------------------------------------------------- */
500
Tilman Schmidt554f2002009-04-23 02:24:21 +0000501/**
502 * attach_capi_ctr() - register CAPI controller
Jan Kiszka52253032010-02-08 10:12:10 +0000503 * @ctr: controller descriptor structure.
Tilman Schmidt554f2002009-04-23 02:24:21 +0000504 *
505 * Called by hardware driver to register a controller with the CAPI subsystem.
506 * Return value: 0 on success, error code < 0 on error
507 */
508
Jan Kiszka52253032010-02-08 10:12:10 +0000509int attach_capi_ctr(struct capi_ctr *ctr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510{
511 int i;
512
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000513 mutex_lock(&capi_controller_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
515 for (i = 0; i < CAPI_MAXCONTR; i++) {
Jan Kiszka52253032010-02-08 10:12:10 +0000516 if (!capi_controller[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 break;
518 }
519 if (i == CAPI_MAXCONTR) {
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000520 mutex_unlock(&capi_controller_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 printk(KERN_ERR "kcapi: out of controller slots\n");
Joe Perches475be4d2012-02-19 19:52:38 -0800522 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 }
Jan Kiszka52253032010-02-08 10:12:10 +0000524 capi_controller[i] = ctr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
Jan Kiszka52253032010-02-08 10:12:10 +0000526 ctr->nrecvctlpkt = 0;
527 ctr->nrecvdatapkt = 0;
528 ctr->nsentctlpkt = 0;
529 ctr->nsentdatapkt = 0;
530 ctr->cnr = i + 1;
531 ctr->state = CAPI_CTR_DETECTED;
532 ctr->blocked = 0;
533 ctr->traceflag = showcapimsgs;
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000534 init_waitqueue_head(&ctr->state_wait_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
Jan Kiszka52253032010-02-08 10:12:10 +0000536 sprintf(ctr->procfn, "capi/controllers/%d", ctr->cnr);
Christoph Hellwig2cd1f0d2018-04-11 18:39:29 +0200537 ctr->procent = proc_create_single_data(ctr->procfn, 0, NULL,
538 ctr->proc_show, ctr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
Jan Kiszka52253032010-02-08 10:12:10 +0000540 ncontrollers++;
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000541
542 mutex_unlock(&capi_controller_lock);
543
Jan Kiszka52253032010-02-08 10:12:10 +0000544 printk(KERN_NOTICE "kcapi: controller [%03d]: %s attached\n",
Joe Perches475be4d2012-02-19 19:52:38 -0800545 ctr->cnr, ctr->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 return 0;
547}
548
549EXPORT_SYMBOL(attach_capi_ctr);
550
Tilman Schmidt554f2002009-04-23 02:24:21 +0000551/**
552 * detach_capi_ctr() - unregister CAPI controller
Jan Kiszka52253032010-02-08 10:12:10 +0000553 * @ctr: controller descriptor structure.
Tilman Schmidt554f2002009-04-23 02:24:21 +0000554 *
555 * Called by hardware driver to remove the registration of a controller
556 * with the CAPI subsystem.
557 * Return value: 0 on success, error code < 0 on error
558 */
559
Jan Kiszka52253032010-02-08 10:12:10 +0000560int detach_capi_ctr(struct capi_ctr *ctr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561{
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000562 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000564 mutex_lock(&capi_controller_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000566 ctr_down(ctr, CAPI_CTR_DETACHED);
567
568 if (capi_controller[ctr->cnr - 1] != ctr) {
569 err = -EINVAL;
570 goto unlock_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 }
Jan Kiszka52253032010-02-08 10:12:10 +0000572 capi_controller[ctr->cnr - 1] = NULL;
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000573 ncontrollers--;
574
575 if (ctr->procent)
576 remove_proc_entry(ctr->procfn, NULL);
577
Jan Kiszka52253032010-02-08 10:12:10 +0000578 printk(KERN_NOTICE "kcapi: controller [%03d]: %s unregistered\n",
579 ctr->cnr, ctr->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000581unlock_out:
582 mutex_unlock(&capi_controller_lock);
583
584 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585}
586
587EXPORT_SYMBOL(detach_capi_ctr);
588
Tilman Schmidt554f2002009-04-23 02:24:21 +0000589/**
590 * register_capi_driver() - register CAPI driver
591 * @driver: driver descriptor structure.
592 *
593 * Called by hardware driver to register itself with the CAPI subsystem.
594 */
595
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596void register_capi_driver(struct capi_driver *driver)
597{
Jan Kiszka9717fb82010-02-08 10:12:11 +0000598 mutex_lock(&capi_drivers_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 list_add_tail(&driver->list, &capi_drivers);
Jan Kiszka9717fb82010-02-08 10:12:11 +0000600 mutex_unlock(&capi_drivers_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601}
602
603EXPORT_SYMBOL(register_capi_driver);
604
Tilman Schmidt554f2002009-04-23 02:24:21 +0000605/**
606 * unregister_capi_driver() - unregister CAPI driver
607 * @driver: driver descriptor structure.
608 *
609 * Called by hardware driver to unregister itself from the CAPI subsystem.
610 */
611
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612void unregister_capi_driver(struct capi_driver *driver)
613{
Jan Kiszka9717fb82010-02-08 10:12:11 +0000614 mutex_lock(&capi_drivers_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 list_del(&driver->list);
Jan Kiszka9717fb82010-02-08 10:12:11 +0000616 mutex_unlock(&capi_drivers_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617}
618
619EXPORT_SYMBOL(unregister_capi_driver);
620
621/* ------------------------------------------------------------- */
622/* -------- CAPI2.0 Interface ---------------------------------- */
623/* ------------------------------------------------------------- */
624
Tilman Schmidt554f2002009-04-23 02:24:21 +0000625/**
626 * capi20_isinstalled() - CAPI 2.0 operation CAPI_INSTALLED
627 *
628 * Return value: CAPI result code (CAPI_NOERROR if at least one ISDN controller
629 * is ready for use, CAPI_REGNOTINSTALLED otherwise)
630 */
631
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632u16 capi20_isinstalled(void)
633{
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000634 u16 ret = CAPI_REGNOTINSTALLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 int i;
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000636
637 mutex_lock(&capi_controller_lock);
638
639 for (i = 0; i < CAPI_MAXCONTR; i++)
Jan Kiszka52253032010-02-08 10:12:10 +0000640 if (capi_controller[i] &&
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000641 capi_controller[i]->state == CAPI_CTR_RUNNING) {
642 ret = CAPI_NOERROR;
643 break;
644 }
645
646 mutex_unlock(&capi_controller_lock);
647
648 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649}
650
651EXPORT_SYMBOL(capi20_isinstalled);
652
Tilman Schmidt554f2002009-04-23 02:24:21 +0000653/**
654 * capi20_register() - CAPI 2.0 operation CAPI_REGISTER
655 * @ap: CAPI application descriptor structure.
656 *
657 * Register an application's presence with CAPI.
658 * A unique application ID is assigned and stored in @ap->applid.
659 * After this function returns successfully, the message receive
660 * callback function @ap->recv_message() may be called at any time
661 * until capi20_release() has been called for the same @ap.
662 * Return value: CAPI result code
663 */
664
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665u16 capi20_register(struct capi20_appl *ap)
666{
667 int i;
668 u16 applid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
670 DBG("");
671
672 if (ap->rparam.datablklen < 128)
673 return CAPI_LOGBLKSIZETOSMALL;
674
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 ap->nrecvctlpkt = 0;
676 ap->nrecvdatapkt = 0;
677 ap->nsentctlpkt = 0;
678 ap->nsentdatapkt = 0;
Matthias Kaehlcke67837f22007-07-17 04:04:16 -0700679 mutex_init(&ap->recv_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 skb_queue_head_init(&ap->recv_queue);
David Howellsc4028952006-11-22 14:57:56 +0000681 INIT_WORK(&ap->recv_work, recv_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 ap->release_in_progress = 0;
683
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000684 mutex_lock(&capi_controller_lock);
685
Jan Kiszka88c896e2010-02-08 10:12:15 +0000686 for (applid = 1; applid <= CAPI_MAXAPPL; applid++) {
687 if (capi_applications[applid - 1] == NULL)
688 break;
689 }
690 if (applid > CAPI_MAXAPPL) {
691 mutex_unlock(&capi_controller_lock);
692 return CAPI_TOOMANYAPPLS;
693 }
694
695 ap->applid = applid;
696 capi_applications[applid - 1] = ap;
697
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 for (i = 0; i < CAPI_MAXCONTR; i++) {
Jan Kiszka52253032010-02-08 10:12:10 +0000699 if (!capi_controller[i] ||
700 capi_controller[i]->state != CAPI_CTR_RUNNING)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 continue;
Jan Kiszka52253032010-02-08 10:12:10 +0000702 register_appl(capi_controller[i], applid, &ap->rparam);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 }
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000704
705 mutex_unlock(&capi_controller_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
707 if (showcapimsgs & 1) {
708 printk(KERN_DEBUG "kcapi: appl %d up\n", applid);
709 }
710
711 return CAPI_NOERROR;
712}
713
714EXPORT_SYMBOL(capi20_register);
715
Tilman Schmidt554f2002009-04-23 02:24:21 +0000716/**
717 * capi20_release() - CAPI 2.0 operation CAPI_RELEASE
718 * @ap: CAPI application descriptor structure.
719 *
720 * Terminate an application's registration with CAPI.
721 * After this function returns successfully, the message receive
722 * callback function @ap->recv_message() will no longer be called.
723 * Return value: CAPI result code
724 */
725
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726u16 capi20_release(struct capi20_appl *ap)
727{
728 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
730 DBG("applid %#x", ap->applid);
731
Jan Kiszka88c896e2010-02-08 10:12:15 +0000732 mutex_lock(&capi_controller_lock);
733
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 ap->release_in_progress = 1;
735 capi_applications[ap->applid - 1] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
Jan Kiszka88c896e2010-02-08 10:12:15 +0000737 synchronize_rcu();
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000738
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 for (i = 0; i < CAPI_MAXCONTR; i++) {
Jan Kiszka52253032010-02-08 10:12:10 +0000740 if (!capi_controller[i] ||
741 capi_controller[i]->state != CAPI_CTR_RUNNING)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 continue;
Jan Kiszka52253032010-02-08 10:12:10 +0000743 release_appl(capi_controller[i], ap->applid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 }
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000745
746 mutex_unlock(&capi_controller_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
Tejun Heo158fa672010-12-24 15:59:06 +0100748 flush_workqueue(kcapi_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 skb_queue_purge(&ap->recv_queue);
750
751 if (showcapimsgs & 1) {
752 printk(KERN_DEBUG "kcapi: appl %d down\n", ap->applid);
753 }
754
755 return CAPI_NOERROR;
756}
757
758EXPORT_SYMBOL(capi20_release);
759
Tilman Schmidt554f2002009-04-23 02:24:21 +0000760/**
761 * capi20_put_message() - CAPI 2.0 operation CAPI_PUT_MESSAGE
762 * @ap: CAPI application descriptor structure.
763 * @skb: CAPI message.
764 *
765 * Transfer a single message to CAPI.
766 * Return value: CAPI result code
767 */
768
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769u16 capi20_put_message(struct capi20_appl *ap, struct sk_buff *skb)
770{
Jan Kiszka52253032010-02-08 10:12:10 +0000771 struct capi_ctr *ctr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 int showctl = 0;
773 u8 cmd, subcmd;
774
775 DBG("applid %#x", ap->applid);
Joe Perches475be4d2012-02-19 19:52:38 -0800776
Jan Kiszka52253032010-02-08 10:12:10 +0000777 if (ncontrollers == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 return CAPI_REGNOTINSTALLED;
779 if ((ap->applid == 0) || ap->release_in_progress)
780 return CAPI_ILLAPPNR;
781 if (skb->len < 12
782 || !capi_cmd_valid(CAPIMSG_COMMAND(skb->data))
783 || !capi_subcmd_valid(CAPIMSG_SUBCOMMAND(skb->data)))
784 return CAPI_ILLCMDORSUBCMDORMSGTOSMALL;
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000785
786 /*
787 * The controller reference is protected by the existence of the
788 * application passed to us. We assume that the caller properly
789 * synchronizes this service with capi20_release.
790 */
Jan Kiszka52253032010-02-08 10:12:10 +0000791 ctr = get_capi_ctr_by_nr(CAPIMSG_CONTROLLER(skb->data));
Jan Kiszkac6af0432010-02-08 10:12:43 +0000792 if (!ctr || ctr->state != CAPI_CTR_RUNNING)
793 return CAPI_REGNOTINSTALLED;
Jan Kiszka52253032010-02-08 10:12:10 +0000794 if (ctr->blocked)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 return CAPI_SENDQUEUEFULL;
796
797 cmd = CAPIMSG_COMMAND(skb->data);
Joe Perches475be4d2012-02-19 19:52:38 -0800798 subcmd = CAPIMSG_SUBCOMMAND(skb->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
Joe Perches475be4d2012-02-19 19:52:38 -0800800 if (cmd == CAPI_DATA_B3 && subcmd == CAPI_REQ) {
Jan Kiszka52253032010-02-08 10:12:10 +0000801 ctr->nsentdatapkt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 ap->nsentdatapkt++;
Jan Kiszka52253032010-02-08 10:12:10 +0000803 if (ctr->traceflag > 2)
804 showctl |= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 } else {
Jan Kiszka52253032010-02-08 10:12:10 +0000806 ctr->nsentctlpkt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 ap->nsentctlpkt++;
Jan Kiszka52253032010-02-08 10:12:10 +0000808 if (ctr->traceflag)
809 showctl |= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 }
Jan Kiszka52253032010-02-08 10:12:10 +0000811 showctl |= (ctr->traceflag & 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 if (showctl & 2) {
813 if (showctl & 1) {
Karsten Keil17f0cd22007-02-28 20:13:50 -0800814 printk(KERN_DEBUG "kcapi: put [%03d] id#%d %s len=%u\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 CAPIMSG_CONTROLLER(skb->data),
816 CAPIMSG_APPID(skb->data),
817 capi_cmd2str(cmd, subcmd),
818 CAPIMSG_LEN(skb->data));
819 } else {
Karsten Keil17f0cd22007-02-28 20:13:50 -0800820 _cdebbuf *cdb = capi_message2str(skb->data);
821 if (cdb) {
822 printk(KERN_DEBUG "kcapi: put [%03d] %s\n",
Joe Perches475be4d2012-02-19 19:52:38 -0800823 CAPIMSG_CONTROLLER(skb->data),
824 cdb->buf);
Karsten Keil17f0cd22007-02-28 20:13:50 -0800825 cdebbuf_free(cdb);
826 } else
827 printk(KERN_DEBUG "kcapi: put [%03d] id#%d %s len=%u cannot trace\n",
Joe Perches475be4d2012-02-19 19:52:38 -0800828 CAPIMSG_CONTROLLER(skb->data),
829 CAPIMSG_APPID(skb->data),
830 capi_cmd2str(cmd, subcmd),
831 CAPIMSG_LEN(skb->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 }
Jan Kiszka52253032010-02-08 10:12:10 +0000834 return ctr->send_message(ctr, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835}
836
837EXPORT_SYMBOL(capi20_put_message);
838
Tilman Schmidt554f2002009-04-23 02:24:21 +0000839/**
840 * capi20_get_manufacturer() - CAPI 2.0 operation CAPI_GET_MANUFACTURER
841 * @contr: controller number.
842 * @buf: result buffer (64 bytes).
843 *
844 * Retrieve information about the manufacturer of the specified ISDN controller
845 * or (for @contr == 0) the driver itself.
846 * Return value: CAPI result code
847 */
848
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849u16 capi20_get_manufacturer(u32 contr, u8 *buf)
850{
Jan Kiszka52253032010-02-08 10:12:10 +0000851 struct capi_ctr *ctr;
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000852 u16 ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
854 if (contr == 0) {
Eric Dumazetd63967e2019-01-02 09:20:27 -0800855 strncpy(buf, capi_manufakturer, CAPI_MANUFACTURER_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 return CAPI_NOERROR;
857 }
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000858
859 mutex_lock(&capi_controller_lock);
860
Jan Kiszka52253032010-02-08 10:12:10 +0000861 ctr = get_capi_ctr_by_nr(contr);
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000862 if (ctr && ctr->state == CAPI_CTR_RUNNING) {
Eric Dumazetd63967e2019-01-02 09:20:27 -0800863 strncpy(buf, ctr->manu, CAPI_MANUFACTURER_LEN);
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000864 ret = CAPI_NOERROR;
865 } else
866 ret = CAPI_REGNOTINSTALLED;
867
868 mutex_unlock(&capi_controller_lock);
869 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870}
871
872EXPORT_SYMBOL(capi20_get_manufacturer);
873
Tilman Schmidt554f2002009-04-23 02:24:21 +0000874/**
875 * capi20_get_version() - CAPI 2.0 operation CAPI_GET_VERSION
876 * @contr: controller number.
877 * @verp: result structure.
878 *
879 * Retrieve version information for the specified ISDN controller
880 * or (for @contr == 0) the driver itself.
881 * Return value: CAPI result code
882 */
883
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884u16 capi20_get_version(u32 contr, struct capi_version *verp)
885{
Jan Kiszka52253032010-02-08 10:12:10 +0000886 struct capi_ctr *ctr;
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000887 u16 ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
889 if (contr == 0) {
890 *verp = driver_version;
891 return CAPI_NOERROR;
892 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000894 mutex_lock(&capi_controller_lock);
895
896 ctr = get_capi_ctr_by_nr(contr);
897 if (ctr && ctr->state == CAPI_CTR_RUNNING) {
898 memcpy(verp, &ctr->version, sizeof(capi_version));
899 ret = CAPI_NOERROR;
900 } else
901 ret = CAPI_REGNOTINSTALLED;
902
903 mutex_unlock(&capi_controller_lock);
904 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905}
906
907EXPORT_SYMBOL(capi20_get_version);
908
Tilman Schmidt554f2002009-04-23 02:24:21 +0000909/**
910 * capi20_get_serial() - CAPI 2.0 operation CAPI_GET_SERIAL_NUMBER
911 * @contr: controller number.
912 * @serial: result buffer (8 bytes).
913 *
914 * Retrieve the serial number of the specified ISDN controller
915 * or (for @contr == 0) the driver itself.
916 * Return value: CAPI result code
917 */
918
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919u16 capi20_get_serial(u32 contr, u8 *serial)
920{
Jan Kiszka52253032010-02-08 10:12:10 +0000921 struct capi_ctr *ctr;
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000922 u16 ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
924 if (contr == 0) {
925 strlcpy(serial, driver_serial, CAPI_SERIAL_LEN);
926 return CAPI_NOERROR;
927 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000929 mutex_lock(&capi_controller_lock);
930
931 ctr = get_capi_ctr_by_nr(contr);
932 if (ctr && ctr->state == CAPI_CTR_RUNNING) {
933 strlcpy(serial, ctr->serial, CAPI_SERIAL_LEN);
934 ret = CAPI_NOERROR;
935 } else
936 ret = CAPI_REGNOTINSTALLED;
937
938 mutex_unlock(&capi_controller_lock);
939 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940}
941
942EXPORT_SYMBOL(capi20_get_serial);
943
Tilman Schmidt554f2002009-04-23 02:24:21 +0000944/**
945 * capi20_get_profile() - CAPI 2.0 operation CAPI_GET_PROFILE
946 * @contr: controller number.
947 * @profp: result structure.
948 *
949 * Retrieve capability information for the specified ISDN controller
950 * or (for @contr == 0) the number of installed controllers.
951 * Return value: CAPI result code
952 */
953
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954u16 capi20_get_profile(u32 contr, struct capi_profile *profp)
955{
Jan Kiszka52253032010-02-08 10:12:10 +0000956 struct capi_ctr *ctr;
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000957 u16 ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
959 if (contr == 0) {
Jan Kiszka52253032010-02-08 10:12:10 +0000960 profp->ncontroller = ncontrollers;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 return CAPI_NOERROR;
962 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000964 mutex_lock(&capi_controller_lock);
965
966 ctr = get_capi_ctr_by_nr(contr);
967 if (ctr && ctr->state == CAPI_CTR_RUNNING) {
968 memcpy(profp, &ctr->profile, sizeof(struct capi_profile));
969 ret = CAPI_NOERROR;
970 } else
971 ret = CAPI_REGNOTINSTALLED;
972
973 mutex_unlock(&capi_controller_lock);
974 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975}
976
977EXPORT_SYMBOL(capi20_get_profile);
978
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000979/* Must be called with capi_controller_lock held. */
980static int wait_on_ctr_state(struct capi_ctr *ctr, unsigned int state)
981{
982 DEFINE_WAIT(wait);
983 int retval = 0;
984
985 ctr = capi_ctr_get(ctr);
986 if (!ctr)
987 return -ESRCH;
988
989 for (;;) {
990 prepare_to_wait(&ctr->state_wait_queue, &wait,
991 TASK_INTERRUPTIBLE);
992
993 if (ctr->state == state)
994 break;
995 if (ctr->state == CAPI_CTR_DETACHED) {
996 retval = -ESRCH;
997 break;
998 }
999 if (signal_pending(current)) {
1000 retval = -EINTR;
1001 break;
1002 }
1003
1004 mutex_unlock(&capi_controller_lock);
1005 schedule();
1006 mutex_lock(&capi_controller_lock);
1007 }
1008 finish_wait(&ctr->state_wait_queue, &wait);
1009
1010 capi_ctr_put(ctr);
1011
1012 return retval;
1013}
1014
Robert P. J. Day37772ac2008-04-28 02:14:42 -07001015#ifdef AVMB1_COMPAT
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016static int old_capi_manufacturer(unsigned int cmd, void __user *data)
1017{
1018 avmb1_loadandconfigdef ldef;
1019 avmb1_extcarddef cdef;
1020 avmb1_resetdef rdef;
1021 capicardparams cparams;
Jan Kiszka52253032010-02-08 10:12:10 +00001022 struct capi_ctr *ctr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 struct capi_driver *driver = NULL;
1024 capiloaddata ldata;
1025 struct list_head *l;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 int retval;
1027
1028 switch (cmd) {
1029 case AVMB1_ADDCARD:
1030 case AVMB1_ADDCARD_WITH_TYPE:
1031 if (cmd == AVMB1_ADDCARD) {
Joe Perches475be4d2012-02-19 19:52:38 -08001032 if ((retval = copy_from_user(&cdef, data,
1033 sizeof(avmb1_carddef))))
1034 return -EFAULT;
1035 cdef.cardtype = AVM_CARDTYPE_B1;
Arnd Bergmannaf109a2c2017-03-28 12:11:07 +02001036 cdef.cardnr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 } else {
Joe Perches475be4d2012-02-19 19:52:38 -08001038 if ((retval = copy_from_user(&cdef, data,
1039 sizeof(avmb1_extcarddef))))
1040 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 }
1042 cparams.port = cdef.port;
1043 cparams.irq = cdef.irq;
1044 cparams.cardnr = cdef.cardnr;
1045
Jan Kiszka9717fb82010-02-08 10:12:11 +00001046 mutex_lock(&capi_drivers_lock);
1047
Joe Perches475be4d2012-02-19 19:52:38 -08001048 switch (cdef.cardtype) {
1049 case AVM_CARDTYPE_B1:
1050 list_for_each(l, &capi_drivers) {
1051 driver = list_entry(l, struct capi_driver, list);
1052 if (strcmp(driver->name, "b1isa") == 0)
1053 break;
1054 }
1055 break;
1056 case AVM_CARDTYPE_T1:
1057 list_for_each(l, &capi_drivers) {
1058 driver = list_entry(l, struct capi_driver, list);
1059 if (strcmp(driver->name, "t1isa") == 0)
1060 break;
1061 }
1062 break;
1063 default:
1064 driver = NULL;
1065 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 }
1067 if (!driver) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 printk(KERN_ERR "kcapi: driver not loaded.\n");
Jan Kiszka9717fb82010-02-08 10:12:11 +00001069 retval = -EIO;
1070 } else if (!driver->add_card) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 printk(KERN_ERR "kcapi: driver has no add card function.\n");
Jan Kiszka9717fb82010-02-08 10:12:11 +00001072 retval = -EIO;
1073 } else
1074 retval = driver->add_card(driver, &cparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
Jan Kiszka9717fb82010-02-08 10:12:11 +00001076 mutex_unlock(&capi_drivers_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 return retval;
1078
1079 case AVMB1_LOAD:
1080 case AVMB1_LOAD_AND_CONFIG:
1081
1082 if (cmd == AVMB1_LOAD) {
1083 if (copy_from_user(&ldef, data,
1084 sizeof(avmb1_loaddef)))
1085 return -EFAULT;
1086 ldef.t4config.len = 0;
1087 ldef.t4config.data = NULL;
1088 } else {
1089 if (copy_from_user(&ldef, data,
1090 sizeof(avmb1_loadandconfigdef)))
1091 return -EFAULT;
1092 }
Jan Kiszka0ca3a012010-02-08 10:12:14 +00001093
1094 mutex_lock(&capi_controller_lock);
1095
Jan Kiszka52253032010-02-08 10:12:10 +00001096 ctr = get_capi_ctr_by_nr(ldef.contr);
Jan Kiszka0ca3a012010-02-08 10:12:14 +00001097 if (!ctr) {
1098 retval = -EINVAL;
1099 goto load_unlock_out;
1100 }
1101
Jan Kiszka52253032010-02-08 10:12:10 +00001102 if (ctr->load_firmware == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 printk(KERN_DEBUG "kcapi: load: no load function\n");
Jan Kiszka0ca3a012010-02-08 10:12:14 +00001104 retval = -ESRCH;
1105 goto load_unlock_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 }
1107
1108 if (ldef.t4file.len <= 0) {
1109 printk(KERN_DEBUG "kcapi: load: invalid parameter: length of t4file is %d ?\n", ldef.t4file.len);
Jan Kiszka0ca3a012010-02-08 10:12:14 +00001110 retval = -EINVAL;
1111 goto load_unlock_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 }
Harvey Harrison2f9e9b62008-04-28 02:14:37 -07001113 if (ldef.t4file.data == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 printk(KERN_DEBUG "kcapi: load: invalid parameter: dataptr is 0\n");
Jan Kiszka0ca3a012010-02-08 10:12:14 +00001115 retval = -EINVAL;
1116 goto load_unlock_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 }
1118
1119 ldata.firmware.user = 1;
1120 ldata.firmware.data = ldef.t4file.data;
1121 ldata.firmware.len = ldef.t4file.len;
1122 ldata.configuration.user = 1;
1123 ldata.configuration.data = ldef.t4config.data;
1124 ldata.configuration.len = ldef.t4config.len;
1125
Jan Kiszka52253032010-02-08 10:12:10 +00001126 if (ctr->state != CAPI_CTR_DETECTED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 printk(KERN_INFO "kcapi: load: contr=%d not in detect state\n", ldef.contr);
Jan Kiszka0ca3a012010-02-08 10:12:14 +00001128 retval = -EBUSY;
1129 goto load_unlock_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 }
Jan Kiszka52253032010-02-08 10:12:10 +00001131 ctr->state = CAPI_CTR_LOADING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
Jan Kiszka52253032010-02-08 10:12:10 +00001133 retval = ctr->load_firmware(ctr, &ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 if (retval) {
Jan Kiszka52253032010-02-08 10:12:10 +00001135 ctr->state = CAPI_CTR_DETECTED;
Jan Kiszka0ca3a012010-02-08 10:12:14 +00001136 goto load_unlock_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 }
1138
Jan Kiszka0ca3a012010-02-08 10:12:14 +00001139 retval = wait_on_ctr_state(ctr, CAPI_CTR_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
Joe Perches475be4d2012-02-19 19:52:38 -08001141 load_unlock_out:
Jan Kiszka0ca3a012010-02-08 10:12:14 +00001142 mutex_unlock(&capi_controller_lock);
1143 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
1145 case AVMB1_RESETCARD:
1146 if (copy_from_user(&rdef, data, sizeof(avmb1_resetdef)))
1147 return -EFAULT;
Jan Kiszka0ca3a012010-02-08 10:12:14 +00001148
1149 retval = 0;
1150
1151 mutex_lock(&capi_controller_lock);
1152
Jan Kiszka52253032010-02-08 10:12:10 +00001153 ctr = get_capi_ctr_by_nr(rdef.contr);
Jan Kiszka0ca3a012010-02-08 10:12:14 +00001154 if (!ctr) {
1155 retval = -ESRCH;
1156 goto reset_unlock_out;
1157 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
Jan Kiszka52253032010-02-08 10:12:10 +00001159 if (ctr->state == CAPI_CTR_DETECTED)
Jan Kiszka0ca3a012010-02-08 10:12:14 +00001160 goto reset_unlock_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
Tilman Schmidt85a83562010-05-23 01:02:08 +00001162 if (ctr->reset_ctr == NULL) {
1163 printk(KERN_DEBUG "kcapi: reset: no reset function\n");
1164 retval = -ESRCH;
1165 goto reset_unlock_out;
1166 }
1167
Jan Kiszka52253032010-02-08 10:12:10 +00001168 ctr->reset_ctr(ctr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
Jan Kiszka0ca3a012010-02-08 10:12:14 +00001170 retval = wait_on_ctr_state(ctr, CAPI_CTR_DETECTED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171
Joe Perches475be4d2012-02-19 19:52:38 -08001172 reset_unlock_out:
Jan Kiszka0ca3a012010-02-08 10:12:14 +00001173 mutex_unlock(&capi_controller_lock);
1174 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 }
1176 return -EINVAL;
1177}
1178#endif
1179
Tilman Schmidt554f2002009-04-23 02:24:21 +00001180/**
1181 * capi20_manufacturer() - CAPI 2.0 operation CAPI_MANUFACTURER
1182 * @cmd: command.
1183 * @data: parameter.
1184 *
1185 * Perform manufacturer specific command.
1186 * Return value: CAPI result code
1187 */
1188
Tilman Schmidt9ea8aa82014-10-11 13:46:30 +02001189int capi20_manufacturer(unsigned long cmd, void __user *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190{
Jan Kiszka52253032010-02-08 10:12:10 +00001191 struct capi_ctr *ctr;
Jan Kiszka0ca3a012010-02-08 10:12:14 +00001192 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
1194 switch (cmd) {
Robert P. J. Day37772ac2008-04-28 02:14:42 -07001195#ifdef AVMB1_COMPAT
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 case AVMB1_LOAD:
1197 case AVMB1_LOAD_AND_CONFIG:
1198 case AVMB1_RESETCARD:
1199 case AVMB1_GET_CARDINFO:
1200 case AVMB1_REMOVECARD:
1201 return old_capi_manufacturer(cmd, data);
1202#endif
1203 case KCAPI_CMD_TRACE:
1204 {
1205 kcapi_flagdef fdef;
1206
1207 if (copy_from_user(&fdef, data, sizeof(kcapi_flagdef)))
1208 return -EFAULT;
1209
Jan Kiszka0ca3a012010-02-08 10:12:14 +00001210 mutex_lock(&capi_controller_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
Jan Kiszka0ca3a012010-02-08 10:12:14 +00001212 ctr = get_capi_ctr_by_nr(fdef.contr);
1213 if (ctr) {
1214 ctr->traceflag = fdef.flag;
1215 printk(KERN_INFO "kcapi: contr [%03d] set trace=%d\n",
1216 ctr->cnr, ctr->traceflag);
1217 retval = 0;
1218 } else
1219 retval = -ESRCH;
1220
1221 mutex_unlock(&capi_controller_lock);
1222
1223 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 }
1225 case KCAPI_CMD_ADDCARD:
1226 {
1227 struct list_head *l;
1228 struct capi_driver *driver = NULL;
1229 capicardparams cparams;
1230 kcapi_carddef cdef;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
1232 if ((retval = copy_from_user(&cdef, data, sizeof(cdef))))
Dan Carpenter60a57112010-06-02 23:56:13 +00001233 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
1235 cparams.port = cdef.port;
1236 cparams.irq = cdef.irq;
1237 cparams.membase = cdef.membase;
1238 cparams.cardnr = cdef.cardnr;
1239 cparams.cardtype = 0;
Joe Perches475be4d2012-02-19 19:52:38 -08001240 cdef.driver[sizeof(cdef.driver) - 1] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241
Jan Kiszka9717fb82010-02-08 10:12:11 +00001242 mutex_lock(&capi_drivers_lock);
1243
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 list_for_each(l, &capi_drivers) {
1245 driver = list_entry(l, struct capi_driver, list);
1246 if (strcmp(driver->name, cdef.driver) == 0)
1247 break;
1248 }
Harvey Harrison2f9e9b62008-04-28 02:14:37 -07001249 if (driver == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 printk(KERN_ERR "kcapi: driver \"%s\" not loaded.\n",
Joe Perches475be4d2012-02-19 19:52:38 -08001251 cdef.driver);
Jan Kiszka9717fb82010-02-08 10:12:11 +00001252 retval = -ESRCH;
1253 } else if (!driver->add_card) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 printk(KERN_ERR "kcapi: driver \"%s\" has no add card function.\n", cdef.driver);
Jan Kiszka9717fb82010-02-08 10:12:11 +00001255 retval = -EIO;
1256 } else
1257 retval = driver->add_card(driver, &cparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
Jan Kiszka9717fb82010-02-08 10:12:11 +00001259 mutex_unlock(&capi_drivers_lock);
1260 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 }
1262
1263 default:
Tilman Schmidt9ea8aa82014-10-11 13:46:30 +02001264 printk(KERN_ERR "kcapi: manufacturer command %lu unknown.\n",
Joe Perches475be4d2012-02-19 19:52:38 -08001265 cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 break;
1267
1268 }
1269 return -EINVAL;
1270}
1271
1272EXPORT_SYMBOL(capi20_manufacturer);
1273
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274/* ------------------------------------------------------------- */
1275/* -------- Init & Cleanup ------------------------------------- */
1276/* ------------------------------------------------------------- */
1277
1278/*
1279 * init / exit functions
1280 */
1281
Jan Kiszkaef69bb22010-02-08 10:12:13 +00001282static struct notifier_block capictr_nb = {
1283 .notifier_call = notify_handler,
1284 .priority = INT_MAX,
1285};
1286
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287static int __init kcapi_init(void)
1288{
Jan Kiszka88549d62010-02-08 10:12:09 +00001289 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
Tejun Heo158fa672010-12-24 15:59:06 +01001291 kcapi_wq = alloc_workqueue("kcapi", 0, 0);
1292 if (!kcapi_wq)
1293 return -ENOMEM;
1294
Jan Kiszkaef69bb22010-02-08 10:12:13 +00001295 register_capictr_notifier(&capictr_nb);
1296
Jan Kiszka88549d62010-02-08 10:12:09 +00001297 err = cdebug_init();
Tejun Heo158fa672010-12-24 15:59:06 +01001298 if (err) {
1299 unregister_capictr_notifier(&capictr_nb);
1300 destroy_workqueue(kcapi_wq);
1301 return err;
1302 }
1303
1304 kcapi_proc_init();
1305 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306}
1307
1308static void __exit kcapi_exit(void)
1309{
Joe Perches475be4d2012-02-19 19:52:38 -08001310 kcapi_proc_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311
Tejun Heo158fa672010-12-24 15:59:06 +01001312 unregister_capictr_notifier(&capictr_nb);
Karsten Keil17f0cd22007-02-28 20:13:50 -08001313 cdebug_exit();
Tejun Heo158fa672010-12-24 15:59:06 +01001314 destroy_workqueue(kcapi_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315}
1316
1317module_init(kcapi_init);
1318module_exit(kcapi_exit);