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