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