Merge branch 'merge'
[linux-2.6] / drivers / s390 / net / smsgiucv.c
1 /*
2  * IUCV special message driver
3  *
4  * Copyright (C) 2003 IBM Deutschland Entwicklung GmbH, IBM Corporation
5  * Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com)
6  *
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.
11  *
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.
16  *
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.
20  */
21
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>
28
29 #include "iucv.h"
30
31 struct smsg_callback {
32         struct list_head list;
33         char *prefix;
34         int len;
35         void (*callback)(char *from, char *str);
36 };
37
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");
41
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);
46
47 static void
48 smsg_connection_complete(iucv_ConnectionComplete *eib, void *pgm_data)
49 {
50 }
51
52
53 static void
54 smsg_message_pending(iucv_MessagePending *eib, void *pgm_data)
55 {
56         struct smsg_callback *cb;
57         unsigned char *msg;
58         unsigned char sender[9];
59         unsigned short len;
60         int rc, i;
61
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;
67         }
68         rc = iucv_receive(eib->ippathid, eib->ipmsgid, eib->iptrgcls,
69                           msg, len, NULL, NULL, NULL);
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;
80                 }
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;
86                         }
87                 spin_unlock(&smsg_list_lock);
88         }
89         kfree(msg);
90 }
91
92 static iucv_interrupt_ops_t smsg_ops = {
93         .ConnectionComplete = smsg_connection_complete,
94         .MessagePending     = smsg_message_pending,
95 };
96
97 static struct device_driver smsg_driver = {
98         .name = "SMSGIUCV",
99         .bus  = &iucv_bus,
100 };
101
102 int
103 smsg_register_callback(char *prefix, void (*callback)(char *from, char *str))
104 {
105         struct smsg_callback *cb;
106
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;
117 }
118
119 void
120 smsg_unregister_callback(char *prefix, void (*callback)(char *from, char *str))
121 {
122         struct smsg_callback *cb, *tmp;
123
124         spin_lock(&smsg_list_lock);
125         cb = NULL;
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;
132                 }
133         spin_unlock(&smsg_list_lock);
134         kfree(cb);
135 }
136
137 static void __exit
138 smsg_exit(void)
139 {
140         if (smsg_handle > 0) {
141                 cpcmd("SET SMSG OFF", NULL, 0, NULL);
142                 iucv_sever(smsg_pathid, NULL);
143                 iucv_unregister_program(smsg_handle);
144                 driver_unregister(&smsg_driver);
145         }
146         return;
147 }
148
149 static int __init
150 smsg_init(void)
151 {
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
156         };
157         int rc;
158
159         rc = driver_register(&smsg_driver);
160         if (rc != 0) {
161                 printk(KERN_ERR "SMSGIUCV: failed to register driver.\n");
162                 return rc;
163         }
164         smsg_handle = iucv_register_program("SMSGIUCV        ", "*MSG    ",
165                                             pgmmask, &smsg_ops, NULL);
166         if (!smsg_handle) {
167                 printk(KERN_ERR "SMSGIUCV: failed to register to iucv");
168                 driver_unregister(&smsg_driver);
169                 return -EIO;    /* better errno ? */
170         }
171         rc = iucv_connect (&smsg_pathid, 255, NULL, "*MSG    ", NULL, 0,
172                            NULL, NULL, smsg_handle, NULL);
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 = NULL;
178                 return -EIO;
179         }
180         cpcmd("SET SMSG IUCV", NULL, 0, NULL);
181         return 0;
182 }
183
184 module_init(smsg_init);
185 module_exit(smsg_exit);
186 MODULE_LICENSE("GPL");
187
188 EXPORT_SYMBOL(smsg_register_callback);
189 EXPORT_SYMBOL(smsg_unregister_callback);