ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / s390 / net / smsgiucv.c
1 /*
2  * IUCV special message driver
3  *
4  * Copyright (C) 2003 IBM Deutschland Entwicklung GmbH, IBM Corporation
5  * Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com)
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2, or (at your option)
10  * any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21
22 #include <linux/module.h>
23 #include <linux/init.h>
24 #include <linux/errno.h>
25 #include <linux/device.h>
26 #include <asm/cpcmd.h>
27 #include <asm/ebcdic.h>
28
29 #include "iucv.h"
30
31 struct smsg_callback {
32         struct list_head list;
33         char *prefix;
34         int len;
35         void (*callback)(char *str);
36 };
37
38 MODULE_AUTHOR
39    ("(C) 2003 IBM Corporation by Martin Schwidefsky (schwidefsky@de.ibm.com)");
40 MODULE_DESCRIPTION ("Linux for S/390 IUCV special message driver");
41
42 static iucv_handle_t smsg_handle;
43 static unsigned short smsg_pathid;
44 static spinlock_t smsg_list_lock = SPIN_LOCK_UNLOCKED;
45 static struct list_head smsg_list = LIST_HEAD_INIT(smsg_list);
46
47 static void
48 smsg_connection_complete(iucv_ConnectionComplete *eib, void *pgm_data)
49 {
50 }
51
52
53 static void
54 smsg_message_pending(iucv_MessagePending *eib, void *pgm_data)
55 {
56         struct smsg_callback *cb;
57         unsigned char *msg;
58         unsigned short len;
59         int rc;
60
61         len = eib->ln1msg2.ipbfln1f;
62         msg = kmalloc(len + 1, GFP_ATOMIC|GFP_DMA);
63         if (!msg) {
64                 iucv_reject(eib->ippathid, eib->ipmsgid, eib->iptrgcls);
65                 return;
66         }
67         rc = iucv_receive(eib->ippathid, eib->ipmsgid, eib->iptrgcls,
68                           msg, len, 0, 0, 0);
69         if (rc == 0) {
70                 msg[len] = 0;
71                 EBCASC(msg, len);
72                 spin_lock(&smsg_list_lock);
73                 list_for_each_entry(cb, &smsg_list, list)
74                         if (strncmp(msg + 8, cb->prefix, cb->len) == 0) {
75                                 cb->callback(msg + 8);
76                                 break;
77                         }
78                 spin_unlock(&smsg_list_lock);
79         }
80         kfree(msg);
81 }
82
83 static iucv_interrupt_ops_t smsg_ops = {
84         .ConnectionComplete = smsg_connection_complete,
85         .MessagePending     = smsg_message_pending,
86 };
87
88 static struct device_driver smsg_driver = {
89         .name = "SMSGIUCV",
90         .bus  = &iucv_bus,
91 };
92
93 int
94 smsg_register_callback(char *prefix, void (*callback)(char *str))
95 {
96         struct smsg_callback *cb;
97
98         cb = kmalloc(sizeof(struct smsg_callback), GFP_KERNEL);
99         if (!cb)
100                 return -ENOMEM;
101         cb->prefix = prefix;
102         cb->len = strlen(prefix);
103         cb->callback = callback;
104         spin_lock(&smsg_list_lock);
105         list_add_tail(&cb->list, &smsg_list);
106         spin_unlock(&smsg_list_lock);
107         return 0;
108 }
109
110 void
111 smsg_unregister_callback(char *prefix, void (*callback)(char *str))
112 {
113         struct smsg_callback *cb, *tmp;
114
115         spin_lock(&smsg_list_lock);
116         cb = 0;
117         list_for_each_entry(tmp, &smsg_list, list)
118                 if (tmp->callback == callback &&
119                     strcmp(tmp->prefix, prefix) == 0) {
120                         cb = tmp;
121                         list_del(&cb->list);
122                         break;
123                 }
124         spin_unlock(&smsg_list_lock);
125         kfree(cb);
126 }
127
128 static void __exit
129 smsg_exit(void)
130 {
131         if (smsg_handle > 0) {
132                 cpcmd("SET SMSG OFF", 0, 0);
133                 iucv_sever(smsg_pathid, 0);
134                 iucv_unregister_program(smsg_handle);
135                 driver_unregister(&smsg_driver);
136         }
137         return;
138 }
139
140 static int __init
141 smsg_init(void)
142 {
143         static unsigned char pgmmask[24] = {
144                 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
145                 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
146                 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
147         };
148         int rc;
149
150         rc = driver_register(&smsg_driver);
151         if (rc != 0) {
152                 printk(KERN_ERR "SMSGIUCV: failed to register driver.\n");
153                 return rc;
154         }
155         smsg_handle = iucv_register_program("SMSGIUCV        ", "*MSG    ",
156                                             pgmmask, &smsg_ops, 0);
157         if (!smsg_handle) {
158                 printk(KERN_ERR "SMSGIUCV: failed to register to iucv");
159                 driver_unregister(&smsg_driver);
160                 return -EIO;    /* better errno ? */
161         }
162         rc = iucv_connect (&smsg_pathid, 1, 0, "*MSG    ", 0, 0, 0, 0,
163                            smsg_handle, 0);
164         if (rc) {
165                 printk(KERN_ERR "SMSGIUCV: failed to connect to *MSG");
166                 iucv_unregister_program(smsg_handle);
167                 driver_unregister(&smsg_driver);
168                 smsg_handle = 0;
169                 return -EIO;
170         }
171         cpcmd("SET SMSG IUCV", 0, 0);
172         return 0;
173 }
174
175 module_init(smsg_init);
176 module_exit(smsg_exit);
177 MODULE_LICENSE("GPL");
178
179 EXPORT_SYMBOL(smsg_register_callback);
180 EXPORT_SYMBOL(smsg_unregister_callback);