/*
 * pub.c - exploting driverfs for fun and profit
 *
 * Copyright (c) 2002 Patrick Mochel
 *                    Open Source Development Lab
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 *
 * This is a sample subsystem to illustrate how easy it is to latch on to
 * driverfs and exploit it. It is the 'pub' subsystem. Objects of type 'beer' 
 * can be registered and unregistered with the pub via beer_register and 
 * beer_unregister.
 * 
 * Every time a beer is registered with the pub subsystem, a directory is
 * created for it. The pub subsystem could then create files on behalf of
 * the beer, in the beer's directory. Once the beer has registered, it
 * can create files for itself in its directory. A second, more robust
 * example will surely follow. 
 *
 * Compiled with

CFLAGS = -Wall -O2 -fomit-frame-pointer -DMODULE -D__KERNEL__
IDIR = /home/mochel/src/kernel/devel/linux-2.5/include

pub.o::pub.c
        $(CC) $(CFLAGS) -I$(IDIR) -c -o $@ $<
 */

#include <linux/module.h>
#include <linux/string.h>
#include <linux/init.h>
#include <linux/stat.h>
#include <linux/err.h>
#include <linux/driverfs_fs.h>

struct beer {
	char			* name;
	char			* style;
	char			* nation;
	u32			founded;
	u32			alcohol;
	u32			calories;
	struct list_head	node;
	struct driver_dir_entry dir;
};

struct beer_attribute {
	struct attribute	attr;
	ssize_t (*show)(struct beer * beer, char * buf, size_t count, loff_t off);
	ssize_t (*store)(struct beer * beer, const char * buf, size_t count, loff_t off);
};

#define BEER_ATTR(_name,_mode,_show,_store) \
struct beer_attribute beer_attr_##_name = { 		\
	.attr = {.name = __stringify(_name), .mode = _mode },	\
	.show	= _show,				\
	.store	= _store,				\
};

#define to_beer_attr(_attr) container_of(_attr,struct beer_attribute,attr)

#define to_beer(_dir) container_of(_dir,struct beer,dir)


static LIST_HEAD(beer_list);

static struct driver_dir_entry pub_dir = {
	.name	= "pub",
	.mode	= (S_IFDIR| S_IRWXU | S_IRUGO | S_IXUGO),
};

static struct driver_dir_entry pub_beer_dir = {
	.name	= "beer",
	.mode	= (S_IFDIR| S_IRWXU | S_IRUGO | S_IXUGO),
};

/* driverfs ops for beer attribute files */

static int empty_attr_open(struct driver_dir_entry * dir)
{
	/* nothing to do */
	return 0;
}

static int empty_attr_close(struct driver_dir_entry * dir)
{
	/* nothing to do */
	return 0;
}

static ssize_t
beer_attr_show(struct driver_dir_entry * dir, struct attribute * attr,
	       char * buf, size_t count, loff_t off)
{
	struct beer * beer = to_beer(dir);
	struct beer_attribute * beer_attr = to_beer_attr(attr);
	ssize_t ret = 0;

	if (beer_attr->show)
		ret = beer_attr->show(beer,buf,count,off);
	return ret;
}

static ssize_t
beer_attr_store(struct driver_dir_entry * dir, struct attribute * attr,
		const char * buf, size_t count, loff_t off)
{
	struct beer * beer = to_beer(dir);
	struct beer_attribute * beer_attr = to_beer_attr(attr);
	ssize_t ret = 0;

	if (beer_attr->store)
		ret = beer_attr->store(beer,buf,count,off);
	return ret;
}

static struct driverfs_ops beer_attr_ops = {
	.open	= empty_attr_open,
	.close	= empty_attr_close,
	.show	= beer_attr_show,
	.store	= beer_attr_store,
};


/* default attributes for beer */

static ssize_t style_show(struct beer * beer, char * buf, size_t count, loff_t off)
{
	return off ? 0 : snprintf(buf,50,"%s\n",beer->style);
}

static BEER_ATTR(style,0444,style_show,NULL);


static ssize_t nation_show(struct beer * beer, char * buf, size_t count, loff_t off)
{
	return off ? 0 : snprintf(buf,50,"%s\n",beer->nation);
}

static BEER_ATTR(nation,0444,nation_show,NULL);


static ssize_t founded_show(struct beer * beer, char * buf, size_t count, loff_t off)
{
	return off ? 0 : snprintf(buf,50,"%u\n",beer->founded);
}

static BEER_ATTR(founded,0444,founded_show,NULL);


static ssize_t alcohol_show(struct beer * beer, char * buf, size_t count, loff_t off)
{
	return off ? 0 : snprintf(buf,50,"%u\n",beer->alcohol);
}

static BEER_ATTR(alcohol,0444,alcohol_show,NULL);


static ssize_t calories_show(struct beer * beer, char * buf, size_t count, loff_t off)
{
	return off ? 0 : snprintf(buf,50,"%u\n",beer->calories);
}

static BEER_ATTR(calories,0444,calories_show,NULL);

static struct beer_attribute * def_attrs[] = {
	&beer_attr_style,
	&beer_attr_nation,
	&beer_attr_founded,
	&beer_attr_alcohol,
	&beer_attr_calories,
	NULL,
};

int beer_create_file(struct beer * beer, struct beer_attribute * attr)
{
	int error = -EINVAL;
	if (beer)
		error = driverfs_create_file(&attr->attr,&beer->dir);
	return error;
}

void beer_remove_file(struct beer * beer, struct beer_attribute * attr)
{
	if (beer)
		driverfs_remove_file(&beer->dir,attr->attr.name);
}

static void beer_remove_dir(struct beer * beer)
{
	driverfs_remove_dir(&beer->dir);
}

static int populate_dir(struct beer * beer)
{
	struct beer_attribute * attr;
	int i;
	int error = 0;
	
	for (i = 0; (attr = def_attrs[i]); i++) {
		if ((error = beer_create_file(beer,attr))) {
			beer_remove_dir(beer);
			break;
		}
	}
	return error;
}

static int beer_make_dir(struct beer * beer)
{
	int error;

	beer->dir.name = beer->name;
	beer->dir.ops = &beer_attr_ops;

	error = driverfs_create_dir(&beer->dir,&pub_beer_dir);
	if (!error)
		error = populate_dir(beer);
	return error;
}

int beer_register(struct beer * beer)
{
	int error;
	error = beer_make_dir(beer);
	if (!error)
		list_add_tail(&beer->node,&beer_list);
	return error;
}

void beer_unregister(struct beer * beer)
{
	beer_remove_dir(beer);
	list_del_init(&beer->node);
}

static struct beer guinness_beer = {
	.name		= "Guinness",
	.style		= "stout",
	.nation		= "Ireland",
	.founded	= 1759,
	.alcohol	= 473,
	.calories	= 43,
};

static struct beer pabst_beer = {
	.name		= "Pabst",
	.style		= "pilsner",
	.nation		= "USA",
	.founded	= 1844,
	.alcohol	= 578,
	.calories	= 45,
};

static int __init pub_init(void)
{
	driverfs_create_dir(&pub_dir,NULL);
	driverfs_create_dir(&pub_beer_dir,&pub_dir);
	beer_register(&guinness_beer);
	beer_register(&pabst_beer);
	return 0;
}

static void __exit pub_exit(void)
{
	beer_unregister(&guinness_beer);
	beer_unregister(&pabst_beer);
	driverfs_remove_dir(&pub_beer_dir);
	driverfs_remove_dir(&pub_dir);
}

subsys_initcall(pub_init);
module_exit(pub_exit);