Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
capi.c
Go to the documentation of this file.
1 /* $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 <[email protected]>
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 
12 #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>
21 #include <linux/mutex.h>
22 #include <linux/mm.h>
23 #include <linux/timer.h>
24 #include <linux/wait.h>
25 #include <linux/tty.h>
26 #include <linux/netdevice.h>
27 #include <linux/ppp_defs.h>
28 #include <linux/ppp-ioctl.h>
29 #include <linux/skbuff.h>
30 #include <linux/proc_fs.h>
31 #include <linux/seq_file.h>
32 #include <linux/poll.h>
33 #include <linux/capi.h>
34 #include <linux/kernelcapi.h>
35 #include <linux/init.h>
36 #include <linux/device.h>
37 #include <linux/moduleparam.h>
38 #include <linux/isdn/capiutil.h>
39 #include <linux/isdn/capicmd.h>
40 
41 MODULE_DESCRIPTION("CAPI4Linux: Userspace /dev/capi20 interface");
42 MODULE_AUTHOR("Carsten Paeth");
43 MODULE_LICENSE("GPL");
44 
45 /* -------- driver information -------------------------------------- */
46 
47 static DEFINE_MUTEX(capi_mutex);
48 static struct class *capi_class;
49 static int capi_major = 68; /* allocated */
50 
51 module_param_named(major, capi_major, uint, 0);
52 
53 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
54 #define CAPINC_NR_PORTS 32
55 #define CAPINC_MAX_PORTS 256
56 
57 static int capi_ttyminors = CAPINC_NR_PORTS;
58 
59 module_param_named(ttyminors, capi_ttyminors, uint, 0);
60 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
61 
62 /* -------- defines ------------------------------------------------- */
63 
64 #define CAPINC_MAX_RECVQUEUE 10
65 #define CAPINC_MAX_SENDQUEUE 10
66 #define CAPI_MAX_BLKSIZE 2048
67 
68 /* -------- data structures ----------------------------------------- */
69 
70 struct capidev;
71 struct capincci;
72 struct capiminor;
73 
75  struct list_head list;
77 };
78 
79 struct capiminor {
80  struct kref kref;
81 
82  unsigned int minor;
83 
84  struct capi20_appl *ap;
88 
89  struct tty_port port;
90  int ttyinstop;
92 
94 
96  int outbytes;
97  struct sk_buff *outskb;
99 
100  /* transmit path */
102  int nack;
104 };
105 
106 struct capincci {
107  struct list_head list;
109  struct capidev *cdev;
110 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
111  struct capiminor *minorp;
112 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
113 };
114 
115 struct capidev {
116  struct list_head list;
117  struct capi20_appl ap;
119  unsigned userflags;
120 
123 
124  struct list_head nccis;
125 
126  struct mutex lock;
127 };
128 
129 /* -------- global variables ---------------------------------------- */
130 
131 static DEFINE_MUTEX(capidev_list_lock);
132 static LIST_HEAD(capidev_list);
133 
134 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
135 
136 static DEFINE_SPINLOCK(capiminors_lock);
137 static struct capiminor **capiminors;
138 
139 static struct tty_driver *capinc_tty_driver;
140 
141 /* -------- datahandles --------------------------------------------- */
142 
143 static int capiminor_add_ack(struct capiminor *mp, u16 datahandle)
144 {
145  struct ackqueue_entry *n;
146 
147  n = kmalloc(sizeof(*n), GFP_ATOMIC);
148  if (unlikely(!n)) {
149  printk(KERN_ERR "capi: alloc datahandle failed\n");
150  return -1;
151  }
152  n->datahandle = datahandle;
153  INIT_LIST_HEAD(&n->list);
154  spin_lock_bh(&mp->ackqlock);
155  list_add_tail(&n->list, &mp->ackqueue);
156  mp->nack++;
157  spin_unlock_bh(&mp->ackqlock);
158  return 0;
159 }
160 
161 static int capiminor_del_ack(struct capiminor *mp, u16 datahandle)
162 {
163  struct ackqueue_entry *p, *tmp;
164 
165  spin_lock_bh(&mp->ackqlock);
166  list_for_each_entry_safe(p, tmp, &mp->ackqueue, list) {
167  if (p->datahandle == datahandle) {
168  list_del(&p->list);
169  mp->nack--;
170  spin_unlock_bh(&mp->ackqlock);
171  kfree(p);
172  return 0;
173  }
174  }
175  spin_unlock_bh(&mp->ackqlock);
176  return -1;
177 }
178 
179 static void capiminor_del_all_ack(struct capiminor *mp)
180 {
181  struct ackqueue_entry *p, *tmp;
182 
183  list_for_each_entry_safe(p, tmp, &mp->ackqueue, list) {
184  list_del(&p->list);
185  kfree(p);
186  mp->nack--;
187  }
188 }
189 
190 
191 /* -------- struct capiminor ---------------------------------------- */
192 
193 static const struct tty_port_operations capiminor_port_ops; /* we have none */
194 
195 static struct capiminor *capiminor_alloc(struct capi20_appl *ap, u32 ncci)
196 {
197  struct capiminor *mp;
198  struct device *dev;
199  unsigned int minor;
200 
201  mp = kzalloc(sizeof(*mp), GFP_KERNEL);
202  if (!mp) {
203  printk(KERN_ERR "capi: can't alloc capiminor\n");
204  return NULL;
205  }
206 
207  kref_init(&mp->kref);
208 
209  mp->ap = ap;
210  mp->ncci = ncci;
211  INIT_LIST_HEAD(&mp->ackqueue);
212  spin_lock_init(&mp->ackqlock);
213 
214  skb_queue_head_init(&mp->inqueue);
215  skb_queue_head_init(&mp->outqueue);
216  spin_lock_init(&mp->outlock);
217 
218  tty_port_init(&mp->port);
219  mp->port.ops = &capiminor_port_ops;
220 
221  /* Allocate the least unused minor number. */
222  spin_lock(&capiminors_lock);
223  for (minor = 0; minor < capi_ttyminors; minor++)
224  if (!capiminors[minor]) {
225  capiminors[minor] = mp;
226  break;
227  }
228  spin_unlock(&capiminors_lock);
229 
230  if (minor == capi_ttyminors) {
231  printk(KERN_NOTICE "capi: out of minors\n");
232  goto err_out1;
233  }
234 
235  mp->minor = minor;
236 
237  dev = tty_port_register_device(&mp->port, capinc_tty_driver, minor,
238  NULL);
239  if (IS_ERR(dev))
240  goto err_out2;
241 
242  return mp;
243 
244 err_out2:
245  spin_lock(&capiminors_lock);
246  capiminors[minor] = NULL;
247  spin_unlock(&capiminors_lock);
248 
249 err_out1:
250  kfree(mp);
251  return NULL;
252 }
253 
254 static void capiminor_destroy(struct kref *kref)
255 {
256  struct capiminor *mp = container_of(kref, struct capiminor, kref);
257 
258  kfree_skb(mp->outskb);
259  skb_queue_purge(&mp->inqueue);
261  capiminor_del_all_ack(mp);
262  kfree(mp);
263 }
264 
265 static struct capiminor *capiminor_get(unsigned int minor)
266 {
267  struct capiminor *mp;
268 
269  spin_lock(&capiminors_lock);
270  mp = capiminors[minor];
271  if (mp)
272  kref_get(&mp->kref);
273  spin_unlock(&capiminors_lock);
274 
275  return mp;
276 }
277 
278 static inline void capiminor_put(struct capiminor *mp)
279 {
280  kref_put(&mp->kref, capiminor_destroy);
281 }
282 
283 static void capiminor_free(struct capiminor *mp)
284 {
285  tty_unregister_device(capinc_tty_driver, mp->minor);
286 
287  spin_lock(&capiminors_lock);
288  capiminors[mp->minor] = NULL;
289  spin_unlock(&capiminors_lock);
290 
291  capiminor_put(mp);
292 }
293 
294 /* -------- struct capincci ----------------------------------------- */
295 
296 static void capincci_alloc_minor(struct capidev *cdev, struct capincci *np)
297 {
298  if (cdev->userflags & CAPIFLAG_HIGHJACKING)
299  np->minorp = capiminor_alloc(&cdev->ap, np->ncci);
300 }
301 
302 static void capincci_free_minor(struct capincci *np)
303 {
304  struct capiminor *mp = np->minorp;
305  struct tty_struct *tty;
306 
307  if (mp) {
308  tty = tty_port_tty_get(&mp->port);
309  if (tty) {
310  tty_vhangup(tty);
311  tty_kref_put(tty);
312  }
313 
314  capiminor_free(mp);
315  }
316 }
317 
318 static inline unsigned int capincci_minor_opencount(struct capincci *np)
319 {
320  struct capiminor *mp = np->minorp;
321  unsigned int count = 0;
322  struct tty_struct *tty;
323 
324  if (mp) {
325  tty = tty_port_tty_get(&mp->port);
326  if (tty) {
327  count = tty->count;
328  tty_kref_put(tty);
329  }
330  }
331  return count;
332 }
333 
334 #else /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
335 
336 static inline void
337 capincci_alloc_minor(struct capidev *cdev, struct capincci *np) { }
338 static inline void capincci_free_minor(struct capincci *np) { }
339 
340 #endif /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
341 
342 static struct capincci *capincci_alloc(struct capidev *cdev, u32 ncci)
343 {
344  struct capincci *np;
345 
346  np = kzalloc(sizeof(*np), GFP_KERNEL);
347  if (!np)
348  return NULL;
349  np->ncci = ncci;
350  np->cdev = cdev;
351 
352  capincci_alloc_minor(cdev, np);
353 
354  list_add_tail(&np->list, &cdev->nccis);
355 
356  return np;
357 }
358 
359 static void capincci_free(struct capidev *cdev, u32 ncci)
360 {
361  struct capincci *np, *tmp;
362 
363  list_for_each_entry_safe(np, tmp, &cdev->nccis, list)
364  if (ncci == 0xffffffff || np->ncci == ncci) {
365  capincci_free_minor(np);
366  list_del(&np->list);
367  kfree(np);
368  }
369 }
370 
371 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
372 static struct capincci *capincci_find(struct capidev *cdev, u32 ncci)
373 {
374  struct capincci *np;
375 
376  list_for_each_entry(np, &cdev->nccis, list)
377  if (np->ncci == ncci)
378  return np;
379  return NULL;
380 }
381 
382 /* -------- handle data queue --------------------------------------- */
383 
384 static struct sk_buff *
385 gen_data_b3_resp_for(struct capiminor *mp, struct sk_buff *skb)
386 {
387  struct sk_buff *nskb;
388  nskb = alloc_skb(CAPI_DATA_B3_RESP_LEN, GFP_KERNEL);
389  if (nskb) {
390  u16 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN + 4 + 4 + 2);
391  unsigned char *s = skb_put(nskb, CAPI_DATA_B3_RESP_LEN);
392  capimsg_setu16(s, 0, CAPI_DATA_B3_RESP_LEN);
393  capimsg_setu16(s, 2, mp->ap->applid);
394  capimsg_setu8 (s, 4, CAPI_DATA_B3);
395  capimsg_setu8 (s, 5, CAPI_RESP);
396  capimsg_setu16(s, 6, atomic_inc_return(&mp->msgid));
397  capimsg_setu32(s, 8, mp->ncci);
398  capimsg_setu16(s, 12, datahandle);
399  }
400  return nskb;
401 }
402 
403 static int handle_recv_skb(struct capiminor *mp, struct sk_buff *skb)
404 {
405  unsigned int datalen = skb->len - CAPIMSG_LEN(skb->data);
406  struct tty_struct *tty;
407  struct sk_buff *nskb;
408  u16 errcode, datahandle;
409  struct tty_ldisc *ld;
410  int ret = -1;
411 
412  tty = tty_port_tty_get(&mp->port);
413  if (!tty) {
414  pr_debug("capi: currently no receiver\n");
415  return -1;
416  }
417 
418  ld = tty_ldisc_ref(tty);
419  if (!ld) {
420  /* fatal error, do not requeue */
421  ret = 0;
422  kfree_skb(skb);
423  goto deref_tty;
424  }
425 
426  if (ld->ops->receive_buf == NULL) {
427  pr_debug("capi: ldisc has no receive_buf function\n");
428  /* fatal error, do not requeue */
429  goto free_skb;
430  }
431  if (mp->ttyinstop) {
432  pr_debug("capi: recv tty throttled\n");
433  goto deref_ldisc;
434  }
435 
436  if (tty->receive_room < datalen) {
437  pr_debug("capi: no room in tty\n");
438  goto deref_ldisc;
439  }
440 
441  nskb = gen_data_b3_resp_for(mp, skb);
442  if (!nskb) {
443  printk(KERN_ERR "capi: gen_data_b3_resp failed\n");
444  goto deref_ldisc;
445  }
446 
447  datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN + 4);
448 
449  errcode = capi20_put_message(mp->ap, nskb);
450 
451  if (errcode == CAPI_NOERROR) {
452  skb_pull(skb, CAPIMSG_LEN(skb->data));
453  pr_debug("capi: DATA_B3_RESP %u len=%d => ldisc\n",
454  datahandle, skb->len);
455  ld->ops->receive_buf(tty, skb->data, NULL, skb->len);
456  } else {
457  printk(KERN_ERR "capi: send DATA_B3_RESP failed=%x\n",
458  errcode);
459  kfree_skb(nskb);
460 
461  if (errcode == CAPI_SENDQUEUEFULL)
462  goto deref_ldisc;
463  }
464 
465 free_skb:
466  ret = 0;
467  kfree_skb(skb);
468 
469 deref_ldisc:
470  tty_ldisc_deref(ld);
471 
472 deref_tty:
473  tty_kref_put(tty);
474  return ret;
475 }
476 
477 static void handle_minor_recv(struct capiminor *mp)
478 {
479  struct sk_buff *skb;
480 
481  while ((skb = skb_dequeue(&mp->inqueue)) != NULL)
482  if (handle_recv_skb(mp, skb) < 0) {
483  skb_queue_head(&mp->inqueue, skb);
484  return;
485  }
486 }
487 
488 static void handle_minor_send(struct capiminor *mp)
489 {
490  struct tty_struct *tty;
491  struct sk_buff *skb;
492  u16 len;
493  u16 errcode;
494  u16 datahandle;
495 
496  tty = tty_port_tty_get(&mp->port);
497  if (!tty)
498  return;
499 
500  if (mp->ttyoutstop) {
501  pr_debug("capi: send: tty stopped\n");
502  tty_kref_put(tty);
503  return;
504  }
505 
506  while (1) {
507  spin_lock_bh(&mp->outlock);
508  skb = __skb_dequeue(&mp->outqueue);
509  if (!skb) {
510  spin_unlock_bh(&mp->outlock);
511  break;
512  }
513  len = (u16)skb->len;
514  mp->outbytes -= len;
515  spin_unlock_bh(&mp->outlock);
516 
517  datahandle = atomic_inc_return(&mp->datahandle);
519  memset(skb->data, 0, CAPI_DATA_B3_REQ_LEN);
520  capimsg_setu16(skb->data, 0, CAPI_DATA_B3_REQ_LEN);
521  capimsg_setu16(skb->data, 2, mp->ap->applid);
522  capimsg_setu8 (skb->data, 4, CAPI_DATA_B3);
523  capimsg_setu8 (skb->data, 5, CAPI_REQ);
524  capimsg_setu16(skb->data, 6, atomic_inc_return(&mp->msgid));
525  capimsg_setu32(skb->data, 8, mp->ncci); /* NCCI */
526  capimsg_setu32(skb->data, 12, (u32)(long)skb->data);/* Data32 */
527  capimsg_setu16(skb->data, 16, len); /* Data length */
528  capimsg_setu16(skb->data, 18, datahandle);
529  capimsg_setu16(skb->data, 20, 0); /* Flags */
530 
531  if (capiminor_add_ack(mp, datahandle) < 0) {
533 
534  spin_lock_bh(&mp->outlock);
535  __skb_queue_head(&mp->outqueue, skb);
536  mp->outbytes += len;
537  spin_unlock_bh(&mp->outlock);
538 
539  break;
540  }
541  errcode = capi20_put_message(mp->ap, skb);
542  if (errcode == CAPI_NOERROR) {
543  pr_debug("capi: DATA_B3_REQ %u len=%u\n",
544  datahandle, len);
545  continue;
546  }
547  capiminor_del_ack(mp, datahandle);
548 
549  if (errcode == CAPI_SENDQUEUEFULL) {
551 
552  spin_lock_bh(&mp->outlock);
553  __skb_queue_head(&mp->outqueue, skb);
554  mp->outbytes += len;
555  spin_unlock_bh(&mp->outlock);
556 
557  break;
558  }
559 
560  /* ups, drop packet */
561  printk(KERN_ERR "capi: put_message = %x\n", errcode);
562  kfree_skb(skb);
563  }
564  tty_kref_put(tty);
565 }
566 
567 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
568 /* -------- function called by lower level -------------------------- */
569 
570 static void capi_recv_message(struct capi20_appl *ap, struct sk_buff *skb)
571 {
572  struct capidev *cdev = ap->private;
573 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
574  struct tty_struct *tty;
575  struct capiminor *mp;
576  u16 datahandle;
577  struct capincci *np;
578 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
579 
580  mutex_lock(&cdev->lock);
581 
582  if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_CONF) {
583  u16 info = CAPIMSG_U16(skb->data, 12); // Info field
584  if ((info & 0xff00) == 0)
585  capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
586  }
587  if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_IND)
588  capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
589 
590  if (CAPIMSG_COMMAND(skb->data) != CAPI_DATA_B3) {
591  skb_queue_tail(&cdev->recvqueue, skb);
593  goto unlock_out;
594  }
595 
596 #ifndef CONFIG_ISDN_CAPI_MIDDLEWARE
597  skb_queue_tail(&cdev->recvqueue, skb);
599 
600 #else /* CONFIG_ISDN_CAPI_MIDDLEWARE */
601 
602  np = capincci_find(cdev, CAPIMSG_CONTROL(skb->data));
603  if (!np) {
604  printk(KERN_ERR "BUG: capi_signal: ncci not found\n");
605  skb_queue_tail(&cdev->recvqueue, skb);
607  goto unlock_out;
608  }
609 
610  mp = np->minorp;
611  if (!mp) {
612  skb_queue_tail(&cdev->recvqueue, skb);
614  goto unlock_out;
615  }
616  if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_IND) {
617  datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN + 4 + 4 + 2);
618  pr_debug("capi_signal: DATA_B3_IND %u len=%d\n",
619  datahandle, skb->len-CAPIMSG_LEN(skb->data));
620  skb_queue_tail(&mp->inqueue, skb);
621 
622  handle_minor_recv(mp);
623 
624  } else if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_CONF) {
625 
626  datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN + 4);
627  pr_debug("capi_signal: DATA_B3_CONF %u 0x%x\n",
628  datahandle,
629  CAPIMSG_U16(skb->data, CAPIMSG_BASELEN + 4 + 2));
630  kfree_skb(skb);
631  capiminor_del_ack(mp, datahandle);
632  tty = tty_port_tty_get(&mp->port);
633  if (tty) {
634  tty_wakeup(tty);
635  tty_kref_put(tty);
636  }
637  handle_minor_send(mp);
638 
639  } else {
640  /* ups, let capi application handle it :-) */
641  skb_queue_tail(&cdev->recvqueue, skb);
643  }
644 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
645 
646 unlock_out:
647  mutex_unlock(&cdev->lock);
648 }
649 
650 /* -------- file_operations for capidev ----------------------------- */
651 
652 static ssize_t
653 capi_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
654 {
655  struct capidev *cdev = file->private_data;
656  struct sk_buff *skb;
657  size_t copied;
658  int err;
659 
660  if (!cdev->ap.applid)
661  return -ENODEV;
662 
663  skb = skb_dequeue(&cdev->recvqueue);
664  if (!skb) {
665  if (file->f_flags & O_NONBLOCK)
666  return -EAGAIN;
668  (skb = skb_dequeue(&cdev->recvqueue)));
669  if (err)
670  return err;
671  }
672  if (skb->len > count) {
673  skb_queue_head(&cdev->recvqueue, skb);
674  return -EMSGSIZE;
675  }
676  if (copy_to_user(buf, skb->data, skb->len)) {
677  skb_queue_head(&cdev->recvqueue, skb);
678  return -EFAULT;
679  }
680  copied = skb->len;
681 
682  kfree_skb(skb);
683 
684  return copied;
685 }
686 
687 static ssize_t
688 capi_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
689 {
690  struct capidev *cdev = file->private_data;
691  struct sk_buff *skb;
692  u16 mlen;
693 
694  if (!cdev->ap.applid)
695  return -ENODEV;
696 
697  skb = alloc_skb(count, GFP_USER);
698  if (!skb)
699  return -ENOMEM;
700 
701  if (copy_from_user(skb_put(skb, count), buf, count)) {
702  kfree_skb(skb);
703  return -EFAULT;
704  }
705  mlen = CAPIMSG_LEN(skb->data);
706  if (CAPIMSG_CMD(skb->data) == CAPI_DATA_B3_REQ) {
707  if ((size_t)(mlen + CAPIMSG_DATALEN(skb->data)) != count) {
708  kfree_skb(skb);
709  return -EINVAL;
710  }
711  } else {
712  if (mlen != count) {
713  kfree_skb(skb);
714  return -EINVAL;
715  }
716  }
717  CAPIMSG_SETAPPID(skb->data, cdev->ap.applid);
718 
720  mutex_lock(&cdev->lock);
721  capincci_free(cdev, CAPIMSG_NCCI(skb->data));
722  mutex_unlock(&cdev->lock);
723  }
724 
725  cdev->errcode = capi20_put_message(&cdev->ap, skb);
726 
727  if (cdev->errcode) {
728  kfree_skb(skb);
729  return -EIO;
730  }
731  return count;
732 }
733 
734 static unsigned int
735 capi_poll(struct file *file, poll_table *wait)
736 {
737  struct capidev *cdev = file->private_data;
738  unsigned int mask = 0;
739 
740  if (!cdev->ap.applid)
741  return POLLERR;
742 
743  poll_wait(file, &(cdev->recvwait), wait);
744  mask = POLLOUT | POLLWRNORM;
745  if (!skb_queue_empty(&cdev->recvqueue))
746  mask |= POLLIN | POLLRDNORM;
747  return mask;
748 }
749 
750 static int
751 capi_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
752 {
753  struct capidev *cdev = file->private_data;
755  int retval = -EINVAL;
756  void __user *argp = (void __user *)arg;
757 
758  switch (cmd) {
759  case CAPI_REGISTER:
760  mutex_lock(&cdev->lock);
761 
762  if (cdev->ap.applid) {
763  retval = -EEXIST;
764  goto register_out;
765  }
766  if (copy_from_user(&cdev->ap.rparam, argp,
767  sizeof(struct capi_register_params))) {
768  retval = -EFAULT;
769  goto register_out;
770  }
771  cdev->ap.private = cdev;
772  cdev->ap.recv_message = capi_recv_message;
773  cdev->errcode = capi20_register(&cdev->ap);
774  retval = (int)cdev->ap.applid;
775  if (cdev->errcode) {
776  cdev->ap.applid = 0;
777  retval = -EIO;
778  }
779 
780 register_out:
781  mutex_unlock(&cdev->lock);
782  return retval;
783 
784  case CAPI_GET_VERSION:
785  if (copy_from_user(&data.contr, argp,
786  sizeof(data.contr)))
787  return -EFAULT;
788  cdev->errcode = capi20_get_version(data.contr, &data.version);
789  if (cdev->errcode)
790  return -EIO;
791  if (copy_to_user(argp, &data.version,
792  sizeof(data.version)))
793  return -EFAULT;
794  return 0;
795 
796  case CAPI_GET_SERIAL:
797  if (copy_from_user(&data.contr, argp,
798  sizeof(data.contr)))
799  return -EFAULT;
800  cdev->errcode = capi20_get_serial(data.contr, data.serial);
801  if (cdev->errcode)
802  return -EIO;
803  if (copy_to_user(argp, data.serial,
804  sizeof(data.serial)))
805  return -EFAULT;
806  return 0;
807 
808  case CAPI_GET_PROFILE:
809  if (copy_from_user(&data.contr, argp,
810  sizeof(data.contr)))
811  return -EFAULT;
812 
813  if (data.contr == 0) {
814  cdev->errcode = capi20_get_profile(data.contr, &data.profile);
815  if (cdev->errcode)
816  return -EIO;
817 
818  retval = copy_to_user(argp,
819  &data.profile.ncontroller,
820  sizeof(data.profile.ncontroller));
821 
822  } else {
823  cdev->errcode = capi20_get_profile(data.contr, &data.profile);
824  if (cdev->errcode)
825  return -EIO;
826 
827  retval = copy_to_user(argp, &data.profile,
828  sizeof(data.profile));
829  }
830  if (retval)
831  return -EFAULT;
832  return 0;
833 
835  if (copy_from_user(&data.contr, argp,
836  sizeof(data.contr)))
837  return -EFAULT;
839  if (cdev->errcode)
840  return -EIO;
841 
842  if (copy_to_user(argp, data.manufacturer,
843  sizeof(data.manufacturer)))
844  return -EFAULT;
845 
846  return 0;
847 
848  case CAPI_GET_ERRCODE:
849  data.errcode = cdev->errcode;
850  cdev->errcode = CAPI_NOERROR;
851  if (arg) {
852  if (copy_to_user(argp, &data.errcode,
853  sizeof(data.errcode)))
854  return -EFAULT;
855  }
856  return data.errcode;
857 
858  case CAPI_INSTALLED:
860  return 0;
861  return -ENXIO;
862 
863  case CAPI_MANUFACTURER_CMD: {
864  struct capi_manufacturer_cmd mcmd;
865  if (!capable(CAP_SYS_ADMIN))
866  return -EPERM;
867  if (copy_from_user(&mcmd, argp, sizeof(mcmd)))
868  return -EFAULT;
869  return capi20_manufacturer(mcmd.cmd, mcmd.data);
870  }
871  case CAPI_SET_FLAGS:
872  case CAPI_CLR_FLAGS: {
873  unsigned userflags;
874 
875  if (copy_from_user(&userflags, argp, sizeof(userflags)))
876  return -EFAULT;
877 
878  mutex_lock(&cdev->lock);
879  if (cmd == CAPI_SET_FLAGS)
880  cdev->userflags |= userflags;
881  else
882  cdev->userflags &= ~userflags;
883  mutex_unlock(&cdev->lock);
884  return 0;
885  }
886  case CAPI_GET_FLAGS:
887  if (copy_to_user(argp, &cdev->userflags,
888  sizeof(cdev->userflags)))
889  return -EFAULT;
890  return 0;
891 
892 #ifndef CONFIG_ISDN_CAPI_MIDDLEWARE
893  case CAPI_NCCI_OPENCOUNT:
894  return 0;
895 
896 #else /* CONFIG_ISDN_CAPI_MIDDLEWARE */
897  case CAPI_NCCI_OPENCOUNT: {
898  struct capincci *nccip;
899  unsigned ncci;
900  int count = 0;
901 
902  if (copy_from_user(&ncci, argp, sizeof(ncci)))
903  return -EFAULT;
904 
905  mutex_lock(&cdev->lock);
906  nccip = capincci_find(cdev, (u32)ncci);
907  if (nccip)
908  count = capincci_minor_opencount(nccip);
909  mutex_unlock(&cdev->lock);
910  return count;
911  }
912 
913  case CAPI_NCCI_GETUNIT: {
914  struct capincci *nccip;
915  struct capiminor *mp;
916  unsigned ncci;
917  int unit = -ESRCH;
918 
919  if (copy_from_user(&ncci, argp, sizeof(ncci)))
920  return -EFAULT;
921 
922  mutex_lock(&cdev->lock);
923  nccip = capincci_find(cdev, (u32)ncci);
924  if (nccip) {
925  mp = nccip->minorp;
926  if (mp)
927  unit = mp->minor;
928  }
929  mutex_unlock(&cdev->lock);
930  return unit;
931  }
932 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
933 
934  default:
935  return -EINVAL;
936  }
937 }
938 
939 static long
940 capi_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
941 {
942  int ret;
943 
944  mutex_lock(&capi_mutex);
945  ret = capi_ioctl(file, cmd, arg);
946  mutex_unlock(&capi_mutex);
947 
948  return ret;
949 }
950 
951 static int capi_open(struct inode *inode, struct file *file)
952 {
953  struct capidev *cdev;
954 
955  cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
956  if (!cdev)
957  return -ENOMEM;
958 
959  mutex_init(&cdev->lock);
960  skb_queue_head_init(&cdev->recvqueue);
962  INIT_LIST_HEAD(&cdev->nccis);
963  file->private_data = cdev;
964 
965  mutex_lock(&capidev_list_lock);
966  list_add_tail(&cdev->list, &capidev_list);
967  mutex_unlock(&capidev_list_lock);
968 
969  return nonseekable_open(inode, file);
970 }
971 
972 static int capi_release(struct inode *inode, struct file *file)
973 {
974  struct capidev *cdev = file->private_data;
975 
976  mutex_lock(&capidev_list_lock);
977  list_del(&cdev->list);
978  mutex_unlock(&capidev_list_lock);
979 
980  if (cdev->ap.applid)
981  capi20_release(&cdev->ap);
982  skb_queue_purge(&cdev->recvqueue);
983  capincci_free(cdev, 0xffffffff);
984 
985  kfree(cdev);
986  return 0;
987 }
988 
989 static const struct file_operations capi_fops =
990 {
991  .owner = THIS_MODULE,
992  .llseek = no_llseek,
993  .read = capi_read,
994  .write = capi_write,
995  .poll = capi_poll,
996  .unlocked_ioctl = capi_unlocked_ioctl,
997  .open = capi_open,
998  .release = capi_release,
999 };
1000 
1001 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1002 /* -------- tty_operations for capincci ----------------------------- */
1003 
1004 static int
1005 capinc_tty_install(struct tty_driver *driver, struct tty_struct *tty)
1006 {
1007  struct capiminor *mp = capiminor_get(tty->index);
1008  int ret = tty_standard_install(driver, tty);
1009 
1010  if (ret == 0)
1011  tty->driver_data = mp;
1012  else
1013  capiminor_put(mp);
1014  return ret;
1015 }
1016 
1017 static void capinc_tty_cleanup(struct tty_struct *tty)
1018 {
1019  struct capiminor *mp = tty->driver_data;
1020  tty->driver_data = NULL;
1021  capiminor_put(mp);
1022 }
1023 
1024 static int capinc_tty_open(struct tty_struct *tty, struct file *filp)
1025 {
1026  struct capiminor *mp = tty->driver_data;
1027  int err;
1028 
1029  err = tty_port_open(&mp->port, tty, filp);
1030  if (err)
1031  return err;
1032 
1033  handle_minor_recv(mp);
1034  return 0;
1035 }
1036 
1037 static void capinc_tty_close(struct tty_struct *tty, struct file *filp)
1038 {
1039  struct capiminor *mp = tty->driver_data;
1040 
1041  tty_port_close(&mp->port, tty, filp);
1042 }
1043 
1044 static int capinc_tty_write(struct tty_struct *tty,
1045  const unsigned char *buf, int count)
1046 {
1047  struct capiminor *mp = tty->driver_data;
1048  struct sk_buff *skb;
1049 
1050  pr_debug("capinc_tty_write(count=%d)\n", count);
1051 
1052  spin_lock_bh(&mp->outlock);
1053  skb = mp->outskb;
1054  if (skb) {
1055  mp->outskb = NULL;
1056  __skb_queue_tail(&mp->outqueue, skb);
1057  mp->outbytes += skb->len;
1058  }
1059 
1060  skb = alloc_skb(CAPI_DATA_B3_REQ_LEN + count, GFP_ATOMIC);
1061  if (!skb) {
1062  printk(KERN_ERR "capinc_tty_write: alloc_skb failed\n");
1063  spin_unlock_bh(&mp->outlock);
1064  return -ENOMEM;
1065  }
1066 
1067  skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
1068  memcpy(skb_put(skb, count), buf, count);
1069 
1070  __skb_queue_tail(&mp->outqueue, skb);
1071  mp->outbytes += skb->len;
1072  spin_unlock_bh(&mp->outlock);
1073 
1074  handle_minor_send(mp);
1075 
1076  return count;
1077 }
1078 
1079 static int capinc_tty_put_char(struct tty_struct *tty, unsigned char ch)
1080 {
1081  struct capiminor *mp = tty->driver_data;
1082  bool invoke_send = false;
1083  struct sk_buff *skb;
1084  int ret = 1;
1085 
1086  pr_debug("capinc_put_char(%u)\n", ch);
1087 
1088  spin_lock_bh(&mp->outlock);
1089  skb = mp->outskb;
1090  if (skb) {
1091  if (skb_tailroom(skb) > 0) {
1092  *(skb_put(skb, 1)) = ch;
1093  goto unlock_out;
1094  }
1095  mp->outskb = NULL;
1096  __skb_queue_tail(&mp->outqueue, skb);
1097  mp->outbytes += skb->len;
1098  invoke_send = true;
1099  }
1100 
1101  skb = alloc_skb(CAPI_DATA_B3_REQ_LEN + CAPI_MAX_BLKSIZE, GFP_ATOMIC);
1102  if (skb) {
1103  skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
1104  *(skb_put(skb, 1)) = ch;
1105  mp->outskb = skb;
1106  } else {
1107  printk(KERN_ERR "capinc_put_char: char %u lost\n", ch);
1108  ret = 0;
1109  }
1110 
1111 unlock_out:
1112  spin_unlock_bh(&mp->outlock);
1113 
1114  if (invoke_send)
1115  handle_minor_send(mp);
1116 
1117  return ret;
1118 }
1119 
1120 static void capinc_tty_flush_chars(struct tty_struct *tty)
1121 {
1122  struct capiminor *mp = tty->driver_data;
1123  struct sk_buff *skb;
1124 
1125  pr_debug("capinc_tty_flush_chars\n");
1126 
1127  spin_lock_bh(&mp->outlock);
1128  skb = mp->outskb;
1129  if (skb) {
1130  mp->outskb = NULL;
1131  __skb_queue_tail(&mp->outqueue, skb);
1132  mp->outbytes += skb->len;
1133  spin_unlock_bh(&mp->outlock);
1134 
1135  handle_minor_send(mp);
1136  } else
1137  spin_unlock_bh(&mp->outlock);
1138 
1139  handle_minor_recv(mp);
1140 }
1141 
1142 static int capinc_tty_write_room(struct tty_struct *tty)
1143 {
1144  struct capiminor *mp = tty->driver_data;
1145  int room;
1146 
1147  room = CAPINC_MAX_SENDQUEUE-skb_queue_len(&mp->outqueue);
1148  room *= CAPI_MAX_BLKSIZE;
1149  pr_debug("capinc_tty_write_room = %d\n", room);
1150  return room;
1151 }
1152 
1153 static int capinc_tty_chars_in_buffer(struct tty_struct *tty)
1154 {
1155  struct capiminor *mp = tty->driver_data;
1156 
1157  pr_debug("capinc_tty_chars_in_buffer = %d nack=%d sq=%d rq=%d\n",
1158  mp->outbytes, mp->nack,
1159  skb_queue_len(&mp->outqueue),
1160  skb_queue_len(&mp->inqueue));
1161  return mp->outbytes;
1162 }
1163 
1164 static int capinc_tty_ioctl(struct tty_struct *tty,
1165  unsigned int cmd, unsigned long arg)
1166 {
1167  return -ENOIOCTLCMD;
1168 }
1169 
1170 static void capinc_tty_set_termios(struct tty_struct *tty, struct ktermios *old)
1171 {
1172  pr_debug("capinc_tty_set_termios\n");
1173 }
1174 
1175 static void capinc_tty_throttle(struct tty_struct *tty)
1176 {
1177  struct capiminor *mp = tty->driver_data;
1178  pr_debug("capinc_tty_throttle\n");
1179  mp->ttyinstop = 1;
1180 }
1181 
1182 static void capinc_tty_unthrottle(struct tty_struct *tty)
1183 {
1184  struct capiminor *mp = tty->driver_data;
1185 
1186  pr_debug("capinc_tty_unthrottle\n");
1187  mp->ttyinstop = 0;
1188  handle_minor_recv(mp);
1189 }
1190 
1191 static void capinc_tty_stop(struct tty_struct *tty)
1192 {
1193  struct capiminor *mp = tty->driver_data;
1194 
1195  pr_debug("capinc_tty_stop\n");
1196  mp->ttyoutstop = 1;
1197 }
1198 
1199 static void capinc_tty_start(struct tty_struct *tty)
1200 {
1201  struct capiminor *mp = tty->driver_data;
1202 
1203  pr_debug("capinc_tty_start\n");
1204  mp->ttyoutstop = 0;
1205  handle_minor_send(mp);
1206 }
1207 
1208 static void capinc_tty_hangup(struct tty_struct *tty)
1209 {
1210  struct capiminor *mp = tty->driver_data;
1211 
1212  pr_debug("capinc_tty_hangup\n");
1213  tty_port_hangup(&mp->port);
1214 }
1215 
1216 static int capinc_tty_break_ctl(struct tty_struct *tty, int state)
1217 {
1218  pr_debug("capinc_tty_break_ctl(%d)\n", state);
1219  return 0;
1220 }
1221 
1222 static void capinc_tty_flush_buffer(struct tty_struct *tty)
1223 {
1224  pr_debug("capinc_tty_flush_buffer\n");
1225 }
1226 
1227 static void capinc_tty_set_ldisc(struct tty_struct *tty)
1228 {
1229  pr_debug("capinc_tty_set_ldisc\n");
1230 }
1231 
1232 static void capinc_tty_send_xchar(struct tty_struct *tty, char ch)
1233 {
1234  pr_debug("capinc_tty_send_xchar(%d)\n", ch);
1235 }
1236 
1237 static const struct tty_operations capinc_ops = {
1238  .open = capinc_tty_open,
1239  .close = capinc_tty_close,
1240  .write = capinc_tty_write,
1241  .put_char = capinc_tty_put_char,
1242  .flush_chars = capinc_tty_flush_chars,
1243  .write_room = capinc_tty_write_room,
1244  .chars_in_buffer = capinc_tty_chars_in_buffer,
1245  .ioctl = capinc_tty_ioctl,
1246  .set_termios = capinc_tty_set_termios,
1247  .throttle = capinc_tty_throttle,
1248  .unthrottle = capinc_tty_unthrottle,
1249  .stop = capinc_tty_stop,
1250  .start = capinc_tty_start,
1251  .hangup = capinc_tty_hangup,
1252  .break_ctl = capinc_tty_break_ctl,
1253  .flush_buffer = capinc_tty_flush_buffer,
1254  .set_ldisc = capinc_tty_set_ldisc,
1255  .send_xchar = capinc_tty_send_xchar,
1256  .install = capinc_tty_install,
1257  .cleanup = capinc_tty_cleanup,
1258 };
1259 
1260 static int __init capinc_tty_init(void)
1261 {
1262  struct tty_driver *drv;
1263  int err;
1264 
1265  if (capi_ttyminors > CAPINC_MAX_PORTS)
1266  capi_ttyminors = CAPINC_MAX_PORTS;
1267  if (capi_ttyminors <= 0)
1268  capi_ttyminors = CAPINC_NR_PORTS;
1269 
1270  capiminors = kzalloc(sizeof(struct capi_minor *) * capi_ttyminors,
1271  GFP_KERNEL);
1272  if (!capiminors)
1273  return -ENOMEM;
1274 
1275  drv = alloc_tty_driver(capi_ttyminors);
1276  if (!drv) {
1277  kfree(capiminors);
1278  return -ENOMEM;
1279  }
1280  drv->driver_name = "capi_nc";
1281  drv->name = "capi";
1282  drv->major = 0;
1283  drv->minor_start = 0;
1285  drv->subtype = SERIAL_TYPE_NORMAL;
1287  drv->init_termios.c_iflag = ICRNL;
1288  drv->init_termios.c_oflag = OPOST | ONLCR;
1289  drv->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1290  drv->init_termios.c_lflag = 0;
1291  drv->flags =
1294  tty_set_operations(drv, &capinc_ops);
1295 
1296  err = tty_register_driver(drv);
1297  if (err) {
1298  put_tty_driver(drv);
1299  kfree(capiminors);
1300  printk(KERN_ERR "Couldn't register capi_nc driver\n");
1301  return err;
1302  }
1303  capinc_tty_driver = drv;
1304  return 0;
1305 }
1306 
1307 static void __exit capinc_tty_exit(void)
1308 {
1309  tty_unregister_driver(capinc_tty_driver);
1310  put_tty_driver(capinc_tty_driver);
1311  kfree(capiminors);
1312 }
1313 
1314 #else /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
1315 
1316 static inline int capinc_tty_init(void)
1317 {
1318  return 0;
1319 }
1320 
1321 static inline void capinc_tty_exit(void) { }
1322 
1323 #endif /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
1324 
1325 /* -------- /proc functions ----------------------------------------- */
1326 
1327 /*
1328  * /proc/capi/capi20:
1329  * minor applid nrecvctlpkt nrecvdatapkt nsendctlpkt nsenddatapkt
1330  */
1331 static int capi20_proc_show(struct seq_file *m, void *v)
1332 {
1333  struct capidev *cdev;
1334  struct list_head *l;
1335 
1336  mutex_lock(&capidev_list_lock);
1337  list_for_each(l, &capidev_list) {
1338  cdev = list_entry(l, struct capidev, list);
1339  seq_printf(m, "0 %d %lu %lu %lu %lu\n",
1340  cdev->ap.applid,
1341  cdev->ap.nrecvctlpkt,
1342  cdev->ap.nrecvdatapkt,
1343  cdev->ap.nsentctlpkt,
1344  cdev->ap.nsentdatapkt);
1345  }
1346  mutex_unlock(&capidev_list_lock);
1347  return 0;
1348 }
1349 
1350 static int capi20_proc_open(struct inode *inode, struct file *file)
1351 {
1352  return single_open(file, capi20_proc_show, NULL);
1353 }
1354 
1355 static const struct file_operations capi20_proc_fops = {
1356  .owner = THIS_MODULE,
1357  .open = capi20_proc_open,
1358  .read = seq_read,
1359  .llseek = seq_lseek,
1360  .release = single_release,
1361 };
1362 
1363 /*
1364  * /proc/capi/capi20ncci:
1365  * applid ncci
1366  */
1367 static int capi20ncci_proc_show(struct seq_file *m, void *v)
1368 {
1369  struct capidev *cdev;
1370  struct capincci *np;
1371 
1372  mutex_lock(&capidev_list_lock);
1373  list_for_each_entry(cdev, &capidev_list, list) {
1374  mutex_lock(&cdev->lock);
1375  list_for_each_entry(np, &cdev->nccis, list)
1376  seq_printf(m, "%d 0x%x\n", cdev->ap.applid, np->ncci);
1377  mutex_unlock(&cdev->lock);
1378  }
1379  mutex_unlock(&capidev_list_lock);
1380  return 0;
1381 }
1382 
1383 static int capi20ncci_proc_open(struct inode *inode, struct file *file)
1384 {
1385  return single_open(file, capi20ncci_proc_show, NULL);
1386 }
1387 
1388 static const struct file_operations capi20ncci_proc_fops = {
1389  .owner = THIS_MODULE,
1390  .open = capi20ncci_proc_open,
1391  .read = seq_read,
1392  .llseek = seq_lseek,
1393  .release = single_release,
1394 };
1395 
1396 static void __init proc_init(void)
1397 {
1398  proc_create("capi/capi20", 0, NULL, &capi20_proc_fops);
1399  proc_create("capi/capi20ncci", 0, NULL, &capi20ncci_proc_fops);
1400 }
1401 
1402 static void __exit proc_exit(void)
1403 {
1404  remove_proc_entry("capi/capi20", NULL);
1405  remove_proc_entry("capi/capi20ncci", NULL);
1406 }
1407 
1408 /* -------- init function and module interface ---------------------- */
1409 
1410 
1411 static int __init capi_init(void)
1412 {
1413  const char *compileinfo;
1414  int major_ret;
1415 
1416  major_ret = register_chrdev(capi_major, "capi20", &capi_fops);
1417  if (major_ret < 0) {
1418  printk(KERN_ERR "capi20: unable to get major %d\n", capi_major);
1419  return major_ret;
1420  }
1421  capi_class = class_create(THIS_MODULE, "capi");
1422  if (IS_ERR(capi_class)) {
1423  unregister_chrdev(capi_major, "capi20");
1424  return PTR_ERR(capi_class);
1425  }
1426 
1427  device_create(capi_class, NULL, MKDEV(capi_major, 0), NULL, "capi");
1428 
1429  if (capinc_tty_init() < 0) {
1430  device_destroy(capi_class, MKDEV(capi_major, 0));
1431  class_destroy(capi_class);
1432  unregister_chrdev(capi_major, "capi20");
1433  return -ENOMEM;
1434  }
1435 
1436  proc_init();
1437 
1438 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1439  compileinfo = " (middleware)";
1440 #else
1441  compileinfo = " (no middleware)";
1442 #endif
1443  printk(KERN_NOTICE "CAPI 2.0 started up with major %d%s\n",
1444  capi_major, compileinfo);
1445 
1446  return 0;
1447 }
1448 
1449 static void __exit capi_exit(void)
1450 {
1451  proc_exit();
1452 
1453  device_destroy(capi_class, MKDEV(capi_major, 0));
1454  class_destroy(capi_class);
1455  unregister_chrdev(capi_major, "capi20");
1456 
1457  capinc_tty_exit();
1458 }
1459 
1460 module_init(capi_init);
1461 module_exit(capi_exit);