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