smsgiucv.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. const char *prefix;
  33. int len;
  34. void (*callback)(const 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(const char *prefix,
  93. void (*callback)(const 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(const char *prefix,
  108. void (*callback)(const char *from,
  109. char *str))
  110. {
  111. struct smsg_callback *cb, *tmp;
  112. spin_lock_bh(&smsg_list_lock);
  113. cb = NULL;
  114. list_for_each_entry(tmp, &smsg_list, list)
  115. if (tmp->callback == callback &&
  116. strcmp(tmp->prefix, prefix) == 0) {
  117. cb = tmp;
  118. list_del(&cb->list);
  119. break;
  120. }
  121. spin_unlock_bh(&smsg_list_lock);
  122. kfree(cb);
  123. }
  124. static int smsg_pm_freeze(struct device *dev)
  125. {
  126. #ifdef CONFIG_PM_DEBUG
  127. printk(KERN_WARNING "smsg_pm_freeze\n");
  128. #endif
  129. if (smsg_path)
  130. iucv_path_sever(smsg_path, NULL);
  131. return 0;
  132. }
  133. static int smsg_pm_restore_thaw(struct device *dev)
  134. {
  135. int rc;
  136. #ifdef CONFIG_PM_DEBUG
  137. printk(KERN_WARNING "smsg_pm_restore_thaw\n");
  138. #endif
  139. if (smsg_path) {
  140. memset(smsg_path, 0, sizeof(*smsg_path));
  141. smsg_path->msglim = 255;
  142. smsg_path->flags = 0;
  143. rc = iucv_path_connect(smsg_path, &smsg_handler, "*MSG ",
  144. NULL, NULL, NULL);
  145. #ifdef CONFIG_PM_DEBUG
  146. if (rc)
  147. printk(KERN_ERR
  148. "iucv_path_connect returned with rc %i\n", rc);
  149. #endif
  150. cpcmd("SET SMSG IUCV", NULL, 0, NULL);
  151. }
  152. return 0;
  153. }
  154. static const struct dev_pm_ops smsg_pm_ops = {
  155. .freeze = smsg_pm_freeze,
  156. .thaw = smsg_pm_restore_thaw,
  157. .restore = smsg_pm_restore_thaw,
  158. };
  159. static struct device_driver smsg_driver = {
  160. .owner = THIS_MODULE,
  161. .name = SMSGIUCV_DRV_NAME,
  162. .bus = &iucv_bus,
  163. .pm = &smsg_pm_ops,
  164. };
  165. static void __exit smsg_exit(void)
  166. {
  167. cpcmd("SET SMSG IUCV", NULL, 0, NULL);
  168. device_unregister(smsg_dev);
  169. iucv_unregister(&smsg_handler, 1);
  170. driver_unregister(&smsg_driver);
  171. }
  172. static int __init smsg_init(void)
  173. {
  174. int rc;
  175. if (!MACHINE_IS_VM) {
  176. rc = -EPROTONOSUPPORT;
  177. goto out;
  178. }
  179. rc = driver_register(&smsg_driver);
  180. if (rc != 0)
  181. goto out;
  182. rc = iucv_register(&smsg_handler, 1);
  183. if (rc)
  184. goto out_driver;
  185. smsg_path = iucv_path_alloc(255, 0, GFP_KERNEL);
  186. if (!smsg_path) {
  187. rc = -ENOMEM;
  188. goto out_register;
  189. }
  190. rc = iucv_path_connect(smsg_path, &smsg_handler, "*MSG ",
  191. NULL, NULL, NULL);
  192. if (rc)
  193. goto out_free_path;
  194. smsg_dev = kzalloc(sizeof(struct device), GFP_KERNEL);
  195. if (!smsg_dev) {
  196. rc = -ENOMEM;
  197. goto out_free_path;
  198. }
  199. dev_set_name(smsg_dev, "smsg_iucv");
  200. smsg_dev->bus = &iucv_bus;
  201. smsg_dev->parent = iucv_root;
  202. smsg_dev->release = (void (*)(struct device *))kfree;
  203. smsg_dev->driver = &smsg_driver;
  204. rc = device_register(smsg_dev);
  205. if (rc)
  206. goto out_put;
  207. cpcmd("SET SMSG IUCV", NULL, 0, NULL);
  208. return 0;
  209. out_put:
  210. put_device(smsg_dev);
  211. out_free_path:
  212. iucv_path_free(smsg_path);
  213. smsg_path = NULL;
  214. out_register:
  215. iucv_unregister(&smsg_handler, 1);
  216. out_driver:
  217. driver_unregister(&smsg_driver);
  218. out:
  219. return rc;
  220. }
  221. module_init(smsg_init);
  222. module_exit(smsg_exit);
  223. MODULE_LICENSE("GPL");
  224. EXPORT_SYMBOL(smsg_register_callback);
  225. EXPORT_SYMBOL(smsg_unregister_callback);