Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * event.c - exporting ACPI events via procfs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  *  Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *  Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
^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) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/spinlock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/export.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/proc_fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/poll.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/gfp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/acpi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <net/netlink.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <net/genetlink.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include "internal.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) /* ACPI notifier chain */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) static BLOCKING_NOTIFIER_HEAD(acpi_chain_head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) int acpi_notifier_call_chain(struct acpi_device *dev, u32 type, u32 data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 	struct acpi_bus_event event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	strcpy(event.device_class, dev->pnp.device_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	strcpy(event.bus_id, dev->pnp.bus_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	event.type = type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	event.data = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	return (blocking_notifier_call_chain(&acpi_chain_head, 0, (void *)&event)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 			== NOTIFY_BAD) ? -EINVAL : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) EXPORT_SYMBOL(acpi_notifier_call_chain);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) int register_acpi_notifier(struct notifier_block *nb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	return blocking_notifier_chain_register(&acpi_chain_head, nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) EXPORT_SYMBOL(register_acpi_notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) int unregister_acpi_notifier(struct notifier_block *nb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	return blocking_notifier_chain_unregister(&acpi_chain_head, nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) EXPORT_SYMBOL(unregister_acpi_notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) #ifdef CONFIG_NET
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) static unsigned int acpi_event_seqnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) struct acpi_genl_event {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	acpi_device_class device_class;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	char bus_id[15];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	u32 type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	u32 data;
^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) /* attributes of acpi_genl_family */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) enum {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	ACPI_GENL_ATTR_UNSPEC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	ACPI_GENL_ATTR_EVENT,	/* ACPI event info needed by user space */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	__ACPI_GENL_ATTR_MAX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) #define ACPI_GENL_ATTR_MAX (__ACPI_GENL_ATTR_MAX - 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) /* commands supported by the acpi_genl_family */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) enum {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	ACPI_GENL_CMD_UNSPEC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	ACPI_GENL_CMD_EVENT,	/* kernel->user notifications for ACPI events */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	__ACPI_GENL_CMD_MAX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) #define ACPI_GENL_CMD_MAX (__ACPI_GENL_CMD_MAX - 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) #define ACPI_GENL_FAMILY_NAME		"acpi_event"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) #define ACPI_GENL_VERSION		0x01
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) #define ACPI_GENL_MCAST_GROUP_NAME 	"acpi_mc_group"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) static const struct genl_multicast_group acpi_event_mcgrps[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	{ .name = ACPI_GENL_MCAST_GROUP_NAME, },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) static struct genl_family acpi_event_genl_family __ro_after_init = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	.module = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	.name = ACPI_GENL_FAMILY_NAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	.version = ACPI_GENL_VERSION,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	.maxattr = ACPI_GENL_ATTR_MAX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	.mcgrps = acpi_event_mcgrps,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	.n_mcgrps = ARRAY_SIZE(acpi_event_mcgrps),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) int acpi_bus_generate_netlink_event(const char *device_class,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 				      const char *bus_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 				      u8 type, int data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	struct sk_buff *skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	struct nlattr *attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	struct acpi_genl_event *event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	void *msg_header;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	int size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	/* allocate memory */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	size = nla_total_size(sizeof(struct acpi_genl_event)) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	    nla_total_size(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	skb = genlmsg_new(size, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	if (!skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	/* add the genetlink message header */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	msg_header = genlmsg_put(skb, 0, acpi_event_seqnum++,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 				 &acpi_event_genl_family, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 				 ACPI_GENL_CMD_EVENT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	if (!msg_header) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 		nlmsg_free(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	/* fill the data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	attr =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	    nla_reserve(skb, ACPI_GENL_ATTR_EVENT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 			sizeof(struct acpi_genl_event));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	if (!attr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 		nlmsg_free(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	event = nla_data(attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	memset(event, 0, sizeof(struct acpi_genl_event));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	strscpy(event->device_class, device_class, sizeof(event->device_class));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	strscpy(event->bus_id, bus_id, sizeof(event->bus_id));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	event->type = type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	event->data = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	/* send multicast genetlink message */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	genlmsg_end(skb, msg_header);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	genlmsg_multicast(&acpi_event_genl_family, skb, 0, 0, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) EXPORT_SYMBOL(acpi_bus_generate_netlink_event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) static int __init acpi_event_genetlink_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	return genl_register_family(&acpi_event_genl_family);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) int acpi_bus_generate_netlink_event(const char *device_class,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 				      const char *bus_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 				      u8 type, int data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	return 0;
^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) EXPORT_SYMBOL(acpi_bus_generate_netlink_event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) static int acpi_event_genetlink_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) static int __init acpi_event_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	int error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	if (acpi_disabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	/* create genetlink for acpi event */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	error = acpi_event_genetlink_init();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 		printk(KERN_WARNING PREFIX
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 		       "Failed to create genetlink family for ACPI event\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) fs_initcall(acpi_event_init);