smsgiucv.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. #ifdef CONFIG_PM_DEBUG
  145. if (rc)
  146. printk(KERN_ERR
  147. "iucv_path_connect returned with rc %i\n", rc);
  148. #endif
  149. cpcmd("SET SMSG IUCV", NULL, 0, NULL);
  150. }
  151. return 0;
  152. }
  153. static const struct dev_pm_ops smsg_pm_ops = {
  154. .freeze = smsg_pm_freeze,
  155. .thaw = smsg_pm_restore_thaw,
  156. .restore = smsg_pm_restore_thaw,
  157. };
  158. static struct device_driver smsg_driver = {
  159. .owner = THIS_MODULE,
  160. .name = "SMSGIUCV",
  161. .bus = &iucv_bus,
  162. .pm = &smsg_pm_ops,
  163. };
  164. static void __exit smsg_exit(void)
  165. {
  166. cpcmd("SET SMSG IUCV", NULL, 0, NULL);
  167. device_unregister(smsg_dev);
  168. iucv_unregister(&smsg_handler, 1);
  169. driver_unregister(&smsg_driver);
  170. }
  171. static int __init smsg_init(void)
  172. {
  173. int rc;
  174. if (!MACHINE_IS_VM) {
  175. rc = -EPROTONOSUPPORT;
  176. goto out;
  177. }
  178. rc = driver_register(&smsg_driver);
  179. if (rc != 0)
  180. goto out;
  181. rc = iucv_register(&smsg_handler, 1);
  182. if (rc)
  183. goto out_driver;
  184. smsg_path = iucv_path_alloc(255, 0, GFP_KERNEL);
  185. if (!smsg_path) {
  186. rc = -ENOMEM;
  187. goto out_register;
  188. }
  189. rc = iucv_path_connect(smsg_path, &smsg_handler, "*MSG ",
  190. NULL, NULL, NULL);
  191. if (rc)
  192. goto out_free_path;
  193. smsg_dev = kzalloc(sizeof(struct device), GFP_KERNEL);
  194. if (!smsg_dev) {
  195. rc = -ENOMEM;
  196. goto out_free_path;
  197. }
  198. dev_set_name(smsg_dev, "smsg_iucv");
  199. smsg_dev->bus = &iucv_bus;
  200. smsg_dev->parent = iucv_root;
  201. smsg_dev->release = (void (*)(struct device *))kfree;
  202. smsg_dev->driver = &smsg_driver;
  203. rc = device_register(smsg_dev);
  204. if (rc)
  205. goto out_put;
  206. cpcmd("SET SMSG IUCV", NULL, 0, NULL);
  207. return 0;
  208. out_put:
  209. put_device(smsg_dev);
  210. out_free_path:
  211. iucv_path_free(smsg_path);
  212. smsg_path = NULL;
  213. out_register:
  214. iucv_unregister(&smsg_handler, 1);
  215. out_driver:
  216. driver_unregister(&smsg_driver);
  217. out:
  218. return rc;
  219. }
  220. module_init(smsg_init);
  221. module_exit(smsg_exit);
  222. MODULE_LICENSE("GPL");
  223. EXPORT_SYMBOL(smsg_register_callback);
  224. EXPORT_SYMBOL(smsg_unregister_callback);