cn_test.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include <linux/moduleparam.h>
  24. #include <linux/skbuff.h>
  25. #include <linux/timer.h>
  26. #include <linux/connector.h>
  27. static struct cb_id cn_test_id = { 0x123, 0x456 };
  28. static char cn_test_name[] = "cn_test";
  29. static struct sock *nls;
  30. static struct timer_list cn_test_timer;
  31. void cn_test_callback(void *data)
  32. {
  33. struct cn_msg *msg = (struct cn_msg *)data;
  34. printk("%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, (char *)msg->data);
  37. }
  38. /*
  39. * Do not remove this function even if no one is using it as
  40. * this is an example of how to get notifications about new
  41. * connector user registration
  42. */
  43. #if 0
  44. static int cn_test_want_notify(void)
  45. {
  46. struct cn_ctl_msg *ctl;
  47. struct cn_notify_req *req;
  48. struct cn_msg *msg = NULL;
  49. int size, size0;
  50. struct sk_buff *skb;
  51. struct nlmsghdr *nlh;
  52. u32 group = 1;
  53. size0 = sizeof(*msg) + sizeof(*ctl) + 3 * sizeof(*req);
  54. size = NLMSG_SPACE(size0);
  55. skb = alloc_skb(size, GFP_ATOMIC);
  56. if (!skb) {
  57. printk(KERN_ERR "Failed to allocate new skb with size=%u.\n",
  58. 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. printk(KERN_INFO "Request was sent. Group=0x%x.\n", ctl->group);
  96. return 0;
  97. nlmsg_failure:
  98. printk(KERN_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. m = kzalloc(sizeof(*m) + sizeof(data), GFP_ATOMIC);
  109. if (m) {
  110. memcpy(&m->id, &cn_test_id, sizeof(m->id));
  111. m->seq = cn_test_timer_counter;
  112. m->len = sizeof(data);
  113. m->len =
  114. scnprintf(data, sizeof(data), "counter = %u",
  115. cn_test_timer_counter) + 1;
  116. memcpy(m + 1, data, m->len);
  117. cn_netlink_send(m, 0, GFP_ATOMIC);
  118. kfree(m);
  119. }
  120. cn_test_timer_counter++;
  121. mod_timer(&cn_test_timer, jiffies + HZ);
  122. }
  123. static int cn_test_init(void)
  124. {
  125. int err;
  126. err = cn_add_callback(&cn_test_id, cn_test_name, cn_test_callback);
  127. if (err)
  128. goto err_out;
  129. cn_test_id.val++;
  130. err = cn_add_callback(&cn_test_id, cn_test_name, cn_test_callback);
  131. if (err) {
  132. cn_del_callback(&cn_test_id);
  133. goto err_out;
  134. }
  135. setup_timer(&cn_test_timer, cn_test_timer_func, 0);
  136. cn_test_timer.expires = jiffies + HZ;
  137. add_timer(&cn_test_timer);
  138. return 0;
  139. err_out:
  140. if (nls && nls->sk_socket)
  141. sock_release(nls->sk_socket);
  142. return err;
  143. }
  144. static void cn_test_fini(void)
  145. {
  146. del_timer_sync(&cn_test_timer);
  147. cn_del_callback(&cn_test_id);
  148. cn_test_id.val--;
  149. cn_del_callback(&cn_test_id);
  150. if (nls && nls->sk_socket)
  151. sock_release(nls->sk_socket);
  152. }
  153. module_init(cn_test_init);
  154. module_exit(cn_test_fini);
  155. MODULE_LICENSE("GPL");
  156. MODULE_AUTHOR("Evgeniy Polyakov <zbr@ioremap.net>");
  157. MODULE_DESCRIPTION("Connector's test module");