Commit | Line | Data |
---|---|---|
c66ac9db NB |
1 | /* -*- mode: c; c-basic-offset: 8; -*- |
2 | * vim: noexpandtab sw=8 ts=8 sts=0: | |
3 | * | |
4 | * configfs_macros.h - extends macros for configfs | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or | |
7 | * modify it under the terms of the GNU General Public | |
8 | * License as published by the Free Software Foundation; either | |
9 | * version 2 of the License, or (at your option) any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | * General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public | |
17 | * License along with this program; if not, write to the | |
18 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
19 | * Boston, MA 021110-1307, USA. | |
20 | * | |
21 | * Based on sysfs: | |
22 | * sysfs is Copyright (C) 2001, 2002, 2003 Patrick Mochel | |
23 | * | |
24 | * Based on kobject.h: | |
25 | * Copyright (c) 2002-2003 Patrick Mochel | |
26 | * Copyright (c) 2002-2003 Open Source Development Labs | |
27 | * | |
28 | * configfs Copyright (C) 2005 Oracle. All rights reserved. | |
29 | * | |
30 | * Added CONFIGFS_EATTR() macros from original configfs.h macros | |
31 | * Copright (C) 2008-2009 Nicholas A. Bellinger <nab@linux-iscsi.org> | |
32 | * | |
395cf969 PB |
33 | * Please read Documentation/filesystems/configfs/configfs.txt before using |
34 | * the configfs interface, ESPECIALLY the parts about reference counts and | |
c66ac9db NB |
35 | * item destructors. |
36 | */ | |
37 | ||
38 | #ifndef _CONFIGFS_MACROS_H_ | |
39 | #define _CONFIGFS_MACROS_H_ | |
40 | ||
41 | #include <linux/configfs.h> | |
42 | ||
43 | /* | |
44 | * Users often need to create attribute structures for their configurable | |
45 | * attributes, containing a configfs_attribute member and function pointers | |
46 | * for the show() and store() operations on that attribute. If they don't | |
47 | * need anything else on the extended attribute structure, they can use | |
48 | * this macro to define it. The argument _name isends up as | |
49 | * 'struct _name_attribute, as well as names of to CONFIGFS_ATTR_OPS() below. | |
50 | * The argument _item is the name of the structure containing the | |
51 | * struct config_item or struct config_group structure members | |
52 | */ | |
53 | #define CONFIGFS_EATTR_STRUCT(_name, _item) \ | |
54 | struct _name##_attribute { \ | |
55 | struct configfs_attribute attr; \ | |
56 | ssize_t (*show)(struct _item *, char *); \ | |
57 | ssize_t (*store)(struct _item *, const char *, size_t); \ | |
58 | } | |
59 | ||
60 | /* | |
61 | * With the extended attribute structure, users can use this macro | |
62 | * (similar to sysfs' __ATTR) to make defining attributes easier. | |
63 | * An example: | |
64 | * #define MYITEM_EATTR(_name, _mode, _show, _store) \ | |
65 | * struct myitem_attribute childless_attr_##_name = \ | |
66 | * __CONFIGFS_EATTR(_name, _mode, _show, _store) | |
67 | */ | |
68 | #define __CONFIGFS_EATTR(_name, _mode, _show, _store) \ | |
69 | { \ | |
70 | .attr = { \ | |
71 | .ca_name = __stringify(_name), \ | |
72 | .ca_mode = _mode, \ | |
73 | .ca_owner = THIS_MODULE, \ | |
74 | }, \ | |
75 | .show = _show, \ | |
76 | .store = _store, \ | |
77 | } | |
78 | /* Here is a readonly version, only requiring a show() operation */ | |
79 | #define __CONFIGFS_EATTR_RO(_name, _show) \ | |
80 | { \ | |
81 | .attr = { \ | |
82 | .ca_name = __stringify(_name), \ | |
83 | .ca_mode = 0444, \ | |
84 | .ca_owner = THIS_MODULE, \ | |
85 | }, \ | |
86 | .show = _show, \ | |
87 | } | |
88 | ||
89 | /* | |
90 | * With these extended attributes, the simple show_attribute() and | |
91 | * store_attribute() operations need to call the show() and store() of the | |
92 | * attributes. This is a common pattern, so we provide a macro to define | |
93 | * them. The argument _name is the name of the attribute defined by | |
94 | * CONFIGFS_ATTR_STRUCT(). The argument _item is the name of the structure | |
95 | * containing the struct config_item or struct config_group structure member. | |
96 | * The argument _item_member is the actual name of the struct config_* struct | |
97 | * in your _item structure. Meaning my_structure->some_config_group. | |
98 | * ^^_item^^^^^ ^^_item_member^^^ | |
99 | * This macro expects the attributes to be named "struct <name>_attribute". | |
100 | */ | |
101 | #define CONFIGFS_EATTR_OPS_TO_FUNC(_name, _item, _item_member) \ | |
102 | static struct _item *to_##_name(struct config_item *ci) \ | |
103 | { \ | |
104 | return (ci) ? container_of(to_config_group(ci), struct _item, \ | |
105 | _item_member) : NULL; \ | |
106 | } | |
107 | ||
108 | #define CONFIGFS_EATTR_OPS_SHOW(_name, _item) \ | |
109 | static ssize_t _name##_attr_show(struct config_item *item, \ | |
110 | struct configfs_attribute *attr, \ | |
111 | char *page) \ | |
112 | { \ | |
113 | struct _item *_item = to_##_name(item); \ | |
114 | struct _name##_attribute * _name##_attr = \ | |
115 | container_of(attr, struct _name##_attribute, attr); \ | |
116 | ssize_t ret = 0; \ | |
117 | \ | |
118 | if (_name##_attr->show) \ | |
119 | ret = _name##_attr->show(_item, page); \ | |
120 | return ret; \ | |
121 | } | |
122 | ||
123 | #define CONFIGFS_EATTR_OPS_STORE(_name, _item) \ | |
124 | static ssize_t _name##_attr_store(struct config_item *item, \ | |
125 | struct configfs_attribute *attr, \ | |
126 | const char *page, size_t count) \ | |
127 | { \ | |
128 | struct _item *_item = to_##_name(item); \ | |
129 | struct _name##_attribute * _name##_attr = \ | |
130 | container_of(attr, struct _name##_attribute, attr); \ | |
131 | ssize_t ret = -EINVAL; \ | |
132 | \ | |
133 | if (_name##_attr->store) \ | |
134 | ret = _name##_attr->store(_item, page, count); \ | |
135 | return ret; \ | |
136 | } | |
137 | ||
138 | #define CONFIGFS_EATTR_OPS(_name, _item, _item_member) \ | |
139 | CONFIGFS_EATTR_OPS_TO_FUNC(_name, _item, _item_member); \ | |
140 | CONFIGFS_EATTR_OPS_SHOW(_name, _item); \ | |
141 | CONFIGFS_EATTR_OPS_STORE(_name, _item); | |
142 | ||
143 | #define CONFIGFS_EATTR_OPS_RO(_name, _item, _item_member) \ | |
144 | CONFIGFS_EATTR_OPS_TO_FUNC(_name, _item, _item_member); \ | |
145 | CONFIGFS_EATTR_OPS_SHOW(_name, _item); | |
146 | ||
147 | #endif /* _CONFIGFS_MACROS_H_ */ |