smsgiucv.c 6.1 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3
/*
 * IUCV special message driver
 *
4 5
 * Copyright IBM Corp. 2003, 2009
 *
L
Linus Torvalds 已提交
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
 * Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com)
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <linux/module.h>
#include <linux/init.h>
#include <linux/errno.h>
#include <linux/device.h>
27
#include <net/iucv/iucv.h>
L
Linus Torvalds 已提交
28 29
#include <asm/cpcmd.h>
#include <asm/ebcdic.h>
30
#include "smsgiucv.h"
L
Linus Torvalds 已提交
31 32 33

struct smsg_callback {
	struct list_head list;
34
	const char *prefix;
L
Linus Torvalds 已提交
35
	int len;
36
	void (*callback)(const char *from, char *str);
L
Linus Torvalds 已提交
37 38 39 40 41 42
};

MODULE_AUTHOR
   ("(C) 2003 IBM Corporation by Martin Schwidefsky (schwidefsky@de.ibm.com)");
MODULE_DESCRIPTION ("Linux for S/390 IUCV special message driver");

43
static struct iucv_path *smsg_path;
44 45
/* dummy device used as trigger for PM functions */
static struct device *smsg_dev;
46

L
Linus Torvalds 已提交
47
static DEFINE_SPINLOCK(smsg_list_lock);
48
static LIST_HEAD(smsg_list);
L
Linus Torvalds 已提交
49

50 51 52 53 54 55 56 57 58 59
static int smsg_path_pending(struct iucv_path *, u8 ipvmid[8], u8 ipuser[16]);
static void smsg_message_pending(struct iucv_path *, struct iucv_message *);

static struct iucv_handler smsg_handler = {
	.path_pending	 = smsg_path_pending,
	.message_pending = smsg_message_pending,
};

static int smsg_path_pending(struct iucv_path *path, u8 ipvmid[8],
			     u8 ipuser[16])
L
Linus Torvalds 已提交
60
{
61 62 63 64
	if (strncmp(ipvmid, "*MSG    ", sizeof(ipvmid)) != 0)
		return -EINVAL;
	/* Path pending from *MSG. */
	return iucv_path_accept(path, &smsg_handler, "SMSGIUCV        ", NULL);
L
Linus Torvalds 已提交
65 66
}

67 68
static void smsg_message_pending(struct iucv_path *path,
				 struct iucv_message *msg)
L
Linus Torvalds 已提交
69 70
{
	struct smsg_callback *cb;
71
	unsigned char *buffer;
72 73
	unsigned char sender[9];
	int rc, i;
L
Linus Torvalds 已提交
74

75 76 77
	buffer = kmalloc(msg->length + 1, GFP_ATOMIC | GFP_DMA);
	if (!buffer) {
		iucv_message_reject(path, msg);
L
Linus Torvalds 已提交
78 79
		return;
	}
80
	rc = iucv_message_receive(path, msg, 0, buffer, msg->length, NULL);
L
Linus Torvalds 已提交
81
	if (rc == 0) {
82 83 84
		buffer[msg->length] = 0;
		EBCASC(buffer, msg->length);
		memcpy(sender, buffer, 8);
85 86 87 88 89 90 91
		sender[8] = 0;
		/* Remove trailing whitespace from the sender name. */
		for (i = 7; i >= 0; i--) {
			if (sender[i] != ' ' && sender[i] != '\t')
				break;
			sender[i] = 0;
		}
L
Linus Torvalds 已提交
92 93
		spin_lock(&smsg_list_lock);
		list_for_each_entry(cb, &smsg_list, list)
94 95
			if (strncmp(buffer + 8, cb->prefix, cb->len) == 0) {
				cb->callback(sender, buffer + 8);
L
Linus Torvalds 已提交
96 97 98 99
				break;
			}
		spin_unlock(&smsg_list_lock);
	}
100
	kfree(buffer);
L
Linus Torvalds 已提交
101 102
}

103 104
int smsg_register_callback(const char *prefix,
			   void (*callback)(const char *from, char *str))
L
Linus Torvalds 已提交
105 106 107 108 109 110 111 112 113
{
	struct smsg_callback *cb;

	cb = kmalloc(sizeof(struct smsg_callback), GFP_KERNEL);
	if (!cb)
		return -ENOMEM;
	cb->prefix = prefix;
	cb->len = strlen(prefix);
	cb->callback = callback;
114
	spin_lock_bh(&smsg_list_lock);
L
Linus Torvalds 已提交
115
	list_add_tail(&cb->list, &smsg_list);
116
	spin_unlock_bh(&smsg_list_lock);
L
Linus Torvalds 已提交
117 118 119
	return 0;
}

120 121 122
void smsg_unregister_callback(const char *prefix,
			      void (*callback)(const char *from,
					       char *str))
L
Linus Torvalds 已提交
123 124 125
{
	struct smsg_callback *cb, *tmp;

126
	spin_lock_bh(&smsg_list_lock);
H
Heiko Carstens 已提交
127
	cb = NULL;
L
Linus Torvalds 已提交
128 129 130 131 132 133 134
	list_for_each_entry(tmp, &smsg_list, list)
		if (tmp->callback == callback &&
		    strcmp(tmp->prefix, prefix) == 0) {
			cb = tmp;
			list_del(&cb->list);
			break;
		}
135
	spin_unlock_bh(&smsg_list_lock);
L
Linus Torvalds 已提交
136 137 138
	kfree(cb);
}

139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
static int smsg_pm_freeze(struct device *dev)
{
#ifdef CONFIG_PM_DEBUG
	printk(KERN_WARNING "smsg_pm_freeze\n");
#endif
	if (smsg_path)
		iucv_path_sever(smsg_path, NULL);
	return 0;
}

static int smsg_pm_restore_thaw(struct device *dev)
{
	int rc;

#ifdef CONFIG_PM_DEBUG
	printk(KERN_WARNING "smsg_pm_restore_thaw\n");
#endif
	if (smsg_path) {
		memset(smsg_path, 0, sizeof(*smsg_path));
		smsg_path->msglim = 255;
		smsg_path->flags = 0;
		rc = iucv_path_connect(smsg_path, &smsg_handler, "*MSG    ",
				       NULL, NULL, NULL);
M
Martin Schwidefsky 已提交
162 163 164 165 166 167
#ifdef CONFIG_PM_DEBUG
		if (rc)
			printk(KERN_ERR
			       "iucv_path_connect returned with rc %i\n", rc);
#endif
		cpcmd("SET SMSG IUCV", NULL, 0, NULL);
168 169 170 171
	}
	return 0;
}

172
static const struct dev_pm_ops smsg_pm_ops = {
173 174 175 176 177
	.freeze = smsg_pm_freeze,
	.thaw = smsg_pm_restore_thaw,
	.restore = smsg_pm_restore_thaw,
};

178
static struct device_driver smsg_driver = {
179
	.owner = THIS_MODULE,
180
	.name = SMSGIUCV_DRV_NAME,
181
	.bus  = &iucv_bus,
182
	.pm = &smsg_pm_ops,
183 184 185
};

static void __exit smsg_exit(void)
L
Linus Torvalds 已提交
186
{
187
	cpcmd("SET SMSG IUCV", NULL, 0, NULL);
188
	device_unregister(smsg_dev);
189 190
	iucv_unregister(&smsg_handler, 1);
	driver_unregister(&smsg_driver);
L
Linus Torvalds 已提交
191 192
}

193
static int __init smsg_init(void)
L
Linus Torvalds 已提交
194 195 196
{
	int rc;

197 198 199 200
	if (!MACHINE_IS_VM) {
		rc = -EPROTONOSUPPORT;
		goto out;
	}
L
Linus Torvalds 已提交
201
	rc = driver_register(&smsg_driver);
202 203 204
	if (rc != 0)
		goto out;
	rc = iucv_register(&smsg_handler, 1);
205
	if (rc)
206 207 208 209 210
		goto out_driver;
	smsg_path = iucv_path_alloc(255, 0, GFP_KERNEL);
	if (!smsg_path) {
		rc = -ENOMEM;
		goto out_register;
L
Linus Torvalds 已提交
211
	}
212 213
	rc = iucv_path_connect(smsg_path, &smsg_handler, "*MSG    ",
			       NULL, NULL, NULL);
214
	if (rc)
215 216 217 218 219 220 221 222 223 224 225 226 227
		goto out_free_path;
	smsg_dev = kzalloc(sizeof(struct device), GFP_KERNEL);
	if (!smsg_dev) {
		rc = -ENOMEM;
		goto out_free_path;
	}
	dev_set_name(smsg_dev, "smsg_iucv");
	smsg_dev->bus = &iucv_bus;
	smsg_dev->parent = iucv_root;
	smsg_dev->release = (void (*)(struct device *))kfree;
	smsg_dev->driver = &smsg_driver;
	rc = device_register(smsg_dev);
	if (rc)
228
		goto out_put;
229

230
	cpcmd("SET SMSG IUCV", NULL, 0, NULL);
L
Linus Torvalds 已提交
231
	return 0;
232

233 234
out_put:
	put_device(smsg_dev);
235
out_free_path:
236
	iucv_path_free(smsg_path);
237
	smsg_path = NULL;
238 239 240 241 242 243
out_register:
	iucv_unregister(&smsg_handler, 1);
out_driver:
	driver_unregister(&smsg_driver);
out:
	return rc;
L
Linus Torvalds 已提交
244 245 246 247 248 249 250 251
}

module_init(smsg_init);
module_exit(smsg_exit);
MODULE_LICENSE("GPL");

EXPORT_SYMBOL(smsg_register_callback);
EXPORT_SYMBOL(smsg_unregister_callback);