2 * drivers/extcon/extcon_class.c
4 * External connector (extcon) class driver
6 * Copyright (C) 2012 Samsung Electronics
7 * Author: Donggeun Kim <dg77.kim@samsung.com>
8 * Author: MyungJoo Ham <myungjoo.ham@samsung.com>
10 * based on android/drivers/switch/switch_class.c
11 * Copyright (C) 2008 Google, Inc.
12 * Author: Mike Lockwood <lockwood@android.com>
14 * This software is licensed under the terms of the GNU General Public
15 * License version 2, as published by the Free Software Foundation, and
16 * may be copied, distributed, and modified under those terms.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
25 #include <linux/module.h>
26 #include <linux/types.h>
27 #include <linux/init.h>
28 #include <linux/device.h>
30 #include <linux/err.h>
31 #include <linux/extcon.h>
32 #include <linux/slab.h>
33 #include <linux/sysfs.h>
36 * extcon_cable_name suggests the standard cable names for commonly used
39 * However, please do not use extcon_cable_name directly for extcon_dev
40 * struct's supported_cable pointer unless your device really supports
41 * every single port-type of the following cable names. Please choose cable
42 * names that are actually used in your extcon device.
44 const char *extcon_cable_name[] = {
46 [EXTCON_USB_HOST] = "USB-Host",
48 [EXTCON_FAST_CHARGER] = "Fast-charger",
49 [EXTCON_SLOW_CHARGER] = "Slow-charger",
50 [EXTCON_CHARGE_DOWNSTREAM] = "Charge-downstream",
51 [EXTCON_HDMI] = "HDMI",
55 [EXTCON_DOCK] = "Dock",
56 [EXTCON_LINE_IN] = "Line-in",
57 [EXTCON_LINE_OUT] = "Line-out",
58 [EXTCON_MIC_IN] = "Microphone",
59 [EXTCON_HEADPHONE_OUT] = "Headphone",
60 [EXTCON_SPDIF_IN] = "SPDIF-in",
61 [EXTCON_SPDIF_OUT] = "SPDIF-out",
62 [EXTCON_VIDEO_IN] = "Video-in",
63 [EXTCON_VIDEO_OUT] = "Video-out",
64 [EXTCON_MECHANICAL] = "Mechanical",
69 static struct class *extcon_class;
70 #if defined(CONFIG_ANDROID)
71 static struct class_compat *switch_class;
72 #endif /* CONFIG_ANDROID */
74 static LIST_HEAD(extcon_dev_list);
75 static DEFINE_MUTEX(extcon_dev_list_lock);
78 * check_mutually_exclusive - Check if new_state violates mutually_exclusive
80 * @edev: the extcon device
81 * @new_state: new cable attach status for @edev
83 * Returns 0 if nothing violates. Returns the index + 1 for the first
86 static int check_mutually_exclusive(struct extcon_dev *edev, u32 new_state)
90 if (!edev->mutually_exclusive)
93 for (i = 0; edev->mutually_exclusive[i]; i++) {
95 u32 correspondants = new_state & edev->mutually_exclusive[i];
98 for (j = 0; j < 32; j++) {
99 if (exp & correspondants)
110 static ssize_t state_show(struct device *dev, struct device_attribute *attr,
114 struct extcon_dev *edev = (struct extcon_dev *) dev_get_drvdata(dev);
116 if (edev->print_state) {
117 int ret = edev->print_state(edev, buf);
121 /* Use default if failed */
124 if (edev->max_supported == 0)
125 return sprintf(buf, "%u\n", edev->state);
127 for (i = 0; i < SUPPORTED_CABLE_MAX; i++) {
128 if (!edev->supported_cable[i])
130 count += sprintf(buf + count, "%s=%d\n",
131 edev->supported_cable[i],
132 !!(edev->state & (1 << i)));
138 int extcon_set_state(struct extcon_dev *edev, u32 state);
139 static ssize_t state_store(struct device *dev, struct device_attribute *attr,
140 const char *buf, size_t count)
144 struct extcon_dev *edev = (struct extcon_dev *) dev_get_drvdata(dev);
146 ret = sscanf(buf, "0x%x", &state);
150 ret = extcon_set_state(edev, state);
158 static ssize_t name_show(struct device *dev, struct device_attribute *attr,
161 struct extcon_dev *edev = (struct extcon_dev *) dev_get_drvdata(dev);
163 /* Optional callback given by the user */
164 if (edev->print_name) {
165 int ret = edev->print_name(edev, buf);
170 return sprintf(buf, "%s\n", dev_name(edev->dev));
173 static ssize_t cable_name_show(struct device *dev,
174 struct device_attribute *attr, char *buf)
176 struct extcon_cable *cable = container_of(attr, struct extcon_cable,
179 return sprintf(buf, "%s\n",
180 cable->edev->supported_cable[cable->cable_index]);
183 static ssize_t cable_state_show(struct device *dev,
184 struct device_attribute *attr, char *buf)
186 struct extcon_cable *cable = container_of(attr, struct extcon_cable,
189 return sprintf(buf, "%d\n",
190 extcon_get_cable_state_(cable->edev,
191 cable->cable_index));
194 static ssize_t cable_state_store(struct device *dev,
195 struct device_attribute *attr, const char *buf,
198 struct extcon_cable *cable = container_of(attr, struct extcon_cable,
202 ret = sscanf(buf, "%d", &state);
206 ret = extcon_set_cable_state_(cable->edev, cable->cable_index,
215 * extcon_update_state() - Update the cable attach states of the extcon device
216 * only for the masked bits.
217 * @edev: the extcon device
218 * @mask: the bit mask to designate updated bits.
219 * @state: new cable attach status for @edev
221 * Changing the state sends uevent with environment variable containing
222 * the name of extcon device (envp[0]) and the state output (envp[1]).
223 * Tizen uses this format for extcon device to get events from ports.
224 * Android uses this format as well.
226 * Note that the notifier provides which bits are changed in the state
227 * variable with the val parameter (second) to the callback.
229 int extcon_update_state(struct extcon_dev *edev, u32 mask, u32 state)
239 spin_lock_irqsave(&edev->lock, flags);
241 if (edev->state != ((edev->state & ~mask) | (state & mask))) {
242 u32 old_state = edev->state;
244 if (check_mutually_exclusive(edev, (edev->state & ~mask) |
246 spin_unlock_irqrestore(&edev->lock, flags);
250 edev->state &= ~mask;
251 edev->state |= state & mask;
253 raw_notifier_call_chain(&edev->nh, old_state, edev);
255 /* This could be in interrupt handler */
256 prop_buf = (char *)get_zeroed_page(GFP_ATOMIC);
258 length = name_show(edev->dev, NULL, prop_buf);
260 if (prop_buf[length - 1] == '\n')
261 prop_buf[length - 1] = 0;
262 snprintf(name_buf, sizeof(name_buf),
263 "NAME=%s", prop_buf);
264 envp[env_offset++] = name_buf;
266 length = state_show(edev->dev, NULL, prop_buf);
268 if (prop_buf[length - 1] == '\n')
269 prop_buf[length - 1] = 0;
270 snprintf(state_buf, sizeof(state_buf),
271 "STATE=%s", prop_buf);
272 envp[env_offset++] = state_buf;
274 envp[env_offset] = NULL;
275 /* Unlock early before uevent */
276 spin_unlock_irqrestore(&edev->lock, flags);
278 kobject_uevent_env(&edev->dev->kobj, KOBJ_CHANGE, envp);
279 free_page((unsigned long)prop_buf);
281 /* Unlock early before uevent */
282 spin_unlock_irqrestore(&edev->lock, flags);
284 dev_err(edev->dev, "out of memory in extcon_set_state\n");
285 kobject_uevent(&edev->dev->kobj, KOBJ_CHANGE);
289 spin_unlock_irqrestore(&edev->lock, flags);
294 EXPORT_SYMBOL_GPL(extcon_update_state);
297 * extcon_set_state() - Set the cable attach states of the extcon device.
298 * @edev: the extcon device
299 * @state: new cable attach status for @edev
301 * Note that notifier provides which bits are changed in the state
302 * variable with the val parameter (second) to the callback.
304 int extcon_set_state(struct extcon_dev *edev, u32 state)
306 return extcon_update_state(edev, 0xffffffff, state);
308 EXPORT_SYMBOL_GPL(extcon_set_state);
311 * extcon_find_cable_index() - Get the cable index based on the cable name.
312 * @edev: the extcon device that has the cable.
313 * @cable_name: cable name to be searched.
315 * Note that accessing a cable state based on cable_index is faster than
316 * cable_name because using cable_name induces a loop with strncmp().
317 * Thus, when get/set_cable_state is repeatedly used, using cable_index
320 int extcon_find_cable_index(struct extcon_dev *edev, const char *cable_name)
324 if (edev->supported_cable) {
325 for (i = 0; edev->supported_cable[i]; i++) {
326 if (!strncmp(edev->supported_cable[i],
327 cable_name, CABLE_NAME_MAX))
334 EXPORT_SYMBOL_GPL(extcon_find_cable_index);
337 * extcon_get_cable_state_() - Get the status of a specific cable.
338 * @edev: the extcon device that has the cable.
339 * @index: cable index that can be retrieved by extcon_find_cable_index().
341 int extcon_get_cable_state_(struct extcon_dev *edev, int index)
343 if (index < 0 || (edev->max_supported && edev->max_supported <= index))
346 return !!(edev->state & (1 << index));
348 EXPORT_SYMBOL_GPL(extcon_get_cable_state_);
351 * extcon_get_cable_state() - Get the status of a specific cable.
352 * @edev: the extcon device that has the cable.
353 * @cable_name: cable name.
355 * Note that this is slower than extcon_get_cable_state_.
357 int extcon_get_cable_state(struct extcon_dev *edev, const char *cable_name)
359 return extcon_get_cable_state_(edev, extcon_find_cable_index
362 EXPORT_SYMBOL_GPL(extcon_get_cable_state);
365 * extcon_get_cable_state_() - Set the status of a specific cable.
366 * @edev: the extcon device that has the cable.
367 * @index: cable index that can be retrieved by extcon_find_cable_index().
368 * @cable_state: the new cable status. The default semantics is
369 * true: attached / false: detached.
371 int extcon_set_cable_state_(struct extcon_dev *edev,
372 int index, bool cable_state)
376 if (index < 0 || (edev->max_supported && edev->max_supported <= index))
379 state = cable_state ? (1 << index) : 0;
380 return extcon_update_state(edev, 1 << index, state);
382 EXPORT_SYMBOL_GPL(extcon_set_cable_state_);
385 * extcon_get_cable_state() - Set the status of a specific cable.
386 * @edev: the extcon device that has the cable.
387 * @cable_name: cable name.
388 * @cable_state: the new cable status. The default semantics is
389 * true: attached / false: detached.
391 * Note that this is slower than extcon_set_cable_state_.
393 int extcon_set_cable_state(struct extcon_dev *edev,
394 const char *cable_name, bool cable_state)
396 return extcon_set_cable_state_(edev, extcon_find_cable_index
397 (edev, cable_name), cable_state);
399 EXPORT_SYMBOL_GPL(extcon_set_cable_state);
402 * extcon_get_extcon_dev() - Get the extcon device instance from the name
403 * @extcon_name: The extcon name provided with extcon_dev_register()
405 struct extcon_dev *extcon_get_extcon_dev(const char *extcon_name)
407 struct extcon_dev *sd;
409 mutex_lock(&extcon_dev_list_lock);
410 list_for_each_entry(sd, &extcon_dev_list, entry) {
411 if (!strcmp(sd->name, extcon_name))
416 mutex_unlock(&extcon_dev_list_lock);
419 EXPORT_SYMBOL_GPL(extcon_get_extcon_dev);
421 static int _call_per_cable(struct notifier_block *nb, unsigned long val,
424 struct extcon_specific_cable_nb *obj = container_of(nb,
425 struct extcon_specific_cable_nb, internal_nb);
426 struct extcon_dev *edev = ptr;
428 if ((val & (1 << obj->cable_index)) !=
429 (edev->state & (1 << obj->cable_index))) {
430 bool cable_state = true;
432 obj->previous_value = val;
434 if (val & (1 << obj->cable_index))
437 return obj->user_nb->notifier_call(obj->user_nb,
445 * extcon_register_interest() - Register a notifier for a state change of a
446 * specific cable, not an entier set of cables of a
448 * @obj: an empty extcon_specific_cable_nb object to be returned.
449 * @extcon_name: the name of extcon device.
450 * @cable_name: the target cable name.
451 * @nb: the notifier block to get notified.
453 * Provide an empty extcon_specific_cable_nb. extcon_register_interest() sets
454 * the struct for you.
456 * extcon_register_interest is a helper function for those who want to get
457 * notification for a single specific cable's status change. If a user wants
458 * to get notification for any changes of all cables of a extcon device,
459 * he/she should use the general extcon_register_notifier().
461 * Note that the second parameter given to the callback of nb (val) is
462 * "old_state", not the current state. The current state can be retrieved
463 * by looking at the third pameter (edev pointer)'s state value.
465 int extcon_register_interest(struct extcon_specific_cable_nb *obj,
466 const char *extcon_name, const char *cable_name,
467 struct notifier_block *nb)
469 if (!obj || !extcon_name || !cable_name || !nb)
472 obj->edev = extcon_get_extcon_dev(extcon_name);
476 obj->cable_index = extcon_find_cable_index(obj->edev, cable_name);
477 if (obj->cable_index < 0)
482 obj->internal_nb.notifier_call = _call_per_cable;
484 return raw_notifier_chain_register(&obj->edev->nh, &obj->internal_nb);
488 * extcon_unregister_interest() - Unregister the notifier registered by
489 * extcon_register_interest().
490 * @obj: the extcon_specific_cable_nb object returned by
491 * extcon_register_interest().
493 int extcon_unregister_interest(struct extcon_specific_cable_nb *obj)
498 return raw_notifier_chain_unregister(&obj->edev->nh, &obj->internal_nb);
502 * extcon_register_notifier() - Register a notifiee to get notified by
503 * any attach status changes from the extcon.
504 * @edev: the extcon device.
505 * @nb: a notifier block to be registered.
507 * Note that the second parameter given to the callback of nb (val) is
508 * "old_state", not the current state. The current state can be retrieved
509 * by looking at the third pameter (edev pointer)'s state value.
511 int extcon_register_notifier(struct extcon_dev *edev,
512 struct notifier_block *nb)
514 return raw_notifier_chain_register(&edev->nh, nb);
516 EXPORT_SYMBOL_GPL(extcon_register_notifier);
519 * extcon_unregister_notifier() - Unregister a notifiee from the extcon device.
520 * @edev: the extcon device.
521 * @nb: a registered notifier block to be unregistered.
523 int extcon_unregister_notifier(struct extcon_dev *edev,
524 struct notifier_block *nb)
526 return raw_notifier_chain_unregister(&edev->nh, nb);
528 EXPORT_SYMBOL_GPL(extcon_unregister_notifier);
530 static struct device_attribute extcon_attrs[] = {
531 __ATTR(state, S_IRUGO | S_IWUSR, state_show, state_store),
536 static int create_extcon_class(void)
539 extcon_class = class_create(THIS_MODULE, "extcon");
540 if (IS_ERR(extcon_class))
541 return PTR_ERR(extcon_class);
542 extcon_class->dev_attrs = extcon_attrs;
544 #if defined(CONFIG_ANDROID)
545 switch_class = class_compat_register("switch");
546 if (WARN(!switch_class, "cannot allocate"))
548 #endif /* CONFIG_ANDROID */
554 static void extcon_cleanup(struct extcon_dev *edev, bool skip)
556 mutex_lock(&extcon_dev_list_lock);
557 list_del(&edev->entry);
558 mutex_unlock(&extcon_dev_list_lock);
560 if (!skip && get_device(edev->dev)) {
563 if (edev->mutually_exclusive && edev->max_supported) {
564 for (index = 0; edev->mutually_exclusive[index];
566 kfree(edev->d_attrs_muex[index].attr.name);
567 kfree(edev->d_attrs_muex);
568 kfree(edev->attrs_muex);
571 for (index = 0; index < edev->max_supported; index++)
572 kfree(edev->cables[index].attr_g.name);
574 if (edev->max_supported) {
575 kfree(edev->extcon_dev_type.groups);
579 device_unregister(edev->dev);
580 put_device(edev->dev);
586 static void extcon_dev_release(struct device *dev)
588 struct extcon_dev *edev = (struct extcon_dev *) dev_get_drvdata(dev);
590 extcon_cleanup(edev, true);
593 static const char *muex_name = "mutually_exclusive";
594 static void dummy_sysfs_dev_release(struct device *dev)
599 * extcon_dev_register() - Register a new extcon device
600 * @edev : the new extcon device (should be allocated before calling)
601 * @dev : the parent device for this extcon device.
603 * Among the members of edev struct, please set the "user initializing data"
604 * in any case and set the "optional callbacks" if required. However, please
605 * do not set the values of "internal data", which are initialized by
608 int extcon_dev_register(struct extcon_dev *edev, struct device *dev)
613 ret = create_extcon_class();
618 if (edev->supported_cable) {
619 /* Get size of array */
620 for (index = 0; edev->supported_cable[index]; index++)
622 edev->max_supported = index;
624 edev->max_supported = 0;
627 if (index > SUPPORTED_CABLE_MAX) {
628 dev_err(edev->dev, "extcon: maximum number of supported cables exceeded.\n");
632 edev->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
635 edev->dev->parent = dev;
636 edev->dev->class = extcon_class;
637 edev->dev->release = extcon_dev_release;
639 dev_set_name(edev->dev, edev->name ? edev->name : dev_name(dev));
641 if (edev->max_supported) {
644 struct extcon_cable *cable;
646 edev->cables = kzalloc(sizeof(struct extcon_cable) *
647 edev->max_supported, GFP_KERNEL);
650 goto err_sysfs_alloc;
652 for (index = 0; index < edev->max_supported; index++) {
653 cable = &edev->cables[index];
655 snprintf(buf, 10, "cable.%d", index);
656 str = kzalloc(sizeof(char) * (strlen(buf) + 1),
659 for (index--; index >= 0; index--) {
660 cable = &edev->cables[index];
661 kfree(cable->attr_g.name);
665 goto err_alloc_cables;
670 cable->cable_index = index;
671 cable->attrs[0] = &cable->attr_name.attr;
672 cable->attrs[1] = &cable->attr_state.attr;
673 cable->attrs[2] = NULL;
674 cable->attr_g.name = str;
675 cable->attr_g.attrs = cable->attrs;
677 sysfs_attr_init(&cable->attr_name.attr);
678 cable->attr_name.attr.name = "name";
679 cable->attr_name.attr.mode = 0444;
680 cable->attr_name.show = cable_name_show;
682 sysfs_attr_init(&cable->attr_state.attr);
683 cable->attr_state.attr.name = "state";
684 cable->attr_state.attr.mode = 0644;
685 cable->attr_state.show = cable_state_show;
686 cable->attr_state.store = cable_state_store;
690 if (edev->max_supported && edev->mutually_exclusive) {
694 /* Count the size of mutually_exclusive array */
695 for (index = 0; edev->mutually_exclusive[index]; index++)
698 edev->attrs_muex = kzalloc(sizeof(struct attribute *) *
699 (index + 1), GFP_KERNEL);
700 if (!edev->attrs_muex) {
705 edev->d_attrs_muex = kzalloc(sizeof(struct device_attribute) *
707 if (!edev->d_attrs_muex) {
709 kfree(edev->attrs_muex);
713 for (index = 0; edev->mutually_exclusive[index]; index++) {
714 sprintf(buf, "0x%x", edev->mutually_exclusive[index]);
715 name = kzalloc(sizeof(char) * (strlen(buf) + 1),
718 for (index--; index >= 0; index--) {
719 kfree(edev->d_attrs_muex[index].attr.
722 kfree(edev->d_attrs_muex);
723 kfree(edev->attrs_muex);
728 sysfs_attr_init(&edev->d_attrs_muex[index].attr);
729 edev->d_attrs_muex[index].attr.name = name;
730 edev->d_attrs_muex[index].attr.mode = 0000;
731 edev->attrs_muex[index] = &edev->d_attrs_muex[index]
734 edev->attr_g_muex.name = muex_name;
735 edev->attr_g_muex.attrs = edev->attrs_muex;
739 if (edev->max_supported) {
740 edev->extcon_dev_type.groups =
741 kzalloc(sizeof(struct attribute_group *) *
742 (edev->max_supported + 2), GFP_KERNEL);
743 if (!edev->extcon_dev_type.groups) {
745 goto err_alloc_groups;
748 edev->extcon_dev_type.name = dev_name(edev->dev);
749 edev->extcon_dev_type.release = dummy_sysfs_dev_release;
751 for (index = 0; index < edev->max_supported; index++)
752 edev->extcon_dev_type.groups[index] =
753 &edev->cables[index].attr_g;
754 if (edev->mutually_exclusive)
755 edev->extcon_dev_type.groups[index] =
758 edev->dev->type = &edev->extcon_dev_type;
761 ret = device_register(edev->dev);
763 put_device(edev->dev);
766 #if defined(CONFIG_ANDROID)
768 ret = class_compat_create_link(switch_class, edev->dev,
770 #endif /* CONFIG_ANDROID */
772 spin_lock_init(&edev->lock);
774 RAW_INIT_NOTIFIER_HEAD(&edev->nh);
776 dev_set_drvdata(edev->dev, edev);
779 mutex_lock(&extcon_dev_list_lock);
780 list_add(&edev->entry, &extcon_dev_list);
781 mutex_unlock(&extcon_dev_list_lock);
786 if (edev->max_supported)
787 kfree(edev->extcon_dev_type.groups);
789 if (edev->max_supported && edev->mutually_exclusive) {
790 for (index = 0; edev->mutually_exclusive[index]; index++)
791 kfree(edev->d_attrs_muex[index].attr.name);
792 kfree(edev->d_attrs_muex);
793 kfree(edev->attrs_muex);
796 for (index = 0; index < edev->max_supported; index++)
797 kfree(edev->cables[index].attr_g.name);
799 if (edev->max_supported)
805 EXPORT_SYMBOL_GPL(extcon_dev_register);
808 * extcon_dev_unregister() - Unregister the extcon device.
809 * @edev: the extcon device instance to be unregistered.
811 * Note that this does not call kfree(edev) because edev was not allocated
814 void extcon_dev_unregister(struct extcon_dev *edev)
816 extcon_cleanup(edev, false);
818 EXPORT_SYMBOL_GPL(extcon_dev_unregister);
820 static int __init extcon_class_init(void)
822 return create_extcon_class();
824 module_init(extcon_class_init);
826 static void __exit extcon_class_exit(void)
828 class_destroy(extcon_class);
830 module_exit(extcon_class_exit);
832 MODULE_AUTHOR("Mike Lockwood <lockwood@android.com>");
833 MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>");
834 MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
835 MODULE_DESCRIPTION("External connector (extcon) class driver");
836 MODULE_LICENSE("GPL");