1 // SPDX-License-Identifier: GPL-2.0-or-later
5 * 2004+ Copyright (c) Evgeniy Polyakov <zbr@ioremap.net>
9 #define pr_fmt(fmt) "cn_test: " fmt
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/moduleparam.h>
14 #include <linux/skbuff.h>
15 #include <linux/slab.h>
16 #include <linux/timer.h>
18 #include <linux/connector.h>
20 static struct cb_id cn_test_id = { CN_NETLINK_USERS + 3, 0x456 };
21 static char cn_test_name[] = "cn_test";
22 static struct sock *nls;
23 static struct timer_list cn_test_timer;
25 static void cn_test_callback(struct cn_msg *msg, struct netlink_skb_parms *nsp)
27 pr_info("%s: %lu: idx=%x, val=%x, seq=%u, ack=%u, len=%d: %s.\n",
28 __func__, jiffies, msg->id.idx, msg->id.val,
29 msg->seq, msg->ack, msg->len,
30 msg->len ? (char *)msg->data : "");
34 * Do not remove this function even if no one is using it as
35 * this is an example of how to get notifications about new
36 * connector user registration
39 static int cn_test_want_notify(void)
41 struct cn_ctl_msg *ctl;
42 struct cn_notify_req *req;
43 struct cn_msg *msg = NULL;
49 size0 = sizeof(*msg) + sizeof(*ctl) + 3 * sizeof(*req);
51 size = NLMSG_SPACE(size0);
53 skb = alloc_skb(size, GFP_ATOMIC);
55 pr_err("failed to allocate new skb with size=%u\n", size);
59 nlh = nlmsg_put(skb, 0, 0x123, NLMSG_DONE, size - sizeof(*nlh), 0);
65 msg = nlmsg_data(nlh);
67 memset(msg, 0, size0);
73 msg->len = size0 - sizeof(*msg);
75 ctl = (struct cn_ctl_msg *)(msg + 1);
77 ctl->idx_notify_num = 1;
78 ctl->val_notify_num = 2;
80 ctl->len = msg->len - sizeof(*ctl);
82 req = (struct cn_notify_req *)(ctl + 1);
87 req->first = cn_test_id.idx;
94 req->first = cn_test_id.val;
101 req->first = cn_test_id.val + 20;
104 NETLINK_CB(skb).dst_group = ctl->group;
105 //netlink_broadcast(nls, skb, 0, ctl->group, GFP_ATOMIC);
106 netlink_unicast(nls, skb, 0, 0);
108 pr_info("request was sent: group=0x%x\n", ctl->group);
114 static u32 cn_test_timer_counter;
115 static void cn_test_timer_func(struct timer_list *unused)
120 pr_debug("%s: timer fired\n", __func__);
122 m = kzalloc(sizeof(*m) + sizeof(data), GFP_ATOMIC);
125 memcpy(&m->id, &cn_test_id, sizeof(m->id));
126 m->seq = cn_test_timer_counter;
127 m->len = sizeof(data);
130 scnprintf(data, sizeof(data), "counter = %u",
131 cn_test_timer_counter) + 1;
133 memcpy(m + 1, data, m->len);
135 cn_netlink_send(m, 0, 0, GFP_ATOMIC);
139 cn_test_timer_counter++;
141 mod_timer(&cn_test_timer, jiffies + msecs_to_jiffies(1000));
144 static int cn_test_init(void)
148 err = cn_add_callback(&cn_test_id, cn_test_name, cn_test_callback);
152 err = cn_add_callback(&cn_test_id, cn_test_name, cn_test_callback);
154 cn_del_callback(&cn_test_id);
158 timer_setup(&cn_test_timer, cn_test_timer_func, 0);
159 mod_timer(&cn_test_timer, jiffies + msecs_to_jiffies(1000));
161 pr_info("initialized with id={%u.%u}\n",
162 cn_test_id.idx, cn_test_id.val);
167 if (nls && nls->sk_socket)
168 sock_release(nls->sk_socket);
173 static void cn_test_fini(void)
175 del_timer_sync(&cn_test_timer);
176 cn_del_callback(&cn_test_id);
178 cn_del_callback(&cn_test_id);
179 if (nls && nls->sk_socket)
180 sock_release(nls->sk_socket);
183 module_init(cn_test_init);
184 module_exit(cn_test_fini);
186 MODULE_LICENSE("GPL");
187 MODULE_AUTHOR("Evgeniy Polyakov <zbr@ioremap.net>");
188 MODULE_DESCRIPTION("Connector's test module");