Path: blob/master/include/target/configfs_macros.h
10814 views
/* -*- mode: c; c-basic-offset: 8; -*-1* vim: noexpandtab sw=8 ts=8 sts=0:2*3* configfs_macros.h - extends macros for configfs4*5* This program is free software; you can redistribute it and/or6* modify it under the terms of the GNU General Public7* License as published by the Free Software Foundation; either8* version 2 of the License, or (at your option) any later version.9*10* This program is distributed in the hope that it will be useful,11* but WITHOUT ANY WARRANTY; without even the implied warranty of12* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU13* General Public License for more details.14*15* You should have received a copy of the GNU General Public16* License along with this program; if not, write to the17* Free Software Foundation, Inc., 59 Temple Place - Suite 330,18* Boston, MA 021110-1307, USA.19*20* Based on sysfs:21* sysfs is Copyright (C) 2001, 2002, 2003 Patrick Mochel22*23* Based on kobject.h:24* Copyright (c) 2002-2003 Patrick Mochel25* Copyright (c) 2002-2003 Open Source Development Labs26*27* configfs Copyright (C) 2005 Oracle. All rights reserved.28*29* Added CONFIGFS_EATTR() macros from original configfs.h macros30* Copright (C) 2008-2009 Nicholas A. Bellinger <[email protected]>31*32* Please read Documentation/filesystems/configfs.txt before using the33* configfs interface, ESPECIALLY the parts about reference counts and34* item destructors.35*/3637#ifndef _CONFIGFS_MACROS_H_38#define _CONFIGFS_MACROS_H_3940#include <linux/configfs.h>4142/*43* Users often need to create attribute structures for their configurable44* attributes, containing a configfs_attribute member and function pointers45* for the show() and store() operations on that attribute. If they don't46* need anything else on the extended attribute structure, they can use47* this macro to define it. The argument _name isends up as48* 'struct _name_attribute, as well as names of to CONFIGFS_ATTR_OPS() below.49* The argument _item is the name of the structure containing the50* struct config_item or struct config_group structure members51*/52#define CONFIGFS_EATTR_STRUCT(_name, _item) \53struct _name##_attribute { \54struct configfs_attribute attr; \55ssize_t (*show)(struct _item *, char *); \56ssize_t (*store)(struct _item *, const char *, size_t); \57}5859/*60* With the extended attribute structure, users can use this macro61* (similar to sysfs' __ATTR) to make defining attributes easier.62* An example:63* #define MYITEM_EATTR(_name, _mode, _show, _store) \64* struct myitem_attribute childless_attr_##_name = \65* __CONFIGFS_EATTR(_name, _mode, _show, _store)66*/67#define __CONFIGFS_EATTR(_name, _mode, _show, _store) \68{ \69.attr = { \70.ca_name = __stringify(_name), \71.ca_mode = _mode, \72.ca_owner = THIS_MODULE, \73}, \74.show = _show, \75.store = _store, \76}77/* Here is a readonly version, only requiring a show() operation */78#define __CONFIGFS_EATTR_RO(_name, _show) \79{ \80.attr = { \81.ca_name = __stringify(_name), \82.ca_mode = 0444, \83.ca_owner = THIS_MODULE, \84}, \85.show = _show, \86}8788/*89* With these extended attributes, the simple show_attribute() and90* store_attribute() operations need to call the show() and store() of the91* attributes. This is a common pattern, so we provide a macro to define92* them. The argument _name is the name of the attribute defined by93* CONFIGFS_ATTR_STRUCT(). The argument _item is the name of the structure94* containing the struct config_item or struct config_group structure member.95* The argument _item_member is the actual name of the struct config_* struct96* in your _item structure. Meaning my_structure->some_config_group.97* ^^_item^^^^^ ^^_item_member^^^98* This macro expects the attributes to be named "struct <name>_attribute".99*/100#define CONFIGFS_EATTR_OPS_TO_FUNC(_name, _item, _item_member) \101static struct _item *to_##_name(struct config_item *ci) \102{ \103return (ci) ? container_of(to_config_group(ci), struct _item, \104_item_member) : NULL; \105}106107#define CONFIGFS_EATTR_OPS_SHOW(_name, _item) \108static ssize_t _name##_attr_show(struct config_item *item, \109struct configfs_attribute *attr, \110char *page) \111{ \112struct _item *_item = to_##_name(item); \113struct _name##_attribute * _name##_attr = \114container_of(attr, struct _name##_attribute, attr); \115ssize_t ret = 0; \116\117if (_name##_attr->show) \118ret = _name##_attr->show(_item, page); \119return ret; \120}121122#define CONFIGFS_EATTR_OPS_STORE(_name, _item) \123static ssize_t _name##_attr_store(struct config_item *item, \124struct configfs_attribute *attr, \125const char *page, size_t count) \126{ \127struct _item *_item = to_##_name(item); \128struct _name##_attribute * _name##_attr = \129container_of(attr, struct _name##_attribute, attr); \130ssize_t ret = -EINVAL; \131\132if (_name##_attr->store) \133ret = _name##_attr->store(_item, page, count); \134return ret; \135}136137#define CONFIGFS_EATTR_OPS(_name, _item, _item_member) \138CONFIGFS_EATTR_OPS_TO_FUNC(_name, _item, _item_member); \139CONFIGFS_EATTR_OPS_SHOW(_name, _item); \140CONFIGFS_EATTR_OPS_STORE(_name, _item);141142#define CONFIGFS_EATTR_OPS_RO(_name, _item, _item_member) \143CONFIGFS_EATTR_OPS_TO_FUNC(_name, _item, _item_member); \144CONFIGFS_EATTR_OPS_SHOW(_name, _item);145146#endif /* _CONFIGFS_MACROS_H_ */147148149