]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/extcon/extcon.c
Merge branch 'upstream' of git://git.infradead.org/users/pcmoore/selinux into for...
[karo-tx-linux.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 static struct class *extcon_class;
81 #if defined(CONFIG_ANDROID)
82 static struct class_compat *switch_class;
83 #endif /* CONFIG_ANDROID */
84
85 static LIST_HEAD(extcon_dev_list);
86 static DEFINE_MUTEX(extcon_dev_list_lock);
87
88 /**
89  * check_mutually_exclusive - Check if new_state violates mutually_exclusive
90  *                            condition.
91  * @edev:       the extcon device
92  * @new_state:  new cable attach status for @edev
93  *
94  * Returns 0 if nothing violates. Returns the index + 1 for the first
95  * violated condition.
96  */
97 static int check_mutually_exclusive(struct extcon_dev *edev, u32 new_state)
98 {
99         int i = 0;
100
101         if (!edev->mutually_exclusive)
102                 return 0;
103
104         for (i = 0; edev->mutually_exclusive[i]; i++) {
105                 int weight;
106                 u32 correspondants = new_state & edev->mutually_exclusive[i];
107
108                 /* calculate the total number of bits set */
109                 weight = hweight32(correspondants);
110                 if (weight > 1)
111                         return i + 1;
112         }
113
114         return 0;
115 }
116
117 static int find_cable_index_by_id(struct extcon_dev *edev, const unsigned int id)
118 {
119         int i;
120
121         /* Find the the index of extcon cable in edev->supported_cable */
122         for (i = 0; i < edev->max_supported; i++) {
123                 if (edev->supported_cable[i] == id)
124                         return i;
125         }
126
127         return -EINVAL;
128 }
129
130 static int find_cable_id_by_name(struct extcon_dev *edev, const char *name)
131 {
132         int id = -EINVAL;
133         int i = 0;
134
135         /* Find the id of extcon cable */
136         while (extcon_name[i]) {
137                 if (!strncmp(extcon_name[i], name, CABLE_NAME_MAX)) {
138                         id = i;
139                         break;
140                 }
141                 i++;
142         }
143
144         return id;
145 }
146
147 static int find_cable_index_by_name(struct extcon_dev *edev, const char *name)
148 {
149         int id;
150
151         if (edev->max_supported == 0)
152                 return -EINVAL;
153
154         /* Find the the number of extcon cable */
155         id = find_cable_id_by_name(edev, name);
156         if (id < 0)
157                 return id;
158
159         return find_cable_index_by_id(edev, id);
160 }
161
162 static bool is_extcon_changed(u32 prev, u32 new, int idx, bool *attached)
163 {
164         if (((prev >> idx) & 0x1) != ((new >> idx) & 0x1)) {
165                 *attached = ((new >> idx) & 0x1) ? true : false;
166                 return true;
167         }
168
169         return false;
170 }
171
172 static ssize_t state_show(struct device *dev, struct device_attribute *attr,
173                           char *buf)
174 {
175         int i, count = 0;
176         struct extcon_dev *edev = dev_get_drvdata(dev);
177
178         if (edev->max_supported == 0)
179                 return sprintf(buf, "%u\n", edev->state);
180
181         for (i = 0; i < edev->max_supported; i++) {
182                 count += sprintf(buf + count, "%s=%d\n",
183                                 extcon_name[edev->supported_cable[i]],
184                                  !!(edev->state & (1 << i)));
185         }
186
187         return count;
188 }
189
190 static ssize_t state_store(struct device *dev, struct device_attribute *attr,
191                            const char *buf, size_t count)
192 {
193         u32 state;
194         ssize_t ret = 0;
195         struct extcon_dev *edev = dev_get_drvdata(dev);
196
197         ret = sscanf(buf, "0x%x", &state);
198         if (ret == 0)
199                 ret = -EINVAL;
200         else
201                 ret = extcon_set_state(edev, state);
202
203         if (ret < 0)
204                 return ret;
205
206         return count;
207 }
208 static DEVICE_ATTR_RW(state);
209
210 static ssize_t name_show(struct device *dev, struct device_attribute *attr,
211                 char *buf)
212 {
213         struct extcon_dev *edev = dev_get_drvdata(dev);
214
215         return sprintf(buf, "%s\n", edev->name);
216 }
217 static DEVICE_ATTR_RO(name);
218
219 static ssize_t cable_name_show(struct device *dev,
220                                struct device_attribute *attr, char *buf)
221 {
222         struct extcon_cable *cable = container_of(attr, struct extcon_cable,
223                                                   attr_name);
224         int i = cable->cable_index;
225
226         return sprintf(buf, "%s\n",
227                         extcon_name[cable->edev->supported_cable[i]]);
228 }
229
230 static ssize_t cable_state_show(struct device *dev,
231                                 struct device_attribute *attr, char *buf)
232 {
233         struct extcon_cable *cable = container_of(attr, struct extcon_cable,
234                                                   attr_state);
235
236         int i = cable->cable_index;
237
238         return sprintf(buf, "%d\n",
239                        extcon_get_cable_state_(cable->edev,
240                                                cable->edev->supported_cable[i]));
241 }
242
243 /**
244  * extcon_update_state() - Update the cable attach states of the extcon device
245  *                         only for the masked bits.
246  * @edev:       the extcon device
247  * @mask:       the bit mask to designate updated bits.
248  * @state:      new cable attach status for @edev
249  *
250  * Changing the state sends uevent with environment variable containing
251  * the name of extcon device (envp[0]) and the state output (envp[1]).
252  * Tizen uses this format for extcon device to get events from ports.
253  * Android uses this format as well.
254  *
255  * Note that the notifier provides which bits are changed in the state
256  * variable with the val parameter (second) to the callback.
257  */
258 int extcon_update_state(struct extcon_dev *edev, u32 mask, u32 state)
259 {
260         char name_buf[120];
261         char state_buf[120];
262         char *prop_buf;
263         char *envp[3];
264         int env_offset = 0;
265         int length;
266         int index;
267         unsigned long flags;
268         bool attached;
269
270         if (!edev)
271                 return -EINVAL;
272
273         spin_lock_irqsave(&edev->lock, flags);
274
275         if (edev->state != ((edev->state & ~mask) | (state & mask))) {
276                 u32 old_state;
277
278                 if (check_mutually_exclusive(edev, (edev->state & ~mask) |
279                                                    (state & mask))) {
280                         spin_unlock_irqrestore(&edev->lock, flags);
281                         return -EPERM;
282                 }
283
284                 old_state = edev->state;
285                 edev->state &= ~mask;
286                 edev->state |= state & mask;
287
288                 for (index = 0; index < edev->max_supported; index++) {
289                         if (is_extcon_changed(old_state, edev->state, index,
290                                               &attached))
291                                 raw_notifier_call_chain(&edev->nh[index],
292                                                         attached, edev);
293                 }
294
295                 /* This could be in interrupt handler */
296                 prop_buf = (char *)get_zeroed_page(GFP_ATOMIC);
297                 if (prop_buf) {
298                         length = name_show(&edev->dev, NULL, prop_buf);
299                         if (length > 0) {
300                                 if (prop_buf[length - 1] == '\n')
301                                         prop_buf[length - 1] = 0;
302                                 snprintf(name_buf, sizeof(name_buf),
303                                         "NAME=%s", prop_buf);
304                                 envp[env_offset++] = name_buf;
305                         }
306                         length = state_show(&edev->dev, NULL, prop_buf);
307                         if (length > 0) {
308                                 if (prop_buf[length - 1] == '\n')
309                                         prop_buf[length - 1] = 0;
310                                 snprintf(state_buf, sizeof(state_buf),
311                                         "STATE=%s", prop_buf);
312                                 envp[env_offset++] = state_buf;
313                         }
314                         envp[env_offset] = NULL;
315                         /* Unlock early before uevent */
316                         spin_unlock_irqrestore(&edev->lock, flags);
317
318                         kobject_uevent_env(&edev->dev.kobj, KOBJ_CHANGE, envp);
319                         free_page((unsigned long)prop_buf);
320                 } else {
321                         /* Unlock early before uevent */
322                         spin_unlock_irqrestore(&edev->lock, flags);
323
324                         dev_err(&edev->dev, "out of memory in extcon_set_state\n");
325                         kobject_uevent(&edev->dev.kobj, KOBJ_CHANGE);
326                 }
327         } else {
328                 /* No changes */
329                 spin_unlock_irqrestore(&edev->lock, flags);
330         }
331
332         return 0;
333 }
334 EXPORT_SYMBOL_GPL(extcon_update_state);
335
336 /**
337  * extcon_set_state() - Set the cable attach states of the extcon device.
338  * @edev:       the extcon device
339  * @state:      new cable attach status for @edev
340  *
341  * Note that notifier provides which bits are changed in the state
342  * variable with the val parameter (second) to the callback.
343  */
344 int extcon_set_state(struct extcon_dev *edev, u32 state)
345 {
346         if (!edev)
347                 return -EINVAL;
348
349         return extcon_update_state(edev, 0xffffffff, state);
350 }
351 EXPORT_SYMBOL_GPL(extcon_set_state);
352
353 /**
354  * extcon_get_cable_state_() - Get the status of a specific cable.
355  * @edev:       the extcon device that has the cable.
356  * @id:         the unique id of each external connector in extcon enumeration.
357  */
358 int extcon_get_cable_state_(struct extcon_dev *edev, const unsigned int id)
359 {
360         int index;
361
362         if (!edev)
363                 return -EINVAL;
364
365         index = find_cable_index_by_id(edev, id);
366         if (index < 0)
367                 return index;
368
369         if (edev->max_supported && edev->max_supported <= index)
370                 return -EINVAL;
371
372         return !!(edev->state & (1 << index));
373 }
374 EXPORT_SYMBOL_GPL(extcon_get_cable_state_);
375
376 /**
377  * extcon_get_cable_state() - Get the status of a specific cable.
378  * @edev:       the extcon device that has the cable.
379  * @cable_name: cable name.
380  *
381  * Note that this is slower than extcon_get_cable_state_.
382  */
383 int extcon_get_cable_state(struct extcon_dev *edev, const char *cable_name)
384 {
385         int id;
386
387         id = find_cable_id_by_name(edev, cable_name);
388         if (id < 0)
389                 return id;
390
391         return extcon_get_cable_state_(edev, id);
392 }
393 EXPORT_SYMBOL_GPL(extcon_get_cable_state);
394
395 /**
396  * extcon_set_cable_state_() - Set the status of a specific cable.
397  * @edev:               the extcon device that has the cable.
398  * @id:                 the unique id of each external connector
399  *                      in extcon enumeration.
400  * @state:              the new cable status. The default semantics is
401  *                      true: attached / false: detached.
402  */
403 int extcon_set_cable_state_(struct extcon_dev *edev, unsigned int id,
404                                 bool cable_state)
405 {
406         u32 state;
407         int index;
408
409         if (!edev)
410                 return -EINVAL;
411
412         index = find_cable_index_by_id(edev, id);
413         if (index < 0)
414                 return index;
415
416         if (edev->max_supported && edev->max_supported <= index)
417                 return -EINVAL;
418
419         state = cable_state ? (1 << index) : 0;
420         return extcon_update_state(edev, 1 << index, state);
421 }
422 EXPORT_SYMBOL_GPL(extcon_set_cable_state_);
423
424 /**
425  * extcon_set_cable_state() - Set the status of a specific cable.
426  * @edev:               the extcon device that has the cable.
427  * @cable_name:         cable name.
428  * @cable_state:        the new cable status. The default semantics is
429  *                      true: attached / false: detached.
430  *
431  * Note that this is slower than extcon_set_cable_state_.
432  */
433 int extcon_set_cable_state(struct extcon_dev *edev,
434                         const char *cable_name, bool cable_state)
435 {
436         int id;
437
438         id = find_cable_id_by_name(edev, cable_name);
439         if (id < 0)
440                 return id;
441
442         return extcon_set_cable_state_(edev, id, cable_state);
443 }
444 EXPORT_SYMBOL_GPL(extcon_set_cable_state);
445
446 /**
447  * extcon_get_extcon_dev() - Get the extcon device instance from the name
448  * @extcon_name:        The extcon name provided with extcon_dev_register()
449  */
450 struct extcon_dev *extcon_get_extcon_dev(const char *extcon_name)
451 {
452         struct extcon_dev *sd;
453
454         if (!extcon_name)
455                 return ERR_PTR(-EINVAL);
456
457         mutex_lock(&extcon_dev_list_lock);
458         list_for_each_entry(sd, &extcon_dev_list, entry) {
459                 if (!strcmp(sd->name, extcon_name))
460                         goto out;
461         }
462         sd = NULL;
463 out:
464         mutex_unlock(&extcon_dev_list_lock);
465         return sd;
466 }
467 EXPORT_SYMBOL_GPL(extcon_get_extcon_dev);
468
469 /**
470  * extcon_register_interest() - Register a notifier for a state change of a
471  *                              specific cable, not an entier set of cables of a
472  *                              extcon device.
473  * @obj:                an empty extcon_specific_cable_nb object to be returned.
474  * @extcon_name:        the name of extcon device.
475  *                      if NULL, extcon_register_interest will register
476  *                      every cable with the target cable_name given.
477  * @cable_name:         the target cable name.
478  * @nb:                 the notifier block to get notified.
479  *
480  * Provide an empty extcon_specific_cable_nb. extcon_register_interest() sets
481  * the struct for you.
482  *
483  * extcon_register_interest is a helper function for those who want to get
484  * notification for a single specific cable's status change. If a user wants
485  * to get notification for any changes of all cables of a extcon device,
486  * he/she should use the general extcon_register_notifier().
487  *
488  * Note that the second parameter given to the callback of nb (val) is
489  * "old_state", not the current state. The current state can be retrieved
490  * by looking at the third pameter (edev pointer)'s state value.
491  */
492 int extcon_register_interest(struct extcon_specific_cable_nb *obj,
493                              const char *extcon_name, const char *cable_name,
494                              struct notifier_block *nb)
495 {
496         unsigned long flags;
497         int ret;
498
499         if (!obj || !cable_name || !nb)
500                 return -EINVAL;
501
502         if (extcon_name) {
503                 obj->edev = extcon_get_extcon_dev(extcon_name);
504                 if (!obj->edev)
505                         return -ENODEV;
506
507                 obj->cable_index = find_cable_index_by_name(obj->edev,
508                                                         cable_name);
509                 if (obj->cable_index < 0)
510                         return obj->cable_index;
511
512                 obj->user_nb = nb;
513
514                 spin_lock_irqsave(&obj->edev->lock, flags);
515                 ret = raw_notifier_chain_register(
516                                         &obj->edev->nh[obj->cable_index],
517                                         obj->user_nb);
518                 spin_unlock_irqrestore(&obj->edev->lock, flags);
519         } else {
520                 struct class_dev_iter iter;
521                 struct extcon_dev *extd;
522                 struct device *dev;
523
524                 if (!extcon_class)
525                         return -ENODEV;
526                 class_dev_iter_init(&iter, extcon_class, NULL, NULL);
527                 while ((dev = class_dev_iter_next(&iter))) {
528                         extd = dev_get_drvdata(dev);
529
530                         if (find_cable_index_by_name(extd, cable_name) < 0)
531                                 continue;
532
533                         class_dev_iter_exit(&iter);
534                         return extcon_register_interest(obj, extd->name,
535                                                 cable_name, nb);
536                 }
537
538                 ret = -ENODEV;
539         }
540
541         return ret;
542 }
543 EXPORT_SYMBOL_GPL(extcon_register_interest);
544
545 /**
546  * extcon_unregister_interest() - Unregister the notifier registered by
547  *                                extcon_register_interest().
548  * @obj:        the extcon_specific_cable_nb object returned by
549  *              extcon_register_interest().
550  */
551 int extcon_unregister_interest(struct extcon_specific_cable_nb *obj)
552 {
553         unsigned long flags;
554         int ret;
555
556         if (!obj)
557                 return -EINVAL;
558
559         spin_lock_irqsave(&obj->edev->lock, flags);
560         ret = raw_notifier_chain_unregister(
561                         &obj->edev->nh[obj->cable_index], obj->user_nb);
562         spin_unlock_irqrestore(&obj->edev->lock, flags);
563
564         return ret;
565 }
566 EXPORT_SYMBOL_GPL(extcon_unregister_interest);
567
568 /**
569  * extcon_register_notifier() - Register a notifiee to get notified by
570  *                              any attach status changes from the extcon.
571  * @edev:       the extcon device that has the external connecotr.
572  * @id:         the unique id of each external connector in extcon enumeration.
573  * @nb:         a notifier block to be registered.
574  *
575  * Note that the second parameter given to the callback of nb (val) is
576  * "old_state", not the current state. The current state can be retrieved
577  * by looking at the third pameter (edev pointer)'s state value.
578  */
579 int extcon_register_notifier(struct extcon_dev *edev, unsigned int id,
580                              struct notifier_block *nb)
581 {
582         unsigned long flags;
583         int ret, idx;
584
585         if (!edev || !nb)
586                 return -EINVAL;
587
588         idx = find_cable_index_by_id(edev, id);
589
590         spin_lock_irqsave(&edev->lock, flags);
591         ret = raw_notifier_chain_register(&edev->nh[idx], nb);
592         spin_unlock_irqrestore(&edev->lock, flags);
593
594         return ret;
595 }
596 EXPORT_SYMBOL_GPL(extcon_register_notifier);
597
598 /**
599  * extcon_unregister_notifier() - Unregister a notifiee from the extcon device.
600  * @edev:       the extcon device that has the external connecotr.
601  * @id:         the unique id of each external connector in extcon enumeration.
602  * @nb:         a notifier block to be registered.
603  */
604 int extcon_unregister_notifier(struct extcon_dev *edev, unsigned int id,
605                                 struct notifier_block *nb)
606 {
607         unsigned long flags;
608         int ret, idx;
609
610         if (!edev || !nb)
611                 return -EINVAL;
612
613         idx = find_cable_index_by_id(edev, id);
614
615         spin_lock_irqsave(&edev->lock, flags);
616         ret = raw_notifier_chain_unregister(&edev->nh[idx], nb);
617         spin_unlock_irqrestore(&edev->lock, flags);
618
619         return ret;
620 }
621 EXPORT_SYMBOL_GPL(extcon_unregister_notifier);
622
623 static struct attribute *extcon_attrs[] = {
624         &dev_attr_state.attr,
625         &dev_attr_name.attr,
626         NULL,
627 };
628 ATTRIBUTE_GROUPS(extcon);
629
630 static int create_extcon_class(void)
631 {
632         if (!extcon_class) {
633                 extcon_class = class_create(THIS_MODULE, "extcon");
634                 if (IS_ERR(extcon_class))
635                         return PTR_ERR(extcon_class);
636                 extcon_class->dev_groups = extcon_groups;
637
638 #if defined(CONFIG_ANDROID)
639                 switch_class = class_compat_register("switch");
640                 if (WARN(!switch_class, "cannot allocate"))
641                         return -ENOMEM;
642 #endif /* CONFIG_ANDROID */
643         }
644
645         return 0;
646 }
647
648 static void extcon_dev_release(struct device *dev)
649 {
650 }
651
652 static const char *muex_name = "mutually_exclusive";
653 static void dummy_sysfs_dev_release(struct device *dev)
654 {
655 }
656
657 /*
658  * extcon_dev_allocate() - Allocate the memory of extcon device.
659  * @supported_cable:    Array of supported extcon ending with EXTCON_NONE.
660  *                      If supported_cable is NULL, cable name related APIs
661  *                      are disabled.
662  *
663  * This function allocates the memory for extcon device without allocating
664  * memory in each extcon provider driver and initialize default setting for
665  * extcon device.
666  *
667  * Return the pointer of extcon device if success or ERR_PTR(err) if fail
668  */
669 struct extcon_dev *extcon_dev_allocate(const unsigned int *supported_cable)
670 {
671         struct extcon_dev *edev;
672
673         if (!supported_cable)
674                 return ERR_PTR(-EINVAL);
675
676         edev = kzalloc(sizeof(*edev), GFP_KERNEL);
677         if (!edev)
678                 return ERR_PTR(-ENOMEM);
679
680         edev->max_supported = 0;
681         edev->supported_cable = supported_cable;
682
683         return edev;
684 }
685
686 /*
687  * extcon_dev_free() - Free the memory of extcon device.
688  * @edev:       the extcon device to free
689  */
690 void extcon_dev_free(struct extcon_dev *edev)
691 {
692         kfree(edev);
693 }
694 EXPORT_SYMBOL_GPL(extcon_dev_free);
695
696 static int devm_extcon_dev_match(struct device *dev, void *res, void *data)
697 {
698         struct extcon_dev **r = res;
699
700         if (WARN_ON(!r || !*r))
701                 return 0;
702
703         return *r == data;
704 }
705
706 static void devm_extcon_dev_release(struct device *dev, void *res)
707 {
708         extcon_dev_free(*(struct extcon_dev **)res);
709 }
710
711 /**
712  * devm_extcon_dev_allocate - Allocate managed extcon device
713  * @dev:                device owning the extcon device being created
714  * @supported_cable:    Array of supported extcon ending with EXTCON_NONE.
715  *                      If supported_cable is NULL, cable name related APIs
716  *                      are disabled.
717  *
718  * This function manages automatically the memory of extcon device using device
719  * resource management and simplify the control of freeing the memory of extcon
720  * device.
721  *
722  * Returns the pointer memory of allocated extcon_dev if success
723  * or ERR_PTR(err) if fail
724  */
725 struct extcon_dev *devm_extcon_dev_allocate(struct device *dev,
726                                         const unsigned int *supported_cable)
727 {
728         struct extcon_dev **ptr, *edev;
729
730         ptr = devres_alloc(devm_extcon_dev_release, sizeof(*ptr), GFP_KERNEL);
731         if (!ptr)
732                 return ERR_PTR(-ENOMEM);
733
734         edev = extcon_dev_allocate(supported_cable);
735         if (IS_ERR(edev)) {
736                 devres_free(ptr);
737                 return edev;
738         }
739
740         edev->dev.parent = dev;
741
742         *ptr = edev;
743         devres_add(dev, ptr);
744
745         return edev;
746 }
747 EXPORT_SYMBOL_GPL(devm_extcon_dev_allocate);
748
749 void devm_extcon_dev_free(struct device *dev, struct extcon_dev *edev)
750 {
751         WARN_ON(devres_release(dev, devm_extcon_dev_release,
752                                devm_extcon_dev_match, edev));
753 }
754 EXPORT_SYMBOL_GPL(devm_extcon_dev_free);
755
756 /**
757  * extcon_dev_register() - Register a new extcon device
758  * @edev        : the new extcon device (should be allocated before calling)
759  *
760  * Among the members of edev struct, please set the "user initializing data"
761  * in any case and set the "optional callbacks" if required. However, please
762  * do not set the values of "internal data", which are initialized by
763  * this function.
764  */
765 int extcon_dev_register(struct extcon_dev *edev)
766 {
767         int ret, index = 0;
768         static atomic_t edev_no = ATOMIC_INIT(-1);
769
770         if (!extcon_class) {
771                 ret = create_extcon_class();
772                 if (ret < 0)
773                         return ret;
774         }
775
776         if (!edev || !edev->supported_cable)
777                 return -EINVAL;
778
779         for (; edev->supported_cable[index] != EXTCON_NONE; index++);
780
781         edev->max_supported = index;
782         if (index > SUPPORTED_CABLE_MAX) {
783                 dev_err(&edev->dev,
784                         "exceed the maximum number of supported cables\n");
785                 return -EINVAL;
786         }
787
788         edev->dev.class = extcon_class;
789         edev->dev.release = extcon_dev_release;
790
791         edev->name = dev_name(edev->dev.parent);
792         if (IS_ERR_OR_NULL(edev->name)) {
793                 dev_err(&edev->dev,
794                         "extcon device name is null\n");
795                 return -EINVAL;
796         }
797         dev_set_name(&edev->dev, "extcon%lu",
798                         (unsigned long)atomic_inc_return(&edev_no));
799
800         if (edev->max_supported) {
801                 char buf[10];
802                 char *str;
803                 struct extcon_cable *cable;
804
805                 edev->cables = kzalloc(sizeof(struct extcon_cable) *
806                                        edev->max_supported, GFP_KERNEL);
807                 if (!edev->cables) {
808                         ret = -ENOMEM;
809                         goto err_sysfs_alloc;
810                 }
811                 for (index = 0; index < edev->max_supported; index++) {
812                         cable = &edev->cables[index];
813
814                         snprintf(buf, 10, "cable.%d", index);
815                         str = kzalloc(sizeof(char) * (strlen(buf) + 1),
816                                       GFP_KERNEL);
817                         if (!str) {
818                                 for (index--; index >= 0; index--) {
819                                         cable = &edev->cables[index];
820                                         kfree(cable->attr_g.name);
821                                 }
822                                 ret = -ENOMEM;
823
824                                 goto err_alloc_cables;
825                         }
826                         strcpy(str, buf);
827
828                         cable->edev = edev;
829                         cable->cable_index = index;
830                         cable->attrs[0] = &cable->attr_name.attr;
831                         cable->attrs[1] = &cable->attr_state.attr;
832                         cable->attrs[2] = NULL;
833                         cable->attr_g.name = str;
834                         cable->attr_g.attrs = cable->attrs;
835
836                         sysfs_attr_init(&cable->attr_name.attr);
837                         cable->attr_name.attr.name = "name";
838                         cable->attr_name.attr.mode = 0444;
839                         cable->attr_name.show = cable_name_show;
840
841                         sysfs_attr_init(&cable->attr_state.attr);
842                         cable->attr_state.attr.name = "state";
843                         cable->attr_state.attr.mode = 0444;
844                         cable->attr_state.show = cable_state_show;
845                 }
846         }
847
848         if (edev->max_supported && edev->mutually_exclusive) {
849                 char buf[80];
850                 char *name;
851
852                 /* Count the size of mutually_exclusive array */
853                 for (index = 0; edev->mutually_exclusive[index]; index++)
854                         ;
855
856                 edev->attrs_muex = kzalloc(sizeof(struct attribute *) *
857                                            (index + 1), GFP_KERNEL);
858                 if (!edev->attrs_muex) {
859                         ret = -ENOMEM;
860                         goto err_muex;
861                 }
862
863                 edev->d_attrs_muex = kzalloc(sizeof(struct device_attribute) *
864                                              index, GFP_KERNEL);
865                 if (!edev->d_attrs_muex) {
866                         ret = -ENOMEM;
867                         kfree(edev->attrs_muex);
868                         goto err_muex;
869                 }
870
871                 for (index = 0; edev->mutually_exclusive[index]; index++) {
872                         sprintf(buf, "0x%x", edev->mutually_exclusive[index]);
873                         name = kzalloc(sizeof(char) * (strlen(buf) + 1),
874                                        GFP_KERNEL);
875                         if (!name) {
876                                 for (index--; index >= 0; index--) {
877                                         kfree(edev->d_attrs_muex[index].attr.
878                                               name);
879                                 }
880                                 kfree(edev->d_attrs_muex);
881                                 kfree(edev->attrs_muex);
882                                 ret = -ENOMEM;
883                                 goto err_muex;
884                         }
885                         strcpy(name, buf);
886                         sysfs_attr_init(&edev->d_attrs_muex[index].attr);
887                         edev->d_attrs_muex[index].attr.name = name;
888                         edev->d_attrs_muex[index].attr.mode = 0000;
889                         edev->attrs_muex[index] = &edev->d_attrs_muex[index]
890                                                         .attr;
891                 }
892                 edev->attr_g_muex.name = muex_name;
893                 edev->attr_g_muex.attrs = edev->attrs_muex;
894
895         }
896
897         if (edev->max_supported) {
898                 edev->extcon_dev_type.groups =
899                         kzalloc(sizeof(struct attribute_group *) *
900                                 (edev->max_supported + 2), GFP_KERNEL);
901                 if (!edev->extcon_dev_type.groups) {
902                         ret = -ENOMEM;
903                         goto err_alloc_groups;
904                 }
905
906                 edev->extcon_dev_type.name = dev_name(&edev->dev);
907                 edev->extcon_dev_type.release = dummy_sysfs_dev_release;
908
909                 for (index = 0; index < edev->max_supported; index++)
910                         edev->extcon_dev_type.groups[index] =
911                                 &edev->cables[index].attr_g;
912                 if (edev->mutually_exclusive)
913                         edev->extcon_dev_type.groups[index] =
914                                 &edev->attr_g_muex;
915
916                 edev->dev.type = &edev->extcon_dev_type;
917         }
918
919         ret = device_register(&edev->dev);
920         if (ret) {
921                 put_device(&edev->dev);
922                 goto err_dev;
923         }
924 #if defined(CONFIG_ANDROID)
925         if (switch_class)
926                 ret = class_compat_create_link(switch_class, &edev->dev, NULL);
927 #endif /* CONFIG_ANDROID */
928
929         spin_lock_init(&edev->lock);
930
931         edev->nh = devm_kzalloc(&edev->dev,
932                         sizeof(*edev->nh) * edev->max_supported, GFP_KERNEL);
933         if (!edev->nh) {
934                 ret = -ENOMEM;
935                 goto err_dev;
936         }
937
938         for (index = 0; index < edev->max_supported; index++)
939                 RAW_INIT_NOTIFIER_HEAD(&edev->nh[index]);
940
941         dev_set_drvdata(&edev->dev, edev);
942         edev->state = 0;
943
944         mutex_lock(&extcon_dev_list_lock);
945         list_add(&edev->entry, &extcon_dev_list);
946         mutex_unlock(&extcon_dev_list_lock);
947
948         return 0;
949
950 err_dev:
951         if (edev->max_supported)
952                 kfree(edev->extcon_dev_type.groups);
953 err_alloc_groups:
954         if (edev->max_supported && edev->mutually_exclusive) {
955                 for (index = 0; edev->mutually_exclusive[index]; index++)
956                         kfree(edev->d_attrs_muex[index].attr.name);
957                 kfree(edev->d_attrs_muex);
958                 kfree(edev->attrs_muex);
959         }
960 err_muex:
961         for (index = 0; index < edev->max_supported; index++)
962                 kfree(edev->cables[index].attr_g.name);
963 err_alloc_cables:
964         if (edev->max_supported)
965                 kfree(edev->cables);
966 err_sysfs_alloc:
967         return ret;
968 }
969 EXPORT_SYMBOL_GPL(extcon_dev_register);
970
971 /**
972  * extcon_dev_unregister() - Unregister the extcon device.
973  * @edev:       the extcon device instance to be unregistered.
974  *
975  * Note that this does not call kfree(edev) because edev was not allocated
976  * by this class.
977  */
978 void extcon_dev_unregister(struct extcon_dev *edev)
979 {
980         int index;
981
982         if (!edev)
983                 return;
984
985         mutex_lock(&extcon_dev_list_lock);
986         list_del(&edev->entry);
987         mutex_unlock(&extcon_dev_list_lock);
988
989         if (IS_ERR_OR_NULL(get_device(&edev->dev))) {
990                 dev_err(&edev->dev, "Failed to unregister extcon_dev (%s)\n",
991                                 dev_name(&edev->dev));
992                 return;
993         }
994
995         device_unregister(&edev->dev);
996
997         if (edev->mutually_exclusive && edev->max_supported) {
998                 for (index = 0; edev->mutually_exclusive[index];
999                                 index++)
1000                         kfree(edev->d_attrs_muex[index].attr.name);
1001                 kfree(edev->d_attrs_muex);
1002                 kfree(edev->attrs_muex);
1003         }
1004
1005         for (index = 0; index < edev->max_supported; index++)
1006                 kfree(edev->cables[index].attr_g.name);
1007
1008         if (edev->max_supported) {
1009                 kfree(edev->extcon_dev_type.groups);
1010                 kfree(edev->cables);
1011         }
1012
1013 #if defined(CONFIG_ANDROID)
1014         if (switch_class)
1015                 class_compat_remove_link(switch_class, &edev->dev, NULL);
1016 #endif
1017         put_device(&edev->dev);
1018 }
1019 EXPORT_SYMBOL_GPL(extcon_dev_unregister);
1020
1021 static void devm_extcon_dev_unreg(struct device *dev, void *res)
1022 {
1023         extcon_dev_unregister(*(struct extcon_dev **)res);
1024 }
1025
1026 /**
1027  * devm_extcon_dev_register() - Resource-managed extcon_dev_register()
1028  * @dev:        device to allocate extcon device
1029  * @edev:       the new extcon device to register
1030  *
1031  * Managed extcon_dev_register() function. If extcon device is attached with
1032  * this function, that extcon device is automatically unregistered on driver
1033  * detach. Internally this function calls extcon_dev_register() function.
1034  * To get more information, refer that function.
1035  *
1036  * If extcon device is registered with this function and the device needs to be
1037  * unregistered separately, devm_extcon_dev_unregister() should be used.
1038  *
1039  * Returns 0 if success or negaive error number if failure.
1040  */
1041 int devm_extcon_dev_register(struct device *dev, struct extcon_dev *edev)
1042 {
1043         struct extcon_dev **ptr;
1044         int ret;
1045
1046         ptr = devres_alloc(devm_extcon_dev_unreg, sizeof(*ptr), GFP_KERNEL);
1047         if (!ptr)
1048                 return -ENOMEM;
1049
1050         ret = extcon_dev_register(edev);
1051         if (ret) {
1052                 devres_free(ptr);
1053                 return ret;
1054         }
1055
1056         *ptr = edev;
1057         devres_add(dev, ptr);
1058
1059         return 0;
1060 }
1061 EXPORT_SYMBOL_GPL(devm_extcon_dev_register);
1062
1063 /**
1064  * devm_extcon_dev_unregister() - Resource-managed extcon_dev_unregister()
1065  * @dev:        device the extcon belongs to
1066  * @edev:       the extcon device to unregister
1067  *
1068  * Unregister extcon device that is registered with devm_extcon_dev_register()
1069  * function.
1070  */
1071 void devm_extcon_dev_unregister(struct device *dev, struct extcon_dev *edev)
1072 {
1073         WARN_ON(devres_release(dev, devm_extcon_dev_unreg,
1074                                devm_extcon_dev_match, edev));
1075 }
1076 EXPORT_SYMBOL_GPL(devm_extcon_dev_unregister);
1077
1078 #ifdef CONFIG_OF
1079 /*
1080  * extcon_get_edev_by_phandle - Get the extcon device from devicetree
1081  * @dev - instance to the given device
1082  * @index - index into list of extcon_dev
1083  *
1084  * return the instance of extcon device
1085  */
1086 struct extcon_dev *extcon_get_edev_by_phandle(struct device *dev, int index)
1087 {
1088         struct device_node *node;
1089         struct extcon_dev *edev;
1090
1091         if (!dev)
1092                 return ERR_PTR(-EINVAL);
1093
1094         if (!dev->of_node) {
1095                 dev_err(dev, "device does not have a device node entry\n");
1096                 return ERR_PTR(-EINVAL);
1097         }
1098
1099         node = of_parse_phandle(dev->of_node, "extcon", index);
1100         if (!node) {
1101                 dev_err(dev, "failed to get phandle in %s node\n",
1102                         dev->of_node->full_name);
1103                 return ERR_PTR(-ENODEV);
1104         }
1105
1106         mutex_lock(&extcon_dev_list_lock);
1107         list_for_each_entry(edev, &extcon_dev_list, entry) {
1108                 if (edev->dev.parent && edev->dev.parent->of_node == node) {
1109                         mutex_unlock(&extcon_dev_list_lock);
1110                         return edev;
1111                 }
1112         }
1113         mutex_unlock(&extcon_dev_list_lock);
1114
1115         return ERR_PTR(-EPROBE_DEFER);
1116 }
1117 #else
1118 struct extcon_dev *extcon_get_edev_by_phandle(struct device *dev, int index)
1119 {
1120         return ERR_PTR(-ENOSYS);
1121 }
1122 #endif /* CONFIG_OF */
1123 EXPORT_SYMBOL_GPL(extcon_get_edev_by_phandle);
1124
1125 /**
1126  * extcon_get_edev_name() - Get the name of the extcon device.
1127  * @edev:       the extcon device
1128  */
1129 const char *extcon_get_edev_name(struct extcon_dev *edev)
1130 {
1131         return !edev ? NULL : edev->name;
1132 }
1133
1134 static int __init extcon_class_init(void)
1135 {
1136         return create_extcon_class();
1137 }
1138 module_init(extcon_class_init);
1139
1140 static void __exit extcon_class_exit(void)
1141 {
1142 #if defined(CONFIG_ANDROID)
1143         class_compat_unregister(switch_class);
1144 #endif
1145         class_destroy(extcon_class);
1146 }
1147 module_exit(extcon_class_exit);
1148
1149 MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>");
1150 MODULE_AUTHOR("Mike Lockwood <lockwood@android.com>");
1151 MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>");
1152 MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
1153 MODULE_DESCRIPTION("External connector (extcon) class driver");
1154 MODULE_LICENSE("GPL");