Commit | Line | Data |
---|---|---|
355b0502 GKH |
1 | /* 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 | ||
c810a399 SK |
17 | #define pr_fmt(fmt) "timed_output: " fmt |
18 | ||
355b0502 GKH |
19 | #include <linux/module.h> |
20 | #include <linux/types.h> | |
21 | #include <linux/device.h> | |
22 | #include <linux/fs.h> | |
23 | #include <linux/err.h> | |
24 | ||
25 | #include "timed_output.h" | |
26 | ||
27 | static struct class *timed_output_class; | |
28 | static atomic_t device_count; | |
29 | ||
30 | static ssize_t enable_show(struct device *dev, struct device_attribute *attr, | |
9d3ab801 | 31 | char *buf) |
355b0502 GKH |
32 | { |
33 | struct timed_output_dev *tdev = dev_get_drvdata(dev); | |
34 | int remaining = tdev->get_time(tdev); | |
35 | ||
36 | return sprintf(buf, "%d\n", remaining); | |
37 | } | |
38 | ||
9d3ab801 GKH |
39 | static ssize_t enable_store(struct device *dev, struct device_attribute *attr, |
40 | const char *buf, size_t size) | |
355b0502 GKH |
41 | { |
42 | struct timed_output_dev *tdev = dev_get_drvdata(dev); | |
43 | int value; | |
44 | ||
8bfe15f3 ML |
45 | if (sscanf(buf, "%d", &value) != 1) |
46 | return -EINVAL; | |
47 | ||
355b0502 GKH |
48 | tdev->enable(tdev, value); |
49 | ||
50 | return size; | |
51 | } | |
9d3ab801 | 52 | static DEVICE_ATTR_RW(enable); |
355b0502 | 53 | |
9d3ab801 GKH |
54 | static struct attribute *timed_output_attrs[] = { |
55 | &dev_attr_enable.attr, | |
56 | NULL, | |
57 | }; | |
58 | ATTRIBUTE_GROUPS(timed_output); | |
355b0502 GKH |
59 | |
60 | static int create_timed_output_class(void) | |
61 | { | |
62 | if (!timed_output_class) { | |
63 | timed_output_class = class_create(THIS_MODULE, "timed_output"); | |
64 | if (IS_ERR(timed_output_class)) | |
65 | return PTR_ERR(timed_output_class); | |
66 | atomic_set(&device_count, 0); | |
9d3ab801 | 67 | timed_output_class->dev_groups = timed_output_groups; |
355b0502 GKH |
68 | } |
69 | ||
70 | return 0; | |
71 | } | |
72 | ||
73 | int timed_output_dev_register(struct timed_output_dev *tdev) | |
74 | { | |
75 | int ret; | |
76 | ||
77 | if (!tdev || !tdev->name || !tdev->enable || !tdev->get_time) | |
78 | return -EINVAL; | |
79 | ||
80 | ret = create_timed_output_class(); | |
81 | if (ret < 0) | |
82 | return ret; | |
83 | ||
84 | tdev->index = atomic_inc_return(&device_count); | |
85 | tdev->dev = device_create(timed_output_class, NULL, | |
02aa2a37 | 86 | MKDEV(0, tdev->index), NULL, "%s", tdev->name); |
355b0502 GKH |
87 | if (IS_ERR(tdev->dev)) |
88 | return PTR_ERR(tdev->dev); | |
89 | ||
355b0502 GKH |
90 | dev_set_drvdata(tdev->dev, tdev); |
91 | tdev->state = 0; | |
92 | return 0; | |
355b0502 GKH |
93 | } |
94 | EXPORT_SYMBOL_GPL(timed_output_dev_register); | |
95 | ||
96 | void timed_output_dev_unregister(struct timed_output_dev *tdev) | |
97 | { | |
d9a861d5 | 98 | tdev->enable(tdev, 0); |
355b0502 GKH |
99 | device_destroy(timed_output_class, MKDEV(0, tdev->index)); |
100 | dev_set_drvdata(tdev->dev, NULL); | |
101 | } | |
102 | EXPORT_SYMBOL_GPL(timed_output_dev_unregister); | |
103 | ||
104 | static int __init timed_output_init(void) | |
105 | { | |
106 | return create_timed_output_class(); | |
107 | } | |
108 | ||
109 | static void __exit timed_output_exit(void) | |
110 | { | |
111 | class_destroy(timed_output_class); | |
112 | } | |
113 | ||
114 | module_init(timed_output_init); | |
115 | module_exit(timed_output_exit); | |
116 | ||
117 | MODULE_AUTHOR("Mike Lockwood <lockwood@android.com>"); | |
118 | MODULE_DESCRIPTION("timed output class driver"); | |
119 | MODULE_LICENSE("GPL"); |