]>
Commit | Line | Data |
---|---|---|
ab9953ff | 1 | // SPDX-License-Identifier: GPL-2.0+ |
1da177e4 LT |
2 | /* |
3 | * IUCV special message driver | |
4 | * | |
6a1d96dc MS |
5 | * Copyright IBM Corp. 2003, 2009 |
6 | * | |
1da177e4 | 7 | * Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com) |
1da177e4 LT |
8 | */ |
9 | ||
10 | #include <linux/module.h> | |
11 | #include <linux/init.h> | |
12 | #include <linux/errno.h> | |
13 | #include <linux/device.h> | |
5a0e3ad6 | 14 | #include <linux/slab.h> |
5da5e658 | 15 | #include <net/iucv/iucv.h> |
1da177e4 LT |
16 | #include <asm/cpcmd.h> |
17 | #include <asm/ebcdic.h> | |
5da5e658 | 18 | #include "smsgiucv.h" |
1da177e4 LT |
19 | |
20 | struct smsg_callback { | |
21 | struct list_head list; | |
09003ed9 | 22 | const char *prefix; |
1da177e4 | 23 | int len; |
09003ed9 | 24 | void (*callback)(const char *from, char *str); |
1da177e4 LT |
25 | }; |
26 | ||
27 | MODULE_AUTHOR | |
28 | ("(C) 2003 IBM Corporation by Martin Schwidefsky (schwidefsky@de.ibm.com)"); | |
29 | MODULE_DESCRIPTION ("Linux for S/390 IUCV special message driver"); | |
30 | ||
5da5e658 MS |
31 | static struct iucv_path *smsg_path; |
32 | ||
1da177e4 | 33 | static DEFINE_SPINLOCK(smsg_list_lock); |
c11ca97e | 34 | static LIST_HEAD(smsg_list); |
1da177e4 | 35 | |
91e60eb6 | 36 | static int smsg_path_pending(struct iucv_path *, u8 *, u8 *); |
5da5e658 MS |
37 | static void smsg_message_pending(struct iucv_path *, struct iucv_message *); |
38 | ||
39 | static struct iucv_handler smsg_handler = { | |
40 | .path_pending = smsg_path_pending, | |
41 | .message_pending = smsg_message_pending, | |
42 | }; | |
43 | ||
91e60eb6 | 44 | static int smsg_path_pending(struct iucv_path *path, u8 *ipvmid, u8 *ipuser) |
1da177e4 | 45 | { |
08b01832 | 46 | if (strncmp(ipvmid, "*MSG ", 8) != 0) |
5da5e658 MS |
47 | return -EINVAL; |
48 | /* Path pending from *MSG. */ | |
49 | return iucv_path_accept(path, &smsg_handler, "SMSGIUCV ", NULL); | |
1da177e4 LT |
50 | } |
51 | ||
5da5e658 MS |
52 | static void smsg_message_pending(struct iucv_path *path, |
53 | struct iucv_message *msg) | |
1da177e4 LT |
54 | { |
55 | struct smsg_callback *cb; | |
5da5e658 | 56 | unsigned char *buffer; |
15439d74 | 57 | unsigned char sender[9]; |
15439d74 | 58 | int rc, i; |
1da177e4 | 59 | |
5da5e658 MS |
60 | buffer = kmalloc(msg->length + 1, GFP_ATOMIC | GFP_DMA); |
61 | if (!buffer) { | |
62 | iucv_message_reject(path, msg); | |
1da177e4 LT |
63 | return; |
64 | } | |
5da5e658 | 65 | rc = iucv_message_receive(path, msg, 0, buffer, msg->length, NULL); |
1da177e4 | 66 | if (rc == 0) { |
5da5e658 MS |
67 | buffer[msg->length] = 0; |
68 | EBCASC(buffer, msg->length); | |
69 | memcpy(sender, buffer, 8); | |
15439d74 MS |
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 | } | |
1da177e4 LT |
77 | spin_lock(&smsg_list_lock); |
78 | list_for_each_entry(cb, &smsg_list, list) | |
5da5e658 MS |
79 | if (strncmp(buffer + 8, cb->prefix, cb->len) == 0) { |
80 | cb->callback(sender, buffer + 8); | |
1da177e4 LT |
81 | break; |
82 | } | |
83 | spin_unlock(&smsg_list_lock); | |
84 | } | |
5da5e658 | 85 | kfree(buffer); |
1da177e4 LT |
86 | } |
87 | ||
09003ed9 HB |
88 | int smsg_register_callback(const char *prefix, |
89 | void (*callback)(const char *from, char *str)) | |
1da177e4 LT |
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; | |
5da5e658 | 99 | spin_lock_bh(&smsg_list_lock); |
1da177e4 | 100 | list_add_tail(&cb->list, &smsg_list); |
5da5e658 | 101 | spin_unlock_bh(&smsg_list_lock); |
1da177e4 LT |
102 | return 0; |
103 | } | |
104 | ||
09003ed9 HB |
105 | void smsg_unregister_callback(const char *prefix, |
106 | void (*callback)(const char *from, | |
107 | char *str)) | |
1da177e4 LT |
108 | { |
109 | struct smsg_callback *cb, *tmp; | |
110 | ||
5da5e658 | 111 | spin_lock_bh(&smsg_list_lock); |
d2c993d8 | 112 | cb = NULL; |
1da177e4 LT |
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 | } | |
5da5e658 | 120 | spin_unlock_bh(&smsg_list_lock); |
1da177e4 LT |
121 | kfree(cb); |
122 | } | |
123 | ||
5da5e658 | 124 | static struct device_driver smsg_driver = { |
6a1d96dc | 125 | .owner = THIS_MODULE, |
1ffaa640 | 126 | .name = SMSGIUCV_DRV_NAME, |
5da5e658 MS |
127 | .bus = &iucv_bus, |
128 | }; | |
129 | ||
130 | static void __exit smsg_exit(void) | |
1da177e4 | 131 | { |
760dd0ee | 132 | cpcmd("SET SMSG OFF", NULL, 0, NULL); |
5da5e658 MS |
133 | iucv_unregister(&smsg_handler, 1); |
134 | driver_unregister(&smsg_driver); | |
1da177e4 LT |
135 | } |
136 | ||
5da5e658 | 137 | static int __init smsg_init(void) |
1da177e4 | 138 | { |
1da177e4 LT |
139 | int rc; |
140 | ||
0fc3ddd6 CB |
141 | if (!MACHINE_IS_VM) { |
142 | rc = -EPROTONOSUPPORT; | |
143 | goto out; | |
144 | } | |
1da177e4 | 145 | rc = driver_register(&smsg_driver); |
5da5e658 MS |
146 | if (rc != 0) |
147 | goto out; | |
148 | rc = iucv_register(&smsg_handler, 1); | |
d5ddc809 | 149 | if (rc) |
5da5e658 | 150 | goto out_driver; |
5da5e658 MS |
151 | smsg_path = iucv_path_alloc(255, 0, GFP_KERNEL); |
152 | if (!smsg_path) { | |
153 | rc = -ENOMEM; | |
154 | goto out_register; | |
1da177e4 | 155 | } |
5da5e658 MS |
156 | rc = iucv_path_connect(smsg_path, &smsg_handler, "*MSG ", |
157 | NULL, NULL, NULL); | |
d5ddc809 | 158 | if (rc) |
6a1d96dc | 159 | goto out_free_path; |
6a1d96dc | 160 | |
6b979de3 | 161 | cpcmd("SET SMSG IUCV", NULL, 0, NULL); |
1da177e4 | 162 | return 0; |
5da5e658 | 163 | |
6a1d96dc | 164 | out_free_path: |
5da5e658 | 165 | iucv_path_free(smsg_path); |
6a1d96dc | 166 | smsg_path = NULL; |
5da5e658 MS |
167 | out_register: |
168 | iucv_unregister(&smsg_handler, 1); | |
169 | out_driver: | |
170 | driver_unregister(&smsg_driver); | |
171 | out: | |
172 | return rc; | |
1da177e4 LT |
173 | } |
174 | ||
175 | module_init(smsg_init); | |
176 | module_exit(smsg_exit); | |
177 | MODULE_LICENSE("GPL"); | |
178 | ||
179 | EXPORT_SYMBOL(smsg_register_callback); | |
180 | EXPORT_SYMBOL(smsg_unregister_callback); |