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 #include <linux/kernel.h>
23 #include <linux/module.h>
24 #include <linux/moduleparam.h>
25 #include <linux/skbuff.h>
26 #include <linux/timer.h>
28 #include <linux/connector.h>
30 static struct cb_id cn_test_id
= { 0x123, 0x456 };
31 static char cn_test_name
[] = "cn_test";
32 static struct sock
*nls
;
33 static struct timer_list cn_test_timer
;
35 static void cn_test_callback(struct cn_msg
*msg
, struct netlink_skb_parms
*nsp
)
37 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
);
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
48 static int cn_test_want_notify(void)
50 struct cn_ctl_msg
*ctl
;
51 struct cn_notify_req
*req
;
52 struct cn_msg
*msg
= NULL
;
58 size0
= sizeof(*msg
) + sizeof(*ctl
) + 3 * sizeof(*req
);
60 size
= NLMSG_SPACE(size0
);
62 skb
= alloc_skb(size
, GFP_ATOMIC
);
64 printk(KERN_ERR
"Failed to allocate new skb with size=%u.\n",
70 nlh
= NLMSG_PUT(skb
, 0, 0x123, NLMSG_DONE
, size
- sizeof(*nlh
));
72 msg
= (struct cn_msg
*)NLMSG_DATA(nlh
);
74 memset(msg
, 0, size0
);
80 msg
->len
= size0
- sizeof(*msg
);
82 ctl
= (struct cn_ctl_msg
*)(msg
+ 1);
84 ctl
->idx_notify_num
= 1;
85 ctl
->val_notify_num
= 2;
87 ctl
->len
= msg
->len
- sizeof(*ctl
);
89 req
= (struct cn_notify_req
*)(ctl
+ 1);
94 req
->first
= cn_test_id
.idx
;
101 req
->first
= cn_test_id
.val
;
108 req
->first
= cn_test_id
.val
+ 20;
111 NETLINK_CB(skb
).dst_group
= ctl
->group
;
112 //netlink_broadcast(nls, skb, 0, ctl->group, GFP_ATOMIC);
113 netlink_unicast(nls
, skb
, 0, 0);
115 printk(KERN_INFO
"Request was sent. Group=0x%x.\n", ctl
->group
);
120 printk(KERN_ERR
"Failed to send %u.%u\n", msg
->seq
, msg
->ack
);
126 static u32 cn_test_timer_counter
;
127 static void cn_test_timer_func(unsigned long __data
)
132 m
= kzalloc(sizeof(*m
) + sizeof(data
), GFP_ATOMIC
);
135 memcpy(&m
->id
, &cn_test_id
, sizeof(m
->id
));
136 m
->seq
= cn_test_timer_counter
;
137 m
->len
= sizeof(data
);
140 scnprintf(data
, sizeof(data
), "counter = %u",
141 cn_test_timer_counter
) + 1;
143 memcpy(m
+ 1, data
, m
->len
);
145 cn_netlink_send(m
, 0, GFP_ATOMIC
);
149 cn_test_timer_counter
++;
151 mod_timer(&cn_test_timer
, jiffies
+ HZ
);
154 static int cn_test_init(void)
158 err
= cn_add_callback(&cn_test_id
, cn_test_name
, cn_test_callback
);
162 err
= cn_add_callback(&cn_test_id
, cn_test_name
, cn_test_callback
);
164 cn_del_callback(&cn_test_id
);
168 setup_timer(&cn_test_timer
, cn_test_timer_func
, 0);
169 cn_test_timer
.expires
= jiffies
+ HZ
;
170 add_timer(&cn_test_timer
);
175 if (nls
&& nls
->sk_socket
)
176 sock_release(nls
->sk_socket
);
181 static void cn_test_fini(void)
183 del_timer_sync(&cn_test_timer
);
184 cn_del_callback(&cn_test_id
);
186 cn_del_callback(&cn_test_id
);
187 if (nls
&& nls
->sk_socket
)
188 sock_release(nls
->sk_socket
);
191 module_init(cn_test_init
);
192 module_exit(cn_test_fini
);
194 MODULE_LICENSE("GPL");
195 MODULE_AUTHOR("Evgeniy Polyakov <zbr@ioremap.net>");
196 MODULE_DESCRIPTION("Connector's test module");