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