1219820Sjeff/*-
2219820Sjeff * Copyright (c) 2010 Isilon Systems, Inc.
3219820Sjeff * Copyright (c) 2010 iX Systems, Inc.
4219820Sjeff * Copyright (c) 2010 Panasas, Inc.
5271127Shselasky * Copyright (c) 2013, 2014 Mellanox Technologies, Ltd.
6219820Sjeff * All rights reserved.
7219820Sjeff *
8219820Sjeff * Redistribution and use in source and binary forms, with or without
9219820Sjeff * modification, are permitted provided that the following conditions
10219820Sjeff * are met:
11219820Sjeff * 1. Redistributions of source code must retain the above copyright
12219820Sjeff *    notice unmodified, this list of conditions, and the following
13219820Sjeff *    disclaimer.
14219820Sjeff * 2. Redistributions in binary form must reproduce the above copyright
15219820Sjeff *    notice, this list of conditions and the following disclaimer in the
16219820Sjeff *    documentation and/or other materials provided with the distribution.
17219820Sjeff *
18219820Sjeff * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19219820Sjeff * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20219820Sjeff * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21219820Sjeff * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22219820Sjeff * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23219820Sjeff * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24219820Sjeff * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25219820Sjeff * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26219820Sjeff * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27219820Sjeff * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28219820Sjeff */
29219820Sjeff#ifndef	_LINUX_KOBJECT_H_
30219820Sjeff#define	_LINUX_KOBJECT_H_
31219820Sjeff
32219820Sjeff#include <machine/stdarg.h>
33219820Sjeff
34219820Sjeff#include <linux/kernel.h>
35219820Sjeff#include <linux/kref.h>
36219820Sjeff#include <linux/slab.h>
37219820Sjeff
38219820Sjeffstruct kobject;
39219820Sjeffstruct sysctl_oid;
40219820Sjeff
41219820Sjeffstruct kobj_type {
42219820Sjeff	void (*release)(struct kobject *kobj);
43219820Sjeff	const struct sysfs_ops *sysfs_ops;
44219820Sjeff	struct attribute **default_attrs;
45219820Sjeff};
46219820Sjeff
47219820Sjeffextern struct kobj_type kfree_type;
48219820Sjeff
49219820Sjeffstruct kobject {
50219820Sjeff	struct kobject		*parent;
51219820Sjeff	char			*name;
52219820Sjeff	struct kref		kref;
53219820Sjeff	struct kobj_type	*ktype;
54219820Sjeff	struct list_head	entry;
55219820Sjeff	struct sysctl_oid	*oidp;
56219820Sjeff};
57219820Sjeff
58271127Shselaskyextern struct kobject *mm_kobj;
59271127Shselasky
60219820Sjeffstatic inline void
61219820Sjeffkobject_init(struct kobject *kobj, struct kobj_type *ktype)
62219820Sjeff{
63219820Sjeff
64219820Sjeff	kref_init(&kobj->kref);
65219820Sjeff	INIT_LIST_HEAD(&kobj->entry);
66219820Sjeff	kobj->ktype = ktype;
67219820Sjeff	kobj->oidp = NULL;
68219820Sjeff}
69219820Sjeff
70219820Sjeffstatic inline void kobject_put(struct kobject *kobj);
71219820Sjeffvoid kobject_release(struct kref *kref);
72219820Sjeff
73219820Sjeffstatic inline void
74219820Sjeffkobject_put(struct kobject *kobj)
75219820Sjeff{
76219820Sjeff
77219820Sjeff	if (kobj)
78219820Sjeff		kref_put(&kobj->kref, kobject_release);
79219820Sjeff}
80219820Sjeff
81219820Sjeffstatic inline struct kobject *
82219820Sjeffkobject_get(struct kobject *kobj)
83219820Sjeff{
84219820Sjeff
85219820Sjeff	if (kobj)
86219820Sjeff		kref_get(&kobj->kref);
87219820Sjeff	return kobj;
88219820Sjeff}
89219820Sjeff
90293151Shselaskyint	kobject_set_name_vargs(struct kobject *kobj, const char *fmt, va_list);
91219820Sjeffint	kobject_add(struct kobject *kobj, struct kobject *parent,
92219820Sjeff	    const char *fmt, ...);
93219820Sjeff
94219820Sjeffstatic inline struct kobject *
95219820Sjeffkobject_create(void)
96219820Sjeff{
97219820Sjeff	struct kobject *kobj;
98219820Sjeff
99219820Sjeff	kobj = kzalloc(sizeof(*kobj), GFP_KERNEL);
100219820Sjeff	if (kobj == NULL)
101219820Sjeff		return (NULL);
102219820Sjeff	kobject_init(kobj, &kfree_type);
103219820Sjeff
104219820Sjeff	return (kobj);
105219820Sjeff}
106219820Sjeff
107219820Sjeffstatic inline struct kobject *
108219820Sjeffkobject_create_and_add(const char *name, struct kobject *parent)
109219820Sjeff{
110219820Sjeff	struct kobject *kobj;
111219820Sjeff
112219820Sjeff	kobj = kobject_create();
113219820Sjeff	if (kobj == NULL)
114219820Sjeff		return (NULL);
115219820Sjeff	if (kobject_add(kobj, parent, "%s", name) == 0)
116219820Sjeff		return (kobj);
117219820Sjeff	kobject_put(kobj);
118219820Sjeff
119219820Sjeff	return (NULL);
120219820Sjeff}
121219820Sjeff
122219820Sjeff
123219820Sjeffstatic inline char *
124219820Sjeffkobject_name(const struct kobject *kobj)
125219820Sjeff{
126219820Sjeff
127219820Sjeff	return kobj->name;
128219820Sjeff}
129219820Sjeff
130219820Sjeffint	kobject_set_name(struct kobject *kobj, const char *fmt, ...);
131219820Sjeffint	kobject_init_and_add(struct kobject *kobj, struct kobj_type *ktype,
132219820Sjeff	    struct kobject *parent, const char *fmt, ...);
133219820Sjeff
134271127Shselasky/* sysfs.h calles for 'kobject' which is defined here,
135271127Shselasky * so we need to add the include only after the 'kobject' def.
136271127Shselasky */
137271127Shselasky#include <linux/sysfs.h>
138271127Shselasky
139271127Shselaskystruct kobj_attribute {
140271127Shselasky        struct attribute attr;
141271127Shselasky        ssize_t (*show)(struct kobject *kobj, struct kobj_attribute *attr,
142271127Shselasky                        char *buf);
143271127Shselasky        ssize_t (*store)(struct kobject *kobj, struct kobj_attribute *attr,
144271127Shselasky                         const char *buf, size_t count);
145271127Shselasky};
146271127Shselasky
147219820Sjeff#endif /* _LINUX_KOBJECT_H_ */
148