1 // SPDX-License-Identifier: GPL-2.0+
3 * IUCV special message driver
5 * Copyright IBM Corp. 2003, 2009
7 * Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com)
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>
20 struct smsg_callback
{
21 struct list_head list
;
24 void (*callback
)(const char *from
, char *str
);
28 ("(C) 2003 IBM Corporation by Martin Schwidefsky (schwidefsky@de.ibm.com)");
29 MODULE_DESCRIPTION ("Linux for S/390 IUCV special message driver");
31 static struct iucv_path
*smsg_path
;
32 /* dummy device used as trigger for PM functions */
33 static struct device
*smsg_dev
;
35 static DEFINE_SPINLOCK(smsg_list_lock
);
36 static LIST_HEAD(smsg_list
);
37 static int iucv_path_connected
;
39 static int smsg_path_pending(struct iucv_path
*, u8
*, u8
*);
40 static void smsg_message_pending(struct iucv_path
*, struct iucv_message
*);
42 static struct iucv_handler smsg_handler
= {
43 .path_pending
= smsg_path_pending
,
44 .message_pending
= smsg_message_pending
,
47 static int smsg_path_pending(struct iucv_path
*path
, u8
*ipvmid
, u8
*ipuser
)
49 if (strncmp(ipvmid
, "*MSG ", 8) != 0)
51 /* Path pending from *MSG. */
52 return iucv_path_accept(path
, &smsg_handler
, "SMSGIUCV ", NULL
);
55 static void smsg_message_pending(struct iucv_path
*path
,
56 struct iucv_message
*msg
)
58 struct smsg_callback
*cb
;
59 unsigned char *buffer
;
60 unsigned char sender
[9];
63 buffer
= kmalloc(msg
->length
+ 1, GFP_ATOMIC
| GFP_DMA
);
65 iucv_message_reject(path
, msg
);
68 rc
= iucv_message_receive(path
, msg
, 0, buffer
, msg
->length
, NULL
);
70 buffer
[msg
->length
] = 0;
71 EBCASC(buffer
, msg
->length
);
72 memcpy(sender
, buffer
, 8);
74 /* Remove trailing whitespace from the sender name. */
75 for (i
= 7; i
>= 0; i
--) {
76 if (sender
[i
] != ' ' && sender
[i
] != '\t')
80 spin_lock(&smsg_list_lock
);
81 list_for_each_entry(cb
, &smsg_list
, list
)
82 if (strncmp(buffer
+ 8, cb
->prefix
, cb
->len
) == 0) {
83 cb
->callback(sender
, buffer
+ 8);
86 spin_unlock(&smsg_list_lock
);
91 int smsg_register_callback(const char *prefix
,
92 void (*callback
)(const char *from
, char *str
))
94 struct smsg_callback
*cb
;
96 cb
= kmalloc(sizeof(struct smsg_callback
), GFP_KERNEL
);
100 cb
->len
= strlen(prefix
);
101 cb
->callback
= callback
;
102 spin_lock_bh(&smsg_list_lock
);
103 list_add_tail(&cb
->list
, &smsg_list
);
104 spin_unlock_bh(&smsg_list_lock
);
108 void smsg_unregister_callback(const char *prefix
,
109 void (*callback
)(const char *from
,
112 struct smsg_callback
*cb
, *tmp
;
114 spin_lock_bh(&smsg_list_lock
);
116 list_for_each_entry(tmp
, &smsg_list
, list
)
117 if (tmp
->callback
== callback
&&
118 strcmp(tmp
->prefix
, prefix
) == 0) {
123 spin_unlock_bh(&smsg_list_lock
);
127 static int smsg_pm_freeze(struct device
*dev
)
129 #ifdef CONFIG_PM_DEBUG
130 printk(KERN_WARNING
"smsg_pm_freeze\n");
132 if (smsg_path
&& iucv_path_connected
) {
133 iucv_path_sever(smsg_path
, NULL
);
134 iucv_path_connected
= 0;
139 static int smsg_pm_restore_thaw(struct device
*dev
)
143 #ifdef CONFIG_PM_DEBUG
144 printk(KERN_WARNING
"smsg_pm_restore_thaw\n");
146 if (smsg_path
&& !iucv_path_connected
) {
147 memset(smsg_path
, 0, sizeof(*smsg_path
));
148 smsg_path
->msglim
= 255;
149 smsg_path
->flags
= 0;
150 rc
= iucv_path_connect(smsg_path
, &smsg_handler
, "*MSG ",
152 #ifdef CONFIG_PM_DEBUG
155 "iucv_path_connect returned with rc %i\n", rc
);
158 iucv_path_connected
= 1;
159 cpcmd("SET SMSG IUCV", NULL
, 0, NULL
);
164 static const struct dev_pm_ops smsg_pm_ops
= {
165 .freeze
= smsg_pm_freeze
,
166 .thaw
= smsg_pm_restore_thaw
,
167 .restore
= smsg_pm_restore_thaw
,
170 static struct device_driver smsg_driver
= {
171 .owner
= THIS_MODULE
,
172 .name
= SMSGIUCV_DRV_NAME
,
177 static void __exit
smsg_exit(void)
179 cpcmd("SET SMSG OFF", NULL
, 0, NULL
);
180 device_unregister(smsg_dev
);
181 iucv_unregister(&smsg_handler
, 1);
182 driver_unregister(&smsg_driver
);
185 static int __init
smsg_init(void)
189 if (!MACHINE_IS_VM
) {
190 rc
= -EPROTONOSUPPORT
;
193 rc
= driver_register(&smsg_driver
);
196 rc
= iucv_register(&smsg_handler
, 1);
199 smsg_path
= iucv_path_alloc(255, 0, GFP_KERNEL
);
204 rc
= iucv_path_connect(smsg_path
, &smsg_handler
, "*MSG ",
209 iucv_path_connected
= 1;
210 smsg_dev
= kzalloc(sizeof(struct device
), GFP_KERNEL
);
215 dev_set_name(smsg_dev
, "smsg_iucv");
216 smsg_dev
->bus
= &iucv_bus
;
217 smsg_dev
->parent
= iucv_root
;
218 smsg_dev
->release
= (void (*)(struct device
*))kfree
;
219 smsg_dev
->driver
= &smsg_driver
;
220 rc
= device_register(smsg_dev
);
224 cpcmd("SET SMSG IUCV", NULL
, 0, NULL
);
228 put_device(smsg_dev
);
230 iucv_path_free(smsg_path
);
233 iucv_unregister(&smsg_handler
, 1);
235 driver_unregister(&smsg_driver
);
240 module_init(smsg_init
);
241 module_exit(smsg_exit
);
242 MODULE_LICENSE("GPL");
244 EXPORT_SYMBOL(smsg_register_callback
);
245 EXPORT_SYMBOL(smsg_unregister_callback
);