blob: 946a3188b2b78a4014f84e93588e8423c41501c0 [file] [log] [blame]
MyungJoo Hamde55d872012-04-20 14:16:22 +09001/*
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>
Mark Brown9baf3222012-08-16 20:03:21 +010033#include <linux/sysfs.h>
MyungJoo Hamde55d872012-04-20 14:16:22 +090034
MyungJoo Ham806d9dd2012-04-20 14:16:25 +090035/*
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 */
44const char *extcon_cable_name[] = {
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",
Mark Brown0e1507c2012-05-02 10:38:51 +010064 [EXTCON_MECHANICAL] = "Mechanical",
MyungJoo Ham806d9dd2012-04-20 14:16:25 +090065
66 NULL,
67};
68
Mark Brownbe3a07f2012-06-05 16:14:38 +010069static struct class *extcon_class;
MyungJoo Ham449a2bf2012-04-23 20:19:57 +090070#if defined(CONFIG_ANDROID)
MyungJoo Hamde55d872012-04-20 14:16:22 +090071static struct class_compat *switch_class;
MyungJoo Ham449a2bf2012-04-23 20:19:57 +090072#endif /* CONFIG_ANDROID */
MyungJoo Hamde55d872012-04-20 14:16:22 +090073
Donggeun Kim74c5d092012-04-20 14:16:24 +090074static LIST_HEAD(extcon_dev_list);
75static DEFINE_MUTEX(extcon_dev_list_lock);
76
MyungJoo Hambde68e62012-04-20 14:16:26 +090077/**
78 * check_mutually_exclusive - Check if new_state violates mutually_exclusive
79 * condition.
80 * @edev: the extcon device
81 * @new_state: new cable attach status for @edev
82 *
83 * Returns 0 if nothing violates. Returns the index + 1 for the first
84 * violated condition.
85 */
86static int check_mutually_exclusive(struct extcon_dev *edev, u32 new_state)
87{
88 int i = 0;
89
90 if (!edev->mutually_exclusive)
91 return 0;
92
93 for (i = 0; edev->mutually_exclusive[i]; i++) {
94 int count = 0, j;
95 u32 correspondants = new_state & edev->mutually_exclusive[i];
96 u32 exp = 1;
97
98 for (j = 0; j < 32; j++) {
99 if (exp & correspondants)
100 count++;
101 if (count > 1)
102 return i + 1;
103 exp <<= 1;
104 }
105 }
106
107 return 0;
108}
109
MyungJoo Hamde55d872012-04-20 14:16:22 +0900110static ssize_t state_show(struct device *dev, struct device_attribute *attr,
111 char *buf)
112{
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900113 int i, count = 0;
MyungJoo Hamde55d872012-04-20 14:16:22 +0900114 struct extcon_dev *edev = (struct extcon_dev *) dev_get_drvdata(dev);
115
116 if (edev->print_state) {
117 int ret = edev->print_state(edev, buf);
118
119 if (ret >= 0)
120 return ret;
121 /* Use default if failed */
122 }
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900123
124 if (edev->max_supported == 0)
125 return sprintf(buf, "%u\n", edev->state);
126
127 for (i = 0; i < SUPPORTED_CABLE_MAX; i++) {
128 if (!edev->supported_cable[i])
129 break;
130 count += sprintf(buf + count, "%s=%d\n",
131 edev->supported_cable[i],
132 !!(edev->state & (1 << i)));
133 }
134
135 return count;
136}
137
MyungJoo Hambde68e62012-04-20 14:16:26 +0900138int extcon_set_state(struct extcon_dev *edev, u32 state);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900139static ssize_t state_store(struct device *dev, struct device_attribute *attr,
140 const char *buf, size_t count)
141{
142 u32 state;
143 ssize_t ret = 0;
144 struct extcon_dev *edev = (struct extcon_dev *) dev_get_drvdata(dev);
145
146 ret = sscanf(buf, "0x%x", &state);
147 if (ret == 0)
148 ret = -EINVAL;
149 else
MyungJoo Hambde68e62012-04-20 14:16:26 +0900150 ret = extcon_set_state(edev, state);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900151
152 if (ret < 0)
153 return ret;
154
155 return count;
MyungJoo Hamde55d872012-04-20 14:16:22 +0900156}
157
158static ssize_t name_show(struct device *dev, struct device_attribute *attr,
159 char *buf)
160{
161 struct extcon_dev *edev = (struct extcon_dev *) dev_get_drvdata(dev);
162
163 /* Optional callback given by the user */
164 if (edev->print_name) {
165 int ret = edev->print_name(edev, buf);
166 if (ret >= 0)
167 return ret;
168 }
169
170 return sprintf(buf, "%s\n", dev_name(edev->dev));
171}
172
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900173static ssize_t cable_name_show(struct device *dev,
174 struct device_attribute *attr, char *buf)
175{
176 struct extcon_cable *cable = container_of(attr, struct extcon_cable,
177 attr_name);
178
179 return sprintf(buf, "%s\n",
180 cable->edev->supported_cable[cable->cable_index]);
181}
182
183static ssize_t cable_state_show(struct device *dev,
184 struct device_attribute *attr, char *buf)
185{
186 struct extcon_cable *cable = container_of(attr, struct extcon_cable,
187 attr_state);
188
189 return sprintf(buf, "%d\n",
190 extcon_get_cable_state_(cable->edev,
191 cable->cable_index));
192}
193
194static ssize_t cable_state_store(struct device *dev,
195 struct device_attribute *attr, const char *buf,
196 size_t count)
197{
198 struct extcon_cable *cable = container_of(attr, struct extcon_cable,
199 attr_state);
200 int ret, state;
201
202 ret = sscanf(buf, "%d", &state);
203 if (ret == 0)
204 ret = -EINVAL;
205 else
206 ret = extcon_set_cable_state_(cable->edev, cable->cable_index,
207 state);
208
209 if (ret < 0)
210 return ret;
211 return count;
212}
213
MyungJoo Hamde55d872012-04-20 14:16:22 +0900214/**
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900215 * extcon_update_state() - Update the cable attach states of the extcon device
216 * only for the masked bits.
MyungJoo Hamde55d872012-04-20 14:16:22 +0900217 * @edev: the extcon device
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900218 * @mask: the bit mask to designate updated bits.
MyungJoo Hamde55d872012-04-20 14:16:22 +0900219 * @state: new cable attach status for @edev
220 *
221 * Changing the state sends uevent with environment variable containing
222 * the name of extcon device (envp[0]) and the state output (envp[1]).
223 * Tizen uses this format for extcon device to get events from ports.
224 * Android uses this format as well.
Donggeun Kim74c5d092012-04-20 14:16:24 +0900225 *
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900226 * Note that the notifier provides which bits are changed in the state
227 * variable with the val parameter (second) to the callback.
MyungJoo Hamde55d872012-04-20 14:16:22 +0900228 */
MyungJoo Hambde68e62012-04-20 14:16:26 +0900229int extcon_update_state(struct extcon_dev *edev, u32 mask, u32 state)
MyungJoo Hamde55d872012-04-20 14:16:22 +0900230{
231 char name_buf[120];
232 char state_buf[120];
233 char *prop_buf;
234 char *envp[3];
235 int env_offset = 0;
236 int length;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900237 unsigned long flags;
MyungJoo Hamde55d872012-04-20 14:16:22 +0900238
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900239 spin_lock_irqsave(&edev->lock, flags);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900240
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900241 if (edev->state != ((edev->state & ~mask) | (state & mask))) {
242 u32 old_state = edev->state;
Donggeun Kim74c5d092012-04-20 14:16:24 +0900243
MyungJoo Hambde68e62012-04-20 14:16:26 +0900244 if (check_mutually_exclusive(edev, (edev->state & ~mask) |
245 (state & mask))) {
246 spin_unlock_irqrestore(&edev->lock, flags);
247 return -EPERM;
248 }
249
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900250 edev->state &= ~mask;
251 edev->state |= state & mask;
252
253 raw_notifier_call_chain(&edev->nh, old_state, edev);
254
255 /* This could be in interrupt handler */
256 prop_buf = (char *)get_zeroed_page(GFP_ATOMIC);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900257 if (prop_buf) {
258 length = name_show(edev->dev, NULL, prop_buf);
259 if (length > 0) {
260 if (prop_buf[length - 1] == '\n')
261 prop_buf[length - 1] = 0;
262 snprintf(name_buf, sizeof(name_buf),
263 "NAME=%s", prop_buf);
264 envp[env_offset++] = name_buf;
265 }
266 length = state_show(edev->dev, NULL, prop_buf);
267 if (length > 0) {
268 if (prop_buf[length - 1] == '\n')
269 prop_buf[length - 1] = 0;
270 snprintf(state_buf, sizeof(state_buf),
271 "STATE=%s", prop_buf);
272 envp[env_offset++] = state_buf;
273 }
274 envp[env_offset] = NULL;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900275 /* Unlock early before uevent */
276 spin_unlock_irqrestore(&edev->lock, flags);
277
MyungJoo Hamde55d872012-04-20 14:16:22 +0900278 kobject_uevent_env(&edev->dev->kobj, KOBJ_CHANGE, envp);
279 free_page((unsigned long)prop_buf);
280 } else {
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900281 /* Unlock early before uevent */
282 spin_unlock_irqrestore(&edev->lock, flags);
283
MyungJoo Hamde55d872012-04-20 14:16:22 +0900284 dev_err(edev->dev, "out of memory in extcon_set_state\n");
285 kobject_uevent(&edev->dev->kobj, KOBJ_CHANGE);
286 }
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900287 } else {
288 /* No changes */
289 spin_unlock_irqrestore(&edev->lock, flags);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900290 }
MyungJoo Hambde68e62012-04-20 14:16:26 +0900291
292 return 0;
MyungJoo Hamde55d872012-04-20 14:16:22 +0900293}
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900294EXPORT_SYMBOL_GPL(extcon_update_state);
295
296/**
297 * extcon_set_state() - Set the cable attach states of the extcon device.
298 * @edev: the extcon device
299 * @state: new cable attach status for @edev
300 *
301 * Note that notifier provides which bits are changed in the state
302 * variable with the val parameter (second) to the callback.
303 */
MyungJoo Hambde68e62012-04-20 14:16:26 +0900304int extcon_set_state(struct extcon_dev *edev, u32 state)
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900305{
MyungJoo Hambde68e62012-04-20 14:16:26 +0900306 return extcon_update_state(edev, 0xffffffff, state);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900307}
MyungJoo Hamde55d872012-04-20 14:16:22 +0900308EXPORT_SYMBOL_GPL(extcon_set_state);
309
Donggeun Kim74c5d092012-04-20 14:16:24 +0900310/**
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900311 * extcon_find_cable_index() - Get the cable index based on the cable name.
312 * @edev: the extcon device that has the cable.
313 * @cable_name: cable name to be searched.
314 *
315 * Note that accessing a cable state based on cable_index is faster than
316 * cable_name because using cable_name induces a loop with strncmp().
317 * Thus, when get/set_cable_state is repeatedly used, using cable_index
318 * is recommended.
319 */
320int extcon_find_cable_index(struct extcon_dev *edev, const char *cable_name)
321{
322 int i;
323
324 if (edev->supported_cable) {
325 for (i = 0; edev->supported_cable[i]; i++) {
326 if (!strncmp(edev->supported_cable[i],
327 cable_name, CABLE_NAME_MAX))
328 return i;
329 }
330 }
331
332 return -EINVAL;
333}
334EXPORT_SYMBOL_GPL(extcon_find_cable_index);
335
336/**
337 * extcon_get_cable_state_() - Get the status of a specific cable.
338 * @edev: the extcon device that has the cable.
339 * @index: cable index that can be retrieved by extcon_find_cable_index().
340 */
341int extcon_get_cable_state_(struct extcon_dev *edev, int index)
342{
343 if (index < 0 || (edev->max_supported && edev->max_supported <= index))
344 return -EINVAL;
345
346 return !!(edev->state & (1 << index));
347}
348EXPORT_SYMBOL_GPL(extcon_get_cable_state_);
349
350/**
351 * extcon_get_cable_state() - Get the status of a specific cable.
352 * @edev: the extcon device that has the cable.
353 * @cable_name: cable name.
354 *
355 * Note that this is slower than extcon_get_cable_state_.
356 */
357int extcon_get_cable_state(struct extcon_dev *edev, const char *cable_name)
358{
359 return extcon_get_cable_state_(edev, extcon_find_cable_index
360 (edev, cable_name));
361}
362EXPORT_SYMBOL_GPL(extcon_get_cable_state);
363
364/**
365 * extcon_get_cable_state_() - Set the status of a specific cable.
366 * @edev: the extcon device that has the cable.
367 * @index: cable index that can be retrieved by extcon_find_cable_index().
368 * @cable_state: the new cable status. The default semantics is
369 * true: attached / false: detached.
370 */
371int extcon_set_cable_state_(struct extcon_dev *edev,
372 int index, bool cable_state)
373{
374 u32 state;
375
376 if (index < 0 || (edev->max_supported && edev->max_supported <= index))
377 return -EINVAL;
378
379 state = cable_state ? (1 << index) : 0;
MyungJoo Hambde68e62012-04-20 14:16:26 +0900380 return extcon_update_state(edev, 1 << index, state);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900381}
382EXPORT_SYMBOL_GPL(extcon_set_cable_state_);
383
384/**
385 * extcon_get_cable_state() - Set the status of a specific cable.
386 * @edev: the extcon device that has the cable.
387 * @cable_name: cable name.
388 * @cable_state: the new cable status. The default semantics is
389 * true: attached / false: detached.
390 *
391 * Note that this is slower than extcon_set_cable_state_.
392 */
393int extcon_set_cable_state(struct extcon_dev *edev,
394 const char *cable_name, bool cable_state)
395{
396 return extcon_set_cable_state_(edev, extcon_find_cable_index
397 (edev, cable_name), cable_state);
398}
399EXPORT_SYMBOL_GPL(extcon_set_cable_state);
400
401/**
Donggeun Kim74c5d092012-04-20 14:16:24 +0900402 * extcon_get_extcon_dev() - Get the extcon device instance from the name
403 * @extcon_name: The extcon name provided with extcon_dev_register()
404 */
405struct extcon_dev *extcon_get_extcon_dev(const char *extcon_name)
406{
407 struct extcon_dev *sd;
408
409 mutex_lock(&extcon_dev_list_lock);
410 list_for_each_entry(sd, &extcon_dev_list, entry) {
411 if (!strcmp(sd->name, extcon_name))
412 goto out;
413 }
414 sd = NULL;
415out:
416 mutex_unlock(&extcon_dev_list_lock);
417 return sd;
418}
419EXPORT_SYMBOL_GPL(extcon_get_extcon_dev);
420
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900421static int _call_per_cable(struct notifier_block *nb, unsigned long val,
422 void *ptr)
423{
424 struct extcon_specific_cable_nb *obj = container_of(nb,
425 struct extcon_specific_cable_nb, internal_nb);
426 struct extcon_dev *edev = ptr;
427
428 if ((val & (1 << obj->cable_index)) !=
429 (edev->state & (1 << obj->cable_index))) {
Chanwoo Choif4cce692012-04-27 15:17:28 +0900430 bool cable_state = true;
431
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900432 obj->previous_value = val;
Chanwoo Choif4cce692012-04-27 15:17:28 +0900433
434 if (val & (1 << obj->cable_index))
435 cable_state = false;
436
437 return obj->user_nb->notifier_call(obj->user_nb,
438 cable_state, ptr);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900439 }
440
441 return NOTIFY_OK;
442}
443
444/**
445 * extcon_register_interest() - Register a notifier for a state change of a
Peter Meerwaldc338bb02012-08-23 09:11:54 +0900446 * specific cable, not an entier set of cables of a
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900447 * extcon device.
448 * @obj: an empty extcon_specific_cable_nb object to be returned.
449 * @extcon_name: the name of extcon device.
450 * @cable_name: the target cable name.
451 * @nb: the notifier block to get notified.
452 *
453 * Provide an empty extcon_specific_cable_nb. extcon_register_interest() sets
454 * the struct for you.
455 *
456 * extcon_register_interest is a helper function for those who want to get
457 * notification for a single specific cable's status change. If a user wants
458 * to get notification for any changes of all cables of a extcon device,
459 * he/she should use the general extcon_register_notifier().
460 *
461 * Note that the second parameter given to the callback of nb (val) is
462 * "old_state", not the current state. The current state can be retrieved
463 * by looking at the third pameter (edev pointer)'s state value.
464 */
465int extcon_register_interest(struct extcon_specific_cable_nb *obj,
466 const char *extcon_name, const char *cable_name,
467 struct notifier_block *nb)
468{
469 if (!obj || !extcon_name || !cable_name || !nb)
470 return -EINVAL;
471
472 obj->edev = extcon_get_extcon_dev(extcon_name);
473 if (!obj->edev)
474 return -ENODEV;
475
476 obj->cable_index = extcon_find_cable_index(obj->edev, cable_name);
477 if (obj->cable_index < 0)
478 return -ENODEV;
479
480 obj->user_nb = nb;
481
482 obj->internal_nb.notifier_call = _call_per_cable;
483
484 return raw_notifier_chain_register(&obj->edev->nh, &obj->internal_nb);
485}
486
487/**
488 * extcon_unregister_interest() - Unregister the notifier registered by
489 * extcon_register_interest().
490 * @obj: the extcon_specific_cable_nb object returned by
491 * extcon_register_interest().
492 */
493int extcon_unregister_interest(struct extcon_specific_cable_nb *obj)
494{
495 if (!obj)
496 return -EINVAL;
497
498 return raw_notifier_chain_unregister(&obj->edev->nh, &obj->internal_nb);
499}
500
Donggeun Kim74c5d092012-04-20 14:16:24 +0900501/**
Peter Meerwaldc338bb02012-08-23 09:11:54 +0900502 * extcon_register_notifier() - Register a notifiee to get notified by
Donggeun Kim74c5d092012-04-20 14:16:24 +0900503 * any attach status changes from the extcon.
504 * @edev: the extcon device.
505 * @nb: a notifier block to be registered.
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900506 *
507 * Note that the second parameter given to the callback of nb (val) is
508 * "old_state", not the current state. The current state can be retrieved
509 * by looking at the third pameter (edev pointer)'s state value.
Donggeun Kim74c5d092012-04-20 14:16:24 +0900510 */
511int extcon_register_notifier(struct extcon_dev *edev,
512 struct notifier_block *nb)
513{
514 return raw_notifier_chain_register(&edev->nh, nb);
515}
516EXPORT_SYMBOL_GPL(extcon_register_notifier);
517
518/**
Peter Meerwaldc338bb02012-08-23 09:11:54 +0900519 * extcon_unregister_notifier() - Unregister a notifiee from the extcon device.
Donggeun Kim74c5d092012-04-20 14:16:24 +0900520 * @edev: the extcon device.
521 * @nb: a registered notifier block to be unregistered.
522 */
523int extcon_unregister_notifier(struct extcon_dev *edev,
524 struct notifier_block *nb)
525{
526 return raw_notifier_chain_unregister(&edev->nh, nb);
527}
528EXPORT_SYMBOL_GPL(extcon_unregister_notifier);
529
MyungJoo Hamde55d872012-04-20 14:16:22 +0900530static struct device_attribute extcon_attrs[] = {
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900531 __ATTR(state, S_IRUGO | S_IWUSR, state_show, state_store),
MyungJoo Hamde55d872012-04-20 14:16:22 +0900532 __ATTR_RO(name),
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900533 __ATTR_NULL,
MyungJoo Hamde55d872012-04-20 14:16:22 +0900534};
535
536static int create_extcon_class(void)
537{
538 if (!extcon_class) {
539 extcon_class = class_create(THIS_MODULE, "extcon");
540 if (IS_ERR(extcon_class))
541 return PTR_ERR(extcon_class);
542 extcon_class->dev_attrs = extcon_attrs;
543
MyungJoo Ham449a2bf2012-04-23 20:19:57 +0900544#if defined(CONFIG_ANDROID)
MyungJoo Hamde55d872012-04-20 14:16:22 +0900545 switch_class = class_compat_register("switch");
546 if (WARN(!switch_class, "cannot allocate"))
547 return -ENOMEM;
MyungJoo Ham449a2bf2012-04-23 20:19:57 +0900548#endif /* CONFIG_ANDROID */
MyungJoo Hamde55d872012-04-20 14:16:22 +0900549 }
550
551 return 0;
552}
553
554static void extcon_cleanup(struct extcon_dev *edev, bool skip)
555{
Donggeun Kim74c5d092012-04-20 14:16:24 +0900556 mutex_lock(&extcon_dev_list_lock);
557 list_del(&edev->entry);
558 mutex_unlock(&extcon_dev_list_lock);
559
MyungJoo Hamde55d872012-04-20 14:16:22 +0900560 if (!skip && get_device(edev->dev)) {
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900561 int index;
562
MyungJoo Hambde68e62012-04-20 14:16:26 +0900563 if (edev->mutually_exclusive && edev->max_supported) {
564 for (index = 0; edev->mutually_exclusive[index];
565 index++)
566 kfree(edev->d_attrs_muex[index].attr.name);
567 kfree(edev->d_attrs_muex);
568 kfree(edev->attrs_muex);
569 }
570
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900571 for (index = 0; index < edev->max_supported; index++)
572 kfree(edev->cables[index].attr_g.name);
573
574 if (edev->max_supported) {
575 kfree(edev->extcon_dev_type.groups);
576 kfree(edev->cables);
577 }
578
MyungJoo Hamde55d872012-04-20 14:16:22 +0900579 device_unregister(edev->dev);
580 put_device(edev->dev);
581 }
582
583 kfree(edev->dev);
584}
585
586static void extcon_dev_release(struct device *dev)
587{
588 struct extcon_dev *edev = (struct extcon_dev *) dev_get_drvdata(dev);
589
590 extcon_cleanup(edev, true);
591}
592
MyungJoo Hambde68e62012-04-20 14:16:26 +0900593static const char *muex_name = "mutually_exclusive";
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900594static void dummy_sysfs_dev_release(struct device *dev)
595{
596}
597
MyungJoo Hamde55d872012-04-20 14:16:22 +0900598/**
599 * extcon_dev_register() - Register a new extcon device
600 * @edev : the new extcon device (should be allocated before calling)
601 * @dev : the parent device for this extcon device.
602 *
603 * Among the members of edev struct, please set the "user initializing data"
604 * in any case and set the "optional callbacks" if required. However, please
605 * do not set the values of "internal data", which are initialized by
606 * this function.
607 */
608int extcon_dev_register(struct extcon_dev *edev, struct device *dev)
609{
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900610 int ret, index = 0;
MyungJoo Hamde55d872012-04-20 14:16:22 +0900611
612 if (!extcon_class) {
613 ret = create_extcon_class();
614 if (ret < 0)
615 return ret;
616 }
617
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900618 if (edev->supported_cable) {
619 /* Get size of array */
620 for (index = 0; edev->supported_cable[index]; index++)
621 ;
622 edev->max_supported = index;
623 } else {
624 edev->max_supported = 0;
625 }
626
627 if (index > SUPPORTED_CABLE_MAX) {
628 dev_err(edev->dev, "extcon: maximum number of supported cables exceeded.\n");
629 return -EINVAL;
630 }
631
MyungJoo Hamde55d872012-04-20 14:16:22 +0900632 edev->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
Dan Carpentera1d26ac2012-04-25 11:47:02 +0300633 if (!edev->dev)
634 return -ENOMEM;
MyungJoo Hamde55d872012-04-20 14:16:22 +0900635 edev->dev->parent = dev;
636 edev->dev->class = extcon_class;
637 edev->dev->release = extcon_dev_release;
638
639 dev_set_name(edev->dev, edev->name ? edev->name : dev_name(dev));
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900640
641 if (edev->max_supported) {
642 char buf[10];
643 char *str;
644 struct extcon_cable *cable;
645
646 edev->cables = kzalloc(sizeof(struct extcon_cable) *
647 edev->max_supported, GFP_KERNEL);
648 if (!edev->cables) {
649 ret = -ENOMEM;
650 goto err_sysfs_alloc;
651 }
652 for (index = 0; index < edev->max_supported; index++) {
653 cable = &edev->cables[index];
654
655 snprintf(buf, 10, "cable.%d", index);
656 str = kzalloc(sizeof(char) * (strlen(buf) + 1),
657 GFP_KERNEL);
658 if (!str) {
659 for (index--; index >= 0; index--) {
660 cable = &edev->cables[index];
661 kfree(cable->attr_g.name);
662 }
663 ret = -ENOMEM;
664
665 goto err_alloc_cables;
666 }
667 strcpy(str, buf);
668
669 cable->edev = edev;
670 cable->cable_index = index;
671 cable->attrs[0] = &cable->attr_name.attr;
672 cable->attrs[1] = &cable->attr_state.attr;
673 cable->attrs[2] = NULL;
674 cable->attr_g.name = str;
675 cable->attr_g.attrs = cable->attrs;
676
Mark Brown9baf3222012-08-16 20:03:21 +0100677 sysfs_attr_init(&cable->attr_name.attr);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900678 cable->attr_name.attr.name = "name";
679 cable->attr_name.attr.mode = 0444;
680 cable->attr_name.show = cable_name_show;
681
Mark Brown9baf3222012-08-16 20:03:21 +0100682 sysfs_attr_init(&cable->attr_state.attr);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900683 cable->attr_state.attr.name = "state";
684 cable->attr_state.attr.mode = 0644;
685 cable->attr_state.show = cable_state_show;
686 cable->attr_state.store = cable_state_store;
687 }
688 }
689
MyungJoo Hambde68e62012-04-20 14:16:26 +0900690 if (edev->max_supported && edev->mutually_exclusive) {
691 char buf[80];
692 char *name;
693
694 /* Count the size of mutually_exclusive array */
695 for (index = 0; edev->mutually_exclusive[index]; index++)
696 ;
697
698 edev->attrs_muex = kzalloc(sizeof(struct attribute *) *
699 (index + 1), GFP_KERNEL);
700 if (!edev->attrs_muex) {
701 ret = -ENOMEM;
702 goto err_muex;
703 }
704
705 edev->d_attrs_muex = kzalloc(sizeof(struct device_attribute) *
706 index, GFP_KERNEL);
707 if (!edev->d_attrs_muex) {
708 ret = -ENOMEM;
709 kfree(edev->attrs_muex);
710 goto err_muex;
711 }
712
713 for (index = 0; edev->mutually_exclusive[index]; index++) {
714 sprintf(buf, "0x%x", edev->mutually_exclusive[index]);
715 name = kzalloc(sizeof(char) * (strlen(buf) + 1),
716 GFP_KERNEL);
717 if (!name) {
718 for (index--; index >= 0; index--) {
719 kfree(edev->d_attrs_muex[index].attr.
720 name);
721 }
722 kfree(edev->d_attrs_muex);
723 kfree(edev->attrs_muex);
724 ret = -ENOMEM;
725 goto err_muex;
726 }
727 strcpy(name, buf);
Mark Brown9baf3222012-08-16 20:03:21 +0100728 sysfs_attr_init(&edev->d_attrs_muex[index].attr);
MyungJoo Hambde68e62012-04-20 14:16:26 +0900729 edev->d_attrs_muex[index].attr.name = name;
730 edev->d_attrs_muex[index].attr.mode = 0000;
731 edev->attrs_muex[index] = &edev->d_attrs_muex[index]
732 .attr;
733 }
734 edev->attr_g_muex.name = muex_name;
735 edev->attr_g_muex.attrs = edev->attrs_muex;
736
737 }
738
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900739 if (edev->max_supported) {
740 edev->extcon_dev_type.groups =
741 kzalloc(sizeof(struct attribute_group *) *
MyungJoo Hambde68e62012-04-20 14:16:26 +0900742 (edev->max_supported + 2), GFP_KERNEL);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900743 if (!edev->extcon_dev_type.groups) {
744 ret = -ENOMEM;
745 goto err_alloc_groups;
746 }
747
748 edev->extcon_dev_type.name = dev_name(edev->dev);
749 edev->extcon_dev_type.release = dummy_sysfs_dev_release;
750
751 for (index = 0; index < edev->max_supported; index++)
752 edev->extcon_dev_type.groups[index] =
753 &edev->cables[index].attr_g;
MyungJoo Hambde68e62012-04-20 14:16:26 +0900754 if (edev->mutually_exclusive)
755 edev->extcon_dev_type.groups[index] =
756 &edev->attr_g_muex;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900757
758 edev->dev->type = &edev->extcon_dev_type;
759 }
760
MyungJoo Hamde55d872012-04-20 14:16:22 +0900761 ret = device_register(edev->dev);
762 if (ret) {
763 put_device(edev->dev);
764 goto err_dev;
765 }
MyungJoo Ham449a2bf2012-04-23 20:19:57 +0900766#if defined(CONFIG_ANDROID)
MyungJoo Hamde55d872012-04-20 14:16:22 +0900767 if (switch_class)
768 ret = class_compat_create_link(switch_class, edev->dev,
Mark Brownc3b15452012-06-05 16:43:53 +0100769 NULL);
MyungJoo Ham449a2bf2012-04-23 20:19:57 +0900770#endif /* CONFIG_ANDROID */
MyungJoo Hamde55d872012-04-20 14:16:22 +0900771
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900772 spin_lock_init(&edev->lock);
773
Donggeun Kim74c5d092012-04-20 14:16:24 +0900774 RAW_INIT_NOTIFIER_HEAD(&edev->nh);
775
MyungJoo Hamde55d872012-04-20 14:16:22 +0900776 dev_set_drvdata(edev->dev, edev);
777 edev->state = 0;
Donggeun Kim74c5d092012-04-20 14:16:24 +0900778
779 mutex_lock(&extcon_dev_list_lock);
780 list_add(&edev->entry, &extcon_dev_list);
781 mutex_unlock(&extcon_dev_list_lock);
782
MyungJoo Hamde55d872012-04-20 14:16:22 +0900783 return 0;
784
785err_dev:
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900786 if (edev->max_supported)
787 kfree(edev->extcon_dev_type.groups);
788err_alloc_groups:
MyungJoo Hambde68e62012-04-20 14:16:26 +0900789 if (edev->max_supported && edev->mutually_exclusive) {
790 for (index = 0; edev->mutually_exclusive[index]; index++)
791 kfree(edev->d_attrs_muex[index].attr.name);
792 kfree(edev->d_attrs_muex);
793 kfree(edev->attrs_muex);
794 }
795err_muex:
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900796 for (index = 0; index < edev->max_supported; index++)
797 kfree(edev->cables[index].attr_g.name);
798err_alloc_cables:
799 if (edev->max_supported)
800 kfree(edev->cables);
801err_sysfs_alloc:
MyungJoo Hamde55d872012-04-20 14:16:22 +0900802 kfree(edev->dev);
803 return ret;
804}
805EXPORT_SYMBOL_GPL(extcon_dev_register);
806
807/**
808 * extcon_dev_unregister() - Unregister the extcon device.
Peter Meerwaldc338bb02012-08-23 09:11:54 +0900809 * @edev: the extcon device instance to be unregistered.
MyungJoo Hamde55d872012-04-20 14:16:22 +0900810 *
811 * Note that this does not call kfree(edev) because edev was not allocated
812 * by this class.
813 */
814void extcon_dev_unregister(struct extcon_dev *edev)
815{
816 extcon_cleanup(edev, false);
817}
818EXPORT_SYMBOL_GPL(extcon_dev_unregister);
819
820static int __init extcon_class_init(void)
821{
822 return create_extcon_class();
823}
824module_init(extcon_class_init);
825
826static void __exit extcon_class_exit(void)
827{
828 class_destroy(extcon_class);
829}
830module_exit(extcon_class_exit);
831
832MODULE_AUTHOR("Mike Lockwood <lockwood@android.com>");
833MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>");
834MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
835MODULE_DESCRIPTION("External connector (extcon) class driver");
836MODULE_LICENSE("GPL");