Merge branch 'bkl/procfs' of git://git.kernel.org/pub/scm/linux/kernel/git/frederic...
[linux-2.6-block.git] / drivers / s390 / net / smsgiucv.c
CommitLineData
1da177e4
LT
1/*
2 * IUCV special message driver
3 *
6a1d96dc
MS
4 * Copyright IBM Corp. 2003, 2009
5 *
1da177e4
LT
6 * Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com)
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2, or (at your option)
11 * any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23#include <linux/module.h>
24#include <linux/init.h>
25#include <linux/errno.h>
26#include <linux/device.h>
5a0e3ad6 27#include <linux/slab.h>
5da5e658 28#include <net/iucv/iucv.h>
1da177e4
LT
29#include <asm/cpcmd.h>
30#include <asm/ebcdic.h>
5da5e658 31#include "smsgiucv.h"
1da177e4
LT
32
33struct smsg_callback {
34 struct list_head list;
09003ed9 35 const char *prefix;
1da177e4 36 int len;
09003ed9 37 void (*callback)(const char *from, char *str);
1da177e4
LT
38};
39
40MODULE_AUTHOR
41 ("(C) 2003 IBM Corporation by Martin Schwidefsky (schwidefsky@de.ibm.com)");
42MODULE_DESCRIPTION ("Linux for S/390 IUCV special message driver");
43
5da5e658 44static struct iucv_path *smsg_path;
6a1d96dc
MS
45/* dummy device used as trigger for PM functions */
46static struct device *smsg_dev;
5da5e658 47
1da177e4 48static DEFINE_SPINLOCK(smsg_list_lock);
c11ca97e 49static LIST_HEAD(smsg_list);
1da177e4 50
5da5e658
MS
51static int smsg_path_pending(struct iucv_path *, u8 ipvmid[8], u8 ipuser[16]);
52static void smsg_message_pending(struct iucv_path *, struct iucv_message *);
53
54static struct iucv_handler smsg_handler = {
55 .path_pending = smsg_path_pending,
56 .message_pending = smsg_message_pending,
57};
58
59static int smsg_path_pending(struct iucv_path *path, u8 ipvmid[8],
60 u8 ipuser[16])
1da177e4 61{
5da5e658
MS
62 if (strncmp(ipvmid, "*MSG ", sizeof(ipvmid)) != 0)
63 return -EINVAL;
64 /* Path pending from *MSG. */
65 return iucv_path_accept(path, &smsg_handler, "SMSGIUCV ", NULL);
1da177e4
LT
66}
67
5da5e658
MS
68static void smsg_message_pending(struct iucv_path *path,
69 struct iucv_message *msg)
1da177e4
LT
70{
71 struct smsg_callback *cb;
5da5e658 72 unsigned char *buffer;
15439d74 73 unsigned char sender[9];
15439d74 74 int rc, i;
1da177e4 75
5da5e658
MS
76 buffer = kmalloc(msg->length + 1, GFP_ATOMIC | GFP_DMA);
77 if (!buffer) {
78 iucv_message_reject(path, msg);
1da177e4
LT
79 return;
80 }
5da5e658 81 rc = iucv_message_receive(path, msg, 0, buffer, msg->length, NULL);
1da177e4 82 if (rc == 0) {
5da5e658
MS
83 buffer[msg->length] = 0;
84 EBCASC(buffer, msg->length);
85 memcpy(sender, buffer, 8);
15439d74
MS
86 sender[8] = 0;
87 /* Remove trailing whitespace from the sender name. */
88 for (i = 7; i >= 0; i--) {
89 if (sender[i] != ' ' && sender[i] != '\t')
90 break;
91 sender[i] = 0;
92 }
1da177e4
LT
93 spin_lock(&smsg_list_lock);
94 list_for_each_entry(cb, &smsg_list, list)
5da5e658
MS
95 if (strncmp(buffer + 8, cb->prefix, cb->len) == 0) {
96 cb->callback(sender, buffer + 8);
1da177e4
LT
97 break;
98 }
99 spin_unlock(&smsg_list_lock);
100 }
5da5e658 101 kfree(buffer);
1da177e4
LT
102}
103
09003ed9
HB
104int smsg_register_callback(const char *prefix,
105 void (*callback)(const char *from, char *str))
1da177e4
LT
106{
107 struct smsg_callback *cb;
108
109 cb = kmalloc(sizeof(struct smsg_callback), GFP_KERNEL);
110 if (!cb)
111 return -ENOMEM;
112 cb->prefix = prefix;
113 cb->len = strlen(prefix);
114 cb->callback = callback;
5da5e658 115 spin_lock_bh(&smsg_list_lock);
1da177e4 116 list_add_tail(&cb->list, &smsg_list);
5da5e658 117 spin_unlock_bh(&smsg_list_lock);
1da177e4
LT
118 return 0;
119}
120
09003ed9
HB
121void smsg_unregister_callback(const char *prefix,
122 void (*callback)(const char *from,
123 char *str))
1da177e4
LT
124{
125 struct smsg_callback *cb, *tmp;
126
5da5e658 127 spin_lock_bh(&smsg_list_lock);
d2c993d8 128 cb = NULL;
1da177e4
LT
129 list_for_each_entry(tmp, &smsg_list, list)
130 if (tmp->callback == callback &&
131 strcmp(tmp->prefix, prefix) == 0) {
132 cb = tmp;
133 list_del(&cb->list);
134 break;
135 }
5da5e658 136 spin_unlock_bh(&smsg_list_lock);
1da177e4
LT
137 kfree(cb);
138}
139
6a1d96dc
MS
140static int smsg_pm_freeze(struct device *dev)
141{
142#ifdef CONFIG_PM_DEBUG
143 printk(KERN_WARNING "smsg_pm_freeze\n");
144#endif
145 if (smsg_path)
146 iucv_path_sever(smsg_path, NULL);
147 return 0;
148}
149
150static int smsg_pm_restore_thaw(struct device *dev)
151{
152 int rc;
153
154#ifdef CONFIG_PM_DEBUG
155 printk(KERN_WARNING "smsg_pm_restore_thaw\n");
156#endif
157 if (smsg_path) {
158 memset(smsg_path, 0, sizeof(*smsg_path));
159 smsg_path->msglim = 255;
160 smsg_path->flags = 0;
161 rc = iucv_path_connect(smsg_path, &smsg_handler, "*MSG ",
162 NULL, NULL, NULL);
8ca45667
MS
163#ifdef CONFIG_PM_DEBUG
164 if (rc)
165 printk(KERN_ERR
166 "iucv_path_connect returned with rc %i\n", rc);
167#endif
168 cpcmd("SET SMSG IUCV", NULL, 0, NULL);
6a1d96dc
MS
169 }
170 return 0;
171}
172
47145210 173static const struct dev_pm_ops smsg_pm_ops = {
6a1d96dc
MS
174 .freeze = smsg_pm_freeze,
175 .thaw = smsg_pm_restore_thaw,
176 .restore = smsg_pm_restore_thaw,
177};
178
5da5e658 179static struct device_driver smsg_driver = {
6a1d96dc 180 .owner = THIS_MODULE,
1ffaa640 181 .name = SMSGIUCV_DRV_NAME,
5da5e658 182 .bus = &iucv_bus,
6a1d96dc 183 .pm = &smsg_pm_ops,
5da5e658
MS
184};
185
186static void __exit smsg_exit(void)
1da177e4 187{
5da5e658 188 cpcmd("SET SMSG IUCV", NULL, 0, NULL);
6a1d96dc 189 device_unregister(smsg_dev);
5da5e658
MS
190 iucv_unregister(&smsg_handler, 1);
191 driver_unregister(&smsg_driver);
1da177e4
LT
192}
193
5da5e658 194static int __init smsg_init(void)
1da177e4 195{
1da177e4
LT
196 int rc;
197
0fc3ddd6
CB
198 if (!MACHINE_IS_VM) {
199 rc = -EPROTONOSUPPORT;
200 goto out;
201 }
1da177e4 202 rc = driver_register(&smsg_driver);
5da5e658
MS
203 if (rc != 0)
204 goto out;
205 rc = iucv_register(&smsg_handler, 1);
d5ddc809 206 if (rc)
5da5e658 207 goto out_driver;
5da5e658
MS
208 smsg_path = iucv_path_alloc(255, 0, GFP_KERNEL);
209 if (!smsg_path) {
210 rc = -ENOMEM;
211 goto out_register;
1da177e4 212 }
5da5e658
MS
213 rc = iucv_path_connect(smsg_path, &smsg_handler, "*MSG ",
214 NULL, NULL, NULL);
d5ddc809 215 if (rc)
6a1d96dc
MS
216 goto out_free_path;
217 smsg_dev = kzalloc(sizeof(struct device), GFP_KERNEL);
218 if (!smsg_dev) {
219 rc = -ENOMEM;
220 goto out_free_path;
221 }
222 dev_set_name(smsg_dev, "smsg_iucv");
223 smsg_dev->bus = &iucv_bus;
224 smsg_dev->parent = iucv_root;
225 smsg_dev->release = (void (*)(struct device *))kfree;
226 smsg_dev->driver = &smsg_driver;
227 rc = device_register(smsg_dev);
228 if (rc)
c6304933 229 goto out_put;
6a1d96dc 230
6b979de3 231 cpcmd("SET SMSG IUCV", NULL, 0, NULL);
1da177e4 232 return 0;
5da5e658 233
c6304933
SO
234out_put:
235 put_device(smsg_dev);
6a1d96dc 236out_free_path:
5da5e658 237 iucv_path_free(smsg_path);
6a1d96dc 238 smsg_path = NULL;
5da5e658
MS
239out_register:
240 iucv_unregister(&smsg_handler, 1);
241out_driver:
242 driver_unregister(&smsg_driver);
243out:
244 return rc;
1da177e4
LT
245}
246
247module_init(smsg_init);
248module_exit(smsg_exit);
249MODULE_LICENSE("GPL");
250
251EXPORT_SYMBOL(smsg_register_callback);
252EXPORT_SYMBOL(smsg_unregister_callback);