blob: 1e1a3f17a7828895df6d2cfaac8f984ce393c093 [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 */
anish kumar0cf6ad82012-08-30 00:35:09 +053044const char extcon_cable_name[][CABLE_NAME_MAX + 1] = {
MyungJoo Ham806d9dd2012-04-20 14:16:25 +090045 [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
Mark Brownbe3a07f2012-06-05 16:14:38 +010067static struct class *extcon_class;
MyungJoo Ham449a2bf2012-04-23 20:19:57 +090068#if defined(CONFIG_ANDROID)
MyungJoo Hamde55d872012-04-20 14:16:22 +090069static struct class_compat *switch_class;
MyungJoo Ham449a2bf2012-04-23 20:19:57 +090070#endif /* CONFIG_ANDROID */
MyungJoo Hamde55d872012-04-20 14:16:22 +090071
Donggeun Kim74c5d092012-04-20 14:16:24 +090072static LIST_HEAD(extcon_dev_list);
73static DEFINE_MUTEX(extcon_dev_list_lock);
74
MyungJoo Hambde68e62012-04-20 14:16:26 +090075/**
76 * check_mutually_exclusive - Check if new_state violates mutually_exclusive
77 * condition.
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++) {
92 int count = 0, j;
93 u32 correspondants = new_state & edev->mutually_exclusive[i];
94 u32 exp = 1;
95
96 for (j = 0; j < 32; j++) {
97 if (exp & correspondants)
98 count++;
99 if (count > 1)
100 return i + 1;
101 exp <<= 1;
102 }
103 }
104
105 return 0;
106}
107
MyungJoo Hamde55d872012-04-20 14:16:22 +0900108static ssize_t state_show(struct device *dev, struct device_attribute *attr,
109 char *buf)
110{
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900111 int i, count = 0;
MyungJoo Hamde55d872012-04-20 14:16:22 +0900112 struct extcon_dev *edev = (struct extcon_dev *) dev_get_drvdata(dev);
113
114 if (edev->print_state) {
115 int ret = edev->print_state(edev, buf);
116
117 if (ret >= 0)
118 return ret;
119 /* Use default if failed */
120 }
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900121
122 if (edev->max_supported == 0)
123 return sprintf(buf, "%u\n", edev->state);
124
125 for (i = 0; i < SUPPORTED_CABLE_MAX; i++) {
126 if (!edev->supported_cable[i])
127 break;
128 count += sprintf(buf + count, "%s=%d\n",
129 edev->supported_cable[i],
130 !!(edev->state & (1 << i)));
131 }
132
133 return count;
134}
135
MyungJoo Hambde68e62012-04-20 14:16:26 +0900136int extcon_set_state(struct extcon_dev *edev, u32 state);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900137static ssize_t state_store(struct device *dev, struct device_attribute *attr,
138 const char *buf, size_t count)
139{
140 u32 state;
141 ssize_t ret = 0;
142 struct extcon_dev *edev = (struct extcon_dev *) dev_get_drvdata(dev);
143
144 ret = sscanf(buf, "0x%x", &state);
145 if (ret == 0)
146 ret = -EINVAL;
147 else
MyungJoo Hambde68e62012-04-20 14:16:26 +0900148 ret = extcon_set_state(edev, state);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900149
150 if (ret < 0)
151 return ret;
152
153 return count;
MyungJoo Hamde55d872012-04-20 14:16:22 +0900154}
155
156static ssize_t name_show(struct device *dev, struct device_attribute *attr,
157 char *buf)
158{
159 struct extcon_dev *edev = (struct extcon_dev *) dev_get_drvdata(dev);
160
161 /* Optional callback given by the user */
162 if (edev->print_name) {
163 int ret = edev->print_name(edev, buf);
164 if (ret >= 0)
165 return ret;
166 }
167
168 return sprintf(buf, "%s\n", dev_name(edev->dev));
169}
170
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900171static ssize_t cable_name_show(struct device *dev,
172 struct device_attribute *attr, char *buf)
173{
174 struct extcon_cable *cable = container_of(attr, struct extcon_cable,
175 attr_name);
176
177 return sprintf(buf, "%s\n",
178 cable->edev->supported_cable[cable->cable_index]);
179}
180
181static ssize_t cable_state_show(struct device *dev,
182 struct device_attribute *attr, char *buf)
183{
184 struct extcon_cable *cable = container_of(attr, struct extcon_cable,
185 attr_state);
186
187 return sprintf(buf, "%d\n",
188 extcon_get_cable_state_(cable->edev,
189 cable->cable_index));
190}
191
192static ssize_t cable_state_store(struct device *dev,
193 struct device_attribute *attr, const char *buf,
194 size_t count)
195{
196 struct extcon_cable *cable = container_of(attr, struct extcon_cable,
197 attr_state);
198 int ret, state;
199
200 ret = sscanf(buf, "%d", &state);
201 if (ret == 0)
202 ret = -EINVAL;
203 else
204 ret = extcon_set_cable_state_(cable->edev, cable->cable_index,
205 state);
206
207 if (ret < 0)
208 return ret;
209 return count;
210}
211
MyungJoo Hamde55d872012-04-20 14:16:22 +0900212/**
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900213 * extcon_update_state() - Update the cable attach states of the extcon device
214 * only for the masked bits.
MyungJoo Hamde55d872012-04-20 14:16:22 +0900215 * @edev: the extcon device
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900216 * @mask: the bit mask to designate updated bits.
MyungJoo Hamde55d872012-04-20 14:16:22 +0900217 * @state: new cable attach status for @edev
218 *
219 * Changing the state sends uevent with environment variable containing
220 * the name of extcon device (envp[0]) and the state output (envp[1]).
221 * Tizen uses this format for extcon device to get events from ports.
222 * Android uses this format as well.
Donggeun Kim74c5d092012-04-20 14:16:24 +0900223 *
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900224 * Note that the notifier provides which bits are changed in the state
225 * variable with the val parameter (second) to the callback.
MyungJoo Hamde55d872012-04-20 14:16:22 +0900226 */
MyungJoo Hambde68e62012-04-20 14:16:26 +0900227int extcon_update_state(struct extcon_dev *edev, u32 mask, u32 state)
MyungJoo Hamde55d872012-04-20 14:16:22 +0900228{
229 char name_buf[120];
230 char state_buf[120];
231 char *prop_buf;
232 char *envp[3];
233 int env_offset = 0;
234 int length;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900235 unsigned long flags;
MyungJoo Hamde55d872012-04-20 14:16:22 +0900236
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900237 spin_lock_irqsave(&edev->lock, flags);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900238
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900239 if (edev->state != ((edev->state & ~mask) | (state & mask))) {
240 u32 old_state = edev->state;
Donggeun Kim74c5d092012-04-20 14:16:24 +0900241
MyungJoo Hambde68e62012-04-20 14:16:26 +0900242 if (check_mutually_exclusive(edev, (edev->state & ~mask) |
243 (state & mask))) {
244 spin_unlock_irqrestore(&edev->lock, flags);
245 return -EPERM;
246 }
247
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900248 edev->state &= ~mask;
249 edev->state |= state & mask;
250
251 raw_notifier_call_chain(&edev->nh, old_state, edev);
252
253 /* This could be in interrupt handler */
254 prop_buf = (char *)get_zeroed_page(GFP_ATOMIC);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900255 if (prop_buf) {
256 length = name_show(edev->dev, NULL, prop_buf);
257 if (length > 0) {
258 if (prop_buf[length - 1] == '\n')
259 prop_buf[length - 1] = 0;
260 snprintf(name_buf, sizeof(name_buf),
261 "NAME=%s", prop_buf);
262 envp[env_offset++] = name_buf;
263 }
264 length = state_show(edev->dev, NULL, prop_buf);
265 if (length > 0) {
266 if (prop_buf[length - 1] == '\n')
267 prop_buf[length - 1] = 0;
268 snprintf(state_buf, sizeof(state_buf),
269 "STATE=%s", prop_buf);
270 envp[env_offset++] = state_buf;
271 }
272 envp[env_offset] = NULL;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900273 /* Unlock early before uevent */
274 spin_unlock_irqrestore(&edev->lock, flags);
275
MyungJoo Hamde55d872012-04-20 14:16:22 +0900276 kobject_uevent_env(&edev->dev->kobj, KOBJ_CHANGE, envp);
277 free_page((unsigned long)prop_buf);
278 } else {
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900279 /* Unlock early before uevent */
280 spin_unlock_irqrestore(&edev->lock, flags);
281
MyungJoo Hamde55d872012-04-20 14:16:22 +0900282 dev_err(edev->dev, "out of memory in extcon_set_state\n");
283 kobject_uevent(&edev->dev->kobj, KOBJ_CHANGE);
284 }
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900285 } else {
286 /* No changes */
287 spin_unlock_irqrestore(&edev->lock, flags);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900288 }
MyungJoo Hambde68e62012-04-20 14:16:26 +0900289
290 return 0;
MyungJoo Hamde55d872012-04-20 14:16:22 +0900291}
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900292EXPORT_SYMBOL_GPL(extcon_update_state);
293
294/**
295 * extcon_set_state() - Set the cable attach states of the extcon device.
296 * @edev: the extcon device
297 * @state: new cable attach status for @edev
298 *
299 * Note that notifier provides which bits are changed in the state
300 * variable with the val parameter (second) to the callback.
301 */
MyungJoo Hambde68e62012-04-20 14:16:26 +0900302int extcon_set_state(struct extcon_dev *edev, u32 state)
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900303{
MyungJoo Hambde68e62012-04-20 14:16:26 +0900304 return extcon_update_state(edev, 0xffffffff, state);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900305}
MyungJoo Hamde55d872012-04-20 14:16:22 +0900306EXPORT_SYMBOL_GPL(extcon_set_state);
307
Donggeun Kim74c5d092012-04-20 14:16:24 +0900308/**
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900309 * extcon_find_cable_index() - Get the cable index based on the cable name.
310 * @edev: the extcon device that has the cable.
311 * @cable_name: cable name to be searched.
312 *
313 * Note that accessing a cable state based on cable_index is faster than
314 * cable_name because using cable_name induces a loop with strncmp().
315 * Thus, when get/set_cable_state is repeatedly used, using cable_index
316 * is recommended.
317 */
318int extcon_find_cable_index(struct extcon_dev *edev, const char *cable_name)
319{
320 int i;
321
322 if (edev->supported_cable) {
323 for (i = 0; edev->supported_cable[i]; i++) {
324 if (!strncmp(edev->supported_cable[i],
325 cable_name, CABLE_NAME_MAX))
326 return i;
327 }
328 }
329
330 return -EINVAL;
331}
332EXPORT_SYMBOL_GPL(extcon_find_cable_index);
333
334/**
335 * extcon_get_cable_state_() - Get the status of a specific cable.
336 * @edev: the extcon device that has the cable.
337 * @index: cable index that can be retrieved by extcon_find_cable_index().
338 */
339int extcon_get_cable_state_(struct extcon_dev *edev, int index)
340{
341 if (index < 0 || (edev->max_supported && edev->max_supported <= index))
342 return -EINVAL;
343
344 return !!(edev->state & (1 << index));
345}
346EXPORT_SYMBOL_GPL(extcon_get_cable_state_);
347
348/**
349 * extcon_get_cable_state() - Get the status of a specific cable.
350 * @edev: the extcon device that has the cable.
351 * @cable_name: cable name.
352 *
353 * Note that this is slower than extcon_get_cable_state_.
354 */
355int extcon_get_cable_state(struct extcon_dev *edev, const char *cable_name)
356{
357 return extcon_get_cable_state_(edev, extcon_find_cable_index
358 (edev, cable_name));
359}
360EXPORT_SYMBOL_GPL(extcon_get_cable_state);
361
362/**
363 * extcon_get_cable_state_() - Set the status of a specific cable.
364 * @edev: the extcon device that has the cable.
365 * @index: cable index that can be retrieved by extcon_find_cable_index().
366 * @cable_state: the new cable status. The default semantics is
367 * true: attached / false: detached.
368 */
369int extcon_set_cable_state_(struct extcon_dev *edev,
370 int index, bool cable_state)
371{
372 u32 state;
373
374 if (index < 0 || (edev->max_supported && edev->max_supported <= index))
375 return -EINVAL;
376
377 state = cable_state ? (1 << index) : 0;
MyungJoo Hambde68e62012-04-20 14:16:26 +0900378 return extcon_update_state(edev, 1 << index, state);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900379}
380EXPORT_SYMBOL_GPL(extcon_set_cable_state_);
381
382/**
383 * extcon_get_cable_state() - Set the status of a specific cable.
384 * @edev: the extcon device that has the cable.
385 * @cable_name: cable name.
386 * @cable_state: the new cable status. The default semantics is
387 * true: attached / false: detached.
388 *
389 * Note that this is slower than extcon_set_cable_state_.
390 */
391int extcon_set_cable_state(struct extcon_dev *edev,
392 const char *cable_name, bool cable_state)
393{
394 return extcon_set_cable_state_(edev, extcon_find_cable_index
395 (edev, cable_name), cable_state);
396}
397EXPORT_SYMBOL_GPL(extcon_set_cable_state);
398
399/**
Donggeun Kim74c5d092012-04-20 14:16:24 +0900400 * extcon_get_extcon_dev() - Get the extcon device instance from the name
401 * @extcon_name: The extcon name provided with extcon_dev_register()
402 */
403struct extcon_dev *extcon_get_extcon_dev(const char *extcon_name)
404{
405 struct extcon_dev *sd;
406
407 mutex_lock(&extcon_dev_list_lock);
408 list_for_each_entry(sd, &extcon_dev_list, entry) {
409 if (!strcmp(sd->name, extcon_name))
410 goto out;
411 }
412 sd = NULL;
413out:
414 mutex_unlock(&extcon_dev_list_lock);
415 return sd;
416}
417EXPORT_SYMBOL_GPL(extcon_get_extcon_dev);
418
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900419static int _call_per_cable(struct notifier_block *nb, unsigned long val,
420 void *ptr)
421{
422 struct extcon_specific_cable_nb *obj = container_of(nb,
423 struct extcon_specific_cable_nb, internal_nb);
424 struct extcon_dev *edev = ptr;
425
426 if ((val & (1 << obj->cable_index)) !=
427 (edev->state & (1 << obj->cable_index))) {
Chanwoo Choif4cce692012-04-27 15:17:28 +0900428 bool cable_state = true;
429
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900430 obj->previous_value = val;
Chanwoo Choif4cce692012-04-27 15:17:28 +0900431
432 if (val & (1 << obj->cable_index))
433 cable_state = false;
434
435 return obj->user_nb->notifier_call(obj->user_nb,
436 cable_state, ptr);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900437 }
438
439 return NOTIFY_OK;
440}
441
442/**
443 * extcon_register_interest() - Register a notifier for a state change of a
Peter Meerwaldc338bb02012-08-23 09:11:54 +0900444 * specific cable, not an entier set of cables of a
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900445 * extcon device.
446 * @obj: an empty extcon_specific_cable_nb object to be returned.
447 * @extcon_name: the name of extcon device.
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 */
463int 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 if (!obj || !extcon_name || !cable_name || !nb)
468 return -EINVAL;
469
470 obj->edev = extcon_get_extcon_dev(extcon_name);
471 if (!obj->edev)
472 return -ENODEV;
473
474 obj->cable_index = extcon_find_cable_index(obj->edev, cable_name);
475 if (obj->cable_index < 0)
476 return -ENODEV;
477
478 obj->user_nb = nb;
479
480 obj->internal_nb.notifier_call = _call_per_cable;
481
482 return raw_notifier_chain_register(&obj->edev->nh, &obj->internal_nb);
483}
484
485/**
486 * extcon_unregister_interest() - Unregister the notifier registered by
487 * extcon_register_interest().
488 * @obj: the extcon_specific_cable_nb object returned by
489 * extcon_register_interest().
490 */
491int extcon_unregister_interest(struct extcon_specific_cable_nb *obj)
492{
493 if (!obj)
494 return -EINVAL;
495
496 return raw_notifier_chain_unregister(&obj->edev->nh, &obj->internal_nb);
497}
498
Donggeun Kim74c5d092012-04-20 14:16:24 +0900499/**
Peter Meerwaldc338bb02012-08-23 09:11:54 +0900500 * extcon_register_notifier() - Register a notifiee to get notified by
Donggeun Kim74c5d092012-04-20 14:16:24 +0900501 * any attach status changes from the extcon.
502 * @edev: the extcon device.
503 * @nb: a notifier block to be registered.
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900504 *
505 * Note that the second parameter given to the callback of nb (val) is
506 * "old_state", not the current state. The current state can be retrieved
507 * by looking at the third pameter (edev pointer)'s state value.
Donggeun Kim74c5d092012-04-20 14:16:24 +0900508 */
509int extcon_register_notifier(struct extcon_dev *edev,
510 struct notifier_block *nb)
511{
512 return raw_notifier_chain_register(&edev->nh, nb);
513}
514EXPORT_SYMBOL_GPL(extcon_register_notifier);
515
516/**
Peter Meerwaldc338bb02012-08-23 09:11:54 +0900517 * extcon_unregister_notifier() - Unregister a notifiee from the extcon device.
Donggeun Kim74c5d092012-04-20 14:16:24 +0900518 * @edev: the extcon device.
519 * @nb: a registered notifier block to be unregistered.
520 */
521int extcon_unregister_notifier(struct extcon_dev *edev,
522 struct notifier_block *nb)
523{
524 return raw_notifier_chain_unregister(&edev->nh, nb);
525}
526EXPORT_SYMBOL_GPL(extcon_unregister_notifier);
527
MyungJoo Hamde55d872012-04-20 14:16:22 +0900528static struct device_attribute extcon_attrs[] = {
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900529 __ATTR(state, S_IRUGO | S_IWUSR, state_show, state_store),
MyungJoo Hamde55d872012-04-20 14:16:22 +0900530 __ATTR_RO(name),
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900531 __ATTR_NULL,
MyungJoo Hamde55d872012-04-20 14:16:22 +0900532};
533
534static int create_extcon_class(void)
535{
536 if (!extcon_class) {
537 extcon_class = class_create(THIS_MODULE, "extcon");
538 if (IS_ERR(extcon_class))
539 return PTR_ERR(extcon_class);
540 extcon_class->dev_attrs = extcon_attrs;
541
MyungJoo Ham449a2bf2012-04-23 20:19:57 +0900542#if defined(CONFIG_ANDROID)
MyungJoo Hamde55d872012-04-20 14:16:22 +0900543 switch_class = class_compat_register("switch");
544 if (WARN(!switch_class, "cannot allocate"))
545 return -ENOMEM;
MyungJoo Ham449a2bf2012-04-23 20:19:57 +0900546#endif /* CONFIG_ANDROID */
MyungJoo Hamde55d872012-04-20 14:16:22 +0900547 }
548
549 return 0;
550}
551
552static void extcon_cleanup(struct extcon_dev *edev, bool skip)
553{
Donggeun Kim74c5d092012-04-20 14:16:24 +0900554 mutex_lock(&extcon_dev_list_lock);
555 list_del(&edev->entry);
556 mutex_unlock(&extcon_dev_list_lock);
557
MyungJoo Hamde55d872012-04-20 14:16:22 +0900558 if (!skip && get_device(edev->dev)) {
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900559 int index;
560
MyungJoo Hambde68e62012-04-20 14:16:26 +0900561 if (edev->mutually_exclusive && edev->max_supported) {
562 for (index = 0; edev->mutually_exclusive[index];
563 index++)
564 kfree(edev->d_attrs_muex[index].attr.name);
565 kfree(edev->d_attrs_muex);
566 kfree(edev->attrs_muex);
567 }
568
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900569 for (index = 0; index < edev->max_supported; index++)
570 kfree(edev->cables[index].attr_g.name);
571
572 if (edev->max_supported) {
573 kfree(edev->extcon_dev_type.groups);
574 kfree(edev->cables);
575 }
576
MyungJoo Hamde55d872012-04-20 14:16:22 +0900577 device_unregister(edev->dev);
578 put_device(edev->dev);
579 }
580
581 kfree(edev->dev);
582}
583
584static void extcon_dev_release(struct device *dev)
585{
586 struct extcon_dev *edev = (struct extcon_dev *) dev_get_drvdata(dev);
587
588 extcon_cleanup(edev, true);
589}
590
MyungJoo Hambde68e62012-04-20 14:16:26 +0900591static const char *muex_name = "mutually_exclusive";
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900592static void dummy_sysfs_dev_release(struct device *dev)
593{
594}
595
MyungJoo Hamde55d872012-04-20 14:16:22 +0900596/**
597 * extcon_dev_register() - Register a new extcon device
598 * @edev : the new extcon device (should be allocated before calling)
599 * @dev : the parent device for this extcon device.
600 *
601 * Among the members of edev struct, please set the "user initializing data"
602 * in any case and set the "optional callbacks" if required. However, please
603 * do not set the values of "internal data", which are initialized by
604 * this function.
605 */
606int extcon_dev_register(struct extcon_dev *edev, struct device *dev)
607{
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900608 int ret, index = 0;
MyungJoo Hamde55d872012-04-20 14:16:22 +0900609
610 if (!extcon_class) {
611 ret = create_extcon_class();
612 if (ret < 0)
613 return ret;
614 }
615
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900616 if (edev->supported_cable) {
617 /* Get size of array */
618 for (index = 0; edev->supported_cable[index]; index++)
619 ;
620 edev->max_supported = index;
621 } else {
622 edev->max_supported = 0;
623 }
624
625 if (index > SUPPORTED_CABLE_MAX) {
626 dev_err(edev->dev, "extcon: maximum number of supported cables exceeded.\n");
627 return -EINVAL;
628 }
629
MyungJoo Hamde55d872012-04-20 14:16:22 +0900630 edev->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
Dan Carpentera1d26ac2012-04-25 11:47:02 +0300631 if (!edev->dev)
632 return -ENOMEM;
MyungJoo Hamde55d872012-04-20 14:16:22 +0900633 edev->dev->parent = dev;
634 edev->dev->class = extcon_class;
635 edev->dev->release = extcon_dev_release;
636
637 dev_set_name(edev->dev, edev->name ? edev->name : dev_name(dev));
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900638
639 if (edev->max_supported) {
640 char buf[10];
641 char *str;
642 struct extcon_cable *cable;
643
644 edev->cables = kzalloc(sizeof(struct extcon_cable) *
645 edev->max_supported, GFP_KERNEL);
646 if (!edev->cables) {
647 ret = -ENOMEM;
648 goto err_sysfs_alloc;
649 }
650 for (index = 0; index < edev->max_supported; index++) {
651 cable = &edev->cables[index];
652
653 snprintf(buf, 10, "cable.%d", index);
654 str = kzalloc(sizeof(char) * (strlen(buf) + 1),
655 GFP_KERNEL);
656 if (!str) {
657 for (index--; index >= 0; index--) {
658 cable = &edev->cables[index];
659 kfree(cable->attr_g.name);
660 }
661 ret = -ENOMEM;
662
663 goto err_alloc_cables;
664 }
665 strcpy(str, buf);
666
667 cable->edev = edev;
668 cable->cable_index = index;
669 cable->attrs[0] = &cable->attr_name.attr;
670 cable->attrs[1] = &cable->attr_state.attr;
671 cable->attrs[2] = NULL;
672 cable->attr_g.name = str;
673 cable->attr_g.attrs = cable->attrs;
674
Mark Brown9baf3222012-08-16 20:03:21 +0100675 sysfs_attr_init(&cable->attr_name.attr);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900676 cable->attr_name.attr.name = "name";
677 cable->attr_name.attr.mode = 0444;
678 cable->attr_name.show = cable_name_show;
679
Mark Brown9baf3222012-08-16 20:03:21 +0100680 sysfs_attr_init(&cable->attr_state.attr);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900681 cable->attr_state.attr.name = "state";
682 cable->attr_state.attr.mode = 0644;
683 cable->attr_state.show = cable_state_show;
684 cable->attr_state.store = cable_state_store;
685 }
686 }
687
MyungJoo Hambde68e62012-04-20 14:16:26 +0900688 if (edev->max_supported && edev->mutually_exclusive) {
689 char buf[80];
690 char *name;
691
692 /* Count the size of mutually_exclusive array */
693 for (index = 0; edev->mutually_exclusive[index]; index++)
694 ;
695
696 edev->attrs_muex = kzalloc(sizeof(struct attribute *) *
697 (index + 1), GFP_KERNEL);
698 if (!edev->attrs_muex) {
699 ret = -ENOMEM;
700 goto err_muex;
701 }
702
703 edev->d_attrs_muex = kzalloc(sizeof(struct device_attribute) *
704 index, GFP_KERNEL);
705 if (!edev->d_attrs_muex) {
706 ret = -ENOMEM;
707 kfree(edev->attrs_muex);
708 goto err_muex;
709 }
710
711 for (index = 0; edev->mutually_exclusive[index]; index++) {
712 sprintf(buf, "0x%x", edev->mutually_exclusive[index]);
713 name = kzalloc(sizeof(char) * (strlen(buf) + 1),
714 GFP_KERNEL);
715 if (!name) {
716 for (index--; index >= 0; index--) {
717 kfree(edev->d_attrs_muex[index].attr.
718 name);
719 }
720 kfree(edev->d_attrs_muex);
721 kfree(edev->attrs_muex);
722 ret = -ENOMEM;
723 goto err_muex;
724 }
725 strcpy(name, buf);
Mark Brown9baf3222012-08-16 20:03:21 +0100726 sysfs_attr_init(&edev->d_attrs_muex[index].attr);
MyungJoo Hambde68e62012-04-20 14:16:26 +0900727 edev->d_attrs_muex[index].attr.name = name;
728 edev->d_attrs_muex[index].attr.mode = 0000;
729 edev->attrs_muex[index] = &edev->d_attrs_muex[index]
730 .attr;
731 }
732 edev->attr_g_muex.name = muex_name;
733 edev->attr_g_muex.attrs = edev->attrs_muex;
734
735 }
736
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900737 if (edev->max_supported) {
738 edev->extcon_dev_type.groups =
739 kzalloc(sizeof(struct attribute_group *) *
MyungJoo Hambde68e62012-04-20 14:16:26 +0900740 (edev->max_supported + 2), GFP_KERNEL);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900741 if (!edev->extcon_dev_type.groups) {
742 ret = -ENOMEM;
743 goto err_alloc_groups;
744 }
745
746 edev->extcon_dev_type.name = dev_name(edev->dev);
747 edev->extcon_dev_type.release = dummy_sysfs_dev_release;
748
749 for (index = 0; index < edev->max_supported; index++)
750 edev->extcon_dev_type.groups[index] =
751 &edev->cables[index].attr_g;
MyungJoo Hambde68e62012-04-20 14:16:26 +0900752 if (edev->mutually_exclusive)
753 edev->extcon_dev_type.groups[index] =
754 &edev->attr_g_muex;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900755
756 edev->dev->type = &edev->extcon_dev_type;
757 }
758
MyungJoo Hamde55d872012-04-20 14:16:22 +0900759 ret = device_register(edev->dev);
760 if (ret) {
761 put_device(edev->dev);
762 goto err_dev;
763 }
MyungJoo Ham449a2bf2012-04-23 20:19:57 +0900764#if defined(CONFIG_ANDROID)
MyungJoo Hamde55d872012-04-20 14:16:22 +0900765 if (switch_class)
766 ret = class_compat_create_link(switch_class, edev->dev,
Mark Brownc3b15452012-06-05 16:43:53 +0100767 NULL);
MyungJoo Ham449a2bf2012-04-23 20:19:57 +0900768#endif /* CONFIG_ANDROID */
MyungJoo Hamde55d872012-04-20 14:16:22 +0900769
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900770 spin_lock_init(&edev->lock);
771
Donggeun Kim74c5d092012-04-20 14:16:24 +0900772 RAW_INIT_NOTIFIER_HEAD(&edev->nh);
773
MyungJoo Hamde55d872012-04-20 14:16:22 +0900774 dev_set_drvdata(edev->dev, edev);
775 edev->state = 0;
Donggeun Kim74c5d092012-04-20 14:16:24 +0900776
777 mutex_lock(&extcon_dev_list_lock);
778 list_add(&edev->entry, &extcon_dev_list);
779 mutex_unlock(&extcon_dev_list_lock);
780
MyungJoo Hamde55d872012-04-20 14:16:22 +0900781 return 0;
782
783err_dev:
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900784 if (edev->max_supported)
785 kfree(edev->extcon_dev_type.groups);
786err_alloc_groups:
MyungJoo Hambde68e62012-04-20 14:16:26 +0900787 if (edev->max_supported && edev->mutually_exclusive) {
788 for (index = 0; edev->mutually_exclusive[index]; index++)
789 kfree(edev->d_attrs_muex[index].attr.name);
790 kfree(edev->d_attrs_muex);
791 kfree(edev->attrs_muex);
792 }
793err_muex:
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900794 for (index = 0; index < edev->max_supported; index++)
795 kfree(edev->cables[index].attr_g.name);
796err_alloc_cables:
797 if (edev->max_supported)
798 kfree(edev->cables);
799err_sysfs_alloc:
MyungJoo Hamde55d872012-04-20 14:16:22 +0900800 kfree(edev->dev);
801 return ret;
802}
803EXPORT_SYMBOL_GPL(extcon_dev_register);
804
805/**
806 * extcon_dev_unregister() - Unregister the extcon device.
Peter Meerwaldc338bb02012-08-23 09:11:54 +0900807 * @edev: the extcon device instance to be unregistered.
MyungJoo Hamde55d872012-04-20 14:16:22 +0900808 *
809 * Note that this does not call kfree(edev) because edev was not allocated
810 * by this class.
811 */
812void extcon_dev_unregister(struct extcon_dev *edev)
813{
814 extcon_cleanup(edev, false);
815}
816EXPORT_SYMBOL_GPL(extcon_dev_unregister);
817
818static int __init extcon_class_init(void)
819{
820 return create_extcon_class();
821}
822module_init(extcon_class_init);
823
824static void __exit extcon_class_exit(void)
825{
826 class_destroy(extcon_class);
827}
828module_exit(extcon_class_exit);
829
830MODULE_AUTHOR("Mike Lockwood <lockwood@android.com>");
831MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>");
832MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
833MODULE_DESCRIPTION("External connector (extcon) class driver");
834MODULE_LICENSE("GPL");