xref: /freebsd-13-stable/sys/compat/linuxkpi/common/include/linux/kobject.h (revision f8167e0404dab9ffeaca95853dd237ab7c587f82)
1 /*-
2  * Copyright (c) 2010 Isilon Systems, Inc.
3  * Copyright (c) 2010 iX Systems, Inc.
4  * Copyright (c) 2010 Panasas, Inc.
5  * Copyright (c) 2013, 2014 Mellanox Technologies, Ltd.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice unmodified, this list of conditions, and the following
13  *    disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 #ifndef	_LINUXKPI_LINUX_KOBJECT_H_
30 #define	_LINUXKPI_LINUX_KOBJECT_H_
31 
32 #include <machine/stdarg.h>
33 
34 #include <linux/kernel.h>
35 #include <linux/kref.h>
36 #include <linux/list.h>
37 #include <linux/slab.h>
38 
39 struct kobject;
40 struct sysctl_oid;
41 
42 #define	KOBJ_CHANGE		0x01
43 
44 struct kobj_type {
45 	void (*release)(struct kobject *kobj);
46 	const struct sysfs_ops *sysfs_ops;
47 	struct attribute **default_attrs;
48 };
49 
50 extern const struct kobj_type linux_kfree_type;
51 
52 struct kobject {
53 	struct kobject		*parent;
54 	char			*name;
55 	struct kref		kref;
56 	const struct kobj_type	*ktype;
57 	struct list_head	entry;
58 	struct sysctl_oid	*oidp;
59 };
60 
61 extern struct kobject *mm_kobj;
62 
63 struct attribute {
64 	const char	*name;
65 	struct module	*owner;
66 	mode_t		mode;
67 };
68 
69 extern const struct sysfs_ops kobj_sysfs_ops;
70 
71 struct kobj_attribute {
72 	struct attribute attr;
73 	ssize_t (*show)(struct kobject *kobj, struct kobj_attribute *attr,
74 	    char *buf);
75 	ssize_t (*store)(struct kobject *kobj, struct kobj_attribute *attr,
76 	    const char *buf, size_t count);
77 };
78 
79 static inline void
kobject_init(struct kobject * kobj,const struct kobj_type * ktype)80 kobject_init(struct kobject *kobj, const struct kobj_type *ktype)
81 {
82 
83 	kref_init(&kobj->kref);
84 	INIT_LIST_HEAD(&kobj->entry);
85 	kobj->ktype = ktype;
86 	kobj->oidp = NULL;
87 }
88 
89 void linux_kobject_release(struct kref *kref);
90 
91 static inline void
kobject_put(struct kobject * kobj)92 kobject_put(struct kobject *kobj)
93 {
94 
95 	if (kobj)
96 		kref_put(&kobj->kref, linux_kobject_release);
97 }
98 
99 static inline struct kobject *
kobject_get(struct kobject * kobj)100 kobject_get(struct kobject *kobj)
101 {
102 
103 	if (kobj)
104 		kref_get(&kobj->kref);
105 	return kobj;
106 }
107 
108 struct kobject *kobject_create(void);
109 int	kobject_set_name_vargs(struct kobject *kobj, const char *fmt, va_list);
110 int	kobject_add(struct kobject *kobj, struct kobject *parent,
111 	    const char *fmt, ...);
112 
113 static inline struct kobject *
kobject_create_and_add(const char * name,struct kobject * parent)114 kobject_create_and_add(const char *name, struct kobject *parent)
115 {
116 	struct kobject *kobj;
117 
118 	kobj = kobject_create();
119 	if (kobj == NULL)
120 		return (NULL);
121 	if (kobject_add(kobj, parent, "%s", name) == 0)
122 		return (kobj);
123 	kobject_put(kobj);
124 
125 	return (NULL);
126 }
127 
128 static inline void
kobject_del(struct kobject * kobj __unused)129 kobject_del(struct kobject *kobj __unused)
130 {
131 }
132 
133 static inline char *
kobject_name(const struct kobject * kobj)134 kobject_name(const struct kobject *kobj)
135 {
136 
137 	return kobj->name;
138 }
139 
140 int	kobject_set_name(struct kobject *kobj, const char *fmt, ...);
141 int	kobject_init_and_add(struct kobject *kobj, const struct kobj_type *ktype,
142 	    struct kobject *parent, const char *fmt, ...);
143 
144 static __inline void
kobject_uevent_env(struct kobject * kobj,int action,char * envp[])145 kobject_uevent_env(struct kobject *kobj, int action, char *envp[])
146 {
147 
148 	/*
149 	 * iwlwifi(4) sends an INACCESSIBLE event when it detects that the card
150 	 * (pice endpoint) is gone and it attempts a removal cleanup.
151 	 * Not sure if we do anything related to udev/sysfs at the moment or
152 	 * need a shortcut or simply ignore it (for now).
153 	 */
154 }
155 
156 #endif /* _LINUXKPI_LINUX_KOBJECT_H_ */
157