linux/drivers/extcon/extcon_class.c
<<
>>
Prefs
   1/*
   2 *  drivers/extcon/extcon_class.c
   3 *
   4 *  External connector (extcon) class driver
   5 *
   6 * Copyright (C) 2012 Samsung Electronics
   7 * Author: Donggeun Kim <dg77.kim@samsung.com>
   8 * Author: MyungJoo Ham <myungjoo.ham@samsung.com>
   9 *
  10 * based on android/drivers/switch/switch_class.c
  11 * Copyright (C) 2008 Google, Inc.
  12 * Author: Mike Lockwood <lockwood@android.com>
  13 *
  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.
  17 *
  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.
  22 *
  23*/
  24
  25#include <linux/module.h>
  26#include <linux/types.h>
  27#include <linux/init.h>
  28#include <linux/device.h>
  29#include <linux/fs.h>
  30#include <linux/err.h>
  31#include <linux/extcon.h>
  32#include <linux/slab.h>
  33
  34/*
  35 * extcon_cable_name suggests the standard cable names for commonly used
  36 * cable types.
  37 *
  38 * However, please do not use extcon_cable_name directly for extcon_dev
  39 * struct's supported_cable pointer unless your device really supports
  40 * every single port-type of the following cable names. Please choose cable
  41 * names that are actually used in your extcon device.
  42 */
  43const char *extcon_cable_name[] = {
  44        [EXTCON_USB]            = "USB",
  45        [EXTCON_USB_HOST]       = "USB-Host",
  46        [EXTCON_TA]             = "TA",
  47        [EXTCON_FAST_CHARGER]   = "Fast-charger",
  48        [EXTCON_SLOW_CHARGER]   = "Slow-charger",
  49        [EXTCON_CHARGE_DOWNSTREAM]      = "Charge-downstream",
  50        [EXTCON_HDMI]           = "HDMI",
  51        [EXTCON_MHL]            = "MHL",
  52        [EXTCON_DVI]            = "DVI",
  53        [EXTCON_VGA]            = "VGA",
  54        [EXTCON_DOCK]           = "Dock",
  55        [EXTCON_LINE_IN]        = "Line-in",
  56        [EXTCON_LINE_OUT]       = "Line-out",
  57        [EXTCON_MIC_IN]         = "Microphone",
  58        [EXTCON_HEADPHONE_OUT]  = "Headphone",
  59        [EXTCON_SPDIF_IN]       = "SPDIF-in",
  60        [EXTCON_SPDIF_OUT]      = "SPDIF-out",
  61        [EXTCON_VIDEO_IN]       = "Video-in",
  62        [EXTCON_VIDEO_OUT]      = "Video-out",
  63        [EXTCON_MECHANICAL]     = "Mechanical",
  64
  65        NULL,
  66};
  67
  68static struct class *extcon_class;
  69#if defined(CONFIG_ANDROID)
  70static struct class_compat *switch_class;
  71#endif /* CONFIG_ANDROID */
  72
  73static LIST_HEAD(extcon_dev_list);
  74static DEFINE_MUTEX(extcon_dev_list_lock);
  75
  76/**
  77 * check_mutually_exclusive - Check if new_state violates mutually_exclusive
  78 *                          condition.
  79 * @edev:       the extcon device
  80 * @new_state:  new cable attach status for @edev
  81 *
  82 * Returns 0 if nothing violates. Returns the index + 1 for the first
  83 * violated condition.
  84 */
  85static int check_mutually_exclusive(struct extcon_dev *edev, u32 new_state)
  86{
  87        int i = 0;
  88
  89        if (!edev->mutually_exclusive)
  90                return 0;
  91
  92        for (i = 0; edev->mutually_exclusive[i]; i++) {
  93                int count = 0, j;
  94                u32 correspondants = new_state & edev->mutually_exclusive[i];
  95                u32 exp = 1;
  96
  97                for (j = 0; j < 32; j++) {
  98                        if (exp & correspondants)
  99                                count++;
 100                        if (count > 1)
 101                                return i + 1;
 102                        exp <<= 1;
 103                }
 104        }
 105
 106        return 0;
 107}
 108
 109static ssize_t state_show(struct device *dev, struct device_attribute *attr,
 110                          char *buf)
 111{
 112        int i, count = 0;
 113        struct extcon_dev *edev = (struct extcon_dev *) dev_get_drvdata(dev);
 114
 115        if (edev->print_state) {
 116                int ret = edev->print_state(edev, buf);
 117
 118                if (ret >= 0)
 119                        return ret;
 120                /* Use default if failed */
 121        }
 122
 123        if (edev->max_supported == 0)
 124                return sprintf(buf, "%u\n", edev->state);
 125
 126        for (i = 0; i < SUPPORTED_CABLE_MAX; i++) {
 127                if (!edev->supported_cable[i])
 128                        break;
 129                count += sprintf(buf + count, "%s=%d\n",
 130                                 edev->supported_cable[i],
 131                                 !!(edev->state & (1 << i)));
 132        }
 133
 134        return count;
 135}
 136
 137int extcon_set_state(struct extcon_dev *edev, u32 state);
 138static ssize_t state_store(struct device *dev, struct device_attribute *attr,
 139                           const char *buf, size_t count)
 140{
 141        u32 state;
 142        ssize_t ret = 0;
 143        struct extcon_dev *edev = (struct extcon_dev *) dev_get_drvdata(dev);
 144
 145        ret = sscanf(buf, "0x%x", &state);
 146        if (ret == 0)
 147                ret = -EINVAL;
 148        else
 149                ret = extcon_set_state(edev, state);
 150
 151        if (ret < 0)
 152                return ret;
 153
 154        return count;
 155}
 156
 157static ssize_t name_show(struct device *dev, struct device_attribute *attr,
 158                char *buf)
 159{
 160        struct extcon_dev *edev = (struct extcon_dev *) dev_get_drvdata(dev);
 161
 162        /* Optional callback given by the user */
 163        if (edev->print_name) {
 164                int ret = edev->print_name(edev, buf);
 165                if (ret >= 0)
 166                        return ret;
 167        }
 168
 169        return sprintf(buf, "%s\n", dev_name(edev->dev));
 170}
 171
 172static ssize_t cable_name_show(struct device *dev,
 173                               struct device_attribute *attr, char *buf)
 174{
 175        struct extcon_cable *cable = container_of(attr, struct extcon_cable,
 176                                                  attr_name);
 177
 178        return sprintf(buf, "%s\n",
 179                       cable->edev->supported_cable[cable->cable_index]);
 180}
 181
 182static ssize_t cable_state_show(struct device *dev,
 183                                struct device_attribute *attr, char *buf)
 184{
 185        struct extcon_cable *cable = container_of(attr, struct extcon_cable,
 186                                                  attr_state);
 187
 188        return sprintf(buf, "%d\n",
 189                       extcon_get_cable_state_(cable->edev,
 190                                               cable->cable_index));
 191}
 192
 193static ssize_t cable_state_store(struct device *dev,
 194                                 struct device_attribute *attr, const char *buf,
 195                                 size_t count)
 196{
 197        struct extcon_cable *cable = container_of(attr, struct extcon_cable,
 198                                                  attr_state);
 199        int ret, state;
 200
 201        ret = sscanf(buf, "%d", &state);
 202        if (ret == 0)
 203                ret = -EINVAL;
 204        else
 205                ret = extcon_set_cable_state_(cable->edev, cable->cable_index,
 206                                              state);
 207
 208        if (ret < 0)
 209                return ret;
 210        return count;
 211}
 212
 213/**
 214 * extcon_update_state() - Update the cable attach states of the extcon device
 215 *                      only for the masked bits.
 216 * @edev:       the extcon device
 217 * @mask:       the bit mask to designate updated bits.
 218 * @state:      new cable attach status for @edev
 219 *
 220 * Changing the state sends uevent with environment variable containing
 221 * the name of extcon device (envp[0]) and the state output (envp[1]).
 222 * Tizen uses this format for extcon device to get events from ports.
 223 * Android uses this format as well.
 224 *
 225 * Note that the notifier provides which bits are changed in the state
 226 * variable with the val parameter (second) to the callback.
 227 */
 228int extcon_update_state(struct extcon_dev *edev, u32 mask, u32 state)
 229{
 230        char name_buf[120];
 231        char state_buf[120];
 232        char *prop_buf;
 233        char *envp[3];
 234        int env_offset = 0;
 235        int length;
 236        unsigned long flags;
 237
 238        spin_lock_irqsave(&edev->lock, flags);
 239
 240        if (edev->state != ((edev->state & ~mask) | (state & mask))) {
 241                u32 old_state = edev->state;
 242
 243                if (check_mutually_exclusive(edev, (edev->state & ~mask) |
 244                                                   (state & mask))) {
 245                        spin_unlock_irqrestore(&edev->lock, flags);
 246                        return -EPERM;
 247                }
 248
 249                edev->state &= ~mask;
 250                edev->state |= state & mask;
 251
 252                raw_notifier_call_chain(&edev->nh, old_state, edev);
 253
 254                /* This could be in interrupt handler */
 255                prop_buf = (char *)get_zeroed_page(GFP_ATOMIC);
 256                if (prop_buf) {
 257                        length = name_show(edev->dev, NULL, prop_buf);
 258                        if (length > 0) {
 259                                if (prop_buf[length - 1] == '\n')
 260                                        prop_buf[length - 1] = 0;
 261                                snprintf(name_buf, sizeof(name_buf),
 262                                        "NAME=%s", prop_buf);
 263                                envp[env_offset++] = name_buf;
 264                        }
 265                        length = state_show(edev->dev, NULL, prop_buf);
 266                        if (length > 0) {
 267                                if (prop_buf[length - 1] == '\n')
 268                                        prop_buf[length - 1] = 0;
 269                                snprintf(state_buf, sizeof(state_buf),
 270                                        "STATE=%s", prop_buf);
 271                                envp[env_offset++] = state_buf;
 272                        }
 273                        envp[env_offset] = NULL;
 274                        /* Unlock early before uevent */
 275                        spin_unlock_irqrestore(&edev->lock, flags);
 276
 277                        kobject_uevent_env(&edev->dev->kobj, KOBJ_CHANGE, envp);
 278                        free_page((unsigned long)prop_buf);
 279                } else {
 280                        /* Unlock early before uevent */
 281                        spin_unlock_irqrestore(&edev->lock, flags);
 282
 283                        dev_err(edev->dev, "out of memory in extcon_set_state\n");
 284                        kobject_uevent(&edev->dev->kobj, KOBJ_CHANGE);
 285                }
 286        } else {
 287                /* No changes */
 288                spin_unlock_irqrestore(&edev->lock, flags);
 289        }
 290
 291        return 0;
 292}
 293EXPORT_SYMBOL_GPL(extcon_update_state);
 294
 295/**
 296 * extcon_set_state() - Set the cable attach states of the extcon device.
 297 * @edev:       the extcon device
 298 * @state:      new cable attach status for @edev
 299 *
 300 * Note that notifier provides which bits are changed in the state
 301 * variable with the val parameter (second) to the callback.
 302 */
 303int extcon_set_state(struct extcon_dev *edev, u32 state)
 304{
 305        return extcon_update_state(edev, 0xffffffff, state);
 306}
 307EXPORT_SYMBOL_GPL(extcon_set_state);
 308
 309/**
 310 * extcon_find_cable_index() - Get the cable index based on the cable name.
 311 * @edev:       the extcon device that has the cable.
 312 * @cable_name: cable name to be searched.
 313 *
 314 * Note that accessing a cable state based on cable_index is faster than
 315 * cable_name because using cable_name induces a loop with strncmp().
 316 * Thus, when get/set_cable_state is repeatedly used, using cable_index
 317 * is recommended.
 318 */
 319int extcon_find_cable_index(struct extcon_dev *edev, const char *cable_name)
 320{
 321        int i;
 322
 323        if (edev->supported_cable) {
 324                for (i = 0; edev->supported_cable[i]; i++) {
 325                        if (!strncmp(edev->supported_cable[i],
 326                                cable_name, CABLE_NAME_MAX))
 327                                return i;
 328                }
 329        }
 330
 331        return -EINVAL;
 332}
 333EXPORT_SYMBOL_GPL(extcon_find_cable_index);
 334
 335/**
 336 * extcon_get_cable_state_() - Get the status of a specific cable.
 337 * @edev:       the extcon device that has the cable.
 338 * @index:      cable index that can be retrieved by extcon_find_cable_index().
 339 */
 340int extcon_get_cable_state_(struct extcon_dev *edev, int index)
 341{
 342        if (index < 0 || (edev->max_supported && edev->max_supported <= index))
 343                return -EINVAL;
 344
 345        return !!(edev->state & (1 << index));
 346}
 347EXPORT_SYMBOL_GPL(extcon_get_cable_state_);
 348
 349/**
 350 * extcon_get_cable_state() - Get the status of a specific cable.
 351 * @edev:       the extcon device that has the cable.
 352 * @cable_name: cable name.
 353 *
 354 * Note that this is slower than extcon_get_cable_state_.
 355 */
 356int extcon_get_cable_state(struct extcon_dev *edev, const char *cable_name)
 357{
 358        return extcon_get_cable_state_(edev, extcon_find_cable_index
 359                                                (edev, cable_name));
 360}
 361EXPORT_SYMBOL_GPL(extcon_get_cable_state);
 362
 363/**
 364 * extcon_get_cable_state_() - Set the status of a specific cable.
 365 * @edev:       the extcon device that has the cable.
 366 * @index:      cable index that can be retrieved by extcon_find_cable_index().
 367 * @cable_state:        the new cable status. The default semantics is
 368 *                      true: attached / false: detached.
 369 */
 370int extcon_set_cable_state_(struct extcon_dev *edev,
 371                        int index, bool cable_state)
 372{
 373        u32 state;
 374
 375        if (index < 0 || (edev->max_supported && edev->max_supported <= index))
 376                return -EINVAL;
 377
 378        state = cable_state ? (1 << index) : 0;
 379        return extcon_update_state(edev, 1 << index, state);
 380}
 381EXPORT_SYMBOL_GPL(extcon_set_cable_state_);
 382
 383/**
 384 * extcon_get_cable_state() - Set the status of a specific cable.
 385 * @edev:       the extcon device that has the cable.
 386 * @cable_name: cable name.
 387 * @cable_state:        the new cable status. The default semantics is
 388 *                      true: attached / false: detached.
 389 *
 390 * Note that this is slower than extcon_set_cable_state_.
 391 */
 392int extcon_set_cable_state(struct extcon_dev *edev,
 393                        const char *cable_name, bool cable_state)
 394{
 395        return extcon_set_cable_state_(edev, extcon_find_cable_index
 396                                        (edev, cable_name), cable_state);
 397}
 398EXPORT_SYMBOL_GPL(extcon_set_cable_state);
 399
 400/**
 401 * extcon_get_extcon_dev() - Get the extcon device instance from the name
 402 * @extcon_name:        The extcon name provided with extcon_dev_register()
 403 */
 404struct extcon_dev *extcon_get_extcon_dev(const char *extcon_name)
 405{
 406        struct extcon_dev *sd;
 407
 408        mutex_lock(&extcon_dev_list_lock);
 409        list_for_each_entry(sd, &extcon_dev_list, entry) {
 410                if (!strcmp(sd->name, extcon_name))
 411                        goto out;
 412        }
 413        sd = NULL;
 414out:
 415        mutex_unlock(&extcon_dev_list_lock);
 416        return sd;
 417}
 418EXPORT_SYMBOL_GPL(extcon_get_extcon_dev);
 419
 420static int _call_per_cable(struct notifier_block *nb, unsigned long val,
 421                           void *ptr)
 422{
 423        struct extcon_specific_cable_nb *obj = container_of(nb,
 424                        struct extcon_specific_cable_nb, internal_nb);
 425        struct extcon_dev *edev = ptr;
 426
 427        if ((val & (1 << obj->cable_index)) !=
 428            (edev->state & (1 << obj->cable_index))) {
 429                bool cable_state = true;
 430
 431                obj->previous_value = val;
 432
 433                if (val & (1 << obj->cable_index))
 434                        cable_state = false;
 435
 436                return obj->user_nb->notifier_call(obj->user_nb,
 437                                cable_state, ptr);
 438        }
 439
 440        return NOTIFY_OK;
 441}
 442
 443/**
 444 * extcon_register_interest() - Register a notifier for a state change of a
 445 *                            specific cable, not a entier set of cables of a
 446 *                            extcon device.
 447 * @obj:        an empty extcon_specific_cable_nb object to be returned.
 448 * @extcon_name:        the name of extcon device.
 449 * @cable_name:         the target cable name.
 450 * @nb:         the notifier block to get notified.
 451 *
 452 * Provide an empty extcon_specific_cable_nb. extcon_register_interest() sets
 453 * the struct for you.
 454 *
 455 * extcon_register_interest is a helper function for those who want to get
 456 * notification for a single specific cable's status change. If a user wants
 457 * to get notification for any changes of all cables of a extcon device,
 458 * he/she should use the general extcon_register_notifier().
 459 *
 460 * Note that the second parameter given to the callback of nb (val) is
 461 * "old_state", not the current state. The current state can be retrieved
 462 * by looking at the third pameter (edev pointer)'s state value.
 463 */
 464int extcon_register_interest(struct extcon_specific_cable_nb *obj,
 465                             const char *extcon_name, const char *cable_name,
 466                             struct notifier_block *nb)
 467{
 468        if (!obj || !extcon_name || !cable_name || !nb)
 469                return -EINVAL;
 470
 471        obj->edev = extcon_get_extcon_dev(extcon_name);
 472        if (!obj->edev)
 473                return -ENODEV;
 474
 475        obj->cable_index = extcon_find_cable_index(obj->edev, cable_name);
 476        if (obj->cable_index < 0)
 477                return -ENODEV;
 478
 479        obj->user_nb = nb;
 480
 481        obj->internal_nb.notifier_call = _call_per_cable;
 482
 483        return raw_notifier_chain_register(&obj->edev->nh, &obj->internal_nb);
 484}
 485
 486/**
 487 * extcon_unregister_interest() - Unregister the notifier registered by
 488 *                              extcon_register_interest().
 489 * @obj:        the extcon_specific_cable_nb object returned by
 490 *              extcon_register_interest().
 491 */
 492int extcon_unregister_interest(struct extcon_specific_cable_nb *obj)
 493{
 494        if (!obj)
 495                return -EINVAL;
 496
 497        return raw_notifier_chain_unregister(&obj->edev->nh, &obj->internal_nb);
 498}
 499
 500/**
 501 * extcon_register_notifier() - Register a notifee to get notified by
 502 *                            any attach status changes from the extcon.
 503 * @edev:       the extcon device.
 504 * @nb:         a notifier block to be registered.
 505 *
 506 * Note that the second parameter given to the callback of nb (val) is
 507 * "old_state", not the current state. The current state can be retrieved
 508 * by looking at the third pameter (edev pointer)'s state value.
 509 */
 510int extcon_register_notifier(struct extcon_dev *edev,
 511                        struct notifier_block *nb)
 512{
 513        return raw_notifier_chain_register(&edev->nh, nb);
 514}
 515EXPORT_SYMBOL_GPL(extcon_register_notifier);
 516
 517/**
 518 * extcon_unregister_notifier() - Unregister a notifee from the extcon device.
 519 * @edev:       the extcon device.
 520 * @nb:         a registered notifier block to be unregistered.
 521 */
 522int extcon_unregister_notifier(struct extcon_dev *edev,
 523                        struct notifier_block *nb)
 524{
 525        return raw_notifier_chain_unregister(&edev->nh, nb);
 526}
 527EXPORT_SYMBOL_GPL(extcon_unregister_notifier);
 528
 529static struct device_attribute extcon_attrs[] = {
 530        __ATTR(state, S_IRUGO | S_IWUSR, state_show, state_store),
 531        __ATTR_RO(name),
 532        __ATTR_NULL,
 533};
 534
 535static int create_extcon_class(void)
 536{
 537        if (!extcon_class) {
 538                extcon_class = class_create(THIS_MODULE, "extcon");
 539                if (IS_ERR(extcon_class))
 540                        return PTR_ERR(extcon_class);
 541                extcon_class->dev_attrs = extcon_attrs;
 542
 543#if defined(CONFIG_ANDROID)
 544                switch_class = class_compat_register("switch");
 545                if (WARN(!switch_class, "cannot allocate"))
 546                        return -ENOMEM;
 547#endif /* CONFIG_ANDROID */
 548        }
 549
 550        return 0;
 551}
 552
 553static void extcon_cleanup(struct extcon_dev *edev, bool skip)
 554{
 555        mutex_lock(&extcon_dev_list_lock);
 556        list_del(&edev->entry);
 557        mutex_unlock(&extcon_dev_list_lock);
 558
 559        if (!skip && get_device(edev->dev)) {
 560                int index;
 561
 562                if (edev->mutually_exclusive && edev->max_supported) {
 563                        for (index = 0; edev->mutually_exclusive[index];
 564                             index++)
 565                                kfree(edev->d_attrs_muex[index].attr.name);
 566                        kfree(edev->d_attrs_muex);
 567                        kfree(edev->attrs_muex);
 568                }
 569
 570                for (index = 0; index < edev->max_supported; index++)
 571                        kfree(edev->cables[index].attr_g.name);
 572
 573                if (edev->max_supported) {
 574                        kfree(edev->extcon_dev_type.groups);
 575                        kfree(edev->cables);
 576                }
 577
 578#if defined(CONFIG_ANDROID)
 579                if (switch_class)
 580                        class_compat_remove_link(switch_class, edev->dev, NULL);
 581#endif
 582                device_unregister(edev->dev);
 583                put_device(edev->dev);
 584        }
 585
 586        kfree(edev->dev);
 587}
 588
 589static void extcon_dev_release(struct device *dev)
 590{
 591        struct extcon_dev *edev = (struct extcon_dev *) dev_get_drvdata(dev);
 592
 593        extcon_cleanup(edev, true);
 594}
 595
 596static const char *muex_name = "mutually_exclusive";
 597static void dummy_sysfs_dev_release(struct device *dev)
 598{
 599}
 600
 601/**
 602 * extcon_dev_register() - Register a new extcon device
 603 * @edev        : the new extcon device (should be allocated before calling)
 604 * @dev         : the parent device for this extcon device.
 605 *
 606 * Among the members of edev struct, please set the "user initializing data"
 607 * in any case and set the "optional callbacks" if required. However, please
 608 * do not set the values of "internal data", which are initialized by
 609 * this function.
 610 */
 611int extcon_dev_register(struct extcon_dev *edev, struct device *dev)
 612{
 613        int ret, index = 0;
 614
 615        if (!extcon_class) {
 616                ret = create_extcon_class();
 617                if (ret < 0)
 618                        return ret;
 619        }
 620
 621        if (edev->supported_cable) {
 622                /* Get size of array */
 623                for (index = 0; edev->supported_cable[index]; index++)
 624                        ;
 625                edev->max_supported = index;
 626        } else {
 627                edev->max_supported = 0;
 628        }
 629
 630        if (index > SUPPORTED_CABLE_MAX) {
 631                dev_err(edev->dev, "extcon: maximum number of supported cables exceeded.\n");
 632                return -EINVAL;
 633        }
 634
 635        edev->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
 636        if (!edev->dev)
 637                return -ENOMEM;
 638        edev->dev->parent = dev;
 639        edev->dev->class = extcon_class;
 640        edev->dev->release = extcon_dev_release;
 641
 642        dev_set_name(edev->dev, edev->name ? edev->name : dev_name(dev));
 643
 644        if (edev->max_supported) {
 645                char buf[10];
 646                char *str;
 647                struct extcon_cable *cable;
 648
 649                edev->cables = kzalloc(sizeof(struct extcon_cable) *
 650                                       edev->max_supported, GFP_KERNEL);
 651                if (!edev->cables) {
 652                        ret = -ENOMEM;
 653                        goto err_sysfs_alloc;
 654                }
 655                for (index = 0; index < edev->max_supported; index++) {
 656                        cable = &edev->cables[index];
 657
 658                        snprintf(buf, 10, "cable.%d", index);
 659                        str = kzalloc(sizeof(char) * (strlen(buf) + 1),
 660                                      GFP_KERNEL);
 661                        if (!str) {
 662                                for (index--; index >= 0; index--) {
 663                                        cable = &edev->cables[index];
 664                                        kfree(cable->attr_g.name);
 665                                }
 666                                ret = -ENOMEM;
 667
 668                                goto err_alloc_cables;
 669                        }
 670                        strcpy(str, buf);
 671
 672                        cable->edev = edev;
 673                        cable->cable_index = index;
 674                        cable->attrs[0] = &cable->attr_name.attr;
 675                        cable->attrs[1] = &cable->attr_state.attr;
 676                        cable->attrs[2] = NULL;
 677                        cable->attr_g.name = str;
 678                        cable->attr_g.attrs = cable->attrs;
 679
 680                        cable->attr_name.attr.name = "name";
 681                        cable->attr_name.attr.mode = 0444;
 682                        cable->attr_name.show = cable_name_show;
 683
 684                        cable->attr_state.attr.name = "state";
 685                        cable->attr_state.attr.mode = 0644;
 686                        cable->attr_state.show = cable_state_show;
 687                        cable->attr_state.store = cable_state_store;
 688                }
 689        }
 690
 691        if (edev->max_supported && edev->mutually_exclusive) {
 692                char buf[80];
 693                char *name;
 694
 695                /* Count the size of mutually_exclusive array */
 696                for (index = 0; edev->mutually_exclusive[index]; index++)
 697                        ;
 698
 699                edev->attrs_muex = kzalloc(sizeof(struct attribute *) *
 700                                           (index + 1), GFP_KERNEL);
 701                if (!edev->attrs_muex) {
 702                        ret = -ENOMEM;
 703                        goto err_muex;
 704                }
 705
 706                edev->d_attrs_muex = kzalloc(sizeof(struct device_attribute) *
 707                                             index, GFP_KERNEL);
 708                if (!edev->d_attrs_muex) {
 709                        ret = -ENOMEM;
 710                        kfree(edev->attrs_muex);
 711                        goto err_muex;
 712                }
 713
 714                for (index = 0; edev->mutually_exclusive[index]; index++) {
 715                        sprintf(buf, "0x%x", edev->mutually_exclusive[index]);
 716                        name = kzalloc(sizeof(char) * (strlen(buf) + 1),
 717                                       GFP_KERNEL);
 718                        if (!name) {
 719                                for (index--; index >= 0; index--) {
 720                                        kfree(edev->d_attrs_muex[index].attr.
 721                                              name);
 722                                }
 723                                kfree(edev->d_attrs_muex);
 724                                kfree(edev->attrs_muex);
 725                                ret = -ENOMEM;
 726                                goto err_muex;
 727                        }
 728                        strcpy(name, buf);
 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]
 732                                                        .attr;
 733                }
 734                edev->attr_g_muex.name = muex_name;
 735                edev->attr_g_muex.attrs = edev->attrs_muex;
 736
 737        }
 738
 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) {
 744                        ret = -ENOMEM;
 745                        goto err_alloc_groups;
 746                }
 747
 748                edev->extcon_dev_type.name = dev_name(edev->dev);
 749                edev->extcon_dev_type.release = dummy_sysfs_dev_release;
 750
 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] =
 756                                &edev->attr_g_muex;
 757
 758                edev->dev->type = &edev->extcon_dev_type;
 759        }
 760
 761        ret = device_register(edev->dev);
 762        if (ret) {
 763                put_device(edev->dev);
 764                goto err_dev;
 765        }
 766#if defined(CONFIG_ANDROID)
 767        if (switch_class)
 768                ret = class_compat_create_link(switch_class, edev->dev,
 769                                               NULL);
 770#endif /* CONFIG_ANDROID */
 771
 772        spin_lock_init(&edev->lock);
 773
 774        RAW_INIT_NOTIFIER_HEAD(&edev->nh);
 775
 776        dev_set_drvdata(edev->dev, edev);
 777        edev->state = 0;
 778
 779        mutex_lock(&extcon_dev_list_lock);
 780        list_add(&edev->entry, &extcon_dev_list);
 781        mutex_unlock(&extcon_dev_list_lock);
 782
 783        return 0;
 784
 785err_dev:
 786        if (edev->max_supported)
 787                kfree(edev->extcon_dev_type.groups);
 788err_alloc_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);
 794        }
 795err_muex:
 796        for (index = 0; index < edev->max_supported; index++)
 797                kfree(edev->cables[index].attr_g.name);
 798err_alloc_cables:
 799        if (edev->max_supported)
 800                kfree(edev->cables);
 801err_sysfs_alloc:
 802        kfree(edev->dev);
 803        return ret;
 804}
 805EXPORT_SYMBOL_GPL(extcon_dev_register);
 806
 807/**
 808 * extcon_dev_unregister() - Unregister the extcon device.
 809 * @edev:       the extcon device instance to be unregitered.
 810 *
 811 * Note that this does not call kfree(edev) because edev was not allocated
 812 * by this class.
 813 */
 814void extcon_dev_unregister(struct extcon_dev *edev)
 815{
 816        extcon_cleanup(edev, false);
 817}
 818EXPORT_SYMBOL_GPL(extcon_dev_unregister);
 819
 820static int __init extcon_class_init(void)
 821{
 822        return create_extcon_class();
 823}
 824module_init(extcon_class_init);
 825
 826static void __exit extcon_class_exit(void)
 827{
 828#if defined(CONFIG_ANDROID)
 829        class_compat_unregister(switch_class);
 830#endif
 831        class_destroy(extcon_class);
 832}
 833module_exit(extcon_class_exit);
 834
 835MODULE_AUTHOR("Mike Lockwood <lockwood@android.com>");
 836MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>");
 837MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
 838MODULE_DESCRIPTION("External connector (extcon) class driver");
 839MODULE_LICENSE("GPL");
 840
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.