Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  *  cn_test.c
0004  * 
0005  * 2004+ Copyright (c) Evgeniy Polyakov <zbr@ioremap.net>
0006  * All rights reserved.
0007  */
0008 
0009 #define pr_fmt(fmt) "cn_test: " fmt
0010 
0011 #include <linux/kernel.h>
0012 #include <linux/module.h>
0013 #include <linux/moduleparam.h>
0014 #include <linux/skbuff.h>
0015 #include <linux/slab.h>
0016 #include <linux/timer.h>
0017 
0018 #include <linux/connector.h>
0019 
0020 static struct cb_id cn_test_id = { CN_NETLINK_USERS + 3, 0x456 };
0021 static char cn_test_name[] = "cn_test";
0022 static struct sock *nls;
0023 static struct timer_list cn_test_timer;
0024 
0025 static void cn_test_callback(struct cn_msg *msg, struct netlink_skb_parms *nsp)
0026 {
0027     pr_info("%s: %lu: idx=%x, val=%x, seq=%u, ack=%u, len=%d: %s.\n",
0028             __func__, jiffies, msg->id.idx, msg->id.val,
0029             msg->seq, msg->ack, msg->len,
0030             msg->len ? (char *)msg->data : "");
0031 }
0032 
0033 /*
0034  * Do not remove this function even if no one is using it as
0035  * this is an example of how to get notifications about new
0036  * connector user registration
0037  */
0038 #if 0
0039 static int cn_test_want_notify(void)
0040 {
0041     struct cn_ctl_msg *ctl;
0042     struct cn_notify_req *req;
0043     struct cn_msg *msg = NULL;
0044     int size, size0;
0045     struct sk_buff *skb;
0046     struct nlmsghdr *nlh;
0047     u32 group = 1;
0048 
0049     size0 = sizeof(*msg) + sizeof(*ctl) + 3 * sizeof(*req);
0050 
0051     size = NLMSG_SPACE(size0);
0052 
0053     skb = alloc_skb(size, GFP_ATOMIC);
0054     if (!skb) {
0055         pr_err("failed to allocate new skb with size=%u\n", size);
0056         return -ENOMEM;
0057     }
0058 
0059     nlh = nlmsg_put(skb, 0, 0x123, NLMSG_DONE, size - sizeof(*nlh), 0);
0060     if (!nlh) {
0061         kfree_skb(skb);
0062         return -EMSGSIZE;
0063     }
0064 
0065     msg = nlmsg_data(nlh);
0066 
0067     memset(msg, 0, size0);
0068 
0069     msg->id.idx = -1;
0070     msg->id.val = -1;
0071     msg->seq = 0x123;
0072     msg->ack = 0x345;
0073     msg->len = size0 - sizeof(*msg);
0074 
0075     ctl = (struct cn_ctl_msg *)(msg + 1);
0076 
0077     ctl->idx_notify_num = 1;
0078     ctl->val_notify_num = 2;
0079     ctl->group = group;
0080     ctl->len = msg->len - sizeof(*ctl);
0081 
0082     req = (struct cn_notify_req *)(ctl + 1);
0083 
0084     /*
0085      * Idx.
0086      */
0087     req->first = cn_test_id.idx;
0088     req->range = 10;
0089 
0090     /*
0091      * Val 0.
0092      */
0093     req++;
0094     req->first = cn_test_id.val;
0095     req->range = 10;
0096 
0097     /*
0098      * Val 1.
0099      */
0100     req++;
0101     req->first = cn_test_id.val + 20;
0102     req->range = 10;
0103 
0104     NETLINK_CB(skb).dst_group = ctl->group;
0105     //netlink_broadcast(nls, skb, 0, ctl->group, GFP_ATOMIC);
0106     netlink_unicast(nls, skb, 0, 0);
0107 
0108     pr_info("request was sent: group=0x%x\n", ctl->group);
0109 
0110     return 0;
0111 }
0112 #endif
0113 
0114 static u32 cn_test_timer_counter;
0115 static void cn_test_timer_func(struct timer_list *unused)
0116 {
0117     struct cn_msg *m;
0118     char data[32];
0119 
0120     pr_debug("%s: timer fired\n", __func__);
0121 
0122     m = kzalloc(sizeof(*m) + sizeof(data), GFP_ATOMIC);
0123     if (m) {
0124 
0125         memcpy(&m->id, &cn_test_id, sizeof(m->id));
0126         m->seq = cn_test_timer_counter;
0127         m->len = sizeof(data);
0128 
0129         m->len =
0130             scnprintf(data, sizeof(data), "counter = %u",
0131                   cn_test_timer_counter) + 1;
0132 
0133         memcpy(m + 1, data, m->len);
0134 
0135         cn_netlink_send(m, 0, 0, GFP_ATOMIC);
0136         kfree(m);
0137     }
0138 
0139     cn_test_timer_counter++;
0140 
0141     mod_timer(&cn_test_timer, jiffies + msecs_to_jiffies(1000));
0142 }
0143 
0144 static int cn_test_init(void)
0145 {
0146     int err;
0147 
0148     err = cn_add_callback(&cn_test_id, cn_test_name, cn_test_callback);
0149     if (err)
0150         goto err_out;
0151     cn_test_id.val++;
0152     err = cn_add_callback(&cn_test_id, cn_test_name, cn_test_callback);
0153     if (err) {
0154         cn_del_callback(&cn_test_id);
0155         goto err_out;
0156     }
0157 
0158     timer_setup(&cn_test_timer, cn_test_timer_func, 0);
0159     mod_timer(&cn_test_timer, jiffies + msecs_to_jiffies(1000));
0160 
0161     pr_info("initialized with id={%u.%u}\n",
0162         cn_test_id.idx, cn_test_id.val);
0163 
0164     return 0;
0165 
0166       err_out:
0167     if (nls && nls->sk_socket)
0168         sock_release(nls->sk_socket);
0169 
0170     return err;
0171 }
0172 
0173 static void cn_test_fini(void)
0174 {
0175     del_timer_sync(&cn_test_timer);
0176     cn_del_callback(&cn_test_id);
0177     cn_test_id.val--;
0178     cn_del_callback(&cn_test_id);
0179     if (nls && nls->sk_socket)
0180         sock_release(nls->sk_socket);
0181 }
0182 
0183 module_init(cn_test_init);
0184 module_exit(cn_test_fini);
0185 
0186 MODULE_LICENSE("GPL");
0187 MODULE_AUTHOR("Evgeniy Polyakov <zbr@ioremap.net>");
0188 MODULE_DESCRIPTION("Connector's test module");