smsgiucv.c 4.1 KB

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