Loading...
1/*
2 * IUCV special message driver
3 *
4 * Copyright IBM Corp. 2003, 2009
5 *
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>
27#include <linux/slab.h>
28#include <net/iucv/iucv.h>
29#include <asm/cpcmd.h>
30#include <asm/ebcdic.h>
31#include "smsgiucv.h"
32
33struct smsg_callback {
34 struct list_head list;
35 const char *prefix;
36 int len;
37 void (*callback)(const char *from, char *str);
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
44static struct iucv_path *smsg_path;
45/* dummy device used as trigger for PM functions */
46static struct device *smsg_dev;
47
48static DEFINE_SPINLOCK(smsg_list_lock);
49static LIST_HEAD(smsg_list);
50static int iucv_path_connected;
51
52static int smsg_path_pending(struct iucv_path *, u8 ipvmid[8], u8 ipuser[16]);
53static void smsg_message_pending(struct iucv_path *, struct iucv_message *);
54
55static struct iucv_handler smsg_handler = {
56 .path_pending = smsg_path_pending,
57 .message_pending = smsg_message_pending,
58};
59
60static int smsg_path_pending(struct iucv_path *path, u8 ipvmid[8],
61 u8 ipuser[16])
62{
63 if (strncmp(ipvmid, "*MSG ", 8) != 0)
64 return -EINVAL;
65 /* Path pending from *MSG. */
66 return iucv_path_accept(path, &smsg_handler, "SMSGIUCV ", NULL);
67}
68
69static void smsg_message_pending(struct iucv_path *path,
70 struct iucv_message *msg)
71{
72 struct smsg_callback *cb;
73 unsigned char *buffer;
74 unsigned char sender[9];
75 int rc, i;
76
77 buffer = kmalloc(msg->length + 1, GFP_ATOMIC | GFP_DMA);
78 if (!buffer) {
79 iucv_message_reject(path, msg);
80 return;
81 }
82 rc = iucv_message_receive(path, msg, 0, buffer, msg->length, NULL);
83 if (rc == 0) {
84 buffer[msg->length] = 0;
85 EBCASC(buffer, msg->length);
86 memcpy(sender, buffer, 8);
87 sender[8] = 0;
88 /* Remove trailing whitespace from the sender name. */
89 for (i = 7; i >= 0; i--) {
90 if (sender[i] != ' ' && sender[i] != '\t')
91 break;
92 sender[i] = 0;
93 }
94 spin_lock(&smsg_list_lock);
95 list_for_each_entry(cb, &smsg_list, list)
96 if (strncmp(buffer + 8, cb->prefix, cb->len) == 0) {
97 cb->callback(sender, buffer + 8);
98 break;
99 }
100 spin_unlock(&smsg_list_lock);
101 }
102 kfree(buffer);
103}
104
105int smsg_register_callback(const char *prefix,
106 void (*callback)(const char *from, char *str))
107{
108 struct smsg_callback *cb;
109
110 cb = kmalloc(sizeof(struct smsg_callback), GFP_KERNEL);
111 if (!cb)
112 return -ENOMEM;
113 cb->prefix = prefix;
114 cb->len = strlen(prefix);
115 cb->callback = callback;
116 spin_lock_bh(&smsg_list_lock);
117 list_add_tail(&cb->list, &smsg_list);
118 spin_unlock_bh(&smsg_list_lock);
119 return 0;
120}
121
122void smsg_unregister_callback(const char *prefix,
123 void (*callback)(const char *from,
124 char *str))
125{
126 struct smsg_callback *cb, *tmp;
127
128 spin_lock_bh(&smsg_list_lock);
129 cb = NULL;
130 list_for_each_entry(tmp, &smsg_list, list)
131 if (tmp->callback == callback &&
132 strcmp(tmp->prefix, prefix) == 0) {
133 cb = tmp;
134 list_del(&cb->list);
135 break;
136 }
137 spin_unlock_bh(&smsg_list_lock);
138 kfree(cb);
139}
140
141static int smsg_pm_freeze(struct device *dev)
142{
143#ifdef CONFIG_PM_DEBUG
144 printk(KERN_WARNING "smsg_pm_freeze\n");
145#endif
146 if (smsg_path && iucv_path_connected) {
147 iucv_path_sever(smsg_path, NULL);
148 iucv_path_connected = 0;
149 }
150 return 0;
151}
152
153static int smsg_pm_restore_thaw(struct device *dev)
154{
155 int rc;
156
157#ifdef CONFIG_PM_DEBUG
158 printk(KERN_WARNING "smsg_pm_restore_thaw\n");
159#endif
160 if (smsg_path && !iucv_path_connected) {
161 memset(smsg_path, 0, sizeof(*smsg_path));
162 smsg_path->msglim = 255;
163 smsg_path->flags = 0;
164 rc = iucv_path_connect(smsg_path, &smsg_handler, "*MSG ",
165 NULL, NULL, NULL);
166#ifdef CONFIG_PM_DEBUG
167 if (rc)
168 printk(KERN_ERR
169 "iucv_path_connect returned with rc %i\n", rc);
170#endif
171 if (!rc)
172 iucv_path_connected = 1;
173 cpcmd("SET SMSG IUCV", NULL, 0, NULL);
174 }
175 return 0;
176}
177
178static const struct dev_pm_ops smsg_pm_ops = {
179 .freeze = smsg_pm_freeze,
180 .thaw = smsg_pm_restore_thaw,
181 .restore = smsg_pm_restore_thaw,
182};
183
184static struct device_driver smsg_driver = {
185 .owner = THIS_MODULE,
186 .name = SMSGIUCV_DRV_NAME,
187 .bus = &iucv_bus,
188 .pm = &smsg_pm_ops,
189};
190
191static void __exit smsg_exit(void)
192{
193 cpcmd("SET SMSG IUCV", NULL, 0, NULL);
194 device_unregister(smsg_dev);
195 iucv_unregister(&smsg_handler, 1);
196 driver_unregister(&smsg_driver);
197}
198
199static int __init smsg_init(void)
200{
201 int rc;
202
203 if (!MACHINE_IS_VM) {
204 rc = -EPROTONOSUPPORT;
205 goto out;
206 }
207 rc = driver_register(&smsg_driver);
208 if (rc != 0)
209 goto out;
210 rc = iucv_register(&smsg_handler, 1);
211 if (rc)
212 goto out_driver;
213 smsg_path = iucv_path_alloc(255, 0, GFP_KERNEL);
214 if (!smsg_path) {
215 rc = -ENOMEM;
216 goto out_register;
217 }
218 rc = iucv_path_connect(smsg_path, &smsg_handler, "*MSG ",
219 NULL, NULL, NULL);
220 if (rc)
221 goto out_free_path;
222 else
223 iucv_path_connected = 1;
224 smsg_dev = kzalloc(sizeof(struct device), GFP_KERNEL);
225 if (!smsg_dev) {
226 rc = -ENOMEM;
227 goto out_free_path;
228 }
229 dev_set_name(smsg_dev, "smsg_iucv");
230 smsg_dev->bus = &iucv_bus;
231 smsg_dev->parent = iucv_root;
232 smsg_dev->release = (void (*)(struct device *))kfree;
233 smsg_dev->driver = &smsg_driver;
234 rc = device_register(smsg_dev);
235 if (rc)
236 goto out_put;
237
238 cpcmd("SET SMSG IUCV", NULL, 0, NULL);
239 return 0;
240
241out_put:
242 put_device(smsg_dev);
243out_free_path:
244 iucv_path_free(smsg_path);
245 smsg_path = NULL;
246out_register:
247 iucv_unregister(&smsg_handler, 1);
248out_driver:
249 driver_unregister(&smsg_driver);
250out:
251 return rc;
252}
253
254module_init(smsg_init);
255module_exit(smsg_exit);
256MODULE_LICENSE("GPL");
257
258EXPORT_SYMBOL(smsg_register_callback);
259EXPORT_SYMBOL(smsg_unregister_callback);
1// SPDX-License-Identifier: GPL-2.0+
2/*
3 * IUCV special message driver
4 *
5 * Copyright IBM Corp. 2003, 2009
6 *
7 * Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com)
8 */
9
10#include <linux/module.h>
11#include <linux/init.h>
12#include <linux/errno.h>
13#include <linux/device.h>
14#include <linux/slab.h>
15#include <net/iucv/iucv.h>
16#include <asm/cpcmd.h>
17#include <asm/ebcdic.h>
18#include "smsgiucv.h"
19
20struct smsg_callback {
21 struct list_head list;
22 const char *prefix;
23 int len;
24 void (*callback)(const char *from, char *str);
25};
26
27MODULE_AUTHOR
28 ("(C) 2003 IBM Corporation by Martin Schwidefsky (schwidefsky@de.ibm.com)");
29MODULE_DESCRIPTION ("Linux for S/390 IUCV special message driver");
30
31static struct iucv_path *smsg_path;
32
33static DEFINE_SPINLOCK(smsg_list_lock);
34static LIST_HEAD(smsg_list);
35
36static int smsg_path_pending(struct iucv_path *, u8 *, u8 *);
37static void smsg_message_pending(struct iucv_path *, struct iucv_message *);
38
39static struct iucv_handler smsg_handler = {
40 .path_pending = smsg_path_pending,
41 .message_pending = smsg_message_pending,
42};
43
44static int smsg_path_pending(struct iucv_path *path, u8 *ipvmid, u8 *ipuser)
45{
46 if (strncmp(ipvmid, "*MSG ", 8) != 0)
47 return -EINVAL;
48 /* Path pending from *MSG. */
49 return iucv_path_accept(path, &smsg_handler, "SMSGIUCV ", NULL);
50}
51
52static void smsg_message_pending(struct iucv_path *path,
53 struct iucv_message *msg)
54{
55 struct smsg_callback *cb;
56 unsigned char *buffer;
57 unsigned char sender[9];
58 int rc, i;
59
60 buffer = kmalloc(msg->length + 1, GFP_ATOMIC | GFP_DMA);
61 if (!buffer) {
62 iucv_message_reject(path, msg);
63 return;
64 }
65 rc = iucv_message_receive(path, msg, 0, buffer, msg->length, NULL);
66 if (rc == 0) {
67 buffer[msg->length] = 0;
68 EBCASC(buffer, msg->length);
69 memcpy(sender, buffer, 8);
70 sender[8] = 0;
71 /* Remove trailing whitespace from the sender name. */
72 for (i = 7; i >= 0; i--) {
73 if (sender[i] != ' ' && sender[i] != '\t')
74 break;
75 sender[i] = 0;
76 }
77 spin_lock(&smsg_list_lock);
78 list_for_each_entry(cb, &smsg_list, list)
79 if (strncmp(buffer + 8, cb->prefix, cb->len) == 0) {
80 cb->callback(sender, buffer + 8);
81 break;
82 }
83 spin_unlock(&smsg_list_lock);
84 }
85 kfree(buffer);
86}
87
88int smsg_register_callback(const char *prefix,
89 void (*callback)(const char *from, char *str))
90{
91 struct smsg_callback *cb;
92
93 cb = kmalloc(sizeof(struct smsg_callback), GFP_KERNEL);
94 if (!cb)
95 return -ENOMEM;
96 cb->prefix = prefix;
97 cb->len = strlen(prefix);
98 cb->callback = callback;
99 spin_lock_bh(&smsg_list_lock);
100 list_add_tail(&cb->list, &smsg_list);
101 spin_unlock_bh(&smsg_list_lock);
102 return 0;
103}
104
105void smsg_unregister_callback(const char *prefix,
106 void (*callback)(const char *from,
107 char *str))
108{
109 struct smsg_callback *cb, *tmp;
110
111 spin_lock_bh(&smsg_list_lock);
112 cb = NULL;
113 list_for_each_entry(tmp, &smsg_list, list)
114 if (tmp->callback == callback &&
115 strcmp(tmp->prefix, prefix) == 0) {
116 cb = tmp;
117 list_del(&cb->list);
118 break;
119 }
120 spin_unlock_bh(&smsg_list_lock);
121 kfree(cb);
122}
123
124static struct device_driver smsg_driver = {
125 .owner = THIS_MODULE,
126 .name = SMSGIUCV_DRV_NAME,
127 .bus = &iucv_bus,
128};
129
130static void __exit smsg_exit(void)
131{
132 cpcmd("SET SMSG OFF", NULL, 0, NULL);
133 iucv_unregister(&smsg_handler, 1);
134 driver_unregister(&smsg_driver);
135}
136
137static int __init smsg_init(void)
138{
139 int rc;
140
141 if (!MACHINE_IS_VM) {
142 rc = -EPROTONOSUPPORT;
143 goto out;
144 }
145 rc = driver_register(&smsg_driver);
146 if (rc != 0)
147 goto out;
148 rc = iucv_register(&smsg_handler, 1);
149 if (rc)
150 goto out_driver;
151 smsg_path = iucv_path_alloc(255, 0, GFP_KERNEL);
152 if (!smsg_path) {
153 rc = -ENOMEM;
154 goto out_register;
155 }
156 rc = iucv_path_connect(smsg_path, &smsg_handler, "*MSG ",
157 NULL, NULL, NULL);
158 if (rc)
159 goto out_free_path;
160
161 cpcmd("SET SMSG IUCV", NULL, 0, NULL);
162 return 0;
163
164out_free_path:
165 iucv_path_free(smsg_path);
166 smsg_path = NULL;
167out_register:
168 iucv_unregister(&smsg_handler, 1);
169out_driver:
170 driver_unregister(&smsg_driver);
171out:
172 return rc;
173}
174
175module_init(smsg_init);
176module_exit(smsg_exit);
177MODULE_LICENSE("GPL");
178
179EXPORT_SYMBOL(smsg_register_callback);
180EXPORT_SYMBOL(smsg_unregister_callback);