blob: a99f7e3f8f51f8c00f0e1b5dfd2edc6168ebd870 [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>
30#include <asm/uaccess.h>
31#include <linux/isdn/capicmd.h>
32#include <linux/isdn/capiutil.h>
Robert P. J. Day37772ac2008-04-28 02:14:42 -070033#ifdef AVMB1_COMPAT
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/b1lli.h>
35#endif
Arjan van de Ven9cdf1822006-03-23 03:00:21 -080036#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Linus Torvalds1da177e2005-04-16 15:20:36 -070038static int showcapimsgs = 0;
39
40MODULE_DESCRIPTION("CAPI4Linux: kernel CAPI layer");
41MODULE_AUTHOR("Carsten Paeth");
42MODULE_LICENSE("GPL");
43module_param(showcapimsgs, uint, 0);
44
45/* ------------------------------------------------------------- */
46
Jan Kiszkaef69bb22010-02-08 10:12:13 +000047struct capictr_event {
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 struct work_struct work;
Jan Kiszkaef69bb22010-02-08 10:12:13 +000049 unsigned int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 u32 controller;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051};
52
53/* ------------------------------------------------------------- */
54
55static struct capi_version driver_version = {2, 0, 1, 1<<4};
56static char driver_serial[CAPI_SERIAL_LEN] = "0004711";
57static char capi_manufakturer[64] = "AVM Berlin";
58
59#define NCCI2CTRL(ncci) (((ncci) >> 24) & 0x7f)
60
61LIST_HEAD(capi_drivers);
Jan Kiszka9717fb82010-02-08 10:12:11 +000062DEFINE_MUTEX(capi_drivers_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Jan Kiszka0ca3a012010-02-08 10:12:14 +000064struct capi_ctr *capi_controller[CAPI_MAXCONTR];
65DEFINE_MUTEX(capi_controller_lock);
66
Linus Torvalds1da177e2005-04-16 15:20:36 -070067static DEFINE_RWLOCK(application_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69struct 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
106 return capi_applications[applid - 1];
107}
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);
189 if (!ap || ap->release_in_progress)
190 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 Kiszkaef69bb22010-02-08 10:12:13 +0000219 if (ap && !ap->release_in_progress)
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;
339 unsigned long flags;
Karsten Keil17f0cd22007-02-28 20:13:50 -0800340 _cdebbuf *cdb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
Jan Kiszka52253032010-02-08 10:12:10 +0000342 if (ctr->state != CAPI_CTR_RUNNING) {
Karsten Keil17f0cd22007-02-28 20:13:50 -0800343 cdb = capi_message2str(skb->data);
344 if (cdb) {
345 printk(KERN_INFO "kcapi: controller [%03d] not active, got: %s",
Jan Kiszka52253032010-02-08 10:12:10 +0000346 ctr->cnr, cdb->buf);
Karsten Keil17f0cd22007-02-28 20:13:50 -0800347 cdebbuf_free(cdb);
348 } else
349 printk(KERN_INFO "kcapi: controller [%03d] not active, cannot trace\n",
Jan Kiszka52253032010-02-08 10:12:10 +0000350 ctr->cnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 goto error;
352 }
353
354 cmd = CAPIMSG_COMMAND(skb->data);
355 subcmd = CAPIMSG_SUBCOMMAND(skb->data);
356 if (cmd == CAPI_DATA_B3 && subcmd == CAPI_IND) {
Jan Kiszka52253032010-02-08 10:12:10 +0000357 ctr->nrecvdatapkt++;
358 if (ctr->traceflag > 2)
359 showctl |= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 } else {
Jan Kiszka52253032010-02-08 10:12:10 +0000361 ctr->nrecvctlpkt++;
362 if (ctr->traceflag)
363 showctl |= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 }
Jan Kiszka52253032010-02-08 10:12:10 +0000365 showctl |= (ctr->traceflag & 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 if (showctl & 2) {
367 if (showctl & 1) {
Karsten Keil17f0cd22007-02-28 20:13:50 -0800368 printk(KERN_DEBUG "kcapi: got [%03d] id#%d %s len=%u\n",
Jan Kiszka52253032010-02-08 10:12:10 +0000369 ctr->cnr, CAPIMSG_APPID(skb->data),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 capi_cmd2str(cmd, subcmd),
371 CAPIMSG_LEN(skb->data));
372 } else {
Karsten Keil17f0cd22007-02-28 20:13:50 -0800373 cdb = capi_message2str(skb->data);
374 if (cdb) {
375 printk(KERN_DEBUG "kcapi: got [%03d] %s\n",
Jan Kiszka52253032010-02-08 10:12:10 +0000376 ctr->cnr, cdb->buf);
Karsten Keil17f0cd22007-02-28 20:13:50 -0800377 cdebbuf_free(cdb);
378 } else
379 printk(KERN_DEBUG "kcapi: got [%03d] id#%d %s len=%u, cannot trace\n",
Jan Kiszka52253032010-02-08 10:12:10 +0000380 ctr->cnr, CAPIMSG_APPID(skb->data),
Karsten Keil17f0cd22007-02-28 20:13:50 -0800381 capi_cmd2str(cmd, subcmd),
382 CAPIMSG_LEN(skb->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 }
384
385 }
386
387 read_lock_irqsave(&application_lock, flags);
388 ap = get_capi_appl_by_nr(CAPIMSG_APPID(skb->data));
389 if ((!ap) || (ap->release_in_progress)) {
390 read_unlock_irqrestore(&application_lock, flags);
Karsten Keil17f0cd22007-02-28 20:13:50 -0800391 cdb = capi_message2str(skb->data);
392 if (cdb) {
393 printk(KERN_ERR "kcapi: handle_message: applid %d state released (%s)\n",
394 CAPIMSG_APPID(skb->data), cdb->buf);
395 cdebbuf_free(cdb);
396 } else
397 printk(KERN_ERR "kcapi: handle_message: applid %d state released (%s) cannot trace\n",
398 CAPIMSG_APPID(skb->data),
399 capi_cmd2str(cmd, subcmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 goto error;
401 }
402 skb_queue_tail(&ap->recv_queue, skb);
403 schedule_work(&ap->recv_work);
404 read_unlock_irqrestore(&application_lock, flags);
405
406 return;
407
408error:
409 kfree_skb(skb);
410}
411
412EXPORT_SYMBOL(capi_ctr_handle_message);
413
Tilman Schmidt554f2002009-04-23 02:24:21 +0000414/**
415 * capi_ctr_ready() - signal CAPI controller ready
Jan Kiszka52253032010-02-08 10:12:10 +0000416 * @ctr: controller descriptor structure.
Tilman Schmidt554f2002009-04-23 02:24:21 +0000417 *
418 * Called by hardware driver to signal that the controller is up and running.
419 */
420
Jan Kiszka52253032010-02-08 10:12:10 +0000421void capi_ctr_ready(struct capi_ctr *ctr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422{
Jan Kiszka52253032010-02-08 10:12:10 +0000423 printk(KERN_NOTICE "kcapi: controller [%03d] \"%s\" ready.\n",
424 ctr->cnr, ctr->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
Jan Kiszkaef69bb22010-02-08 10:12:13 +0000426 notify_push(CAPICTR_UP, ctr->cnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427}
428
429EXPORT_SYMBOL(capi_ctr_ready);
430
Tilman Schmidt554f2002009-04-23 02:24:21 +0000431/**
Tilman Schmidt4e329972009-06-07 09:09:23 +0000432 * capi_ctr_down() - signal CAPI controller not ready
Jan Kiszka52253032010-02-08 10:12:10 +0000433 * @ctr: controller descriptor structure.
Tilman Schmidt554f2002009-04-23 02:24:21 +0000434 *
435 * Called by hardware driver to signal that the controller is down and
436 * unavailable for use.
437 */
438
Jan Kiszka52253032010-02-08 10:12:10 +0000439void capi_ctr_down(struct capi_ctr *ctr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440{
Jan Kiszka52253032010-02-08 10:12:10 +0000441 printk(KERN_NOTICE "kcapi: controller [%03d] down.\n", ctr->cnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
Jan Kiszkaef69bb22010-02-08 10:12:13 +0000443 notify_push(CAPICTR_DOWN, ctr->cnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444}
445
Tilman Schmidt4e329972009-06-07 09:09:23 +0000446EXPORT_SYMBOL(capi_ctr_down);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
Tilman Schmidt554f2002009-04-23 02:24:21 +0000448/**
449 * capi_ctr_suspend_output() - suspend controller
Jan Kiszka52253032010-02-08 10:12:10 +0000450 * @ctr: controller descriptor structure.
Tilman Schmidt554f2002009-04-23 02:24:21 +0000451 *
452 * Called by hardware driver to stop data flow.
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000453 *
454 * Note: The caller is responsible for synchronizing concurrent state changes
455 * as well as invocations of capi_ctr_handle_message.
Tilman Schmidt554f2002009-04-23 02:24:21 +0000456 */
457
Jan Kiszka52253032010-02-08 10:12:10 +0000458void capi_ctr_suspend_output(struct capi_ctr *ctr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459{
Jan Kiszka52253032010-02-08 10:12:10 +0000460 if (!ctr->blocked) {
461 printk(KERN_DEBUG "kcapi: controller [%03d] suspend\n",
462 ctr->cnr);
463 ctr->blocked = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 }
465}
466
467EXPORT_SYMBOL(capi_ctr_suspend_output);
468
Tilman Schmidt554f2002009-04-23 02:24:21 +0000469/**
470 * capi_ctr_resume_output() - resume controller
Jan Kiszka52253032010-02-08 10:12:10 +0000471 * @ctr: controller descriptor structure.
Tilman Schmidt554f2002009-04-23 02:24:21 +0000472 *
473 * Called by hardware driver to resume data flow.
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000474 *
475 * Note: The caller is responsible for synchronizing concurrent state changes
476 * as well as invocations of capi_ctr_handle_message.
Tilman Schmidt554f2002009-04-23 02:24:21 +0000477 */
478
Jan Kiszka52253032010-02-08 10:12:10 +0000479void capi_ctr_resume_output(struct capi_ctr *ctr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480{
Jan Kiszka52253032010-02-08 10:12:10 +0000481 if (ctr->blocked) {
482 printk(KERN_DEBUG "kcapi: controller [%03d] resumed\n",
483 ctr->cnr);
484 ctr->blocked = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 }
486}
487
488EXPORT_SYMBOL(capi_ctr_resume_output);
489
490/* ------------------------------------------------------------- */
491
Tilman Schmidt554f2002009-04-23 02:24:21 +0000492/**
493 * attach_capi_ctr() - register CAPI controller
Jan Kiszka52253032010-02-08 10:12:10 +0000494 * @ctr: controller descriptor structure.
Tilman Schmidt554f2002009-04-23 02:24:21 +0000495 *
496 * Called by hardware driver to register a controller with the CAPI subsystem.
497 * Return value: 0 on success, error code < 0 on error
498 */
499
Jan Kiszka52253032010-02-08 10:12:10 +0000500int attach_capi_ctr(struct capi_ctr *ctr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501{
502 int i;
503
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000504 mutex_lock(&capi_controller_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
506 for (i = 0; i < CAPI_MAXCONTR; i++) {
Jan Kiszka52253032010-02-08 10:12:10 +0000507 if (!capi_controller[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 break;
509 }
510 if (i == CAPI_MAXCONTR) {
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000511 mutex_unlock(&capi_controller_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 printk(KERN_ERR "kcapi: out of controller slots\n");
513 return -EBUSY;
514 }
Jan Kiszka52253032010-02-08 10:12:10 +0000515 capi_controller[i] = ctr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
Jan Kiszka52253032010-02-08 10:12:10 +0000517 ctr->nrecvctlpkt = 0;
518 ctr->nrecvdatapkt = 0;
519 ctr->nsentctlpkt = 0;
520 ctr->nsentdatapkt = 0;
521 ctr->cnr = i + 1;
522 ctr->state = CAPI_CTR_DETECTED;
523 ctr->blocked = 0;
524 ctr->traceflag = showcapimsgs;
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000525 init_waitqueue_head(&ctr->state_wait_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
Jan Kiszka52253032010-02-08 10:12:10 +0000527 sprintf(ctr->procfn, "capi/controllers/%d", ctr->cnr);
528 ctr->procent = proc_create_data(ctr->procfn, 0, NULL, ctr->proc_fops, ctr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
Jan Kiszka52253032010-02-08 10:12:10 +0000530 ncontrollers++;
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000531
532 mutex_unlock(&capi_controller_lock);
533
Jan Kiszka52253032010-02-08 10:12:10 +0000534 printk(KERN_NOTICE "kcapi: controller [%03d]: %s attached\n",
535 ctr->cnr, ctr->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 return 0;
537}
538
539EXPORT_SYMBOL(attach_capi_ctr);
540
Tilman Schmidt554f2002009-04-23 02:24:21 +0000541/**
542 * detach_capi_ctr() - unregister CAPI controller
Jan Kiszka52253032010-02-08 10:12:10 +0000543 * @ctr: controller descriptor structure.
Tilman Schmidt554f2002009-04-23 02:24:21 +0000544 *
545 * Called by hardware driver to remove the registration of a controller
546 * with the CAPI subsystem.
547 * Return value: 0 on success, error code < 0 on error
548 */
549
Jan Kiszka52253032010-02-08 10:12:10 +0000550int detach_capi_ctr(struct capi_ctr *ctr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551{
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000552 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000554 mutex_lock(&capi_controller_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000556 ctr_down(ctr, CAPI_CTR_DETACHED);
557
558 if (capi_controller[ctr->cnr - 1] != ctr) {
559 err = -EINVAL;
560 goto unlock_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 }
Jan Kiszka52253032010-02-08 10:12:10 +0000562 capi_controller[ctr->cnr - 1] = NULL;
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000563 ncontrollers--;
564
565 if (ctr->procent)
566 remove_proc_entry(ctr->procfn, NULL);
567
Jan Kiszka52253032010-02-08 10:12:10 +0000568 printk(KERN_NOTICE "kcapi: controller [%03d]: %s unregistered\n",
569 ctr->cnr, ctr->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000571unlock_out:
572 mutex_unlock(&capi_controller_lock);
573
574 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575}
576
577EXPORT_SYMBOL(detach_capi_ctr);
578
Tilman Schmidt554f2002009-04-23 02:24:21 +0000579/**
580 * register_capi_driver() - register CAPI driver
581 * @driver: driver descriptor structure.
582 *
583 * Called by hardware driver to register itself with the CAPI subsystem.
584 */
585
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586void register_capi_driver(struct capi_driver *driver)
587{
Jan Kiszka9717fb82010-02-08 10:12:11 +0000588 mutex_lock(&capi_drivers_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 list_add_tail(&driver->list, &capi_drivers);
Jan Kiszka9717fb82010-02-08 10:12:11 +0000590 mutex_unlock(&capi_drivers_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591}
592
593EXPORT_SYMBOL(register_capi_driver);
594
Tilman Schmidt554f2002009-04-23 02:24:21 +0000595/**
596 * unregister_capi_driver() - unregister CAPI driver
597 * @driver: driver descriptor structure.
598 *
599 * Called by hardware driver to unregister itself from the CAPI subsystem.
600 */
601
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602void unregister_capi_driver(struct capi_driver *driver)
603{
Jan Kiszka9717fb82010-02-08 10:12:11 +0000604 mutex_lock(&capi_drivers_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 list_del(&driver->list);
Jan Kiszka9717fb82010-02-08 10:12:11 +0000606 mutex_unlock(&capi_drivers_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607}
608
609EXPORT_SYMBOL(unregister_capi_driver);
610
611/* ------------------------------------------------------------- */
612/* -------- CAPI2.0 Interface ---------------------------------- */
613/* ------------------------------------------------------------- */
614
Tilman Schmidt554f2002009-04-23 02:24:21 +0000615/**
616 * capi20_isinstalled() - CAPI 2.0 operation CAPI_INSTALLED
617 *
618 * Return value: CAPI result code (CAPI_NOERROR if at least one ISDN controller
619 * is ready for use, CAPI_REGNOTINSTALLED otherwise)
620 */
621
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622u16 capi20_isinstalled(void)
623{
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000624 u16 ret = CAPI_REGNOTINSTALLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 int i;
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000626
627 mutex_lock(&capi_controller_lock);
628
629 for (i = 0; i < CAPI_MAXCONTR; i++)
Jan Kiszka52253032010-02-08 10:12:10 +0000630 if (capi_controller[i] &&
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000631 capi_controller[i]->state == CAPI_CTR_RUNNING) {
632 ret = CAPI_NOERROR;
633 break;
634 }
635
636 mutex_unlock(&capi_controller_lock);
637
638 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639}
640
641EXPORT_SYMBOL(capi20_isinstalled);
642
Tilman Schmidt554f2002009-04-23 02:24:21 +0000643/**
644 * capi20_register() - CAPI 2.0 operation CAPI_REGISTER
645 * @ap: CAPI application descriptor structure.
646 *
647 * Register an application's presence with CAPI.
648 * A unique application ID is assigned and stored in @ap->applid.
649 * After this function returns successfully, the message receive
650 * callback function @ap->recv_message() may be called at any time
651 * until capi20_release() has been called for the same @ap.
652 * Return value: CAPI result code
653 */
654
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655u16 capi20_register(struct capi20_appl *ap)
656{
657 int i;
658 u16 applid;
659 unsigned long flags;
660
661 DBG("");
662
663 if (ap->rparam.datablklen < 128)
664 return CAPI_LOGBLKSIZETOSMALL;
665
666 write_lock_irqsave(&application_lock, flags);
667
668 for (applid = 1; applid <= CAPI_MAXAPPL; applid++) {
669 if (capi_applications[applid - 1] == NULL)
670 break;
671 }
672 if (applid > CAPI_MAXAPPL) {
673 write_unlock_irqrestore(&application_lock, flags);
674 return CAPI_TOOMANYAPPLS;
675 }
676
677 ap->applid = applid;
678 capi_applications[applid - 1] = ap;
679
680 ap->nrecvctlpkt = 0;
681 ap->nrecvdatapkt = 0;
682 ap->nsentctlpkt = 0;
683 ap->nsentdatapkt = 0;
Matthias Kaehlcke67837f22007-07-17 04:04:16 -0700684 mutex_init(&ap->recv_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 skb_queue_head_init(&ap->recv_queue);
David Howellsc4028952006-11-22 14:57:56 +0000686 INIT_WORK(&ap->recv_work, recv_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 ap->release_in_progress = 0;
688
689 write_unlock_irqrestore(&application_lock, flags);
690
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000691 mutex_lock(&capi_controller_lock);
692
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 for (i = 0; i < CAPI_MAXCONTR; i++) {
Jan Kiszka52253032010-02-08 10:12:10 +0000694 if (!capi_controller[i] ||
695 capi_controller[i]->state != CAPI_CTR_RUNNING)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 continue;
Jan Kiszka52253032010-02-08 10:12:10 +0000697 register_appl(capi_controller[i], applid, &ap->rparam);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 }
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000699
700 mutex_unlock(&capi_controller_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
702 if (showcapimsgs & 1) {
703 printk(KERN_DEBUG "kcapi: appl %d up\n", applid);
704 }
705
706 return CAPI_NOERROR;
707}
708
709EXPORT_SYMBOL(capi20_register);
710
Tilman Schmidt554f2002009-04-23 02:24:21 +0000711/**
712 * capi20_release() - CAPI 2.0 operation CAPI_RELEASE
713 * @ap: CAPI application descriptor structure.
714 *
715 * Terminate an application's registration with CAPI.
716 * After this function returns successfully, the message receive
717 * callback function @ap->recv_message() will no longer be called.
718 * Return value: CAPI result code
719 */
720
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721u16 capi20_release(struct capi20_appl *ap)
722{
723 int i;
724 unsigned long flags;
725
726 DBG("applid %#x", ap->applid);
727
728 write_lock_irqsave(&application_lock, flags);
729 ap->release_in_progress = 1;
730 capi_applications[ap->applid - 1] = NULL;
731 write_unlock_irqrestore(&application_lock, flags);
732
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000733 mutex_lock(&capi_controller_lock);
734
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 for (i = 0; i < CAPI_MAXCONTR; i++) {
Jan Kiszka52253032010-02-08 10:12:10 +0000736 if (!capi_controller[i] ||
737 capi_controller[i]->state != CAPI_CTR_RUNNING)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 continue;
Jan Kiszka52253032010-02-08 10:12:10 +0000739 release_appl(capi_controller[i], ap->applid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 }
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000741
742 mutex_unlock(&capi_controller_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
744 flush_scheduled_work();
745 skb_queue_purge(&ap->recv_queue);
746
747 if (showcapimsgs & 1) {
748 printk(KERN_DEBUG "kcapi: appl %d down\n", ap->applid);
749 }
750
751 return CAPI_NOERROR;
752}
753
754EXPORT_SYMBOL(capi20_release);
755
Tilman Schmidt554f2002009-04-23 02:24:21 +0000756/**
757 * capi20_put_message() - CAPI 2.0 operation CAPI_PUT_MESSAGE
758 * @ap: CAPI application descriptor structure.
759 * @skb: CAPI message.
760 *
761 * Transfer a single message to CAPI.
762 * Return value: CAPI result code
763 */
764
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765u16 capi20_put_message(struct capi20_appl *ap, struct sk_buff *skb)
766{
Jan Kiszka52253032010-02-08 10:12:10 +0000767 struct capi_ctr *ctr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 int showctl = 0;
769 u8 cmd, subcmd;
770
771 DBG("applid %#x", ap->applid);
772
Jan Kiszka52253032010-02-08 10:12:10 +0000773 if (ncontrollers == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 return CAPI_REGNOTINSTALLED;
775 if ((ap->applid == 0) || ap->release_in_progress)
776 return CAPI_ILLAPPNR;
777 if (skb->len < 12
778 || !capi_cmd_valid(CAPIMSG_COMMAND(skb->data))
779 || !capi_subcmd_valid(CAPIMSG_SUBCOMMAND(skb->data)))
780 return CAPI_ILLCMDORSUBCMDORMSGTOSMALL;
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000781
782 /*
783 * The controller reference is protected by the existence of the
784 * application passed to us. We assume that the caller properly
785 * synchronizes this service with capi20_release.
786 */
Jan Kiszka52253032010-02-08 10:12:10 +0000787 ctr = get_capi_ctr_by_nr(CAPIMSG_CONTROLLER(skb->data));
788 if (!ctr || ctr->state != CAPI_CTR_RUNNING) {
789 ctr = get_capi_ctr_by_nr(1); /* XXX why? */
790 if (!ctr || ctr->state != CAPI_CTR_RUNNING)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 return CAPI_REGNOTINSTALLED;
792 }
Jan Kiszka52253032010-02-08 10:12:10 +0000793 if (ctr->blocked)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 return CAPI_SENDQUEUEFULL;
795
796 cmd = CAPIMSG_COMMAND(skb->data);
797 subcmd = CAPIMSG_SUBCOMMAND(skb->data);
798
799 if (cmd == CAPI_DATA_B3 && subcmd== CAPI_REQ) {
Jan Kiszka52253032010-02-08 10:12:10 +0000800 ctr->nsentdatapkt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 ap->nsentdatapkt++;
Jan Kiszka52253032010-02-08 10:12:10 +0000802 if (ctr->traceflag > 2)
803 showctl |= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 } else {
Jan Kiszka52253032010-02-08 10:12:10 +0000805 ctr->nsentctlpkt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 ap->nsentctlpkt++;
Jan Kiszka52253032010-02-08 10:12:10 +0000807 if (ctr->traceflag)
808 showctl |= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 }
Jan Kiszka52253032010-02-08 10:12:10 +0000810 showctl |= (ctr->traceflag & 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 if (showctl & 2) {
812 if (showctl & 1) {
Karsten Keil17f0cd22007-02-28 20:13:50 -0800813 printk(KERN_DEBUG "kcapi: put [%03d] id#%d %s len=%u\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 CAPIMSG_CONTROLLER(skb->data),
815 CAPIMSG_APPID(skb->data),
816 capi_cmd2str(cmd, subcmd),
817 CAPIMSG_LEN(skb->data));
818 } else {
Karsten Keil17f0cd22007-02-28 20:13:50 -0800819 _cdebbuf *cdb = capi_message2str(skb->data);
820 if (cdb) {
821 printk(KERN_DEBUG "kcapi: put [%03d] %s\n",
822 CAPIMSG_CONTROLLER(skb->data),
823 cdb->buf);
824 cdebbuf_free(cdb);
825 } else
826 printk(KERN_DEBUG "kcapi: put [%03d] id#%d %s len=%u cannot trace\n",
827 CAPIMSG_CONTROLLER(skb->data),
828 CAPIMSG_APPID(skb->data),
829 capi_cmd2str(cmd, subcmd),
830 CAPIMSG_LEN(skb->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 }
Jan Kiszka52253032010-02-08 10:12:10 +0000833 return ctr->send_message(ctr, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834}
835
836EXPORT_SYMBOL(capi20_put_message);
837
Tilman Schmidt554f2002009-04-23 02:24:21 +0000838/**
839 * capi20_get_manufacturer() - CAPI 2.0 operation CAPI_GET_MANUFACTURER
840 * @contr: controller number.
841 * @buf: result buffer (64 bytes).
842 *
843 * Retrieve information about the manufacturer of the specified ISDN controller
844 * or (for @contr == 0) the driver itself.
845 * Return value: CAPI result code
846 */
847
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848u16 capi20_get_manufacturer(u32 contr, u8 *buf)
849{
Jan Kiszka52253032010-02-08 10:12:10 +0000850 struct capi_ctr *ctr;
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000851 u16 ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
853 if (contr == 0) {
854 strlcpy(buf, capi_manufakturer, CAPI_MANUFACTURER_LEN);
855 return CAPI_NOERROR;
856 }
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000857
858 mutex_lock(&capi_controller_lock);
859
Jan Kiszka52253032010-02-08 10:12:10 +0000860 ctr = get_capi_ctr_by_nr(contr);
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000861 if (ctr && ctr->state == CAPI_CTR_RUNNING) {
862 strlcpy(buf, ctr->manu, CAPI_MANUFACTURER_LEN);
863 ret = CAPI_NOERROR;
864 } else
865 ret = CAPI_REGNOTINSTALLED;
866
867 mutex_unlock(&capi_controller_lock);
868 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869}
870
871EXPORT_SYMBOL(capi20_get_manufacturer);
872
Tilman Schmidt554f2002009-04-23 02:24:21 +0000873/**
874 * capi20_get_version() - CAPI 2.0 operation CAPI_GET_VERSION
875 * @contr: controller number.
876 * @verp: result structure.
877 *
878 * Retrieve version information for the specified ISDN controller
879 * or (for @contr == 0) the driver itself.
880 * Return value: CAPI result code
881 */
882
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883u16 capi20_get_version(u32 contr, struct capi_version *verp)
884{
Jan Kiszka52253032010-02-08 10:12:10 +0000885 struct capi_ctr *ctr;
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000886 u16 ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
888 if (contr == 0) {
889 *verp = driver_version;
890 return CAPI_NOERROR;
891 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000893 mutex_lock(&capi_controller_lock);
894
895 ctr = get_capi_ctr_by_nr(contr);
896 if (ctr && ctr->state == CAPI_CTR_RUNNING) {
897 memcpy(verp, &ctr->version, sizeof(capi_version));
898 ret = CAPI_NOERROR;
899 } else
900 ret = CAPI_REGNOTINSTALLED;
901
902 mutex_unlock(&capi_controller_lock);
903 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904}
905
906EXPORT_SYMBOL(capi20_get_version);
907
Tilman Schmidt554f2002009-04-23 02:24:21 +0000908/**
909 * capi20_get_serial() - CAPI 2.0 operation CAPI_GET_SERIAL_NUMBER
910 * @contr: controller number.
911 * @serial: result buffer (8 bytes).
912 *
913 * Retrieve the serial number of the specified ISDN controller
914 * or (for @contr == 0) the driver itself.
915 * Return value: CAPI result code
916 */
917
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918u16 capi20_get_serial(u32 contr, u8 *serial)
919{
Jan Kiszka52253032010-02-08 10:12:10 +0000920 struct capi_ctr *ctr;
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000921 u16 ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
923 if (contr == 0) {
924 strlcpy(serial, driver_serial, CAPI_SERIAL_LEN);
925 return CAPI_NOERROR;
926 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000928 mutex_lock(&capi_controller_lock);
929
930 ctr = get_capi_ctr_by_nr(contr);
931 if (ctr && ctr->state == CAPI_CTR_RUNNING) {
932 strlcpy(serial, ctr->serial, CAPI_SERIAL_LEN);
933 ret = CAPI_NOERROR;
934 } else
935 ret = CAPI_REGNOTINSTALLED;
936
937 mutex_unlock(&capi_controller_lock);
938 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939}
940
941EXPORT_SYMBOL(capi20_get_serial);
942
Tilman Schmidt554f2002009-04-23 02:24:21 +0000943/**
944 * capi20_get_profile() - CAPI 2.0 operation CAPI_GET_PROFILE
945 * @contr: controller number.
946 * @profp: result structure.
947 *
948 * Retrieve capability information for the specified ISDN controller
949 * or (for @contr == 0) the number of installed controllers.
950 * Return value: CAPI result code
951 */
952
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953u16 capi20_get_profile(u32 contr, struct capi_profile *profp)
954{
Jan Kiszka52253032010-02-08 10:12:10 +0000955 struct capi_ctr *ctr;
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000956 u16 ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
958 if (contr == 0) {
Jan Kiszka52253032010-02-08 10:12:10 +0000959 profp->ncontroller = ncontrollers;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 return CAPI_NOERROR;
961 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000963 mutex_lock(&capi_controller_lock);
964
965 ctr = get_capi_ctr_by_nr(contr);
966 if (ctr && ctr->state == CAPI_CTR_RUNNING) {
967 memcpy(profp, &ctr->profile, sizeof(struct capi_profile));
968 ret = CAPI_NOERROR;
969 } else
970 ret = CAPI_REGNOTINSTALLED;
971
972 mutex_unlock(&capi_controller_lock);
973 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974}
975
976EXPORT_SYMBOL(capi20_get_profile);
977
Jan Kiszka0ca3a012010-02-08 10:12:14 +0000978/* Must be called with capi_controller_lock held. */
979static int wait_on_ctr_state(struct capi_ctr *ctr, unsigned int state)
980{
981 DEFINE_WAIT(wait);
982 int retval = 0;
983
984 ctr = capi_ctr_get(ctr);
985 if (!ctr)
986 return -ESRCH;
987
988 for (;;) {
989 prepare_to_wait(&ctr->state_wait_queue, &wait,
990 TASK_INTERRUPTIBLE);
991
992 if (ctr->state == state)
993 break;
994 if (ctr->state == CAPI_CTR_DETACHED) {
995 retval = -ESRCH;
996 break;
997 }
998 if (signal_pending(current)) {
999 retval = -EINTR;
1000 break;
1001 }
1002
1003 mutex_unlock(&capi_controller_lock);
1004 schedule();
1005 mutex_lock(&capi_controller_lock);
1006 }
1007 finish_wait(&ctr->state_wait_queue, &wait);
1008
1009 capi_ctr_put(ctr);
1010
1011 return retval;
1012}
1013
Robert P. J. Day37772ac2008-04-28 02:14:42 -07001014#ifdef AVMB1_COMPAT
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015static int old_capi_manufacturer(unsigned int cmd, void __user *data)
1016{
1017 avmb1_loadandconfigdef ldef;
1018 avmb1_extcarddef cdef;
1019 avmb1_resetdef rdef;
1020 capicardparams cparams;
Jan Kiszka52253032010-02-08 10:12:10 +00001021 struct capi_ctr *ctr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 struct capi_driver *driver = NULL;
1023 capiloaddata ldata;
1024 struct list_head *l;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 int retval;
1026
1027 switch (cmd) {
1028 case AVMB1_ADDCARD:
1029 case AVMB1_ADDCARD_WITH_TYPE:
1030 if (cmd == AVMB1_ADDCARD) {
1031 if ((retval = copy_from_user(&cdef, data,
1032 sizeof(avmb1_carddef))))
1033 return retval;
1034 cdef.cardtype = AVM_CARDTYPE_B1;
1035 } else {
1036 if ((retval = copy_from_user(&cdef, data,
1037 sizeof(avmb1_extcarddef))))
1038 return retval;
1039 }
1040 cparams.port = cdef.port;
1041 cparams.irq = cdef.irq;
1042 cparams.cardnr = cdef.cardnr;
1043
Jan Kiszka9717fb82010-02-08 10:12:11 +00001044 mutex_lock(&capi_drivers_lock);
1045
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 switch (cdef.cardtype) {
1047 case AVM_CARDTYPE_B1:
1048 list_for_each(l, &capi_drivers) {
1049 driver = list_entry(l, struct capi_driver, list);
1050 if (strcmp(driver->name, "b1isa") == 0)
1051 break;
1052 }
1053 break;
1054 case AVM_CARDTYPE_T1:
1055 list_for_each(l, &capi_drivers) {
1056 driver = list_entry(l, struct capi_driver, list);
1057 if (strcmp(driver->name, "t1isa") == 0)
1058 break;
1059 }
1060 break;
1061 default:
1062 driver = NULL;
1063 break;
1064 }
1065 if (!driver) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 printk(KERN_ERR "kcapi: driver not loaded.\n");
Jan Kiszka9717fb82010-02-08 10:12:11 +00001067 retval = -EIO;
1068 } else if (!driver->add_card) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 printk(KERN_ERR "kcapi: driver has no add card function.\n");
Jan Kiszka9717fb82010-02-08 10:12:11 +00001070 retval = -EIO;
1071 } else
1072 retval = driver->add_card(driver, &cparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
Jan Kiszka9717fb82010-02-08 10:12:11 +00001074 mutex_unlock(&capi_drivers_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 return retval;
1076
1077 case AVMB1_LOAD:
1078 case AVMB1_LOAD_AND_CONFIG:
1079
1080 if (cmd == AVMB1_LOAD) {
1081 if (copy_from_user(&ldef, data,
1082 sizeof(avmb1_loaddef)))
1083 return -EFAULT;
1084 ldef.t4config.len = 0;
1085 ldef.t4config.data = NULL;
1086 } else {
1087 if (copy_from_user(&ldef, data,
1088 sizeof(avmb1_loadandconfigdef)))
1089 return -EFAULT;
1090 }
Jan Kiszka0ca3a012010-02-08 10:12:14 +00001091
1092 mutex_lock(&capi_controller_lock);
1093
Jan Kiszka52253032010-02-08 10:12:10 +00001094 ctr = get_capi_ctr_by_nr(ldef.contr);
Jan Kiszka0ca3a012010-02-08 10:12:14 +00001095 if (!ctr) {
1096 retval = -EINVAL;
1097 goto load_unlock_out;
1098 }
1099
Jan Kiszka52253032010-02-08 10:12:10 +00001100 if (ctr->load_firmware == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 printk(KERN_DEBUG "kcapi: load: no load function\n");
Jan Kiszka0ca3a012010-02-08 10:12:14 +00001102 retval = -ESRCH;
1103 goto load_unlock_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 }
1105
1106 if (ldef.t4file.len <= 0) {
1107 printk(KERN_DEBUG "kcapi: load: invalid parameter: length of t4file is %d ?\n", ldef.t4file.len);
Jan Kiszka0ca3a012010-02-08 10:12:14 +00001108 retval = -EINVAL;
1109 goto load_unlock_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 }
Harvey Harrison2f9e9b62008-04-28 02:14:37 -07001111 if (ldef.t4file.data == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 printk(KERN_DEBUG "kcapi: load: invalid parameter: dataptr is 0\n");
Jan Kiszka0ca3a012010-02-08 10:12:14 +00001113 retval = -EINVAL;
1114 goto load_unlock_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 }
1116
1117 ldata.firmware.user = 1;
1118 ldata.firmware.data = ldef.t4file.data;
1119 ldata.firmware.len = ldef.t4file.len;
1120 ldata.configuration.user = 1;
1121 ldata.configuration.data = ldef.t4config.data;
1122 ldata.configuration.len = ldef.t4config.len;
1123
Jan Kiszka52253032010-02-08 10:12:10 +00001124 if (ctr->state != CAPI_CTR_DETECTED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 printk(KERN_INFO "kcapi: load: contr=%d not in detect state\n", ldef.contr);
Jan Kiszka0ca3a012010-02-08 10:12:14 +00001126 retval = -EBUSY;
1127 goto load_unlock_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 }
Jan Kiszka52253032010-02-08 10:12:10 +00001129 ctr->state = CAPI_CTR_LOADING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130
Jan Kiszka52253032010-02-08 10:12:10 +00001131 retval = ctr->load_firmware(ctr, &ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 if (retval) {
Jan Kiszka52253032010-02-08 10:12:10 +00001133 ctr->state = CAPI_CTR_DETECTED;
Jan Kiszka0ca3a012010-02-08 10:12:14 +00001134 goto load_unlock_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 }
1136
Jan Kiszka0ca3a012010-02-08 10:12:14 +00001137 retval = wait_on_ctr_state(ctr, CAPI_CTR_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
Jan Kiszka0ca3a012010-02-08 10:12:14 +00001139load_unlock_out:
1140 mutex_unlock(&capi_controller_lock);
1141 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142
1143 case AVMB1_RESETCARD:
1144 if (copy_from_user(&rdef, data, sizeof(avmb1_resetdef)))
1145 return -EFAULT;
Jan Kiszka0ca3a012010-02-08 10:12:14 +00001146
1147 retval = 0;
1148
1149 mutex_lock(&capi_controller_lock);
1150
Jan Kiszka52253032010-02-08 10:12:10 +00001151 ctr = get_capi_ctr_by_nr(rdef.contr);
Jan Kiszka0ca3a012010-02-08 10:12:14 +00001152 if (!ctr) {
1153 retval = -ESRCH;
1154 goto reset_unlock_out;
1155 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156
Jan Kiszka52253032010-02-08 10:12:10 +00001157 if (ctr->state == CAPI_CTR_DETECTED)
Jan Kiszka0ca3a012010-02-08 10:12:14 +00001158 goto reset_unlock_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
Jan Kiszka52253032010-02-08 10:12:10 +00001160 ctr->reset_ctr(ctr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
Jan Kiszka0ca3a012010-02-08 10:12:14 +00001162 retval = wait_on_ctr_state(ctr, CAPI_CTR_DETECTED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
Jan Kiszka0ca3a012010-02-08 10:12:14 +00001164reset_unlock_out:
1165 mutex_unlock(&capi_controller_lock);
1166 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 }
1168 return -EINVAL;
1169}
1170#endif
1171
Tilman Schmidt554f2002009-04-23 02:24:21 +00001172/**
1173 * capi20_manufacturer() - CAPI 2.0 operation CAPI_MANUFACTURER
1174 * @cmd: command.
1175 * @data: parameter.
1176 *
1177 * Perform manufacturer specific command.
1178 * Return value: CAPI result code
1179 */
1180
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181int capi20_manufacturer(unsigned int cmd, void __user *data)
1182{
Jan Kiszka52253032010-02-08 10:12:10 +00001183 struct capi_ctr *ctr;
Jan Kiszka0ca3a012010-02-08 10:12:14 +00001184 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
1186 switch (cmd) {
Robert P. J. Day37772ac2008-04-28 02:14:42 -07001187#ifdef AVMB1_COMPAT
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 case AVMB1_LOAD:
1189 case AVMB1_LOAD_AND_CONFIG:
1190 case AVMB1_RESETCARD:
1191 case AVMB1_GET_CARDINFO:
1192 case AVMB1_REMOVECARD:
1193 return old_capi_manufacturer(cmd, data);
1194#endif
1195 case KCAPI_CMD_TRACE:
1196 {
1197 kcapi_flagdef fdef;
1198
1199 if (copy_from_user(&fdef, data, sizeof(kcapi_flagdef)))
1200 return -EFAULT;
1201
Jan Kiszka0ca3a012010-02-08 10:12:14 +00001202 mutex_lock(&capi_controller_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
Jan Kiszka0ca3a012010-02-08 10:12:14 +00001204 ctr = get_capi_ctr_by_nr(fdef.contr);
1205 if (ctr) {
1206 ctr->traceflag = fdef.flag;
1207 printk(KERN_INFO "kcapi: contr [%03d] set trace=%d\n",
1208 ctr->cnr, ctr->traceflag);
1209 retval = 0;
1210 } else
1211 retval = -ESRCH;
1212
1213 mutex_unlock(&capi_controller_lock);
1214
1215 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 }
1217 case KCAPI_CMD_ADDCARD:
1218 {
1219 struct list_head *l;
1220 struct capi_driver *driver = NULL;
1221 capicardparams cparams;
1222 kcapi_carddef cdef;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
1224 if ((retval = copy_from_user(&cdef, data, sizeof(cdef))))
1225 return retval;
1226
1227 cparams.port = cdef.port;
1228 cparams.irq = cdef.irq;
1229 cparams.membase = cdef.membase;
1230 cparams.cardnr = cdef.cardnr;
1231 cparams.cardtype = 0;
1232 cdef.driver[sizeof(cdef.driver)-1] = 0;
1233
Jan Kiszka9717fb82010-02-08 10:12:11 +00001234 mutex_lock(&capi_drivers_lock);
1235
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 list_for_each(l, &capi_drivers) {
1237 driver = list_entry(l, struct capi_driver, list);
1238 if (strcmp(driver->name, cdef.driver) == 0)
1239 break;
1240 }
Harvey Harrison2f9e9b62008-04-28 02:14:37 -07001241 if (driver == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 printk(KERN_ERR "kcapi: driver \"%s\" not loaded.\n",
1243 cdef.driver);
Jan Kiszka9717fb82010-02-08 10:12:11 +00001244 retval = -ESRCH;
1245 } else if (!driver->add_card) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 printk(KERN_ERR "kcapi: driver \"%s\" has no add card function.\n", cdef.driver);
Jan Kiszka9717fb82010-02-08 10:12:11 +00001247 retval = -EIO;
1248 } else
1249 retval = driver->add_card(driver, &cparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
Jan Kiszka9717fb82010-02-08 10:12:11 +00001251 mutex_unlock(&capi_drivers_lock);
1252 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 }
1254
1255 default:
1256 printk(KERN_ERR "kcapi: manufacturer command %d unknown.\n",
1257 cmd);
1258 break;
1259
1260 }
1261 return -EINVAL;
1262}
1263
1264EXPORT_SYMBOL(capi20_manufacturer);
1265
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266/* ------------------------------------------------------------- */
1267/* -------- Init & Cleanup ------------------------------------- */
1268/* ------------------------------------------------------------- */
1269
1270/*
1271 * init / exit functions
1272 */
1273
Jan Kiszkaef69bb22010-02-08 10:12:13 +00001274static struct notifier_block capictr_nb = {
1275 .notifier_call = notify_handler,
1276 .priority = INT_MAX,
1277};
1278
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279static int __init kcapi_init(void)
1280{
Jan Kiszka88549d62010-02-08 10:12:09 +00001281 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282
Jan Kiszkaef69bb22010-02-08 10:12:13 +00001283 register_capictr_notifier(&capictr_nb);
1284
Jan Kiszka88549d62010-02-08 10:12:09 +00001285 err = cdebug_init();
1286 if (!err)
1287 kcapi_proc_init();
1288 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289}
1290
1291static void __exit kcapi_exit(void)
1292{
1293 kcapi_proc_exit();
1294
1295 /* make sure all notifiers are finished */
1296 flush_scheduled_work();
Karsten Keil17f0cd22007-02-28 20:13:50 -08001297 cdebug_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298}
1299
1300module_init(kcapi_init);
1301module_exit(kcapi_exit);