blob: bd00dceacaf0d6cac10a1ab8da423264f706a313 [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
101static inline struct capi20_appl *get_capi_appl_by_nr(u16 applid)
102{
103 if (applid - 1 >= CAPI_MAXAPPL)
104 return NULL;
105
Jan Kiszka88c896e2010-02-08 10:12:15 +0000106 return rcu_dereference(capi_applications[applid - 1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107}
108
109/* -------- util functions ------------------------------------ */
110
111static inline int capi_cmd_valid(u8 cmd)
112{
113 switch (cmd) {
114 case CAPI_ALERT:
115 case CAPI_CONNECT:
116 case CAPI_CONNECT_ACTIVE:
117 case CAPI_CONNECT_B3_ACTIVE:
118 case CAPI_CONNECT_B3:
119 case CAPI_CONNECT_B3_T90_ACTIVE:
120 case CAPI_DATA_B3:
121 case CAPI_DISCONNECT_B3:
122 case CAPI_DISCONNECT:
123 case CAPI_FACILITY:
124 case CAPI_INFO:
125 case CAPI_LISTEN:
126 case CAPI_MANUFACTURER:
127 case CAPI_RESET_B3:
128 case CAPI_SELECT_B_PROTOCOL:
129 return 1;
130 }
131 return 0;
132}
133
134static inline int capi_subcmd_valid(u8 subcmd)
135{
136 switch (subcmd) {
137 case CAPI_REQ:
138 case CAPI_CONF:
139 case CAPI_IND:
140 case CAPI_RESP:
141 return 1;
142 }
143 return 0;
144}
145
146/* ------------------------------------------------------------ */
147
Jan Kiszka52253032010-02-08 10:12:10 +0000148static void
149register_appl(struct capi_ctr *ctr, u16 applid, capi_register_params *rparam)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150{
Jan Kiszka52253032010-02-08 10:12:10 +0000151 ctr = capi_ctr_get(ctr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
Jan Kiszka52253032010-02-08 10:12:10 +0000153 if (ctr)
154 ctr->register_appl(ctr, applid, rparam);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 else
Jan Kiszka52253032010-02-08 10:12:10 +0000156 printk(KERN_WARNING "%s: cannot get controller resources\n",
157 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158}
159
160
Jan Kiszka52253032010-02-08 10:12:10 +0000161static void release_appl(struct capi_ctr *ctr, u16 applid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162{
163 DBG("applid %#x", applid);
164
Jan Kiszka52253032010-02-08 10:12:10 +0000165 ctr->release_appl(ctr, applid);
166 capi_ctr_put(ctr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167}
168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169static void notify_up(u32 contr)
170{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 struct capi20_appl *ap;
Jan Kiszka3efecf72010-02-08 10:12:12 +0000172 struct capi_ctr *ctr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 u16 applid;
174
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000175 mutex_lock(&capi_controller_lock);
176
Jan Kiszka3efecf72010-02-08 10:12:12 +0000177 if (showcapimsgs & 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 printk(KERN_DEBUG "kcapi: notify up contr %d\n", contr);
Jan Kiszka3efecf72010-02-08 10:12:12 +0000179
180 ctr = get_capi_ctr_by_nr(contr);
181 if (ctr) {
182 if (ctr->state == CAPI_CTR_RUNNING)
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000183 goto unlock_out;
Jan Kiszka3efecf72010-02-08 10:12:12 +0000184
185 ctr->state = CAPI_CTR_RUNNING;
186
187 for (applid = 1; applid <= CAPI_MAXAPPL; applid++) {
188 ap = get_capi_appl_by_nr(applid);
Jan Kiszka88c896e2010-02-08 10:12:15 +0000189 if (!ap)
Jan Kiszka3efecf72010-02-08 10:12:12 +0000190 continue;
191 register_appl(ctr, applid, &ap->rparam);
Jan Kiszka3efecf72010-02-08 10:12:12 +0000192 }
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000193
194 wake_up_interruptible_all(&ctr->state_wait_queue);
Jan Kiszka3efecf72010-02-08 10:12:12 +0000195 } else
Harvey Harrison156f1ed2008-04-28 02:14:40 -0700196 printk(KERN_WARNING "%s: invalid contr %d\n", __func__, contr);
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000197
198unlock_out:
199 mutex_unlock(&capi_controller_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200}
201
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000202static void ctr_down(struct capi_ctr *ctr, int new_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203{
204 struct capi20_appl *ap;
205 u16 applid;
206
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000207 if (ctr->state == CAPI_CTR_DETECTED || ctr->state == CAPI_CTR_DETACHED)
Jan Kiszka3efecf72010-02-08 10:12:12 +0000208 return;
209
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000210 ctr->state = new_state;
Jan Kiszka3efecf72010-02-08 10:12:12 +0000211
212 memset(ctr->manu, 0, sizeof(ctr->manu));
213 memset(&ctr->version, 0, sizeof(ctr->version));
214 memset(&ctr->profile, 0, sizeof(ctr->profile));
215 memset(ctr->serial, 0, sizeof(ctr->serial));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
217 for (applid = 1; applid <= CAPI_MAXAPPL; applid++) {
218 ap = get_capi_appl_by_nr(applid);
Jan Kiszka88c896e2010-02-08 10:12:15 +0000219 if (ap)
Jan Kiszka3efecf72010-02-08 10:12:12 +0000220 capi_ctr_put(ctr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 }
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000222
223 wake_up_interruptible_all(&ctr->state_wait_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224}
225
Jan Kiszka3efecf72010-02-08 10:12:12 +0000226static void notify_down(u32 contr)
227{
228 struct capi_ctr *ctr;
229
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000230 mutex_lock(&capi_controller_lock);
231
Jan Kiszka3efecf72010-02-08 10:12:12 +0000232 if (showcapimsgs & 1)
233 printk(KERN_DEBUG "kcapi: notify down contr %d\n", contr);
234
235 ctr = get_capi_ctr_by_nr(contr);
236 if (ctr)
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000237 ctr_down(ctr, CAPI_CTR_DETECTED);
Jan Kiszka3efecf72010-02-08 10:12:12 +0000238 else
239 printk(KERN_WARNING "%s: invalid contr %d\n", __func__, contr);
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000240
241 mutex_unlock(&capi_controller_lock);
Jan Kiszka3efecf72010-02-08 10:12:12 +0000242}
243
Jan Kiszkaef69bb22010-02-08 10:12:13 +0000244static int
245notify_handler(struct notifier_block *nb, unsigned long val, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246{
Jan Kiszkaef69bb22010-02-08 10:12:13 +0000247 u32 contr = (long)v;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
Jan Kiszkaef69bb22010-02-08 10:12:13 +0000249 switch (val) {
250 case CAPICTR_UP:
251 notify_up(contr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 break;
Jan Kiszkaef69bb22010-02-08 10:12:13 +0000253 case CAPICTR_DOWN:
254 notify_down(contr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 break;
256 }
Jan Kiszkaef69bb22010-02-08 10:12:13 +0000257 return NOTIFY_OK;
258}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
Jan Kiszkaef69bb22010-02-08 10:12:13 +0000260static void do_notify_work(struct work_struct *work)
261{
262 struct capictr_event *event =
263 container_of(work, struct capictr_event, work);
264
265 blocking_notifier_call_chain(&ctr_notifier_list, event->type,
266 (void *)(long)event->controller);
267 kfree(event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268}
269
270/*
271 * The notifier will result in adding/deleteing of devices. Devices can
272 * only removed in user process, not in bh.
273 */
Jan Kiszkaef69bb22010-02-08 10:12:13 +0000274static int notify_push(unsigned int event_type, u32 controller)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
Jan Kiszkaef69bb22010-02-08 10:12:13 +0000276 struct capictr_event *event = kmalloc(sizeof(*event), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
Jan Kiszkaef69bb22010-02-08 10:12:13 +0000278 if (!event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 return -ENOMEM;
280
Jan Kiszkaef69bb22010-02-08 10:12:13 +0000281 INIT_WORK(&event->work, do_notify_work);
282 event->type = event_type;
283 event->controller = controller;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Jan Kiszkaef69bb22010-02-08 10:12:13 +0000285 schedule_work(&event->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 return 0;
287}
288
Jan Kiszkaef69bb22010-02-08 10:12:13 +0000289int register_capictr_notifier(struct notifier_block *nb)
290{
291 return blocking_notifier_chain_register(&ctr_notifier_list, nb);
292}
293EXPORT_SYMBOL_GPL(register_capictr_notifier);
294
295int unregister_capictr_notifier(struct notifier_block *nb)
296{
297 return blocking_notifier_chain_unregister(&ctr_notifier_list, nb);
298}
299EXPORT_SYMBOL_GPL(unregister_capictr_notifier);
300
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301/* -------- Receiver ------------------------------------------ */
302
David Howellsc4028952006-11-22 14:57:56 +0000303static void recv_handler(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304{
305 struct sk_buff *skb;
David Howellsc4028952006-11-22 14:57:56 +0000306 struct capi20_appl *ap =
307 container_of(work, struct capi20_appl, recv_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
309 if ((!ap) || (ap->release_in_progress))
310 return;
311
Matthias Kaehlcke67837f22007-07-17 04:04:16 -0700312 mutex_lock(&ap->recv_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 while ((skb = skb_dequeue(&ap->recv_queue))) {
314 if (CAPIMSG_CMD(skb->data) == CAPI_DATA_B3_IND)
315 ap->nrecvdatapkt++;
316 else
317 ap->nrecvctlpkt++;
318
319 ap->recv_message(ap, skb);
320 }
Matthias Kaehlcke67837f22007-07-17 04:04:16 -0700321 mutex_unlock(&ap->recv_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322}
323
Tilman Schmidt554f2002009-04-23 02:24:21 +0000324/**
325 * capi_ctr_handle_message() - handle incoming CAPI message
Jan Kiszka52253032010-02-08 10:12:10 +0000326 * @ctr: controller descriptor structure.
Tilman Schmidt554f2002009-04-23 02:24:21 +0000327 * @appl: application ID.
328 * @skb: message.
329 *
330 * Called by hardware driver to pass a CAPI message to the application.
331 */
332
Jan Kiszka52253032010-02-08 10:12:10 +0000333void capi_ctr_handle_message(struct capi_ctr *ctr, u16 appl,
334 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335{
336 struct capi20_appl *ap;
337 int showctl = 0;
338 u8 cmd, subcmd;
Karsten Keil17f0cd22007-02-28 20:13:50 -0800339 _cdebbuf *cdb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
Jan Kiszka52253032010-02-08 10:12:10 +0000341 if (ctr->state != CAPI_CTR_RUNNING) {
Karsten Keil17f0cd22007-02-28 20:13:50 -0800342 cdb = capi_message2str(skb->data);
343 if (cdb) {
344 printk(KERN_INFO "kcapi: controller [%03d] not active, got: %s",
Jan Kiszka52253032010-02-08 10:12:10 +0000345 ctr->cnr, cdb->buf);
Karsten Keil17f0cd22007-02-28 20:13:50 -0800346 cdebbuf_free(cdb);
347 } else
348 printk(KERN_INFO "kcapi: controller [%03d] not active, cannot trace\n",
Jan Kiszka52253032010-02-08 10:12:10 +0000349 ctr->cnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 goto error;
351 }
352
353 cmd = CAPIMSG_COMMAND(skb->data);
354 subcmd = CAPIMSG_SUBCOMMAND(skb->data);
355 if (cmd == CAPI_DATA_B3 && subcmd == CAPI_IND) {
Jan Kiszka52253032010-02-08 10:12:10 +0000356 ctr->nrecvdatapkt++;
357 if (ctr->traceflag > 2)
358 showctl |= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 } else {
Jan Kiszka52253032010-02-08 10:12:10 +0000360 ctr->nrecvctlpkt++;
361 if (ctr->traceflag)
362 showctl |= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 }
Jan Kiszka52253032010-02-08 10:12:10 +0000364 showctl |= (ctr->traceflag & 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 if (showctl & 2) {
366 if (showctl & 1) {
Karsten Keil17f0cd22007-02-28 20:13:50 -0800367 printk(KERN_DEBUG "kcapi: got [%03d] id#%d %s len=%u\n",
Jan Kiszka52253032010-02-08 10:12:10 +0000368 ctr->cnr, CAPIMSG_APPID(skb->data),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 capi_cmd2str(cmd, subcmd),
370 CAPIMSG_LEN(skb->data));
371 } else {
Karsten Keil17f0cd22007-02-28 20:13:50 -0800372 cdb = capi_message2str(skb->data);
373 if (cdb) {
374 printk(KERN_DEBUG "kcapi: got [%03d] %s\n",
Jan Kiszka52253032010-02-08 10:12:10 +0000375 ctr->cnr, cdb->buf);
Karsten Keil17f0cd22007-02-28 20:13:50 -0800376 cdebbuf_free(cdb);
377 } else
378 printk(KERN_DEBUG "kcapi: got [%03d] id#%d %s len=%u, cannot trace\n",
Jan Kiszka52253032010-02-08 10:12:10 +0000379 ctr->cnr, CAPIMSG_APPID(skb->data),
Karsten Keil17f0cd22007-02-28 20:13:50 -0800380 capi_cmd2str(cmd, subcmd),
381 CAPIMSG_LEN(skb->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 }
383
384 }
385
Jan Kiszka88c896e2010-02-08 10:12:15 +0000386 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 ap = get_capi_appl_by_nr(CAPIMSG_APPID(skb->data));
Jan Kiszka88c896e2010-02-08 10:12:15 +0000388 if (!ap) {
389 rcu_read_unlock();
Karsten Keil17f0cd22007-02-28 20:13:50 -0800390 cdb = capi_message2str(skb->data);
391 if (cdb) {
392 printk(KERN_ERR "kcapi: handle_message: applid %d state released (%s)\n",
393 CAPIMSG_APPID(skb->data), cdb->buf);
394 cdebbuf_free(cdb);
395 } else
396 printk(KERN_ERR "kcapi: handle_message: applid %d state released (%s) cannot trace\n",
397 CAPIMSG_APPID(skb->data),
398 capi_cmd2str(cmd, subcmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 goto error;
400 }
401 skb_queue_tail(&ap->recv_queue, skb);
402 schedule_work(&ap->recv_work);
Jan Kiszka88c896e2010-02-08 10:12:15 +0000403 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
405 return;
406
407error:
408 kfree_skb(skb);
409}
410
411EXPORT_SYMBOL(capi_ctr_handle_message);
412
Tilman Schmidt554f2002009-04-23 02:24:21 +0000413/**
414 * capi_ctr_ready() - signal CAPI controller ready
Jan Kiszka52253032010-02-08 10:12:10 +0000415 * @ctr: controller descriptor structure.
Tilman Schmidt554f2002009-04-23 02:24:21 +0000416 *
417 * Called by hardware driver to signal that the controller is up and running.
418 */
419
Jan Kiszka52253032010-02-08 10:12:10 +0000420void capi_ctr_ready(struct capi_ctr *ctr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421{
Jan Kiszka52253032010-02-08 10:12:10 +0000422 printk(KERN_NOTICE "kcapi: controller [%03d] \"%s\" ready.\n",
423 ctr->cnr, ctr->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
Jan Kiszkaef69bb22010-02-08 10:12:13 +0000425 notify_push(CAPICTR_UP, ctr->cnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426}
427
428EXPORT_SYMBOL(capi_ctr_ready);
429
Tilman Schmidt554f2002009-04-23 02:24:21 +0000430/**
Tilman Schmidt4e329972009-06-07 09:09:23 +0000431 * capi_ctr_down() - signal CAPI controller not ready
Jan Kiszka52253032010-02-08 10:12:10 +0000432 * @ctr: controller descriptor structure.
Tilman Schmidt554f2002009-04-23 02:24:21 +0000433 *
434 * Called by hardware driver to signal that the controller is down and
435 * unavailable for use.
436 */
437
Jan Kiszka52253032010-02-08 10:12:10 +0000438void capi_ctr_down(struct capi_ctr *ctr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439{
Jan Kiszka52253032010-02-08 10:12:10 +0000440 printk(KERN_NOTICE "kcapi: controller [%03d] down.\n", ctr->cnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
Jan Kiszkaef69bb22010-02-08 10:12:13 +0000442 notify_push(CAPICTR_DOWN, ctr->cnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443}
444
Tilman Schmidt4e329972009-06-07 09:09:23 +0000445EXPORT_SYMBOL(capi_ctr_down);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
Tilman Schmidt554f2002009-04-23 02:24:21 +0000447/**
448 * capi_ctr_suspend_output() - suspend controller
Jan Kiszka52253032010-02-08 10:12:10 +0000449 * @ctr: controller descriptor structure.
Tilman Schmidt554f2002009-04-23 02:24:21 +0000450 *
451 * Called by hardware driver to stop data flow.
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000452 *
453 * Note: The caller is responsible for synchronizing concurrent state changes
454 * as well as invocations of capi_ctr_handle_message.
Tilman Schmidt554f2002009-04-23 02:24:21 +0000455 */
456
Jan Kiszka52253032010-02-08 10:12:10 +0000457void capi_ctr_suspend_output(struct capi_ctr *ctr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458{
Jan Kiszka52253032010-02-08 10:12:10 +0000459 if (!ctr->blocked) {
460 printk(KERN_DEBUG "kcapi: controller [%03d] suspend\n",
461 ctr->cnr);
462 ctr->blocked = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 }
464}
465
466EXPORT_SYMBOL(capi_ctr_suspend_output);
467
Tilman Schmidt554f2002009-04-23 02:24:21 +0000468/**
469 * capi_ctr_resume_output() - resume controller
Jan Kiszka52253032010-02-08 10:12:10 +0000470 * @ctr: controller descriptor structure.
Tilman Schmidt554f2002009-04-23 02:24:21 +0000471 *
472 * Called by hardware driver to resume data flow.
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000473 *
474 * Note: The caller is responsible for synchronizing concurrent state changes
475 * as well as invocations of capi_ctr_handle_message.
Tilman Schmidt554f2002009-04-23 02:24:21 +0000476 */
477
Jan Kiszka52253032010-02-08 10:12:10 +0000478void capi_ctr_resume_output(struct capi_ctr *ctr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479{
Jan Kiszka52253032010-02-08 10:12:10 +0000480 if (ctr->blocked) {
481 printk(KERN_DEBUG "kcapi: controller [%03d] resumed\n",
482 ctr->cnr);
483 ctr->blocked = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 }
485}
486
487EXPORT_SYMBOL(capi_ctr_resume_output);
488
489/* ------------------------------------------------------------- */
490
Tilman Schmidt554f2002009-04-23 02:24:21 +0000491/**
492 * attach_capi_ctr() - register CAPI controller
Jan Kiszka52253032010-02-08 10:12:10 +0000493 * @ctr: controller descriptor structure.
Tilman Schmidt554f2002009-04-23 02:24:21 +0000494 *
495 * Called by hardware driver to register a controller with the CAPI subsystem.
496 * Return value: 0 on success, error code < 0 on error
497 */
498
Jan Kiszka52253032010-02-08 10:12:10 +0000499int attach_capi_ctr(struct capi_ctr *ctr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500{
501 int i;
502
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000503 mutex_lock(&capi_controller_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
505 for (i = 0; i < CAPI_MAXCONTR; i++) {
Jan Kiszka52253032010-02-08 10:12:10 +0000506 if (!capi_controller[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 break;
508 }
509 if (i == CAPI_MAXCONTR) {
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000510 mutex_unlock(&capi_controller_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 printk(KERN_ERR "kcapi: out of controller slots\n");
512 return -EBUSY;
513 }
Jan Kiszka52253032010-02-08 10:12:10 +0000514 capi_controller[i] = ctr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
Jan Kiszka52253032010-02-08 10:12:10 +0000516 ctr->nrecvctlpkt = 0;
517 ctr->nrecvdatapkt = 0;
518 ctr->nsentctlpkt = 0;
519 ctr->nsentdatapkt = 0;
520 ctr->cnr = i + 1;
521 ctr->state = CAPI_CTR_DETECTED;
522 ctr->blocked = 0;
523 ctr->traceflag = showcapimsgs;
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000524 init_waitqueue_head(&ctr->state_wait_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
Jan Kiszka52253032010-02-08 10:12:10 +0000526 sprintf(ctr->procfn, "capi/controllers/%d", ctr->cnr);
527 ctr->procent = proc_create_data(ctr->procfn, 0, NULL, ctr->proc_fops, ctr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
Jan Kiszka52253032010-02-08 10:12:10 +0000529 ncontrollers++;
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000530
531 mutex_unlock(&capi_controller_lock);
532
Jan Kiszka52253032010-02-08 10:12:10 +0000533 printk(KERN_NOTICE "kcapi: controller [%03d]: %s attached\n",
534 ctr->cnr, ctr->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 return 0;
536}
537
538EXPORT_SYMBOL(attach_capi_ctr);
539
Tilman Schmidt554f2002009-04-23 02:24:21 +0000540/**
541 * detach_capi_ctr() - unregister CAPI controller
Jan Kiszka52253032010-02-08 10:12:10 +0000542 * @ctr: controller descriptor structure.
Tilman Schmidt554f2002009-04-23 02:24:21 +0000543 *
544 * Called by hardware driver to remove the registration of a controller
545 * with the CAPI subsystem.
546 * Return value: 0 on success, error code < 0 on error
547 */
548
Jan Kiszka52253032010-02-08 10:12:10 +0000549int detach_capi_ctr(struct capi_ctr *ctr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550{
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000551 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000553 mutex_lock(&capi_controller_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000555 ctr_down(ctr, CAPI_CTR_DETACHED);
556
557 if (capi_controller[ctr->cnr - 1] != ctr) {
558 err = -EINVAL;
559 goto unlock_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 }
Jan Kiszka52253032010-02-08 10:12:10 +0000561 capi_controller[ctr->cnr - 1] = NULL;
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000562 ncontrollers--;
563
564 if (ctr->procent)
565 remove_proc_entry(ctr->procfn, NULL);
566
Jan Kiszka52253032010-02-08 10:12:10 +0000567 printk(KERN_NOTICE "kcapi: controller [%03d]: %s unregistered\n",
568 ctr->cnr, ctr->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000570unlock_out:
571 mutex_unlock(&capi_controller_lock);
572
573 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574}
575
576EXPORT_SYMBOL(detach_capi_ctr);
577
Tilman Schmidt554f2002009-04-23 02:24:21 +0000578/**
579 * register_capi_driver() - register CAPI driver
580 * @driver: driver descriptor structure.
581 *
582 * Called by hardware driver to register itself with the CAPI subsystem.
583 */
584
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585void register_capi_driver(struct capi_driver *driver)
586{
Jan Kiszka9717fb82010-02-08 10:12:11 +0000587 mutex_lock(&capi_drivers_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 list_add_tail(&driver->list, &capi_drivers);
Jan Kiszka9717fb82010-02-08 10:12:11 +0000589 mutex_unlock(&capi_drivers_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590}
591
592EXPORT_SYMBOL(register_capi_driver);
593
Tilman Schmidt554f2002009-04-23 02:24:21 +0000594/**
595 * unregister_capi_driver() - unregister CAPI driver
596 * @driver: driver descriptor structure.
597 *
598 * Called by hardware driver to unregister itself from the CAPI subsystem.
599 */
600
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601void unregister_capi_driver(struct capi_driver *driver)
602{
Jan Kiszka9717fb82010-02-08 10:12:11 +0000603 mutex_lock(&capi_drivers_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 list_del(&driver->list);
Jan Kiszka9717fb82010-02-08 10:12:11 +0000605 mutex_unlock(&capi_drivers_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606}
607
608EXPORT_SYMBOL(unregister_capi_driver);
609
610/* ------------------------------------------------------------- */
611/* -------- CAPI2.0 Interface ---------------------------------- */
612/* ------------------------------------------------------------- */
613
Tilman Schmidt554f2002009-04-23 02:24:21 +0000614/**
615 * capi20_isinstalled() - CAPI 2.0 operation CAPI_INSTALLED
616 *
617 * Return value: CAPI result code (CAPI_NOERROR if at least one ISDN controller
618 * is ready for use, CAPI_REGNOTINSTALLED otherwise)
619 */
620
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621u16 capi20_isinstalled(void)
622{
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000623 u16 ret = CAPI_REGNOTINSTALLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 int i;
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000625
626 mutex_lock(&capi_controller_lock);
627
628 for (i = 0; i < CAPI_MAXCONTR; i++)
Jan Kiszka52253032010-02-08 10:12:10 +0000629 if (capi_controller[i] &&
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000630 capi_controller[i]->state == CAPI_CTR_RUNNING) {
631 ret = CAPI_NOERROR;
632 break;
633 }
634
635 mutex_unlock(&capi_controller_lock);
636
637 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638}
639
640EXPORT_SYMBOL(capi20_isinstalled);
641
Tilman Schmidt554f2002009-04-23 02:24:21 +0000642/**
643 * capi20_register() - CAPI 2.0 operation CAPI_REGISTER
644 * @ap: CAPI application descriptor structure.
645 *
646 * Register an application's presence with CAPI.
647 * A unique application ID is assigned and stored in @ap->applid.
648 * After this function returns successfully, the message receive
649 * callback function @ap->recv_message() may be called at any time
650 * until capi20_release() has been called for the same @ap.
651 * Return value: CAPI result code
652 */
653
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654u16 capi20_register(struct capi20_appl *ap)
655{
656 int i;
657 u16 applid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
659 DBG("");
660
661 if (ap->rparam.datablklen < 128)
662 return CAPI_LOGBLKSIZETOSMALL;
663
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 ap->nrecvctlpkt = 0;
665 ap->nrecvdatapkt = 0;
666 ap->nsentctlpkt = 0;
667 ap->nsentdatapkt = 0;
Matthias Kaehlcke67837f22007-07-17 04:04:16 -0700668 mutex_init(&ap->recv_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 skb_queue_head_init(&ap->recv_queue);
David Howellsc4028952006-11-22 14:57:56 +0000670 INIT_WORK(&ap->recv_work, recv_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 ap->release_in_progress = 0;
672
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000673 mutex_lock(&capi_controller_lock);
674
Jan Kiszka88c896e2010-02-08 10:12:15 +0000675 for (applid = 1; applid <= CAPI_MAXAPPL; applid++) {
676 if (capi_applications[applid - 1] == NULL)
677 break;
678 }
679 if (applid > CAPI_MAXAPPL) {
680 mutex_unlock(&capi_controller_lock);
681 return CAPI_TOOMANYAPPLS;
682 }
683
684 ap->applid = applid;
685 capi_applications[applid - 1] = ap;
686
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 for (i = 0; i < CAPI_MAXCONTR; i++) {
Jan Kiszka52253032010-02-08 10:12:10 +0000688 if (!capi_controller[i] ||
689 capi_controller[i]->state != CAPI_CTR_RUNNING)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 continue;
Jan Kiszka52253032010-02-08 10:12:10 +0000691 register_appl(capi_controller[i], applid, &ap->rparam);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 }
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000693
694 mutex_unlock(&capi_controller_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
696 if (showcapimsgs & 1) {
697 printk(KERN_DEBUG "kcapi: appl %d up\n", applid);
698 }
699
700 return CAPI_NOERROR;
701}
702
703EXPORT_SYMBOL(capi20_register);
704
Tilman Schmidt554f2002009-04-23 02:24:21 +0000705/**
706 * capi20_release() - CAPI 2.0 operation CAPI_RELEASE
707 * @ap: CAPI application descriptor structure.
708 *
709 * Terminate an application's registration with CAPI.
710 * After this function returns successfully, the message receive
711 * callback function @ap->recv_message() will no longer be called.
712 * Return value: CAPI result code
713 */
714
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715u16 capi20_release(struct capi20_appl *ap)
716{
717 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
719 DBG("applid %#x", ap->applid);
720
Jan Kiszka88c896e2010-02-08 10:12:15 +0000721 mutex_lock(&capi_controller_lock);
722
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 ap->release_in_progress = 1;
724 capi_applications[ap->applid - 1] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
Jan Kiszka88c896e2010-02-08 10:12:15 +0000726 synchronize_rcu();
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000727
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 for (i = 0; i < CAPI_MAXCONTR; i++) {
Jan Kiszka52253032010-02-08 10:12:10 +0000729 if (!capi_controller[i] ||
730 capi_controller[i]->state != CAPI_CTR_RUNNING)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 continue;
Jan Kiszka52253032010-02-08 10:12:10 +0000732 release_appl(capi_controller[i], ap->applid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 }
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000734
735 mutex_unlock(&capi_controller_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
737 flush_scheduled_work();
738 skb_queue_purge(&ap->recv_queue);
739
740 if (showcapimsgs & 1) {
741 printk(KERN_DEBUG "kcapi: appl %d down\n", ap->applid);
742 }
743
744 return CAPI_NOERROR;
745}
746
747EXPORT_SYMBOL(capi20_release);
748
Tilman Schmidt554f2002009-04-23 02:24:21 +0000749/**
750 * capi20_put_message() - CAPI 2.0 operation CAPI_PUT_MESSAGE
751 * @ap: CAPI application descriptor structure.
752 * @skb: CAPI message.
753 *
754 * Transfer a single message to CAPI.
755 * Return value: CAPI result code
756 */
757
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758u16 capi20_put_message(struct capi20_appl *ap, struct sk_buff *skb)
759{
Jan Kiszka52253032010-02-08 10:12:10 +0000760 struct capi_ctr *ctr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 int showctl = 0;
762 u8 cmd, subcmd;
763
764 DBG("applid %#x", ap->applid);
765
Jan Kiszka52253032010-02-08 10:12:10 +0000766 if (ncontrollers == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 return CAPI_REGNOTINSTALLED;
768 if ((ap->applid == 0) || ap->release_in_progress)
769 return CAPI_ILLAPPNR;
770 if (skb->len < 12
771 || !capi_cmd_valid(CAPIMSG_COMMAND(skb->data))
772 || !capi_subcmd_valid(CAPIMSG_SUBCOMMAND(skb->data)))
773 return CAPI_ILLCMDORSUBCMDORMSGTOSMALL;
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000774
775 /*
776 * The controller reference is protected by the existence of the
777 * application passed to us. We assume that the caller properly
778 * synchronizes this service with capi20_release.
779 */
Jan Kiszka52253032010-02-08 10:12:10 +0000780 ctr = get_capi_ctr_by_nr(CAPIMSG_CONTROLLER(skb->data));
Jan Kiszkac6af0432010-02-08 10:12:43 +0000781 if (!ctr || ctr->state != CAPI_CTR_RUNNING)
782 return CAPI_REGNOTINSTALLED;
Jan Kiszka52253032010-02-08 10:12:10 +0000783 if (ctr->blocked)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 return CAPI_SENDQUEUEFULL;
785
786 cmd = CAPIMSG_COMMAND(skb->data);
787 subcmd = CAPIMSG_SUBCOMMAND(skb->data);
788
789 if (cmd == CAPI_DATA_B3 && subcmd== CAPI_REQ) {
Jan Kiszka52253032010-02-08 10:12:10 +0000790 ctr->nsentdatapkt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 ap->nsentdatapkt++;
Jan Kiszka52253032010-02-08 10:12:10 +0000792 if (ctr->traceflag > 2)
793 showctl |= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 } else {
Jan Kiszka52253032010-02-08 10:12:10 +0000795 ctr->nsentctlpkt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 ap->nsentctlpkt++;
Jan Kiszka52253032010-02-08 10:12:10 +0000797 if (ctr->traceflag)
798 showctl |= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 }
Jan Kiszka52253032010-02-08 10:12:10 +0000800 showctl |= (ctr->traceflag & 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 if (showctl & 2) {
802 if (showctl & 1) {
Karsten Keil17f0cd22007-02-28 20:13:50 -0800803 printk(KERN_DEBUG "kcapi: put [%03d] id#%d %s len=%u\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 CAPIMSG_CONTROLLER(skb->data),
805 CAPIMSG_APPID(skb->data),
806 capi_cmd2str(cmd, subcmd),
807 CAPIMSG_LEN(skb->data));
808 } else {
Karsten Keil17f0cd22007-02-28 20:13:50 -0800809 _cdebbuf *cdb = capi_message2str(skb->data);
810 if (cdb) {
811 printk(KERN_DEBUG "kcapi: put [%03d] %s\n",
812 CAPIMSG_CONTROLLER(skb->data),
813 cdb->buf);
814 cdebbuf_free(cdb);
815 } else
816 printk(KERN_DEBUG "kcapi: put [%03d] id#%d %s len=%u cannot trace\n",
817 CAPIMSG_CONTROLLER(skb->data),
818 CAPIMSG_APPID(skb->data),
819 capi_cmd2str(cmd, subcmd),
820 CAPIMSG_LEN(skb->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 }
Jan Kiszka52253032010-02-08 10:12:10 +0000823 return ctr->send_message(ctr, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824}
825
826EXPORT_SYMBOL(capi20_put_message);
827
Tilman Schmidt554f2002009-04-23 02:24:21 +0000828/**
829 * capi20_get_manufacturer() - CAPI 2.0 operation CAPI_GET_MANUFACTURER
830 * @contr: controller number.
831 * @buf: result buffer (64 bytes).
832 *
833 * Retrieve information about the manufacturer of the specified ISDN controller
834 * or (for @contr == 0) the driver itself.
835 * Return value: CAPI result code
836 */
837
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838u16 capi20_get_manufacturer(u32 contr, u8 *buf)
839{
Jan Kiszka52253032010-02-08 10:12:10 +0000840 struct capi_ctr *ctr;
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000841 u16 ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
843 if (contr == 0) {
844 strlcpy(buf, capi_manufakturer, CAPI_MANUFACTURER_LEN);
845 return CAPI_NOERROR;
846 }
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000847
848 mutex_lock(&capi_controller_lock);
849
Jan Kiszka52253032010-02-08 10:12:10 +0000850 ctr = get_capi_ctr_by_nr(contr);
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000851 if (ctr && ctr->state == CAPI_CTR_RUNNING) {
852 strlcpy(buf, ctr->manu, CAPI_MANUFACTURER_LEN);
853 ret = CAPI_NOERROR;
854 } else
855 ret = CAPI_REGNOTINSTALLED;
856
857 mutex_unlock(&capi_controller_lock);
858 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859}
860
861EXPORT_SYMBOL(capi20_get_manufacturer);
862
Tilman Schmidt554f2002009-04-23 02:24:21 +0000863/**
864 * capi20_get_version() - CAPI 2.0 operation CAPI_GET_VERSION
865 * @contr: controller number.
866 * @verp: result structure.
867 *
868 * Retrieve version information for the specified ISDN controller
869 * or (for @contr == 0) the driver itself.
870 * Return value: CAPI result code
871 */
872
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873u16 capi20_get_version(u32 contr, struct capi_version *verp)
874{
Jan Kiszka52253032010-02-08 10:12:10 +0000875 struct capi_ctr *ctr;
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000876 u16 ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
878 if (contr == 0) {
879 *verp = driver_version;
880 return CAPI_NOERROR;
881 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000883 mutex_lock(&capi_controller_lock);
884
885 ctr = get_capi_ctr_by_nr(contr);
886 if (ctr && ctr->state == CAPI_CTR_RUNNING) {
887 memcpy(verp, &ctr->version, sizeof(capi_version));
888 ret = CAPI_NOERROR;
889 } else
890 ret = CAPI_REGNOTINSTALLED;
891
892 mutex_unlock(&capi_controller_lock);
893 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894}
895
896EXPORT_SYMBOL(capi20_get_version);
897
Tilman Schmidt554f2002009-04-23 02:24:21 +0000898/**
899 * capi20_get_serial() - CAPI 2.0 operation CAPI_GET_SERIAL_NUMBER
900 * @contr: controller number.
901 * @serial: result buffer (8 bytes).
902 *
903 * Retrieve the serial number of the specified ISDN controller
904 * or (for @contr == 0) the driver itself.
905 * Return value: CAPI result code
906 */
907
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908u16 capi20_get_serial(u32 contr, u8 *serial)
909{
Jan Kiszka52253032010-02-08 10:12:10 +0000910 struct capi_ctr *ctr;
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000911 u16 ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
913 if (contr == 0) {
914 strlcpy(serial, driver_serial, CAPI_SERIAL_LEN);
915 return CAPI_NOERROR;
916 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000918 mutex_lock(&capi_controller_lock);
919
920 ctr = get_capi_ctr_by_nr(contr);
921 if (ctr && ctr->state == CAPI_CTR_RUNNING) {
922 strlcpy(serial, ctr->serial, CAPI_SERIAL_LEN);
923 ret = CAPI_NOERROR;
924 } else
925 ret = CAPI_REGNOTINSTALLED;
926
927 mutex_unlock(&capi_controller_lock);
928 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929}
930
931EXPORT_SYMBOL(capi20_get_serial);
932
Tilman Schmidt554f2002009-04-23 02:24:21 +0000933/**
934 * capi20_get_profile() - CAPI 2.0 operation CAPI_GET_PROFILE
935 * @contr: controller number.
936 * @profp: result structure.
937 *
938 * Retrieve capability information for the specified ISDN controller
939 * or (for @contr == 0) the number of installed controllers.
940 * Return value: CAPI result code
941 */
942
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943u16 capi20_get_profile(u32 contr, struct capi_profile *profp)
944{
Jan Kiszka52253032010-02-08 10:12:10 +0000945 struct capi_ctr *ctr;
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000946 u16 ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
948 if (contr == 0) {
Jan Kiszka52253032010-02-08 10:12:10 +0000949 profp->ncontroller = ncontrollers;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 return CAPI_NOERROR;
951 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000953 mutex_lock(&capi_controller_lock);
954
955 ctr = get_capi_ctr_by_nr(contr);
956 if (ctr && ctr->state == CAPI_CTR_RUNNING) {
957 memcpy(profp, &ctr->profile, sizeof(struct capi_profile));
958 ret = CAPI_NOERROR;
959 } else
960 ret = CAPI_REGNOTINSTALLED;
961
962 mutex_unlock(&capi_controller_lock);
963 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964}
965
966EXPORT_SYMBOL(capi20_get_profile);
967
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000968/* Must be called with capi_controller_lock held. */
969static int wait_on_ctr_state(struct capi_ctr *ctr, unsigned int state)
970{
971 DEFINE_WAIT(wait);
972 int retval = 0;
973
974 ctr = capi_ctr_get(ctr);
975 if (!ctr)
976 return -ESRCH;
977
978 for (;;) {
979 prepare_to_wait(&ctr->state_wait_queue, &wait,
980 TASK_INTERRUPTIBLE);
981
982 if (ctr->state == state)
983 break;
984 if (ctr->state == CAPI_CTR_DETACHED) {
985 retval = -ESRCH;
986 break;
987 }
988 if (signal_pending(current)) {
989 retval = -EINTR;
990 break;
991 }
992
993 mutex_unlock(&capi_controller_lock);
994 schedule();
995 mutex_lock(&capi_controller_lock);
996 }
997 finish_wait(&ctr->state_wait_queue, &wait);
998
999 capi_ctr_put(ctr);
1000
1001 return retval;
1002}
1003
Robert P. J. Day37772ac2008-04-28 02:14:42 -07001004#ifdef AVMB1_COMPAT
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005static int old_capi_manufacturer(unsigned int cmd, void __user *data)
1006{
1007 avmb1_loadandconfigdef ldef;
1008 avmb1_extcarddef cdef;
1009 avmb1_resetdef rdef;
1010 capicardparams cparams;
Jan Kiszka52253032010-02-08 10:12:10 +00001011 struct capi_ctr *ctr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 struct capi_driver *driver = NULL;
1013 capiloaddata ldata;
1014 struct list_head *l;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 int retval;
1016
1017 switch (cmd) {
1018 case AVMB1_ADDCARD:
1019 case AVMB1_ADDCARD_WITH_TYPE:
1020 if (cmd == AVMB1_ADDCARD) {
1021 if ((retval = copy_from_user(&cdef, data,
1022 sizeof(avmb1_carddef))))
1023 return retval;
1024 cdef.cardtype = AVM_CARDTYPE_B1;
1025 } else {
1026 if ((retval = copy_from_user(&cdef, data,
1027 sizeof(avmb1_extcarddef))))
1028 return retval;
1029 }
1030 cparams.port = cdef.port;
1031 cparams.irq = cdef.irq;
1032 cparams.cardnr = cdef.cardnr;
1033
Jan Kiszka9717fb82010-02-08 10:12:11 +00001034 mutex_lock(&capi_drivers_lock);
1035
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 switch (cdef.cardtype) {
1037 case AVM_CARDTYPE_B1:
1038 list_for_each(l, &capi_drivers) {
1039 driver = list_entry(l, struct capi_driver, list);
1040 if (strcmp(driver->name, "b1isa") == 0)
1041 break;
1042 }
1043 break;
1044 case AVM_CARDTYPE_T1:
1045 list_for_each(l, &capi_drivers) {
1046 driver = list_entry(l, struct capi_driver, list);
1047 if (strcmp(driver->name, "t1isa") == 0)
1048 break;
1049 }
1050 break;
1051 default:
1052 driver = NULL;
1053 break;
1054 }
1055 if (!driver) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 printk(KERN_ERR "kcapi: driver not loaded.\n");
Jan Kiszka9717fb82010-02-08 10:12:11 +00001057 retval = -EIO;
1058 } else if (!driver->add_card) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 printk(KERN_ERR "kcapi: driver has no add card function.\n");
Jan Kiszka9717fb82010-02-08 10:12:11 +00001060 retval = -EIO;
1061 } else
1062 retval = driver->add_card(driver, &cparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063
Jan Kiszka9717fb82010-02-08 10:12:11 +00001064 mutex_unlock(&capi_drivers_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 return retval;
1066
1067 case AVMB1_LOAD:
1068 case AVMB1_LOAD_AND_CONFIG:
1069
1070 if (cmd == AVMB1_LOAD) {
1071 if (copy_from_user(&ldef, data,
1072 sizeof(avmb1_loaddef)))
1073 return -EFAULT;
1074 ldef.t4config.len = 0;
1075 ldef.t4config.data = NULL;
1076 } else {
1077 if (copy_from_user(&ldef, data,
1078 sizeof(avmb1_loadandconfigdef)))
1079 return -EFAULT;
1080 }
Jan Kiszka0ca3a012010-02-08 10:12:14 +00001081
1082 mutex_lock(&capi_controller_lock);
1083
Jan Kiszka52253032010-02-08 10:12:10 +00001084 ctr = get_capi_ctr_by_nr(ldef.contr);
Jan Kiszka0ca3a012010-02-08 10:12:14 +00001085 if (!ctr) {
1086 retval = -EINVAL;
1087 goto load_unlock_out;
1088 }
1089
Jan Kiszka52253032010-02-08 10:12:10 +00001090 if (ctr->load_firmware == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 printk(KERN_DEBUG "kcapi: load: no load function\n");
Jan Kiszka0ca3a012010-02-08 10:12:14 +00001092 retval = -ESRCH;
1093 goto load_unlock_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 }
1095
1096 if (ldef.t4file.len <= 0) {
1097 printk(KERN_DEBUG "kcapi: load: invalid parameter: length of t4file is %d ?\n", ldef.t4file.len);
Jan Kiszka0ca3a012010-02-08 10:12:14 +00001098 retval = -EINVAL;
1099 goto load_unlock_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 }
Harvey Harrison2f9e9b62008-04-28 02:14:37 -07001101 if (ldef.t4file.data == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 printk(KERN_DEBUG "kcapi: load: invalid parameter: dataptr is 0\n");
Jan Kiszka0ca3a012010-02-08 10:12:14 +00001103 retval = -EINVAL;
1104 goto load_unlock_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 }
1106
1107 ldata.firmware.user = 1;
1108 ldata.firmware.data = ldef.t4file.data;
1109 ldata.firmware.len = ldef.t4file.len;
1110 ldata.configuration.user = 1;
1111 ldata.configuration.data = ldef.t4config.data;
1112 ldata.configuration.len = ldef.t4config.len;
1113
Jan Kiszka52253032010-02-08 10:12:10 +00001114 if (ctr->state != CAPI_CTR_DETECTED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 printk(KERN_INFO "kcapi: load: contr=%d not in detect state\n", ldef.contr);
Jan Kiszka0ca3a012010-02-08 10:12:14 +00001116 retval = -EBUSY;
1117 goto load_unlock_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 }
Jan Kiszka52253032010-02-08 10:12:10 +00001119 ctr->state = CAPI_CTR_LOADING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
Jan Kiszka52253032010-02-08 10:12:10 +00001121 retval = ctr->load_firmware(ctr, &ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 if (retval) {
Jan Kiszka52253032010-02-08 10:12:10 +00001123 ctr->state = CAPI_CTR_DETECTED;
Jan Kiszka0ca3a012010-02-08 10:12:14 +00001124 goto load_unlock_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 }
1126
Jan Kiszka0ca3a012010-02-08 10:12:14 +00001127 retval = wait_on_ctr_state(ctr, CAPI_CTR_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
Jan Kiszka0ca3a012010-02-08 10:12:14 +00001129load_unlock_out:
1130 mutex_unlock(&capi_controller_lock);
1131 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
1133 case AVMB1_RESETCARD:
1134 if (copy_from_user(&rdef, data, sizeof(avmb1_resetdef)))
1135 return -EFAULT;
Jan Kiszka0ca3a012010-02-08 10:12:14 +00001136
1137 retval = 0;
1138
1139 mutex_lock(&capi_controller_lock);
1140
Jan Kiszka52253032010-02-08 10:12:10 +00001141 ctr = get_capi_ctr_by_nr(rdef.contr);
Jan Kiszka0ca3a012010-02-08 10:12:14 +00001142 if (!ctr) {
1143 retval = -ESRCH;
1144 goto reset_unlock_out;
1145 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
Jan Kiszka52253032010-02-08 10:12:10 +00001147 if (ctr->state == CAPI_CTR_DETECTED)
Jan Kiszka0ca3a012010-02-08 10:12:14 +00001148 goto reset_unlock_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149
Jan Kiszka52253032010-02-08 10:12:10 +00001150 ctr->reset_ctr(ctr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
Jan Kiszka0ca3a012010-02-08 10:12:14 +00001152 retval = wait_on_ctr_state(ctr, CAPI_CTR_DETECTED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153
Jan Kiszka0ca3a012010-02-08 10:12:14 +00001154reset_unlock_out:
1155 mutex_unlock(&capi_controller_lock);
1156 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 }
1158 return -EINVAL;
1159}
1160#endif
1161
Tilman Schmidt554f2002009-04-23 02:24:21 +00001162/**
1163 * capi20_manufacturer() - CAPI 2.0 operation CAPI_MANUFACTURER
1164 * @cmd: command.
1165 * @data: parameter.
1166 *
1167 * Perform manufacturer specific command.
1168 * Return value: CAPI result code
1169 */
1170
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171int capi20_manufacturer(unsigned int cmd, void __user *data)
1172{
Jan Kiszka52253032010-02-08 10:12:10 +00001173 struct capi_ctr *ctr;
Jan Kiszka0ca3a012010-02-08 10:12:14 +00001174 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
1176 switch (cmd) {
Robert P. J. Day37772ac2008-04-28 02:14:42 -07001177#ifdef AVMB1_COMPAT
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 case AVMB1_LOAD:
1179 case AVMB1_LOAD_AND_CONFIG:
1180 case AVMB1_RESETCARD:
1181 case AVMB1_GET_CARDINFO:
1182 case AVMB1_REMOVECARD:
1183 return old_capi_manufacturer(cmd, data);
1184#endif
1185 case KCAPI_CMD_TRACE:
1186 {
1187 kcapi_flagdef fdef;
1188
1189 if (copy_from_user(&fdef, data, sizeof(kcapi_flagdef)))
1190 return -EFAULT;
1191
Jan Kiszka0ca3a012010-02-08 10:12:14 +00001192 mutex_lock(&capi_controller_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
Jan Kiszka0ca3a012010-02-08 10:12:14 +00001194 ctr = get_capi_ctr_by_nr(fdef.contr);
1195 if (ctr) {
1196 ctr->traceflag = fdef.flag;
1197 printk(KERN_INFO "kcapi: contr [%03d] set trace=%d\n",
1198 ctr->cnr, ctr->traceflag);
1199 retval = 0;
1200 } else
1201 retval = -ESRCH;
1202
1203 mutex_unlock(&capi_controller_lock);
1204
1205 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 }
1207 case KCAPI_CMD_ADDCARD:
1208 {
1209 struct list_head *l;
1210 struct capi_driver *driver = NULL;
1211 capicardparams cparams;
1212 kcapi_carddef cdef;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
1214 if ((retval = copy_from_user(&cdef, data, sizeof(cdef))))
1215 return retval;
1216
1217 cparams.port = cdef.port;
1218 cparams.irq = cdef.irq;
1219 cparams.membase = cdef.membase;
1220 cparams.cardnr = cdef.cardnr;
1221 cparams.cardtype = 0;
1222 cdef.driver[sizeof(cdef.driver)-1] = 0;
1223
Jan Kiszka9717fb82010-02-08 10:12:11 +00001224 mutex_lock(&capi_drivers_lock);
1225
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 list_for_each(l, &capi_drivers) {
1227 driver = list_entry(l, struct capi_driver, list);
1228 if (strcmp(driver->name, cdef.driver) == 0)
1229 break;
1230 }
Harvey Harrison2f9e9b62008-04-28 02:14:37 -07001231 if (driver == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 printk(KERN_ERR "kcapi: driver \"%s\" not loaded.\n",
1233 cdef.driver);
Jan Kiszka9717fb82010-02-08 10:12:11 +00001234 retval = -ESRCH;
1235 } else if (!driver->add_card) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 printk(KERN_ERR "kcapi: driver \"%s\" has no add card function.\n", cdef.driver);
Jan Kiszka9717fb82010-02-08 10:12:11 +00001237 retval = -EIO;
1238 } else
1239 retval = driver->add_card(driver, &cparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
Jan Kiszka9717fb82010-02-08 10:12:11 +00001241 mutex_unlock(&capi_drivers_lock);
1242 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 }
1244
1245 default:
1246 printk(KERN_ERR "kcapi: manufacturer command %d unknown.\n",
1247 cmd);
1248 break;
1249
1250 }
1251 return -EINVAL;
1252}
1253
1254EXPORT_SYMBOL(capi20_manufacturer);
1255
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256/* ------------------------------------------------------------- */
1257/* -------- Init & Cleanup ------------------------------------- */
1258/* ------------------------------------------------------------- */
1259
1260/*
1261 * init / exit functions
1262 */
1263
Jan Kiszkaef69bb22010-02-08 10:12:13 +00001264static struct notifier_block capictr_nb = {
1265 .notifier_call = notify_handler,
1266 .priority = INT_MAX,
1267};
1268
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269static int __init kcapi_init(void)
1270{
Jan Kiszka88549d62010-02-08 10:12:09 +00001271 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272
Jan Kiszkaef69bb22010-02-08 10:12:13 +00001273 register_capictr_notifier(&capictr_nb);
1274
Jan Kiszka88549d62010-02-08 10:12:09 +00001275 err = cdebug_init();
1276 if (!err)
1277 kcapi_proc_init();
1278 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279}
1280
1281static void __exit kcapi_exit(void)
1282{
1283 kcapi_proc_exit();
1284
1285 /* make sure all notifiers are finished */
1286 flush_scheduled_work();
Karsten Keil17f0cd22007-02-28 20:13:50 -08001287 cdebug_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288}
1289
1290module_init(kcapi_init);
1291module_exit(kcapi_exit);