blob: 47bb47b485814497e12201b3e4dd2f0697859504 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * IUCV special message driver
3 *
Martin Schwidefsky5da5e652007-02-08 13:51:11 -08004 * Copyright 2003 IBM Deutschland Entwicklung GmbH, IBM Corporation
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * 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>
Martin Schwidefsky5da5e652007-02-08 13:51:11 -080026#include <net/iucv/iucv.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <asm/cpcmd.h>
28#include <asm/ebcdic.h>
Martin Schwidefsky5da5e652007-02-08 13:51:11 -080029#include "smsgiucv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31struct smsg_callback {
32 struct list_head list;
33 char *prefix;
34 int len;
Martin Schwidefsky15439d72005-05-01 08:58:58 -070035 void (*callback)(char *from, char *str);
Linus Torvalds1da177e2005-04-16 15:20:36 -070036};
37
38MODULE_AUTHOR
39 ("(C) 2003 IBM Corporation by Martin Schwidefsky (schwidefsky@de.ibm.com)");
40MODULE_DESCRIPTION ("Linux for S/390 IUCV special message driver");
41
Martin Schwidefsky5da5e652007-02-08 13:51:11 -080042static struct iucv_path *smsg_path;
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044static DEFINE_SPINLOCK(smsg_list_lock);
45static struct list_head smsg_list = LIST_HEAD_INIT(smsg_list);
46
Martin Schwidefsky5da5e652007-02-08 13:51:11 -080047static int smsg_path_pending(struct iucv_path *, u8 ipvmid[8], u8 ipuser[16]);
48static void smsg_message_pending(struct iucv_path *, struct iucv_message *);
49
50static struct iucv_handler smsg_handler = {
51 .path_pending = smsg_path_pending,
52 .message_pending = smsg_message_pending,
53};
54
55static int smsg_path_pending(struct iucv_path *path, u8 ipvmid[8],
56 u8 ipuser[16])
Linus Torvalds1da177e2005-04-16 15:20:36 -070057{
Martin Schwidefsky5da5e652007-02-08 13:51:11 -080058 if (strncmp(ipvmid, "*MSG ", sizeof(ipvmid)) != 0)
59 return -EINVAL;
60 /* Path pending from *MSG. */
61 return iucv_path_accept(path, &smsg_handler, "SMSGIUCV ", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062}
63
Martin Schwidefsky5da5e652007-02-08 13:51:11 -080064static void smsg_message_pending(struct iucv_path *path,
65 struct iucv_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066{
67 struct smsg_callback *cb;
Martin Schwidefsky5da5e652007-02-08 13:51:11 -080068 unsigned char *buffer;
Martin Schwidefsky15439d72005-05-01 08:58:58 -070069 unsigned char sender[9];
Martin Schwidefsky15439d72005-05-01 08:58:58 -070070 int rc, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Martin Schwidefsky5da5e652007-02-08 13:51:11 -080072 buffer = kmalloc(msg->length + 1, GFP_ATOMIC | GFP_DMA);
73 if (!buffer) {
74 iucv_message_reject(path, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 return;
76 }
Martin Schwidefsky5da5e652007-02-08 13:51:11 -080077 rc = iucv_message_receive(path, msg, 0, buffer, msg->length, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 if (rc == 0) {
Martin Schwidefsky5da5e652007-02-08 13:51:11 -080079 buffer[msg->length] = 0;
80 EBCASC(buffer, msg->length);
81 memcpy(sender, buffer, 8);
Martin Schwidefsky15439d72005-05-01 08:58:58 -070082 sender[8] = 0;
83 /* Remove trailing whitespace from the sender name. */
84 for (i = 7; i >= 0; i--) {
85 if (sender[i] != ' ' && sender[i] != '\t')
86 break;
87 sender[i] = 0;
88 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 spin_lock(&smsg_list_lock);
90 list_for_each_entry(cb, &smsg_list, list)
Martin Schwidefsky5da5e652007-02-08 13:51:11 -080091 if (strncmp(buffer + 8, cb->prefix, cb->len) == 0) {
92 cb->callback(sender, buffer + 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 break;
94 }
95 spin_unlock(&smsg_list_lock);
96 }
Martin Schwidefsky5da5e652007-02-08 13:51:11 -080097 kfree(buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098}
99
Martin Schwidefsky5da5e652007-02-08 13:51:11 -0800100int smsg_register_callback(char *prefix,
101 void (*callback)(char *from, char *str))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102{
103 struct smsg_callback *cb;
104
105 cb = kmalloc(sizeof(struct smsg_callback), GFP_KERNEL);
106 if (!cb)
107 return -ENOMEM;
108 cb->prefix = prefix;
109 cb->len = strlen(prefix);
110 cb->callback = callback;
Martin Schwidefsky5da5e652007-02-08 13:51:11 -0800111 spin_lock_bh(&smsg_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 list_add_tail(&cb->list, &smsg_list);
Martin Schwidefsky5da5e652007-02-08 13:51:11 -0800113 spin_unlock_bh(&smsg_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 return 0;
115}
116
Martin Schwidefsky5da5e652007-02-08 13:51:11 -0800117void smsg_unregister_callback(char *prefix,
118 void (*callback)(char *from, char *str))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119{
120 struct smsg_callback *cb, *tmp;
121
Martin Schwidefsky5da5e652007-02-08 13:51:11 -0800122 spin_lock_bh(&smsg_list_lock);
Heiko Carstensd2c993d2006-07-12 16:41:55 +0200123 cb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 list_for_each_entry(tmp, &smsg_list, list)
125 if (tmp->callback == callback &&
126 strcmp(tmp->prefix, prefix) == 0) {
127 cb = tmp;
128 list_del(&cb->list);
129 break;
130 }
Martin Schwidefsky5da5e652007-02-08 13:51:11 -0800131 spin_unlock_bh(&smsg_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 kfree(cb);
133}
134
Martin Schwidefsky5da5e652007-02-08 13:51:11 -0800135static struct device_driver smsg_driver = {
136 .name = "SMSGIUCV",
137 .bus = &iucv_bus,
138};
139
140static void __exit smsg_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141{
Martin Schwidefsky5da5e652007-02-08 13:51:11 -0800142 cpcmd("SET SMSG IUCV", NULL, 0, NULL);
143 iucv_unregister(&smsg_handler, 1);
144 driver_unregister(&smsg_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145}
146
Martin Schwidefsky5da5e652007-02-08 13:51:11 -0800147static int __init smsg_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 int rc;
150
Christian Borntraeger0fc3ddd2007-11-05 11:10:08 +0100151 if (!MACHINE_IS_VM) {
152 rc = -EPROTONOSUPPORT;
153 goto out;
154 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 rc = driver_register(&smsg_driver);
Martin Schwidefsky5da5e652007-02-08 13:51:11 -0800156 if (rc != 0)
157 goto out;
158 rc = iucv_register(&smsg_handler, 1);
159 if (rc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 printk(KERN_ERR "SMSGIUCV: failed to register to iucv");
Martin Schwidefsky5da5e652007-02-08 13:51:11 -0800161 rc = -EIO; /* better errno ? */
162 goto out_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 }
Martin Schwidefsky5da5e652007-02-08 13:51:11 -0800164 smsg_path = iucv_path_alloc(255, 0, GFP_KERNEL);
165 if (!smsg_path) {
166 rc = -ENOMEM;
167 goto out_register;
168 }
169 rc = iucv_path_connect(smsg_path, &smsg_handler, "*MSG ",
170 NULL, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 if (rc) {
172 printk(KERN_ERR "SMSGIUCV: failed to connect to *MSG");
Martin Schwidefsky5da5e652007-02-08 13:51:11 -0800173 rc = -EIO; /* better errno ? */
174 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 }
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700176 cpcmd("SET SMSG IUCV", NULL, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 return 0;
Martin Schwidefsky5da5e652007-02-08 13:51:11 -0800178
179out_free:
180 iucv_path_free(smsg_path);
181out_register:
182 iucv_unregister(&smsg_handler, 1);
183out_driver:
184 driver_unregister(&smsg_driver);
185out:
186 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187}
188
189module_init(smsg_init);
190module_exit(smsg_exit);
191MODULE_LICENSE("GPL");
192
193EXPORT_SYMBOL(smsg_register_callback);
194EXPORT_SYMBOL(smsg_unregister_callback);