12 #include <linux/module.h>
13 #include <linux/errno.h>
14 #include <linux/kernel.h>
16 #include <linux/sched.h>
17 #include <linux/slab.h>
18 #include <linux/fcntl.h>
20 #include <linux/signal.h>
24 #include <linux/wait.h>
25 #include <linux/tty.h>
26 #include <linux/netdevice.h>
27 #include <linux/ppp_defs.h>
32 #include <linux/poll.h>
34 #include <linux/kernelcapi.h>
36 #include <linux/device.h>
48 static struct class *capi_class;
49 static int capi_major = 68;
53 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
54 #define CAPINC_NR_PORTS 32
55 #define CAPINC_MAX_PORTS 256
57 static int capi_ttyminors = CAPINC_NR_PORTS;
64 #define CAPINC_MAX_RECVQUEUE 10
65 #define CAPINC_MAX_SENDQUEUE 10
66 #define CAPI_MAX_BLKSIZE 2048
110 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
134 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
143 static int capiminor_add_ack(
struct capiminor *
mp,
u16 datahandle)
153 INIT_LIST_HEAD(&n->
list);
161 static int capiminor_del_ack(
struct capiminor *mp,
u16 datahandle)
179 static void capiminor_del_all_ack(
struct capiminor *mp)
207 kref_init(&mp->
kref);
214 skb_queue_head_init(&mp->
inqueue);
219 mp->
port.ops = &capiminor_port_ops;
222 spin_lock(&capiminors_lock);
223 for (minor = 0; minor < capi_ttyminors; minor++)
224 if (!capiminors[minor]) {
225 capiminors[minor] =
mp;
228 spin_unlock(&capiminors_lock);
230 if (minor == capi_ttyminors) {
245 spin_lock(&capiminors_lock);
246 capiminors[minor] =
NULL;
247 spin_unlock(&capiminors_lock);
254 static void capiminor_destroy(
struct kref *
kref)
261 capiminor_del_all_ack(mp);
265 static struct capiminor *capiminor_get(
unsigned int minor)
269 spin_lock(&capiminors_lock);
270 mp = capiminors[
minor];
273 spin_unlock(&capiminors_lock);
278 static inline void capiminor_put(
struct capiminor *mp)
280 kref_put(&mp->
kref, capiminor_destroy);
283 static void capiminor_free(
struct capiminor *mp)
287 spin_lock(&capiminors_lock);
289 spin_unlock(&capiminors_lock);
299 np->minorp = capiminor_alloc(&cdev->
ap, np->
ncci);
302 static void capincci_free_minor(
struct capincci *np)
318 static inline unsigned int capincci_minor_opencount(
struct capincci *np)
321 unsigned int count = 0;
338 static inline void capincci_free_minor(
struct capincci *np) { }
352 capincci_alloc_minor(cdev, np);
359 static void capincci_free(
struct capidev *cdev,
u32 ncci)
364 if (ncci == 0xffffffff || np->ncci == ncci) {
365 capincci_free_minor(np);
371 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
377 if (np->ncci == ncci)
393 capimsg_setu16(s, 2, mp->ap->applid);
397 capimsg_setu32(s, 8, mp->ncci);
398 capimsg_setu16(s, 12, datahandle);
414 pr_debug(
"capi: currently no receiver\n");
426 if (ld->
ops->receive_buf == NULL) {
427 pr_debug(
"capi: ldisc has no receive_buf function\n");
432 pr_debug(
"capi: recv tty throttled\n");
441 nskb = gen_data_b3_resp_for(mp, skb);
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);
477 static void handle_minor_recv(
struct capiminor *mp)
482 if (handle_recv_skb(mp, skb) < 0) {
488 static void handle_minor_send(
struct capiminor *mp)
501 pr_debug(
"capi: send: tty stopped\n");
521 capimsg_setu16(skb->
data, 2, mp->
ap->applid);
525 capimsg_setu32(skb->
data, 8, mp->
ncci);
526 capimsg_setu32(skb->
data, 12, (
u32)(
long)skb->
data);
527 capimsg_setu16(skb->
data, 16, len);
528 capimsg_setu16(skb->
data, 18, datahandle);
529 capimsg_setu16(skb->
data, 20, 0);
531 if (capiminor_add_ack(mp, datahandle) < 0) {
535 __skb_queue_head(&mp->
outqueue, skb);
543 pr_debug(
"capi: DATA_B3_REQ %u len=%u\n",
547 capiminor_del_ack(mp, datahandle);
553 __skb_queue_head(&mp->
outqueue, skb);
573 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
584 if ((info & 0xff00) == 0)
596 #ifndef CONFIG_ISDN_CAPI_MIDDLEWARE
618 pr_debug(
"capi_signal: DATA_B3_IND %u len=%d\n",
622 handle_minor_recv(mp);
627 pr_debug(
"capi_signal: DATA_B3_CONF %u 0x%x\n",
631 capiminor_del_ack(mp, datahandle);
637 handle_minor_send(mp);
653 capi_read(
struct file *
file,
char __user *
buf,
size_t count, loff_t *ppos)
660 if (!cdev->
ap.applid)
672 if (skb->
len > count) {
688 capi_write(
struct file *file,
const char __user *buf,
size_t count, loff_t *ppos)
694 if (!cdev->
ap.applid)
738 unsigned int mask = 0;
740 if (!cdev->
ap.applid)
743 poll_wait(file, &(cdev->
recvwait), wait);
751 capi_ioctl(
struct file *file,
unsigned int cmd,
unsigned long arg)
762 if (cdev->
ap.applid) {
772 cdev->
ap.recv_message = capi_recv_message;
774 retval = (
int)cdev->
ap.applid;
813 if (data.
contr == 0) {
892 #ifndef CONFIG_ISDN_CAPI_MIDDLEWARE
906 nccip = capincci_find(cdev, (
u32)ncci);
908 count = capincci_minor_opencount(nccip);
923 nccip = capincci_find(cdev, (
u32)ncci);
940 capi_unlocked_ioctl(
struct file *file,
unsigned int cmd,
unsigned long arg)
945 ret = capi_ioctl(file, cmd, arg);
951 static int capi_open(
struct inode *
inode,
struct file *file)
962 INIT_LIST_HEAD(&cdev->
nccis);
972 static int capi_release(
struct inode *inode,
struct file *file)
983 capincci_free(cdev, 0xffffffff);
996 .unlocked_ioctl = capi_unlocked_ioctl,
998 .release = capi_release,
1001 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1017 static void capinc_tty_cleanup(
struct tty_struct *tty)
1024 static int capinc_tty_open(
struct tty_struct *tty,
struct file *filp)
1033 handle_minor_recv(mp);
1037 static void capinc_tty_close(
struct tty_struct *tty,
struct file *filp)
1044 static int capinc_tty_write(
struct tty_struct *tty,
1045 const unsigned char *buf,
int count)
1050 pr_debug(
"capinc_tty_write(count=%d)\n", count);
1056 __skb_queue_tail(&mp->
outqueue, skb);
1070 __skb_queue_tail(&mp->
outqueue, skb);
1074 handle_minor_send(mp);
1079 static int capinc_tty_put_char(
struct tty_struct *tty,
unsigned char ch)
1082 bool invoke_send =
false;
1086 pr_debug(
"capinc_put_char(%u)\n", ch);
1091 if (skb_tailroom(skb) > 0) {
1096 __skb_queue_tail(&mp->
outqueue, skb);
1115 handle_minor_send(mp);
1120 static void capinc_tty_flush_chars(
struct tty_struct *tty)
1125 pr_debug(
"capinc_tty_flush_chars\n");
1131 __skb_queue_tail(&mp->
outqueue, skb);
1135 handle_minor_send(mp);
1139 handle_minor_recv(mp);
1142 static int capinc_tty_write_room(
struct tty_struct *tty)
1149 pr_debug(
"capinc_tty_write_room = %d\n", room);
1153 static int capinc_tty_chars_in_buffer(
struct tty_struct *tty)
1157 pr_debug(
"capinc_tty_chars_in_buffer = %d nack=%d sq=%d rq=%d\n",
1164 static int capinc_tty_ioctl(
struct tty_struct *tty,
1165 unsigned int cmd,
unsigned long arg)
1172 pr_debug(
"capinc_tty_set_termios\n");
1175 static void capinc_tty_throttle(
struct tty_struct *tty)
1182 static void capinc_tty_unthrottle(
struct tty_struct *tty)
1186 pr_debug(
"capinc_tty_unthrottle\n");
1188 handle_minor_recv(mp);
1191 static void capinc_tty_stop(
struct tty_struct *tty)
1199 static void capinc_tty_start(
struct tty_struct *tty)
1205 handle_minor_send(mp);
1208 static void capinc_tty_hangup(
struct tty_struct *tty)
1218 pr_debug(
"capinc_tty_break_ctl(%d)\n", state);
1222 static void capinc_tty_flush_buffer(
struct tty_struct *tty)
1224 pr_debug(
"capinc_tty_flush_buffer\n");
1227 static void capinc_tty_set_ldisc(
struct tty_struct *tty)
1229 pr_debug(
"capinc_tty_set_ldisc\n");
1232 static void capinc_tty_send_xchar(
struct tty_struct *tty,
char ch)
1234 pr_debug(
"capinc_tty_send_xchar(%d)\n", ch);
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,
1260 static int __init capinc_tty_init(
void)
1265 if (capi_ttyminors > CAPINC_MAX_PORTS)
1266 capi_ttyminors = CAPINC_MAX_PORTS;
1267 if (capi_ttyminors <= 0)
1268 capi_ttyminors = CAPINC_NR_PORTS;
1270 capiminors = kzalloc(
sizeof(
struct capi_minor *) * capi_ttyminors,
1275 drv = alloc_tty_driver(capi_ttyminors);
1303 capinc_tty_driver = drv;
1307 static void __exit capinc_tty_exit(
void)
1316 static inline int capinc_tty_init(
void)
1321 static inline void capinc_tty_exit(
void) { }
1331 static int capi20_proc_show(
struct seq_file *
m,
void *
v)
1341 cdev->
ap.nrecvctlpkt,
1342 cdev->
ap.nrecvdatapkt,
1343 cdev->
ap.nsentctlpkt,
1344 cdev->
ap.nsentdatapkt);
1350 static int capi20_proc_open(
struct inode *inode,
struct file *file)
1357 .open = capi20_proc_open,
1367 static int capi20ncci_proc_show(
struct seq_file *
m,
void *
v)
1383 static
int capi20ncci_proc_open(
struct inode *inode,
struct file *file)
1385 return single_open(file, capi20ncci_proc_show, NULL);
1390 .open = capi20ncci_proc_open,
1396 static void __init proc_init(
void)
1398 proc_create(
"capi/capi20", 0, NULL, &capi20_proc_fops);
1399 proc_create(
"capi/capi20ncci", 0, NULL, &capi20ncci_proc_fops);
1402 static void __exit proc_exit(
void)
1411 static int __init capi_init(
void)
1413 const char *compileinfo;
1416 major_ret = register_chrdev(capi_major,
"capi20", &capi_fops);
1417 if (major_ret < 0) {
1422 if (IS_ERR(capi_class)) {
1423 unregister_chrdev(capi_major,
"capi20");
1424 return PTR_ERR(capi_class);
1429 if (capinc_tty_init() < 0) {
1432 unregister_chrdev(capi_major,
"capi20");
1438 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1439 compileinfo =
" (middleware)";
1441 compileinfo =
" (no middleware)";
1444 capi_major, compileinfo);
1449 static void __exit capi_exit(
void)
1455 unregister_chrdev(capi_major,
"capi20");