blob: aff9cdb007e5b71f35b9f2495571dcdb0c895acd [file] [log] [blame]
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001/* drivers/misc/timed_output.c
2 *
3 * Copyright (C) 2009 Google, Inc.
4 * Author: Mike Lockwood <lockwood@android.com>
5 *
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.
9 *
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.
14 *
15 */
16
Sachin Kamatc810a392012-06-05 16:40:10 +053017#define pr_fmt(fmt) "timed_output: " fmt
18
Paul Gortmakere20ec732015-10-11 15:47:33 -040019#include <linux/init.h>
20#include <linux/export.h>
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090021#include <linux/types.h>
22#include <linux/device.h>
23#include <linux/fs.h>
24#include <linux/err.h>
25
26#include "timed_output.h"
27
28static struct class *timed_output_class;
29static atomic_t device_count;
30
31static ssize_t enable_show(struct device *dev, struct device_attribute *attr,
Greg Kroah-Hartman9d3ab802013-08-24 10:27:29 -070032 char *buf)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090033{
34 struct timed_output_dev *tdev = dev_get_drvdata(dev);
35 int remaining = tdev->get_time(tdev);
36
37 return sprintf(buf, "%d\n", remaining);
38}
39
Greg Kroah-Hartman9d3ab802013-08-24 10:27:29 -070040static ssize_t enable_store(struct device *dev, struct device_attribute *attr,
41 const char *buf, size_t size)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090042{
43 struct timed_output_dev *tdev = dev_get_drvdata(dev);
44 int value;
Murilo Opsfelder Araujo6aa26212014-07-28 20:45:06 -030045 int rc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090046
Murilo Opsfelder Araujo6aa26212014-07-28 20:45:06 -030047 rc = kstrtoint(buf, 0, &value);
48 if (rc != 0)
Mike Lockwood8bfe15f2010-04-17 12:01:35 -040049 return -EINVAL;
50
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090051 tdev->enable(tdev, value);
52
53 return size;
54}
Greg Kroah-Hartman9d3ab802013-08-24 10:27:29 -070055static DEVICE_ATTR_RW(enable);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090056
Greg Kroah-Hartman9d3ab802013-08-24 10:27:29 -070057static struct attribute *timed_output_attrs[] = {
58 &dev_attr_enable.attr,
59 NULL,
60};
61ATTRIBUTE_GROUPS(timed_output);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090062
63static int create_timed_output_class(void)
64{
65 if (!timed_output_class) {
66 timed_output_class = class_create(THIS_MODULE, "timed_output");
67 if (IS_ERR(timed_output_class))
68 return PTR_ERR(timed_output_class);
69 atomic_set(&device_count, 0);
Greg Kroah-Hartman9d3ab802013-08-24 10:27:29 -070070 timed_output_class->dev_groups = timed_output_groups;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090071 }
72
73 return 0;
74}
75
76int timed_output_dev_register(struct timed_output_dev *tdev)
77{
78 int ret;
79
80 if (!tdev || !tdev->name || !tdev->enable || !tdev->get_time)
81 return -EINVAL;
82
83 ret = create_timed_output_class();
84 if (ret < 0)
85 return ret;
86
87 tdev->index = atomic_inc_return(&device_count);
88 tdev->dev = device_create(timed_output_class, NULL,
Kees Cook02aa2a32013-07-03 15:04:56 -070089 MKDEV(0, tdev->index), NULL, "%s", tdev->name);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090090 if (IS_ERR(tdev->dev))
91 return PTR_ERR(tdev->dev);
92
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090093 dev_set_drvdata(tdev->dev, tdev);
94 tdev->state = 0;
95 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090096}
97EXPORT_SYMBOL_GPL(timed_output_dev_register);
98
99void timed_output_dev_unregister(struct timed_output_dev *tdev)
100{
Kim, Milod9a861d2012-04-26 22:01:10 -0700101 tdev->enable(tdev, 0);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900102 device_destroy(timed_output_class, MKDEV(0, tdev->index));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900103}
104EXPORT_SYMBOL_GPL(timed_output_dev_unregister);
105
106static int __init timed_output_init(void)
107{
108 return create_timed_output_class();
109}
Paul Gortmakere20ec732015-10-11 15:47:33 -0400110device_initcall(timed_output_init);