[PATCH] w1: clean up W1_CON dependency.
[linux-2.6/verdex.git] / Documentation / connector / cn_test.c
blob3e73231695b3107eb743452836e8d0b24d17af85
1 /*
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.
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 void cn_test_callback(void *data)
37 struct cn_msg *msg = (struct cn_msg *)data;
39 printk("%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, (char *)msg->data);
44 static int cn_test_want_notify(void)
46 struct cn_ctl_msg *ctl;
47 struct cn_notify_req *req;
48 struct cn_msg *msg = NULL;
49 int size, size0;
50 struct sk_buff *skb;
51 struct nlmsghdr *nlh;
52 u32 group = 1;
54 size0 = sizeof(*msg) + sizeof(*ctl) + 3 * sizeof(*req);
56 size = NLMSG_SPACE(size0);
58 skb = alloc_skb(size, GFP_ATOMIC);
59 if (!skb) {
60 printk(KERN_ERR "Failed to allocate new skb with size=%u.\n",
61 size);
63 return -ENOMEM;
66 nlh = NLMSG_PUT(skb, 0, 0x123, NLMSG_DONE, size - sizeof(*nlh));
68 msg = (struct cn_msg *)NLMSG_DATA(nlh);
70 memset(msg, 0, size0);
72 msg->id.idx = -1;
73 msg->id.val = -1;
74 msg->seq = 0x123;
75 msg->ack = 0x345;
76 msg->len = size0 - sizeof(*msg);
78 ctl = (struct cn_ctl_msg *)(msg + 1);
80 ctl->idx_notify_num = 1;
81 ctl->val_notify_num = 2;
82 ctl->group = group;
83 ctl->len = msg->len - sizeof(*ctl);
85 req = (struct cn_notify_req *)(ctl + 1);
88 * Idx.
90 req->first = cn_test_id.idx;
91 req->range = 10;
94 * Val 0.
96 req++;
97 req->first = cn_test_id.val;
98 req->range = 10;
101 * Val 1.
103 req++;
104 req->first = cn_test_id.val + 20;
105 req->range = 10;
107 NETLINK_CB(skb).dst_group = ctl->group;
108 //netlink_broadcast(nls, skb, 0, ctl->group, GFP_ATOMIC);
109 netlink_unicast(nls, skb, 0, 0);
111 printk(KERN_INFO "Request was sent. Group=0x%x.\n", ctl->group);
113 return 0;
115 nlmsg_failure:
116 printk(KERN_ERR "Failed to send %u.%u\n", msg->seq, msg->ack);
117 kfree_skb(skb);
118 return -EINVAL;
121 static u32 cn_test_timer_counter;
122 static void cn_test_timer_func(unsigned long __data)
124 struct cn_msg *m;
125 char data[32];
127 m = kmalloc(sizeof(*m) + sizeof(data), GFP_ATOMIC);
128 if (m) {
129 memset(m, 0, sizeof(*m) + sizeof(data));
131 memcpy(&m->id, &cn_test_id, sizeof(m->id));
132 m->seq = cn_test_timer_counter;
133 m->len = sizeof(data);
135 m->len =
136 scnprintf(data, sizeof(data), "counter = %u",
137 cn_test_timer_counter) + 1;
139 memcpy(m + 1, data, m->len);
141 cn_netlink_send(m, 0, gfp_any());
142 kfree(m);
145 cn_test_timer_counter++;
147 mod_timer(&cn_test_timer, jiffies + HZ);
150 static int cn_test_init(void)
152 int err;
154 err = cn_add_callback(&cn_test_id, cn_test_name, cn_test_callback);
155 if (err)
156 goto err_out;
157 cn_test_id.val++;
158 err = cn_add_callback(&cn_test_id, cn_test_name, cn_test_callback);
159 if (err) {
160 cn_del_callback(&cn_test_id);
161 goto err_out;
164 init_timer(&cn_test_timer);
165 cn_test_timer.function = cn_test_timer_func;
166 cn_test_timer.expires = jiffies + HZ;
167 cn_test_timer.data = 0;
168 add_timer(&cn_test_timer);
170 return 0;
172 err_out:
173 if (nls && nls->sk_socket)
174 sock_release(nls->sk_socket);
176 return err;
179 static void cn_test_fini(void)
181 del_timer_sync(&cn_test_timer);
182 cn_del_callback(&cn_test_id);
183 cn_test_id.val--;
184 cn_del_callback(&cn_test_id);
185 if (nls && nls->sk_socket)
186 sock_release(nls->sk_socket);
189 module_init(cn_test_init);
190 module_exit(cn_test_fini);
192 MODULE_LICENSE("GPL");
193 MODULE_AUTHOR("Evgeniy Polyakov <johnpol@2ka.mipt.ru>");
194 MODULE_DESCRIPTION("Connector's test module");