/** * struct extcon_cable - An internal data for an external connector. * @edev: the extcon device * @cable_index: the index of this cable in the edev * @attr_g: the attribute group for the cable * @attr_name: "name" sysfs entry * @attr_state: "state" sysfs entry * @attrs: the array pointing to attr_name and attr_state for attr_g * @usb_propval: the array of USB connector properties * @chg_propval: the array of charger connector properties * @jack_propval: the array of jack connector properties * @disp_propval: the array of display connector properties * @usb_bits: the bit array of the USB connector property capabilities * @chg_bits: the bit array of the charger connector property capabilities * @jack_bits: the bit array of the jack connector property capabilities * @disp_bits: the bit array of the display connector property capabilities
*/ struct extcon_cable { struct extcon_dev *edev; int cable_index;
struct attribute *attrs[3]; /* to be fed to attr_g.attrs */
union extcon_property_value usb_propval[EXTCON_PROP_USB_CNT]; union extcon_property_value chg_propval[EXTCON_PROP_CHG_CNT]; union extcon_property_value jack_propval[EXTCON_PROP_JACK_CNT]; union extcon_property_value disp_propval[EXTCON_PROP_DISP_CNT];
staticint check_mutually_exclusive(struct extcon_dev *edev, u32 new_state)
{ int i;
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;
}
staticint find_cable_index_by_id(struct extcon_dev *edev, constunsignedint id)
{ int i;
/* Find the index of extcon cable in edev->supported_cable */ for (i = 0; i < edev->max_supported; i++) { if (edev->supported_cable[i] == id) return i;
}
return -EINVAL;
}
staticint get_extcon_type(unsignedint prop)
{ switch (prop) { case EXTCON_PROP_USB_MIN ... EXTCON_PROP_USB_MAX: return EXTCON_TYPE_USB; case EXTCON_PROP_CHG_MIN ... EXTCON_PROP_CHG_MAX: return EXTCON_TYPE_CHG; case EXTCON_PROP_JACK_MIN ... EXTCON_PROP_JACK_MAX: return EXTCON_TYPE_JACK; case EXTCON_PROP_DISP_MIN ... EXTCON_PROP_DISP_MAX: return EXTCON_TYPE_DISP; default: return -EINVAL;
}
}
staticbool is_extcon_changed(struct extcon_dev *edev, int index, bool new_state)
{ int state = !!(edev->state & BIT(index)); return (state != new_state);
}
staticbool is_extcon_property_supported(unsignedint id, unsignedint prop)
{ int type;
/* Check whether the property is supported or not. */
type = get_extcon_type(prop); if (type < 0) returnfalse;
/* Check whether a specific extcon id supports the property or not. */ return !!(extcon_info[id].type & type);
}
staticint is_extcon_property_capability(struct extcon_dev *edev, unsignedint id, int index,unsignedint prop)
{ struct extcon_cable *cable; int type, ret;
/* Check whether the property is supported or not. */
type = get_extcon_type(prop); if (type < 0) return type;
cable = &edev->cables[index];
switch (type) { case EXTCON_TYPE_USB:
ret = test_bit(prop - EXTCON_PROP_USB_MIN, cable->usb_bits); break; case EXTCON_TYPE_CHG:
ret = test_bit(prop - EXTCON_PROP_CHG_MIN, cable->chg_bits); break; case EXTCON_TYPE_JACK:
ret = test_bit(prop - EXTCON_PROP_JACK_MIN, cable->jack_bits); break; case EXTCON_TYPE_DISP:
ret = test_bit(prop - EXTCON_PROP_DISP_MIN, cable->disp_bits); break; default:
ret = -EINVAL;
}
return ret;
}
staticvoid init_property(struct extcon_dev *edev, unsignedint id, int index)
{ unsignedint type = extcon_info[id].type; struct extcon_cable *cable = &edev->cables[index];
if (EXTCON_TYPE_USB & type)
memset(cable->usb_propval, 0, sizeof(cable->usb_propval)); if (EXTCON_TYPE_CHG & type)
memset(cable->chg_propval, 0, sizeof(cable->chg_propval)); if (EXTCON_TYPE_JACK & type)
memset(cable->jack_propval, 0, sizeof(cable->jack_propval)); if (EXTCON_TYPE_DISP & type)
memset(cable->disp_propval, 0, sizeof(cable->disp_propval));
}
/** * extcon_sync() - Synchronize the state for an external connector. * @edev: the extcon device * @id: the unique id indicating an external connector * * Note that this function send a notification in order to synchronize * the state and property of an external connector. * * Returns 0 if success or error number if fail.
*/ int extcon_sync(struct extcon_dev *edev, unsignedint id)
{ char name_buf[120]; char state_buf[120]; char *prop_buf; char *envp[3]; int env_offset = 0; int length; int index; int state; unsignedlong flags;
if (!edev) return -EINVAL;
index = find_cable_index_by_id(edev, id); if (index < 0) return index;
spin_lock_irqsave(&edev->lock, flags);
state = !!(edev->state & BIT(index));
spin_unlock_irqrestore(&edev->lock, flags);
/* * Call functions in a raw notifier chain for the specific one * external connector.
*/
raw_notifier_call_chain(&edev->nh[index], state, edev);
/* * Call functions in a raw notifier chain for the all supported * external connectors.
*/
raw_notifier_call_chain(&edev->nh_all, state, edev);
spin_lock_irqsave(&edev->lock, flags); /* This could be in interrupt handler */
prop_buf = (char *)get_zeroed_page(GFP_ATOMIC); if (!prop_buf) { /* Unlock early before uevent */
spin_unlock_irqrestore(&edev->lock, flags);
dev_err(&edev->dev, "out of memory in extcon_set_state\n");
kobject_uevent(&edev->dev.kobj, KOBJ_CHANGE);
/* Unlock early before uevent */
spin_unlock_irqrestore(&edev->lock, flags);
kobject_uevent_env(&edev->dev.kobj, KOBJ_CHANGE, envp);
free_page((unsignedlong)prop_buf);
return 0;
}
EXPORT_SYMBOL_GPL(extcon_sync);
/** * extcon_get_state() - Get the state of an external connector. * @edev: the extcon device * @id: the unique id indicating an external connector * * Returns 0 if success or error number if fail.
*/ int extcon_get_state(struct extcon_dev *edev, constunsignedint id)
{ int index, state; unsignedlong flags;
if (!edev) return -EINVAL;
index = find_cable_index_by_id(edev, id); if (index < 0) return index;
spin_lock_irqsave(&edev->lock, flags);
state = is_extcon_attached(edev, index);
spin_unlock_irqrestore(&edev->lock, flags);
/** * extcon_set_state() - Set the state of an external connector. * @edev: the extcon device * @id: the unique id indicating an external connector * @state: the new state of an external connector. * the default semantics is true: attached / false: detached. * * Note that this function set the state of an external connector without * a notification. To synchronize the state of an external connector, * have to use extcon_set_state_sync() and extcon_sync(). * * Returns 0 if success or error number if fail.
*/ int extcon_set_state(struct extcon_dev *edev, unsignedint id, bool state)
{ unsignedlong flags; int index, ret = 0;
if (!edev) return -EINVAL;
index = find_cable_index_by_id(edev, id); if (index < 0) return index;
spin_lock_irqsave(&edev->lock, flags);
/* Check whether the external connector's state is changed. */ if (!is_extcon_changed(edev, index, state)) goto out;
if (check_mutually_exclusive(edev,
(edev->state & ~BIT(index)) | (state & BIT(index)))) {
ret = -EPERM; goto out;
}
/* * Initialize the value of extcon property before setting * the detached state for an external connector.
*/ if (!state)
init_property(edev, id, index);
/* Update the state for an external connector. */ if (state)
edev->state |= BIT(index); else
edev->state &= ~(BIT(index));
out:
spin_unlock_irqrestore(&edev->lock, flags);
/** * extcon_set_state_sync() - Set the state of an external connector with sync. * @edev: the extcon device * @id: the unique id indicating an external connector * @state: the new state of external connector. * the default semantics is true: attached / false: detached. * * Note that this function set the state of external connector * and synchronize the state by sending a notification. * * Returns 0 if success or error number if fail.
*/ int extcon_set_state_sync(struct extcon_dev *edev, unsignedint id, bool state)
{ int ret;
ret = extcon_set_state(edev, id, state); if (ret < 0) return ret;
/** * extcon_get_property() - Get the property value of an external connector. * @edev: the extcon device * @id: the unique id indicating an external connector * @prop: the property id indicating an extcon property * @prop_val: the pointer which store the value of extcon property * * Note that when getting the property value of external connector, * the external connector should be attached. If detached state, function * return 0 without property value. Also, the each property should be * included in the list of supported properties according to extcon type. * * Returns 0 if success or error number if fail.
*/ int extcon_get_property(struct extcon_dev *edev, unsignedint id, unsignedint prop, union extcon_property_value *prop_val)
{ struct extcon_cable *cable; unsignedlong flags; int index, ret = 0;
*prop_val = (union extcon_property_value){0};
if (!edev) return -EINVAL;
/* Check whether the property is supported or not */ if (!is_extcon_property_supported(id, prop)) return -EINVAL;
/* Find the cable index of external connector by using id */
index = find_cable_index_by_id(edev, id); if (index < 0) return index;
spin_lock_irqsave(&edev->lock, flags);
/* Check whether the property is available or not. */ if (!is_extcon_property_capability(edev, id, index, prop)) {
spin_unlock_irqrestore(&edev->lock, flags); return -EPERM;
}
/* * Check whether the external connector is attached. * If external connector is detached, the user can not * get the property value.
*/ if (!is_extcon_attached(edev, index)) {
spin_unlock_irqrestore(&edev->lock, flags); return 0;
}
cable = &edev->cables[index];
/* Get the property value according to extcon type */ switch (prop) { case EXTCON_PROP_USB_MIN ... EXTCON_PROP_USB_MAX:
*prop_val = cable->usb_propval[prop - EXTCON_PROP_USB_MIN]; break; case EXTCON_PROP_CHG_MIN ... EXTCON_PROP_CHG_MAX:
*prop_val = cable->chg_propval[prop - EXTCON_PROP_CHG_MIN]; break; case EXTCON_PROP_JACK_MIN ... EXTCON_PROP_JACK_MAX:
*prop_val = cable->jack_propval[prop - EXTCON_PROP_JACK_MIN]; break; case EXTCON_PROP_DISP_MIN ... EXTCON_PROP_DISP_MAX:
*prop_val = cable->disp_propval[prop - EXTCON_PROP_DISP_MIN]; break; default:
ret = -EINVAL; break;
}
/** * extcon_set_property() - Set the property value of an external connector. * @edev: the extcon device * @id: the unique id indicating an external connector * @prop: the property id indicating an extcon property * @prop_val: the pointer including the new value of extcon property * * Note that each property should be included in the list of supported * properties according to the extcon type. * * Returns 0 if success or error number if fail.
*/ int extcon_set_property(struct extcon_dev *edev, unsignedint id, unsignedint prop, union extcon_property_value prop_val)
{ struct extcon_cable *cable; unsignedlong flags; int index, ret = 0;
if (!edev) return -EINVAL;
/* Check whether the property is supported or not */ if (!is_extcon_property_supported(id, prop)) return -EINVAL;
/* Find the cable index of external connector by using id */
index = find_cable_index_by_id(edev, id); if (index < 0) return index;
spin_lock_irqsave(&edev->lock, flags);
/* Check whether the property is available or not. */ if (!is_extcon_property_capability(edev, id, index, prop)) {
spin_unlock_irqrestore(&edev->lock, flags); return -EPERM;
}
cable = &edev->cables[index];
/* Set the property value according to extcon type */ switch (prop) { case EXTCON_PROP_USB_MIN ... EXTCON_PROP_USB_MAX:
cable->usb_propval[prop - EXTCON_PROP_USB_MIN] = prop_val; break; case EXTCON_PROP_CHG_MIN ... EXTCON_PROP_CHG_MAX:
cable->chg_propval[prop - EXTCON_PROP_CHG_MIN] = prop_val; break; case EXTCON_PROP_JACK_MIN ... EXTCON_PROP_JACK_MAX:
cable->jack_propval[prop - EXTCON_PROP_JACK_MIN] = prop_val; break; case EXTCON_PROP_DISP_MIN ... EXTCON_PROP_DISP_MAX:
cable->disp_propval[prop - EXTCON_PROP_DISP_MIN] = prop_val; break; default:
ret = -EINVAL; break;
}
/** * extcon_set_property_sync() - Set property of an external connector with sync. * @edev: the extcon device * @id: the unique id indicating an external connector * @prop: the property id indicating an extcon property * @prop_val: the pointer including the new value of extcon property * * Note that when setting the property value of external connector, * the external connector should be attached. The each property should * be included in the list of supported properties according to extcon type. * * Returns 0 if success or error number if fail.
*/ int extcon_set_property_sync(struct extcon_dev *edev, unsignedint id, unsignedint prop, union extcon_property_value prop_val)
{ int ret;
ret = extcon_set_property(edev, id, prop, prop_val); if (ret < 0) return ret;
/** * extcon_get_property_capability() - Get the capability of the property * for an external connector. * @edev: the extcon device * @id: the unique id indicating an external connector * @prop: the property id indicating an extcon property * * Returns 1 if the property is available or 0 if not available.
*/ int extcon_get_property_capability(struct extcon_dev *edev, unsignedint id, unsignedint prop)
{ int index;
if (!edev) return -EINVAL;
/* Check whether the property is supported or not */ if (!is_extcon_property_supported(id, prop)) return -EINVAL;
/* Find the cable index of external connector by using id */
index = find_cable_index_by_id(edev, id); if (index < 0) return index;
/** * extcon_set_property_capability() - Set the capability of the property * for an external connector. * @edev: the extcon device * @id: the unique id indicating an external connector * @prop: the property id indicating an extcon property * * Note that this function set the capability of the property * for an external connector in order to mark the bit in capability * bitmap which mean the available state of the property. * * Returns 0 if success or error number if fail.
*/ int extcon_set_property_capability(struct extcon_dev *edev, unsignedint id, unsignedint prop)
{ struct extcon_cable *cable; int index, type, ret = 0;
if (!edev) return -EINVAL;
/* Check whether the property is supported or not. */ if (!is_extcon_property_supported(id, prop)) return -EINVAL;
/* Find the cable index of external connector by using id. */
index = find_cable_index_by_id(edev, id); if (index < 0) return index;
type = get_extcon_type(prop); if (type < 0) return type;
cable = &edev->cables[index];
switch (type) { case EXTCON_TYPE_USB:
__set_bit(prop - EXTCON_PROP_USB_MIN, cable->usb_bits); break; case EXTCON_TYPE_CHG:
__set_bit(prop - EXTCON_PROP_CHG_MIN, cable->chg_bits); break; case EXTCON_TYPE_JACK:
__set_bit(prop - EXTCON_PROP_JACK_MIN, cable->jack_bits); break; case EXTCON_TYPE_DISP:
__set_bit(prop - EXTCON_PROP_DISP_MIN, cable->disp_bits); break; default:
ret = -EINVAL;
}
/** * extcon_get_extcon_dev() - Get the extcon device instance from the name. * @extcon_name: the extcon name provided with extcon_dev_register() * * Return the pointer of extcon device if success or ERR_PTR(err) if fail. * NOTE: This function returns -EPROBE_DEFER so it may only be called from * probe() functions.
*/ struct extcon_dev *extcon_get_extcon_dev(constchar *extcon_name)
{ struct extcon_dev *sd;
/** * extcon_register_notifier() - Register a notifier block to get notified by * any state changes from the extcon. * @edev: the extcon device * @id: the unique id indicating an external connector * @nb: a notifier block to be registered * * Note that the second parameter given to the callback of nb (val) is * the current state of an external connector and the third pameter * is the pointer of extcon device. * * Returns 0 if success or error number if fail.
*/ int extcon_register_notifier(struct extcon_dev *edev, unsignedint id, struct notifier_block *nb)
{ unsignedlong flags; int ret, idx;
if (!edev || !nb) return -EINVAL;
idx = find_cable_index_by_id(edev, id); if (idx < 0) return idx;
spin_lock_irqsave(&edev->lock, flags);
ret = raw_notifier_chain_register(&edev->nh[idx], nb);
spin_unlock_irqrestore(&edev->lock, flags);
/** * extcon_unregister_notifier() - Unregister a notifier block from the extcon. * @edev: the extcon device * @id: the unique id indicating an external connector * @nb: a notifier block to be registered * * Returns 0 if success or error number if fail.
*/ int extcon_unregister_notifier(struct extcon_dev *edev, unsignedint id, struct notifier_block *nb)
{ unsignedlong flags; int ret, idx;
if (!edev || !nb) return -EINVAL;
idx = find_cable_index_by_id(edev, id); if (idx < 0) return idx;
spin_lock_irqsave(&edev->lock, flags);
ret = raw_notifier_chain_unregister(&edev->nh[idx], nb);
spin_unlock_irqrestore(&edev->lock, flags);
/** * extcon_register_notifier_all() - Register a notifier block for all connectors. * @edev: the extcon device * @nb: a notifier block to be registered * * Note that this function registers a notifier block in order to receive * the state change of all supported external connectors from extcon device. * And the second parameter given to the callback of nb (val) is * the current state and the third pameter is the pointer of extcon device. * * Returns 0 if success or error number if fail.
*/ int extcon_register_notifier_all(struct extcon_dev *edev, struct notifier_block *nb)
{ unsignedlong flags; int ret;
if (!edev || !nb) return -EINVAL;
spin_lock_irqsave(&edev->lock, flags);
ret = raw_notifier_chain_register(&edev->nh_all, nb);
spin_unlock_irqrestore(&edev->lock, flags);
/** * extcon_unregister_notifier_all() - Unregister a notifier block from extcon. * @edev: the extcon device * @nb: a notifier block to be registered * * Returns 0 if success or error number if fail.
*/ int extcon_unregister_notifier_all(struct extcon_dev *edev, struct notifier_block *nb)
{ unsignedlong flags; int ret;
if (!edev || !nb) return -EINVAL;
spin_lock_irqsave(&edev->lock, flags);
ret = raw_notifier_chain_unregister(&edev->nh_all, nb);
spin_unlock_irqrestore(&edev->lock, flags);
/* * extcon_dev_allocate() - Allocate the memory of extcon device. * @supported_cable: the array of the supported external connectors * ending with EXTCON_NONE. * * Note that this function allocates the memory for extcon device * and initialize default setting for the extcon device. * * Returns the pointer memory of allocated extcon_dev if success * or ERR_PTR(err) if fail.
*/ struct extcon_dev *extcon_dev_allocate(constunsignedint *supported_cable)
{ struct extcon_dev *edev;
if (!supported_cable) return ERR_PTR(-EINVAL);
edev = kzalloc(sizeof(*edev), GFP_KERNEL); if (!edev) return ERR_PTR(-ENOMEM);
/* * extcon_dev_free() - Free the memory of extcon device. * @edev: the extcon device
*/ void extcon_dev_free(struct extcon_dev *edev)
{
kfree(edev);
}
EXPORT_SYMBOL_GPL(extcon_dev_free);
/** * extcon_alloc_cables() - alloc the cables for extcon device * @edev: extcon device which has cables * * Returns 0 if success or error number if fail.
*/ staticint extcon_alloc_cables(struct extcon_dev *edev)
{ int index; char *str; struct extcon_cable *cable;
if (!edev) return -EINVAL;
if (!edev->max_supported) return 0;
edev->cables = kcalloc(edev->max_supported, sizeof(*edev->cables),
GFP_KERNEL); if (!edev->cables) return -ENOMEM;
for (index = 0; index < edev->max_supported; index++) {
cable = &edev->cables[index];
str = kasprintf(GFP_KERNEL, "cable.%d", index); if (!str) { for (index--; index >= 0; index--) {
cable = &edev->cables[index];
kfree(cable->attr_g.name);
}
for (index = 0; edev->mutually_exclusive[index]; index++) {
name = kasprintf(GFP_KERNEL, "0x%x",
edev->mutually_exclusive[index]); if (!name) { for (index--; index >= 0; index--)
kfree(edev->d_attrs_muex[index].attr.name);
/** * extcon_alloc_groups() - alloc the groups for extcon device * @edev: extcon device * * Returns 0 if success or error number if fail.
*/ staticint extcon_alloc_groups(struct extcon_dev *edev)
{ int index;
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;
return 0;
}
/** * extcon_dev_register() - Register an new extcon device * @edev: the extcon device to be registered * * Among the members of edev struct, please set the "user initializing data" * do not set the values of "internal data", which are initialized by * this function. * * Note that before calling this funciton, have to allocate the memory * of an extcon device by using the extcon_dev_allocate(). And the extcon * dev should include the supported_cable information. * * Returns 0 if success or error number if fail.
*/ int extcon_dev_register(struct extcon_dev *edev)
{ int ret, index;
ret = create_extcon_class(); if (ret < 0) return ret;
if (!edev || !edev->supported_cable) return -EINVAL;
for (index = 0; edev->supported_cable[index] != EXTCON_NONE; index++);
edev->max_supported = index; if (index > SUPPORTED_CABLE_MAX) {
dev_err(&edev->dev, "exceed the maximum number of supported cables\n"); return -EINVAL;
}
/** * extcon_dev_unregister() - Unregister the extcon device. * @edev: the extcon device 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;
/* * extcon_get_edev_by_phandle - Get the extcon device from devicetree. * @dev : the instance to the given device * @index : the index into list of extcon_dev * * Return the pointer of extcon device if success or ERR_PTR(err) if fail.
*/ struct extcon_dev *extcon_get_edev_by_phandle(struct device *dev, int index)
{ struct device_node *node, *np = dev_of_node(dev); struct extcon_dev *edev;
if (!np) {
dev_dbg(dev, "device does not have a device node entry\n"); return ERR_PTR(-EINVAL);
}
node = of_parse_phandle(np, "extcon", index); if (!node) {
dev_dbg(dev, "failed to get phandle in %pOF node\n", np); return ERR_PTR(-ENODEV);
}
/** * extcon_get_edev_name() - Get the name of the extcon device. * @edev: the extcon device
*/ constchar *extcon_get_edev_name(struct extcon_dev *edev)
{ return !edev ? NULL : edev->name;
}
EXPORT_SYMBOL_GPL(extcon_get_edev_name);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.