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 <net/iucv/iucv.h>
28 #include <asm/cpcmd.h>
29 #include <asm/ebcdic.h>
32 struct smsg_callback
{
33 struct list_head list
;
36 void (*callback
)(char *from
, char *str
);
40 ("(C) 2003 IBM Corporation by Martin Schwidefsky (schwidefsky@de.ibm.com)");
41 MODULE_DESCRIPTION ("Linux for S/390 IUCV special message driver");
43 static struct iucv_path
*smsg_path
;
44 /* dummy device used as trigger for PM functions */
45 static struct device
*smsg_dev
;
47 static DEFINE_SPINLOCK(smsg_list_lock
);
48 static LIST_HEAD(smsg_list
);
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
*);
53 static struct iucv_handler smsg_handler
= {
54 .path_pending
= smsg_path_pending
,
55 .message_pending
= smsg_message_pending
,
58 static int smsg_path_pending(struct iucv_path
*path
, u8 ipvmid
[8],
61 if (strncmp(ipvmid
, "*MSG ", sizeof(ipvmid
)) != 0)
63 /* Path pending from *MSG. */
64 return iucv_path_accept(path
, &smsg_handler
, "SMSGIUCV ", NULL
);
67 static void smsg_message_pending(struct iucv_path
*path
,
68 struct iucv_message
*msg
)
70 struct smsg_callback
*cb
;
71 unsigned char *buffer
;
72 unsigned char sender
[9];
75 buffer
= kmalloc(msg
->length
+ 1, GFP_ATOMIC
| GFP_DMA
);
77 iucv_message_reject(path
, msg
);
80 rc
= iucv_message_receive(path
, msg
, 0, buffer
, msg
->length
, NULL
);
82 buffer
[msg
->length
] = 0;
83 EBCASC(buffer
, msg
->length
);
84 memcpy(sender
, buffer
, 8);
86 /* Remove trailing whitespace from the sender name. */
87 for (i
= 7; i
>= 0; i
--) {
88 if (sender
[i
] != ' ' && sender
[i
] != '\t')
92 spin_lock(&smsg_list_lock
);
93 list_for_each_entry(cb
, &smsg_list
, list
)
94 if (strncmp(buffer
+ 8, cb
->prefix
, cb
->len
) == 0) {
95 cb
->callback(sender
, buffer
+ 8);
98 spin_unlock(&smsg_list_lock
);
103 int smsg_register_callback(char *prefix
,
104 void (*callback
)(char *from
, char *str
))
106 struct smsg_callback
*cb
;
108 cb
= kmalloc(sizeof(struct smsg_callback
), GFP_KERNEL
);
112 cb
->len
= strlen(prefix
);
113 cb
->callback
= callback
;
114 spin_lock_bh(&smsg_list_lock
);
115 list_add_tail(&cb
->list
, &smsg_list
);
116 spin_unlock_bh(&smsg_list_lock
);
120 void smsg_unregister_callback(char *prefix
,
121 void (*callback
)(char *from
, char *str
))
123 struct smsg_callback
*cb
, *tmp
;
125 spin_lock_bh(&smsg_list_lock
);
127 list_for_each_entry(tmp
, &smsg_list
, list
)
128 if (tmp
->callback
== callback
&&
129 strcmp(tmp
->prefix
, prefix
) == 0) {
134 spin_unlock_bh(&smsg_list_lock
);
138 static int smsg_pm_freeze(struct device
*dev
)
140 #ifdef CONFIG_PM_DEBUG
141 printk(KERN_WARNING
"smsg_pm_freeze\n");
144 iucv_path_sever(smsg_path
, NULL
);
148 static int smsg_pm_restore_thaw(struct device
*dev
)
152 #ifdef CONFIG_PM_DEBUG
153 printk(KERN_WARNING
"smsg_pm_restore_thaw\n");
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 ",
161 printk(KERN_ERR
"iucv_path_connect returned with rc %i\n", rc
);
166 static struct dev_pm_ops smsg_pm_ops
= {
167 .freeze
= smsg_pm_freeze
,
168 .thaw
= smsg_pm_restore_thaw
,
169 .restore
= smsg_pm_restore_thaw
,
172 static struct device_driver smsg_driver
= {
173 .owner
= THIS_MODULE
,
179 static void __exit
smsg_exit(void)
181 cpcmd("SET SMSG IUCV", NULL
, 0, NULL
);
182 device_unregister(smsg_dev
);
183 iucv_unregister(&smsg_handler
, 1);
184 driver_unregister(&smsg_driver
);
187 static int __init
smsg_init(void)
191 if (!MACHINE_IS_VM
) {
192 rc
= -EPROTONOSUPPORT
;
195 rc
= driver_register(&smsg_driver
);
198 rc
= iucv_register(&smsg_handler
, 1);
201 smsg_path
= iucv_path_alloc(255, 0, GFP_KERNEL
);
206 rc
= iucv_path_connect(smsg_path
, &smsg_handler
, "*MSG ",
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
);
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
);