smsgiucv.c 4.1 KB

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