4 * 2004+ Copyright (c) Evgeniy Polyakov <zbr@ioremap.net>
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.
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.
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
22 #define pr_fmt(fmt) "cn_test: " fmt
24 #include <linux/kernel.h>
25 #include <linux/module.h>
26 #include <linux/moduleparam.h>
27 #include <linux/skbuff.h>
28 #include <linux/slab.h>
29 #include <linux/timer.h>
31 #include <linux/connector.h>
33 static struct cb_id cn_test_id = { CN_NETLINK_USERS + 3, 0x456 };
34 static char cn_test_name[] = "cn_test";
35 static struct sock *nls;
36 static struct timer_list cn_test_timer;
38 static void cn_test_callback(struct cn_msg *msg, struct netlink_skb_parms *nsp)
40 pr_info("%s: %lu: idx=%x, val=%x, seq=%u, ack=%u, len=%d: %s.\n",
41 __func__, jiffies, msg->id.idx, msg->id.val,
42 msg->seq, msg->ack, msg->len,
43 msg->len ? (char *)msg->data : "");
47 * Do not remove this function even if no one is using it as
48 * this is an example of how to get notifications about new
49 * connector user registration
52 static int cn_test_want_notify(void)
54 struct cn_ctl_msg *ctl;
55 struct cn_notify_req *req;
56 struct cn_msg *msg = NULL;
62 size0 = sizeof(*msg) + sizeof(*ctl) + 3 * sizeof(*req);
64 size = NLMSG_SPACE(size0);
66 skb = alloc_skb(size, GFP_ATOMIC);
68 pr_err("failed to allocate new skb with size=%u\n", size);
72 nlh = NLMSG_PUT(skb, 0, 0x123, NLMSG_DONE, size - sizeof(*nlh));
74 msg = (struct cn_msg *)NLMSG_DATA(nlh);
76 memset(msg, 0, size0);
82 msg->len = size0 - sizeof(*msg);
84 ctl = (struct cn_ctl_msg *)(msg + 1);
86 ctl->idx_notify_num = 1;
87 ctl->val_notify_num = 2;
89 ctl->len = msg->len - sizeof(*ctl);
91 req = (struct cn_notify_req *)(ctl + 1);
96 req->first = cn_test_id.idx;
103 req->first = cn_test_id.val;
110 req->first = cn_test_id.val + 20;
113 NETLINK_CB(skb).dst_group = ctl->group;
114 //netlink_broadcast(nls, skb, 0, ctl->group, GFP_ATOMIC);
115 netlink_unicast(nls, skb, 0, 0);
117 pr_info("request was sent: group=0x%x\n", ctl->group);
122 pr_err("failed to send %u.%u\n", msg->seq, msg->ack);
128 static u32 cn_test_timer_counter;
129 static void cn_test_timer_func(unsigned long __data)
134 pr_debug("%s: timer fired with data %lu\n", __func__, __data);
136 m = kzalloc(sizeof(*m) + sizeof(data), GFP_ATOMIC);
139 memcpy(&m->id, &cn_test_id, sizeof(m->id));
140 m->seq = cn_test_timer_counter;
141 m->len = sizeof(data);
144 scnprintf(data, sizeof(data), "counter = %u",
145 cn_test_timer_counter) + 1;
147 memcpy(m + 1, data, m->len);
149 cn_netlink_send(m, 0, GFP_ATOMIC);
153 cn_test_timer_counter++;
155 mod_timer(&cn_test_timer, jiffies + msecs_to_jiffies(1000));
158 static int cn_test_init(void)
162 err = cn_add_callback(&cn_test_id, cn_test_name, cn_test_callback);
166 err = cn_add_callback(&cn_test_id, cn_test_name, cn_test_callback);
168 cn_del_callback(&cn_test_id);
172 setup_timer(&cn_test_timer, cn_test_timer_func, 0);
173 mod_timer(&cn_test_timer, jiffies + msecs_to_jiffies(1000));
175 pr_info("initialized with id={%u.%u}\n",
176 cn_test_id.idx, cn_test_id.val);
181 if (nls && nls->sk_socket)
182 sock_release(nls->sk_socket);
187 static void cn_test_fini(void)
189 del_timer_sync(&cn_test_timer);
190 cn_del_callback(&cn_test_id);
192 cn_del_callback(&cn_test_id);
193 if (nls && nls->sk_socket)
194 sock_release(nls->sk_socket);
197 module_init(cn_test_init);
198 module_exit(cn_test_fini);
200 MODULE_LICENSE("GPL");
201 MODULE_AUTHOR("Evgeniy Polyakov <zbr@ioremap.net>");
202 MODULE_DESCRIPTION("Connector's test module");