blob: 3acf94cc5acda3f056db58b559a8af477f6ae76a [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 $
2 *
3 * Kernel CAPI 2.0 Module
4 *
5 * Copyright 1999 by Carsten Paeth <calle@calle.de>
6 * Copyright 2002 by Kai Germaschewski <kai@germaschewski.name>
7 *
8 * This software may be used and distributed according to the terms
9 * of the GNU General Public License, incorporated herein by reference.
10 *
11 */
12
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>
Alexey Dobriyand43c36d2009-10-07 17:09:06 +040021#include <linux/sched.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 Torvalds1da177e2005-04-16 15:20:36 -070031#include <asm/uaccess.h>
32#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;
41
42MODULE_DESCRIPTION("CAPI4Linux: kernel CAPI layer");
43MODULE_AUTHOR("Carsten Paeth");
44MODULE_LICENSE("GPL");
45module_param(showcapimsgs, uint, 0);
46
47/* ------------------------------------------------------------- */
48
Jan Kiszkaef69bb22010-02-08 10:12:13 +000049struct capictr_event {
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 struct work_struct work;
Jan Kiszkaef69bb22010-02-08 10:12:13 +000051 unsigned int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 u32 controller;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053};
54
55/* ------------------------------------------------------------- */
56
57static struct capi_version driver_version = {2, 0, 1, 1<<4};
58static char driver_serial[CAPI_SERIAL_LEN] = "0004711";
59static char capi_manufakturer[64] = "AVM Berlin";
60
61#define NCCI2CTRL(ncci) (((ncci) >> 24) & 0x7f)
62
63LIST_HEAD(capi_drivers);
Jan Kiszka9717fb82010-02-08 10:12:11 +000064DEFINE_MUTEX(capi_drivers_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Jan Kiszka0ca3a012010-02-08 10:12:14 +000066struct capi_ctr *capi_controller[CAPI_MAXCONTR];
67DEFINE_MUTEX(capi_controller_lock);
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069struct capi20_appl *capi_applications[CAPI_MAXAPPL];
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Jan Kiszka52253032010-02-08 10:12:10 +000071static int ncontrollers;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Jan Kiszkaef69bb22010-02-08 10:12:13 +000073static BLOCKING_NOTIFIER_HEAD(ctr_notifier_list);
74
Linus Torvalds1da177e2005-04-16 15:20:36 -070075/* -------- controller ref counting -------------------------------------- */
76
77static inline struct capi_ctr *
Jan Kiszka52253032010-02-08 10:12:10 +000078capi_ctr_get(struct capi_ctr *ctr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079{
Jan Kiszka52253032010-02-08 10:12:10 +000080 if (!try_module_get(ctr->owner))
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 return NULL;
Jan Kiszka52253032010-02-08 10:12:10 +000082 return ctr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083}
84
85static inline void
Jan Kiszka52253032010-02-08 10:12:10 +000086capi_ctr_put(struct capi_ctr *ctr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087{
Jan Kiszka52253032010-02-08 10:12:10 +000088 module_put(ctr->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089}
90
91/* ------------------------------------------------------------- */
92
93static inline struct capi_ctr *get_capi_ctr_by_nr(u16 contr)
94{
95 if (contr - 1 >= CAPI_MAXCONTR)
96 return NULL;
97
Jan Kiszka52253032010-02-08 10:12:10 +000098 return capi_controller[contr - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -070099}
100
Jan Kiszkab003f4e2010-10-17 05:18:15 +0000101static inline struct capi20_appl *__get_capi_appl_by_nr(u16 applid)
102{
103 lockdep_assert_held(&capi_controller_lock);
104
105 if (applid - 1 >= CAPI_MAXAPPL)
106 return NULL;
107
108 return capi_applications[applid - 1];
109}
110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111static inline struct capi20_appl *get_capi_appl_by_nr(u16 applid)
112{
113 if (applid - 1 >= CAPI_MAXAPPL)
114 return NULL;
115
Jan Kiszka88c896e2010-02-08 10:12:15 +0000116 return rcu_dereference(capi_applications[applid - 1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117}
118
119/* -------- util functions ------------------------------------ */
120
121static inline int capi_cmd_valid(u8 cmd)
122{
123 switch (cmd) {
124 case CAPI_ALERT:
125 case CAPI_CONNECT:
126 case CAPI_CONNECT_ACTIVE:
127 case CAPI_CONNECT_B3_ACTIVE:
128 case CAPI_CONNECT_B3:
129 case CAPI_CONNECT_B3_T90_ACTIVE:
130 case CAPI_DATA_B3:
131 case CAPI_DISCONNECT_B3:
132 case CAPI_DISCONNECT:
133 case CAPI_FACILITY:
134 case CAPI_INFO:
135 case CAPI_LISTEN:
136 case CAPI_MANUFACTURER:
137 case CAPI_RESET_B3:
138 case CAPI_SELECT_B_PROTOCOL:
139 return 1;
140 }
141 return 0;
142}
143
144static inline int capi_subcmd_valid(u8 subcmd)
145{
146 switch (subcmd) {
147 case CAPI_REQ:
148 case CAPI_CONF:
149 case CAPI_IND:
150 case CAPI_RESP:
151 return 1;
152 }
153 return 0;
154}
155
156/* ------------------------------------------------------------ */
157
Jan Kiszka52253032010-02-08 10:12:10 +0000158static void
159register_appl(struct capi_ctr *ctr, u16 applid, capi_register_params *rparam)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160{
Jan Kiszka52253032010-02-08 10:12:10 +0000161 ctr = capi_ctr_get(ctr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Jan Kiszka52253032010-02-08 10:12:10 +0000163 if (ctr)
164 ctr->register_appl(ctr, applid, rparam);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 else
Jan Kiszka52253032010-02-08 10:12:10 +0000166 printk(KERN_WARNING "%s: cannot get controller resources\n",
167 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168}
169
170
Jan Kiszka52253032010-02-08 10:12:10 +0000171static void release_appl(struct capi_ctr *ctr, u16 applid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172{
173 DBG("applid %#x", applid);
174
Jan Kiszka52253032010-02-08 10:12:10 +0000175 ctr->release_appl(ctr, applid);
176 capi_ctr_put(ctr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177}
178
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179static void notify_up(u32 contr)
180{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 struct capi20_appl *ap;
Jan Kiszka3efecf72010-02-08 10:12:12 +0000182 struct capi_ctr *ctr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 u16 applid;
184
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000185 mutex_lock(&capi_controller_lock);
186
Jan Kiszka3efecf72010-02-08 10:12:12 +0000187 if (showcapimsgs & 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 printk(KERN_DEBUG "kcapi: notify up contr %d\n", contr);
Jan Kiszka3efecf72010-02-08 10:12:12 +0000189
190 ctr = get_capi_ctr_by_nr(contr);
191 if (ctr) {
192 if (ctr->state == CAPI_CTR_RUNNING)
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000193 goto unlock_out;
Jan Kiszka3efecf72010-02-08 10:12:12 +0000194
195 ctr->state = CAPI_CTR_RUNNING;
196
197 for (applid = 1; applid <= CAPI_MAXAPPL; applid++) {
Jan Kiszkab003f4e2010-10-17 05:18:15 +0000198 ap = __get_capi_appl_by_nr(applid);
199 if (ap)
200 register_appl(ctr, applid, &ap->rparam);
Jan Kiszka3efecf72010-02-08 10:12:12 +0000201 }
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000202
203 wake_up_interruptible_all(&ctr->state_wait_queue);
Jan Kiszka3efecf72010-02-08 10:12:12 +0000204 } else
Harvey Harrison156f1ed2008-04-28 02:14:40 -0700205 printk(KERN_WARNING "%s: invalid contr %d\n", __func__, contr);
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000206
207unlock_out:
208 mutex_unlock(&capi_controller_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209}
210
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000211static void ctr_down(struct capi_ctr *ctr, int new_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212{
213 struct capi20_appl *ap;
214 u16 applid;
215
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000216 if (ctr->state == CAPI_CTR_DETECTED || ctr->state == CAPI_CTR_DETACHED)
Jan Kiszka3efecf72010-02-08 10:12:12 +0000217 return;
218
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000219 ctr->state = new_state;
Jan Kiszka3efecf72010-02-08 10:12:12 +0000220
221 memset(ctr->manu, 0, sizeof(ctr->manu));
222 memset(&ctr->version, 0, sizeof(ctr->version));
223 memset(&ctr->profile, 0, sizeof(ctr->profile));
224 memset(ctr->serial, 0, sizeof(ctr->serial));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
226 for (applid = 1; applid <= CAPI_MAXAPPL; applid++) {
Jan Kiszkab003f4e2010-10-17 05:18:15 +0000227 ap = __get_capi_appl_by_nr(applid);
Jan Kiszka88c896e2010-02-08 10:12:15 +0000228 if (ap)
Jan Kiszka3efecf72010-02-08 10:12:12 +0000229 capi_ctr_put(ctr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 }
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000231
232 wake_up_interruptible_all(&ctr->state_wait_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233}
234
Jan Kiszka3efecf72010-02-08 10:12:12 +0000235static void notify_down(u32 contr)
236{
237 struct capi_ctr *ctr;
238
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000239 mutex_lock(&capi_controller_lock);
240
Jan Kiszka3efecf72010-02-08 10:12:12 +0000241 if (showcapimsgs & 1)
242 printk(KERN_DEBUG "kcapi: notify down contr %d\n", contr);
243
244 ctr = get_capi_ctr_by_nr(contr);
245 if (ctr)
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000246 ctr_down(ctr, CAPI_CTR_DETECTED);
Jan Kiszka3efecf72010-02-08 10:12:12 +0000247 else
248 printk(KERN_WARNING "%s: invalid contr %d\n", __func__, contr);
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000249
250 mutex_unlock(&capi_controller_lock);
Jan Kiszka3efecf72010-02-08 10:12:12 +0000251}
252
Jan Kiszkaef69bb22010-02-08 10:12:13 +0000253static int
254notify_handler(struct notifier_block *nb, unsigned long val, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255{
Jan Kiszkaef69bb22010-02-08 10:12:13 +0000256 u32 contr = (long)v;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
Jan Kiszkaef69bb22010-02-08 10:12:13 +0000258 switch (val) {
259 case CAPICTR_UP:
260 notify_up(contr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 break;
Jan Kiszkaef69bb22010-02-08 10:12:13 +0000262 case CAPICTR_DOWN:
263 notify_down(contr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 break;
265 }
Jan Kiszkaef69bb22010-02-08 10:12:13 +0000266 return NOTIFY_OK;
267}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
Jan Kiszkaef69bb22010-02-08 10:12:13 +0000269static void do_notify_work(struct work_struct *work)
270{
271 struct capictr_event *event =
272 container_of(work, struct capictr_event, work);
273
274 blocking_notifier_call_chain(&ctr_notifier_list, event->type,
275 (void *)(long)event->controller);
276 kfree(event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277}
278
279/*
280 * The notifier will result in adding/deleteing of devices. Devices can
281 * only removed in user process, not in bh.
282 */
Jan Kiszkaef69bb22010-02-08 10:12:13 +0000283static int notify_push(unsigned int event_type, u32 controller)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284{
Jan Kiszkaef69bb22010-02-08 10:12:13 +0000285 struct capictr_event *event = kmalloc(sizeof(*event), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
Jan Kiszkaef69bb22010-02-08 10:12:13 +0000287 if (!event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 return -ENOMEM;
289
Jan Kiszkaef69bb22010-02-08 10:12:13 +0000290 INIT_WORK(&event->work, do_notify_work);
291 event->type = event_type;
292 event->controller = controller;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
Jan Kiszkaef69bb22010-02-08 10:12:13 +0000294 schedule_work(&event->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 return 0;
296}
297
Jan Kiszkaef69bb22010-02-08 10:12:13 +0000298int register_capictr_notifier(struct notifier_block *nb)
299{
300 return blocking_notifier_chain_register(&ctr_notifier_list, nb);
301}
302EXPORT_SYMBOL_GPL(register_capictr_notifier);
303
304int unregister_capictr_notifier(struct notifier_block *nb)
305{
306 return blocking_notifier_chain_unregister(&ctr_notifier_list, nb);
307}
308EXPORT_SYMBOL_GPL(unregister_capictr_notifier);
309
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310/* -------- Receiver ------------------------------------------ */
311
David Howellsc4028952006-11-22 14:57:56 +0000312static void recv_handler(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313{
314 struct sk_buff *skb;
David Howellsc4028952006-11-22 14:57:56 +0000315 struct capi20_appl *ap =
316 container_of(work, struct capi20_appl, recv_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
318 if ((!ap) || (ap->release_in_progress))
319 return;
320
Matthias Kaehlcke67837f22007-07-17 04:04:16 -0700321 mutex_lock(&ap->recv_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 while ((skb = skb_dequeue(&ap->recv_queue))) {
323 if (CAPIMSG_CMD(skb->data) == CAPI_DATA_B3_IND)
324 ap->nrecvdatapkt++;
325 else
326 ap->nrecvctlpkt++;
327
328 ap->recv_message(ap, skb);
329 }
Matthias Kaehlcke67837f22007-07-17 04:04:16 -0700330 mutex_unlock(&ap->recv_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331}
332
Tilman Schmidt554f2002009-04-23 02:24:21 +0000333/**
334 * capi_ctr_handle_message() - handle incoming CAPI message
Jan Kiszka52253032010-02-08 10:12:10 +0000335 * @ctr: controller descriptor structure.
Tilman Schmidt554f2002009-04-23 02:24:21 +0000336 * @appl: application ID.
337 * @skb: message.
338 *
339 * Called by hardware driver to pass a CAPI message to the application.
340 */
341
Jan Kiszka52253032010-02-08 10:12:10 +0000342void capi_ctr_handle_message(struct capi_ctr *ctr, u16 appl,
343 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344{
345 struct capi20_appl *ap;
346 int showctl = 0;
347 u8 cmd, subcmd;
Karsten Keil17f0cd22007-02-28 20:13:50 -0800348 _cdebbuf *cdb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
Jan Kiszka52253032010-02-08 10:12:10 +0000350 if (ctr->state != CAPI_CTR_RUNNING) {
Karsten Keil17f0cd22007-02-28 20:13:50 -0800351 cdb = capi_message2str(skb->data);
352 if (cdb) {
353 printk(KERN_INFO "kcapi: controller [%03d] not active, got: %s",
Jan Kiszka52253032010-02-08 10:12:10 +0000354 ctr->cnr, cdb->buf);
Karsten Keil17f0cd22007-02-28 20:13:50 -0800355 cdebbuf_free(cdb);
356 } else
357 printk(KERN_INFO "kcapi: controller [%03d] not active, cannot trace\n",
Jan Kiszka52253032010-02-08 10:12:10 +0000358 ctr->cnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 goto error;
360 }
361
362 cmd = CAPIMSG_COMMAND(skb->data);
363 subcmd = CAPIMSG_SUBCOMMAND(skb->data);
364 if (cmd == CAPI_DATA_B3 && subcmd == CAPI_IND) {
Jan Kiszka52253032010-02-08 10:12:10 +0000365 ctr->nrecvdatapkt++;
366 if (ctr->traceflag > 2)
367 showctl |= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 } else {
Jan Kiszka52253032010-02-08 10:12:10 +0000369 ctr->nrecvctlpkt++;
370 if (ctr->traceflag)
371 showctl |= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 }
Jan Kiszka52253032010-02-08 10:12:10 +0000373 showctl |= (ctr->traceflag & 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 if (showctl & 2) {
375 if (showctl & 1) {
Karsten Keil17f0cd22007-02-28 20:13:50 -0800376 printk(KERN_DEBUG "kcapi: got [%03d] id#%d %s len=%u\n",
Jan Kiszka52253032010-02-08 10:12:10 +0000377 ctr->cnr, CAPIMSG_APPID(skb->data),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 capi_cmd2str(cmd, subcmd),
379 CAPIMSG_LEN(skb->data));
380 } else {
Karsten Keil17f0cd22007-02-28 20:13:50 -0800381 cdb = capi_message2str(skb->data);
382 if (cdb) {
383 printk(KERN_DEBUG "kcapi: got [%03d] %s\n",
Jan Kiszka52253032010-02-08 10:12:10 +0000384 ctr->cnr, cdb->buf);
Karsten Keil17f0cd22007-02-28 20:13:50 -0800385 cdebbuf_free(cdb);
386 } else
387 printk(KERN_DEBUG "kcapi: got [%03d] id#%d %s len=%u, cannot trace\n",
Jan Kiszka52253032010-02-08 10:12:10 +0000388 ctr->cnr, CAPIMSG_APPID(skb->data),
Karsten Keil17f0cd22007-02-28 20:13:50 -0800389 capi_cmd2str(cmd, subcmd),
390 CAPIMSG_LEN(skb->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 }
392
393 }
394
Jan Kiszka88c896e2010-02-08 10:12:15 +0000395 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 ap = get_capi_appl_by_nr(CAPIMSG_APPID(skb->data));
Jan Kiszka88c896e2010-02-08 10:12:15 +0000397 if (!ap) {
398 rcu_read_unlock();
Karsten Keil17f0cd22007-02-28 20:13:50 -0800399 cdb = capi_message2str(skb->data);
400 if (cdb) {
401 printk(KERN_ERR "kcapi: handle_message: applid %d state released (%s)\n",
402 CAPIMSG_APPID(skb->data), cdb->buf);
403 cdebbuf_free(cdb);
404 } else
405 printk(KERN_ERR "kcapi: handle_message: applid %d state released (%s) cannot trace\n",
406 CAPIMSG_APPID(skb->data),
407 capi_cmd2str(cmd, subcmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 goto error;
409 }
410 skb_queue_tail(&ap->recv_queue, skb);
411 schedule_work(&ap->recv_work);
Jan Kiszka88c896e2010-02-08 10:12:15 +0000412 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
414 return;
415
416error:
417 kfree_skb(skb);
418}
419
420EXPORT_SYMBOL(capi_ctr_handle_message);
421
Tilman Schmidt554f2002009-04-23 02:24:21 +0000422/**
423 * capi_ctr_ready() - signal CAPI controller ready
Jan Kiszka52253032010-02-08 10:12:10 +0000424 * @ctr: controller descriptor structure.
Tilman Schmidt554f2002009-04-23 02:24:21 +0000425 *
426 * Called by hardware driver to signal that the controller is up and running.
427 */
428
Jan Kiszka52253032010-02-08 10:12:10 +0000429void capi_ctr_ready(struct capi_ctr *ctr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430{
Jan Kiszka52253032010-02-08 10:12:10 +0000431 printk(KERN_NOTICE "kcapi: controller [%03d] \"%s\" ready.\n",
432 ctr->cnr, ctr->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
Jan Kiszkaef69bb22010-02-08 10:12:13 +0000434 notify_push(CAPICTR_UP, ctr->cnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435}
436
437EXPORT_SYMBOL(capi_ctr_ready);
438
Tilman Schmidt554f2002009-04-23 02:24:21 +0000439/**
Tilman Schmidt4e329972009-06-07 09:09:23 +0000440 * capi_ctr_down() - signal CAPI controller not ready
Jan Kiszka52253032010-02-08 10:12:10 +0000441 * @ctr: controller descriptor structure.
Tilman Schmidt554f2002009-04-23 02:24:21 +0000442 *
443 * Called by hardware driver to signal that the controller is down and
444 * unavailable for use.
445 */
446
Jan Kiszka52253032010-02-08 10:12:10 +0000447void capi_ctr_down(struct capi_ctr *ctr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448{
Jan Kiszka52253032010-02-08 10:12:10 +0000449 printk(KERN_NOTICE "kcapi: controller [%03d] down.\n", ctr->cnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
Jan Kiszkaef69bb22010-02-08 10:12:13 +0000451 notify_push(CAPICTR_DOWN, ctr->cnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452}
453
Tilman Schmidt4e329972009-06-07 09:09:23 +0000454EXPORT_SYMBOL(capi_ctr_down);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
Tilman Schmidt554f2002009-04-23 02:24:21 +0000456/**
457 * capi_ctr_suspend_output() - suspend controller
Jan Kiszka52253032010-02-08 10:12:10 +0000458 * @ctr: controller descriptor structure.
Tilman Schmidt554f2002009-04-23 02:24:21 +0000459 *
460 * Called by hardware driver to stop data flow.
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000461 *
462 * Note: The caller is responsible for synchronizing concurrent state changes
463 * as well as invocations of capi_ctr_handle_message.
Tilman Schmidt554f2002009-04-23 02:24:21 +0000464 */
465
Jan Kiszka52253032010-02-08 10:12:10 +0000466void capi_ctr_suspend_output(struct capi_ctr *ctr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467{
Jan Kiszka52253032010-02-08 10:12:10 +0000468 if (!ctr->blocked) {
469 printk(KERN_DEBUG "kcapi: controller [%03d] suspend\n",
470 ctr->cnr);
471 ctr->blocked = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 }
473}
474
475EXPORT_SYMBOL(capi_ctr_suspend_output);
476
Tilman Schmidt554f2002009-04-23 02:24:21 +0000477/**
478 * capi_ctr_resume_output() - resume controller
Jan Kiszka52253032010-02-08 10:12:10 +0000479 * @ctr: controller descriptor structure.
Tilman Schmidt554f2002009-04-23 02:24:21 +0000480 *
481 * Called by hardware driver to resume data flow.
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000482 *
483 * Note: The caller is responsible for synchronizing concurrent state changes
484 * as well as invocations of capi_ctr_handle_message.
Tilman Schmidt554f2002009-04-23 02:24:21 +0000485 */
486
Jan Kiszka52253032010-02-08 10:12:10 +0000487void capi_ctr_resume_output(struct capi_ctr *ctr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488{
Jan Kiszka52253032010-02-08 10:12:10 +0000489 if (ctr->blocked) {
490 printk(KERN_DEBUG "kcapi: controller [%03d] resumed\n",
491 ctr->cnr);
492 ctr->blocked = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 }
494}
495
496EXPORT_SYMBOL(capi_ctr_resume_output);
497
498/* ------------------------------------------------------------- */
499
Tilman Schmidt554f2002009-04-23 02:24:21 +0000500/**
501 * attach_capi_ctr() - register CAPI controller
Jan Kiszka52253032010-02-08 10:12:10 +0000502 * @ctr: controller descriptor structure.
Tilman Schmidt554f2002009-04-23 02:24:21 +0000503 *
504 * Called by hardware driver to register a controller with the CAPI subsystem.
505 * Return value: 0 on success, error code < 0 on error
506 */
507
Jan Kiszka52253032010-02-08 10:12:10 +0000508int attach_capi_ctr(struct capi_ctr *ctr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509{
510 int i;
511
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000512 mutex_lock(&capi_controller_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
514 for (i = 0; i < CAPI_MAXCONTR; i++) {
Jan Kiszka52253032010-02-08 10:12:10 +0000515 if (!capi_controller[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 break;
517 }
518 if (i == CAPI_MAXCONTR) {
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000519 mutex_unlock(&capi_controller_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 printk(KERN_ERR "kcapi: out of controller slots\n");
521 return -EBUSY;
522 }
Jan Kiszka52253032010-02-08 10:12:10 +0000523 capi_controller[i] = ctr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
Jan Kiszka52253032010-02-08 10:12:10 +0000525 ctr->nrecvctlpkt = 0;
526 ctr->nrecvdatapkt = 0;
527 ctr->nsentctlpkt = 0;
528 ctr->nsentdatapkt = 0;
529 ctr->cnr = i + 1;
530 ctr->state = CAPI_CTR_DETECTED;
531 ctr->blocked = 0;
532 ctr->traceflag = showcapimsgs;
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000533 init_waitqueue_head(&ctr->state_wait_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
Jan Kiszka52253032010-02-08 10:12:10 +0000535 sprintf(ctr->procfn, "capi/controllers/%d", ctr->cnr);
536 ctr->procent = proc_create_data(ctr->procfn, 0, NULL, ctr->proc_fops, ctr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
Jan Kiszka52253032010-02-08 10:12:10 +0000538 ncontrollers++;
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000539
540 mutex_unlock(&capi_controller_lock);
541
Jan Kiszka52253032010-02-08 10:12:10 +0000542 printk(KERN_NOTICE "kcapi: controller [%03d]: %s attached\n",
543 ctr->cnr, ctr->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 return 0;
545}
546
547EXPORT_SYMBOL(attach_capi_ctr);
548
Tilman Schmidt554f2002009-04-23 02:24:21 +0000549/**
550 * detach_capi_ctr() - unregister CAPI controller
Jan Kiszka52253032010-02-08 10:12:10 +0000551 * @ctr: controller descriptor structure.
Tilman Schmidt554f2002009-04-23 02:24:21 +0000552 *
553 * Called by hardware driver to remove the registration of a controller
554 * with the CAPI subsystem.
555 * Return value: 0 on success, error code < 0 on error
556 */
557
Jan Kiszka52253032010-02-08 10:12:10 +0000558int detach_capi_ctr(struct capi_ctr *ctr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559{
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000560 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000562 mutex_lock(&capi_controller_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000564 ctr_down(ctr, CAPI_CTR_DETACHED);
565
566 if (capi_controller[ctr->cnr - 1] != ctr) {
567 err = -EINVAL;
568 goto unlock_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 }
Jan Kiszka52253032010-02-08 10:12:10 +0000570 capi_controller[ctr->cnr - 1] = NULL;
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000571 ncontrollers--;
572
573 if (ctr->procent)
574 remove_proc_entry(ctr->procfn, NULL);
575
Jan Kiszka52253032010-02-08 10:12:10 +0000576 printk(KERN_NOTICE "kcapi: controller [%03d]: %s unregistered\n",
577 ctr->cnr, ctr->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000579unlock_out:
580 mutex_unlock(&capi_controller_lock);
581
582 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583}
584
585EXPORT_SYMBOL(detach_capi_ctr);
586
Tilman Schmidt554f2002009-04-23 02:24:21 +0000587/**
588 * register_capi_driver() - register CAPI driver
589 * @driver: driver descriptor structure.
590 *
591 * Called by hardware driver to register itself with the CAPI subsystem.
592 */
593
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594void register_capi_driver(struct capi_driver *driver)
595{
Jan Kiszka9717fb82010-02-08 10:12:11 +0000596 mutex_lock(&capi_drivers_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 list_add_tail(&driver->list, &capi_drivers);
Jan Kiszka9717fb82010-02-08 10:12:11 +0000598 mutex_unlock(&capi_drivers_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599}
600
601EXPORT_SYMBOL(register_capi_driver);
602
Tilman Schmidt554f2002009-04-23 02:24:21 +0000603/**
604 * unregister_capi_driver() - unregister CAPI driver
605 * @driver: driver descriptor structure.
606 *
607 * Called by hardware driver to unregister itself from the CAPI subsystem.
608 */
609
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610void unregister_capi_driver(struct capi_driver *driver)
611{
Jan Kiszka9717fb82010-02-08 10:12:11 +0000612 mutex_lock(&capi_drivers_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 list_del(&driver->list);
Jan Kiszka9717fb82010-02-08 10:12:11 +0000614 mutex_unlock(&capi_drivers_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615}
616
617EXPORT_SYMBOL(unregister_capi_driver);
618
619/* ------------------------------------------------------------- */
620/* -------- CAPI2.0 Interface ---------------------------------- */
621/* ------------------------------------------------------------- */
622
Tilman Schmidt554f2002009-04-23 02:24:21 +0000623/**
624 * capi20_isinstalled() - CAPI 2.0 operation CAPI_INSTALLED
625 *
626 * Return value: CAPI result code (CAPI_NOERROR if at least one ISDN controller
627 * is ready for use, CAPI_REGNOTINSTALLED otherwise)
628 */
629
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630u16 capi20_isinstalled(void)
631{
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000632 u16 ret = CAPI_REGNOTINSTALLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 int i;
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000634
635 mutex_lock(&capi_controller_lock);
636
637 for (i = 0; i < CAPI_MAXCONTR; i++)
Jan Kiszka52253032010-02-08 10:12:10 +0000638 if (capi_controller[i] &&
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000639 capi_controller[i]->state == CAPI_CTR_RUNNING) {
640 ret = CAPI_NOERROR;
641 break;
642 }
643
644 mutex_unlock(&capi_controller_lock);
645
646 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647}
648
649EXPORT_SYMBOL(capi20_isinstalled);
650
Tilman Schmidt554f2002009-04-23 02:24:21 +0000651/**
652 * capi20_register() - CAPI 2.0 operation CAPI_REGISTER
653 * @ap: CAPI application descriptor structure.
654 *
655 * Register an application's presence with CAPI.
656 * A unique application ID is assigned and stored in @ap->applid.
657 * After this function returns successfully, the message receive
658 * callback function @ap->recv_message() may be called at any time
659 * until capi20_release() has been called for the same @ap.
660 * Return value: CAPI result code
661 */
662
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663u16 capi20_register(struct capi20_appl *ap)
664{
665 int i;
666 u16 applid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
668 DBG("");
669
670 if (ap->rparam.datablklen < 128)
671 return CAPI_LOGBLKSIZETOSMALL;
672
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 ap->nrecvctlpkt = 0;
674 ap->nrecvdatapkt = 0;
675 ap->nsentctlpkt = 0;
676 ap->nsentdatapkt = 0;
Matthias Kaehlcke67837f22007-07-17 04:04:16 -0700677 mutex_init(&ap->recv_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 skb_queue_head_init(&ap->recv_queue);
David Howellsc4028952006-11-22 14:57:56 +0000679 INIT_WORK(&ap->recv_work, recv_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 ap->release_in_progress = 0;
681
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000682 mutex_lock(&capi_controller_lock);
683
Jan Kiszka88c896e2010-02-08 10:12:15 +0000684 for (applid = 1; applid <= CAPI_MAXAPPL; applid++) {
685 if (capi_applications[applid - 1] == NULL)
686 break;
687 }
688 if (applid > CAPI_MAXAPPL) {
689 mutex_unlock(&capi_controller_lock);
690 return CAPI_TOOMANYAPPLS;
691 }
692
693 ap->applid = applid;
694 capi_applications[applid - 1] = ap;
695
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 for (i = 0; i < CAPI_MAXCONTR; i++) {
Jan Kiszka52253032010-02-08 10:12:10 +0000697 if (!capi_controller[i] ||
698 capi_controller[i]->state != CAPI_CTR_RUNNING)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 continue;
Jan Kiszka52253032010-02-08 10:12:10 +0000700 register_appl(capi_controller[i], applid, &ap->rparam);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 }
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000702
703 mutex_unlock(&capi_controller_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
705 if (showcapimsgs & 1) {
706 printk(KERN_DEBUG "kcapi: appl %d up\n", applid);
707 }
708
709 return CAPI_NOERROR;
710}
711
712EXPORT_SYMBOL(capi20_register);
713
Tilman Schmidt554f2002009-04-23 02:24:21 +0000714/**
715 * capi20_release() - CAPI 2.0 operation CAPI_RELEASE
716 * @ap: CAPI application descriptor structure.
717 *
718 * Terminate an application's registration with CAPI.
719 * After this function returns successfully, the message receive
720 * callback function @ap->recv_message() will no longer be called.
721 * Return value: CAPI result code
722 */
723
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724u16 capi20_release(struct capi20_appl *ap)
725{
726 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
728 DBG("applid %#x", ap->applid);
729
Jan Kiszka88c896e2010-02-08 10:12:15 +0000730 mutex_lock(&capi_controller_lock);
731
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 ap->release_in_progress = 1;
733 capi_applications[ap->applid - 1] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
Jan Kiszka88c896e2010-02-08 10:12:15 +0000735 synchronize_rcu();
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000736
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 for (i = 0; i < CAPI_MAXCONTR; i++) {
Jan Kiszka52253032010-02-08 10:12:10 +0000738 if (!capi_controller[i] ||
739 capi_controller[i]->state != CAPI_CTR_RUNNING)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 continue;
Jan Kiszka52253032010-02-08 10:12:10 +0000741 release_appl(capi_controller[i], ap->applid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 }
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000743
744 mutex_unlock(&capi_controller_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
746 flush_scheduled_work();
747 skb_queue_purge(&ap->recv_queue);
748
749 if (showcapimsgs & 1) {
750 printk(KERN_DEBUG "kcapi: appl %d down\n", ap->applid);
751 }
752
753 return CAPI_NOERROR;
754}
755
756EXPORT_SYMBOL(capi20_release);
757
Tilman Schmidt554f2002009-04-23 02:24:21 +0000758/**
759 * capi20_put_message() - CAPI 2.0 operation CAPI_PUT_MESSAGE
760 * @ap: CAPI application descriptor structure.
761 * @skb: CAPI message.
762 *
763 * Transfer a single message to CAPI.
764 * Return value: CAPI result code
765 */
766
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767u16 capi20_put_message(struct capi20_appl *ap, struct sk_buff *skb)
768{
Jan Kiszka52253032010-02-08 10:12:10 +0000769 struct capi_ctr *ctr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 int showctl = 0;
771 u8 cmd, subcmd;
772
773 DBG("applid %#x", ap->applid);
774
Jan Kiszka52253032010-02-08 10:12:10 +0000775 if (ncontrollers == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 return CAPI_REGNOTINSTALLED;
777 if ((ap->applid == 0) || ap->release_in_progress)
778 return CAPI_ILLAPPNR;
779 if (skb->len < 12
780 || !capi_cmd_valid(CAPIMSG_COMMAND(skb->data))
781 || !capi_subcmd_valid(CAPIMSG_SUBCOMMAND(skb->data)))
782 return CAPI_ILLCMDORSUBCMDORMSGTOSMALL;
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000783
784 /*
785 * The controller reference is protected by the existence of the
786 * application passed to us. We assume that the caller properly
787 * synchronizes this service with capi20_release.
788 */
Jan Kiszka52253032010-02-08 10:12:10 +0000789 ctr = get_capi_ctr_by_nr(CAPIMSG_CONTROLLER(skb->data));
Jan Kiszkac6af0432010-02-08 10:12:43 +0000790 if (!ctr || ctr->state != CAPI_CTR_RUNNING)
791 return CAPI_REGNOTINSTALLED;
Jan Kiszka52253032010-02-08 10:12:10 +0000792 if (ctr->blocked)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 return CAPI_SENDQUEUEFULL;
794
795 cmd = CAPIMSG_COMMAND(skb->data);
796 subcmd = CAPIMSG_SUBCOMMAND(skb->data);
797
798 if (cmd == CAPI_DATA_B3 && subcmd== CAPI_REQ) {
Jan Kiszka52253032010-02-08 10:12:10 +0000799 ctr->nsentdatapkt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 ap->nsentdatapkt++;
Jan Kiszka52253032010-02-08 10:12:10 +0000801 if (ctr->traceflag > 2)
802 showctl |= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 } else {
Jan Kiszka52253032010-02-08 10:12:10 +0000804 ctr->nsentctlpkt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 ap->nsentctlpkt++;
Jan Kiszka52253032010-02-08 10:12:10 +0000806 if (ctr->traceflag)
807 showctl |= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 }
Jan Kiszka52253032010-02-08 10:12:10 +0000809 showctl |= (ctr->traceflag & 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 if (showctl & 2) {
811 if (showctl & 1) {
Karsten Keil17f0cd22007-02-28 20:13:50 -0800812 printk(KERN_DEBUG "kcapi: put [%03d] id#%d %s len=%u\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 CAPIMSG_CONTROLLER(skb->data),
814 CAPIMSG_APPID(skb->data),
815 capi_cmd2str(cmd, subcmd),
816 CAPIMSG_LEN(skb->data));
817 } else {
Karsten Keil17f0cd22007-02-28 20:13:50 -0800818 _cdebbuf *cdb = capi_message2str(skb->data);
819 if (cdb) {
820 printk(KERN_DEBUG "kcapi: put [%03d] %s\n",
821 CAPIMSG_CONTROLLER(skb->data),
822 cdb->buf);
823 cdebbuf_free(cdb);
824 } else
825 printk(KERN_DEBUG "kcapi: put [%03d] id#%d %s len=%u cannot trace\n",
826 CAPIMSG_CONTROLLER(skb->data),
827 CAPIMSG_APPID(skb->data),
828 capi_cmd2str(cmd, subcmd),
829 CAPIMSG_LEN(skb->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 }
Jan Kiszka52253032010-02-08 10:12:10 +0000832 return ctr->send_message(ctr, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833}
834
835EXPORT_SYMBOL(capi20_put_message);
836
Tilman Schmidt554f2002009-04-23 02:24:21 +0000837/**
838 * capi20_get_manufacturer() - CAPI 2.0 operation CAPI_GET_MANUFACTURER
839 * @contr: controller number.
840 * @buf: result buffer (64 bytes).
841 *
842 * Retrieve information about the manufacturer of the specified ISDN controller
843 * or (for @contr == 0) the driver itself.
844 * Return value: CAPI result code
845 */
846
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847u16 capi20_get_manufacturer(u32 contr, u8 *buf)
848{
Jan Kiszka52253032010-02-08 10:12:10 +0000849 struct capi_ctr *ctr;
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000850 u16 ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
852 if (contr == 0) {
853 strlcpy(buf, capi_manufakturer, CAPI_MANUFACTURER_LEN);
854 return CAPI_NOERROR;
855 }
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000856
857 mutex_lock(&capi_controller_lock);
858
Jan Kiszka52253032010-02-08 10:12:10 +0000859 ctr = get_capi_ctr_by_nr(contr);
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000860 if (ctr && ctr->state == CAPI_CTR_RUNNING) {
861 strlcpy(buf, ctr->manu, CAPI_MANUFACTURER_LEN);
862 ret = CAPI_NOERROR;
863 } else
864 ret = CAPI_REGNOTINSTALLED;
865
866 mutex_unlock(&capi_controller_lock);
867 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868}
869
870EXPORT_SYMBOL(capi20_get_manufacturer);
871
Tilman Schmidt554f2002009-04-23 02:24:21 +0000872/**
873 * capi20_get_version() - CAPI 2.0 operation CAPI_GET_VERSION
874 * @contr: controller number.
875 * @verp: result structure.
876 *
877 * Retrieve version information for the specified ISDN controller
878 * or (for @contr == 0) the driver itself.
879 * Return value: CAPI result code
880 */
881
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882u16 capi20_get_version(u32 contr, struct capi_version *verp)
883{
Jan Kiszka52253032010-02-08 10:12:10 +0000884 struct capi_ctr *ctr;
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000885 u16 ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
887 if (contr == 0) {
888 *verp = driver_version;
889 return CAPI_NOERROR;
890 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000892 mutex_lock(&capi_controller_lock);
893
894 ctr = get_capi_ctr_by_nr(contr);
895 if (ctr && ctr->state == CAPI_CTR_RUNNING) {
896 memcpy(verp, &ctr->version, sizeof(capi_version));
897 ret = CAPI_NOERROR;
898 } else
899 ret = CAPI_REGNOTINSTALLED;
900
901 mutex_unlock(&capi_controller_lock);
902 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903}
904
905EXPORT_SYMBOL(capi20_get_version);
906
Tilman Schmidt554f2002009-04-23 02:24:21 +0000907/**
908 * capi20_get_serial() - CAPI 2.0 operation CAPI_GET_SERIAL_NUMBER
909 * @contr: controller number.
910 * @serial: result buffer (8 bytes).
911 *
912 * Retrieve the serial number of the specified ISDN controller
913 * or (for @contr == 0) the driver itself.
914 * Return value: CAPI result code
915 */
916
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917u16 capi20_get_serial(u32 contr, u8 *serial)
918{
Jan Kiszka52253032010-02-08 10:12:10 +0000919 struct capi_ctr *ctr;
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000920 u16 ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
922 if (contr == 0) {
923 strlcpy(serial, driver_serial, CAPI_SERIAL_LEN);
924 return CAPI_NOERROR;
925 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000927 mutex_lock(&capi_controller_lock);
928
929 ctr = get_capi_ctr_by_nr(contr);
930 if (ctr && ctr->state == CAPI_CTR_RUNNING) {
931 strlcpy(serial, ctr->serial, CAPI_SERIAL_LEN);
932 ret = CAPI_NOERROR;
933 } else
934 ret = CAPI_REGNOTINSTALLED;
935
936 mutex_unlock(&capi_controller_lock);
937 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938}
939
940EXPORT_SYMBOL(capi20_get_serial);
941
Tilman Schmidt554f2002009-04-23 02:24:21 +0000942/**
943 * capi20_get_profile() - CAPI 2.0 operation CAPI_GET_PROFILE
944 * @contr: controller number.
945 * @profp: result structure.
946 *
947 * Retrieve capability information for the specified ISDN controller
948 * or (for @contr == 0) the number of installed controllers.
949 * Return value: CAPI result code
950 */
951
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952u16 capi20_get_profile(u32 contr, struct capi_profile *profp)
953{
Jan Kiszka52253032010-02-08 10:12:10 +0000954 struct capi_ctr *ctr;
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000955 u16 ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
957 if (contr == 0) {
Jan Kiszka52253032010-02-08 10:12:10 +0000958 profp->ncontroller = ncontrollers;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 return CAPI_NOERROR;
960 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000962 mutex_lock(&capi_controller_lock);
963
964 ctr = get_capi_ctr_by_nr(contr);
965 if (ctr && ctr->state == CAPI_CTR_RUNNING) {
966 memcpy(profp, &ctr->profile, sizeof(struct capi_profile));
967 ret = CAPI_NOERROR;
968 } else
969 ret = CAPI_REGNOTINSTALLED;
970
971 mutex_unlock(&capi_controller_lock);
972 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973}
974
975EXPORT_SYMBOL(capi20_get_profile);
976
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000977/* Must be called with capi_controller_lock held. */
978static int wait_on_ctr_state(struct capi_ctr *ctr, unsigned int state)
979{
980 DEFINE_WAIT(wait);
981 int retval = 0;
982
983 ctr = capi_ctr_get(ctr);
984 if (!ctr)
985 return -ESRCH;
986
987 for (;;) {
988 prepare_to_wait(&ctr->state_wait_queue, &wait,
989 TASK_INTERRUPTIBLE);
990
991 if (ctr->state == state)
992 break;
993 if (ctr->state == CAPI_CTR_DETACHED) {
994 retval = -ESRCH;
995 break;
996 }
997 if (signal_pending(current)) {
998 retval = -EINTR;
999 break;
1000 }
1001
1002 mutex_unlock(&capi_controller_lock);
1003 schedule();
1004 mutex_lock(&capi_controller_lock);
1005 }
1006 finish_wait(&ctr->state_wait_queue, &wait);
1007
1008 capi_ctr_put(ctr);
1009
1010 return retval;
1011}
1012
Robert P. J. Day37772ac2008-04-28 02:14:42 -07001013#ifdef AVMB1_COMPAT
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014static int old_capi_manufacturer(unsigned int cmd, void __user *data)
1015{
1016 avmb1_loadandconfigdef ldef;
1017 avmb1_extcarddef cdef;
1018 avmb1_resetdef rdef;
1019 capicardparams cparams;
Jan Kiszka52253032010-02-08 10:12:10 +00001020 struct capi_ctr *ctr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 struct capi_driver *driver = NULL;
1022 capiloaddata ldata;
1023 struct list_head *l;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 int retval;
1025
1026 switch (cmd) {
1027 case AVMB1_ADDCARD:
1028 case AVMB1_ADDCARD_WITH_TYPE:
1029 if (cmd == AVMB1_ADDCARD) {
1030 if ((retval = copy_from_user(&cdef, data,
1031 sizeof(avmb1_carddef))))
Dan Carpenter60a57112010-06-02 23:56:13 +00001032 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 cdef.cardtype = AVM_CARDTYPE_B1;
1034 } else {
1035 if ((retval = copy_from_user(&cdef, data,
1036 sizeof(avmb1_extcarddef))))
Dan Carpenter60a57112010-06-02 23:56:13 +00001037 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 }
1039 cparams.port = cdef.port;
1040 cparams.irq = cdef.irq;
1041 cparams.cardnr = cdef.cardnr;
1042
Jan Kiszka9717fb82010-02-08 10:12:11 +00001043 mutex_lock(&capi_drivers_lock);
1044
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 switch (cdef.cardtype) {
1046 case AVM_CARDTYPE_B1:
1047 list_for_each(l, &capi_drivers) {
1048 driver = list_entry(l, struct capi_driver, list);
1049 if (strcmp(driver->name, "b1isa") == 0)
1050 break;
1051 }
1052 break;
1053 case AVM_CARDTYPE_T1:
1054 list_for_each(l, &capi_drivers) {
1055 driver = list_entry(l, struct capi_driver, list);
1056 if (strcmp(driver->name, "t1isa") == 0)
1057 break;
1058 }
1059 break;
1060 default:
1061 driver = NULL;
1062 break;
1063 }
1064 if (!driver) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 printk(KERN_ERR "kcapi: driver not loaded.\n");
Jan Kiszka9717fb82010-02-08 10:12:11 +00001066 retval = -EIO;
1067 } else if (!driver->add_card) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 printk(KERN_ERR "kcapi: driver has no add card function.\n");
Jan Kiszka9717fb82010-02-08 10:12:11 +00001069 retval = -EIO;
1070 } else
1071 retval = driver->add_card(driver, &cparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
Jan Kiszka9717fb82010-02-08 10:12:11 +00001073 mutex_unlock(&capi_drivers_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 return retval;
1075
1076 case AVMB1_LOAD:
1077 case AVMB1_LOAD_AND_CONFIG:
1078
1079 if (cmd == AVMB1_LOAD) {
1080 if (copy_from_user(&ldef, data,
1081 sizeof(avmb1_loaddef)))
1082 return -EFAULT;
1083 ldef.t4config.len = 0;
1084 ldef.t4config.data = NULL;
1085 } else {
1086 if (copy_from_user(&ldef, data,
1087 sizeof(avmb1_loadandconfigdef)))
1088 return -EFAULT;
1089 }
Jan Kiszka0ca3a012010-02-08 10:12:14 +00001090
1091 mutex_lock(&capi_controller_lock);
1092
Jan Kiszka52253032010-02-08 10:12:10 +00001093 ctr = get_capi_ctr_by_nr(ldef.contr);
Jan Kiszka0ca3a012010-02-08 10:12:14 +00001094 if (!ctr) {
1095 retval = -EINVAL;
1096 goto load_unlock_out;
1097 }
1098
Jan Kiszka52253032010-02-08 10:12:10 +00001099 if (ctr->load_firmware == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 printk(KERN_DEBUG "kcapi: load: no load function\n");
Jan Kiszka0ca3a012010-02-08 10:12:14 +00001101 retval = -ESRCH;
1102 goto load_unlock_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 }
1104
1105 if (ldef.t4file.len <= 0) {
1106 printk(KERN_DEBUG "kcapi: load: invalid parameter: length of t4file is %d ?\n", ldef.t4file.len);
Jan Kiszka0ca3a012010-02-08 10:12:14 +00001107 retval = -EINVAL;
1108 goto load_unlock_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 }
Harvey Harrison2f9e9b62008-04-28 02:14:37 -07001110 if (ldef.t4file.data == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 printk(KERN_DEBUG "kcapi: load: invalid parameter: dataptr is 0\n");
Jan Kiszka0ca3a012010-02-08 10:12:14 +00001112 retval = -EINVAL;
1113 goto load_unlock_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 }
1115
1116 ldata.firmware.user = 1;
1117 ldata.firmware.data = ldef.t4file.data;
1118 ldata.firmware.len = ldef.t4file.len;
1119 ldata.configuration.user = 1;
1120 ldata.configuration.data = ldef.t4config.data;
1121 ldata.configuration.len = ldef.t4config.len;
1122
Jan Kiszka52253032010-02-08 10:12:10 +00001123 if (ctr->state != CAPI_CTR_DETECTED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 printk(KERN_INFO "kcapi: load: contr=%d not in detect state\n", ldef.contr);
Jan Kiszka0ca3a012010-02-08 10:12:14 +00001125 retval = -EBUSY;
1126 goto load_unlock_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 }
Jan Kiszka52253032010-02-08 10:12:10 +00001128 ctr->state = CAPI_CTR_LOADING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
Jan Kiszka52253032010-02-08 10:12:10 +00001130 retval = ctr->load_firmware(ctr, &ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 if (retval) {
Jan Kiszka52253032010-02-08 10:12:10 +00001132 ctr->state = CAPI_CTR_DETECTED;
Jan Kiszka0ca3a012010-02-08 10:12:14 +00001133 goto load_unlock_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 }
1135
Jan Kiszka0ca3a012010-02-08 10:12:14 +00001136 retval = wait_on_ctr_state(ctr, CAPI_CTR_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
Jan Kiszka0ca3a012010-02-08 10:12:14 +00001138load_unlock_out:
1139 mutex_unlock(&capi_controller_lock);
1140 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141
1142 case AVMB1_RESETCARD:
1143 if (copy_from_user(&rdef, data, sizeof(avmb1_resetdef)))
1144 return -EFAULT;
Jan Kiszka0ca3a012010-02-08 10:12:14 +00001145
1146 retval = 0;
1147
1148 mutex_lock(&capi_controller_lock);
1149
Jan Kiszka52253032010-02-08 10:12:10 +00001150 ctr = get_capi_ctr_by_nr(rdef.contr);
Jan Kiszka0ca3a012010-02-08 10:12:14 +00001151 if (!ctr) {
1152 retval = -ESRCH;
1153 goto reset_unlock_out;
1154 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
Jan Kiszka52253032010-02-08 10:12:10 +00001156 if (ctr->state == CAPI_CTR_DETECTED)
Jan Kiszka0ca3a012010-02-08 10:12:14 +00001157 goto reset_unlock_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
Tilman Schmidt85a83562010-05-23 01:02:08 +00001159 if (ctr->reset_ctr == NULL) {
1160 printk(KERN_DEBUG "kcapi: reset: no reset function\n");
1161 retval = -ESRCH;
1162 goto reset_unlock_out;
1163 }
1164
Jan Kiszka52253032010-02-08 10:12:10 +00001165 ctr->reset_ctr(ctr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
Jan Kiszka0ca3a012010-02-08 10:12:14 +00001167 retval = wait_on_ctr_state(ctr, CAPI_CTR_DETECTED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168
Jan Kiszka0ca3a012010-02-08 10:12:14 +00001169reset_unlock_out:
1170 mutex_unlock(&capi_controller_lock);
1171 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 }
1173 return -EINVAL;
1174}
1175#endif
1176
Tilman Schmidt554f2002009-04-23 02:24:21 +00001177/**
1178 * capi20_manufacturer() - CAPI 2.0 operation CAPI_MANUFACTURER
1179 * @cmd: command.
1180 * @data: parameter.
1181 *
1182 * Perform manufacturer specific command.
1183 * Return value: CAPI result code
1184 */
1185
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186int capi20_manufacturer(unsigned int cmd, void __user *data)
1187{
Jan Kiszka52253032010-02-08 10:12:10 +00001188 struct capi_ctr *ctr;
Jan Kiszka0ca3a012010-02-08 10:12:14 +00001189 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
1191 switch (cmd) {
Robert P. J. Day37772ac2008-04-28 02:14:42 -07001192#ifdef AVMB1_COMPAT
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 case AVMB1_LOAD:
1194 case AVMB1_LOAD_AND_CONFIG:
1195 case AVMB1_RESETCARD:
1196 case AVMB1_GET_CARDINFO:
1197 case AVMB1_REMOVECARD:
1198 return old_capi_manufacturer(cmd, data);
1199#endif
1200 case KCAPI_CMD_TRACE:
1201 {
1202 kcapi_flagdef fdef;
1203
1204 if (copy_from_user(&fdef, data, sizeof(kcapi_flagdef)))
1205 return -EFAULT;
1206
Jan Kiszka0ca3a012010-02-08 10:12:14 +00001207 mutex_lock(&capi_controller_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
Jan Kiszka0ca3a012010-02-08 10:12:14 +00001209 ctr = get_capi_ctr_by_nr(fdef.contr);
1210 if (ctr) {
1211 ctr->traceflag = fdef.flag;
1212 printk(KERN_INFO "kcapi: contr [%03d] set trace=%d\n",
1213 ctr->cnr, ctr->traceflag);
1214 retval = 0;
1215 } else
1216 retval = -ESRCH;
1217
1218 mutex_unlock(&capi_controller_lock);
1219
1220 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 }
1222 case KCAPI_CMD_ADDCARD:
1223 {
1224 struct list_head *l;
1225 struct capi_driver *driver = NULL;
1226 capicardparams cparams;
1227 kcapi_carddef cdef;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228
1229 if ((retval = copy_from_user(&cdef, data, sizeof(cdef))))
Dan Carpenter60a57112010-06-02 23:56:13 +00001230 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
1232 cparams.port = cdef.port;
1233 cparams.irq = cdef.irq;
1234 cparams.membase = cdef.membase;
1235 cparams.cardnr = cdef.cardnr;
1236 cparams.cardtype = 0;
1237 cdef.driver[sizeof(cdef.driver)-1] = 0;
1238
Jan Kiszka9717fb82010-02-08 10:12:11 +00001239 mutex_lock(&capi_drivers_lock);
1240
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 list_for_each(l, &capi_drivers) {
1242 driver = list_entry(l, struct capi_driver, list);
1243 if (strcmp(driver->name, cdef.driver) == 0)
1244 break;
1245 }
Harvey Harrison2f9e9b62008-04-28 02:14:37 -07001246 if (driver == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 printk(KERN_ERR "kcapi: driver \"%s\" not loaded.\n",
1248 cdef.driver);
Jan Kiszka9717fb82010-02-08 10:12:11 +00001249 retval = -ESRCH;
1250 } else if (!driver->add_card) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 printk(KERN_ERR "kcapi: driver \"%s\" has no add card function.\n", cdef.driver);
Jan Kiszka9717fb82010-02-08 10:12:11 +00001252 retval = -EIO;
1253 } else
1254 retval = driver->add_card(driver, &cparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255
Jan Kiszka9717fb82010-02-08 10:12:11 +00001256 mutex_unlock(&capi_drivers_lock);
1257 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 }
1259
1260 default:
1261 printk(KERN_ERR "kcapi: manufacturer command %d unknown.\n",
1262 cmd);
1263 break;
1264
1265 }
1266 return -EINVAL;
1267}
1268
1269EXPORT_SYMBOL(capi20_manufacturer);
1270
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271/* ------------------------------------------------------------- */
1272/* -------- Init & Cleanup ------------------------------------- */
1273/* ------------------------------------------------------------- */
1274
1275/*
1276 * init / exit functions
1277 */
1278
Jan Kiszkaef69bb22010-02-08 10:12:13 +00001279static struct notifier_block capictr_nb = {
1280 .notifier_call = notify_handler,
1281 .priority = INT_MAX,
1282};
1283
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284static int __init kcapi_init(void)
1285{
Jan Kiszka88549d62010-02-08 10:12:09 +00001286 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
Jan Kiszkaef69bb22010-02-08 10:12:13 +00001288 register_capictr_notifier(&capictr_nb);
1289
Jan Kiszka88549d62010-02-08 10:12:09 +00001290 err = cdebug_init();
1291 if (!err)
1292 kcapi_proc_init();
1293 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294}
1295
1296static void __exit kcapi_exit(void)
1297{
1298 kcapi_proc_exit();
1299
1300 /* make sure all notifiers are finished */
1301 flush_scheduled_work();
Karsten Keil17f0cd22007-02-28 20:13:50 -08001302 cdebug_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303}
1304
1305module_init(kcapi_init);
1306module_exit(kcapi_exit);