smsgiucv.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. * IUCV special message driver
  3. *
  4. * Copyright IBM Corp. 2003, 2009
  5. *
  6. * Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com)
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2, or (at your option)
  11. * any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. */
  22. #include <linux/module.h>
  23. #include <linux/init.h>
  24. #include <linux/errno.h>
  25. #include <linux/device.h>
  26. #include <net/iucv/iucv.h>
  27. #include <asm/cpcmd.h>
  28. #include <asm/ebcdic.h>
  29. #include "smsgiucv.h"
  30. struct smsg_callback {
  31. struct list_head list;
  32. char *prefix;
  33. int len;
  34. void (*callback)(char *from, char *str);
  35. };
  36. MODULE_AUTHOR
  37. ("(C) 2003 IBM Corporation by Martin Schwidefsky (schwidefsky@de.ibm.com)");
  38. MODULE_DESCRIPTION ("Linux for S/390 IUCV special message driver");
  39. static struct iucv_path *smsg_path;
  40. /* dummy device used as trigger for PM functions */
  41. static struct device *smsg_dev;
  42. static DEFINE_SPINLOCK(smsg_list_lock);
  43. static LIST_HEAD(smsg_list);
  44. static int smsg_path_pending(struct iucv_path *, u8 ipvmid[8], u8 ipuser[16]);
  45. static void smsg_message_pending(struct iucv_path *, struct iucv_message *);
  46. static struct iucv_handler smsg_handler = {
  47. .path_pending = smsg_path_pending,
  48. .message_pending = smsg_message_pending,
  49. };
  50. static int smsg_path_pending(struct iucv_path *path, u8 ipvmid[8],
  51. u8 ipuser[16])
  52. {
  53. if (strncmp(ipvmid, "*MSG ", sizeof(ipvmid)) != 0)
  54. return -EINVAL;
  55. /* Path pending from *MSG. */
  56. return iucv_path_accept(path, &smsg_handler, "SMSGIUCV ", NULL);
  57. }
  58. static void smsg_message_pending(struct iucv_path *path,
  59. struct iucv_message *msg)
  60. {
  61. struct smsg_callback *cb;
  62. unsigned char *buffer;
  63. unsigned char sender[9];
  64. int rc, i;
  65. buffer = kmalloc(msg->length + 1, GFP_ATOMIC | GFP_DMA);
  66. if (!buffer) {
  67. iucv_message_reject(path, msg);
  68. return;
  69. }
  70. rc = iucv_message_receive(path, msg, 0, buffer, msg->length, NULL);
  71. if (rc == 0) {
  72. buffer[msg->length] = 0;
  73. EBCASC(buffer, msg->length);
  74. memcpy(sender, buffer, 8);
  75. sender[8] = 0;
  76. /* Remove trailing whitespace from the sender name. */
  77. for (i = 7; i >= 0; i--) {
  78. if (sender[i] != ' ' && sender[i] != '\t')
  79. break;
  80. sender[i] = 0;
  81. }
  82. spin_lock(&smsg_list_lock);
  83. list_for_each_entry(cb, &smsg_list, list)
  84. if (strncmp(buffer + 8, cb->prefix, cb->len) == 0) {
  85. cb->callback(sender, buffer + 8);
  86. break;
  87. }
  88. spin_unlock(&smsg_list_lock);
  89. }
  90. kfree(buffer);
  91. }
  92. int smsg_register_callback(char *prefix,
  93. void (*callback)(char *from, char *str))
  94. {
  95. struct smsg_callback *cb;
  96. cb = kmalloc(sizeof(struct smsg_callback), GFP_KERNEL);
  97. if (!cb)
  98. return -ENOMEM;
  99. cb->prefix = prefix;
  100. cb->len = strlen(prefix);
  101. cb->callback = callback;
  102. spin_lock_bh(&smsg_list_lock);
  103. list_add_tail(&cb->list, &smsg_list);
  104. spin_unlock_bh(&smsg_list_lock);
  105. return 0;
  106. }
  107. void smsg_unregister_callback(char *prefix,
  108. void (*callback)(char *from, char *str))
  109. {
  110. struct smsg_callback *cb, *tmp;
  111. spin_lock_bh(&smsg_list_lock);
  112. cb = NULL;
  113. list_for_each_entry(tmp, &smsg_list, list)
  114. if (tmp->callback == callback &&
  115. strcmp(tmp->prefix, prefix) == 0) {
  116. cb = tmp;
  117. list_del(&cb->list);
  118. break;
  119. }
  120. spin_unlock_bh(&smsg_list_lock);
  121. kfree(cb);
  122. }
  123. static int smsg_pm_freeze(struct device *dev)
  124. {
  125. #ifdef CONFIG_PM_DEBUG
  126. printk(KERN_WARNING "smsg_pm_freeze\n");
  127. #endif
  128. if (smsg_path)
  129. iucv_path_sever(smsg_path, NULL);
  130. return 0;
  131. }
  132. static int smsg_pm_restore_thaw(struct device *dev)
  133. {
  134. int rc;
  135. #ifdef CONFIG_PM_DEBUG
  136. printk(KERN_WARNING "smsg_pm_restore_thaw\n");
  137. #endif
  138. if (smsg_path) {
  139. memset(smsg_path, 0, sizeof(*smsg_path));
  140. smsg_path->msglim = 255;
  141. smsg_path->flags = 0;
  142. rc = iucv_path_connect(smsg_path, &smsg_handler, "*MSG ",
  143. NULL, NULL, NULL);
  144. printk(KERN_ERR "iucv_path_connect returned with rc %i\n", rc);
  145. }
  146. return 0;
  147. }
  148. static struct dev_pm_ops smsg_pm_ops = {
  149. .freeze = smsg_pm_freeze,
  150. .thaw = smsg_pm_restore_thaw,
  151. .restore = smsg_pm_restore_thaw,
  152. };
  153. static struct device_driver smsg_driver = {
  154. .owner = THIS_MODULE,
  155. .name = "SMSGIUCV",
  156. .bus = &iucv_bus,
  157. .pm = &smsg_pm_ops,
  158. };
  159. static void __exit smsg_exit(void)
  160. {
  161. cpcmd("SET SMSG IUCV", NULL, 0, NULL);
  162. device_unregister(smsg_dev);
  163. iucv_unregister(&smsg_handler, 1);
  164. driver_unregister(&smsg_driver);
  165. }
  166. static int __init smsg_init(void)
  167. {
  168. int rc;
  169. if (!MACHINE_IS_VM) {
  170. rc = -EPROTONOSUPPORT;
  171. goto out;
  172. }
  173. rc = driver_register(&smsg_driver);
  174. if (rc != 0)
  175. goto out;
  176. rc = iucv_register(&smsg_handler, 1);
  177. if (rc)
  178. goto out_driver;
  179. smsg_path = iucv_path_alloc(255, 0, GFP_KERNEL);
  180. if (!smsg_path) {
  181. rc = -ENOMEM;
  182. goto out_register;
  183. }
  184. rc = iucv_path_connect(smsg_path, &smsg_handler, "*MSG ",
  185. NULL, NULL, NULL);
  186. if (rc)
  187. goto out_free_path;
  188. smsg_dev = kzalloc(sizeof(struct device), GFP_KERNEL);
  189. if (!smsg_dev) {
  190. rc = -ENOMEM;
  191. goto out_free_path;
  192. }
  193. dev_set_name(smsg_dev, "smsg_iucv");
  194. smsg_dev->bus = &iucv_bus;
  195. smsg_dev->parent = iucv_root;
  196. smsg_dev->release = (void (*)(struct device *))kfree;
  197. smsg_dev->driver = &smsg_driver;
  198. rc = device_register(smsg_dev);
  199. if (rc)
  200. goto out_put;
  201. cpcmd("SET SMSG IUCV", NULL, 0, NULL);
  202. return 0;
  203. out_put:
  204. put_device(smsg_dev);
  205. out_free_path:
  206. iucv_path_free(smsg_path);
  207. smsg_path = NULL;
  208. out_register:
  209. iucv_unregister(&smsg_handler, 1);
  210. out_driver:
  211. driver_unregister(&smsg_driver);
  212. out:
  213. return rc;
  214. }
  215. module_init(smsg_init);
  216. module_exit(smsg_exit);
  217. MODULE_LICENSE("GPL");
  218. EXPORT_SYMBOL(smsg_register_callback);
  219. EXPORT_SYMBOL(smsg_unregister_callback);