cn_test.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. * cn_test.c
  3. *
  4. * 2004+ Copyright (c) Evgeniy Polyakov <zbr@ioremap.net>
  5. * All rights reserved.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #define pr_fmt(fmt) "cn_test: " fmt
  22. #include <linux/kernel.h>
  23. #include <linux/module.h>
  24. #include <linux/moduleparam.h>
  25. #include <linux/skbuff.h>
  26. #include <linux/timer.h>
  27. #include <linux/connector.h>
  28. static struct cb_id cn_test_id = { CN_NETLINK_USERS + 3, 0x456 };
  29. static char cn_test_name[] = "cn_test";
  30. static struct sock *nls;
  31. static struct timer_list cn_test_timer;
  32. static void cn_test_callback(struct cn_msg *msg, struct netlink_skb_parms *nsp)
  33. {
  34. pr_info("%s: %lu: idx=%x, val=%x, seq=%u, ack=%u, len=%d: %s.\n",
  35. __func__, jiffies, msg->id.idx, msg->id.val,
  36. msg->seq, msg->ack, msg->len,
  37. msg->len ? (char *)msg->data : "");
  38. }
  39. /*
  40. * Do not remove this function even if no one is using it as
  41. * this is an example of how to get notifications about new
  42. * connector user registration
  43. */
  44. #if 0
  45. static int cn_test_want_notify(void)
  46. {
  47. struct cn_ctl_msg *ctl;
  48. struct cn_notify_req *req;
  49. struct cn_msg *msg = NULL;
  50. int size, size0;
  51. struct sk_buff *skb;
  52. struct nlmsghdr *nlh;
  53. u32 group = 1;
  54. size0 = sizeof(*msg) + sizeof(*ctl) + 3 * sizeof(*req);
  55. size = NLMSG_SPACE(size0);
  56. skb = alloc_skb(size, GFP_ATOMIC);
  57. if (!skb) {
  58. pr_err("failed to allocate new skb with size=%u\n", size);
  59. return -ENOMEM;
  60. }
  61. nlh = NLMSG_PUT(skb, 0, 0x123, NLMSG_DONE, size - sizeof(*nlh));
  62. msg = (struct cn_msg *)NLMSG_DATA(nlh);
  63. memset(msg, 0, size0);
  64. msg->id.idx = -1;
  65. msg->id.val = -1;
  66. msg->seq = 0x123;
  67. msg->ack = 0x345;
  68. msg->len = size0 - sizeof(*msg);
  69. ctl = (struct cn_ctl_msg *)(msg + 1);
  70. ctl->idx_notify_num = 1;
  71. ctl->val_notify_num = 2;
  72. ctl->group = group;
  73. ctl->len = msg->len - sizeof(*ctl);
  74. req = (struct cn_notify_req *)(ctl + 1);
  75. /*
  76. * Idx.
  77. */
  78. req->first = cn_test_id.idx;
  79. req->range = 10;
  80. /*
  81. * Val 0.
  82. */
  83. req++;
  84. req->first = cn_test_id.val;
  85. req->range = 10;
  86. /*
  87. * Val 1.
  88. */
  89. req++;
  90. req->first = cn_test_id.val + 20;
  91. req->range = 10;
  92. NETLINK_CB(skb).dst_group = ctl->group;
  93. //netlink_broadcast(nls, skb, 0, ctl->group, GFP_ATOMIC);
  94. netlink_unicast(nls, skb, 0, 0);
  95. pr_info("request was sent: group=0x%x\n", ctl->group);
  96. return 0;
  97. nlmsg_failure:
  98. pr_err("failed to send %u.%u\n", msg->seq, msg->ack);
  99. kfree_skb(skb);
  100. return -EINVAL;
  101. }
  102. #endif
  103. static u32 cn_test_timer_counter;
  104. static void cn_test_timer_func(unsigned long __data)
  105. {
  106. struct cn_msg *m;
  107. char data[32];
  108. pr_debug("%s: timer fired with data %lu\n", __func__, __data);
  109. m = kzalloc(sizeof(*m) + sizeof(data), GFP_ATOMIC);
  110. if (m) {
  111. memcpy(&m->id, &cn_test_id, sizeof(m->id));
  112. m->seq = cn_test_timer_counter;
  113. m->len = sizeof(data);
  114. m->len =
  115. scnprintf(data, sizeof(data), "counter = %u",
  116. cn_test_timer_counter) + 1;
  117. memcpy(m + 1, data, m->len);
  118. cn_netlink_send(m, 0, GFP_ATOMIC);
  119. kfree(m);
  120. }
  121. cn_test_timer_counter++;
  122. mod_timer(&cn_test_timer, jiffies + msecs_to_jiffies(1000));
  123. }
  124. static int cn_test_init(void)
  125. {
  126. int err;
  127. err = cn_add_callback(&cn_test_id, cn_test_name, cn_test_callback);
  128. if (err)
  129. goto err_out;
  130. cn_test_id.val++;
  131. err = cn_add_callback(&cn_test_id, cn_test_name, cn_test_callback);
  132. if (err) {
  133. cn_del_callback(&cn_test_id);
  134. goto err_out;
  135. }
  136. setup_timer(&cn_test_timer, cn_test_timer_func, 0);
  137. mod_timer(&cn_test_timer, jiffies + msecs_to_jiffies(1000));
  138. pr_info("initialized with id={%u.%u}\n",
  139. cn_test_id.idx, cn_test_id.val);
  140. return 0;
  141. err_out:
  142. if (nls && nls->sk_socket)
  143. sock_release(nls->sk_socket);
  144. return err;
  145. }
  146. static void cn_test_fini(void)
  147. {
  148. del_timer_sync(&cn_test_timer);
  149. cn_del_callback(&cn_test_id);
  150. cn_test_id.val--;
  151. cn_del_callback(&cn_test_id);
  152. if (nls && nls->sk_socket)
  153. sock_release(nls->sk_socket);
  154. }
  155. module_init(cn_test_init);
  156. module_exit(cn_test_fini);
  157. MODULE_LICENSE("GPL");
  158. MODULE_AUTHOR("Evgeniy Polyakov <zbr@ioremap.net>");
  159. MODULE_DESCRIPTION("Connector's test module");