blob: 0dcfbfba9d35f79c7a9b90d2e3967203a99c8cc9 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* $Id: capi.c,v 1.1.2.7 2004/04/28 09:48:59 armin Exp $
2 *
3 * CAPI 2.0 Interface for Linux
4 *
5 * Copyright 1996 by Carsten Paeth <calle@calle.de>
6 *
7 * This software may be used and distributed according to the terms
8 * of the GNU General Public License, incorporated herein by reference.
9 *
10 */
11
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/module.h>
13#include <linux/errno.h>
14#include <linux/kernel.h>
15#include <linux/major.h>
16#include <linux/sched.h>
17#include <linux/slab.h>
18#include <linux/fcntl.h>
19#include <linux/fs.h>
20#include <linux/signal.h>
Matthias Kaehlcke9ea6e5d2007-05-08 00:32:43 -070021#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/mm.h>
Jonathan Corbeta237f3b2008-05-16 14:15:33 -060023#include <linux/smp_lock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/timer.h>
25#include <linux/wait.h>
26#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
27#include <linux/tty.h>
28#ifdef CONFIG_PPP
29#include <linux/netdevice.h>
30#include <linux/ppp_defs.h>
31#include <linux/if_ppp.h>
32#endif /* CONFIG_PPP */
33#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
34#include <linux/skbuff.h>
35#include <linux/proc_fs.h>
36#include <linux/poll.h>
37#include <linux/capi.h>
38#include <linux/kernelcapi.h>
39#include <linux/init.h>
40#include <linux/device.h>
41#include <linux/moduleparam.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/isdn/capiutil.h>
43#include <linux/isdn/capicmd.h>
44#if defined(CONFIG_ISDN_CAPI_CAPIFS) || defined(CONFIG_ISDN_CAPI_CAPIFS_MODULE)
45#include "capifs.h"
46#endif
47
48static char *revision = "$Revision: 1.1.2.7 $";
49
50MODULE_DESCRIPTION("CAPI4Linux: Userspace /dev/capi20 interface");
51MODULE_AUTHOR("Carsten Paeth");
52MODULE_LICENSE("GPL");
53
54#undef _DEBUG_REFCOUNT /* alloc/free and open/close debug */
55#undef _DEBUG_TTYFUNCS /* call to tty_driver */
56#undef _DEBUG_DATAFLOW /* data flow */
57
58/* -------- driver information -------------------------------------- */
59
gregkh@suse.de56b22932005-03-23 10:01:41 -080060static struct class *capi_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Adrian Bunk408b6642005-05-01 08:59:29 -070062static int capi_major = 68; /* allocated */
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
64#define CAPINC_NR_PORTS 32
65#define CAPINC_MAX_PORTS 256
Adrian Bunk408b6642005-05-01 08:59:29 -070066static int capi_ttymajor = 191;
67static int capi_ttyminors = CAPINC_NR_PORTS;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
69
70module_param_named(major, capi_major, uint, 0);
71#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
72module_param_named(ttymajor, capi_ttymajor, uint, 0);
73module_param_named(ttyminors, capi_ttyminors, uint, 0);
74#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
75
76/* -------- defines ------------------------------------------------- */
77
78#define CAPINC_MAX_RECVQUEUE 10
79#define CAPINC_MAX_SENDQUEUE 10
80#define CAPI_MAX_BLKSIZE 2048
81
82/* -------- data structures ----------------------------------------- */
83
84struct capidev;
85struct capincci;
86#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
87struct capiminor;
88
Michael Buesch6aa65472006-06-26 00:25:30 -070089struct datahandle_queue {
90 struct list_head list;
91 u16 datahandle;
92};
93
Linus Torvalds1da177e2005-04-16 15:20:36 -070094struct capiminor {
95 struct list_head list;
96 struct capincci *nccip;
97 unsigned int minor;
98
99 struct capi20_appl *ap;
100 u32 ncci;
101 u16 datahandle;
102 u16 msgid;
103
104 struct tty_struct *tty;
105 int ttyinstop;
106 int ttyoutstop;
107 struct sk_buff *ttyskb;
108 atomic_t ttyopencount;
109
110 struct sk_buff_head inqueue;
111 int inbytes;
112 struct sk_buff_head outqueue;
113 int outbytes;
114
115 /* transmit path */
Michael Buesch6aa65472006-06-26 00:25:30 -0700116 struct list_head ackqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 int nack;
Michael Buesch6aa65472006-06-26 00:25:30 -0700118 spinlock_t ackqlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119};
120#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
121
Michael Buesch053b47f2007-02-12 00:53:26 -0800122/* FIXME: The following lock is a sledgehammer-workaround to a
123 * locking issue with the capiminor (and maybe other) data structure(s).
124 * Access to this data is done in a racy way and crashes the machine with
125 * a FritzCard DSL driver; sooner or later. This is a workaround
126 * which trades scalability vs stability, so it doesn't crash the kernel anymore.
127 * The correct (and scalable) fix for the issue seems to require
128 * an API change to the drivers... . */
129static DEFINE_SPINLOCK(workaround_lock);
130
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131struct capincci {
132 struct capincci *next;
133 u32 ncci;
134 struct capidev *cdev;
135#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
136 struct capiminor *minorp;
137#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
138};
139
140struct capidev {
141 struct list_head list;
142 struct capi20_appl ap;
143 u16 errcode;
144 unsigned userflags;
145
146 struct sk_buff_head recvqueue;
147 wait_queue_head_t recvwait;
148
149 struct capincci *nccis;
150
Matthias Kaehlcke9ea6e5d2007-05-08 00:32:43 -0700151 struct mutex ncci_list_mtx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152};
153
154/* -------- global variables ---------------------------------------- */
155
156static DEFINE_RWLOCK(capidev_list_lock);
157static LIST_HEAD(capidev_list);
158
159#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
160static DEFINE_RWLOCK(capiminor_list_lock);
161static LIST_HEAD(capiminor_list);
162#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
163
164#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
165/* -------- datahandles --------------------------------------------- */
166
167static int capincci_add_ack(struct capiminor *mp, u16 datahandle)
168{
Michael Buesch6aa65472006-06-26 00:25:30 -0700169 struct datahandle_queue *n;
170 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
172 n = kmalloc(sizeof(*n), GFP_ATOMIC);
Michael Buesch6aa65472006-06-26 00:25:30 -0700173 if (unlikely(!n)) {
174 printk(KERN_ERR "capi: alloc datahandle failed\n");
175 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 n->datahandle = datahandle;
Michael Buesch6aa65472006-06-26 00:25:30 -0700178 INIT_LIST_HEAD(&n->list);
179 spin_lock_irqsave(&mp->ackqlock, flags);
180 list_add_tail(&n->list, &mp->ackqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 mp->nack++;
Michael Buesch6aa65472006-06-26 00:25:30 -0700182 spin_unlock_irqrestore(&mp->ackqlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 return 0;
184}
185
186static int capiminor_del_ack(struct capiminor *mp, u16 datahandle)
187{
Michael Buesch6aa65472006-06-26 00:25:30 -0700188 struct datahandle_queue *p, *tmp;
189 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Michael Buesch6aa65472006-06-26 00:25:30 -0700191 spin_lock_irqsave(&mp->ackqlock, flags);
192 list_for_each_entry_safe(p, tmp, &mp->ackqueue, list) {
193 if (p->datahandle == datahandle) {
194 list_del(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 kfree(p);
196 mp->nack--;
Michael Buesch6aa65472006-06-26 00:25:30 -0700197 spin_unlock_irqrestore(&mp->ackqlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 return 0;
199 }
200 }
Michael Buesch6aa65472006-06-26 00:25:30 -0700201 spin_unlock_irqrestore(&mp->ackqlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 return -1;
203}
204
205static void capiminor_del_all_ack(struct capiminor *mp)
206{
Michael Buesch6aa65472006-06-26 00:25:30 -0700207 struct datahandle_queue *p, *tmp;
208 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Michael Buesch6aa65472006-06-26 00:25:30 -0700210 spin_lock_irqsave(&mp->ackqlock, flags);
211 list_for_each_entry_safe(p, tmp, &mp->ackqueue, list) {
212 list_del(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 kfree(p);
214 mp->nack--;
215 }
Michael Buesch6aa65472006-06-26 00:25:30 -0700216 spin_unlock_irqrestore(&mp->ackqlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217}
218
219
220/* -------- struct capiminor ---------------------------------------- */
221
222static struct capiminor *capiminor_alloc(struct capi20_appl *ap, u32 ncci)
223{
224 struct capiminor *mp, *p;
225 unsigned int minor = 0;
226 unsigned long flags;
227
Burman Yan41f96932006-12-08 02:39:35 -0800228 mp = kzalloc(sizeof(*mp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 if (!mp) {
230 printk(KERN_ERR "capi: can't alloc capiminor\n");
231 return NULL;
232 }
233
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 mp->ap = ap;
235 mp->ncci = ncci;
236 mp->msgid = 0;
237 atomic_set(&mp->ttyopencount,0);
Michael Buesch6aa65472006-06-26 00:25:30 -0700238 INIT_LIST_HEAD(&mp->ackqueue);
239 spin_lock_init(&mp->ackqlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
241 skb_queue_head_init(&mp->inqueue);
242 skb_queue_head_init(&mp->outqueue);
243
244 /* Allocate the least unused minor number.
245 */
246 write_lock_irqsave(&capiminor_list_lock, flags);
247 if (list_empty(&capiminor_list))
248 list_add(&mp->list, &capiminor_list);
249 else {
250 list_for_each_entry(p, &capiminor_list, list) {
251 if (p->minor > minor)
252 break;
253 minor++;
254 }
255
256 if (minor < capi_ttyminors) {
257 mp->minor = minor;
258 list_add(&mp->list, p->list.prev);
259 }
260 }
261 write_unlock_irqrestore(&capiminor_list_lock, flags);
262
263 if (!(minor < capi_ttyminors)) {
264 printk(KERN_NOTICE "capi: out of minors\n");
265 kfree(mp);
266 return NULL;
267 }
268
269 return mp;
270}
271
272static void capiminor_free(struct capiminor *mp)
273{
274 unsigned long flags;
275
276 write_lock_irqsave(&capiminor_list_lock, flags);
277 list_del(&mp->list);
278 write_unlock_irqrestore(&capiminor_list_lock, flags);
279
280 if (mp->ttyskb) kfree_skb(mp->ttyskb);
281 mp->ttyskb = NULL;
282 skb_queue_purge(&mp->inqueue);
283 skb_queue_purge(&mp->outqueue);
284 capiminor_del_all_ack(mp);
285 kfree(mp);
286}
287
Adrian Bunk408b6642005-05-01 08:59:29 -0700288static struct capiminor *capiminor_find(unsigned int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289{
290 struct list_head *l;
291 struct capiminor *p = NULL;
292
293 read_lock(&capiminor_list_lock);
294 list_for_each(l, &capiminor_list) {
295 p = list_entry(l, struct capiminor, list);
296 if (p->minor == minor)
297 break;
298 }
299 read_unlock(&capiminor_list_lock);
300 if (l == &capiminor_list)
301 return NULL;
302
303 return p;
304}
305#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
306
307/* -------- struct capincci ----------------------------------------- */
308
309static struct capincci *capincci_alloc(struct capidev *cdev, u32 ncci)
310{
311 struct capincci *np, **pp;
312#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
313 struct capiminor *mp = NULL;
314#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
315
Burman Yan41f96932006-12-08 02:39:35 -0800316 np = kzalloc(sizeof(*np), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 if (!np)
318 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 np->ncci = ncci;
320 np->cdev = cdev;
321#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
322 mp = NULL;
323 if (cdev->userflags & CAPIFLAG_HIGHJACKING)
324 mp = np->minorp = capiminor_alloc(&cdev->ap, ncci);
325 if (mp) {
326 mp->nccip = np;
327#ifdef _DEBUG_REFCOUNT
328 printk(KERN_DEBUG "set mp->nccip\n");
329#endif
330#if defined(CONFIG_ISDN_CAPI_CAPIFS) || defined(CONFIG_ISDN_CAPI_CAPIFS_MODULE)
331 capifs_new_ncci(mp->minor, MKDEV(capi_ttymajor, mp->minor));
332#endif
333 }
334#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
335 for (pp=&cdev->nccis; *pp; pp = &(*pp)->next)
336 ;
337 *pp = np;
338 return np;
339}
340
341static void capincci_free(struct capidev *cdev, u32 ncci)
342{
343 struct capincci *np, **pp;
344#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
345 struct capiminor *mp;
346#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
347
348 pp=&cdev->nccis;
349 while (*pp) {
350 np = *pp;
351 if (ncci == 0xffffffff || np->ncci == ncci) {
352 *pp = (*pp)->next;
353#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
Harvey Harrison2f9e9b62008-04-28 02:14:37 -0700354 if ((mp = np->minorp) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355#if defined(CONFIG_ISDN_CAPI_CAPIFS) || defined(CONFIG_ISDN_CAPI_CAPIFS_MODULE)
356 capifs_free_ncci(mp->minor);
357#endif
358 if (mp->tty) {
359 mp->nccip = NULL;
360#ifdef _DEBUG_REFCOUNT
361 printk(KERN_DEBUG "reset mp->nccip\n");
362#endif
363 tty_hangup(mp->tty);
364 } else {
365 capiminor_free(mp);
366 }
367 }
368#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
369 kfree(np);
Harvey Harrison2f9e9b62008-04-28 02:14:37 -0700370 if (*pp == NULL) return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 } else {
372 pp = &(*pp)->next;
373 }
374 }
375}
376
377static struct capincci *capincci_find(struct capidev *cdev, u32 ncci)
378{
379 struct capincci *p;
380
381 for (p=cdev->nccis; p ; p = p->next) {
382 if (p->ncci == ncci)
383 break;
384 }
385 return p;
386}
387
388/* -------- struct capidev ------------------------------------------ */
389
390static struct capidev *capidev_alloc(void)
391{
392 struct capidev *cdev;
393 unsigned long flags;
394
Burman Yan41f96932006-12-08 02:39:35 -0800395 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 if (!cdev)
397 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
Matthias Kaehlcke9ea6e5d2007-05-08 00:32:43 -0700399 mutex_init(&cdev->ncci_list_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 skb_queue_head_init(&cdev->recvqueue);
401 init_waitqueue_head(&cdev->recvwait);
402 write_lock_irqsave(&capidev_list_lock, flags);
403 list_add_tail(&cdev->list, &capidev_list);
404 write_unlock_irqrestore(&capidev_list_lock, flags);
405 return cdev;
406}
407
408static void capidev_free(struct capidev *cdev)
409{
410 unsigned long flags;
411
412 if (cdev->ap.applid) {
413 capi20_release(&cdev->ap);
414 cdev->ap.applid = 0;
415 }
416 skb_queue_purge(&cdev->recvqueue);
417
Matthias Kaehlcke9ea6e5d2007-05-08 00:32:43 -0700418 mutex_lock(&cdev->ncci_list_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 capincci_free(cdev, 0xffffffff);
Matthias Kaehlcke9ea6e5d2007-05-08 00:32:43 -0700420 mutex_unlock(&cdev->ncci_list_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
422 write_lock_irqsave(&capidev_list_lock, flags);
423 list_del(&cdev->list);
424 write_unlock_irqrestore(&capidev_list_lock, flags);
425 kfree(cdev);
426}
427
428#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
429/* -------- handle data queue --------------------------------------- */
430
431static struct sk_buff *
432gen_data_b3_resp_for(struct capiminor *mp, struct sk_buff *skb)
433{
434 struct sk_buff *nskb;
435 nskb = alloc_skb(CAPI_DATA_B3_RESP_LEN, GFP_ATOMIC);
436 if (nskb) {
437 u16 datahandle = CAPIMSG_U16(skb->data,CAPIMSG_BASELEN+4+4+2);
438 unsigned char *s = skb_put(nskb, CAPI_DATA_B3_RESP_LEN);
439 capimsg_setu16(s, 0, CAPI_DATA_B3_RESP_LEN);
440 capimsg_setu16(s, 2, mp->ap->applid);
441 capimsg_setu8 (s, 4, CAPI_DATA_B3);
442 capimsg_setu8 (s, 5, CAPI_RESP);
443 capimsg_setu16(s, 6, mp->msgid++);
444 capimsg_setu32(s, 8, mp->ncci);
445 capimsg_setu16(s, 12, datahandle);
446 }
447 return nskb;
448}
449
450static int handle_recv_skb(struct capiminor *mp, struct sk_buff *skb)
451{
452 struct sk_buff *nskb;
453 int datalen;
454 u16 errcode, datahandle;
455 struct tty_ldisc *ld;
456
457 datalen = skb->len - CAPIMSG_LEN(skb->data);
458 if (mp->tty == NULL)
459 {
460#ifdef _DEBUG_DATAFLOW
461 printk(KERN_DEBUG "capi: currently no receiver\n");
462#endif
463 return -1;
464 }
465
466 ld = tty_ldisc_ref(mp->tty);
467 if (ld == NULL)
468 return -1;
Alan Coxa352def2008-07-16 21:53:12 +0100469 if (ld->ops->receive_buf == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470#if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
471 printk(KERN_DEBUG "capi: ldisc has no receive_buf function\n");
472#endif
473 goto bad;
474 }
475 if (mp->ttyinstop) {
476#if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
477 printk(KERN_DEBUG "capi: recv tty throttled\n");
478#endif
479 goto bad;
480 }
Alan Cox33f0f882006-01-09 20:54:13 -0800481 if (mp->tty->receive_room < datalen) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482#if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
483 printk(KERN_DEBUG "capi: no room in tty\n");
484#endif
485 goto bad;
486 }
Harvey Harrison2f9e9b62008-04-28 02:14:37 -0700487 if ((nskb = gen_data_b3_resp_for(mp, skb)) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 printk(KERN_ERR "capi: gen_data_b3_resp failed\n");
489 goto bad;
490 }
491 datahandle = CAPIMSG_U16(skb->data,CAPIMSG_BASELEN+4);
492 errcode = capi20_put_message(mp->ap, nskb);
493 if (errcode != CAPI_NOERROR) {
494 printk(KERN_ERR "capi: send DATA_B3_RESP failed=%x\n",
495 errcode);
496 kfree_skb(nskb);
497 goto bad;
498 }
499 (void)skb_pull(skb, CAPIMSG_LEN(skb->data));
500#ifdef _DEBUG_DATAFLOW
501 printk(KERN_DEBUG "capi: DATA_B3_RESP %u len=%d => ldisc\n",
502 datahandle, skb->len);
503#endif
Alan Coxa352def2008-07-16 21:53:12 +0100504 ld->ops->receive_buf(mp->tty, skb->data, NULL, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 kfree_skb(skb);
506 tty_ldisc_deref(ld);
507 return 0;
508bad:
509 tty_ldisc_deref(ld);
510 return -1;
511}
512
513static void handle_minor_recv(struct capiminor *mp)
514{
515 struct sk_buff *skb;
Harvey Harrison2f9e9b62008-04-28 02:14:37 -0700516 while ((skb = skb_dequeue(&mp->inqueue)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 unsigned int len = skb->len;
518 mp->inbytes -= len;
519 if (handle_recv_skb(mp, skb) < 0) {
520 skb_queue_head(&mp->inqueue, skb);
521 mp->inbytes += len;
522 return;
523 }
524 }
525}
526
527static int handle_minor_send(struct capiminor *mp)
528{
529 struct sk_buff *skb;
530 u16 len;
531 int count = 0;
532 u16 errcode;
533 u16 datahandle;
534
535 if (mp->tty && mp->ttyoutstop) {
536#if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
537 printk(KERN_DEBUG "capi: send: tty stopped\n");
538#endif
539 return 0;
540 }
541
Harvey Harrison2f9e9b62008-04-28 02:14:37 -0700542 while ((skb = skb_dequeue(&mp->outqueue)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 datahandle = mp->datahandle;
544 len = (u16)skb->len;
545 skb_push(skb, CAPI_DATA_B3_REQ_LEN);
546 memset(skb->data, 0, CAPI_DATA_B3_REQ_LEN);
547 capimsg_setu16(skb->data, 0, CAPI_DATA_B3_REQ_LEN);
548 capimsg_setu16(skb->data, 2, mp->ap->applid);
549 capimsg_setu8 (skb->data, 4, CAPI_DATA_B3);
550 capimsg_setu8 (skb->data, 5, CAPI_REQ);
551 capimsg_setu16(skb->data, 6, mp->msgid++);
552 capimsg_setu32(skb->data, 8, mp->ncci); /* NCCI */
Andrew Morton5e6c20a2007-07-17 04:04:22 -0700553 capimsg_setu32(skb->data, 12, (u32)(long)skb->data);/* Data32 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 capimsg_setu16(skb->data, 16, len); /* Data length */
555 capimsg_setu16(skb->data, 18, datahandle);
556 capimsg_setu16(skb->data, 20, 0); /* Flags */
557
558 if (capincci_add_ack(mp, datahandle) < 0) {
559 skb_pull(skb, CAPI_DATA_B3_REQ_LEN);
560 skb_queue_head(&mp->outqueue, skb);
561 return count;
562 }
563 errcode = capi20_put_message(mp->ap, skb);
564 if (errcode == CAPI_NOERROR) {
565 mp->datahandle++;
566 count++;
567 mp->outbytes -= len;
568#ifdef _DEBUG_DATAFLOW
569 printk(KERN_DEBUG "capi: DATA_B3_REQ %u len=%u\n",
570 datahandle, len);
571#endif
572 continue;
573 }
574 capiminor_del_ack(mp, datahandle);
575
576 if (errcode == CAPI_SENDQUEUEFULL) {
577 skb_pull(skb, CAPI_DATA_B3_REQ_LEN);
578 skb_queue_head(&mp->outqueue, skb);
579 break;
580 }
581
582 /* ups, drop packet */
583 printk(KERN_ERR "capi: put_message = %x\n", errcode);
584 mp->outbytes -= len;
585 kfree_skb(skb);
586 }
587 return count;
588}
589
590#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
591/* -------- function called by lower level -------------------------- */
592
593static void capi_recv_message(struct capi20_appl *ap, struct sk_buff *skb)
594{
595 struct capidev *cdev = ap->private;
596#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
597 struct capiminor *mp;
598 u16 datahandle;
599#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
600 struct capincci *np;
601 u32 ncci;
Michael Buesch053b47f2007-02-12 00:53:26 -0800602 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
604 if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_CONF) {
605 u16 info = CAPIMSG_U16(skb->data, 12); // Info field
606 if (info == 0) {
Matthias Kaehlcke9ea6e5d2007-05-08 00:32:43 -0700607 mutex_lock(&cdev->ncci_list_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
Matthias Kaehlcke9ea6e5d2007-05-08 00:32:43 -0700609 mutex_unlock(&cdev->ncci_list_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 }
611 }
612 if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_IND) {
Matthias Kaehlcke9ea6e5d2007-05-08 00:32:43 -0700613 mutex_lock(&cdev->ncci_list_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
Matthias Kaehlcke9ea6e5d2007-05-08 00:32:43 -0700615 mutex_unlock(&cdev->ncci_list_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 }
Michael Buesch053b47f2007-02-12 00:53:26 -0800617 spin_lock_irqsave(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 if (CAPIMSG_COMMAND(skb->data) != CAPI_DATA_B3) {
619 skb_queue_tail(&cdev->recvqueue, skb);
620 wake_up_interruptible(&cdev->recvwait);
Michael Buesch053b47f2007-02-12 00:53:26 -0800621 spin_unlock_irqrestore(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 return;
623 }
624 ncci = CAPIMSG_CONTROL(skb->data);
625 for (np = cdev->nccis; np && np->ncci != ncci; np = np->next)
626 ;
627 if (!np) {
628 printk(KERN_ERR "BUG: capi_signal: ncci not found\n");
629 skb_queue_tail(&cdev->recvqueue, skb);
630 wake_up_interruptible(&cdev->recvwait);
Michael Buesch053b47f2007-02-12 00:53:26 -0800631 spin_unlock_irqrestore(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 return;
633 }
634#ifndef CONFIG_ISDN_CAPI_MIDDLEWARE
635 skb_queue_tail(&cdev->recvqueue, skb);
636 wake_up_interruptible(&cdev->recvwait);
637#else /* CONFIG_ISDN_CAPI_MIDDLEWARE */
638 mp = np->minorp;
639 if (!mp) {
640 skb_queue_tail(&cdev->recvqueue, skb);
641 wake_up_interruptible(&cdev->recvwait);
Michael Buesch053b47f2007-02-12 00:53:26 -0800642 spin_unlock_irqrestore(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 return;
644 }
645
646
647 if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_IND) {
648
649 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4+4+2);
650#ifdef _DEBUG_DATAFLOW
651 printk(KERN_DEBUG "capi_signal: DATA_B3_IND %u len=%d\n",
652 datahandle, skb->len-CAPIMSG_LEN(skb->data));
653#endif
654 skb_queue_tail(&mp->inqueue, skb);
655 mp->inbytes += skb->len;
656 handle_minor_recv(mp);
657
658 } else if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_CONF) {
659
660 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4);
661#ifdef _DEBUG_DATAFLOW
662 printk(KERN_DEBUG "capi_signal: DATA_B3_CONF %u 0x%x\n",
663 datahandle,
664 CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4+2));
665#endif
666 kfree_skb(skb);
667 (void)capiminor_del_ack(mp, datahandle);
668 if (mp->tty)
669 tty_wakeup(mp->tty);
670 (void)handle_minor_send(mp);
671
672 } else {
673 /* ups, let capi application handle it :-) */
674 skb_queue_tail(&cdev->recvqueue, skb);
675 wake_up_interruptible(&cdev->recvwait);
676 }
677#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
Michael Buesch053b47f2007-02-12 00:53:26 -0800678 spin_unlock_irqrestore(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679}
680
681/* -------- file_operations for capidev ----------------------------- */
682
683static ssize_t
684capi_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
685{
686 struct capidev *cdev = (struct capidev *)file->private_data;
687 struct sk_buff *skb;
688 size_t copied;
689
690 if (!cdev->ap.applid)
691 return -ENODEV;
692
Harvey Harrison2f9e9b62008-04-28 02:14:37 -0700693 if ((skb = skb_dequeue(&cdev->recvqueue)) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
695 if (file->f_flags & O_NONBLOCK)
696 return -EAGAIN;
697
698 for (;;) {
699 interruptible_sleep_on(&cdev->recvwait);
Harvey Harrison2f9e9b62008-04-28 02:14:37 -0700700 if ((skb = skb_dequeue(&cdev->recvqueue)) != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 break;
702 if (signal_pending(current))
703 break;
704 }
Harvey Harrison2f9e9b62008-04-28 02:14:37 -0700705 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 return -ERESTARTNOHAND;
707 }
708 if (skb->len > count) {
709 skb_queue_head(&cdev->recvqueue, skb);
710 return -EMSGSIZE;
711 }
712 if (copy_to_user(buf, skb->data, skb->len)) {
713 skb_queue_head(&cdev->recvqueue, skb);
714 return -EFAULT;
715 }
716 copied = skb->len;
717
718 kfree_skb(skb);
719
720 return copied;
721}
722
723static ssize_t
724capi_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
725{
726 struct capidev *cdev = (struct capidev *)file->private_data;
727 struct sk_buff *skb;
728 u16 mlen;
729
730 if (!cdev->ap.applid)
731 return -ENODEV;
732
733 skb = alloc_skb(count, GFP_USER);
734 if (!skb)
735 return -ENOMEM;
736
737 if (copy_from_user(skb_put(skb, count), buf, count)) {
738 kfree_skb(skb);
739 return -EFAULT;
740 }
741 mlen = CAPIMSG_LEN(skb->data);
742 if (CAPIMSG_CMD(skb->data) == CAPI_DATA_B3_REQ) {
743 if ((size_t)(mlen + CAPIMSG_DATALEN(skb->data)) != count) {
744 kfree_skb(skb);
745 return -EINVAL;
746 }
747 } else {
748 if (mlen != count) {
749 kfree_skb(skb);
750 return -EINVAL;
751 }
752 }
753 CAPIMSG_SETAPPID(skb->data, cdev->ap.applid);
754
755 if (CAPIMSG_CMD(skb->data) == CAPI_DISCONNECT_B3_RESP) {
Matthias Kaehlcke9ea6e5d2007-05-08 00:32:43 -0700756 mutex_lock(&cdev->ncci_list_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 capincci_free(cdev, CAPIMSG_NCCI(skb->data));
Matthias Kaehlcke9ea6e5d2007-05-08 00:32:43 -0700758 mutex_unlock(&cdev->ncci_list_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 }
760
761 cdev->errcode = capi20_put_message(&cdev->ap, skb);
762
763 if (cdev->errcode) {
764 kfree_skb(skb);
765 return -EIO;
766 }
767 return count;
768}
769
770static unsigned int
771capi_poll(struct file *file, poll_table * wait)
772{
773 struct capidev *cdev = (struct capidev *)file->private_data;
774 unsigned int mask = 0;
775
776 if (!cdev->ap.applid)
777 return POLLERR;
778
779 poll_wait(file, &(cdev->recvwait), wait);
780 mask = POLLOUT | POLLWRNORM;
781 if (!skb_queue_empty(&cdev->recvqueue))
782 mask |= POLLIN | POLLRDNORM;
783 return mask;
784}
785
786static int
787capi_ioctl(struct inode *inode, struct file *file,
788 unsigned int cmd, unsigned long arg)
789{
790 struct capidev *cdev = file->private_data;
791 struct capi20_appl *ap = &cdev->ap;
792 capi_ioctl_struct data;
793 int retval = -EINVAL;
794 void __user *argp = (void __user *)arg;
795
796 switch (cmd) {
797 case CAPI_REGISTER:
798 {
799 if (ap->applid)
800 return -EEXIST;
801
802 if (copy_from_user(&cdev->ap.rparam, argp,
803 sizeof(struct capi_register_params)))
804 return -EFAULT;
805
806 cdev->ap.private = cdev;
807 cdev->ap.recv_message = capi_recv_message;
808 cdev->errcode = capi20_register(ap);
809 if (cdev->errcode) {
810 ap->applid = 0;
811 return -EIO;
812 }
813 }
814 return (int)ap->applid;
815
816 case CAPI_GET_VERSION:
817 {
818 if (copy_from_user(&data.contr, argp,
819 sizeof(data.contr)))
820 return -EFAULT;
821 cdev->errcode = capi20_get_version(data.contr, &data.version);
822 if (cdev->errcode)
823 return -EIO;
824 if (copy_to_user(argp, &data.version,
825 sizeof(data.version)))
826 return -EFAULT;
827 }
828 return 0;
829
830 case CAPI_GET_SERIAL:
831 {
832 if (copy_from_user(&data.contr, argp,
833 sizeof(data.contr)))
834 return -EFAULT;
835 cdev->errcode = capi20_get_serial (data.contr, data.serial);
836 if (cdev->errcode)
837 return -EIO;
838 if (copy_to_user(argp, data.serial,
839 sizeof(data.serial)))
840 return -EFAULT;
841 }
842 return 0;
843 case CAPI_GET_PROFILE:
844 {
845 if (copy_from_user(&data.contr, argp,
846 sizeof(data.contr)))
847 return -EFAULT;
848
849 if (data.contr == 0) {
850 cdev->errcode = capi20_get_profile(data.contr, &data.profile);
851 if (cdev->errcode)
852 return -EIO;
853
854 retval = copy_to_user(argp,
855 &data.profile.ncontroller,
856 sizeof(data.profile.ncontroller));
857
858 } else {
859 cdev->errcode = capi20_get_profile(data.contr, &data.profile);
860 if (cdev->errcode)
861 return -EIO;
862
863 retval = copy_to_user(argp, &data.profile,
864 sizeof(data.profile));
865 }
866 if (retval)
867 return -EFAULT;
868 }
869 return 0;
870
871 case CAPI_GET_MANUFACTURER:
872 {
873 if (copy_from_user(&data.contr, argp,
874 sizeof(data.contr)))
875 return -EFAULT;
876 cdev->errcode = capi20_get_manufacturer(data.contr, data.manufacturer);
877 if (cdev->errcode)
878 return -EIO;
879
880 if (copy_to_user(argp, data.manufacturer,
881 sizeof(data.manufacturer)))
882 return -EFAULT;
883
884 }
885 return 0;
886 case CAPI_GET_ERRCODE:
887 data.errcode = cdev->errcode;
888 cdev->errcode = CAPI_NOERROR;
889 if (arg) {
890 if (copy_to_user(argp, &data.errcode,
891 sizeof(data.errcode)))
892 return -EFAULT;
893 }
894 return data.errcode;
895
896 case CAPI_INSTALLED:
897 if (capi20_isinstalled() == CAPI_NOERROR)
898 return 0;
899 return -ENXIO;
900
901 case CAPI_MANUFACTURER_CMD:
902 {
903 struct capi_manufacturer_cmd mcmd;
904 if (!capable(CAP_SYS_ADMIN))
905 return -EPERM;
906 if (copy_from_user(&mcmd, argp, sizeof(mcmd)))
907 return -EFAULT;
908 return capi20_manufacturer(mcmd.cmd, mcmd.data);
909 }
910 return 0;
911
912 case CAPI_SET_FLAGS:
913 case CAPI_CLR_FLAGS:
914 {
915 unsigned userflags;
916 if (copy_from_user(&userflags, argp,
917 sizeof(userflags)))
918 return -EFAULT;
919 if (cmd == CAPI_SET_FLAGS)
920 cdev->userflags |= userflags;
921 else
922 cdev->userflags &= ~userflags;
923 }
924 return 0;
925
926 case CAPI_GET_FLAGS:
927 if (copy_to_user(argp, &cdev->userflags,
928 sizeof(cdev->userflags)))
929 return -EFAULT;
930 return 0;
931
932 case CAPI_NCCI_OPENCOUNT:
933 {
934 struct capincci *nccip;
935#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
936 struct capiminor *mp;
937#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
938 unsigned ncci;
939 int count = 0;
940 if (copy_from_user(&ncci, argp, sizeof(ncci)))
941 return -EFAULT;
942
Matthias Kaehlcke9ea6e5d2007-05-08 00:32:43 -0700943 mutex_lock(&cdev->ncci_list_mtx);
Harvey Harrison2f9e9b62008-04-28 02:14:37 -0700944 if ((nccip = capincci_find(cdev, (u32) ncci)) == NULL) {
Matthias Kaehlcke9ea6e5d2007-05-08 00:32:43 -0700945 mutex_unlock(&cdev->ncci_list_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 return 0;
947 }
948#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
Harvey Harrison2f9e9b62008-04-28 02:14:37 -0700949 if ((mp = nccip->minorp) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 count += atomic_read(&mp->ttyopencount);
951 }
952#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
Matthias Kaehlcke9ea6e5d2007-05-08 00:32:43 -0700953 mutex_unlock(&cdev->ncci_list_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 return count;
955 }
956 return 0;
957
958#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
959 case CAPI_NCCI_GETUNIT:
960 {
961 struct capincci *nccip;
962 struct capiminor *mp;
963 unsigned ncci;
964 int unit = 0;
965 if (copy_from_user(&ncci, argp,
966 sizeof(ncci)))
967 return -EFAULT;
Matthias Kaehlcke9ea6e5d2007-05-08 00:32:43 -0700968 mutex_lock(&cdev->ncci_list_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 nccip = capincci_find(cdev, (u32) ncci);
Harvey Harrison2f9e9b62008-04-28 02:14:37 -0700970 if (!nccip || (mp = nccip->minorp) == NULL) {
Matthias Kaehlcke9ea6e5d2007-05-08 00:32:43 -0700971 mutex_unlock(&cdev->ncci_list_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 return -ESRCH;
973 }
974 unit = mp->minor;
Matthias Kaehlcke9ea6e5d2007-05-08 00:32:43 -0700975 mutex_unlock(&cdev->ncci_list_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 return unit;
977 }
978 return 0;
979#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
980 }
981 return -EINVAL;
982}
983
984static int
985capi_open(struct inode *inode, struct file *file)
986{
Jonathan Corbeta237f3b2008-05-16 14:15:33 -0600987 int ret;
988
989 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 if (file->private_data)
Jonathan Corbeta237f3b2008-05-16 14:15:33 -0600991 ret = -EEXIST;
992 else if ((file->private_data = capidev_alloc()) == NULL)
993 ret = -ENOMEM;
994 else
995 ret = nonseekable_open(inode, file);
996 unlock_kernel();
997 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998}
999
1000static int
1001capi_release(struct inode *inode, struct file *file)
1002{
1003 struct capidev *cdev = (struct capidev *)file->private_data;
1004
1005 capidev_free(cdev);
1006 file->private_data = NULL;
1007
1008 return 0;
1009}
1010
Arjan van de Ven2b8693c2007-02-12 00:55:32 -08001011static const struct file_operations capi_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012{
1013 .owner = THIS_MODULE,
1014 .llseek = no_llseek,
1015 .read = capi_read,
1016 .write = capi_write,
1017 .poll = capi_poll,
1018 .ioctl = capi_ioctl,
1019 .open = capi_open,
1020 .release = capi_release,
1021};
1022
1023#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1024/* -------- tty_operations for capincci ----------------------------- */
1025
1026static int capinc_tty_open(struct tty_struct * tty, struct file * file)
1027{
1028 struct capiminor *mp;
Michael Buesch053b47f2007-02-12 00:53:26 -08001029 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
Harvey Harrison2f9e9b62008-04-28 02:14:37 -07001031 if ((mp = capiminor_find(iminor(file->f_path.dentry->d_inode))) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 return -ENXIO;
Harvey Harrison2f9e9b62008-04-28 02:14:37 -07001033 if (mp->nccip == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 return -ENXIO;
1035
1036 tty->driver_data = (void *)mp;
1037
Michael Buesch053b47f2007-02-12 00:53:26 -08001038 spin_lock_irqsave(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 if (atomic_read(&mp->ttyopencount) == 0)
1040 mp->tty = tty;
1041 atomic_inc(&mp->ttyopencount);
1042#ifdef _DEBUG_REFCOUNT
1043 printk(KERN_DEBUG "capinc_tty_open ocount=%d\n", atomic_read(&mp->ttyopencount));
1044#endif
1045 handle_minor_recv(mp);
Michael Buesch053b47f2007-02-12 00:53:26 -08001046 spin_unlock_irqrestore(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 return 0;
1048}
1049
1050static void capinc_tty_close(struct tty_struct * tty, struct file * file)
1051{
1052 struct capiminor *mp;
1053
1054 mp = (struct capiminor *)tty->driver_data;
1055 if (mp) {
1056 if (atomic_dec_and_test(&mp->ttyopencount)) {
1057#ifdef _DEBUG_REFCOUNT
1058 printk(KERN_DEBUG "capinc_tty_close lastclose\n");
1059#endif
1060 tty->driver_data = NULL;
1061 mp->tty = NULL;
1062 }
1063#ifdef _DEBUG_REFCOUNT
1064 printk(KERN_DEBUG "capinc_tty_close ocount=%d\n", atomic_read(&mp->ttyopencount));
1065#endif
Harvey Harrison2f9e9b62008-04-28 02:14:37 -07001066 if (mp->nccip == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 capiminor_free(mp);
1068 }
1069
1070#ifdef _DEBUG_REFCOUNT
1071 printk(KERN_DEBUG "capinc_tty_close\n");
1072#endif
1073}
1074
1075static int capinc_tty_write(struct tty_struct * tty,
1076 const unsigned char *buf, int count)
1077{
1078 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1079 struct sk_buff *skb;
Michael Buesch053b47f2007-02-12 00:53:26 -08001080 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
1082#ifdef _DEBUG_TTYFUNCS
1083 printk(KERN_DEBUG "capinc_tty_write(count=%d)\n", count);
1084#endif
1085
1086 if (!mp || !mp->nccip) {
1087#ifdef _DEBUG_TTYFUNCS
1088 printk(KERN_DEBUG "capinc_tty_write: mp or mp->ncci NULL\n");
1089#endif
1090 return 0;
1091 }
1092
Michael Buesch053b47f2007-02-12 00:53:26 -08001093 spin_lock_irqsave(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 skb = mp->ttyskb;
1095 if (skb) {
1096 mp->ttyskb = NULL;
1097 skb_queue_tail(&mp->outqueue, skb);
1098 mp->outbytes += skb->len;
1099 }
1100
1101 skb = alloc_skb(CAPI_DATA_B3_REQ_LEN+count, GFP_ATOMIC);
1102 if (!skb) {
1103 printk(KERN_ERR "capinc_tty_write: alloc_skb failed\n");
Michael Buesch053b47f2007-02-12 00:53:26 -08001104 spin_unlock_irqrestore(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 return -ENOMEM;
1106 }
1107
1108 skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
1109 memcpy(skb_put(skb, count), buf, count);
1110
1111 skb_queue_tail(&mp->outqueue, skb);
1112 mp->outbytes += skb->len;
1113 (void)handle_minor_send(mp);
1114 (void)handle_minor_recv(mp);
Michael Buesch053b47f2007-02-12 00:53:26 -08001115 spin_unlock_irqrestore(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 return count;
1117}
1118
Alan Coxf2545a72008-04-30 00:54:09 -07001119static int capinc_tty_put_char(struct tty_struct *tty, unsigned char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120{
1121 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1122 struct sk_buff *skb;
Michael Buesch053b47f2007-02-12 00:53:26 -08001123 unsigned long flags;
Alan Coxf2545a72008-04-30 00:54:09 -07001124 int ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
1126#ifdef _DEBUG_TTYFUNCS
1127 printk(KERN_DEBUG "capinc_put_char(%u)\n", ch);
1128#endif
1129
1130 if (!mp || !mp->nccip) {
1131#ifdef _DEBUG_TTYFUNCS
1132 printk(KERN_DEBUG "capinc_tty_put_char: mp or mp->ncci NULL\n");
1133#endif
Alan Coxf2545a72008-04-30 00:54:09 -07001134 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 }
1136
Michael Buesch053b47f2007-02-12 00:53:26 -08001137 spin_lock_irqsave(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 skb = mp->ttyskb;
1139 if (skb) {
1140 if (skb_tailroom(skb) > 0) {
1141 *(skb_put(skb, 1)) = ch;
Michael Buesch053b47f2007-02-12 00:53:26 -08001142 spin_unlock_irqrestore(&workaround_lock, flags);
Alan Coxf2545a72008-04-30 00:54:09 -07001143 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 }
1145 mp->ttyskb = NULL;
1146 skb_queue_tail(&mp->outqueue, skb);
1147 mp->outbytes += skb->len;
1148 (void)handle_minor_send(mp);
1149 }
1150 skb = alloc_skb(CAPI_DATA_B3_REQ_LEN+CAPI_MAX_BLKSIZE, GFP_ATOMIC);
1151 if (skb) {
1152 skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
1153 *(skb_put(skb, 1)) = ch;
1154 mp->ttyskb = skb;
1155 } else {
1156 printk(KERN_ERR "capinc_put_char: char %u lost\n", ch);
Alan Coxf2545a72008-04-30 00:54:09 -07001157 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 }
Michael Buesch053b47f2007-02-12 00:53:26 -08001159 spin_unlock_irqrestore(&workaround_lock, flags);
Alan Coxf2545a72008-04-30 00:54:09 -07001160 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161}
1162
1163static void capinc_tty_flush_chars(struct tty_struct *tty)
1164{
1165 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1166 struct sk_buff *skb;
Michael Buesch053b47f2007-02-12 00:53:26 -08001167 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168
1169#ifdef _DEBUG_TTYFUNCS
1170 printk(KERN_DEBUG "capinc_tty_flush_chars\n");
1171#endif
1172
1173 if (!mp || !mp->nccip) {
1174#ifdef _DEBUG_TTYFUNCS
1175 printk(KERN_DEBUG "capinc_tty_flush_chars: mp or mp->ncci NULL\n");
1176#endif
1177 return;
1178 }
1179
Michael Buesch053b47f2007-02-12 00:53:26 -08001180 spin_lock_irqsave(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 skb = mp->ttyskb;
1182 if (skb) {
1183 mp->ttyskb = NULL;
1184 skb_queue_tail(&mp->outqueue, skb);
1185 mp->outbytes += skb->len;
1186 (void)handle_minor_send(mp);
1187 }
1188 (void)handle_minor_recv(mp);
Michael Buesch053b47f2007-02-12 00:53:26 -08001189 spin_unlock_irqrestore(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190}
1191
1192static int capinc_tty_write_room(struct tty_struct *tty)
1193{
1194 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1195 int room;
1196 if (!mp || !mp->nccip) {
1197#ifdef _DEBUG_TTYFUNCS
1198 printk(KERN_DEBUG "capinc_tty_write_room: mp or mp->ncci NULL\n");
1199#endif
1200 return 0;
1201 }
1202 room = CAPINC_MAX_SENDQUEUE-skb_queue_len(&mp->outqueue);
1203 room *= CAPI_MAX_BLKSIZE;
1204#ifdef _DEBUG_TTYFUNCS
1205 printk(KERN_DEBUG "capinc_tty_write_room = %d\n", room);
1206#endif
1207 return room;
1208}
1209
Adrian Bunk408b6642005-05-01 08:59:29 -07001210static int capinc_tty_chars_in_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211{
1212 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1213 if (!mp || !mp->nccip) {
1214#ifdef _DEBUG_TTYFUNCS
1215 printk(KERN_DEBUG "capinc_tty_chars_in_buffer: mp or mp->ncci NULL\n");
1216#endif
1217 return 0;
1218 }
1219#ifdef _DEBUG_TTYFUNCS
1220 printk(KERN_DEBUG "capinc_tty_chars_in_buffer = %d nack=%d sq=%d rq=%d\n",
1221 mp->outbytes, mp->nack,
1222 skb_queue_len(&mp->outqueue),
1223 skb_queue_len(&mp->inqueue));
1224#endif
1225 return mp->outbytes;
1226}
1227
1228static int capinc_tty_ioctl(struct tty_struct *tty, struct file * file,
1229 unsigned int cmd, unsigned long arg)
1230{
1231 int error = 0;
1232 switch (cmd) {
1233 default:
1234 error = n_tty_ioctl (tty, file, cmd, arg);
1235 break;
1236 }
1237 return error;
1238}
1239
Alan Cox606d0992006-12-08 02:38:45 -08001240static void capinc_tty_set_termios(struct tty_struct *tty, struct ktermios * old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241{
1242#ifdef _DEBUG_TTYFUNCS
1243 printk(KERN_DEBUG "capinc_tty_set_termios\n");
1244#endif
1245}
1246
1247static void capinc_tty_throttle(struct tty_struct * tty)
1248{
1249 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1250#ifdef _DEBUG_TTYFUNCS
1251 printk(KERN_DEBUG "capinc_tty_throttle\n");
1252#endif
1253 if (mp)
1254 mp->ttyinstop = 1;
1255}
1256
1257static void capinc_tty_unthrottle(struct tty_struct * tty)
1258{
1259 struct capiminor *mp = (struct capiminor *)tty->driver_data;
Michael Buesch053b47f2007-02-12 00:53:26 -08001260 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261#ifdef _DEBUG_TTYFUNCS
1262 printk(KERN_DEBUG "capinc_tty_unthrottle\n");
1263#endif
1264 if (mp) {
Michael Buesch053b47f2007-02-12 00:53:26 -08001265 spin_lock_irqsave(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 mp->ttyinstop = 0;
1267 handle_minor_recv(mp);
Michael Buesch053b47f2007-02-12 00:53:26 -08001268 spin_unlock_irqrestore(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 }
1270}
1271
1272static void capinc_tty_stop(struct tty_struct *tty)
1273{
1274 struct capiminor *mp = (struct capiminor *)tty->driver_data;
1275#ifdef _DEBUG_TTYFUNCS
1276 printk(KERN_DEBUG "capinc_tty_stop\n");
1277#endif
1278 if (mp) {
1279 mp->ttyoutstop = 1;
1280 }
1281}
1282
1283static void capinc_tty_start(struct tty_struct *tty)
1284{
1285 struct capiminor *mp = (struct capiminor *)tty->driver_data;
Michael Buesch053b47f2007-02-12 00:53:26 -08001286 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287#ifdef _DEBUG_TTYFUNCS
1288 printk(KERN_DEBUG "capinc_tty_start\n");
1289#endif
1290 if (mp) {
Michael Buesch053b47f2007-02-12 00:53:26 -08001291 spin_lock_irqsave(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 mp->ttyoutstop = 0;
1293 (void)handle_minor_send(mp);
Michael Buesch053b47f2007-02-12 00:53:26 -08001294 spin_unlock_irqrestore(&workaround_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 }
1296}
1297
1298static void capinc_tty_hangup(struct tty_struct *tty)
1299{
1300#ifdef _DEBUG_TTYFUNCS
1301 printk(KERN_DEBUG "capinc_tty_hangup\n");
1302#endif
1303}
1304
1305static void capinc_tty_break_ctl(struct tty_struct *tty, int state)
1306{
1307#ifdef _DEBUG_TTYFUNCS
1308 printk(KERN_DEBUG "capinc_tty_break_ctl(%d)\n", state);
1309#endif
1310}
1311
1312static void capinc_tty_flush_buffer(struct tty_struct *tty)
1313{
1314#ifdef _DEBUG_TTYFUNCS
1315 printk(KERN_DEBUG "capinc_tty_flush_buffer\n");
1316#endif
1317}
1318
1319static void capinc_tty_set_ldisc(struct tty_struct *tty)
1320{
1321#ifdef _DEBUG_TTYFUNCS
1322 printk(KERN_DEBUG "capinc_tty_set_ldisc\n");
1323#endif
1324}
1325
1326static void capinc_tty_send_xchar(struct tty_struct *tty, char ch)
1327{
1328#ifdef _DEBUG_TTYFUNCS
1329 printk(KERN_DEBUG "capinc_tty_send_xchar(%d)\n", ch);
1330#endif
1331}
1332
1333static int capinc_tty_read_proc(char *page, char **start, off_t off,
1334 int count, int *eof, void *data)
1335{
1336 return 0;
1337}
1338
1339static struct tty_driver *capinc_tty_driver;
1340
Jeff Dikeb68e31d2006-10-02 02:17:18 -07001341static const struct tty_operations capinc_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 .open = capinc_tty_open,
1343 .close = capinc_tty_close,
1344 .write = capinc_tty_write,
1345 .put_char = capinc_tty_put_char,
1346 .flush_chars = capinc_tty_flush_chars,
1347 .write_room = capinc_tty_write_room,
1348 .chars_in_buffer = capinc_tty_chars_in_buffer,
1349 .ioctl = capinc_tty_ioctl,
1350 .set_termios = capinc_tty_set_termios,
1351 .throttle = capinc_tty_throttle,
1352 .unthrottle = capinc_tty_unthrottle,
1353 .stop = capinc_tty_stop,
1354 .start = capinc_tty_start,
1355 .hangup = capinc_tty_hangup,
1356 .break_ctl = capinc_tty_break_ctl,
1357 .flush_buffer = capinc_tty_flush_buffer,
1358 .set_ldisc = capinc_tty_set_ldisc,
1359 .send_xchar = capinc_tty_send_xchar,
1360 .read_proc = capinc_tty_read_proc,
1361};
1362
1363static int capinc_tty_init(void)
1364{
1365 struct tty_driver *drv;
1366
1367 if (capi_ttyminors > CAPINC_MAX_PORTS)
1368 capi_ttyminors = CAPINC_MAX_PORTS;
1369 if (capi_ttyminors <= 0)
1370 capi_ttyminors = CAPINC_NR_PORTS;
1371
1372 drv = alloc_tty_driver(capi_ttyminors);
1373 if (!drv)
1374 return -ENOMEM;
1375
1376 drv->owner = THIS_MODULE;
1377 drv->driver_name = "capi_nc";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 drv->name = "capi";
1379 drv->major = capi_ttymajor;
1380 drv->minor_start = 0;
1381 drv->type = TTY_DRIVER_TYPE_SERIAL;
1382 drv->subtype = SERIAL_TYPE_NORMAL;
1383 drv->init_termios = tty_std_termios;
1384 drv->init_termios.c_iflag = ICRNL;
1385 drv->init_termios.c_oflag = OPOST | ONLCR;
1386 drv->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1387 drv->init_termios.c_lflag = 0;
1388 drv->flags = TTY_DRIVER_REAL_RAW|TTY_DRIVER_RESET_TERMIOS;
1389 tty_set_operations(drv, &capinc_ops);
1390 if (tty_register_driver(drv)) {
1391 put_tty_driver(drv);
1392 printk(KERN_ERR "Couldn't register capi_nc driver\n");
1393 return -1;
1394 }
1395 capinc_tty_driver = drv;
1396 return 0;
1397}
1398
1399static void capinc_tty_exit(void)
1400{
1401 struct tty_driver *drv = capinc_tty_driver;
1402 int retval;
1403 if ((retval = tty_unregister_driver(drv)))
1404 printk(KERN_ERR "capi: failed to unregister capi_nc driver (%d)\n", retval);
1405 put_tty_driver(drv);
1406}
1407
1408#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
1409
1410/* -------- /proc functions ----------------------------------------- */
1411
1412/*
1413 * /proc/capi/capi20:
1414 * minor applid nrecvctlpkt nrecvdatapkt nsendctlpkt nsenddatapkt
1415 */
1416static int proc_capidev_read_proc(char *page, char **start, off_t off,
1417 int count, int *eof, void *data)
1418{
1419 struct capidev *cdev;
1420 struct list_head *l;
1421 int len = 0;
1422
1423 read_lock(&capidev_list_lock);
1424 list_for_each(l, &capidev_list) {
1425 cdev = list_entry(l, struct capidev, list);
1426 len += sprintf(page+len, "0 %d %lu %lu %lu %lu\n",
1427 cdev->ap.applid,
1428 cdev->ap.nrecvctlpkt,
1429 cdev->ap.nrecvdatapkt,
1430 cdev->ap.nsentctlpkt,
1431 cdev->ap.nsentdatapkt);
1432 if (len <= off) {
1433 off -= len;
1434 len = 0;
1435 } else {
1436 if (len-off > count)
1437 goto endloop;
1438 }
1439 }
1440
1441endloop:
1442 read_unlock(&capidev_list_lock);
1443 if (len < count)
1444 *eof = 1;
1445 if (len > count) len = count;
1446 if (len < 0) len = 0;
1447 return len;
1448}
1449
1450/*
1451 * /proc/capi/capi20ncci:
1452 * applid ncci
1453 */
1454static int proc_capincci_read_proc(char *page, char **start, off_t off,
1455 int count, int *eof, void *data)
1456{
1457 struct capidev *cdev;
1458 struct capincci *np;
1459 struct list_head *l;
1460 int len = 0;
1461
1462 read_lock(&capidev_list_lock);
1463 list_for_each(l, &capidev_list) {
1464 cdev = list_entry(l, struct capidev, list);
1465 for (np=cdev->nccis; np; np = np->next) {
1466 len += sprintf(page+len, "%d 0x%x\n",
1467 cdev->ap.applid,
1468 np->ncci);
1469 if (len <= off) {
1470 off -= len;
1471 len = 0;
1472 } else {
1473 if (len-off > count)
1474 goto endloop;
1475 }
1476 }
1477 }
1478endloop:
1479 read_unlock(&capidev_list_lock);
1480 *start = page+off;
1481 if (len < count)
1482 *eof = 1;
1483 if (len>count) len = count;
1484 if (len<0) len = 0;
1485 return len;
1486}
1487
1488static struct procfsentries {
1489 char *name;
1490 mode_t mode;
1491 int (*read_proc)(char *page, char **start, off_t off,
1492 int count, int *eof, void *data);
1493 struct proc_dir_entry *procent;
1494} procfsentries[] = {
1495 /* { "capi", S_IFDIR, 0 }, */
1496 { "capi/capi20", 0 , proc_capidev_read_proc },
1497 { "capi/capi20ncci", 0 , proc_capincci_read_proc },
1498};
1499
1500static void __init proc_init(void)
1501{
Ahmed S. Darwishfd863db2007-02-12 00:53:19 -08001502 int nelem = ARRAY_SIZE(procfsentries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 int i;
1504
1505 for (i=0; i < nelem; i++) {
1506 struct procfsentries *p = procfsentries + i;
1507 p->procent = create_proc_entry(p->name, p->mode, NULL);
1508 if (p->procent) p->procent->read_proc = p->read_proc;
1509 }
1510}
1511
1512static void __exit proc_exit(void)
1513{
Ahmed S. Darwishfd863db2007-02-12 00:53:19 -08001514 int nelem = ARRAY_SIZE(procfsentries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 int i;
1516
1517 for (i=nelem-1; i >= 0; i--) {
1518 struct procfsentries *p = procfsentries + i;
1519 if (p->procent) {
1520 remove_proc_entry(p->name, NULL);
1521 p->procent = NULL;
1522 }
1523 }
1524}
1525
1526/* -------- init function and module interface ---------------------- */
1527
1528
1529static char rev[32];
1530
1531static int __init capi_init(void)
1532{
1533 char *p;
1534 char *compileinfo;
Andrew Morton6d9eac32006-03-28 01:56:19 -08001535 int major_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536
Harvey Harrison2f9e9b62008-04-28 02:14:37 -07001537 if ((p = strchr(revision, ':')) != NULL && p[1]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 strlcpy(rev, p + 2, sizeof(rev));
Harvey Harrison2f9e9b62008-04-28 02:14:37 -07001539 if ((p = strchr(rev, '$')) != NULL && p > rev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 *(p-1) = 0;
1541 } else
1542 strcpy(rev, "1.0");
1543
Andrew Morton6d9eac32006-03-28 01:56:19 -08001544 major_ret = register_chrdev(capi_major, "capi20", &capi_fops);
1545 if (major_ret < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 printk(KERN_ERR "capi20: unable to get major %d\n", capi_major);
Andrew Morton6d9eac32006-03-28 01:56:19 -08001547 return major_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 }
gregkh@suse.de56b22932005-03-23 10:01:41 -08001549 capi_class = class_create(THIS_MODULE, "capi");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 if (IS_ERR(capi_class)) {
1551 unregister_chrdev(capi_major, "capi20");
1552 return PTR_ERR(capi_class);
1553 }
1554
Greg Kroah-Hartman24d0b412008-05-21 12:52:33 -07001555 device_create_drvdata(capi_class, NULL, MKDEV(capi_major, 0), NULL,
1556 "capi");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
1558#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1559 if (capinc_tty_init() < 0) {
Tony Jonesd78b0362007-09-25 02:03:03 +02001560 device_destroy(capi_class, MKDEV(capi_major, 0));
gregkh@suse.de56b22932005-03-23 10:01:41 -08001561 class_destroy(capi_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 unregister_chrdev(capi_major, "capi20");
1563 return -ENOMEM;
1564 }
1565#endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
1566
1567 proc_init();
1568
1569#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1570#if defined(CONFIG_ISDN_CAPI_CAPIFS) || defined(CONFIG_ISDN_CAPI_CAPIFS_MODULE)
1571 compileinfo = " (middleware+capifs)";
1572#else
1573 compileinfo = " (no capifs)";
1574#endif
1575#else
1576 compileinfo = " (no middleware)";
1577#endif
1578 printk(KERN_NOTICE "capi20: Rev %s: started up with major %d%s\n",
1579 rev, capi_major, compileinfo);
1580
1581 return 0;
1582}
1583
1584static void __exit capi_exit(void)
1585{
1586 proc_exit();
1587
Tony Jonesd78b0362007-09-25 02:03:03 +02001588 device_destroy(capi_class, MKDEV(capi_major, 0));
gregkh@suse.de56b22932005-03-23 10:01:41 -08001589 class_destroy(capi_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 unregister_chrdev(capi_major, "capi20");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591
1592#ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1593 capinc_tty_exit();
1594#endif
1595 printk(KERN_NOTICE "capi: Rev %s: unloaded\n", rev);
1596}
1597
1598module_init(capi_init);
1599module_exit(capi_exit);