^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * cn_test.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * 2004+ Copyright (c) Evgeniy Polyakov <zbr@ioremap.net>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * All rights reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #define pr_fmt(fmt) "cn_test: " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/moduleparam.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/skbuff.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/timer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/connector.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) static struct cb_id cn_test_id = { CN_NETLINK_USERS + 3, 0x456 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) static char cn_test_name[] = "cn_test";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) static struct sock *nls;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) static struct timer_list cn_test_timer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) static void cn_test_callback(struct cn_msg *msg, struct netlink_skb_parms *nsp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) pr_info("%s: %lu: idx=%x, val=%x, seq=%u, ack=%u, len=%d: %s.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) __func__, jiffies, msg->id.idx, msg->id.val,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) msg->seq, msg->ack, msg->len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) msg->len ? (char *)msg->data : "");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) * Do not remove this function even if no one is using it as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) * this is an example of how to get notifications about new
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) * connector user registration
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) #if 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) static int cn_test_want_notify(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) struct cn_ctl_msg *ctl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) struct cn_notify_req *req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) struct cn_msg *msg = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) int size, size0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) struct sk_buff *skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) struct nlmsghdr *nlh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) u32 group = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) size0 = sizeof(*msg) + sizeof(*ctl) + 3 * sizeof(*req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) size = NLMSG_SPACE(size0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) skb = alloc_skb(size, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) if (!skb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) pr_err("failed to allocate new skb with size=%u\n", size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) nlh = nlmsg_put(skb, 0, 0x123, NLMSG_DONE, size - sizeof(*nlh), 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) if (!nlh) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) return -EMSGSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) msg = nlmsg_data(nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) memset(msg, 0, size0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) msg->id.idx = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) msg->id.val = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) msg->seq = 0x123;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) msg->ack = 0x345;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) msg->len = size0 - sizeof(*msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) ctl = (struct cn_ctl_msg *)(msg + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) ctl->idx_notify_num = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) ctl->val_notify_num = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) ctl->group = group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) ctl->len = msg->len - sizeof(*ctl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) req = (struct cn_notify_req *)(ctl + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) * Idx.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) req->first = cn_test_id.idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) req->range = 10;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) * Val 0.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) req++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) req->first = cn_test_id.val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) req->range = 10;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) * Val 1.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) req++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) req->first = cn_test_id.val + 20;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) req->range = 10;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) NETLINK_CB(skb).dst_group = ctl->group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) //netlink_broadcast(nls, skb, 0, ctl->group, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) netlink_unicast(nls, skb, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) pr_info("request was sent: group=0x%x\n", ctl->group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) static u32 cn_test_timer_counter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) static void cn_test_timer_func(struct timer_list *unused)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) struct cn_msg *m;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) char data[32];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) pr_debug("%s: timer fired\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) m = kzalloc(sizeof(*m) + sizeof(data), GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) if (m) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) memcpy(&m->id, &cn_test_id, sizeof(m->id));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) m->seq = cn_test_timer_counter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) m->len = sizeof(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) m->len =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) scnprintf(data, sizeof(data), "counter = %u",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) cn_test_timer_counter) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) memcpy(m + 1, data, m->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) cn_netlink_send(m, 0, 0, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) kfree(m);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) cn_test_timer_counter++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) mod_timer(&cn_test_timer, jiffies + msecs_to_jiffies(1000));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) static int cn_test_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) err = cn_add_callback(&cn_test_id, cn_test_name, cn_test_callback);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) cn_test_id.val++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) err = cn_add_callback(&cn_test_id, cn_test_name, cn_test_callback);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) cn_del_callback(&cn_test_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) timer_setup(&cn_test_timer, cn_test_timer_func, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) mod_timer(&cn_test_timer, jiffies + msecs_to_jiffies(1000));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) pr_info("initialized with id={%u.%u}\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) cn_test_id.idx, cn_test_id.val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) err_out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) if (nls && nls->sk_socket)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) sock_release(nls->sk_socket);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) static void cn_test_fini(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) del_timer_sync(&cn_test_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) cn_del_callback(&cn_test_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) cn_test_id.val--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) cn_del_callback(&cn_test_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) if (nls && nls->sk_socket)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) sock_release(nls->sk_socket);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) module_init(cn_test_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) module_exit(cn_test_fini);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) MODULE_AUTHOR("Evgeniy Polyakov <zbr@ioremap.net>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) MODULE_DESCRIPTION("Connector's test module");