Loading...
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * cn_test.c
4 *
5 * 2004+ Copyright (c) Evgeniy Polyakov <zbr@ioremap.net>
6 * All rights reserved.
7 */
8
9#define pr_fmt(fmt) "cn_test: " fmt
10
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>
17
18#include <linux/connector.h>
19
20static struct cb_id cn_test_id = { CN_NETLINK_USERS + 3, 0x456 };
21static char cn_test_name[] = "cn_test";
22static struct sock *nls;
23static struct timer_list cn_test_timer;
24
25static void cn_test_callback(struct cn_msg *msg, struct netlink_skb_parms *nsp)
26{
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 : "");
31}
32
33/*
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
37 */
38#if 0
39static int cn_test_want_notify(void)
40{
41 struct cn_ctl_msg *ctl;
42 struct cn_notify_req *req;
43 struct cn_msg *msg = NULL;
44 int size, size0;
45 struct sk_buff *skb;
46 struct nlmsghdr *nlh;
47 u32 group = 1;
48
49 size0 = sizeof(*msg) + sizeof(*ctl) + 3 * sizeof(*req);
50
51 size = NLMSG_SPACE(size0);
52
53 skb = alloc_skb(size, GFP_ATOMIC);
54 if (!skb) {
55 pr_err("failed to allocate new skb with size=%u\n", size);
56 return -ENOMEM;
57 }
58
59 nlh = nlmsg_put(skb, 0, 0x123, NLMSG_DONE, size - sizeof(*nlh), 0);
60 if (!nlh) {
61 kfree_skb(skb);
62 return -EMSGSIZE;
63 }
64
65 msg = nlmsg_data(nlh);
66
67 memset(msg, 0, size0);
68
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
75 ctl = (struct cn_ctl_msg *)(msg + 1);
76
77 ctl->idx_notify_num = 1;
78 ctl->val_notify_num = 2;
79 ctl->group = group;
80 ctl->len = msg->len - sizeof(*ctl);
81
82 req = (struct cn_notify_req *)(ctl + 1);
83
84 /*
85 * Idx.
86 */
87 req->first = cn_test_id.idx;
88 req->range = 10;
89
90 /*
91 * Val 0.
92 */
93 req++;
94 req->first = cn_test_id.val;
95 req->range = 10;
96
97 /*
98 * Val 1.
99 */
100 req++;
101 req->first = cn_test_id.val + 20;
102 req->range = 10;
103
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);
107
108 pr_info("request was sent: group=0x%x\n", ctl->group);
109
110 return 0;
111}
112#endif
113
114static u32 cn_test_timer_counter;
115static void cn_test_timer_func(struct timer_list *unused)
116{
117 struct cn_msg *m;
118 char data[32];
119
120 pr_debug("%s: timer fired\n", __func__);
121
122 m = kzalloc(sizeof(*m) + sizeof(data), GFP_ATOMIC);
123 if (m) {
124
125 memcpy(&m->id, &cn_test_id, sizeof(m->id));
126 m->seq = cn_test_timer_counter;
127 m->len = sizeof(data);
128
129 m->len =
130 scnprintf(data, sizeof(data), "counter = %u",
131 cn_test_timer_counter) + 1;
132
133 memcpy(m + 1, data, m->len);
134
135 cn_netlink_send(m, 0, 0, GFP_ATOMIC);
136 kfree(m);
137 }
138
139 cn_test_timer_counter++;
140
141 mod_timer(&cn_test_timer, jiffies + msecs_to_jiffies(1000));
142}
143
144static int cn_test_init(void)
145{
146 int err;
147
148 err = cn_add_callback(&cn_test_id, cn_test_name, cn_test_callback);
149 if (err)
150 goto err_out;
151 cn_test_id.val++;
152 err = cn_add_callback(&cn_test_id, cn_test_name, cn_test_callback);
153 if (err) {
154 cn_del_callback(&cn_test_id);
155 goto err_out;
156 }
157
158 timer_setup(&cn_test_timer, cn_test_timer_func, 0);
159 mod_timer(&cn_test_timer, jiffies + msecs_to_jiffies(1000));
160
161 pr_info("initialized with id={%u.%u}\n",
162 cn_test_id.idx, cn_test_id.val);
163
164 return 0;
165
166 err_out:
167 if (nls && nls->sk_socket)
168 sock_release(nls->sk_socket);
169
170 return err;
171}
172
173static void cn_test_fini(void)
174{
175 del_timer_sync(&cn_test_timer);
176 cn_del_callback(&cn_test_id);
177 cn_test_id.val--;
178 cn_del_callback(&cn_test_id);
179 if (nls && nls->sk_socket)
180 sock_release(nls->sk_socket);
181}
182
183module_init(cn_test_init);
184module_exit(cn_test_fini);
185
186MODULE_LICENSE("GPL");
187MODULE_AUTHOR("Evgeniy Polyakov <zbr@ioremap.net>");
188MODULE_DESCRIPTION("Connector's test module");
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
22#define pr_fmt(fmt) "cn_test: " fmt
23
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>
30
31#include <linux/connector.h>
32
33static struct cb_id cn_test_id = { CN_NETLINK_USERS + 3, 0x456 };
34static char cn_test_name[] = "cn_test";
35static struct sock *nls;
36static struct timer_list cn_test_timer;
37
38static void cn_test_callback(struct cn_msg *msg, struct netlink_skb_parms *nsp)
39{
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 : "");
44}
45
46/*
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
50 */
51#if 0
52static int cn_test_want_notify(void)
53{
54 struct cn_ctl_msg *ctl;
55 struct cn_notify_req *req;
56 struct cn_msg *msg = NULL;
57 int size, size0;
58 struct sk_buff *skb;
59 struct nlmsghdr *nlh;
60 u32 group = 1;
61
62 size0 = sizeof(*msg) + sizeof(*ctl) + 3 * sizeof(*req);
63
64 size = NLMSG_SPACE(size0);
65
66 skb = alloc_skb(size, GFP_ATOMIC);
67 if (!skb) {
68 pr_err("failed to allocate new skb with size=%u\n", size);
69 return -ENOMEM;
70 }
71
72 nlh = nlmsg_put(skb, 0, 0x123, NLMSG_DONE, size - sizeof(*nlh), 0);
73 if (!nlh) {
74 kfree_skb(skb);
75 return -EMSGSIZE;
76 }
77
78 msg = nlmsg_data(nlh);
79
80 memset(msg, 0, size0);
81
82 msg->id.idx = -1;
83 msg->id.val = -1;
84 msg->seq = 0x123;
85 msg->ack = 0x345;
86 msg->len = size0 - sizeof(*msg);
87
88 ctl = (struct cn_ctl_msg *)(msg + 1);
89
90 ctl->idx_notify_num = 1;
91 ctl->val_notify_num = 2;
92 ctl->group = group;
93 ctl->len = msg->len - sizeof(*ctl);
94
95 req = (struct cn_notify_req *)(ctl + 1);
96
97 /*
98 * Idx.
99 */
100 req->first = cn_test_id.idx;
101 req->range = 10;
102
103 /*
104 * Val 0.
105 */
106 req++;
107 req->first = cn_test_id.val;
108 req->range = 10;
109
110 /*
111 * Val 1.
112 */
113 req++;
114 req->first = cn_test_id.val + 20;
115 req->range = 10;
116
117 NETLINK_CB(skb).dst_group = ctl->group;
118 //netlink_broadcast(nls, skb, 0, ctl->group, GFP_ATOMIC);
119 netlink_unicast(nls, skb, 0, 0);
120
121 pr_info("request was sent: group=0x%x\n", ctl->group);
122
123 return 0;
124}
125#endif
126
127static u32 cn_test_timer_counter;
128static void cn_test_timer_func(struct timer_list *unused)
129{
130 struct cn_msg *m;
131 char data[32];
132
133 pr_debug("%s: timer fired\n", __func__);
134
135 m = kzalloc(sizeof(*m) + sizeof(data), GFP_ATOMIC);
136 if (m) {
137
138 memcpy(&m->id, &cn_test_id, sizeof(m->id));
139 m->seq = cn_test_timer_counter;
140 m->len = sizeof(data);
141
142 m->len =
143 scnprintf(data, sizeof(data), "counter = %u",
144 cn_test_timer_counter) + 1;
145
146 memcpy(m + 1, data, m->len);
147
148 cn_netlink_send(m, 0, 0, GFP_ATOMIC);
149 kfree(m);
150 }
151
152 cn_test_timer_counter++;
153
154 mod_timer(&cn_test_timer, jiffies + msecs_to_jiffies(1000));
155}
156
157static int cn_test_init(void)
158{
159 int err;
160
161 err = cn_add_callback(&cn_test_id, cn_test_name, cn_test_callback);
162 if (err)
163 goto err_out;
164 cn_test_id.val++;
165 err = cn_add_callback(&cn_test_id, cn_test_name, cn_test_callback);
166 if (err) {
167 cn_del_callback(&cn_test_id);
168 goto err_out;
169 }
170
171 timer_setup(&cn_test_timer, cn_test_timer_func, 0);
172 mod_timer(&cn_test_timer, jiffies + msecs_to_jiffies(1000));
173
174 pr_info("initialized with id={%u.%u}\n",
175 cn_test_id.idx, cn_test_id.val);
176
177 return 0;
178
179 err_out:
180 if (nls && nls->sk_socket)
181 sock_release(nls->sk_socket);
182
183 return err;
184}
185
186static void cn_test_fini(void)
187{
188 del_timer_sync(&cn_test_timer);
189 cn_del_callback(&cn_test_id);
190 cn_test_id.val--;
191 cn_del_callback(&cn_test_id);
192 if (nls && nls->sk_socket)
193 sock_release(nls->sk_socket);
194}
195
196module_init(cn_test_init);
197module_exit(cn_test_fini);
198
199MODULE_LICENSE("GPL");
200MODULE_AUTHOR("Evgeniy Polyakov <zbr@ioremap.net>");
201MODULE_DESCRIPTION("Connector's test module");