smsgiucv.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * IUCV special message driver
  3. *
  4. * Copyright 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. #include <linux/module.h>
  22. #include <linux/init.h>
  23. #include <linux/errno.h>
  24. #include <linux/device.h>
  25. #include <net/iucv/iucv.h>
  26. #include <asm/cpcmd.h>
  27. #include <asm/ebcdic.h>
  28. #include "smsgiucv.h"
  29. struct smsg_callback {
  30. struct list_head list;
  31. char *prefix;
  32. int len;
  33. void (*callback)(char *from, char *str);
  34. };
  35. MODULE_AUTHOR
  36. ("(C) 2003 IBM Corporation by Martin Schwidefsky (schwidefsky@de.ibm.com)");
  37. MODULE_DESCRIPTION ("Linux for S/390 IUCV special message driver");
  38. static struct iucv_path *smsg_path;
  39. static DEFINE_SPINLOCK(smsg_list_lock);
  40. static LIST_HEAD(smsg_list);
  41. static int smsg_path_pending(struct iucv_path *, u8 ipvmid[8], u8 ipuser[16]);
  42. static void smsg_message_pending(struct iucv_path *, struct iucv_message *);
  43. static struct iucv_handler smsg_handler = {
  44. .path_pending = smsg_path_pending,
  45. .message_pending = smsg_message_pending,
  46. };
  47. static int smsg_path_pending(struct iucv_path *path, u8 ipvmid[8],
  48. u8 ipuser[16])
  49. {
  50. if (strncmp(ipvmid, "*MSG ", sizeof(ipvmid)) != 0)
  51. return -EINVAL;
  52. /* Path pending from *MSG. */
  53. return iucv_path_accept(path, &smsg_handler, "SMSGIUCV ", NULL);
  54. }
  55. static void smsg_message_pending(struct iucv_path *path,
  56. struct iucv_message *msg)
  57. {
  58. struct smsg_callback *cb;
  59. unsigned char *buffer;
  60. unsigned char sender[9];
  61. int rc, i;
  62. buffer = kmalloc(msg->length + 1, GFP_ATOMIC | GFP_DMA);
  63. if (!buffer) {
  64. iucv_message_reject(path, msg);
  65. return;
  66. }
  67. rc = iucv_message_receive(path, msg, 0, buffer, msg->length, NULL);
  68. if (rc == 0) {
  69. buffer[msg->length] = 0;
  70. EBCASC(buffer, msg->length);
  71. memcpy(sender, buffer, 8);
  72. sender[8] = 0;
  73. /* Remove trailing whitespace from the sender name. */
  74. for (i = 7; i >= 0; i--) {
  75. if (sender[i] != ' ' && sender[i] != '\t')
  76. break;
  77. sender[i] = 0;
  78. }
  79. spin_lock(&smsg_list_lock);
  80. list_for_each_entry(cb, &smsg_list, list)
  81. if (strncmp(buffer + 8, cb->prefix, cb->len) == 0) {
  82. cb->callback(sender, buffer + 8);
  83. break;
  84. }
  85. spin_unlock(&smsg_list_lock);
  86. }
  87. kfree(buffer);
  88. }
  89. int smsg_register_callback(char *prefix,
  90. void (*callback)(char *from, char *str))
  91. {
  92. struct smsg_callback *cb;
  93. cb = kmalloc(sizeof(struct smsg_callback), GFP_KERNEL);
  94. if (!cb)
  95. return -ENOMEM;
  96. cb->prefix = prefix;
  97. cb->len = strlen(prefix);
  98. cb->callback = callback;
  99. spin_lock_bh(&smsg_list_lock);
  100. list_add_tail(&cb->list, &smsg_list);
  101. spin_unlock_bh(&smsg_list_lock);
  102. return 0;
  103. }
  104. void smsg_unregister_callback(char *prefix,
  105. void (*callback)(char *from, char *str))
  106. {
  107. struct smsg_callback *cb, *tmp;
  108. spin_lock_bh(&smsg_list_lock);
  109. cb = NULL;
  110. list_for_each_entry(tmp, &smsg_list, list)
  111. if (tmp->callback == callback &&
  112. strcmp(tmp->prefix, prefix) == 0) {
  113. cb = tmp;
  114. list_del(&cb->list);
  115. break;
  116. }
  117. spin_unlock_bh(&smsg_list_lock);
  118. kfree(cb);
  119. }
  120. static struct device_driver smsg_driver = {
  121. .name = "SMSGIUCV",
  122. .bus = &iucv_bus,
  123. };
  124. static void __exit smsg_exit(void)
  125. {
  126. cpcmd("SET SMSG IUCV", NULL, 0, NULL);
  127. iucv_unregister(&smsg_handler, 1);
  128. driver_unregister(&smsg_driver);
  129. }
  130. static int __init smsg_init(void)
  131. {
  132. int rc;
  133. if (!MACHINE_IS_VM) {
  134. rc = -EPROTONOSUPPORT;
  135. goto out;
  136. }
  137. rc = driver_register(&smsg_driver);
  138. if (rc != 0)
  139. goto out;
  140. rc = iucv_register(&smsg_handler, 1);
  141. if (rc)
  142. goto out_driver;
  143. smsg_path = iucv_path_alloc(255, 0, GFP_KERNEL);
  144. if (!smsg_path) {
  145. rc = -ENOMEM;
  146. goto out_register;
  147. }
  148. rc = iucv_path_connect(smsg_path, &smsg_handler, "*MSG ",
  149. NULL, NULL, NULL);
  150. if (rc)
  151. goto out_free;
  152. cpcmd("SET SMSG IUCV", NULL, 0, NULL);
  153. return 0;
  154. out_free:
  155. iucv_path_free(smsg_path);
  156. out_register:
  157. iucv_unregister(&smsg_handler, 1);
  158. out_driver:
  159. driver_unregister(&smsg_driver);
  160. out:
  161. return rc;
  162. }
  163. module_init(smsg_init);
  164. module_exit(smsg_exit);
  165. MODULE_LICENSE("GPL");
  166. EXPORT_SYMBOL(smsg_register_callback);
  167. EXPORT_SYMBOL(smsg_unregister_callback);