2 * drivers/switch/switch_class.c
4 * Copyright (C) 2008 Google, Inc.
5 * Author: Mike Lockwood <lockwood@android.com>
7 * This software is licensed under the terms of the GNU General Public
8 * License version 2, as published by the Free Software Foundation, and
9 * may be copied, distributed, and modified under those terms.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
18 #include <linux/module.h>
19 #include <linux/types.h>
20 #include <linux/init.h>
21 #include <linux/device.h>
23 #include <linux/err.h>
24 #include <linux/switch.h>
26 struct class *switch_class;
27 static atomic_t device_count;
29 static LIST_HEAD(switch_dev_list);
30 static DEFINE_MUTEX(switch_dev_list_lock);
32 static ssize_t state_show(struct device *dev, struct device_attribute *attr,
35 struct switch_dev *sdev = (struct switch_dev *)
38 if (sdev->print_state) {
39 int ret = sdev->print_state(sdev, buf);
43 return sprintf(buf, "%d\n", sdev->state);
46 static ssize_t name_show(struct device *dev, struct device_attribute *attr,
49 struct switch_dev *sdev = (struct switch_dev *)
52 if (sdev->print_name) {
53 int ret = sdev->print_name(sdev, buf);
57 return sprintf(buf, "%s\n", sdev->name);
60 static DEVICE_ATTR(state, S_IRUGO | S_IWUSR, state_show, NULL);
61 static DEVICE_ATTR(name, S_IRUGO | S_IWUSR, name_show, NULL);
63 void switch_set_state(struct switch_dev *sdev, int state)
72 if (sdev->state != state) {
75 raw_notifier_call_chain(&sdev->nh, sdev->state, NULL);
77 prop_buf = (char *)get_zeroed_page(GFP_KERNEL);
79 length = name_show(sdev->dev, NULL, prop_buf);
81 if (prop_buf[length - 1] == '\n')
82 prop_buf[length - 1] = 0;
83 snprintf(name_buf, sizeof(name_buf),
84 "SWITCH_NAME=%s", prop_buf);
85 envp[env_offset++] = name_buf;
87 length = state_show(sdev->dev, NULL, prop_buf);
89 if (prop_buf[length - 1] == '\n')
90 prop_buf[length - 1] = 0;
91 snprintf(state_buf, sizeof(state_buf),
92 "SWITCH_STATE=%s", prop_buf);
93 envp[env_offset++] = state_buf;
95 envp[env_offset] = NULL;
96 kobject_uevent_env(&sdev->dev->kobj, KOBJ_CHANGE, envp);
97 free_page((unsigned long)prop_buf);
100 printk(KERN_ERR "out of memory in switch_set_state\n");
101 kobject_uevent(&sdev->dev->kobj, KOBJ_CHANGE);
105 EXPORT_SYMBOL_GPL(switch_set_state);
107 static int switch_find_cable_index(struct switch_dev *sdev, const char *cable_name)
111 if (sdev->supported_cable) {
112 for (i = 0; sdev->supported_cable[i]; i++) {
113 if (!strncmp(sdev->supported_cable[i],
114 cable_name, CABLE_NAME_MAX))
122 int switch_set_cable_state(struct switch_dev *sdev,
123 const char *cable_name, int cable_state)
125 int ret = -EINVAL, state;
126 int index = switch_find_cable_index(sdev, cable_name);
130 state = sdev->state | (1 << index);
132 state = ~(~sdev->state | (1 << index));
133 switch_set_state(sdev, state);
139 EXPORT_SYMBOL_GPL(switch_set_cable_state);
141 int switch_get_cable_state(struct switch_dev *sdev, const char *cable_name)
143 int ret = -EINVAL, cable_state;
144 int index = switch_find_cable_index(sdev, cable_name);
147 cable_state = sdev->state & (1 << index);
148 ret = cable_state ? 1 : 0;
153 EXPORT_SYMBOL_GPL(switch_get_cable_state);
155 int switch_get_event_code(struct switch_dev *sdev, const char *cable_name)
158 int index = switch_find_cable_index(sdev, cable_name);
165 EXPORT_SYMBOL_GPL(switch_get_event_code);
167 struct switch_dev *switch_get_switch_dev(const char *switch_name)
169 struct switch_dev *sd;
171 mutex_lock(&switch_dev_list_lock);
172 list_for_each_entry(sd, &switch_dev_list, entry) {
173 if (!strcmp(sd->name, switch_name))
178 mutex_unlock(&switch_dev_list_lock);
181 EXPORT_SYMBOL_GPL(switch_get_switch_dev);
183 int switch_register_notifier(struct switch_dev *sdev,
184 struct notifier_block *bh)
186 return raw_notifier_chain_register(&sdev->nh, bh);
188 EXPORT_SYMBOL_GPL(switch_register_notifier);
190 int switch_unregister_notifier(struct switch_dev *sdev,
191 struct notifier_block *bh)
193 return raw_notifier_chain_register(&sdev->nh, bh);
195 EXPORT_SYMBOL_GPL(switch_unregister_notifier);
197 static int create_switch_class(void)
200 switch_class = class_create(THIS_MODULE, "switch");
201 if (IS_ERR(switch_class))
202 return PTR_ERR(switch_class);
203 atomic_set(&device_count, 0);
209 int switch_dev_register(struct switch_dev *sdev)
214 ret = create_switch_class();
219 if (sdev->supported_cable) {
220 /* Get size of array */
221 for (index = 0; sdev->supported_cable[index]; index++)
223 if (index > SUPPORTED_CABLE_MAX) {
225 "switch: Exceed maximum number of supported cable\n");
230 if (index > SUPPORTED_CABLE_MAX) {
232 "switch: Exceed maximum number of supported cable\n");
236 sdev->index = atomic_inc_return(&device_count);
237 sdev->dev = device_create(switch_class, NULL,
238 MKDEV(0, sdev->index), NULL, sdev->name);
239 if (IS_ERR(sdev->dev))
240 return PTR_ERR(sdev->dev);
242 ret = device_create_file(sdev->dev, &dev_attr_state);
244 goto err_create_file_1;
245 ret = device_create_file(sdev->dev, &dev_attr_name);
247 goto err_create_file_2;
249 RAW_INIT_NOTIFIER_HEAD(&sdev->nh);
251 dev_set_drvdata(sdev->dev, sdev);
254 mutex_lock(&switch_dev_list_lock);
255 list_add(&sdev->entry, &switch_dev_list);
256 mutex_unlock(&switch_dev_list_lock);
261 device_remove_file(sdev->dev, &dev_attr_state);
263 device_destroy(switch_class, MKDEV(0, sdev->index));
264 printk(KERN_ERR "switch: Failed to register driver %s\n", sdev->name);
268 EXPORT_SYMBOL_GPL(switch_dev_register);
270 void switch_dev_unregister(struct switch_dev *sdev)
272 device_remove_file(sdev->dev, &dev_attr_name);
273 device_remove_file(sdev->dev, &dev_attr_state);
274 device_destroy(switch_class, MKDEV(0, sdev->index));
276 mutex_lock(&switch_dev_list_lock);
277 list_del(&sdev->entry);
278 mutex_unlock(&switch_dev_list_lock);
280 dev_set_drvdata(sdev->dev, NULL);
282 EXPORT_SYMBOL_GPL(switch_dev_unregister);
284 static int __init switch_class_init(void)
286 return create_switch_class();
289 static void __exit switch_class_exit(void)
291 class_destroy(switch_class);
294 module_init(switch_class_init);
295 module_exit(switch_class_exit);
297 MODULE_AUTHOR("Mike Lockwood <lockwood@android.com>");
298 MODULE_DESCRIPTION("Switch class driver");
299 MODULE_LICENSE("GPL");