[PATCH] W1: w1_netlink: New init/fini netlink callbacks.
[linux-2.6/verdex.git] / drivers / s390 / net / smsgiucv.c
blobd6469baa7e16ef1698d7ff9ff27436aca11fc723
1 /*
2 * IUCV special message driver
4 * Copyright (C) 2003 IBM Deutschland Entwicklung GmbH, IBM Corporation
5 * Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com)
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2, or (at your option)
10 * any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include <linux/module.h>
23 #include <linux/init.h>
24 #include <linux/errno.h>
25 #include <linux/device.h>
26 #include <asm/cpcmd.h>
27 #include <asm/ebcdic.h>
29 #include "iucv.h"
31 struct smsg_callback {
32 struct list_head list;
33 char *prefix;
34 int len;
35 void (*callback)(char *from, char *str);
38 MODULE_AUTHOR
39 ("(C) 2003 IBM Corporation by Martin Schwidefsky (schwidefsky@de.ibm.com)");
40 MODULE_DESCRIPTION ("Linux for S/390 IUCV special message driver");
42 static iucv_handle_t smsg_handle;
43 static unsigned short smsg_pathid;
44 static DEFINE_SPINLOCK(smsg_list_lock);
45 static struct list_head smsg_list = LIST_HEAD_INIT(smsg_list);
47 static void
48 smsg_connection_complete(iucv_ConnectionComplete *eib, void *pgm_data)
53 static void
54 smsg_message_pending(iucv_MessagePending *eib, void *pgm_data)
56 struct smsg_callback *cb;
57 unsigned char *msg;
58 unsigned char sender[9];
59 unsigned short len;
60 int rc, i;
62 len = eib->ln1msg2.ipbfln1f;
63 msg = kmalloc(len + 1, GFP_ATOMIC|GFP_DMA);
64 if (!msg) {
65 iucv_reject(eib->ippathid, eib->ipmsgid, eib->iptrgcls);
66 return;
68 rc = iucv_receive(eib->ippathid, eib->ipmsgid, eib->iptrgcls,
69 msg, len, 0, 0, 0);
70 if (rc == 0) {
71 msg[len] = 0;
72 EBCASC(msg, len);
73 memcpy(sender, msg, 8);
74 sender[8] = 0;
75 /* Remove trailing whitespace from the sender name. */
76 for (i = 7; i >= 0; i--) {
77 if (sender[i] != ' ' && sender[i] != '\t')
78 break;
79 sender[i] = 0;
81 spin_lock(&smsg_list_lock);
82 list_for_each_entry(cb, &smsg_list, list)
83 if (strncmp(msg + 8, cb->prefix, cb->len) == 0) {
84 cb->callback(sender, msg + 8);
85 break;
87 spin_unlock(&smsg_list_lock);
89 kfree(msg);
92 static iucv_interrupt_ops_t smsg_ops = {
93 .ConnectionComplete = smsg_connection_complete,
94 .MessagePending = smsg_message_pending,
97 static struct device_driver smsg_driver = {
98 .name = "SMSGIUCV",
99 .bus = &iucv_bus,
103 smsg_register_callback(char *prefix, void (*callback)(char *from, char *str))
105 struct smsg_callback *cb;
107 cb = kmalloc(sizeof(struct smsg_callback), GFP_KERNEL);
108 if (!cb)
109 return -ENOMEM;
110 cb->prefix = prefix;
111 cb->len = strlen(prefix);
112 cb->callback = callback;
113 spin_lock(&smsg_list_lock);
114 list_add_tail(&cb->list, &smsg_list);
115 spin_unlock(&smsg_list_lock);
116 return 0;
119 void
120 smsg_unregister_callback(char *prefix, void (*callback)(char *from, char *str))
122 struct smsg_callback *cb, *tmp;
124 spin_lock(&smsg_list_lock);
125 cb = 0;
126 list_for_each_entry(tmp, &smsg_list, list)
127 if (tmp->callback == callback &&
128 strcmp(tmp->prefix, prefix) == 0) {
129 cb = tmp;
130 list_del(&cb->list);
131 break;
133 spin_unlock(&smsg_list_lock);
134 kfree(cb);
137 static void __exit
138 smsg_exit(void)
140 if (smsg_handle > 0) {
141 cpcmd("SET SMSG OFF", NULL, 0, NULL);
142 iucv_sever(smsg_pathid, 0);
143 iucv_unregister_program(smsg_handle);
144 driver_unregister(&smsg_driver);
146 return;
149 static int __init
150 smsg_init(void)
152 static unsigned char pgmmask[24] = {
153 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
154 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
155 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
157 int rc;
159 rc = driver_register(&smsg_driver);
160 if (rc != 0) {
161 printk(KERN_ERR "SMSGIUCV: failed to register driver.\n");
162 return rc;
164 smsg_handle = iucv_register_program("SMSGIUCV ", "*MSG ",
165 pgmmask, &smsg_ops, 0);
166 if (!smsg_handle) {
167 printk(KERN_ERR "SMSGIUCV: failed to register to iucv");
168 driver_unregister(&smsg_driver);
169 return -EIO; /* better errno ? */
171 rc = iucv_connect (&smsg_pathid, 1, 0, "*MSG ", 0, 0, 0, 0,
172 smsg_handle, 0);
173 if (rc) {
174 printk(KERN_ERR "SMSGIUCV: failed to connect to *MSG");
175 iucv_unregister_program(smsg_handle);
176 driver_unregister(&smsg_driver);
177 smsg_handle = 0;
178 return -EIO;
180 cpcmd("SET SMSG IUCV", NULL, 0, NULL);
181 return 0;
184 module_init(smsg_init);
185 module_exit(smsg_exit);
186 MODULE_LICENSE("GPL");
188 EXPORT_SYMBOL(smsg_register_callback);
189 EXPORT_SYMBOL(smsg_unregister_callback);