1 /* $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 
13 #define AVMB1_COMPAT
14 
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>
21 #include <linux/sched/signal.h>
22 #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 <linux/slab.h>
31 #include <linux/uaccess.h>
32 #include <linux/isdn/capicmd.h>
33 #include <linux/isdn/capiutil.h>
34 #ifdef AVMB1_COMPAT
35 #include <linux/b1lli.h>
36 #endif
37 #include <linux/mutex.h>
38 #include <linux/rcupdate.h>
39 
40 static int showcapimsgs = 0;
41 static struct workqueue_struct *kcapi_wq;
42 
43 MODULE_DESCRIPTION("CAPI4Linux: kernel CAPI layer");
44 MODULE_AUTHOR("Carsten Paeth");
45 MODULE_LICENSE("GPL");
46 module_param(showcapimsgs, uint, 0);
47 
48 /* ------------------------------------------------------------- */
49 
50 struct capictr_event {
51 	struct work_struct work;
52 	unsigned int type;
53 	u32 controller;
54 };
55 
56 /* ------------------------------------------------------------- */
57 
58 static const struct capi_version driver_version = {2, 0, 1, 1 << 4};
59 static char driver_serial[CAPI_SERIAL_LEN] = "0004711";
60 static char capi_manufakturer[64] = "AVM Berlin";
61 
62 #define NCCI2CTRL(ncci)    (((ncci) >> 24) & 0x7f)
63 
64 LIST_HEAD(capi_drivers);
65 DEFINE_MUTEX(capi_drivers_lock);
66 
67 struct capi_ctr *capi_controller[CAPI_MAXCONTR];
68 DEFINE_MUTEX(capi_controller_lock);
69 
70 struct capi20_appl *capi_applications[CAPI_MAXAPPL];
71 
72 static int ncontrollers;
73 
74 static BLOCKING_NOTIFIER_HEAD(ctr_notifier_list);
75 
76 /* -------- controller ref counting -------------------------------------- */
77 
78 static inline struct capi_ctr *
capi_ctr_get(struct capi_ctr * ctr)79 capi_ctr_get(struct capi_ctr *ctr)
80 {
81 	if (!try_module_get(ctr->owner))
82 		return NULL;
83 	return ctr;
84 }
85 
86 static inline void
capi_ctr_put(struct capi_ctr * ctr)87 capi_ctr_put(struct capi_ctr *ctr)
88 {
89 	module_put(ctr->owner);
90 }
91 
92 /* ------------------------------------------------------------- */
93 
get_capi_ctr_by_nr(u16 contr)94 static inline struct capi_ctr *get_capi_ctr_by_nr(u16 contr)
95 {
96 	if (contr < 1 || contr - 1 >= CAPI_MAXCONTR)
97 		return NULL;
98 
99 	return capi_controller[contr - 1];
100 }
101 
__get_capi_appl_by_nr(u16 applid)102 static inline struct capi20_appl *__get_capi_appl_by_nr(u16 applid)
103 {
104 	lockdep_assert_held(&capi_controller_lock);
105 
106 	if (applid < 1 || applid - 1 >= CAPI_MAXAPPL)
107 		return NULL;
108 
109 	return capi_applications[applid - 1];
110 }
111 
get_capi_appl_by_nr(u16 applid)112 static inline struct capi20_appl *get_capi_appl_by_nr(u16 applid)
113 {
114 	if (applid < 1 || applid - 1 >= CAPI_MAXAPPL)
115 		return NULL;
116 
117 	return rcu_dereference(capi_applications[applid - 1]);
118 }
119 
120 /* -------- util functions ------------------------------------ */
121 
capi_cmd_valid(u8 cmd)122 static inline int capi_cmd_valid(u8 cmd)
123 {
124 	switch (cmd) {
125 	case CAPI_ALERT:
126 	case CAPI_CONNECT:
127 	case CAPI_CONNECT_ACTIVE:
128 	case CAPI_CONNECT_B3_ACTIVE:
129 	case CAPI_CONNECT_B3:
130 	case CAPI_CONNECT_B3_T90_ACTIVE:
131 	case CAPI_DATA_B3:
132 	case CAPI_DISCONNECT_B3:
133 	case CAPI_DISCONNECT:
134 	case CAPI_FACILITY:
135 	case CAPI_INFO:
136 	case CAPI_LISTEN:
137 	case CAPI_MANUFACTURER:
138 	case CAPI_RESET_B3:
139 	case CAPI_SELECT_B_PROTOCOL:
140 		return 1;
141 	}
142 	return 0;
143 }
144 
capi_subcmd_valid(u8 subcmd)145 static inline int capi_subcmd_valid(u8 subcmd)
146 {
147 	switch (subcmd) {
148 	case CAPI_REQ:
149 	case CAPI_CONF:
150 	case CAPI_IND:
151 	case CAPI_RESP:
152 		return 1;
153 	}
154 	return 0;
155 }
156 
157 /* ------------------------------------------------------------ */
158 
159 static void
register_appl(struct capi_ctr * ctr,u16 applid,capi_register_params * rparam)160 register_appl(struct capi_ctr *ctr, u16 applid, capi_register_params *rparam)
161 {
162 	ctr = capi_ctr_get(ctr);
163 
164 	if (ctr)
165 		ctr->register_appl(ctr, applid, rparam);
166 	else
167 		printk(KERN_WARNING "%s: cannot get controller resources\n",
168 		       __func__);
169 }
170 
171 
release_appl(struct capi_ctr * ctr,u16 applid)172 static void release_appl(struct capi_ctr *ctr, u16 applid)
173 {
174 	DBG("applid %#x", applid);
175 
176 	ctr->release_appl(ctr, applid);
177 	capi_ctr_put(ctr);
178 }
179 
notify_up(u32 contr)180 static void notify_up(u32 contr)
181 {
182 	struct capi20_appl *ap;
183 	struct capi_ctr *ctr;
184 	u16 applid;
185 
186 	mutex_lock(&capi_controller_lock);
187 
188 	if (showcapimsgs & 1)
189 		printk(KERN_DEBUG "kcapi: notify up contr %d\n", contr);
190 
191 	ctr = get_capi_ctr_by_nr(contr);
192 	if (ctr) {
193 		if (ctr->state == CAPI_CTR_RUNNING)
194 			goto unlock_out;
195 
196 		ctr->state = CAPI_CTR_RUNNING;
197 
198 		for (applid = 1; applid <= CAPI_MAXAPPL; applid++) {
199 			ap = __get_capi_appl_by_nr(applid);
200 			if (ap)
201 				register_appl(ctr, applid, &ap->rparam);
202 		}
203 
204 		wake_up_interruptible_all(&ctr->state_wait_queue);
205 	} else
206 		printk(KERN_WARNING "%s: invalid contr %d\n", __func__, contr);
207 
208 unlock_out:
209 	mutex_unlock(&capi_controller_lock);
210 }
211 
ctr_down(struct capi_ctr * ctr,int new_state)212 static void ctr_down(struct capi_ctr *ctr, int new_state)
213 {
214 	struct capi20_appl *ap;
215 	u16 applid;
216 
217 	if (ctr->state == CAPI_CTR_DETECTED || ctr->state == CAPI_CTR_DETACHED)
218 		return;
219 
220 	ctr->state = new_state;
221 
222 	memset(ctr->manu, 0, sizeof(ctr->manu));
223 	memset(&ctr->version, 0, sizeof(ctr->version));
224 	memset(&ctr->profile, 0, sizeof(ctr->profile));
225 	memset(ctr->serial, 0, sizeof(ctr->serial));
226 
227 	for (applid = 1; applid <= CAPI_MAXAPPL; applid++) {
228 		ap = __get_capi_appl_by_nr(applid);
229 		if (ap)
230 			capi_ctr_put(ctr);
231 	}
232 
233 	wake_up_interruptible_all(&ctr->state_wait_queue);
234 }
235 
notify_down(u32 contr)236 static void notify_down(u32 contr)
237 {
238 	struct capi_ctr *ctr;
239 
240 	mutex_lock(&capi_controller_lock);
241 
242 	if (showcapimsgs & 1)
243 		printk(KERN_DEBUG "kcapi: notify down contr %d\n", contr);
244 
245 	ctr = get_capi_ctr_by_nr(contr);
246 	if (ctr)
247 		ctr_down(ctr, CAPI_CTR_DETECTED);
248 	else
249 		printk(KERN_WARNING "%s: invalid contr %d\n", __func__, contr);
250 
251 	mutex_unlock(&capi_controller_lock);
252 }
253 
254 static int
notify_handler(struct notifier_block * nb,unsigned long val,void * v)255 notify_handler(struct notifier_block *nb, unsigned long val, void *v)
256 {
257 	u32 contr = (long)v;
258 
259 	switch (val) {
260 	case CAPICTR_UP:
261 		notify_up(contr);
262 		break;
263 	case CAPICTR_DOWN:
264 		notify_down(contr);
265 		break;
266 	}
267 	return NOTIFY_OK;
268 }
269 
do_notify_work(struct work_struct * work)270 static void do_notify_work(struct work_struct *work)
271 {
272 	struct capictr_event *event =
273 		container_of(work, struct capictr_event, work);
274 
275 	blocking_notifier_call_chain(&ctr_notifier_list, event->type,
276 				     (void *)(long)event->controller);
277 	kfree(event);
278 }
279 
280 /*
281  * The notifier will result in adding/deleteing of devices. Devices can
282  * only removed in user process, not in bh.
283  */
notify_push(unsigned int event_type,u32 controller)284 static int notify_push(unsigned int event_type, u32 controller)
285 {
286 	struct capictr_event *event = kmalloc(sizeof(*event), GFP_ATOMIC);
287 
288 	if (!event)
289 		return -ENOMEM;
290 
291 	INIT_WORK(&event->work, do_notify_work);
292 	event->type = event_type;
293 	event->controller = controller;
294 
295 	queue_work(kcapi_wq, &event->work);
296 	return 0;
297 }
298 
register_capictr_notifier(struct notifier_block * nb)299 int register_capictr_notifier(struct notifier_block *nb)
300 {
301 	return blocking_notifier_chain_register(&ctr_notifier_list, nb);
302 }
303 EXPORT_SYMBOL_GPL(register_capictr_notifier);
304 
unregister_capictr_notifier(struct notifier_block * nb)305 int unregister_capictr_notifier(struct notifier_block *nb)
306 {
307 	return blocking_notifier_chain_unregister(&ctr_notifier_list, nb);
308 }
309 EXPORT_SYMBOL_GPL(unregister_capictr_notifier);
310 
311 /* -------- Receiver ------------------------------------------ */
312 
recv_handler(struct work_struct * work)313 static void recv_handler(struct work_struct *work)
314 {
315 	struct sk_buff *skb;
316 	struct capi20_appl *ap =
317 		container_of(work, struct capi20_appl, recv_work);
318 
319 	if ((!ap) || (ap->release_in_progress))
320 		return;
321 
322 	mutex_lock(&ap->recv_mtx);
323 	while ((skb = skb_dequeue(&ap->recv_queue))) {
324 		if (CAPIMSG_CMD(skb->data) == CAPI_DATA_B3_IND)
325 			ap->nrecvdatapkt++;
326 		else
327 			ap->nrecvctlpkt++;
328 
329 		ap->recv_message(ap, skb);
330 	}
331 	mutex_unlock(&ap->recv_mtx);
332 }
333 
334 /**
335  * capi_ctr_handle_message() - handle incoming CAPI message
336  * @ctr:	controller descriptor structure.
337  * @appl:	application ID.
338  * @skb:	message.
339  *
340  * Called by hardware driver to pass a CAPI message to the application.
341  */
342 
capi_ctr_handle_message(struct capi_ctr * ctr,u16 appl,struct sk_buff * skb)343 void capi_ctr_handle_message(struct capi_ctr *ctr, u16 appl,
344 			     struct sk_buff *skb)
345 {
346 	struct capi20_appl *ap;
347 	int showctl = 0;
348 	u8 cmd, subcmd;
349 	_cdebbuf *cdb;
350 
351 	if (ctr->state != CAPI_CTR_RUNNING) {
352 		cdb = capi_message2str(skb->data);
353 		if (cdb) {
354 			printk(KERN_INFO "kcapi: controller [%03d] not active, got: %s",
355 			       ctr->cnr, cdb->buf);
356 			cdebbuf_free(cdb);
357 		} else
358 			printk(KERN_INFO "kcapi: controller [%03d] not active, cannot trace\n",
359 			       ctr->cnr);
360 		goto error;
361 	}
362 
363 	cmd = CAPIMSG_COMMAND(skb->data);
364 	subcmd = CAPIMSG_SUBCOMMAND(skb->data);
365 	if (cmd == CAPI_DATA_B3 && subcmd == CAPI_IND) {
366 		ctr->nrecvdatapkt++;
367 		if (ctr->traceflag > 2)
368 			showctl |= 2;
369 	} else {
370 		ctr->nrecvctlpkt++;
371 		if (ctr->traceflag)
372 			showctl |= 2;
373 	}
374 	showctl |= (ctr->traceflag & 1);
375 	if (showctl & 2) {
376 		if (showctl & 1) {
377 			printk(KERN_DEBUG "kcapi: got [%03d] id#%d %s len=%u\n",
378 			       ctr->cnr, CAPIMSG_APPID(skb->data),
379 			       capi_cmd2str(cmd, subcmd),
380 			       CAPIMSG_LEN(skb->data));
381 		} else {
382 			cdb = capi_message2str(skb->data);
383 			if (cdb) {
384 				printk(KERN_DEBUG "kcapi: got [%03d] %s\n",
385 				       ctr->cnr, cdb->buf);
386 				cdebbuf_free(cdb);
387 			} else
388 				printk(KERN_DEBUG "kcapi: got [%03d] id#%d %s len=%u, cannot trace\n",
389 				       ctr->cnr, CAPIMSG_APPID(skb->data),
390 				       capi_cmd2str(cmd, subcmd),
391 				       CAPIMSG_LEN(skb->data));
392 		}
393 
394 	}
395 
396 	rcu_read_lock();
397 	ap = get_capi_appl_by_nr(CAPIMSG_APPID(skb->data));
398 	if (!ap) {
399 		rcu_read_unlock();
400 		cdb = capi_message2str(skb->data);
401 		if (cdb) {
402 			printk(KERN_ERR "kcapi: handle_message: applid %d state released (%s)\n",
403 			       CAPIMSG_APPID(skb->data), cdb->buf);
404 			cdebbuf_free(cdb);
405 		} else
406 			printk(KERN_ERR "kcapi: handle_message: applid %d state released (%s) cannot trace\n",
407 			       CAPIMSG_APPID(skb->data),
408 			       capi_cmd2str(cmd, subcmd));
409 		goto error;
410 	}
411 	skb_queue_tail(&ap->recv_queue, skb);
412 	queue_work(kcapi_wq, &ap->recv_work);
413 	rcu_read_unlock();
414 
415 	return;
416 
417 error:
418 	kfree_skb(skb);
419 }
420 
421 EXPORT_SYMBOL(capi_ctr_handle_message);
422 
423 /**
424  * capi_ctr_ready() - signal CAPI controller ready
425  * @ctr:	controller descriptor structure.
426  *
427  * Called by hardware driver to signal that the controller is up and running.
428  */
429 
capi_ctr_ready(struct capi_ctr * ctr)430 void capi_ctr_ready(struct capi_ctr *ctr)
431 {
432 	printk(KERN_NOTICE "kcapi: controller [%03d] \"%s\" ready.\n",
433 	       ctr->cnr, ctr->name);
434 
435 	notify_push(CAPICTR_UP, ctr->cnr);
436 }
437 
438 EXPORT_SYMBOL(capi_ctr_ready);
439 
440 /**
441  * capi_ctr_down() - signal CAPI controller not ready
442  * @ctr:	controller descriptor structure.
443  *
444  * Called by hardware driver to signal that the controller is down and
445  * unavailable for use.
446  */
447 
capi_ctr_down(struct capi_ctr * ctr)448 void capi_ctr_down(struct capi_ctr *ctr)
449 {
450 	printk(KERN_NOTICE "kcapi: controller [%03d] down.\n", ctr->cnr);
451 
452 	notify_push(CAPICTR_DOWN, ctr->cnr);
453 }
454 
455 EXPORT_SYMBOL(capi_ctr_down);
456 
457 /**
458  * capi_ctr_suspend_output() - suspend controller
459  * @ctr:	controller descriptor structure.
460  *
461  * Called by hardware driver to stop data flow.
462  *
463  * Note: The caller is responsible for synchronizing concurrent state changes
464  * as well as invocations of capi_ctr_handle_message.
465  */
466 
capi_ctr_suspend_output(struct capi_ctr * ctr)467 void capi_ctr_suspend_output(struct capi_ctr *ctr)
468 {
469 	if (!ctr->blocked) {
470 		printk(KERN_DEBUG "kcapi: controller [%03d] suspend\n",
471 		       ctr->cnr);
472 		ctr->blocked = 1;
473 	}
474 }
475 
476 EXPORT_SYMBOL(capi_ctr_suspend_output);
477 
478 /**
479  * capi_ctr_resume_output() - resume controller
480  * @ctr:	controller descriptor structure.
481  *
482  * Called by hardware driver to resume data flow.
483  *
484  * Note: The caller is responsible for synchronizing concurrent state changes
485  * as well as invocations of capi_ctr_handle_message.
486  */
487 
capi_ctr_resume_output(struct capi_ctr * ctr)488 void capi_ctr_resume_output(struct capi_ctr *ctr)
489 {
490 	if (ctr->blocked) {
491 		printk(KERN_DEBUG "kcapi: controller [%03d] resumed\n",
492 		       ctr->cnr);
493 		ctr->blocked = 0;
494 	}
495 }
496 
497 EXPORT_SYMBOL(capi_ctr_resume_output);
498 
499 /* ------------------------------------------------------------- */
500 
501 /**
502  * attach_capi_ctr() - register CAPI controller
503  * @ctr:	controller descriptor structure.
504  *
505  * Called by hardware driver to register a controller with the CAPI subsystem.
506  * Return value: 0 on success, error code < 0 on error
507  */
508 
attach_capi_ctr(struct capi_ctr * ctr)509 int attach_capi_ctr(struct capi_ctr *ctr)
510 {
511 	int i;
512 
513 	mutex_lock(&capi_controller_lock);
514 
515 	for (i = 0; i < CAPI_MAXCONTR; i++) {
516 		if (!capi_controller[i])
517 			break;
518 	}
519 	if (i == CAPI_MAXCONTR) {
520 		mutex_unlock(&capi_controller_lock);
521 		printk(KERN_ERR "kcapi: out of controller slots\n");
522 		return -EBUSY;
523 	}
524 	capi_controller[i] = ctr;
525 
526 	ctr->nrecvctlpkt = 0;
527 	ctr->nrecvdatapkt = 0;
528 	ctr->nsentctlpkt = 0;
529 	ctr->nsentdatapkt = 0;
530 	ctr->cnr = i + 1;
531 	ctr->state = CAPI_CTR_DETECTED;
532 	ctr->blocked = 0;
533 	ctr->traceflag = showcapimsgs;
534 	init_waitqueue_head(&ctr->state_wait_queue);
535 
536 	sprintf(ctr->procfn, "capi/controllers/%d", ctr->cnr);
537 	ctr->procent = proc_create_single_data(ctr->procfn, 0, NULL,
538 			ctr->proc_show, ctr);
539 
540 	ncontrollers++;
541 
542 	mutex_unlock(&capi_controller_lock);
543 
544 	printk(KERN_NOTICE "kcapi: controller [%03d]: %s attached\n",
545 	       ctr->cnr, ctr->name);
546 	return 0;
547 }
548 
549 EXPORT_SYMBOL(attach_capi_ctr);
550 
551 /**
552  * detach_capi_ctr() - unregister CAPI controller
553  * @ctr:	controller descriptor structure.
554  *
555  * Called by hardware driver to remove the registration of a controller
556  * with the CAPI subsystem.
557  * Return value: 0 on success, error code < 0 on error
558  */
559 
detach_capi_ctr(struct capi_ctr * ctr)560 int detach_capi_ctr(struct capi_ctr *ctr)
561 {
562 	int err = 0;
563 
564 	mutex_lock(&capi_controller_lock);
565 
566 	ctr_down(ctr, CAPI_CTR_DETACHED);
567 
568 	if (ctr->cnr < 1 || ctr->cnr - 1 >= CAPI_MAXCONTR) {
569 		err = -EINVAL;
570 		goto unlock_out;
571 	}
572 
573 	if (capi_controller[ctr->cnr - 1] != ctr) {
574 		err = -EINVAL;
575 		goto unlock_out;
576 	}
577 	capi_controller[ctr->cnr - 1] = NULL;
578 	ncontrollers--;
579 
580 	if (ctr->procent)
581 		remove_proc_entry(ctr->procfn, NULL);
582 
583 	printk(KERN_NOTICE "kcapi: controller [%03d]: %s unregistered\n",
584 	       ctr->cnr, ctr->name);
585 
586 unlock_out:
587 	mutex_unlock(&capi_controller_lock);
588 
589 	return err;
590 }
591 
592 EXPORT_SYMBOL(detach_capi_ctr);
593 
594 /**
595  * register_capi_driver() - register CAPI driver
596  * @driver:	driver descriptor structure.
597  *
598  * Called by hardware driver to register itself with the CAPI subsystem.
599  */
600 
register_capi_driver(struct capi_driver * driver)601 void register_capi_driver(struct capi_driver *driver)
602 {
603 	mutex_lock(&capi_drivers_lock);
604 	list_add_tail(&driver->list, &capi_drivers);
605 	mutex_unlock(&capi_drivers_lock);
606 }
607 
608 EXPORT_SYMBOL(register_capi_driver);
609 
610 /**
611  * unregister_capi_driver() - unregister CAPI driver
612  * @driver:	driver descriptor structure.
613  *
614  * Called by hardware driver to unregister itself from the CAPI subsystem.
615  */
616 
unregister_capi_driver(struct capi_driver * driver)617 void unregister_capi_driver(struct capi_driver *driver)
618 {
619 	mutex_lock(&capi_drivers_lock);
620 	list_del(&driver->list);
621 	mutex_unlock(&capi_drivers_lock);
622 }
623 
624 EXPORT_SYMBOL(unregister_capi_driver);
625 
626 /* ------------------------------------------------------------- */
627 /* -------- CAPI2.0 Interface ---------------------------------- */
628 /* ------------------------------------------------------------- */
629 
630 /**
631  * capi20_isinstalled() - CAPI 2.0 operation CAPI_INSTALLED
632  *
633  * Return value: CAPI result code (CAPI_NOERROR if at least one ISDN controller
634  *	is ready for use, CAPI_REGNOTINSTALLED otherwise)
635  */
636 
capi20_isinstalled(void)637 u16 capi20_isinstalled(void)
638 {
639 	u16 ret = CAPI_REGNOTINSTALLED;
640 	int i;
641 
642 	mutex_lock(&capi_controller_lock);
643 
644 	for (i = 0; i < CAPI_MAXCONTR; i++)
645 		if (capi_controller[i] &&
646 		    capi_controller[i]->state == CAPI_CTR_RUNNING) {
647 			ret = CAPI_NOERROR;
648 			break;
649 		}
650 
651 	mutex_unlock(&capi_controller_lock);
652 
653 	return ret;
654 }
655 
656 EXPORT_SYMBOL(capi20_isinstalled);
657 
658 /**
659  * capi20_register() - CAPI 2.0 operation CAPI_REGISTER
660  * @ap:		CAPI application descriptor structure.
661  *
662  * Register an application's presence with CAPI.
663  * A unique application ID is assigned and stored in @ap->applid.
664  * After this function returns successfully, the message receive
665  * callback function @ap->recv_message() may be called at any time
666  * until capi20_release() has been called for the same @ap.
667  * Return value: CAPI result code
668  */
669 
capi20_register(struct capi20_appl * ap)670 u16 capi20_register(struct capi20_appl *ap)
671 {
672 	int i;
673 	u16 applid;
674 
675 	DBG("");
676 
677 	if (ap->rparam.datablklen < 128)
678 		return CAPI_LOGBLKSIZETOSMALL;
679 
680 	ap->nrecvctlpkt = 0;
681 	ap->nrecvdatapkt = 0;
682 	ap->nsentctlpkt = 0;
683 	ap->nsentdatapkt = 0;
684 	mutex_init(&ap->recv_mtx);
685 	skb_queue_head_init(&ap->recv_queue);
686 	INIT_WORK(&ap->recv_work, recv_handler);
687 	ap->release_in_progress = 0;
688 
689 	mutex_lock(&capi_controller_lock);
690 
691 	for (applid = 1; applid <= CAPI_MAXAPPL; applid++) {
692 		if (capi_applications[applid - 1] == NULL)
693 			break;
694 	}
695 	if (applid > CAPI_MAXAPPL) {
696 		mutex_unlock(&capi_controller_lock);
697 		return CAPI_TOOMANYAPPLS;
698 	}
699 
700 	ap->applid = applid;
701 	capi_applications[applid - 1] = ap;
702 
703 	for (i = 0; i < CAPI_MAXCONTR; i++) {
704 		if (!capi_controller[i] ||
705 		    capi_controller[i]->state != CAPI_CTR_RUNNING)
706 			continue;
707 		register_appl(capi_controller[i], applid, &ap->rparam);
708 	}
709 
710 	mutex_unlock(&capi_controller_lock);
711 
712 	if (showcapimsgs & 1) {
713 		printk(KERN_DEBUG "kcapi: appl %d up\n", applid);
714 	}
715 
716 	return CAPI_NOERROR;
717 }
718 
719 EXPORT_SYMBOL(capi20_register);
720 
721 /**
722  * capi20_release() - CAPI 2.0 operation CAPI_RELEASE
723  * @ap:		CAPI application descriptor structure.
724  *
725  * Terminate an application's registration with CAPI.
726  * After this function returns successfully, the message receive
727  * callback function @ap->recv_message() will no longer be called.
728  * Return value: CAPI result code
729  */
730 
capi20_release(struct capi20_appl * ap)731 u16 capi20_release(struct capi20_appl *ap)
732 {
733 	int i;
734 
735 	DBG("applid %#x", ap->applid);
736 
737 	mutex_lock(&capi_controller_lock);
738 
739 	ap->release_in_progress = 1;
740 	capi_applications[ap->applid - 1] = NULL;
741 
742 	synchronize_rcu();
743 
744 	for (i = 0; i < CAPI_MAXCONTR; i++) {
745 		if (!capi_controller[i] ||
746 		    capi_controller[i]->state != CAPI_CTR_RUNNING)
747 			continue;
748 		release_appl(capi_controller[i], ap->applid);
749 	}
750 
751 	mutex_unlock(&capi_controller_lock);
752 
753 	flush_workqueue(kcapi_wq);
754 	skb_queue_purge(&ap->recv_queue);
755 
756 	if (showcapimsgs & 1) {
757 		printk(KERN_DEBUG "kcapi: appl %d down\n", ap->applid);
758 	}
759 
760 	return CAPI_NOERROR;
761 }
762 
763 EXPORT_SYMBOL(capi20_release);
764 
765 /**
766  * capi20_put_message() - CAPI 2.0 operation CAPI_PUT_MESSAGE
767  * @ap:		CAPI application descriptor structure.
768  * @skb:	CAPI message.
769  *
770  * Transfer a single message to CAPI.
771  * Return value: CAPI result code
772  */
773 
capi20_put_message(struct capi20_appl * ap,struct sk_buff * skb)774 u16 capi20_put_message(struct capi20_appl *ap, struct sk_buff *skb)
775 {
776 	struct capi_ctr *ctr;
777 	int showctl = 0;
778 	u8 cmd, subcmd;
779 
780 	DBG("applid %#x", ap->applid);
781 
782 	if (ncontrollers == 0)
783 		return CAPI_REGNOTINSTALLED;
784 	if ((ap->applid == 0) || ap->release_in_progress)
785 		return CAPI_ILLAPPNR;
786 	if (skb->len < 12
787 	    || !capi_cmd_valid(CAPIMSG_COMMAND(skb->data))
788 	    || !capi_subcmd_valid(CAPIMSG_SUBCOMMAND(skb->data)))
789 		return CAPI_ILLCMDORSUBCMDORMSGTOSMALL;
790 
791 	/*
792 	 * The controller reference is protected by the existence of the
793 	 * application passed to us. We assume that the caller properly
794 	 * synchronizes this service with capi20_release.
795 	 */
796 	ctr = get_capi_ctr_by_nr(CAPIMSG_CONTROLLER(skb->data));
797 	if (!ctr || ctr->state != CAPI_CTR_RUNNING)
798 		return CAPI_REGNOTINSTALLED;
799 	if (ctr->blocked)
800 		return CAPI_SENDQUEUEFULL;
801 
802 	cmd = CAPIMSG_COMMAND(skb->data);
803 	subcmd = CAPIMSG_SUBCOMMAND(skb->data);
804 
805 	if (cmd == CAPI_DATA_B3 && subcmd == CAPI_REQ) {
806 		ctr->nsentdatapkt++;
807 		ap->nsentdatapkt++;
808 		if (ctr->traceflag > 2)
809 			showctl |= 2;
810 	} else {
811 		ctr->nsentctlpkt++;
812 		ap->nsentctlpkt++;
813 		if (ctr->traceflag)
814 			showctl |= 2;
815 	}
816 	showctl |= (ctr->traceflag & 1);
817 	if (showctl & 2) {
818 		if (showctl & 1) {
819 			printk(KERN_DEBUG "kcapi: put [%03d] id#%d %s len=%u\n",
820 			       CAPIMSG_CONTROLLER(skb->data),
821 			       CAPIMSG_APPID(skb->data),
822 			       capi_cmd2str(cmd, subcmd),
823 			       CAPIMSG_LEN(skb->data));
824 		} else {
825 			_cdebbuf *cdb = capi_message2str(skb->data);
826 			if (cdb) {
827 				printk(KERN_DEBUG "kcapi: put [%03d] %s\n",
828 				       CAPIMSG_CONTROLLER(skb->data),
829 				       cdb->buf);
830 				cdebbuf_free(cdb);
831 			} else
832 				printk(KERN_DEBUG "kcapi: put [%03d] id#%d %s len=%u cannot trace\n",
833 				       CAPIMSG_CONTROLLER(skb->data),
834 				       CAPIMSG_APPID(skb->data),
835 				       capi_cmd2str(cmd, subcmd),
836 				       CAPIMSG_LEN(skb->data));
837 		}
838 	}
839 	return ctr->send_message(ctr, skb);
840 }
841 
842 EXPORT_SYMBOL(capi20_put_message);
843 
844 /**
845  * capi20_get_manufacturer() - CAPI 2.0 operation CAPI_GET_MANUFACTURER
846  * @contr:	controller number.
847  * @buf:	result buffer (64 bytes).
848  *
849  * Retrieve information about the manufacturer of the specified ISDN controller
850  * or (for @contr == 0) the driver itself.
851  * Return value: CAPI result code
852  */
853 
capi20_get_manufacturer(u32 contr,u8 buf[CAPI_MANUFACTURER_LEN])854 u16 capi20_get_manufacturer(u32 contr, u8 buf[CAPI_MANUFACTURER_LEN])
855 {
856 	struct capi_ctr *ctr;
857 	u16 ret;
858 
859 	if (contr == 0) {
860 		strncpy(buf, capi_manufakturer, CAPI_MANUFACTURER_LEN);
861 		return CAPI_NOERROR;
862 	}
863 
864 	mutex_lock(&capi_controller_lock);
865 
866 	ctr = get_capi_ctr_by_nr(contr);
867 	if (ctr && ctr->state == CAPI_CTR_RUNNING) {
868 		strncpy(buf, ctr->manu, CAPI_MANUFACTURER_LEN);
869 		ret = CAPI_NOERROR;
870 	} else
871 		ret = CAPI_REGNOTINSTALLED;
872 
873 	mutex_unlock(&capi_controller_lock);
874 	return ret;
875 }
876 
877 EXPORT_SYMBOL(capi20_get_manufacturer);
878 
879 /**
880  * capi20_get_version() - CAPI 2.0 operation CAPI_GET_VERSION
881  * @contr:	controller number.
882  * @verp:	result structure.
883  *
884  * Retrieve version information for the specified ISDN controller
885  * or (for @contr == 0) the driver itself.
886  * Return value: CAPI result code
887  */
888 
capi20_get_version(u32 contr,struct capi_version * verp)889 u16 capi20_get_version(u32 contr, struct capi_version *verp)
890 {
891 	struct capi_ctr *ctr;
892 	u16 ret;
893 
894 	if (contr == 0) {
895 		*verp = driver_version;
896 		return CAPI_NOERROR;
897 	}
898 
899 	mutex_lock(&capi_controller_lock);
900 
901 	ctr = get_capi_ctr_by_nr(contr);
902 	if (ctr && ctr->state == CAPI_CTR_RUNNING) {
903 		memcpy(verp, &ctr->version, sizeof(capi_version));
904 		ret = CAPI_NOERROR;
905 	} else
906 		ret = CAPI_REGNOTINSTALLED;
907 
908 	mutex_unlock(&capi_controller_lock);
909 	return ret;
910 }
911 
912 EXPORT_SYMBOL(capi20_get_version);
913 
914 /**
915  * capi20_get_serial() - CAPI 2.0 operation CAPI_GET_SERIAL_NUMBER
916  * @contr:	controller number.
917  * @serial:	result buffer (8 bytes).
918  *
919  * Retrieve the serial number of the specified ISDN controller
920  * or (for @contr == 0) the driver itself.
921  * Return value: CAPI result code
922  */
923 
capi20_get_serial(u32 contr,u8 serial[CAPI_SERIAL_LEN])924 u16 capi20_get_serial(u32 contr, u8 serial[CAPI_SERIAL_LEN])
925 {
926 	struct capi_ctr *ctr;
927 	u16 ret;
928 
929 	if (contr == 0) {
930 		strlcpy(serial, driver_serial, CAPI_SERIAL_LEN);
931 		return CAPI_NOERROR;
932 	}
933 
934 	mutex_lock(&capi_controller_lock);
935 
936 	ctr = get_capi_ctr_by_nr(contr);
937 	if (ctr && ctr->state == CAPI_CTR_RUNNING) {
938 		strlcpy(serial, ctr->serial, CAPI_SERIAL_LEN);
939 		ret = CAPI_NOERROR;
940 	} else
941 		ret = CAPI_REGNOTINSTALLED;
942 
943 	mutex_unlock(&capi_controller_lock);
944 	return ret;
945 }
946 
947 EXPORT_SYMBOL(capi20_get_serial);
948 
949 /**
950  * capi20_get_profile() - CAPI 2.0 operation CAPI_GET_PROFILE
951  * @contr:	controller number.
952  * @profp:	result structure.
953  *
954  * Retrieve capability information for the specified ISDN controller
955  * or (for @contr == 0) the number of installed controllers.
956  * Return value: CAPI result code
957  */
958 
capi20_get_profile(u32 contr,struct capi_profile * profp)959 u16 capi20_get_profile(u32 contr, struct capi_profile *profp)
960 {
961 	struct capi_ctr *ctr;
962 	u16 ret;
963 
964 	if (contr == 0) {
965 		profp->ncontroller = ncontrollers;
966 		return CAPI_NOERROR;
967 	}
968 
969 	mutex_lock(&capi_controller_lock);
970 
971 	ctr = get_capi_ctr_by_nr(contr);
972 	if (ctr && ctr->state == CAPI_CTR_RUNNING) {
973 		memcpy(profp, &ctr->profile, sizeof(struct capi_profile));
974 		ret = CAPI_NOERROR;
975 	} else
976 		ret = CAPI_REGNOTINSTALLED;
977 
978 	mutex_unlock(&capi_controller_lock);
979 	return ret;
980 }
981 
982 EXPORT_SYMBOL(capi20_get_profile);
983 
984 /* Must be called with capi_controller_lock held. */
wait_on_ctr_state(struct capi_ctr * ctr,unsigned int state)985 static int wait_on_ctr_state(struct capi_ctr *ctr, unsigned int state)
986 {
987 	DEFINE_WAIT(wait);
988 	int retval = 0;
989 
990 	ctr = capi_ctr_get(ctr);
991 	if (!ctr)
992 		return -ESRCH;
993 
994 	for (;;) {
995 		prepare_to_wait(&ctr->state_wait_queue, &wait,
996 				TASK_INTERRUPTIBLE);
997 
998 		if (ctr->state == state)
999 			break;
1000 		if (ctr->state == CAPI_CTR_DETACHED) {
1001 			retval = -ESRCH;
1002 			break;
1003 		}
1004 		if (signal_pending(current)) {
1005 			retval = -EINTR;
1006 			break;
1007 		}
1008 
1009 		mutex_unlock(&capi_controller_lock);
1010 		schedule();
1011 		mutex_lock(&capi_controller_lock);
1012 	}
1013 	finish_wait(&ctr->state_wait_queue, &wait);
1014 
1015 	capi_ctr_put(ctr);
1016 
1017 	return retval;
1018 }
1019 
1020 #ifdef AVMB1_COMPAT
old_capi_manufacturer(unsigned int cmd,void __user * data)1021 static int old_capi_manufacturer(unsigned int cmd, void __user *data)
1022 {
1023 	avmb1_loadandconfigdef ldef;
1024 	avmb1_extcarddef cdef;
1025 	avmb1_resetdef rdef;
1026 	capicardparams cparams;
1027 	struct capi_ctr *ctr;
1028 	struct capi_driver *driver = NULL;
1029 	capiloaddata ldata;
1030 	struct list_head *l;
1031 	int retval;
1032 
1033 	switch (cmd) {
1034 	case AVMB1_ADDCARD:
1035 	case AVMB1_ADDCARD_WITH_TYPE:
1036 		if (cmd == AVMB1_ADDCARD) {
1037 			if ((retval = copy_from_user(&cdef, data,
1038 						     sizeof(avmb1_carddef))))
1039 				return -EFAULT;
1040 			cdef.cardtype = AVM_CARDTYPE_B1;
1041 			cdef.cardnr = 0;
1042 		} else {
1043 			if ((retval = copy_from_user(&cdef, data,
1044 						     sizeof(avmb1_extcarddef))))
1045 				return -EFAULT;
1046 		}
1047 		cparams.port = cdef.port;
1048 		cparams.irq = cdef.irq;
1049 		cparams.cardnr = cdef.cardnr;
1050 
1051 		mutex_lock(&capi_drivers_lock);
1052 
1053 		switch (cdef.cardtype) {
1054 		case AVM_CARDTYPE_B1:
1055 			list_for_each(l, &capi_drivers) {
1056 				driver = list_entry(l, struct capi_driver, list);
1057 				if (strcmp(driver->name, "b1isa") == 0)
1058 					break;
1059 			}
1060 			break;
1061 		case AVM_CARDTYPE_T1:
1062 			list_for_each(l, &capi_drivers) {
1063 				driver = list_entry(l, struct capi_driver, list);
1064 				if (strcmp(driver->name, "t1isa") == 0)
1065 					break;
1066 			}
1067 			break;
1068 		default:
1069 			driver = NULL;
1070 			break;
1071 		}
1072 		if (!driver) {
1073 			printk(KERN_ERR "kcapi: driver not loaded.\n");
1074 			retval = -EIO;
1075 		} else if (!driver->add_card) {
1076 			printk(KERN_ERR "kcapi: driver has no add card function.\n");
1077 			retval = -EIO;
1078 		} else
1079 			retval = driver->add_card(driver, &cparams);
1080 
1081 		mutex_unlock(&capi_drivers_lock);
1082 		return retval;
1083 
1084 	case AVMB1_LOAD:
1085 	case AVMB1_LOAD_AND_CONFIG:
1086 
1087 		if (cmd == AVMB1_LOAD) {
1088 			if (copy_from_user(&ldef, data,
1089 					   sizeof(avmb1_loaddef)))
1090 				return -EFAULT;
1091 			ldef.t4config.len = 0;
1092 			ldef.t4config.data = NULL;
1093 		} else {
1094 			if (copy_from_user(&ldef, data,
1095 					   sizeof(avmb1_loadandconfigdef)))
1096 				return -EFAULT;
1097 		}
1098 
1099 		mutex_lock(&capi_controller_lock);
1100 
1101 		ctr = get_capi_ctr_by_nr(ldef.contr);
1102 		if (!ctr) {
1103 			retval = -EINVAL;
1104 			goto load_unlock_out;
1105 		}
1106 
1107 		if (ctr->load_firmware == NULL) {
1108 			printk(KERN_DEBUG "kcapi: load: no load function\n");
1109 			retval = -ESRCH;
1110 			goto load_unlock_out;
1111 		}
1112 
1113 		if (ldef.t4file.len <= 0) {
1114 			printk(KERN_DEBUG "kcapi: load: invalid parameter: length of t4file is %d ?\n", ldef.t4file.len);
1115 			retval = -EINVAL;
1116 			goto load_unlock_out;
1117 		}
1118 		if (ldef.t4file.data == NULL) {
1119 			printk(KERN_DEBUG "kcapi: load: invalid parameter: dataptr is 0\n");
1120 			retval = -EINVAL;
1121 			goto load_unlock_out;
1122 		}
1123 
1124 		ldata.firmware.user = 1;
1125 		ldata.firmware.data = ldef.t4file.data;
1126 		ldata.firmware.len = ldef.t4file.len;
1127 		ldata.configuration.user = 1;
1128 		ldata.configuration.data = ldef.t4config.data;
1129 		ldata.configuration.len = ldef.t4config.len;
1130 
1131 		if (ctr->state != CAPI_CTR_DETECTED) {
1132 			printk(KERN_INFO "kcapi: load: contr=%d not in detect state\n", ldef.contr);
1133 			retval = -EBUSY;
1134 			goto load_unlock_out;
1135 		}
1136 		ctr->state = CAPI_CTR_LOADING;
1137 
1138 		retval = ctr->load_firmware(ctr, &ldata);
1139 		if (retval) {
1140 			ctr->state = CAPI_CTR_DETECTED;
1141 			goto load_unlock_out;
1142 		}
1143 
1144 		retval = wait_on_ctr_state(ctr, CAPI_CTR_RUNNING);
1145 
1146 	load_unlock_out:
1147 		mutex_unlock(&capi_controller_lock);
1148 		return retval;
1149 
1150 	case AVMB1_RESETCARD:
1151 		if (copy_from_user(&rdef, data, sizeof(avmb1_resetdef)))
1152 			return -EFAULT;
1153 
1154 		retval = 0;
1155 
1156 		mutex_lock(&capi_controller_lock);
1157 
1158 		ctr = get_capi_ctr_by_nr(rdef.contr);
1159 		if (!ctr) {
1160 			retval = -ESRCH;
1161 			goto reset_unlock_out;
1162 		}
1163 
1164 		if (ctr->state == CAPI_CTR_DETECTED)
1165 			goto reset_unlock_out;
1166 
1167 		if (ctr->reset_ctr == NULL) {
1168 			printk(KERN_DEBUG "kcapi: reset: no reset function\n");
1169 			retval = -ESRCH;
1170 			goto reset_unlock_out;
1171 		}
1172 
1173 		ctr->reset_ctr(ctr);
1174 
1175 		retval = wait_on_ctr_state(ctr, CAPI_CTR_DETECTED);
1176 
1177 	reset_unlock_out:
1178 		mutex_unlock(&capi_controller_lock);
1179 		return retval;
1180 	}
1181 	return -EINVAL;
1182 }
1183 #endif
1184 
1185 /**
1186  * capi20_manufacturer() - CAPI 2.0 operation CAPI_MANUFACTURER
1187  * @cmd:	command.
1188  * @data:	parameter.
1189  *
1190  * Perform manufacturer specific command.
1191  * Return value: CAPI result code
1192  */
1193 
capi20_manufacturer(unsigned long cmd,void __user * data)1194 int capi20_manufacturer(unsigned long cmd, void __user *data)
1195 {
1196 	struct capi_ctr *ctr;
1197 	int retval;
1198 
1199 	switch (cmd) {
1200 #ifdef AVMB1_COMPAT
1201 	case AVMB1_LOAD:
1202 	case AVMB1_LOAD_AND_CONFIG:
1203 	case AVMB1_RESETCARD:
1204 	case AVMB1_GET_CARDINFO:
1205 	case AVMB1_REMOVECARD:
1206 		return old_capi_manufacturer(cmd, data);
1207 #endif
1208 	case KCAPI_CMD_TRACE:
1209 	{
1210 		kcapi_flagdef fdef;
1211 
1212 		if (copy_from_user(&fdef, data, sizeof(kcapi_flagdef)))
1213 			return -EFAULT;
1214 
1215 		mutex_lock(&capi_controller_lock);
1216 
1217 		ctr = get_capi_ctr_by_nr(fdef.contr);
1218 		if (ctr) {
1219 			ctr->traceflag = fdef.flag;
1220 			printk(KERN_INFO "kcapi: contr [%03d] set trace=%d\n",
1221 			       ctr->cnr, ctr->traceflag);
1222 			retval = 0;
1223 		} else
1224 			retval = -ESRCH;
1225 
1226 		mutex_unlock(&capi_controller_lock);
1227 
1228 		return retval;
1229 	}
1230 	case KCAPI_CMD_ADDCARD:
1231 	{
1232 		struct list_head *l;
1233 		struct capi_driver *driver = NULL;
1234 		capicardparams cparams;
1235 		kcapi_carddef cdef;
1236 
1237 		if ((retval = copy_from_user(&cdef, data, sizeof(cdef))))
1238 			return -EFAULT;
1239 
1240 		cparams.port = cdef.port;
1241 		cparams.irq = cdef.irq;
1242 		cparams.membase = cdef.membase;
1243 		cparams.cardnr = cdef.cardnr;
1244 		cparams.cardtype = 0;
1245 		cdef.driver[sizeof(cdef.driver) - 1] = 0;
1246 
1247 		mutex_lock(&capi_drivers_lock);
1248 
1249 		list_for_each(l, &capi_drivers) {
1250 			driver = list_entry(l, struct capi_driver, list);
1251 			if (strcmp(driver->name, cdef.driver) == 0)
1252 				break;
1253 		}
1254 		if (driver == NULL) {
1255 			printk(KERN_ERR "kcapi: driver \"%s\" not loaded.\n",
1256 			       cdef.driver);
1257 			retval = -ESRCH;
1258 		} else if (!driver->add_card) {
1259 			printk(KERN_ERR "kcapi: driver \"%s\" has no add card function.\n", cdef.driver);
1260 			retval = -EIO;
1261 		} else
1262 			retval = driver->add_card(driver, &cparams);
1263 
1264 		mutex_unlock(&capi_drivers_lock);
1265 		return retval;
1266 	}
1267 
1268 	default:
1269 		printk(KERN_ERR "kcapi: manufacturer command %lu unknown.\n",
1270 		       cmd);
1271 		break;
1272 
1273 	}
1274 	return -EINVAL;
1275 }
1276 
1277 EXPORT_SYMBOL(capi20_manufacturer);
1278 
1279 /* ------------------------------------------------------------- */
1280 /* -------- Init & Cleanup ------------------------------------- */
1281 /* ------------------------------------------------------------- */
1282 
1283 /*
1284  * init / exit functions
1285  */
1286 
1287 static struct notifier_block capictr_nb = {
1288 	.notifier_call = notify_handler,
1289 	.priority = INT_MAX,
1290 };
1291 
kcapi_init(void)1292 static int __init kcapi_init(void)
1293 {
1294 	int err;
1295 
1296 	kcapi_wq = alloc_workqueue("kcapi", 0, 0);
1297 	if (!kcapi_wq)
1298 		return -ENOMEM;
1299 
1300 	register_capictr_notifier(&capictr_nb);
1301 
1302 	err = cdebug_init();
1303 	if (err) {
1304 		unregister_capictr_notifier(&capictr_nb);
1305 		destroy_workqueue(kcapi_wq);
1306 		return err;
1307 	}
1308 
1309 	kcapi_proc_init();
1310 	return 0;
1311 }
1312 
kcapi_exit(void)1313 static void __exit kcapi_exit(void)
1314 {
1315 	kcapi_proc_exit();
1316 
1317 	unregister_capictr_notifier(&capictr_nb);
1318 	cdebug_exit();
1319 	destroy_workqueue(kcapi_wq);
1320 }
1321 
1322 module_init(kcapi_init);
1323 module_exit(kcapi_exit);
1324