/* * drivers/extcon/extcon_class.c * * External connector (extcon) class driver * * Copyright (C) 2012 Samsung Electronics * Author: Donggeun Kim * Author: MyungJoo Ham * * based on android/drivers/switch/switch_class.c * Copyright (C) 2008 Google, Inc. * Author: Mike Lockwood * * This software is licensed under the terms of the GNU General Public * License version 2, as published by the Free Software Foundation, and * may be copied, distributed, and modified under those terms. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * */ #include #include #include #include #include #include #include #include #include #include /* * extcon_cable_name suggests the standard cable names for commonly used * cable types. * * However, please do not use extcon_cable_name directly for extcon_dev * struct's supported_cable pointer unless your device really supports * every single port-type of the following cable names. Please choose cable * names that are actually used in your extcon device. */ const char extcon_cable_name[][CABLE_NAME_MAX + 1] = { [EXTCON_USB] = "USB", [EXTCON_USB_HOST] = "USB-Host", [EXTCON_TA] = "TA", [EXTCON_FAST_CHARGER] = "Fast-charger", [EXTCON_SLOW_CHARGER] = "Slow-charger", [EXTCON_CHARGE_DOWNSTREAM] = "Charge-downstream", [EXTCON_HDMI] = "HDMI", [EXTCON_MHL] = "MHL", [EXTCON_DVI] = "DVI", [EXTCON_VGA] = "VGA", [EXTCON_DOCK] = "Dock", [EXTCON_LINE_IN] = "Line-in", [EXTCON_LINE_OUT] = "Line-out", [EXTCON_MIC_IN] = "Microphone", [EXTCON_HEADPHONE_OUT] = "Headphone", [EXTCON_SPDIF_IN] = "SPDIF-in", [EXTCON_SPDIF_OUT] = "SPDIF-out", [EXTCON_VIDEO_IN] = "Video-in", [EXTCON_VIDEO_OUT] = "Video-out", [EXTCON_MECHANICAL] = "Mechanical", }; static struct class *extcon_class; #if defined(CONFIG_ANDROID) static struct class_compat *switch_class; #endif /* CONFIG_ANDROID */ static LIST_HEAD(extcon_dev_list); static DEFINE_MUTEX(extcon_dev_list_lock); /** * check_mutually_exclusive - Check if new_state violates mutually_exclusive * condition. * @edev: the extcon device * @new_state: new cable attach status for @edev * * Returns 0 if nothing violates. Returns the index + 1 for the first * violated condition. */ static int check_mutually_exclusive(struct extcon_dev *edev, u32 new_state) { int i = 0; if (!edev->mutually_exclusive) return 0; for (i = 0; edev->mutually_exclusive[i]; i++) { int weight; u32 correspondants = new_state & edev->mutually_exclusive[i]; /* calculate the total number of bits set */ weight = hweight32(correspondants); if (weight > 1) return i + 1; } return 0; } static ssize_t state_show(struct device *dev, struct device_attribute *attr, char *buf) { int i, count = 0; struct extcon_dev *edev = dev_get_drvdata(dev); if (edev->print_state) { int ret = edev->print_state(edev, buf); if (ret >= 0) return ret; /* Use default if failed */ } if (edev->max_supported == 0) return sprintf(buf, "%u\n", edev->state); for (i = 0; i < SUPPORTED_CABLE_MAX; i++) { if (!edev->supported_cable[i]) break; count += sprintf(buf + count, "%s=%d\n", edev->supported_cable[i], !!(edev->state & (1 << i))); } return count; } static ssize_t state_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) { u32 state; ssize_t ret = 0; struct extcon_dev *edev = dev_get_drvdata(dev); ret = sscanf(buf, "0x%x", &state); if (ret == 0) ret = -EINVAL; else ret = extcon_set_state(edev, state); if (ret < 0) return ret; return count; } static DEVICE_ATTR_RW(state); static ssize_t name_show(struct device *dev, struct device_attribute *attr, char *buf) { struct extcon_dev *edev = dev_get_drvdata(dev); /* Optional callback given by the user */ if (edev->print_name) { int ret = edev->print_name(edev, buf); if (ret >= 0) return ret; } return sprintf(buf, "%s\n", dev_name(&edev->dev)); } static DEVICE_ATTR_RO(name); static ssize_t cable_name_show(struct device *dev, struct device_attribute *attr, char *buf) { struct extcon_cable *cable = container_of(attr, struct extcon_cable, attr_name); return sprintf(buf, "%s\n", cable->edev->supported_cable[cable->cable_index]); } static ssize_t cable_state_show(struct device *dev, struct device_attribute *attr, char *buf) { struct extcon_cable *cable = container_of(attr, struct extcon_cable, attr_state); return sprintf(buf, "%d\n", extcon_get_cable_state_(cable->edev, cable->cable_index)); } /** * extcon_update_state() - Update the cable attach states of the extcon device * only for the masked bits. * @edev: the extcon device * @mask: the bit mask to designate updated bits. * @state: new cable attach status for @edev * * Changing the state sends uevent with environment variable containing * the name of extcon device (envp[0]) and the state output (envp[1]). * Tizen uses this format for extcon device to get events from ports. * Android uses this format as well. * * Note that the notifier provides which bits are changed in the state * variable with the val parameter (second) to the callback. */ int extcon_update_state(struct extcon_dev *edev, u32 mask, u32 state) { char name_buf[120]; char state_buf[120]; char *prop_buf; char *envp[3]; int env_offset = 0; int length; unsigned long flags; spin_lock_irqsave(&edev->lock, flags); if (edev->state != ((edev->state & ~mask) | (state & mask))) { u32 old_state = edev->state; if (check_mutually_exclusive(edev, (edev->state & ~mask) | (state & mask))) { spin_unlock_irqrestore(&edev->lock, flags); return -EPERM; } edev->state &= ~mask; edev->state |= state & mask; raw_notifier_call_chain(&edev->nh, old_state, edev); /* This could be in interrupt handler */ prop_buf = (char *)get_zeroed_page(GFP_ATOMIC); if (prop_buf) { length = name_show(&edev->dev, NULL, prop_buf); if (length > 0) { if (prop_buf[length - 1] == '\n') prop_buf[length - 1] = 0; snprintf(name_buf, sizeof(name_buf), "NAME=%s", prop_buf); envp[env_offset++] = name_buf; } length = state_show(&edev->dev, NULL, prop_buf); if (length > 0) { if (prop_buf[length - 1] == '\n') prop_buf[length - 1] = 0; snprintf(state_buf, sizeof(state_buf), "STATE=%s", prop_buf); envp[env_offset++] = state_buf; } envp[env_offset] = NULL; /* Unlock early before uevent */ spin_unlock_irqr
CONFIG_SYSVIPC=y
CONFIG_NO_HZ=y
CONFIG_HIGH_RES_TIMERS=y
CONFIG_IKCONFIG=y
CONFIG_IKCONFIG_PROC=y
CONFIG_LOG_BUF_SHIFT=14
CONFIG_BLK_DEV_INITRD=y
CONFIG_MODULES=y
CONFIG_MODULE_UNLOAD=y
CONFIG_PARTITION_ADVANCED=y
CONFIG_ARCH_MULTI_V4T=y
CONFIG_ARCH_MULTI_V5=y
# CONFIG_ARCH_MULTI_V7 is not set
CONFIG_ARCH_INTEGRATOR=y
CONFIG_ARCH_INTEGRATOR_AP=y
CONFIG_ARCH_INTEGRATOR_CP=y
CONFIG_INTEGRATOR_IMPD1=y
CONFIG_CPU_ARM720T=y
CONFIG_CPU_ARM920T=y
CONFIG_CPU_ARM922T=y
CONFIG_CPU_ARM926T=y
CONFIG_CPU_ARM1020=y
CONFIG_CPU_ARM1022=y
CONFIG_CPU_ARM1026=y
CONFIG_PCI=y
CONFIG_PREEMPT=y
CONFIG_AEABI=y
# CONFIG_ATAGS is not set
CONFIG_ZBOOT_ROM_TEXT=0x0
CONFIG_ZBOOT_ROM_BSS=0x0
CONFIG_CMDLINE="console=ttyAM0,38400n8 root=/dev/nfs ip=bootp"
CONFIG_CPU_FREQ=y
CONFIG_CPU_FREQ_GOV_POWERSAVE=y
CONFIG_CPU_FREQ_GOV_USERSPACE=y
CONFIG_CPU_FREQ_GOV_ONDEMAND=y
CONFIG_FPE_NWFPE=y
CONFIG_NET=y
CONFIG_PACKET=y
CONFIG_UNIX=y
CONFIG_INET=y
CONFIG_IP_MULTICAST=y
CONFIG_IP_PNP=y
CONFIG_IP_PNP_DHCP=y
CONFIG_IP_PNP_BOOTP=y
# CONFIG_IPV6 is not set
CONFIG_MTD=y
CONFIG_MTD_CMDLINE_PARTS=y
CONFIG_MTD_AFS_PARTS=y
CONFIG_MTD_BLOCK=y
CONFIG_MTD_CFI=y
CONFIG_MTD_CFI_ADV_OPTIONS=y
CONFIG_MTD_CFI_INTELEXT=y
CONFIG_MTD_PHYSMAP=y
CONFIG_PROC_DEVICETREE=y
CONFIG_BLK_DEV_LOOP=y
CONFIG_BLK_DEV_RAM=y
CONFIG_BLK_DEV_RAM_SIZE=8192
CONFIG_NETDEVICES=y
CONFIG_E100=y
CONFIG_SMC91X=y
# CONFIG_KEYBOARD_ATKBD is not set
# CONFIG_SERIO_SERPORT is not set
CONFIG_FB=y
CONFIG_FB_MODE_HELPERS=y
CONFIG_FB_ARMCLCD=y
CONFIG_FB_MATROX=y
CONFIG_FB_MATROX_MILLENIUM=y
CONFIG_FB_MATROX_MYSTIQUE=y
# CONFIG_VGA_CONSOLE is not set
CONFIG_MMC=y
CONFIG_MMC_ARMMMCI=y
CONFIG_NEW_LEDS=y
CONFIG_LEDS_CLASS=y
CONFIG_LEDS_TRIGGERS=y
CONFIG_LEDS_TRIGGER_HEARTBEAT=y
CONFIG_LEDS_TRIGGER_CPU=y
CONFIG_RTC_CLASS=y
CONFIG_RTC_DRV_PL030=y
CONFIG_EXT2_FS=y
CONFIG_VFAT_FS=y
CONFIG_TMPFS=y
CONFIG_JFFS2_FS=y
CONFIG_CRAMFS=y
CONFIG_NFS_FS=y
CONFIG_ROOT_NFS=y
CONFIG_NFSD=y
CONFIG_NFSD_V3=y
CONFIG_NLS_CODEPAGE_437=y
CONFIG_NLS_ISO8859_1=y
CONFIG_MAGIC_SYSRQ=y
CONFIG_DEBUG_KERNEL=y
xtcon device (should be allocated before calling) * * Among the members of edev struct, please set the "user initializing data" * in any case and set the "optional callbacks" if required. However, please * do not set the values of "internal data", which are initialized by * this function. */ int extcon_dev_register(struct extcon_dev *edev) { int ret, index = 0; if (!extcon_class) { ret = create_extcon_class(); if (ret < 0) return ret; } if (edev->supported_cable) { /* Get size of array */ for (index = 0; edev->supported_cable[index]; index++) ; edev->max_supported = index; } else { edev->max_supported = 0; } if (index > SUPPORTED_CABLE_MAX) { dev_err(&edev->dev, "extcon: maximum number of supported cables exceeded.\n"); return -EINVAL; } edev->dev.class = extcon_class; edev->dev.release = extcon_dev_release; edev->name = edev->name ? edev->name : dev_name(edev->dev.parent); if (IS_ERR_OR_NULL(edev->name)) { dev_err(&edev->dev, "extcon device name is null\n"); return -EINVAL; } dev_set_name(&edev->dev, "%s", edev->name); if (edev->max_supported) { char buf[10]; char *str; struct extcon_cable *cable; edev->cables = kzalloc(sizeof(struct extcon_cable) * edev->max_supported, GFP_KERNEL); if (!edev->cables) { ret = -ENOMEM; goto err_sysfs_alloc; } for (index = 0; index < edev->max_supported; index++) { cable = &edev->cables[index]; snprintf(buf, 10, "cable.%d", index); str = kzalloc(sizeof(char) * (strlen(buf) + 1), GFP_KERNEL); if (!str) { for (index--; index >= 0; index--) { cable = &edev->cables[index]; kfree(cable->attr_g.name); } ret = -ENOMEM; goto err_alloc_cables; } strcpy(str, buf); cable->edev = edev; cable->cable_index = index; cable->attrs[0] = &cable->attr_name.attr; cable->attrs[1] = &cable->attr_state.attr; cable->attrs[2] = NULL; cable->attr_g.name = str; cable->attr_g.attrs = cable->attrs; sysfs_attr_init(&cable->attr_name.attr); cable->attr_name.attr.name = "name"; cable->attr_name.attr.mode = 0444; cable->attr_name.show = cable_name_show; sysfs_attr_init(&cable->attr_state.attr); cable->attr_state.attr.name = "state"; cable->attr_state.attr.mode = 0444; cable->attr_state.show = cable_state_show; } } if (edev->max_supported && edev->mutually_exclusive) { char buf[80]; char *name; /* Count the size of mutually_exclusive array */ for (index = 0; edev->mutually_exclusive[index]; index++) ; edev->attrs_muex = kzalloc(sizeof(struct attribute *) * (index + 1), GFP_KERNEL); if (!edev->attrs_muex) { ret = -ENOMEM; goto err_muex; } edev->d_attrs_muex = kzalloc(sizeof(struct device_attribute) * index, GFP_KERNEL); if (!edev->d_attrs_muex) { ret = -ENOMEM; kfree(edev->attrs_muex); goto err_muex; } for (index = 0; edev->mutually_exclusive[index]; index++) { sprintf(buf, "0x%x", edev->mutually_exclusive[index]); name = kzalloc(sizeof(char) * (strlen(buf) + 1), GFP_KERNEL); if (!name) { for (index--; index >= 0; index--) { kfree(edev->d_attrs_muex[index].attr. name); } kfree(edev->d_attrs_muex); kfree(edev->attrs_muex); ret = -ENOMEM; goto err_muex; } strcpy(name, buf); sysfs_attr_init(&edev->d_attrs_muex[index].attr); edev->d_attrs_muex[index].attr.name = name; edev->d_attrs_muex[index].attr.mode = 0000; edev->attrs_muex[index] = &edev->d_attrs_muex[index] .attr; } edev->attr_g_muex.name = muex_name; edev->attr_g_muex.attrs = edev->attrs_muex; } if (edev->max_supported) { edev->extcon_dev_type.groups = kzalloc(sizeof(struct attribute_group *) * (edev->max_supported + 2), GFP_KERNEL); if (!edev->extcon_dev_type.groups) { ret = -ENOMEM; goto err_alloc_groups; } edev->extcon_dev_type.name = dev_name(&edev->dev); edev->extcon_dev_type.release = dummy_sysfs_dev_release; for (index = 0; index < edev->max_supported; index++) edev->extcon_dev_type.groups[index] = &edev->cables[index].attr_g; if (edev->mutually_exclusive) edev->extcon_dev_type.groups[index] = &edev->attr_g_muex; edev->dev.type = &edev->extcon_dev_type; } ret = device_register(&edev->dev); if (ret) { put_device(&edev->dev); goto err_dev; } #if defined(CONFIG_ANDROID) if (switch_class) ret = class_compat_create_link(switch_class, &edev->dev, NULL); #endif /* CONFIG_ANDROID */ spin_lock_init(&edev->lock); RAW_INIT_NOTIFIER_HEAD(&edev->nh); dev_set_drvdata(&edev->dev, edev); edev->state = 0; mutex_lock(&extcon_dev_list_lock); list_add(&edev->entry, &extcon_dev_list); mutex_unlock(&extcon_dev_list_lock); return 0; err_dev: if (edev->max_supported) kfree(edev->extcon_dev_type.groups); err_alloc_groups: if (edev->max_supported && edev->mutually_exclusive) { for (index = 0; edev->mutually_exclusive[index]; index++) kfree(edev->d_attrs_muex[index].attr.name); kfree(edev->d_attrs_muex); kfree(edev->attrs_muex); } err_muex: for (index = 0; index < edev->max_supported; index++) kfree(edev->cables[index].attr_g.name); err_alloc_cables: if (edev->max_supported) kfree(edev->cables); err_sysfs_alloc: return ret; } EXPORT_SYMBOL_GPL(extcon_dev_register); /** * extcon_dev_unregister() - Unregister the extcon device. * @edev: the extcon device instance to be unregistered. * * Note that this does not call kfree(edev) because edev was not allocated * by this class. */ void extcon_dev_unregister(struct extcon_dev *edev) { int index; mutex_lock(&extcon_dev_list_lock); list_del(&edev->entry); mutex_unlock(&extcon_dev_list_lock); if (IS_ERR_OR_NULL(get_device(&edev->dev))) { dev_err(&edev->dev, "Failed to unregister extcon_dev (%s)\n", dev_name(&edev->dev)); return; } device_unregister(&edev->dev); if (edev->mutually_exclusive && edev->max_supported) { for (index = 0; edev->mutually_exclusive[index]; index++) kfree(edev->d_attrs_muex[index].attr.name); kfree(edev->d_attrs_muex); kfree(edev->attrs_muex); } for (index = 0; index < edev->max_supported; index++) kfree(edev->cables[index].attr_g.name); if (edev->max_supported) { kfree(edev->extcon_dev_type.groups); kfree(edev->cables); } #if defined(CONFIG_ANDROID) if (switch_class) class_compat_remove_link(switch_class, &edev->dev, NULL); #endif put_device(&edev->dev); } EXPORT_SYMBOL_GPL(extcon_dev_unregister); static void devm_extcon_dev_unreg(struct device *dev, void *res) { extcon_dev_unregister(*(struct extcon_dev **)res); } /** * devm_extcon_dev_register() - Resource-managed extcon_dev_register() * @dev: device to allocate extcon device * @edev: the new extcon device to register * * Managed extcon_dev_register() function. If extcon device is attached with * this function, that extcon device is automatically unregistered on driver * detach. Internally this function calls extcon_dev_register() function. * To get more information, refer that function. * * If extcon device is registered with this function and the device needs to be * unregistered separately, devm_extcon_dev_unregister() should be used. * * Returns 0 if success or negaive error number if failure. */ int devm_extcon_dev_register(struct device *dev, struct extcon_dev *edev) { struct extcon_dev **ptr; int ret; ptr = devres_alloc(devm_extcon_dev_unreg, sizeof(*ptr), GFP_KERNEL); if (!ptr) return -ENOMEM; ret = extcon_dev_register(edev); if (ret) { devres_free(ptr); return ret; } *ptr = edev; devres_add(dev, ptr); return 0; } EXPORT_SYMBOL_GPL(devm_extcon_dev_register); /** * devm_extcon_dev_unregister() - Resource-managed extcon_dev_unregister() * @dev: device the extcon belongs to * @edev: the extcon device to unregister * * Unregister extcon device that is registered with devm_extcon_dev_register() * function. */ void devm_extcon_dev_unregister(struct device *dev, struct extcon_dev *edev) { WARN_ON(devres_release(dev, devm_extcon_dev_unreg, devm_extcon_dev_match, edev)); } EXPORT_SYMBOL_GPL(devm_extcon_dev_unregister); #ifdef CONFIG_OF /* * extcon_get_edev_by_phandle - Get the extcon device from devicetree * @dev - instance to the given device * @index - index into list of extcon_dev * * return the instance of extcon device */ struct extcon_dev *extcon_get_edev_by_phandle(struct device *dev, int index) { struct device_node *node; struct extcon_dev *edev; if (!dev->of_node) { dev_err(dev, "device does not have a device node entry\n"); return ERR_PTR(-EINVAL); } node = of_parse_phandle(dev->of_node, "extcon", index); if (!node) { dev_err(dev, "failed to get phandle in %s node\n", dev->of_node->full_name); return ERR_PTR(-ENODEV); } mutex_lock(&extcon_dev_list_lock); list_for_each_entry(edev, &extcon_dev_list, entry) { if (edev->dev.parent && edev->dev.parent->of_node == node) { mutex_unlock(&extcon_dev_list_lock); return edev; } } mutex_unlock(&extcon_dev_list_lock); return ERR_PTR(-EPROBE_DEFER); } #else struct extcon_dev *extcon_get_edev_by_phandle(struct device *dev, int index) { return ERR_PTR(-ENOSYS); } #endif /* CONFIG_OF */ EXPORT_SYMBOL_GPL(extcon_get_edev_by_phandle); static int __init extcon_class_init(void) { return create_extcon_class(); } module_init(extcon_class_init); static void __exit extcon_class_exit(void) { #if defined(CONFIG_ANDROID) class_compat_unregister(switch_class); #endif class_destroy(extcon_class); } module_exit(extcon_class_exit); MODULE_AUTHOR("Mike Lockwood "); MODULE_AUTHOR("Donggeun Kim "); MODULE_AUTHOR("MyungJoo Ham "); MODULE_DESCRIPTION("External connector (extcon) class driver"); MODULE_LICENSE("GPL");