UPSTREAM: extcon: Block the bit masking operation for cable state except for extcon...
[firefly-linux-kernel-4.4.55.git] / drivers / extcon / extcon.c
1 /*
2  *  drivers/extcon/extcon.c - External Connector (extcon) framework.
3  *
4  *  External connector (extcon) class driver
5  *
6  * Copyright (C) 2015 Samsung Electronics
7  * Author: Chanwoo Choi <cw00.choi@samsung.com>
8  *
9  * Copyright (C) 2012 Samsung Electronics
10  * Author: Donggeun Kim <dg77.kim@samsung.com>
11  * Author: MyungJoo Ham <myungjoo.ham@samsung.com>
12  *
13  * based on android/drivers/switch/switch_class.c
14  * Copyright (C) 2008 Google, Inc.
15  * Author: Mike Lockwood <lockwood@android.com>
16  *
17  * This software is licensed under the terms of the GNU General Public
18  * License version 2, as published by the Free Software Foundation, and
19  * may be copied, distributed, and modified under those terms.
20  *
21  * This program is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  * GNU General Public License for more details.
25  */
26
27 #include <linux/module.h>
28 #include <linux/types.h>
29 #include <linux/init.h>
30 #include <linux/device.h>
31 #include <linux/fs.h>
32 #include <linux/err.h>
33 #include <linux/extcon.h>
34 #include <linux/of.h>
35 #include <linux/slab.h>
36 #include <linux/sysfs.h>
37
38 #define SUPPORTED_CABLE_MAX     32
39 #define CABLE_NAME_MAX          30
40
41 static const char *extcon_name[] =  {
42         [EXTCON_NONE]                   = "NONE",
43
44         /* USB external connector */
45         [EXTCON_USB]                    = "USB",
46         [EXTCON_USB_HOST]               = "USB-HOST",
47
48         /* Charging external connector */
49         [EXTCON_CHG_USB_SDP]            = "SDP",
50         [EXTCON_CHG_USB_DCP]            = "DCP",
51         [EXTCON_CHG_USB_CDP]            = "CDP",
52         [EXTCON_CHG_USB_ACA]            = "ACA",
53         [EXTCON_CHG_USB_FAST]           = "FAST-CHARGER",
54         [EXTCON_CHG_USB_SLOW]           = "SLOW-CHARGER",
55
56         /* Jack external connector */
57         [EXTCON_JACK_MICROPHONE]        = "MICROPHONE",
58         [EXTCON_JACK_HEADPHONE]         = "HEADPHONE",
59         [EXTCON_JACK_LINE_IN]           = "LINE-IN",
60         [EXTCON_JACK_LINE_OUT]          = "LINE-OUT",
61         [EXTCON_JACK_VIDEO_IN]          = "VIDEO-IN",
62         [EXTCON_JACK_VIDEO_OUT]         = "VIDEO-OUT",
63         [EXTCON_JACK_SPDIF_IN]          = "SPDIF-IN",
64         [EXTCON_JACK_SPDIF_OUT]         = "SPDIF-OUT",
65
66         /* Display external connector */
67         [EXTCON_DISP_HDMI]              = "HDMI",
68         [EXTCON_DISP_MHL]               = "MHL",
69         [EXTCON_DISP_DVI]               = "DVI",
70         [EXTCON_DISP_VGA]               = "VGA",
71
72         /* Miscellaneous external connector */
73         [EXTCON_DOCK]                   = "DOCK",
74         [EXTCON_JIG]                    = "JIG",
75         [EXTCON_MECHANICAL]             = "MECHANICAL",
76
77         NULL,
78 };
79
80 /**
81  * struct extcon_cable - An internal data for each cable of extcon device.
82  * @edev:               The extcon device
83  * @cable_index:        Index of this cable in the edev
84  * @attr_g:             Attribute group for the cable
85  * @attr_name:          "name" sysfs entry
86  * @attr_state:         "state" sysfs entry
87  * @attrs:              Array pointing to attr_name and attr_state for attr_g
88  */
89 struct extcon_cable {
90         struct extcon_dev *edev;
91         int cable_index;
92
93         struct attribute_group attr_g;
94         struct device_attribute attr_name;
95         struct device_attribute attr_state;
96
97         struct attribute *attrs[3]; /* to be fed to attr_g.attrs */
98 };
99
100 static struct class *extcon_class;
101 #if defined(CONFIG_ANDROID)
102 static struct class_compat *switch_class;
103 #endif /* CONFIG_ANDROID */
104
105 static LIST_HEAD(extcon_dev_list);
106 static DEFINE_MUTEX(extcon_dev_list_lock);
107
108 /**
109  * check_mutually_exclusive - Check if new_state violates mutually_exclusive
110  *                            condition.
111  * @edev:       the extcon device
112  * @new_state:  new cable attach status for @edev
113  *
114  * Returns 0 if nothing violates. Returns the index + 1 for the first
115  * violated condition.
116  */
117 static int check_mutually_exclusive(struct extcon_dev *edev, u32 new_state)
118 {
119         int i = 0;
120
121         if (!edev->mutually_exclusive)
122                 return 0;
123
124         for (i = 0; edev->mutually_exclusive[i]; i++) {
125                 int weight;
126                 u32 correspondants = new_state & edev->mutually_exclusive[i];
127
128                 /* calculate the total number of bits set */
129                 weight = hweight32(correspondants);
130                 if (weight > 1)
131                         return i + 1;
132         }
133
134         return 0;
135 }
136
137 static int find_cable_index_by_id(struct extcon_dev *edev, const unsigned int id)
138 {
139         int i;
140
141         /* Find the the index of extcon cable in edev->supported_cable */
142         for (i = 0; i < edev->max_supported; i++) {
143                 if (edev->supported_cable[i] == id)
144                         return i;
145         }
146
147         return -EINVAL;
148 }
149
150 static bool is_extcon_changed(u32 prev, u32 new, int idx, bool *attached)
151 {
152         if (((prev >> idx) & 0x1) != ((new >> idx) & 0x1)) {
153                 *attached = ((new >> idx) & 0x1) ? true : false;
154                 return true;
155         }
156
157         return false;
158 }
159
160 static ssize_t state_show(struct device *dev, struct device_attribute *attr,
161                           char *buf)
162 {
163         int i, count = 0;
164         struct extcon_dev *edev = dev_get_drvdata(dev);
165
166         if (edev->max_supported == 0)
167                 return sprintf(buf, "%u\n", edev->state);
168
169         for (i = 0; i < edev->max_supported; i++) {
170                 count += sprintf(buf + count, "%s=%d\n",
171                                 extcon_name[edev->supported_cable[i]],
172                                  !!(edev->state & (1 << i)));
173         }
174
175         return count;
176 }
177 static DEVICE_ATTR_RO(state);
178
179 static ssize_t name_show(struct device *dev, struct device_attribute *attr,
180                 char *buf)
181 {
182         struct extcon_dev *edev = dev_get_drvdata(dev);
183
184         return sprintf(buf, "%s\n", edev->name);
185 }
186 static DEVICE_ATTR_RO(name);
187
188 static ssize_t cable_name_show(struct device *dev,
189                                struct device_attribute *attr, char *buf)
190 {
191         struct extcon_cable *cable = container_of(attr, struct extcon_cable,
192                                                   attr_name);
193         int i = cable->cable_index;
194
195         return sprintf(buf, "%s\n",
196                         extcon_name[cable->edev->supported_cable[i]]);
197 }
198
199 static ssize_t cable_state_show(struct device *dev,
200                                 struct device_attribute *attr, char *buf)
201 {
202         struct extcon_cable *cable = container_of(attr, struct extcon_cable,
203                                                   attr_state);
204
205         int i = cable->cable_index;
206
207         return sprintf(buf, "%d\n",
208                        extcon_get_cable_state_(cable->edev,
209                                                cable->edev->supported_cable[i]));
210 }
211
212 /**
213  * extcon_update_state() - Update the cable attach states of the extcon device
214  *                         only for the masked bits.
215  * @edev:       the extcon device
216  * @mask:       the bit mask to designate updated bits.
217  * @state:      new cable attach status for @edev
218  *
219  * Changing the state sends uevent with environment variable containing
220  * the name of extcon device (envp[0]) and the state output (envp[1]).
221  * Tizen uses this format for extcon device to get events from ports.
222  * Android uses this format as well.
223  *
224  * Note that the notifier provides which bits are changed in the state
225  * variable with the val parameter (second) to the callback.
226  */
227 static int extcon_update_state(struct extcon_dev *edev, u32 mask, u32 state)
228 {
229         char name_buf[120];
230         char state_buf[120];
231         char *prop_buf;
232         char *envp[3];
233         int env_offset = 0;
234         int length;
235         int index;
236         unsigned long flags;
237         bool attached;
238
239         if (!edev)
240                 return -EINVAL;
241
242         spin_lock_irqsave(&edev->lock, flags);
243
244         if (edev->state != ((edev->state & ~mask) | (state & mask))) {
245                 u32 old_state;
246
247                 if (check_mutually_exclusive(edev, (edev->state & ~mask) |
248                                                    (state & mask))) {
249                         spin_unlock_irqrestore(&edev->lock, flags);
250                         return -EPERM;
251                 }
252
253                 old_state = edev->state;
254                 edev->state &= ~mask;
255                 edev->state |= state & mask;
256
257                 for (index = 0; index < edev->max_supported; index++) {
258                         if (is_extcon_changed(old_state, edev->state, index,
259                                               &attached))
260                                 raw_notifier_call_chain(&edev->nh[index],
261                                                         attached, edev);
262                 }
263
264                 /* This could be in interrupt handler */
265                 prop_buf = (char *)get_zeroed_page(GFP_ATOMIC);
266                 if (prop_buf) {
267                         length = name_show(&edev->dev, NULL, prop_buf);
268                         if (length > 0) {
269                                 if (prop_buf[length - 1] == '\n')
270                                         prop_buf[length - 1] = 0;
271                                 snprintf(name_buf, sizeof(name_buf),
272                                         "NAME=%s", prop_buf);
273                                 envp[env_offset++] = name_buf;
274                         }
275                         length = state_show(&edev->dev, NULL, prop_buf);
276                         if (length > 0) {
277                                 if (prop_buf[length - 1] == '\n')
278                                         prop_buf[length - 1] = 0;
279                                 snprintf(state_buf, sizeof(state_buf),
280                                         "STATE=%s", prop_buf);
281                                 envp[env_offset++] = state_buf;
282                         }
283                         envp[env_offset] = NULL;
284                         /* Unlock early before uevent */
285                         spin_unlock_irqrestore(&edev->lock, flags);
286
287                         kobject_uevent_env(&edev->dev.kobj, KOBJ_CHANGE, envp);
288                         free_page((unsigned long)prop_buf);
289                 } else {
290                         /* Unlock early before uevent */
291                         spin_unlock_irqrestore(&edev->lock, flags);
292
293                         dev_err(&edev->dev, "out of memory in extcon_set_state\n");
294                         kobject_uevent(&edev->dev.kobj, KOBJ_CHANGE);
295                 }
296         } else {
297                 /* No changes */
298                 spin_unlock_irqrestore(&edev->lock, flags);
299         }
300
301         return 0;
302 }
303
304 /**
305  * extcon_get_cable_state_() - Get the status of a specific cable.
306  * @edev:       the extcon device that has the cable.
307  * @id:         the unique id of each external connector in extcon enumeration.
308  */
309 int extcon_get_cable_state_(struct extcon_dev *edev, const unsigned int id)
310 {
311         int index;
312
313         if (!edev)
314                 return -EINVAL;
315
316         index = find_cable_index_by_id(edev, id);
317         if (index < 0)
318                 return index;
319
320         if (edev->max_supported && edev->max_supported <= index)
321                 return -EINVAL;
322
323         return !!(edev->state & (1 << index));
324 }
325 EXPORT_SYMBOL_GPL(extcon_get_cable_state_);
326
327 /**
328  * extcon_set_cable_state_() - Set the status of a specific cable.
329  * @edev:               the extcon device that has the cable.
330  * @id:                 the unique id of each external connector
331  *                      in extcon enumeration.
332  * @state:              the new cable status. The default semantics is
333  *                      true: attached / false: detached.
334  */
335 int extcon_set_cable_state_(struct extcon_dev *edev, unsigned int id,
336                                 bool cable_state)
337 {
338         u32 state;
339         int index;
340
341         if (!edev)
342                 return -EINVAL;
343
344         index = find_cable_index_by_id(edev, id);
345         if (index < 0)
346                 return index;
347
348         if (edev->max_supported && edev->max_supported <= index)
349                 return -EINVAL;
350
351         state = cable_state ? (1 << index) : 0;
352         return extcon_update_state(edev, 1 << index, state);
353 }
354 EXPORT_SYMBOL_GPL(extcon_set_cable_state_);
355
356 /**
357  * extcon_get_extcon_dev() - Get the extcon device instance from the name
358  * @extcon_name:        The extcon name provided with extcon_dev_register()
359  */
360 struct extcon_dev *extcon_get_extcon_dev(const char *extcon_name)
361 {
362         struct extcon_dev *sd;
363
364         if (!extcon_name)
365                 return ERR_PTR(-EINVAL);
366
367         mutex_lock(&extcon_dev_list_lock);
368         list_for_each_entry(sd, &extcon_dev_list, entry) {
369                 if (!strcmp(sd->name, extcon_name))
370                         goto out;
371         }
372         sd = NULL;
373 out:
374         mutex_unlock(&extcon_dev_list_lock);
375         return sd;
376 }
377 EXPORT_SYMBOL_GPL(extcon_get_extcon_dev);
378
379 /**
380  * extcon_register_notifier() - Register a notifiee to get notified by
381  *                              any attach status changes from the extcon.
382  * @edev:       the extcon device that has the external connecotr.
383  * @id:         the unique id of each external connector in extcon enumeration.
384  * @nb:         a notifier block to be registered.
385  *
386  * Note that the second parameter given to the callback of nb (val) is
387  * "old_state", not the current state. The current state can be retrieved
388  * by looking at the third pameter (edev pointer)'s state value.
389  */
390 int extcon_register_notifier(struct extcon_dev *edev, unsigned int id,
391                              struct notifier_block *nb)
392 {
393         unsigned long flags;
394         int ret, idx;
395
396         if (!nb)
397                 return -EINVAL;
398
399         if (edev) {
400                 idx = find_cable_index_by_id(edev, id);
401                 if (idx < 0)
402                         return idx;
403
404                 spin_lock_irqsave(&edev->lock, flags);
405                 ret = raw_notifier_chain_register(&edev->nh[idx], nb);
406                 spin_unlock_irqrestore(&edev->lock, flags);
407         } else {
408                 struct extcon_dev *extd;
409
410                 mutex_lock(&extcon_dev_list_lock);
411                 list_for_each_entry(extd, &extcon_dev_list, entry) {
412                         idx = find_cable_index_by_id(extd, id);
413                         if (idx >= 0)
414                                 break;
415                 }
416                 mutex_unlock(&extcon_dev_list_lock);
417
418                 if (idx >= 0) {
419                         edev = extd;
420                         return extcon_register_notifier(extd, id, nb);
421                 } else {
422                         ret = -ENODEV;
423                 }
424         }
425
426         return ret;
427 }
428 EXPORT_SYMBOL_GPL(extcon_register_notifier);
429
430 /**
431  * extcon_unregister_notifier() - Unregister a notifiee from the extcon device.
432  * @edev:       the extcon device that has the external connecotr.
433  * @id:         the unique id of each external connector in extcon enumeration.
434  * @nb:         a notifier block to be registered.
435  */
436 int extcon_unregister_notifier(struct extcon_dev *edev, unsigned int id,
437                                 struct notifier_block *nb)
438 {
439         unsigned long flags;
440         int ret, idx;
441
442         if (!edev || !nb)
443                 return -EINVAL;
444
445         idx = find_cable_index_by_id(edev, id);
446         if (idx < 0)
447                 return idx;
448
449         spin_lock_irqsave(&edev->lock, flags);
450         ret = raw_notifier_chain_unregister(&edev->nh[idx], nb);
451         spin_unlock_irqrestore(&edev->lock, flags);
452
453         return ret;
454 }
455 EXPORT_SYMBOL_GPL(extcon_unregister_notifier);
456
457 static struct attribute *extcon_attrs[] = {
458         &dev_attr_state.attr,
459         &dev_attr_name.attr,
460         NULL,
461 };
462 ATTRIBUTE_GROUPS(extcon);
463
464 static int create_extcon_class(void)
465 {
466         if (!extcon_class) {
467                 extcon_class = class_create(THIS_MODULE, "extcon");
468                 if (IS_ERR(extcon_class))
469                         return PTR_ERR(extcon_class);
470                 extcon_class->dev_groups = extcon_groups;
471
472 #if defined(CONFIG_ANDROID) && !defined(CONFIG_SWITCH)
473                 switch_class = class_compat_register("switch");
474                 if (WARN(!switch_class, "cannot allocate"))
475                         return -ENOMEM;
476 #endif /* CONFIG_ANDROID */
477         }
478
479         return 0;
480 }
481
482 static void extcon_dev_release(struct device *dev)
483 {
484 }
485
486 static const char *muex_name = "mutually_exclusive";
487 static void dummy_sysfs_dev_release(struct device *dev)
488 {
489 }
490
491 /*
492  * extcon_dev_allocate() - Allocate the memory of extcon device.
493  * @supported_cable:    Array of supported extcon ending with EXTCON_NONE.
494  *                      If supported_cable is NULL, cable name related APIs
495  *                      are disabled.
496  *
497  * This function allocates the memory for extcon device without allocating
498  * memory in each extcon provider driver and initialize default setting for
499  * extcon device.
500  *
501  * Return the pointer of extcon device if success or ERR_PTR(err) if fail
502  */
503 struct extcon_dev *extcon_dev_allocate(const unsigned int *supported_cable)
504 {
505         struct extcon_dev *edev;
506
507         if (!supported_cable)
508                 return ERR_PTR(-EINVAL);
509
510         edev = kzalloc(sizeof(*edev), GFP_KERNEL);
511         if (!edev)
512                 return ERR_PTR(-ENOMEM);
513
514         edev->max_supported = 0;
515         edev->supported_cable = supported_cable;
516
517         return edev;
518 }
519
520 /*
521  * extcon_dev_free() - Free the memory of extcon device.
522  * @edev:       the extcon device to free
523  */
524 void extcon_dev_free(struct extcon_dev *edev)
525 {
526         kfree(edev);
527 }
528 EXPORT_SYMBOL_GPL(extcon_dev_free);
529
530 /**
531  * extcon_dev_register() - Register a new extcon device
532  * @edev        : the new extcon device (should be allocated before calling)
533  *
534  * Among the members of edev struct, please set the "user initializing data"
535  * in any case and set the "optional callbacks" if required. However, please
536  * do not set the values of "internal data", which are initialized by
537  * this function.
538  */
539 int extcon_dev_register(struct extcon_dev *edev)
540 {
541         int ret, index = 0;
542         static atomic_t edev_no = ATOMIC_INIT(-1);
543
544         if (!extcon_class) {
545                 ret = create_extcon_class();
546                 if (ret < 0)
547                         return ret;
548         }
549
550         if (!edev || !edev->supported_cable)
551                 return -EINVAL;
552
553         for (; edev->supported_cable[index] != EXTCON_NONE; index++);
554
555         edev->max_supported = index;
556         if (index > SUPPORTED_CABLE_MAX) {
557                 dev_err(&edev->dev,
558                         "exceed the maximum number of supported cables\n");
559                 return -EINVAL;
560         }
561
562         edev->dev.class = extcon_class;
563         edev->dev.release = extcon_dev_release;
564
565         edev->name = dev_name(edev->dev.parent);
566         if (IS_ERR_OR_NULL(edev->name)) {
567                 dev_err(&edev->dev,
568                         "extcon device name is null\n");
569                 return -EINVAL;
570         }
571         dev_set_name(&edev->dev, "extcon%lu",
572                         (unsigned long)atomic_inc_return(&edev_no));
573
574         if (edev->max_supported) {
575                 char buf[10];
576                 char *str;
577                 struct extcon_cable *cable;
578
579                 edev->cables = kzalloc(sizeof(struct extcon_cable) *
580                                        edev->max_supported, GFP_KERNEL);
581                 if (!edev->cables) {
582                         ret = -ENOMEM;
583                         goto err_sysfs_alloc;
584                 }
585                 for (index = 0; index < edev->max_supported; index++) {
586                         cable = &edev->cables[index];
587
588                         snprintf(buf, 10, "cable.%d", index);
589                         str = kzalloc(sizeof(char) * (strlen(buf) + 1),
590                                       GFP_KERNEL);
591                         if (!str) {
592                                 for (index--; index >= 0; index--) {
593                                         cable = &edev->cables[index];
594                                         kfree(cable->attr_g.name);
595                                 }
596                                 ret = -ENOMEM;
597
598                                 goto err_alloc_cables;
599                         }
600                         strcpy(str, buf);
601
602                         cable->edev = edev;
603                         cable->cable_index = index;
604                         cable->attrs[0] = &cable->attr_name.attr;
605                         cable->attrs[1] = &cable->attr_state.attr;
606                         cable->attrs[2] = NULL;
607                         cable->attr_g.name = str;
608                         cable->attr_g.attrs = cable->attrs;
609
610                         sysfs_attr_init(&cable->attr_name.attr);
611                         cable->attr_name.attr.name = "name";
612                         cable->attr_name.attr.mode = 0444;
613                         cable->attr_name.show = cable_name_show;
614
615                         sysfs_attr_init(&cable->attr_state.attr);
616                         cable->attr_state.attr.name = "state";
617                         cable->attr_state.attr.mode = 0444;
618                         cable->attr_state.show = cable_state_show;
619                 }
620         }
621
622         if (edev->max_supported && edev->mutually_exclusive) {
623                 char buf[80];
624                 char *name;
625
626                 /* Count the size of mutually_exclusive array */
627                 for (index = 0; edev->mutually_exclusive[index]; index++)
628                         ;
629
630                 edev->attrs_muex = kzalloc(sizeof(struct attribute *) *
631                                            (index + 1), GFP_KERNEL);
632                 if (!edev->attrs_muex) {
633                         ret = -ENOMEM;
634                         goto err_muex;
635                 }
636
637                 edev->d_attrs_muex = kzalloc(sizeof(struct device_attribute) *
638                                              index, GFP_KERNEL);
639                 if (!edev->d_attrs_muex) {
640                         ret = -ENOMEM;
641                         kfree(edev->attrs_muex);
642                         goto err_muex;
643                 }
644
645                 for (index = 0; edev->mutually_exclusive[index]; index++) {
646                         sprintf(buf, "0x%x", edev->mutually_exclusive[index]);
647                         name = kzalloc(sizeof(char) * (strlen(buf) + 1),
648                                        GFP_KERNEL);
649                         if (!name) {
650                                 for (index--; index >= 0; index--) {
651                                         kfree(edev->d_attrs_muex[index].attr.
652                                               name);
653                                 }
654                                 kfree(edev->d_attrs_muex);
655                                 kfree(edev->attrs_muex);
656                                 ret = -ENOMEM;
657                                 goto err_muex;
658                         }
659                         strcpy(name, buf);
660                         sysfs_attr_init(&edev->d_attrs_muex[index].attr);
661                         edev->d_attrs_muex[index].attr.name = name;
662                         edev->d_attrs_muex[index].attr.mode = 0000;
663                         edev->attrs_muex[index] = &edev->d_attrs_muex[index]
664                                                         .attr;
665                 }
666                 edev->attr_g_muex.name = muex_name;
667                 edev->attr_g_muex.attrs = edev->attrs_muex;
668
669         }
670
671         if (edev->max_supported) {
672                 edev->extcon_dev_type.groups =
673                         kzalloc(sizeof(struct attribute_group *) *
674                                 (edev->max_supported + 2), GFP_KERNEL);
675                 if (!edev->extcon_dev_type.groups) {
676                         ret = -ENOMEM;
677                         goto err_alloc_groups;
678                 }
679
680                 edev->extcon_dev_type.name = dev_name(&edev->dev);
681                 edev->extcon_dev_type.release = dummy_sysfs_dev_release;
682
683                 for (index = 0; index < edev->max_supported; index++)
684                         edev->extcon_dev_type.groups[index] =
685                                 &edev->cables[index].attr_g;
686                 if (edev->mutually_exclusive)
687                         edev->extcon_dev_type.groups[index] =
688                                 &edev->attr_g_muex;
689
690                 edev->dev.type = &edev->extcon_dev_type;
691         }
692
693         ret = device_register(&edev->dev);
694         if (ret) {
695                 put_device(&edev->dev);
696                 goto err_dev;
697         }
698 #if defined(CONFIG_ANDROID)
699         if (switch_class)
700                 ret = class_compat_create_link(switch_class, &edev->dev, NULL);
701 #endif /* CONFIG_ANDROID */
702
703         spin_lock_init(&edev->lock);
704
705         edev->nh = devm_kzalloc(&edev->dev,
706                         sizeof(*edev->nh) * edev->max_supported, GFP_KERNEL);
707         if (!edev->nh) {
708                 ret = -ENOMEM;
709                 goto err_dev;
710         }
711
712         for (index = 0; index < edev->max_supported; index++)
713                 RAW_INIT_NOTIFIER_HEAD(&edev->nh[index]);
714
715         dev_set_drvdata(&edev->dev, edev);
716         edev->state = 0;
717
718         mutex_lock(&extcon_dev_list_lock);
719         list_add(&edev->entry, &extcon_dev_list);
720         mutex_unlock(&extcon_dev_list_lock);
721
722         return 0;
723
724 err_dev:
725         if (edev->max_supported)
726                 kfree(edev->extcon_dev_type.groups);
727 err_alloc_groups:
728         if (edev->max_supported && edev->mutually_exclusive) {
729                 for (index = 0; edev->mutually_exclusive[index]; index++)
730                         kfree(edev->d_attrs_muex[index].attr.name);
731                 kfree(edev->d_attrs_muex);
732                 kfree(edev->attrs_muex);
733         }
734 err_muex:
735         for (index = 0; index < edev->max_supported; index++)
736                 kfree(edev->cables[index].attr_g.name);
737 err_alloc_cables:
738         if (edev->max_supported)
739                 kfree(edev->cables);
740 err_sysfs_alloc:
741         return ret;
742 }
743 EXPORT_SYMBOL_GPL(extcon_dev_register);
744
745 /**
746  * extcon_dev_unregister() - Unregister the extcon device.
747  * @edev:       the extcon device instance to be unregistered.
748  *
749  * Note that this does not call kfree(edev) because edev was not allocated
750  * by this class.
751  */
752 void extcon_dev_unregister(struct extcon_dev *edev)
753 {
754         int index;
755
756         if (!edev)
757                 return;
758
759         mutex_lock(&extcon_dev_list_lock);
760         list_del(&edev->entry);
761         mutex_unlock(&extcon_dev_list_lock);
762
763         if (IS_ERR_OR_NULL(get_device(&edev->dev))) {
764                 dev_err(&edev->dev, "Failed to unregister extcon_dev (%s)\n",
765                                 dev_name(&edev->dev));
766                 return;
767         }
768
769         device_unregister(&edev->dev);
770
771         if (edev->mutually_exclusive && edev->max_supported) {
772                 for (index = 0; edev->mutually_exclusive[index];
773                                 index++)
774                         kfree(edev->d_attrs_muex[index].attr.name);
775                 kfree(edev->d_attrs_muex);
776                 kfree(edev->attrs_muex);
777         }
778
779         for (index = 0; index < edev->max_supported; index++)
780                 kfree(edev->cables[index].attr_g.name);
781
782         if (edev->max_supported) {
783                 kfree(edev->extcon_dev_type.groups);
784                 kfree(edev->cables);
785         }
786
787 #if defined(CONFIG_ANDROID)
788         if (switch_class)
789                 class_compat_remove_link(switch_class, &edev->dev, NULL);
790 #endif
791         put_device(&edev->dev);
792 }
793 EXPORT_SYMBOL_GPL(extcon_dev_unregister);
794
795 #ifdef CONFIG_OF
796 /*
797  * extcon_get_edev_by_phandle - Get the extcon device from devicetree
798  * @dev - instance to the given device
799  * @index - index into list of extcon_dev
800  *
801  * return the instance of extcon device
802  */
803 struct extcon_dev *extcon_get_edev_by_phandle(struct device *dev, int index)
804 {
805         struct device_node *node;
806         struct extcon_dev *edev;
807
808         if (!dev)
809                 return ERR_PTR(-EINVAL);
810
811         if (!dev->of_node) {
812                 dev_dbg(dev, "device does not have a device node entry\n");
813                 return ERR_PTR(-EINVAL);
814         }
815
816         node = of_parse_phandle(dev->of_node, "extcon", index);
817         if (!node) {
818                 dev_dbg(dev, "failed to get phandle in %s node\n",
819                         dev->of_node->full_name);
820                 return ERR_PTR(-ENODEV);
821         }
822
823         mutex_lock(&extcon_dev_list_lock);
824         list_for_each_entry(edev, &extcon_dev_list, entry) {
825                 if (edev->dev.parent && edev->dev.parent->of_node == node) {
826                         mutex_unlock(&extcon_dev_list_lock);
827                         of_node_put(node);
828                         return edev;
829                 }
830         }
831         mutex_unlock(&extcon_dev_list_lock);
832         of_node_put(node);
833
834         return ERR_PTR(-EPROBE_DEFER);
835 }
836 #else
837 struct extcon_dev *extcon_get_edev_by_phandle(struct device *dev, int index)
838 {
839         return ERR_PTR(-ENOSYS);
840 }
841 #endif /* CONFIG_OF */
842 EXPORT_SYMBOL_GPL(extcon_get_edev_by_phandle);
843
844 /**
845  * extcon_get_edev_name() - Get the name of the extcon device.
846  * @edev:       the extcon device
847  */
848 const char *extcon_get_edev_name(struct extcon_dev *edev)
849 {
850         return !edev ? NULL : edev->name;
851 }
852
853 static int __init extcon_class_init(void)
854 {
855         return create_extcon_class();
856 }
857 module_init(extcon_class_init);
858
859 static void __exit extcon_class_exit(void)
860 {
861 #if defined(CONFIG_ANDROID)
862         class_compat_unregister(switch_class);
863 #endif
864         class_destroy(extcon_class);
865 }
866 module_exit(extcon_class_exit);
867
868 MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>");
869 MODULE_AUTHOR("Mike Lockwood <lockwood@android.com>");
870 MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>");
871 MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
872 MODULE_DESCRIPTION("External connector (extcon) class driver");
873 MODULE_LICENSE("GPL");