blob: 50d5ce4899c8882777cc9ea9ab3478843539bce6 [file] [log] [blame]
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -07001/*
2 * cn_test.c
3 *
4 * 2004-2005 Copyright (c) Evgeniy Polyakov <johnpol@2ka.mipt.ru>
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#include <linux/kernel.h>
23#include <linux/module.h>
24#include <linux/moduleparam.h>
25#include <linux/skbuff.h>
26#include <linux/timer.h>
27
Carlo Comin18a0c232005-11-08 09:38:56 -080028#include <linux/connector.h>
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -070029
30static struct cb_id cn_test_id = { 0x123, 0x456 };
31static char cn_test_name[] = "cn_test";
32static struct sock *nls;
33static struct timer_list cn_test_timer;
34
Mike Frysinger07412412009-07-17 10:13:21 -070035void cn_test_callback(struct cn_msg *msg)
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -070036{
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -070037 printk("%s: %lu: idx=%x, val=%x, seq=%u, ack=%u, len=%d: %s.\n",
38 __func__, jiffies, msg->id.idx, msg->id.val,
39 msg->seq, msg->ack, msg->len, (char *)msg->data);
40}
41
Jaswinder Singh Rajput28f06c62009-06-17 16:26:30 -070042/*
43 * Do not remove this function even if no one is using it as
44 * this is an example of how to get notifications about new
45 * connector user registration
46 */
47#if 0
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -070048static int cn_test_want_notify(void)
49{
50 struct cn_ctl_msg *ctl;
51 struct cn_notify_req *req;
52 struct cn_msg *msg = NULL;
53 int size, size0;
54 struct sk_buff *skb;
55 struct nlmsghdr *nlh;
56 u32 group = 1;
57
58 size0 = sizeof(*msg) + sizeof(*ctl) + 3 * sizeof(*req);
59
60 size = NLMSG_SPACE(size0);
61
62 skb = alloc_skb(size, GFP_ATOMIC);
63 if (!skb) {
64 printk(KERN_ERR "Failed to allocate new skb with size=%u.\n",
65 size);
66
67 return -ENOMEM;
68 }
69
70 nlh = NLMSG_PUT(skb, 0, 0x123, NLMSG_DONE, size - sizeof(*nlh));
71
72 msg = (struct cn_msg *)NLMSG_DATA(nlh);
73
74 memset(msg, 0, size0);
75
76 msg->id.idx = -1;
77 msg->id.val = -1;
78 msg->seq = 0x123;
79 msg->ack = 0x345;
80 msg->len = size0 - sizeof(*msg);
81
82 ctl = (struct cn_ctl_msg *)(msg + 1);
83
84 ctl->idx_notify_num = 1;
85 ctl->val_notify_num = 2;
86 ctl->group = group;
87 ctl->len = msg->len - sizeof(*ctl);
88
89 req = (struct cn_notify_req *)(ctl + 1);
90
91 /*
92 * Idx.
93 */
94 req->first = cn_test_id.idx;
95 req->range = 10;
96
97 /*
98 * Val 0.
99 */
100 req++;
101 req->first = cn_test_id.val;
102 req->range = 10;
103
104 /*
105 * Val 1.
106 */
107 req++;
108 req->first = cn_test_id.val + 20;
109 req->range = 10;
110
Carlo Comin18a0c232005-11-08 09:38:56 -0800111 NETLINK_CB(skb).dst_group = ctl->group;
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -0700112 //netlink_broadcast(nls, skb, 0, ctl->group, GFP_ATOMIC);
113 netlink_unicast(nls, skb, 0, 0);
114
115 printk(KERN_INFO "Request was sent. Group=0x%x.\n", ctl->group);
116
117 return 0;
118
119nlmsg_failure:
120 printk(KERN_ERR "Failed to send %u.%u\n", msg->seq, msg->ack);
121 kfree_skb(skb);
122 return -EINVAL;
123}
Jaswinder Singh Rajput28f06c62009-06-17 16:26:30 -0700124#endif
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -0700125
126static u32 cn_test_timer_counter;
127static void cn_test_timer_func(unsigned long __data)
128{
129 struct cn_msg *m;
130 char data[32];
131
Yoann Padioleaudd00cc42007-07-19 01:49:03 -0700132 m = kzalloc(sizeof(*m) + sizeof(data), GFP_ATOMIC);
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -0700133 if (m) {
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -0700134
135 memcpy(&m->id, &cn_test_id, sizeof(m->id));
136 m->seq = cn_test_timer_counter;
137 m->len = sizeof(data);
138
139 m->len =
140 scnprintf(data, sizeof(data), "counter = %u",
141 cn_test_timer_counter) + 1;
142
143 memcpy(m + 1, data, m->len);
144
Andrew Mortonf82da722009-02-12 16:47:01 -0800145 cn_netlink_send(m, 0, GFP_ATOMIC);
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -0700146 kfree(m);
147 }
148
149 cn_test_timer_counter++;
150
151 mod_timer(&cn_test_timer, jiffies + HZ);
152}
153
154static int cn_test_init(void)
155{
156 int err;
157
158 err = cn_add_callback(&cn_test_id, cn_test_name, cn_test_callback);
159 if (err)
160 goto err_out;
161 cn_test_id.val++;
162 err = cn_add_callback(&cn_test_id, cn_test_name, cn_test_callback);
163 if (err) {
164 cn_del_callback(&cn_test_id);
165 goto err_out;
166 }
167
Andrew Mortonf82da722009-02-12 16:47:01 -0800168 setup_timer(&cn_test_timer, cn_test_timer_func, 0);
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -0700169 cn_test_timer.expires = jiffies + HZ;
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -0700170 add_timer(&cn_test_timer);
171
172 return 0;
173
174 err_out:
175 if (nls && nls->sk_socket)
176 sock_release(nls->sk_socket);
177
178 return err;
179}
180
181static void cn_test_fini(void)
182{
183 del_timer_sync(&cn_test_timer);
184 cn_del_callback(&cn_test_id);
185 cn_test_id.val--;
186 cn_del_callback(&cn_test_id);
187 if (nls && nls->sk_socket)
188 sock_release(nls->sk_socket);
189}
190
191module_init(cn_test_init);
192module_exit(cn_test_fini);
193
194MODULE_LICENSE("GPL");
195MODULE_AUTHOR("Evgeniy Polyakov <johnpol@2ka.mipt.ru>");
196MODULE_DESCRIPTION("Connector's test module");