1 /* drivers/misc/timed_output.c
3 * Copyright (C) 2009 Google, Inc.
4 * Author: Mike Lockwood <lockwood@android.com>
6 * This software is licensed under the terms of the GNU General Public
7 * License version 2, as published by the Free Software Foundation, and
8 * may be copied, distributed, and modified under those terms.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
17 #define pr_fmt(fmt) "timed_output: " fmt
19 #include <linux/module.h>
20 #include <linux/types.h>
21 #include <linux/device.h>
23 #include <linux/err.h>
25 #include "timed_output.h"
27 static struct class *timed_output_class
;
28 static atomic_t device_count
;
30 static ssize_t
enable_show(struct device
*dev
, struct device_attribute
*attr
,
33 struct timed_output_dev
*tdev
= dev_get_drvdata(dev
);
34 int remaining
= tdev
->get_time(tdev
);
36 return sprintf(buf
, "%d\n", remaining
);
39 static ssize_t
enable_store(struct device
*dev
, struct device_attribute
*attr
,
40 const char *buf
, size_t size
)
42 struct timed_output_dev
*tdev
= dev_get_drvdata(dev
);
46 rc
= kstrtoint(buf
, 0, &value
);
50 tdev
->enable(tdev
, value
);
54 static DEVICE_ATTR_RW(enable
);
56 static struct attribute
*timed_output_attrs
[] = {
57 &dev_attr_enable
.attr
,
60 ATTRIBUTE_GROUPS(timed_output
);
62 static int create_timed_output_class(void)
64 if (!timed_output_class
) {
65 timed_output_class
= class_create(THIS_MODULE
, "timed_output");
66 if (IS_ERR(timed_output_class
))
67 return PTR_ERR(timed_output_class
);
68 atomic_set(&device_count
, 0);
69 timed_output_class
->dev_groups
= timed_output_groups
;
75 int timed_output_dev_register(struct timed_output_dev
*tdev
)
79 if (!tdev
|| !tdev
->name
|| !tdev
->enable
|| !tdev
->get_time
)
82 ret
= create_timed_output_class();
86 tdev
->index
= atomic_inc_return(&device_count
);
87 tdev
->dev
= device_create(timed_output_class
, NULL
,
88 MKDEV(0, tdev
->index
), NULL
, "%s", tdev
->name
);
89 if (IS_ERR(tdev
->dev
))
90 return PTR_ERR(tdev
->dev
);
92 dev_set_drvdata(tdev
->dev
, tdev
);
96 EXPORT_SYMBOL_GPL(timed_output_dev_register
);
98 void timed_output_dev_unregister(struct timed_output_dev
*tdev
)
100 tdev
->enable(tdev
, 0);
101 device_destroy(timed_output_class
, MKDEV(0, tdev
->index
));
103 EXPORT_SYMBOL_GPL(timed_output_dev_unregister
);
105 static int __init
timed_output_init(void)
107 return create_timed_output_class();
110 static void __exit
timed_output_exit(void)
112 class_destroy(timed_output_class
);
115 module_init(timed_output_init
);
116 module_exit(timed_output_exit
);
118 MODULE_AUTHOR("Mike Lockwood <lockwood@android.com>");
119 MODULE_DESCRIPTION("timed output class driver");
120 MODULE_LICENSE("GPL");