blob: dbd3bfba42daa8019f9416fc68c04b1359dc51c1 [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>
33
MyungJoo Ham806d9dd2012-04-20 14:16:25 +090034/*
35 * extcon_cable_name suggests the standard cable names for commonly used
36 * cable types.
37 *
38 * However, please do not use extcon_cable_name directly for extcon_dev
39 * struct's supported_cable pointer unless your device really supports
40 * every single port-type of the following cable names. Please choose cable
41 * names that are actually used in your extcon device.
42 */
43const char *extcon_cable_name[] = {
44 [EXTCON_USB] = "USB",
45 [EXTCON_USB_HOST] = "USB-Host",
46 [EXTCON_TA] = "TA",
47 [EXTCON_FAST_CHARGER] = "Fast-charger",
48 [EXTCON_SLOW_CHARGER] = "Slow-charger",
49 [EXTCON_CHARGE_DOWNSTREAM] = "Charge-downstream",
50 [EXTCON_HDMI] = "HDMI",
51 [EXTCON_MHL] = "MHL",
52 [EXTCON_DVI] = "DVI",
53 [EXTCON_VGA] = "VGA",
54 [EXTCON_DOCK] = "Dock",
55 [EXTCON_LINE_IN] = "Line-in",
56 [EXTCON_LINE_OUT] = "Line-out",
57 [EXTCON_MIC_IN] = "Microphone",
58 [EXTCON_HEADPHONE_OUT] = "Headphone",
59 [EXTCON_SPDIF_IN] = "SPDIF-in",
60 [EXTCON_SPDIF_OUT] = "SPDIF-out",
61 [EXTCON_VIDEO_IN] = "Video-in",
62 [EXTCON_VIDEO_OUT] = "Video-out",
63
64 NULL,
65};
66
MyungJoo Hamde55d872012-04-20 14:16:22 +090067struct 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))) {
428 obj->previous_value = val;
429 return obj->user_nb->notifier_call(obj->user_nb, val, ptr);
430 }
431
432 return NOTIFY_OK;
433}
434
435/**
436 * extcon_register_interest() - Register a notifier for a state change of a
437 * specific cable, not a entier set of cables of a
438 * extcon device.
439 * @obj: an empty extcon_specific_cable_nb object to be returned.
440 * @extcon_name: the name of extcon device.
441 * @cable_name: the target cable name.
442 * @nb: the notifier block to get notified.
443 *
444 * Provide an empty extcon_specific_cable_nb. extcon_register_interest() sets
445 * the struct for you.
446 *
447 * extcon_register_interest is a helper function for those who want to get
448 * notification for a single specific cable's status change. If a user wants
449 * to get notification for any changes of all cables of a extcon device,
450 * he/she should use the general extcon_register_notifier().
451 *
452 * Note that the second parameter given to the callback of nb (val) is
453 * "old_state", not the current state. The current state can be retrieved
454 * by looking at the third pameter (edev pointer)'s state value.
455 */
456int extcon_register_interest(struct extcon_specific_cable_nb *obj,
457 const char *extcon_name, const char *cable_name,
458 struct notifier_block *nb)
459{
460 if (!obj || !extcon_name || !cable_name || !nb)
461 return -EINVAL;
462
463 obj->edev = extcon_get_extcon_dev(extcon_name);
464 if (!obj->edev)
465 return -ENODEV;
466
467 obj->cable_index = extcon_find_cable_index(obj->edev, cable_name);
468 if (obj->cable_index < 0)
469 return -ENODEV;
470
471 obj->user_nb = nb;
472
473 obj->internal_nb.notifier_call = _call_per_cable;
474
475 return raw_notifier_chain_register(&obj->edev->nh, &obj->internal_nb);
476}
477
478/**
479 * extcon_unregister_interest() - Unregister the notifier registered by
480 * extcon_register_interest().
481 * @obj: the extcon_specific_cable_nb object returned by
482 * extcon_register_interest().
483 */
484int extcon_unregister_interest(struct extcon_specific_cable_nb *obj)
485{
486 if (!obj)
487 return -EINVAL;
488
489 return raw_notifier_chain_unregister(&obj->edev->nh, &obj->internal_nb);
490}
491
Donggeun Kim74c5d092012-04-20 14:16:24 +0900492/**
493 * extcon_register_notifier() - Register a notifee to get notified by
494 * any attach status changes from the extcon.
495 * @edev: the extcon device.
496 * @nb: a notifier block to be registered.
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900497 *
498 * Note that the second parameter given to the callback of nb (val) is
499 * "old_state", not the current state. The current state can be retrieved
500 * by looking at the third pameter (edev pointer)'s state value.
Donggeun Kim74c5d092012-04-20 14:16:24 +0900501 */
502int extcon_register_notifier(struct extcon_dev *edev,
503 struct notifier_block *nb)
504{
505 return raw_notifier_chain_register(&edev->nh, nb);
506}
507EXPORT_SYMBOL_GPL(extcon_register_notifier);
508
509/**
510 * extcon_unregister_notifier() - Unregister a notifee from the extcon device.
511 * @edev: the extcon device.
512 * @nb: a registered notifier block to be unregistered.
513 */
514int extcon_unregister_notifier(struct extcon_dev *edev,
515 struct notifier_block *nb)
516{
517 return raw_notifier_chain_unregister(&edev->nh, nb);
518}
519EXPORT_SYMBOL_GPL(extcon_unregister_notifier);
520
MyungJoo Hamde55d872012-04-20 14:16:22 +0900521static struct device_attribute extcon_attrs[] = {
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900522 __ATTR(state, S_IRUGO | S_IWUSR, state_show, state_store),
MyungJoo Hamde55d872012-04-20 14:16:22 +0900523 __ATTR_RO(name),
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900524 __ATTR_NULL,
MyungJoo Hamde55d872012-04-20 14:16:22 +0900525};
526
527static int create_extcon_class(void)
528{
529 if (!extcon_class) {
530 extcon_class = class_create(THIS_MODULE, "extcon");
531 if (IS_ERR(extcon_class))
532 return PTR_ERR(extcon_class);
533 extcon_class->dev_attrs = extcon_attrs;
534
MyungJoo Ham449a2bf2012-04-23 20:19:57 +0900535#if defined(CONFIG_ANDROID)
MyungJoo Hamde55d872012-04-20 14:16:22 +0900536 switch_class = class_compat_register("switch");
537 if (WARN(!switch_class, "cannot allocate"))
538 return -ENOMEM;
MyungJoo Ham449a2bf2012-04-23 20:19:57 +0900539#endif /* CONFIG_ANDROID */
MyungJoo Hamde55d872012-04-20 14:16:22 +0900540 }
541
542 return 0;
543}
544
545static void extcon_cleanup(struct extcon_dev *edev, bool skip)
546{
Donggeun Kim74c5d092012-04-20 14:16:24 +0900547 mutex_lock(&extcon_dev_list_lock);
548 list_del(&edev->entry);
549 mutex_unlock(&extcon_dev_list_lock);
550
MyungJoo Hamde55d872012-04-20 14:16:22 +0900551 if (!skip && get_device(edev->dev)) {
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900552 int index;
553
MyungJoo Hambde68e62012-04-20 14:16:26 +0900554 if (edev->mutually_exclusive && edev->max_supported) {
555 for (index = 0; edev->mutually_exclusive[index];
556 index++)
557 kfree(edev->d_attrs_muex[index].attr.name);
558 kfree(edev->d_attrs_muex);
559 kfree(edev->attrs_muex);
560 }
561
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900562 for (index = 0; index < edev->max_supported; index++)
563 kfree(edev->cables[index].attr_g.name);
564
565 if (edev->max_supported) {
566 kfree(edev->extcon_dev_type.groups);
567 kfree(edev->cables);
568 }
569
MyungJoo Hamde55d872012-04-20 14:16:22 +0900570 device_unregister(edev->dev);
571 put_device(edev->dev);
572 }
573
574 kfree(edev->dev);
575}
576
577static void extcon_dev_release(struct device *dev)
578{
579 struct extcon_dev *edev = (struct extcon_dev *) dev_get_drvdata(dev);
580
581 extcon_cleanup(edev, true);
582}
583
MyungJoo Hambde68e62012-04-20 14:16:26 +0900584static const char *muex_name = "mutually_exclusive";
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900585static void dummy_sysfs_dev_release(struct device *dev)
586{
587}
588
MyungJoo Hamde55d872012-04-20 14:16:22 +0900589/**
590 * extcon_dev_register() - Register a new extcon device
591 * @edev : the new extcon device (should be allocated before calling)
592 * @dev : the parent device for this extcon device.
593 *
594 * Among the members of edev struct, please set the "user initializing data"
595 * in any case and set the "optional callbacks" if required. However, please
596 * do not set the values of "internal data", which are initialized by
597 * this function.
598 */
599int extcon_dev_register(struct extcon_dev *edev, struct device *dev)
600{
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900601 int ret, index = 0;
MyungJoo Hamde55d872012-04-20 14:16:22 +0900602
603 if (!extcon_class) {
604 ret = create_extcon_class();
605 if (ret < 0)
606 return ret;
607 }
608
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900609 if (edev->supported_cable) {
610 /* Get size of array */
611 for (index = 0; edev->supported_cable[index]; index++)
612 ;
613 edev->max_supported = index;
614 } else {
615 edev->max_supported = 0;
616 }
617
618 if (index > SUPPORTED_CABLE_MAX) {
619 dev_err(edev->dev, "extcon: maximum number of supported cables exceeded.\n");
620 return -EINVAL;
621 }
622
MyungJoo Hamde55d872012-04-20 14:16:22 +0900623 edev->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
624 edev->dev->parent = dev;
625 edev->dev->class = extcon_class;
626 edev->dev->release = extcon_dev_release;
627
628 dev_set_name(edev->dev, edev->name ? edev->name : dev_name(dev));
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900629
630 if (edev->max_supported) {
631 char buf[10];
632 char *str;
633 struct extcon_cable *cable;
634
635 edev->cables = kzalloc(sizeof(struct extcon_cable) *
636 edev->max_supported, GFP_KERNEL);
637 if (!edev->cables) {
638 ret = -ENOMEM;
639 goto err_sysfs_alloc;
640 }
641 for (index = 0; index < edev->max_supported; index++) {
642 cable = &edev->cables[index];
643
644 snprintf(buf, 10, "cable.%d", index);
645 str = kzalloc(sizeof(char) * (strlen(buf) + 1),
646 GFP_KERNEL);
647 if (!str) {
648 for (index--; index >= 0; index--) {
649 cable = &edev->cables[index];
650 kfree(cable->attr_g.name);
651 }
652 ret = -ENOMEM;
653
654 goto err_alloc_cables;
655 }
656 strcpy(str, buf);
657
658 cable->edev = edev;
659 cable->cable_index = index;
660 cable->attrs[0] = &cable->attr_name.attr;
661 cable->attrs[1] = &cable->attr_state.attr;
662 cable->attrs[2] = NULL;
663 cable->attr_g.name = str;
664 cable->attr_g.attrs = cable->attrs;
665
666 cable->attr_name.attr.name = "name";
667 cable->attr_name.attr.mode = 0444;
668 cable->attr_name.show = cable_name_show;
669
670 cable->attr_state.attr.name = "state";
671 cable->attr_state.attr.mode = 0644;
672 cable->attr_state.show = cable_state_show;
673 cable->attr_state.store = cable_state_store;
674 }
675 }
676
MyungJoo Hambde68e62012-04-20 14:16:26 +0900677 if (edev->max_supported && edev->mutually_exclusive) {
678 char buf[80];
679 char *name;
680
681 /* Count the size of mutually_exclusive array */
682 for (index = 0; edev->mutually_exclusive[index]; index++)
683 ;
684
685 edev->attrs_muex = kzalloc(sizeof(struct attribute *) *
686 (index + 1), GFP_KERNEL);
687 if (!edev->attrs_muex) {
688 ret = -ENOMEM;
689 goto err_muex;
690 }
691
692 edev->d_attrs_muex = kzalloc(sizeof(struct device_attribute) *
693 index, GFP_KERNEL);
694 if (!edev->d_attrs_muex) {
695 ret = -ENOMEM;
696 kfree(edev->attrs_muex);
697 goto err_muex;
698 }
699
700 for (index = 0; edev->mutually_exclusive[index]; index++) {
701 sprintf(buf, "0x%x", edev->mutually_exclusive[index]);
702 name = kzalloc(sizeof(char) * (strlen(buf) + 1),
703 GFP_KERNEL);
704 if (!name) {
705 for (index--; index >= 0; index--) {
706 kfree(edev->d_attrs_muex[index].attr.
707 name);
708 }
709 kfree(edev->d_attrs_muex);
710 kfree(edev->attrs_muex);
711 ret = -ENOMEM;
712 goto err_muex;
713 }
714 strcpy(name, buf);
715 edev->d_attrs_muex[index].attr.name = name;
716 edev->d_attrs_muex[index].attr.mode = 0000;
717 edev->attrs_muex[index] = &edev->d_attrs_muex[index]
718 .attr;
719 }
720 edev->attr_g_muex.name = muex_name;
721 edev->attr_g_muex.attrs = edev->attrs_muex;
722
723 }
724
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900725 if (edev->max_supported) {
726 edev->extcon_dev_type.groups =
727 kzalloc(sizeof(struct attribute_group *) *
MyungJoo Hambde68e62012-04-20 14:16:26 +0900728 (edev->max_supported + 2), GFP_KERNEL);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900729 if (!edev->extcon_dev_type.groups) {
730 ret = -ENOMEM;
731 goto err_alloc_groups;
732 }
733
734 edev->extcon_dev_type.name = dev_name(edev->dev);
735 edev->extcon_dev_type.release = dummy_sysfs_dev_release;
736
737 for (index = 0; index < edev->max_supported; index++)
738 edev->extcon_dev_type.groups[index] =
739 &edev->cables[index].attr_g;
MyungJoo Hambde68e62012-04-20 14:16:26 +0900740 if (edev->mutually_exclusive)
741 edev->extcon_dev_type.groups[index] =
742 &edev->attr_g_muex;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900743
744 edev->dev->type = &edev->extcon_dev_type;
745 }
746
MyungJoo Hamde55d872012-04-20 14:16:22 +0900747 ret = device_register(edev->dev);
748 if (ret) {
749 put_device(edev->dev);
750 goto err_dev;
751 }
MyungJoo Ham449a2bf2012-04-23 20:19:57 +0900752#if defined(CONFIG_ANDROID)
MyungJoo Hamde55d872012-04-20 14:16:22 +0900753 if (switch_class)
754 ret = class_compat_create_link(switch_class, edev->dev,
755 dev);
MyungJoo Ham449a2bf2012-04-23 20:19:57 +0900756#endif /* CONFIG_ANDROID */
MyungJoo Hamde55d872012-04-20 14:16:22 +0900757
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900758 spin_lock_init(&edev->lock);
759
Donggeun Kim74c5d092012-04-20 14:16:24 +0900760 RAW_INIT_NOTIFIER_HEAD(&edev->nh);
761
MyungJoo Hamde55d872012-04-20 14:16:22 +0900762 dev_set_drvdata(edev->dev, edev);
763 edev->state = 0;
Donggeun Kim74c5d092012-04-20 14:16:24 +0900764
765 mutex_lock(&extcon_dev_list_lock);
766 list_add(&edev->entry, &extcon_dev_list);
767 mutex_unlock(&extcon_dev_list_lock);
768
MyungJoo Hamde55d872012-04-20 14:16:22 +0900769 return 0;
770
771err_dev:
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900772 if (edev->max_supported)
773 kfree(edev->extcon_dev_type.groups);
774err_alloc_groups:
MyungJoo Hambde68e62012-04-20 14:16:26 +0900775 if (edev->max_supported && edev->mutually_exclusive) {
776 for (index = 0; edev->mutually_exclusive[index]; index++)
777 kfree(edev->d_attrs_muex[index].attr.name);
778 kfree(edev->d_attrs_muex);
779 kfree(edev->attrs_muex);
780 }
781err_muex:
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900782 for (index = 0; index < edev->max_supported; index++)
783 kfree(edev->cables[index].attr_g.name);
784err_alloc_cables:
785 if (edev->max_supported)
786 kfree(edev->cables);
787err_sysfs_alloc:
MyungJoo Hamde55d872012-04-20 14:16:22 +0900788 kfree(edev->dev);
789 return ret;
790}
791EXPORT_SYMBOL_GPL(extcon_dev_register);
792
793/**
794 * extcon_dev_unregister() - Unregister the extcon device.
795 * @edev: the extcon device instance to be unregitered.
796 *
797 * Note that this does not call kfree(edev) because edev was not allocated
798 * by this class.
799 */
800void extcon_dev_unregister(struct extcon_dev *edev)
801{
802 extcon_cleanup(edev, false);
803}
804EXPORT_SYMBOL_GPL(extcon_dev_unregister);
805
806static int __init extcon_class_init(void)
807{
808 return create_extcon_class();
809}
810module_init(extcon_class_init);
811
812static void __exit extcon_class_exit(void)
813{
814 class_destroy(extcon_class);
815}
816module_exit(extcon_class_exit);
817
818MODULE_AUTHOR("Mike Lockwood <lockwood@android.com>");
819MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>");
820MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
821MODULE_DESCRIPTION("External connector (extcon) class driver");
822MODULE_LICENSE("GPL");