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[][CABLE_NAME_MAX + 1] = {
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",
67 static struct class *extcon_class;
68 #if defined(CONFIG_ANDROID)
69 static struct class_compat *switch_class;
70 #endif /* CONFIG_ANDROID */
72 static LIST_HEAD(extcon_dev_list);
73 static DEFINE_MUTEX(extcon_dev_list_lock);
76 * check_mutually_exclusive - Check if new_state violates mutually_exclusive
78 * @edev: the extcon device
79 * @new_state: new cable attach status for @edev
81 * Returns 0 if nothing violates. Returns the index + 1 for the first
84 static int check_mutually_exclusive(struct extcon_dev *edev, u32 new_state)
88 if (!edev->mutually_exclusive)
91 for (i = 0; edev->mutually_exclusive[i]; i++) {
93 u32 correspondants = new_state & edev->mutually_exclusive[i];
95 /* calculate the total number of bits set */
96 weight = hweight32(correspondants);
104 static ssize_t state_show(struct device *dev, struct device_attribute *attr,
108 struct extcon_dev *edev = (struct extcon_dev *) dev_get_drvdata(dev);
110 if (edev->print_state) {
111 int ret = edev->print_state(edev, buf);
115 /* Use default if failed */
118 if (edev->max_supported == 0)
119 return sprintf(buf, "%u\n", edev->state);
121 for (i = 0; i < SUPPORTED_CABLE_MAX; i++) {
122 if (!edev->supported_cable[i])
124 count += sprintf(buf + count, "%s=%d\n",
125 edev->supported_cable[i],
126 !!(edev->state & (1 << i)));
132 int extcon_set_state(struct extcon_dev *edev, u32 state);
133 static ssize_t state_store(struct device *dev, struct device_attribute *attr,
134 const char *buf, size_t count)
138 struct extcon_dev *edev = (struct extcon_dev *) dev_get_drvdata(dev);
140 ret = sscanf(buf, "0x%x", &state);
144 ret = extcon_set_state(edev, state);
152 static ssize_t name_show(struct device *dev, struct device_attribute *attr,
155 struct extcon_dev *edev = (struct extcon_dev *) dev_get_drvdata(dev);
157 /* Optional callback given by the user */
158 if (edev->print_name) {
159 int ret = edev->print_name(edev, buf);
164 return sprintf(buf, "%s\n", dev_name(edev->dev));
167 static ssize_t cable_name_show(struct device *dev,
168 struct device_attribute *attr, char *buf)
170 struct extcon_cable *cable = container_of(attr, struct extcon_cable,
173 return sprintf(buf, "%s\n",
174 cable->edev->supported_cable[cable->cable_index]);
177 static ssize_t cable_state_show(struct device *dev,
178 struct device_attribute *attr, char *buf)
180 struct extcon_cable *cable = container_of(attr, struct extcon_cable,
183 return sprintf(buf, "%d\n",
184 extcon_get_cable_state_(cable->edev,
185 cable->cable_index));
188 static ssize_t cable_state_store(struct device *dev,
189 struct device_attribute *attr, const char *buf,
192 struct extcon_cable *cable = container_of(attr, struct extcon_cable,
196 ret = sscanf(buf, "%d", &state);
200 ret = extcon_set_cable_state_(cable->edev, cable->cable_index,
209 * extcon_update_state() - Update the cable attach states of the extcon device
210 * only for the masked bits.
211 * @edev: the extcon device
212 * @mask: the bit mask to designate updated bits.
213 * @state: new cable attach status for @edev
215 * Changing the state sends uevent with environment variable containing
216 * the name of extcon device (envp[0]) and the state output (envp[1]).
217 * Tizen uses this format for extcon device to get events from ports.
218 * Android uses this format as well.
220 * Note that the notifier provides which bits are changed in the state
221 * variable with the val parameter (second) to the callback.
223 int extcon_update_state(struct extcon_dev *edev, u32 mask, u32 state)
233 spin_lock_irqsave(&edev->lock, flags);
235 if (edev->state != ((edev->state & ~mask) | (state & mask))) {
236 u32 old_state = edev->state;
238 if (check_mutually_exclusive(edev, (edev->state & ~mask) |
240 spin_unlock_irqrestore(&edev->lock, flags);
244 edev->state &= ~mask;
245 edev->state |= state & mask;
247 raw_notifier_call_chain(&edev->nh, old_state, edev);
249 /* This could be in interrupt handler */
250 prop_buf = (char *)get_zeroed_page(GFP_ATOMIC);
252 length = name_show(edev->dev, NULL, prop_buf);
254 if (prop_buf[length - 1] == '\n')
255 prop_buf[length - 1] = 0;
256 snprintf(name_buf, sizeof(name_buf),
257 "NAME=%s", prop_buf);
258 envp[env_offset++] = name_buf;
260 length = state_show(edev->dev, NULL, prop_buf);
262 if (prop_buf[length - 1] == '\n')
263 prop_buf[length - 1] = 0;
264 snprintf(state_buf, sizeof(state_buf),
265 "STATE=%s", prop_buf);
266 envp[env_offset++] = state_buf;
268 envp[env_offset] = NULL;
269 /* Unlock early before uevent */
270 spin_unlock_irqrestore(&edev->lock, flags);
272 kobject_uevent_env(&edev->dev->kobj, KOBJ_CHANGE, envp);
273 free_page((unsigned long)prop_buf);
275 /* Unlock early before uevent */
276 spin_unlock_irqrestore(&edev->lock, flags);
278 dev_err(edev->dev, "out of memory in extcon_set_state\n");
279 kobject_uevent(&edev->dev->kobj, KOBJ_CHANGE);
283 spin_unlock_irqrestore(&edev->lock, flags);
288 EXPORT_SYMBOL_GPL(extcon_update_state);
291 * extcon_set_state() - Set the cable attach states of the extcon device.
292 * @edev: the extcon device
293 * @state: new cable attach status for @edev
295 * Note that notifier provides which bits are changed in the state
296 * variable with the val parameter (second) to the callback.
298 int extcon_set_state(struct extcon_dev *edev, u32 state)
300 return extcon_update_state(edev, 0xffffffff, state);
302 EXPORT_SYMBOL_GPL(extcon_set_state);
305 * extcon_find_cable_index() - Get the cable index based on the cable name.
306 * @edev: the extcon device that has the cable.
307 * @cable_name: cable name to be searched.
309 * Note that accessing a cable state based on cable_index is faster than
310 * cable_name because using cable_name induces a loop with strncmp().
311 * Thus, when get/set_cable_state is repeatedly used, using cable_index
314 int extcon_find_cable_index(struct extcon_dev *edev, const char *cable_name)
318 if (edev->supported_cable) {
319 for (i = 0; edev->supported_cable[i]; i++) {
320 if (!strncmp(edev->supported_cable[i],
321 cable_name, CABLE_NAME_MAX))
328 EXPORT_SYMBOL_GPL(extcon_find_cable_index);
331 * extcon_get_cable_state_() - Get the status of a specific cable.
332 * @edev: the extcon device that has the cable.
333 * @index: cable index that can be retrieved by extcon_find_cable_index().
335 int extcon_get_cable_state_(struct extcon_dev *edev, int index)
337 if (index < 0 || (edev->max_supported && edev->max_supported <= index))
340 return !!(edev->state & (1 << index));
342 EXPORT_SYMBOL_GPL(extcon_get_cable_state_);
345 * extcon_get_cable_state() - Get the status of a specific cable.
346 * @edev: the extcon device that has the cable.
347 * @cable_name: cable name.
349 * Note that this is slower than extcon_get_cable_state_.
351 int extcon_get_cable_state(struct extcon_dev *edev, const char *cable_name)
353 return extcon_get_cable_state_(edev, extcon_find_cable_index
356 EXPORT_SYMBOL_GPL(extcon_get_cable_state);
359 * extcon_set_cable_state_() - Set the status of a specific cable.
360 * @edev: the extcon device that has the cable.
361 * @index: cable index that can be retrieved by extcon_find_cable_index().
362 * @cable_state: the new cable status. The default semantics is
363 * true: attached / false: detached.
365 int extcon_set_cable_state_(struct extcon_dev *edev,
366 int index, bool cable_state)
370 if (index < 0 || (edev->max_supported && edev->max_supported <= index))
373 state = cable_state ? (1 << index) : 0;
374 return extcon_update_state(edev, 1 << index, state);
376 EXPORT_SYMBOL_GPL(extcon_set_cable_state_);
379 * extcon_set_cable_state() - Set the status of a specific cable.
380 * @edev: the extcon device that has the cable.
381 * @cable_name: cable name.
382 * @cable_state: the new cable status. The default semantics is
383 * true: attached / false: detached.
385 * Note that this is slower than extcon_set_cable_state_.
387 int extcon_set_cable_state(struct extcon_dev *edev,
388 const char *cable_name, bool cable_state)
390 return extcon_set_cable_state_(edev, extcon_find_cable_index
391 (edev, cable_name), cable_state);
393 EXPORT_SYMBOL_GPL(extcon_set_cable_state);
396 * extcon_get_extcon_dev() - Get the extcon device instance from the name
397 * @extcon_name: The extcon name provided with extcon_dev_register()
399 struct extcon_dev *extcon_get_extcon_dev(const char *extcon_name)
401 struct extcon_dev *sd;
403 mutex_lock(&extcon_dev_list_lock);
404 list_for_each_entry(sd, &extcon_dev_list, entry) {
405 if (!strcmp(sd->name, extcon_name))
410 mutex_unlock(&extcon_dev_list_lock);
413 EXPORT_SYMBOL_GPL(extcon_get_extcon_dev);
415 static int _call_per_cable(struct notifier_block *nb, unsigned long val,
418 struct extcon_specific_cable_nb *obj = container_of(nb,
419 struct extcon_specific_cable_nb, internal_nb);
420 struct extcon_dev *edev = ptr;
422 if ((val & (1 << obj->cable_index)) !=
423 (edev->state & (1 << obj->cable_index))) {
424 bool cable_state = true;
426 obj->previous_value = val;
428 if (val & (1 << obj->cable_index))
431 return obj->user_nb->notifier_call(obj->user_nb,
439 * extcon_register_interest() - Register a notifier for a state change of a
440 * specific cable, not an entier set of cables of a
442 * @obj: an empty extcon_specific_cable_nb object to be returned.
443 * @extcon_name: the name of extcon device.
444 * if NULL, extcon_register_interest will register
445 * every cable with the target cable_name given.
446 * @cable_name: the target cable name.
447 * @nb: the notifier block to get notified.
449 * Provide an empty extcon_specific_cable_nb. extcon_register_interest() sets
450 * the struct for you.
452 * extcon_register_interest is a helper function for those who want to get
453 * notification for a single specific cable's status change. If a user wants
454 * to get notification for any changes of all cables of a extcon device,
455 * he/she should use the general extcon_register_notifier().
457 * Note that the second parameter given to the callback of nb (val) is
458 * "old_state", not the current state. The current state can be retrieved
459 * by looking at the third pameter (edev pointer)'s state value.
461 int extcon_register_interest(struct extcon_specific_cable_nb *obj,
462 const char *extcon_name, const char *cable_name,
463 struct notifier_block *nb)
465 if (!obj || !cable_name || !nb)
469 obj->edev = extcon_get_extcon_dev(extcon_name);
473 obj->cable_index = extcon_find_cable_index(obj->edev, cable_name);
474 if (obj->cable_index < 0)
475 return obj->cable_index;
479 obj->internal_nb.notifier_call = _call_per_cable;
481 return raw_notifier_chain_register(&obj->edev->nh, &obj->internal_nb);
483 struct class_dev_iter iter;
484 struct extcon_dev *extd;
489 class_dev_iter_init(&iter, extcon_class, NULL, NULL);
490 while ((dev = class_dev_iter_next(&iter))) {
491 extd = (struct extcon_dev *)dev_get_drvdata(dev);
493 if (extcon_find_cable_index(extd, cable_name) < 0)
496 class_dev_iter_exit(&iter);
497 return extcon_register_interest(obj, extd->name,
506 * extcon_unregister_interest() - Unregister the notifier registered by
507 * extcon_register_interest().
508 * @obj: the extcon_specific_cable_nb object returned by
509 * extcon_register_interest().
511 int extcon_unregister_interest(struct extcon_specific_cable_nb *obj)
516 return raw_notifier_chain_unregister(&obj->edev->nh, &obj->internal_nb);
520 * extcon_register_notifier() - Register a notifiee to get notified by
521 * any attach status changes from the extcon.
522 * @edev: the extcon device.
523 * @nb: a notifier block to be registered.
525 * Note that the second parameter given to the callback of nb (val) is
526 * "old_state", not the current state. The current state can be retrieved
527 * by looking at the third pameter (edev pointer)'s state value.
529 int extcon_register_notifier(struct extcon_dev *edev,
530 struct notifier_block *nb)
532 return raw_notifier_chain_register(&edev->nh, nb);
534 EXPORT_SYMBOL_GPL(extcon_register_notifier);
537 * extcon_unregister_notifier() - Unregister a notifiee from the extcon device.
538 * @edev: the extcon device.
539 * @nb: a registered notifier block to be unregistered.
541 int extcon_unregister_notifier(struct extcon_dev *edev,
542 struct notifier_block *nb)
544 return raw_notifier_chain_unregister(&edev->nh, nb);
546 EXPORT_SYMBOL_GPL(extcon_unregister_notifier);
548 static struct device_attribute extcon_attrs[] = {
549 __ATTR(state, S_IRUGO | S_IWUSR, state_show, state_store),
554 static int create_extcon_class(void)
557 extcon_class = class_create(THIS_MODULE, "extcon");
558 if (IS_ERR(extcon_class))
559 return PTR_ERR(extcon_class);
560 extcon_class->dev_attrs = extcon_attrs;
562 #if defined(CONFIG_ANDROID)
563 switch_class = class_compat_register("switch");
564 if (WARN(!switch_class, "cannot allocate"))
566 #endif /* CONFIG_ANDROID */
572 static void extcon_dev_release(struct device *dev)
577 static const char *muex_name = "mutually_exclusive";
578 static void dummy_sysfs_dev_release(struct device *dev)
583 * extcon_dev_register() - Register a new extcon device
584 * @edev : the new extcon device (should be allocated before calling)
585 * @dev : the parent device for this extcon device.
587 * Among the members of edev struct, please set the "user initializing data"
588 * in any case and set the "optional callbacks" if required. However, please
589 * do not set the values of "internal data", which are initialized by
592 int extcon_dev_register(struct extcon_dev *edev, struct device *dev)
597 ret = create_extcon_class();
602 if (edev->supported_cable) {
603 /* Get size of array */
604 for (index = 0; edev->supported_cable[index]; index++)
606 edev->max_supported = index;
608 edev->max_supported = 0;
611 if (index > SUPPORTED_CABLE_MAX) {
612 dev_err(edev->dev, "extcon: maximum number of supported cables exceeded.\n");
616 edev->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
619 edev->dev->parent = dev;
620 edev->dev->class = extcon_class;
621 edev->dev->release = extcon_dev_release;
623 dev_set_name(edev->dev, edev->name ? edev->name : dev_name(dev));
625 if (edev->max_supported) {
628 struct extcon_cable *cable;
630 edev->cables = kzalloc(sizeof(struct extcon_cable) *
631 edev->max_supported, GFP_KERNEL);
634 goto err_sysfs_alloc;
636 for (index = 0; index < edev->max_supported; index++) {
637 cable = &edev->cables[index];
639 snprintf(buf, 10, "cable.%d", index);
640 str = kzalloc(sizeof(char) * (strlen(buf) + 1),
643 for (index--; index >= 0; index--) {
644 cable = &edev->cables[index];
645 kfree(cable->attr_g.name);
649 goto err_alloc_cables;
654 cable->cable_index = index;
655 cable->attrs[0] = &cable->attr_name.attr;
656 cable->attrs[1] = &cable->attr_state.attr;
657 cable->attrs[2] = NULL;
658 cable->attr_g.name = str;
659 cable->attr_g.attrs = cable->attrs;
661 sysfs_attr_init(&cable->attr_name.attr);
662 cable->attr_name.attr.name = "name";
663 cable->attr_name.attr.mode = 0444;
664 cable->attr_name.show = cable_name_show;
666 sysfs_attr_init(&cable->attr_state.attr);
667 cable->attr_state.attr.name = "state";
668 cable->attr_state.attr.mode = 0644;
669 cable->attr_state.show = cable_state_show;
670 cable->attr_state.store = cable_state_store;
674 if (edev->max_supported && edev->mutually_exclusive) {
678 /* Count the size of mutually_exclusive array */
679 for (index = 0; edev->mutually_exclusive[index]; index++)
682 edev->attrs_muex = kzalloc(sizeof(struct attribute *) *
683 (index + 1), GFP_KERNEL);
684 if (!edev->attrs_muex) {
689 edev->d_attrs_muex = kzalloc(sizeof(struct device_attribute) *
691 if (!edev->d_attrs_muex) {
693 kfree(edev->attrs_muex);
697 for (index = 0; edev->mutually_exclusive[index]; index++) {
698 sprintf(buf, "0x%x", edev->mutually_exclusive[index]);
699 name = kzalloc(sizeof(char) * (strlen(buf) + 1),
702 for (index--; index >= 0; index--) {
703 kfree(edev->d_attrs_muex[index].attr.
706 kfree(edev->d_attrs_muex);
707 kfree(edev->attrs_muex);
712 sysfs_attr_init(&edev->d_attrs_muex[index].attr);
713 edev->d_attrs_muex[index].attr.name = name;
714 edev->d_attrs_muex[index].attr.mode = 0000;
715 edev->attrs_muex[index] = &edev->d_attrs_muex[index]
718 edev->attr_g_muex.name = muex_name;
719 edev->attr_g_muex.attrs = edev->attrs_muex;
723 if (edev->max_supported) {
724 edev->extcon_dev_type.groups =
725 kzalloc(sizeof(struct attribute_group *) *
726 (edev->max_supported + 2), GFP_KERNEL);
727 if (!edev->extcon_dev_type.groups) {
729 goto err_alloc_groups;
732 edev->extcon_dev_type.name = dev_name(edev->dev);
733 edev->extcon_dev_type.release = dummy_sysfs_dev_release;
735 for (index = 0; index < edev->max_supported; index++)
736 edev->extcon_dev_type.groups[index] =
737 &edev->cables[index].attr_g;
738 if (edev->mutually_exclusive)
739 edev->extcon_dev_type.groups[index] =
742 edev->dev->type = &edev->extcon_dev_type;
745 ret = device_register(edev->dev);
747 put_device(edev->dev);
750 #if defined(CONFIG_ANDROID)
752 ret = class_compat_create_link(switch_class, edev->dev,
754 #endif /* CONFIG_ANDROID */
756 spin_lock_init(&edev->lock);
758 RAW_INIT_NOTIFIER_HEAD(&edev->nh);
760 dev_set_drvdata(edev->dev, edev);
763 mutex_lock(&extcon_dev_list_lock);
764 list_add(&edev->entry, &extcon_dev_list);
765 mutex_unlock(&extcon_dev_list_lock);
770 if (edev->max_supported)
771 kfree(edev->extcon_dev_type.groups);
773 if (edev->max_supported && edev->mutually_exclusive) {
774 for (index = 0; edev->mutually_exclusive[index]; index++)
775 kfree(edev->d_attrs_muex[index].attr.name);
776 kfree(edev->d_attrs_muex);
777 kfree(edev->attrs_muex);
780 for (index = 0; index < edev->max_supported; index++)
781 kfree(edev->cables[index].attr_g.name);
783 if (edev->max_supported)
789 EXPORT_SYMBOL_GPL(extcon_dev_register);
792 * extcon_dev_unregister() - Unregister the extcon device.
793 * @edev: the extcon device instance to be unregistered.
795 * Note that this does not call kfree(edev) because edev was not allocated
798 void extcon_dev_unregister(struct extcon_dev *edev)
802 mutex_lock(&extcon_dev_list_lock);
803 list_del(&edev->entry);
804 mutex_unlock(&extcon_dev_list_lock);
806 if (IS_ERR_OR_NULL(get_device(edev->dev))) {
807 dev_err(edev->dev, "Failed to unregister extcon_dev (%s)\n",
808 dev_name(edev->dev));
812 if (edev->mutually_exclusive && edev->max_supported) {
813 for (index = 0; edev->mutually_exclusive[index];
815 kfree(edev->d_attrs_muex[index].attr.name);
816 kfree(edev->d_attrs_muex);
817 kfree(edev->attrs_muex);
820 for (index = 0; index < edev->max_supported; index++)
821 kfree(edev->cables[index].attr_g.name);
823 if (edev->max_supported) {
824 kfree(edev->extcon_dev_type.groups);
828 #if defined(CONFIG_ANDROID)
830 class_compat_remove_link(switch_class, edev->dev, NULL);
832 device_unregister(edev->dev);
833 put_device(edev->dev);
835 EXPORT_SYMBOL_GPL(extcon_dev_unregister);
837 static int __init extcon_class_init(void)
839 return create_extcon_class();
841 module_init(extcon_class_init);
843 static void __exit extcon_class_exit(void)
845 #if defined(CONFIG_ANDROID)
846 class_compat_unregister(switch_class);
848 class_destroy(extcon_class);
850 module_exit(extcon_class_exit);
852 MODULE_AUTHOR("Mike Lockwood <lockwood@android.com>");
853 MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>");
854 MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
855 MODULE_DESCRIPTION("External connector (extcon) class driver");
856 MODULE_LICENSE("GPL");