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 #include <linux/spinlock.h> 39 40 struct kobject; 41 struct kset; 42 struct sysctl_oid; 43 44 #define KOBJ_CHANGE 0x01 45 46 struct kobj_type { 47 void (*release)(struct kobject *kobj); 48 const struct sysfs_ops *sysfs_ops; 49 struct attribute **default_attrs; 50 const struct attribute_group **default_groups; 51 }; 52 53 extern const struct kobj_type linux_kfree_type; 54 55 struct kobject { 56 struct kobject *parent; 57 char *name; 58 struct kref kref; 59 const struct kobj_type *ktype; 60 struct list_head entry; 61 struct sysctl_oid *oidp; 62 struct kset *kset; 63 }; 64 65 extern struct kobject *mm_kobj; 66 67 struct attribute { 68 const char *name; 69 struct module *owner; 70 mode_t mode; 71 }; 72 73 extern const struct sysfs_ops kobj_sysfs_ops; 74 75 struct kobj_attribute { 76 struct attribute attr; 77 ssize_t (*show)(struct kobject *kobj, struct kobj_attribute *attr, 78 char *buf); 79 ssize_t (*store)(struct kobject *kobj, struct kobj_attribute *attr, 80 const char *buf, size_t count); 81 }; 82 83 struct kset_uevent_ops { 84 /* TODO */ 85 }; 86 87 struct kset { 88 struct list_head list; 89 spinlock_t list_lock; 90 struct kobject kobj; 91 const struct kset_uevent_ops *uevent_ops; 92 }; 93 94 static inline void 95 kobject_init(struct kobject *kobj, const struct kobj_type *ktype) 96 { 97 98 kref_init(&kobj->kref); 99 INIT_LIST_HEAD(&kobj->entry); 100 kobj->ktype = ktype; 101 kobj->oidp = NULL; 102 } 103 104 void linux_kobject_release(struct kref *kref); 105 106 static inline void 107 kobject_put(struct kobject *kobj) 108 { 109 110 if (kobj) 111 kref_put(&kobj->kref, linux_kobject_release); 112 } 113 114 static inline struct kobject * 115 kobject_get(struct kobject *kobj) 116 { 117 118 if (kobj) 119 kref_get(&kobj->kref); 120 return kobj; 121 } 122 123 struct kobject *kobject_create(void); 124 int kobject_set_name_vargs(struct kobject *kobj, const char *fmt, va_list); 125 int kobject_add(struct kobject *kobj, struct kobject *parent, 126 const char *fmt, ...); 127 128 static inline struct kobject * 129 kobject_create_and_add(const char *name, struct kobject *parent) 130 { 131 struct kobject *kobj; 132 133 kobj = kobject_create(); 134 if (kobj == NULL) 135 return (NULL); 136 if (kobject_add(kobj, parent, "%s", name) == 0) 137 return (kobj); 138 kobject_put(kobj); 139 140 return (NULL); 141 } 142 143 static inline void 144 kobject_del(struct kobject *kobj __unused) 145 { 146 } 147 148 static inline char * 149 kobject_name(const struct kobject *kobj) 150 { 151 152 return kobj->name; 153 } 154 155 int kobject_set_name(struct kobject *kobj, const char *fmt, ...); 156 int kobject_init_and_add(struct kobject *kobj, const struct kobj_type *ktype, 157 struct kobject *parent, const char *fmt, ...); 158 159 static __inline void 160 kobject_uevent_env(struct kobject *kobj, int action, char *envp[]) 161 { 162 163 /* 164 * iwlwifi(4) sends an INACCESSIBLE event when it detects that the card 165 * (pice endpoint) is gone and it attempts a removal cleanup. 166 * Not sure if we do anything related to udev/sysfs at the moment or 167 * need a shortcut or simply ignore it (for now). 168 */ 169 } 170 171 void kset_init(struct kset *kset); 172 int kset_register(struct kset *kset); 173 void kset_unregister(struct kset *kset); 174 struct kset * kset_create_and_add(const char *name, 175 const struct kset_uevent_ops *u, struct kobject *parent_kobj); 176 177 static inline struct kset * 178 to_kset(struct kobject *kobj) 179 { 180 if (kobj != NULL) 181 return container_of(kobj, struct kset, kobj); 182 else 183 return NULL; 184 } 185 186 static inline struct kset * 187 kset_get(struct kset *kset) 188 { 189 if (kset != NULL) { 190 struct kobject *kobj; 191 192 kobj = kobject_get(&kset->kobj); 193 return to_kset(kobj); 194 } else { 195 return NULL; 196 } 197 } 198 199 static inline void 200 kset_put(struct kset *kset) 201 { 202 if (kset != NULL) 203 kobject_put(&kset->kobj); 204 } 205 206 void linux_kobject_kfree_name(struct kobject *kobj); 207 208 #endif /* _LINUXKPI_LINUX_KOBJECT_H_ */ 209