2 * IUCV special message driver
4 * Copyright IBM Corp. 2003, 2009
6 * Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com)
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)
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.
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.
23 #include <linux/module.h>
24 #include <linux/init.h>
25 #include <linux/errno.h>
26 #include <linux/device.h>
27 #include <linux/slab.h>
28 #include <net/iucv/iucv.h>
29 #include <asm/cpcmd.h>
30 #include <asm/ebcdic.h>
33 struct smsg_callback
{
34 struct list_head list
;
37 void (*callback
)(const char *from
, char *str
);
41 ("(C) 2003 IBM Corporation by Martin Schwidefsky (schwidefsky@de.ibm.com)");
42 MODULE_DESCRIPTION ("Linux for S/390 IUCV special message driver");
44 static struct iucv_path
*smsg_path
;
45 /* dummy device used as trigger for PM functions */
46 static struct device
*smsg_dev
;
48 static DEFINE_SPINLOCK(smsg_list_lock
);
49 static LIST_HEAD(smsg_list
);
50 static int iucv_path_connected
;
52 static int smsg_path_pending(struct iucv_path
*, u8 ipvmid
[8], u8 ipuser
[16]);
53 static void smsg_message_pending(struct iucv_path
*, struct iucv_message
*);
55 static struct iucv_handler smsg_handler
= {
56 .path_pending
= smsg_path_pending
,
57 .message_pending
= smsg_message_pending
,
60 static int smsg_path_pending(struct iucv_path
*path
, u8 ipvmid
[8],
63 if (strncmp(ipvmid
, "*MSG ", 8) != 0)
65 /* Path pending from *MSG. */
66 return iucv_path_accept(path
, &smsg_handler
, "SMSGIUCV ", NULL
);
69 static void smsg_message_pending(struct iucv_path
*path
,
70 struct iucv_message
*msg
)
72 struct smsg_callback
*cb
;
73 unsigned char *buffer
;
74 unsigned char sender
[9];
77 buffer
= kmalloc(msg
->length
+ 1, GFP_ATOMIC
| GFP_DMA
);
79 iucv_message_reject(path
, msg
);
82 rc
= iucv_message_receive(path
, msg
, 0, buffer
, msg
->length
, NULL
);
84 buffer
[msg
->length
] = 0;
85 EBCASC(buffer
, msg
->length
);
86 memcpy(sender
, buffer
, 8);
88 /* Remove trailing whitespace from the sender name. */
89 for (i
= 7; i
>= 0; i
--) {
90 if (sender
[i
] != ' ' && sender
[i
] != '\t')
94 spin_lock(&smsg_list_lock
);
95 list_for_each_entry(cb
, &smsg_list
, list
)
96 if (strncmp(buffer
+ 8, cb
->prefix
, cb
->len
) == 0) {
97 cb
->callback(sender
, buffer
+ 8);
100 spin_unlock(&smsg_list_lock
);
105 int smsg_register_callback(const char *prefix
,
106 void (*callback
)(const char *from
, char *str
))
108 struct smsg_callback
*cb
;
110 cb
= kmalloc(sizeof(struct smsg_callback
), GFP_KERNEL
);
114 cb
->len
= strlen(prefix
);
115 cb
->callback
= callback
;
116 spin_lock_bh(&smsg_list_lock
);
117 list_add_tail(&cb
->list
, &smsg_list
);
118 spin_unlock_bh(&smsg_list_lock
);
122 void smsg_unregister_callback(const char *prefix
,
123 void (*callback
)(const char *from
,
126 struct smsg_callback
*cb
, *tmp
;
128 spin_lock_bh(&smsg_list_lock
);
130 list_for_each_entry(tmp
, &smsg_list
, list
)
131 if (tmp
->callback
== callback
&&
132 strcmp(tmp
->prefix
, prefix
) == 0) {
137 spin_unlock_bh(&smsg_list_lock
);
141 static int smsg_pm_freeze(struct device
*dev
)
143 #ifdef CONFIG_PM_DEBUG
144 printk(KERN_WARNING
"smsg_pm_freeze\n");
146 if (smsg_path
&& iucv_path_connected
) {
147 iucv_path_sever(smsg_path
, NULL
);
148 iucv_path_connected
= 0;
153 static int smsg_pm_restore_thaw(struct device
*dev
)
157 #ifdef CONFIG_PM_DEBUG
158 printk(KERN_WARNING
"smsg_pm_restore_thaw\n");
160 if (smsg_path
&& iucv_path_connected
) {
161 memset(smsg_path
, 0, sizeof(*smsg_path
));
162 smsg_path
->msglim
= 255;
163 smsg_path
->flags
= 0;
164 rc
= iucv_path_connect(smsg_path
, &smsg_handler
, "*MSG ",
166 #ifdef CONFIG_PM_DEBUG
169 "iucv_path_connect returned with rc %i\n", rc
);
172 iucv_path_connected
= 1;
173 cpcmd("SET SMSG IUCV", NULL
, 0, NULL
);
178 static const struct dev_pm_ops smsg_pm_ops
= {
179 .freeze
= smsg_pm_freeze
,
180 .thaw
= smsg_pm_restore_thaw
,
181 .restore
= smsg_pm_restore_thaw
,
184 static struct device_driver smsg_driver
= {
185 .owner
= THIS_MODULE
,
186 .name
= SMSGIUCV_DRV_NAME
,
191 static void __exit
smsg_exit(void)
193 cpcmd("SET SMSG IUCV", NULL
, 0, NULL
);
194 device_unregister(smsg_dev
);
195 iucv_unregister(&smsg_handler
, 1);
196 driver_unregister(&smsg_driver
);
199 static int __init
smsg_init(void)
203 if (!MACHINE_IS_VM
) {
204 rc
= -EPROTONOSUPPORT
;
207 rc
= driver_register(&smsg_driver
);
210 rc
= iucv_register(&smsg_handler
, 1);
213 smsg_path
= iucv_path_alloc(255, 0, GFP_KERNEL
);
218 rc
= iucv_path_connect(smsg_path
, &smsg_handler
, "*MSG ",
223 iucv_path_connected
= 1;
224 smsg_dev
= kzalloc(sizeof(struct device
), GFP_KERNEL
);
229 dev_set_name(smsg_dev
, "smsg_iucv");
230 smsg_dev
->bus
= &iucv_bus
;
231 smsg_dev
->parent
= iucv_root
;
232 smsg_dev
->release
= (void (*)(struct device
*))kfree
;
233 smsg_dev
->driver
= &smsg_driver
;
234 rc
= device_register(smsg_dev
);
238 cpcmd("SET SMSG IUCV", NULL
, 0, NULL
);
242 put_device(smsg_dev
);
244 iucv_path_free(smsg_path
);
247 iucv_unregister(&smsg_handler
, 1);
249 driver_unregister(&smsg_driver
);
254 module_init(smsg_init
);
255 module_exit(smsg_exit
);
256 MODULE_LICENSE("GPL");
258 EXPORT_SYMBOL(smsg_register_callback
);
259 EXPORT_SYMBOL(smsg_unregister_callback
);