cn_test.c 4.5 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/slab.h>
  27. #include <linux/timer.h>
  28. #include <linux/connector.h>
  29. static struct cb_id cn_test_id = { CN_NETLINK_USERS + 3, 0x456 };
  30. static char cn_test_name[] = "cn_test";
  31. static struct sock *nls;
  32. static struct timer_list cn_test_timer;
  33. static void cn_test_callback(struct cn_msg *msg, struct netlink_skb_parms *nsp)
  34. {
  35. pr_info("%s: %lu: idx=%x, val=%x, seq=%u, ack=%u, len=%d: %s.\n",
  36. __func__, jiffies, msg->id.idx, msg->id.val,
  37. msg->seq, msg->ack, msg->len,
  38. msg->len ? (char *)msg->data : "");
  39. }
  40. /*
  41. * Do not remove this function even if no one is using it as
  42. * this is an example of how to get notifications about new
  43. * connector user registration
  44. */
  45. #if 0
  46. static int cn_test_want_notify(void)
  47. {
  48. struct cn_ctl_msg *ctl;
  49. struct cn_notify_req *req;
  50. struct cn_msg *msg = NULL;
  51. int size, size0;
  52. struct sk_buff *skb;
  53. struct nlmsghdr *nlh;
  54. u32 group = 1;
  55. size0 = sizeof(*msg) + sizeof(*ctl) + 3 * sizeof(*req);
  56. size = NLMSG_SPACE(size0);
  57. skb = alloc_skb(size, GFP_ATOMIC);
  58. if (!skb) {
  59. pr_err("failed to allocate new skb with size=%u\n", size);
  60. return -ENOMEM;
  61. }
  62. nlh = nlmsg_put(skb, 0, 0x123, NLMSG_DONE, size - sizeof(*nlh), 0);
  63. if (!nlh) {
  64. kfree_skb(skb);
  65. return -EMSGSIZE;
  66. }
  67. msg = nlmsg_data(nlh);
  68. memset(msg, 0, size0);
  69. msg->id.idx = -1;
  70. msg->id.val = -1;
  71. msg->seq = 0x123;
  72. msg->ack = 0x345;
  73. msg->len = size0 - sizeof(*msg);
  74. ctl = (struct cn_ctl_msg *)(msg + 1);
  75. ctl->idx_notify_num = 1;
  76. ctl->val_notify_num = 2;
  77. ctl->group = group;
  78. ctl->len = msg->len - sizeof(*ctl);
  79. req = (struct cn_notify_req *)(ctl + 1);
  80. /*
  81. * Idx.
  82. */
  83. req->first = cn_test_id.idx;
  84. req->range = 10;
  85. /*
  86. * Val 0.
  87. */
  88. req++;
  89. req->first = cn_test_id.val;
  90. req->range = 10;
  91. /*
  92. * Val 1.
  93. */
  94. req++;
  95. req->first = cn_test_id.val + 20;
  96. req->range = 10;
  97. NETLINK_CB(skb).dst_group = ctl->group;
  98. //netlink_broadcast(nls, skb, 0, ctl->group, GFP_ATOMIC);
  99. netlink_unicast(nls, skb, 0, 0);
  100. pr_info("request was sent: group=0x%x\n", ctl->group);
  101. return 0;
  102. }
  103. #endif
  104. static u32 cn_test_timer_counter;
  105. static void cn_test_timer_func(unsigned long __data)
  106. {
  107. struct cn_msg *m;
  108. char data[32];
  109. pr_debug("%s: timer fired with data %lu\n", __func__, __data);
  110. m = kzalloc(sizeof(*m) + sizeof(data), GFP_ATOMIC);
  111. if (m) {
  112. memcpy(&m->id, &cn_test_id, sizeof(m->id));
  113. m->seq = cn_test_timer_counter;
  114. m->len = sizeof(data);
  115. m->len =
  116. scnprintf(data, sizeof(data), "counter = %u",
  117. cn_test_timer_counter) + 1;
  118. memcpy(m + 1, data, m->len);
  119. cn_netlink_send(m, 0, 0, GFP_ATOMIC);
  120. kfree(m);
  121. }
  122. cn_test_timer_counter++;
  123. mod_timer(&cn_test_timer, jiffies + msecs_to_jiffies(1000));
  124. }
  125. static int cn_test_init(void)
  126. {
  127. int err;
  128. err = cn_add_callback(&cn_test_id, cn_test_name, cn_test_callback);
  129. if (err)
  130. goto err_out;
  131. cn_test_id.val++;
  132. err = cn_add_callback(&cn_test_id, cn_test_name, cn_test_callback);
  133. if (err) {
  134. cn_del_callback(&cn_test_id);
  135. goto err_out;
  136. }
  137. setup_timer(&cn_test_timer, cn_test_timer_func, 0);
  138. mod_timer(&cn_test_timer, jiffies + msecs_to_jiffies(1000));
  139. pr_info("initialized with id={%u.%u}\n",
  140. cn_test_id.idx, cn_test_id.val);
  141. return 0;
  142. err_out:
  143. if (nls && nls->sk_socket)
  144. sock_release(nls->sk_socket);
  145. return err;
  146. }
  147. static void cn_test_fini(void)
  148. {
  149. del_timer_sync(&cn_test_timer);
  150. cn_del_callback(&cn_test_id);
  151. cn_test_id.val--;
  152. cn_del_callback(&cn_test_id);
  153. if (nls && nls->sk_socket)
  154. sock_release(nls->sk_socket);
  155. }
  156. module_init(cn_test_init);
  157. module_exit(cn_test_fini);
  158. MODULE_LICENSE("GPL");
  159. MODULE_AUTHOR("Evgeniy Polyakov <zbr@ioremap.net>");
  160. MODULE_DESCRIPTION("Connector's test module");