smsgiucv.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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 iucv_path_connected;
  46. static int smsg_path_pending(struct iucv_path *, u8 *, u8 *);
  47. static void smsg_message_pending(struct iucv_path *, struct iucv_message *);
  48. static struct iucv_handler smsg_handler = {
  49. .path_pending = smsg_path_pending,
  50. .message_pending = smsg_message_pending,
  51. };
  52. static int smsg_path_pending(struct iucv_path *path, u8 *ipvmid, u8 *ipuser)
  53. {
  54. if (strncmp(ipvmid, "*MSG ", 8) != 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 && iucv_path_connected) {
  131. iucv_path_sever(smsg_path, NULL);
  132. iucv_path_connected = 0;
  133. }
  134. return 0;
  135. }
  136. static int smsg_pm_restore_thaw(struct device *dev)
  137. {
  138. int rc;
  139. #ifdef CONFIG_PM_DEBUG
  140. printk(KERN_WARNING "smsg_pm_restore_thaw\n");
  141. #endif
  142. if (smsg_path && !iucv_path_connected) {
  143. memset(smsg_path, 0, sizeof(*smsg_path));
  144. smsg_path->msglim = 255;
  145. smsg_path->flags = 0;
  146. rc = iucv_path_connect(smsg_path, &smsg_handler, "*MSG ",
  147. NULL, NULL, NULL);
  148. #ifdef CONFIG_PM_DEBUG
  149. if (rc)
  150. printk(KERN_ERR
  151. "iucv_path_connect returned with rc %i\n", rc);
  152. #endif
  153. if (!rc)
  154. iucv_path_connected = 1;
  155. cpcmd("SET SMSG IUCV", NULL, 0, NULL);
  156. }
  157. return 0;
  158. }
  159. static const struct dev_pm_ops smsg_pm_ops = {
  160. .freeze = smsg_pm_freeze,
  161. .thaw = smsg_pm_restore_thaw,
  162. .restore = smsg_pm_restore_thaw,
  163. };
  164. static struct device_driver smsg_driver = {
  165. .owner = THIS_MODULE,
  166. .name = SMSGIUCV_DRV_NAME,
  167. .bus = &iucv_bus,
  168. .pm = &smsg_pm_ops,
  169. };
  170. static void __exit smsg_exit(void)
  171. {
  172. cpcmd("SET SMSG IUCV", NULL, 0, NULL);
  173. device_unregister(smsg_dev);
  174. iucv_unregister(&smsg_handler, 1);
  175. driver_unregister(&smsg_driver);
  176. }
  177. static int __init smsg_init(void)
  178. {
  179. int rc;
  180. if (!MACHINE_IS_VM) {
  181. rc = -EPROTONOSUPPORT;
  182. goto out;
  183. }
  184. rc = driver_register(&smsg_driver);
  185. if (rc != 0)
  186. goto out;
  187. rc = iucv_register(&smsg_handler, 1);
  188. if (rc)
  189. goto out_driver;
  190. smsg_path = iucv_path_alloc(255, 0, GFP_KERNEL);
  191. if (!smsg_path) {
  192. rc = -ENOMEM;
  193. goto out_register;
  194. }
  195. rc = iucv_path_connect(smsg_path, &smsg_handler, "*MSG ",
  196. NULL, NULL, NULL);
  197. if (rc)
  198. goto out_free_path;
  199. else
  200. iucv_path_connected = 1;
  201. smsg_dev = kzalloc(sizeof(struct device), GFP_KERNEL);
  202. if (!smsg_dev) {
  203. rc = -ENOMEM;
  204. goto out_free_path;
  205. }
  206. dev_set_name(smsg_dev, "smsg_iucv");
  207. smsg_dev->bus = &iucv_bus;
  208. smsg_dev->parent = iucv_root;
  209. smsg_dev->release = (void (*)(struct device *))kfree;
  210. smsg_dev->driver = &smsg_driver;
  211. rc = device_register(smsg_dev);
  212. if (rc)
  213. goto out_put;
  214. cpcmd("SET SMSG IUCV", NULL, 0, NULL);
  215. return 0;
  216. out_put:
  217. put_device(smsg_dev);
  218. out_free_path:
  219. iucv_path_free(smsg_path);
  220. smsg_path = NULL;
  221. out_register:
  222. iucv_unregister(&smsg_handler, 1);
  223. out_driver:
  224. driver_unregister(&smsg_driver);
  225. out:
  226. return rc;
  227. }
  228. module_init(smsg_init);
  229. module_exit(smsg_exit);
  230. MODULE_LICENSE("GPL");
  231. EXPORT_SYMBOL(smsg_register_callback);
  232. EXPORT_SYMBOL(smsg_unregister_callback);