| Evgeniy Polyakov | 7672d0b | 2005-09-11 19:15:07 -0700 | [diff] [blame] | 1 | /* | 
 | 2 |  * 	cn_test.c | 
 | 3 |  *  | 
| Evgeniy Polyakov | acb9c1b | 2009-07-21 12:43:51 -0700 | [diff] [blame] | 4 |  * 2004+ Copyright (c) Evgeniy Polyakov <zbr@ioremap.net> | 
| Evgeniy Polyakov | 7672d0b | 2005-09-11 19:15:07 -0700 | [diff] [blame] | 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 |  | 
| Mike Frysinger | 37cf2b8 | 2009-07-17 10:14:26 -0700 | [diff] [blame] | 22 | #define pr_fmt(fmt) "cn_test: " fmt | 
 | 23 |  | 
| Evgeniy Polyakov | 7672d0b | 2005-09-11 19:15:07 -0700 | [diff] [blame] | 24 | #include <linux/kernel.h> | 
 | 25 | #include <linux/module.h> | 
 | 26 | #include <linux/moduleparam.h> | 
 | 27 | #include <linux/skbuff.h> | 
| Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 28 | #include <linux/slab.h> | 
| Evgeniy Polyakov | 7672d0b | 2005-09-11 19:15:07 -0700 | [diff] [blame] | 29 | #include <linux/timer.h> | 
 | 30 |  | 
| Carlo Comin | 18a0c23 | 2005-11-08 09:38:56 -0800 | [diff] [blame] | 31 | #include <linux/connector.h> | 
| Evgeniy Polyakov | 7672d0b | 2005-09-11 19:15:07 -0700 | [diff] [blame] | 32 |  | 
| Mike Frysinger | 37cf2b8 | 2009-07-17 10:14:26 -0700 | [diff] [blame] | 33 | static struct cb_id cn_test_id = { CN_NETLINK_USERS + 3, 0x456 }; | 
| Evgeniy Polyakov | 7672d0b | 2005-09-11 19:15:07 -0700 | [diff] [blame] | 34 | static char cn_test_name[] = "cn_test"; | 
 | 35 | static struct sock *nls; | 
 | 36 | static struct timer_list cn_test_timer; | 
 | 37 |  | 
| Philipp Reisner | 7069331 | 2009-10-02 02:40:05 +0000 | [diff] [blame] | 38 | static void cn_test_callback(struct cn_msg *msg, struct netlink_skb_parms *nsp) | 
| Evgeniy Polyakov | 7672d0b | 2005-09-11 19:15:07 -0700 | [diff] [blame] | 39 | { | 
| Mike Frysinger | 37cf2b8 | 2009-07-17 10:14:26 -0700 | [diff] [blame] | 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 : ""); | 
| Evgeniy Polyakov | 7672d0b | 2005-09-11 19:15:07 -0700 | [diff] [blame] | 44 | } | 
 | 45 |  | 
| Jaswinder Singh Rajput | 28f06c6 | 2009-06-17 16:26:30 -0700 | [diff] [blame] | 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 | 
| Evgeniy Polyakov | 7672d0b | 2005-09-11 19:15:07 -0700 | [diff] [blame] | 52 | static 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) { | 
| Mike Frysinger | 37cf2b8 | 2009-07-17 10:14:26 -0700 | [diff] [blame] | 68 | 		pr_err("failed to allocate new skb with size=%u\n", size); | 
| Evgeniy Polyakov | 7672d0b | 2005-09-11 19:15:07 -0700 | [diff] [blame] | 69 | 		return -ENOMEM; | 
 | 70 | 	} | 
 | 71 |  | 
 | 72 | 	nlh = NLMSG_PUT(skb, 0, 0x123, NLMSG_DONE, size - sizeof(*nlh)); | 
 | 73 |  | 
 | 74 | 	msg = (struct cn_msg *)NLMSG_DATA(nlh); | 
 | 75 |  | 
 | 76 | 	memset(msg, 0, size0); | 
 | 77 |  | 
 | 78 | 	msg->id.idx = -1; | 
 | 79 | 	msg->id.val = -1; | 
 | 80 | 	msg->seq = 0x123; | 
 | 81 | 	msg->ack = 0x345; | 
 | 82 | 	msg->len = size0 - sizeof(*msg); | 
 | 83 |  | 
 | 84 | 	ctl = (struct cn_ctl_msg *)(msg + 1); | 
 | 85 |  | 
 | 86 | 	ctl->idx_notify_num = 1; | 
 | 87 | 	ctl->val_notify_num = 2; | 
 | 88 | 	ctl->group = group; | 
 | 89 | 	ctl->len = msg->len - sizeof(*ctl); | 
 | 90 |  | 
 | 91 | 	req = (struct cn_notify_req *)(ctl + 1); | 
 | 92 |  | 
 | 93 | 	/* | 
 | 94 | 	 * Idx. | 
 | 95 | 	 */ | 
 | 96 | 	req->first = cn_test_id.idx; | 
 | 97 | 	req->range = 10; | 
 | 98 |  | 
 | 99 | 	/* | 
 | 100 | 	 * Val 0. | 
 | 101 | 	 */ | 
 | 102 | 	req++; | 
 | 103 | 	req->first = cn_test_id.val; | 
 | 104 | 	req->range = 10; | 
 | 105 |  | 
 | 106 | 	/* | 
 | 107 | 	 * Val 1. | 
 | 108 | 	 */ | 
 | 109 | 	req++; | 
 | 110 | 	req->first = cn_test_id.val + 20; | 
 | 111 | 	req->range = 10; | 
 | 112 |  | 
| Carlo Comin | 18a0c23 | 2005-11-08 09:38:56 -0800 | [diff] [blame] | 113 | 	NETLINK_CB(skb).dst_group = ctl->group; | 
| Evgeniy Polyakov | 7672d0b | 2005-09-11 19:15:07 -0700 | [diff] [blame] | 114 | 	//netlink_broadcast(nls, skb, 0, ctl->group, GFP_ATOMIC); | 
 | 115 | 	netlink_unicast(nls, skb, 0, 0); | 
 | 116 |  | 
| Mike Frysinger | 37cf2b8 | 2009-07-17 10:14:26 -0700 | [diff] [blame] | 117 | 	pr_info("request was sent: group=0x%x\n", ctl->group); | 
| Evgeniy Polyakov | 7672d0b | 2005-09-11 19:15:07 -0700 | [diff] [blame] | 118 |  | 
 | 119 | 	return 0; | 
 | 120 |  | 
 | 121 | nlmsg_failure: | 
| Mike Frysinger | 37cf2b8 | 2009-07-17 10:14:26 -0700 | [diff] [blame] | 122 | 	pr_err("failed to send %u.%u\n", msg->seq, msg->ack); | 
| Evgeniy Polyakov | 7672d0b | 2005-09-11 19:15:07 -0700 | [diff] [blame] | 123 | 	kfree_skb(skb); | 
 | 124 | 	return -EINVAL; | 
 | 125 | } | 
| Jaswinder Singh Rajput | 28f06c6 | 2009-06-17 16:26:30 -0700 | [diff] [blame] | 126 | #endif | 
| Evgeniy Polyakov | 7672d0b | 2005-09-11 19:15:07 -0700 | [diff] [blame] | 127 |  | 
 | 128 | static u32 cn_test_timer_counter; | 
 | 129 | static void cn_test_timer_func(unsigned long __data) | 
 | 130 | { | 
 | 131 | 	struct cn_msg *m; | 
 | 132 | 	char data[32]; | 
 | 133 |  | 
| Mike Frysinger | 37cf2b8 | 2009-07-17 10:14:26 -0700 | [diff] [blame] | 134 | 	pr_debug("%s: timer fired with data %lu\n", __func__, __data); | 
 | 135 |  | 
| Yoann Padioleau | dd00cc4 | 2007-07-19 01:49:03 -0700 | [diff] [blame] | 136 | 	m = kzalloc(sizeof(*m) + sizeof(data), GFP_ATOMIC); | 
| Evgeniy Polyakov | 7672d0b | 2005-09-11 19:15:07 -0700 | [diff] [blame] | 137 | 	if (m) { | 
| Evgeniy Polyakov | 7672d0b | 2005-09-11 19:15:07 -0700 | [diff] [blame] | 138 |  | 
 | 139 | 		memcpy(&m->id, &cn_test_id, sizeof(m->id)); | 
 | 140 | 		m->seq = cn_test_timer_counter; | 
 | 141 | 		m->len = sizeof(data); | 
 | 142 |  | 
 | 143 | 		m->len = | 
 | 144 | 		    scnprintf(data, sizeof(data), "counter = %u", | 
 | 145 | 			      cn_test_timer_counter) + 1; | 
 | 146 |  | 
 | 147 | 		memcpy(m + 1, data, m->len); | 
 | 148 |  | 
| Andrew Morton | f82da72 | 2009-02-12 16:47:01 -0800 | [diff] [blame] | 149 | 		cn_netlink_send(m, 0, GFP_ATOMIC); | 
| Evgeniy Polyakov | 7672d0b | 2005-09-11 19:15:07 -0700 | [diff] [blame] | 150 | 		kfree(m); | 
 | 151 | 	} | 
 | 152 |  | 
 | 153 | 	cn_test_timer_counter++; | 
 | 154 |  | 
| Mike Frysinger | 37cf2b8 | 2009-07-17 10:14:26 -0700 | [diff] [blame] | 155 | 	mod_timer(&cn_test_timer, jiffies + msecs_to_jiffies(1000)); | 
| Evgeniy Polyakov | 7672d0b | 2005-09-11 19:15:07 -0700 | [diff] [blame] | 156 | } | 
 | 157 |  | 
 | 158 | static int cn_test_init(void) | 
 | 159 | { | 
 | 160 | 	int err; | 
 | 161 |  | 
 | 162 | 	err = cn_add_callback(&cn_test_id, cn_test_name, cn_test_callback); | 
 | 163 | 	if (err) | 
 | 164 | 		goto err_out; | 
 | 165 | 	cn_test_id.val++; | 
 | 166 | 	err = cn_add_callback(&cn_test_id, cn_test_name, cn_test_callback); | 
 | 167 | 	if (err) { | 
 | 168 | 		cn_del_callback(&cn_test_id); | 
 | 169 | 		goto err_out; | 
 | 170 | 	} | 
 | 171 |  | 
| Andrew Morton | f82da72 | 2009-02-12 16:47:01 -0800 | [diff] [blame] | 172 | 	setup_timer(&cn_test_timer, cn_test_timer_func, 0); | 
| Mike Frysinger | 37cf2b8 | 2009-07-17 10:14:26 -0700 | [diff] [blame] | 173 | 	mod_timer(&cn_test_timer, jiffies + msecs_to_jiffies(1000)); | 
 | 174 |  | 
 | 175 | 	pr_info("initialized with id={%u.%u}\n", | 
 | 176 | 		cn_test_id.idx, cn_test_id.val); | 
| Evgeniy Polyakov | 7672d0b | 2005-09-11 19:15:07 -0700 | [diff] [blame] | 177 |  | 
 | 178 | 	return 0; | 
 | 179 |  | 
 | 180 |       err_out: | 
 | 181 | 	if (nls && nls->sk_socket) | 
 | 182 | 		sock_release(nls->sk_socket); | 
 | 183 |  | 
 | 184 | 	return err; | 
 | 185 | } | 
 | 186 |  | 
 | 187 | static void cn_test_fini(void) | 
 | 188 | { | 
 | 189 | 	del_timer_sync(&cn_test_timer); | 
 | 190 | 	cn_del_callback(&cn_test_id); | 
 | 191 | 	cn_test_id.val--; | 
 | 192 | 	cn_del_callback(&cn_test_id); | 
 | 193 | 	if (nls && nls->sk_socket) | 
 | 194 | 		sock_release(nls->sk_socket); | 
 | 195 | } | 
 | 196 |  | 
 | 197 | module_init(cn_test_init); | 
 | 198 | module_exit(cn_test_fini); | 
 | 199 |  | 
 | 200 | MODULE_LICENSE("GPL"); | 
| Evgeniy Polyakov | acb9c1b | 2009-07-21 12:43:51 -0700 | [diff] [blame] | 201 | MODULE_AUTHOR("Evgeniy Polyakov <zbr@ioremap.net>"); | 
| Evgeniy Polyakov | 7672d0b | 2005-09-11 19:15:07 -0700 | [diff] [blame] | 202 | MODULE_DESCRIPTION("Connector's test module"); |