smsgiucv.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 <linux/slab.h>
  27. #include <net/iucv/iucv.h>
  28. #include <asm/cpcmd.h>
  29. #include <asm/ebcdic.h>
  30. #include "smsgiucv.h"
  31. struct smsg_callback {
  32. struct list_head list;
  33. const char *prefix;
  34. int len;
  35. void (*callback)(const char *from, char *str);
  36. };
  37. MODULE_AUTHOR
  38. ("(C) 2003 IBM Corporation by Martin Schwidefsky (schwidefsky@de.ibm.com)");
  39. MODULE_DESCRIPTION ("Linux for S/390 IUCV special message driver");
  40. static struct iucv_path *smsg_path;
  41. /* dummy device used as trigger for PM functions */
  42. static struct device *smsg_dev;
  43. static DEFINE_SPINLOCK(smsg_list_lock);
  44. static LIST_HEAD(smsg_list);
  45. static int smsg_path_pending(struct iucv_path *, u8 ipvmid[8], u8 ipuser[16]);
  46. static void smsg_message_pending(struct iucv_path *, struct iucv_message *);
  47. static struct iucv_handler smsg_handler = {
  48. .path_pending = smsg_path_pending,
  49. .message_pending = smsg_message_pending,
  50. };
  51. static int smsg_path_pending(struct iucv_path *path, u8 ipvmid[8],
  52. u8 ipuser[16])
  53. {
  54. if (strncmp(ipvmid, "*MSG ", sizeof(ipvmid)) != 0)
  55. return -EINVAL;
  56. /* Path pending from *MSG. */
  57. return iucv_path_accept(path, &smsg_handler, "SMSGIUCV ", NULL);
  58. }
  59. static void smsg_message_pending(struct iucv_path *path,
  60. struct iucv_message *msg)
  61. {
  62. struct smsg_callback *cb;
  63. unsigned char *buffer;
  64. unsigned char sender[9];
  65. int rc, i;
  66. buffer = kmalloc(msg->length + 1, GFP_ATOMIC | GFP_DMA);
  67. if (!buffer) {
  68. iucv_message_reject(path, msg);
  69. return;
  70. }
  71. rc = iucv_message_receive(path, msg, 0, buffer, msg->length, NULL);
  72. if (rc == 0) {
  73. buffer[msg->length] = 0;
  74. EBCASC(buffer, msg->length);
  75. memcpy(sender, buffer, 8);
  76. sender[8] = 0;
  77. /* Remove trailing whitespace from the sender name. */
  78. for (i = 7; i >= 0; i--) {
  79. if (sender[i] != ' ' && sender[i] != '\t')
  80. break;
  81. sender[i] = 0;
  82. }
  83. spin_lock(&smsg_list_lock);
  84. list_for_each_entry(cb, &smsg_list, list)
  85. if (strncmp(buffer + 8, cb->prefix, cb->len) == 0) {
  86. cb->callback(sender, buffer + 8);
  87. break;
  88. }
  89. spin_unlock(&smsg_list_lock);
  90. }
  91. kfree(buffer);
  92. }
  93. int smsg_register_callback(const char *prefix,
  94. void (*callback)(const char *from, char *str))
  95. {
  96. struct smsg_callback *cb;
  97. cb = kmalloc(sizeof(struct smsg_callback), GFP_KERNEL);
  98. if (!cb)
  99. return -ENOMEM;
  100. cb->prefix = prefix;
  101. cb->len = strlen(prefix);
  102. cb->callback = callback;
  103. spin_lock_bh(&smsg_list_lock);
  104. list_add_tail(&cb->list, &smsg_list);
  105. spin_unlock_bh(&smsg_list_lock);
  106. return 0;
  107. }
  108. void smsg_unregister_callback(const char *prefix,
  109. void (*callback)(const char *from,
  110. char *str))
  111. {
  112. struct smsg_callback *cb, *tmp;
  113. spin_lock_bh(&smsg_list_lock);
  114. cb = NULL;
  115. list_for_each_entry(tmp, &smsg_list, list)
  116. if (tmp->callback == callback &&
  117. strcmp(tmp->prefix, prefix) == 0) {
  118. cb = tmp;
  119. list_del(&cb->list);
  120. break;
  121. }
  122. spin_unlock_bh(&smsg_list_lock);
  123. kfree(cb);
  124. }
  125. static int smsg_pm_freeze(struct device *dev)
  126. {
  127. #ifdef CONFIG_PM_DEBUG
  128. printk(KERN_WARNING "smsg_pm_freeze\n");
  129. #endif
  130. if (smsg_path)
  131. iucv_path_sever(smsg_path, NULL);
  132. return 0;
  133. }
  134. static int smsg_pm_restore_thaw(struct device *dev)
  135. {
  136. int rc;
  137. #ifdef CONFIG_PM_DEBUG
  138. printk(KERN_WARNING "smsg_pm_restore_thaw\n");
  139. #endif
  140. if (smsg_path) {
  141. memset(smsg_path, 0, sizeof(*smsg_path));
  142. smsg_path->msglim = 255;
  143. smsg_path->flags = 0;
  144. rc = iucv_path_connect(smsg_path, &smsg_handler, "*MSG ",
  145. NULL, NULL, NULL);
  146. #ifdef CONFIG_PM_DEBUG
  147. if (rc)
  148. printk(KERN_ERR
  149. "iucv_path_connect returned with rc %i\n", rc);
  150. #endif
  151. cpcmd("SET SMSG IUCV", NULL, 0, NULL);
  152. }
  153. return 0;
  154. }
  155. static const struct dev_pm_ops smsg_pm_ops = {
  156. .freeze = smsg_pm_freeze,
  157. .thaw = smsg_pm_restore_thaw,
  158. .restore = smsg_pm_restore_thaw,
  159. };
  160. static struct device_driver smsg_driver = {
  161. .owner = THIS_MODULE,
  162. .name = SMSGIUCV_DRV_NAME,
  163. .bus = &iucv_bus,
  164. .pm = &smsg_pm_ops,
  165. };
  166. static void __exit smsg_exit(void)
  167. {
  168. cpcmd("SET SMSG IUCV", NULL, 0, NULL);
  169. device_unregister(smsg_dev);
  170. iucv_unregister(&smsg_handler, 1);
  171. driver_unregister(&smsg_driver);
  172. }
  173. static int __init smsg_init(void)
  174. {
  175. int rc;
  176. if (!MACHINE_IS_VM) {
  177. rc = -EPROTONOSUPPORT;
  178. goto out;
  179. }
  180. rc = driver_register(&smsg_driver);
  181. if (rc != 0)
  182. goto out;
  183. rc = iucv_register(&smsg_handler, 1);
  184. if (rc)
  185. goto out_driver;
  186. smsg_path = iucv_path_alloc(255, 0, GFP_KERNEL);
  187. if (!smsg_path) {
  188. rc = -ENOMEM;
  189. goto out_register;
  190. }
  191. rc = iucv_path_connect(smsg_path, &smsg_handler, "*MSG ",
  192. NULL, NULL, NULL);
  193. if (rc)
  194. goto out_free_path;
  195. smsg_dev = kzalloc(sizeof(struct device), GFP_KERNEL);
  196. if (!smsg_dev) {
  197. rc = -ENOMEM;
  198. goto out_free_path;
  199. }
  200. dev_set_name(smsg_dev, "smsg_iucv");
  201. smsg_dev->bus = &iucv_bus;
  202. smsg_dev->parent = iucv_root;
  203. smsg_dev->release = (void (*)(struct device *))kfree;
  204. smsg_dev->driver = &smsg_driver;
  205. rc = device_register(smsg_dev);
  206. if (rc)
  207. goto out_put;
  208. cpcmd("SET SMSG IUCV", NULL, 0, NULL);
  209. return 0;
  210. out_put:
  211. put_device(smsg_dev);
  212. out_free_path:
  213. iucv_path_free(smsg_path);
  214. smsg_path = NULL;
  215. out_register:
  216. iucv_unregister(&smsg_handler, 1);
  217. out_driver:
  218. driver_unregister(&smsg_driver);
  219. out:
  220. return rc;
  221. }
  222. module_init(smsg_init);
  223. module_exit(smsg_exit);
  224. MODULE_LICENSE("GPL");
  225. EXPORT_SYMBOL(smsg_register_callback);
  226. EXPORT_SYMBOL(smsg_unregister_callback);