blob: 17544320d1a634309841b57050c9ce2c722ad708 [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++) {
anish kumar28c0ada2012-08-30 00:35:10 +053092 int weight;
MyungJoo Hambde68e62012-04-20 14:16:26 +090093 u32 correspondants = new_state & edev->mutually_exclusive[i];
MyungJoo Hambde68e62012-04-20 14:16:26 +090094
anish kumar28c0ada2012-08-30 00:35:10 +053095 /* calculate the total number of bits set */
96 weight = hweight32(correspondants);
97 if (weight > 1)
98 return i + 1;
MyungJoo Hambde68e62012-04-20 14:16:26 +090099 }
100
101 return 0;
102}
103
MyungJoo Hamde55d872012-04-20 14:16:22 +0900104static ssize_t state_show(struct device *dev, struct device_attribute *attr,
105 char *buf)
106{
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900107 int i, count = 0;
MyungJoo Hamde55d872012-04-20 14:16:22 +0900108 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 }
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900117
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
MyungJoo Hambde68e62012-04-20 14:16:26 +0900132int extcon_set_state(struct extcon_dev *edev, u32 state);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900133static 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
MyungJoo Hambde68e62012-04-20 14:16:26 +0900144 ret = extcon_set_state(edev, state);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900145
146 if (ret < 0)
147 return ret;
148
149 return count;
MyungJoo Hamde55d872012-04-20 14:16:22 +0900150}
Greg Kroah-Hartmanaf01da02013-07-24 15:05:10 -0700151static DEVICE_ATTR_RW(state);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900152
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}
Greg Kroah-Hartmanaf01da02013-07-24 15:05:10 -0700167static DEVICE_ATTR_RO(name);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900168
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900169static 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
MyungJoo Hamde55d872012-04-20 14:16:22 +0900190/**
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900191 * extcon_update_state() - Update the cable attach states of the extcon device
192 * only for the masked bits.
MyungJoo Hamde55d872012-04-20 14:16:22 +0900193 * @edev: the extcon device
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900194 * @mask: the bit mask to designate updated bits.
MyungJoo Hamde55d872012-04-20 14:16:22 +0900195 * @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.
Donggeun Kim74c5d092012-04-20 14:16:24 +0900201 *
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900202 * Note that the notifier provides which bits are changed in the state
203 * variable with the val parameter (second) to the callback.
MyungJoo Hamde55d872012-04-20 14:16:22 +0900204 */
MyungJoo Hambde68e62012-04-20 14:16:26 +0900205int extcon_update_state(struct extcon_dev *edev, u32 mask, u32 state)
MyungJoo Hamde55d872012-04-20 14:16:22 +0900206{
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;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900213 unsigned long flags;
MyungJoo Hamde55d872012-04-20 14:16:22 +0900214
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900215 spin_lock_irqsave(&edev->lock, flags);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900216
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900217 if (edev->state != ((edev->state & ~mask) | (state & mask))) {
218 u32 old_state = edev->state;
Donggeun Kim74c5d092012-04-20 14:16:24 +0900219
MyungJoo Hambde68e62012-04-20 14:16:26 +0900220 if (check_mutually_exclusive(edev, (edev->state & ~mask) |
221 (state & mask))) {
222 spin_unlock_irqrestore(&edev->lock, flags);
223 return -EPERM;
224 }
225
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900226 edev->state &= ~mask;
227 edev->state |= state & mask;
228
229 raw_notifier_call_chain(&edev->nh, old_state, edev);
230
231 /* This could be in interrupt handler */
232 prop_buf = (char *)get_zeroed_page(GFP_ATOMIC);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900233 if (prop_buf) {
234 length = name_show(edev->dev, NULL, prop_buf);
235 if (length > 0) {
236 if (prop_buf[length - 1] == '\n')
237 prop_buf[length - 1] = 0;
238 snprintf(name_buf, sizeof(name_buf),
239 "NAME=%s", prop_buf);
240 envp[env_offset++] = name_buf;
241 }
242 length = state_show(edev->dev, NULL, prop_buf);
243 if (length > 0) {
244 if (prop_buf[length - 1] == '\n')
245 prop_buf[length - 1] = 0;
246 snprintf(state_buf, sizeof(state_buf),
247 "STATE=%s", prop_buf);
248 envp[env_offset++] = state_buf;
249 }
250 envp[env_offset] = NULL;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900251 /* Unlock early before uevent */
252 spin_unlock_irqrestore(&edev->lock, flags);
253
MyungJoo Hamde55d872012-04-20 14:16:22 +0900254 kobject_uevent_env(&edev->dev->kobj, KOBJ_CHANGE, envp);
255 free_page((unsigned long)prop_buf);
256 } else {
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900257 /* Unlock early before uevent */
258 spin_unlock_irqrestore(&edev->lock, flags);
259
MyungJoo Hamde55d872012-04-20 14:16:22 +0900260 dev_err(edev->dev, "out of memory in extcon_set_state\n");
261 kobject_uevent(&edev->dev->kobj, KOBJ_CHANGE);
262 }
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900263 } else {
264 /* No changes */
265 spin_unlock_irqrestore(&edev->lock, flags);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900266 }
MyungJoo Hambde68e62012-04-20 14:16:26 +0900267
268 return 0;
MyungJoo Hamde55d872012-04-20 14:16:22 +0900269}
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900270EXPORT_SYMBOL_GPL(extcon_update_state);
271
272/**
273 * extcon_set_state() - Set the cable attach states of the extcon device.
274 * @edev: the extcon device
275 * @state: new cable attach status for @edev
276 *
277 * Note that notifier provides which bits are changed in the state
278 * variable with the val parameter (second) to the callback.
279 */
MyungJoo Hambde68e62012-04-20 14:16:26 +0900280int extcon_set_state(struct extcon_dev *edev, u32 state)
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900281{
MyungJoo Hambde68e62012-04-20 14:16:26 +0900282 return extcon_update_state(edev, 0xffffffff, state);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900283}
MyungJoo Hamde55d872012-04-20 14:16:22 +0900284EXPORT_SYMBOL_GPL(extcon_set_state);
285
Donggeun Kim74c5d092012-04-20 14:16:24 +0900286/**
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900287 * extcon_find_cable_index() - Get the cable index based on the cable name.
288 * @edev: the extcon device that has the cable.
289 * @cable_name: cable name to be searched.
290 *
291 * Note that accessing a cable state based on cable_index is faster than
292 * cable_name because using cable_name induces a loop with strncmp().
293 * Thus, when get/set_cable_state is repeatedly used, using cable_index
294 * is recommended.
295 */
296int extcon_find_cable_index(struct extcon_dev *edev, const char *cable_name)
297{
298 int i;
299
300 if (edev->supported_cable) {
301 for (i = 0; edev->supported_cable[i]; i++) {
302 if (!strncmp(edev->supported_cable[i],
303 cable_name, CABLE_NAME_MAX))
304 return i;
305 }
306 }
307
308 return -EINVAL;
309}
310EXPORT_SYMBOL_GPL(extcon_find_cable_index);
311
312/**
313 * extcon_get_cable_state_() - Get the status of a specific cable.
314 * @edev: the extcon device that has the cable.
315 * @index: cable index that can be retrieved by extcon_find_cable_index().
316 */
317int extcon_get_cable_state_(struct extcon_dev *edev, int index)
318{
319 if (index < 0 || (edev->max_supported && edev->max_supported <= index))
320 return -EINVAL;
321
322 return !!(edev->state & (1 << index));
323}
324EXPORT_SYMBOL_GPL(extcon_get_cable_state_);
325
326/**
327 * extcon_get_cable_state() - Get the status of a specific cable.
328 * @edev: the extcon device that has the cable.
329 * @cable_name: cable name.
330 *
331 * Note that this is slower than extcon_get_cable_state_.
332 */
333int extcon_get_cable_state(struct extcon_dev *edev, const char *cable_name)
334{
335 return extcon_get_cable_state_(edev, extcon_find_cable_index
336 (edev, cable_name));
337}
338EXPORT_SYMBOL_GPL(extcon_get_cable_state);
339
340/**
Axel Lin909f9ec2012-10-04 09:53:45 +0900341 * extcon_set_cable_state_() - Set the status of a specific cable.
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900342 * @edev: the extcon device that has the cable.
343 * @index: cable index that can be retrieved by extcon_find_cable_index().
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;
MyungJoo Hambde68e62012-04-20 14:16:26 +0900356 return extcon_update_state(edev, 1 << index, state);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900357}
358EXPORT_SYMBOL_GPL(extcon_set_cable_state_);
359
360/**
Axel Lin909f9ec2012-10-04 09:53:45 +0900361 * extcon_set_cable_state() - Set the status of a specific cable.
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900362 * @edev: the extcon device that has the cable.
363 * @cable_name: cable name.
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
377/**
Donggeun Kim74c5d092012-04-20 14:16:24 +0900378 * 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
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900397static 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))) {
Chanwoo Choif4cce692012-04-27 15:17:28 +0900406 bool cable_state = true;
407
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900408 obj->previous_value = val;
Chanwoo Choif4cce692012-04-27 15:17:28 +0900409
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);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900415 }
416
417 return NOTIFY_OK;
418}
419
420/**
421 * extcon_register_interest() - Register a notifier for a state change of a
Peter Meerwaldc338bb02012-08-23 09:11:54 +0900422 * specific cable, not an entier set of cables of a
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900423 * extcon device.
424 * @obj: an empty extcon_specific_cable_nb object to be returned.
425 * @extcon_name: the name of extcon device.
Jenny TC4f2de3b2012-10-18 21:00:32 +0900426 * if NULL, extcon_register_interest will register
427 * every cable with the target cable_name given.
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900428 * @cable_name: the target cable name.
429 * @nb: the notifier block to get notified.
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{
Jenny TC4f2de3b2012-10-18 21:00:32 +0900447 if (!obj || !cable_name || !nb)
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900448 return -EINVAL;
449
Jenny TC4f2de3b2012-10-18 21:00:32 +0900450 if (extcon_name) {
451 obj->edev = extcon_get_extcon_dev(extcon_name);
452 if (!obj->edev)
453 return -ENODEV;
454
Chanwoo Choic2275d22013-08-23 10:21:37 +0900455 obj->cable_index = extcon_find_cable_index(obj->edev,
456 cable_name);
Jenny TC4f2de3b2012-10-18 21:00:32 +0900457 if (obj->cable_index < 0)
Sachin Kamatc0c078c2012-11-20 16:30:31 +0900458 return obj->cable_index;
Jenny TC4f2de3b2012-10-18 21:00:32 +0900459
460 obj->user_nb = nb;
461
462 obj->internal_nb.notifier_call = _call_per_cable;
463
Chanwoo Choic2275d22013-08-23 10:21:37 +0900464 return raw_notifier_chain_register(&obj->edev->nh,
465 &obj->internal_nb);
Jenny TC4f2de3b2012-10-18 21:00:32 +0900466 } else {
467 struct class_dev_iter iter;
468 struct extcon_dev *extd;
469 struct device *dev;
470
471 if (!extcon_class)
472 return -ENODEV;
473 class_dev_iter_init(&iter, extcon_class, NULL, NULL);
474 while ((dev = class_dev_iter_next(&iter))) {
475 extd = (struct extcon_dev *)dev_get_drvdata(dev);
476
477 if (extcon_find_cable_index(extd, cable_name) < 0)
478 continue;
479
480 class_dev_iter_exit(&iter);
481 return extcon_register_interest(obj, extd->name,
482 cable_name, nb);
483 }
484
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900485 return -ENODEV;
Jenny TC4f2de3b2012-10-18 21:00:32 +0900486 }
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900487}
Kishon Vijay Abraham I9c8a0132013-06-04 01:13:38 +0900488EXPORT_SYMBOL_GPL(extcon_register_interest);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900489
490/**
491 * extcon_unregister_interest() - Unregister the notifier registered by
492 * extcon_register_interest().
493 * @obj: the extcon_specific_cable_nb object returned by
494 * extcon_register_interest().
495 */
496int extcon_unregister_interest(struct extcon_specific_cable_nb *obj)
497{
498 if (!obj)
499 return -EINVAL;
500
501 return raw_notifier_chain_unregister(&obj->edev->nh, &obj->internal_nb);
502}
Kishon Vijay Abraham I9c8a0132013-06-04 01:13:38 +0900503EXPORT_SYMBOL_GPL(extcon_unregister_interest);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900504
Donggeun Kim74c5d092012-04-20 14:16:24 +0900505/**
Peter Meerwaldc338bb02012-08-23 09:11:54 +0900506 * extcon_register_notifier() - Register a notifiee to get notified by
Donggeun Kim74c5d092012-04-20 14:16:24 +0900507 * any attach status changes from the extcon.
508 * @edev: the extcon device.
509 * @nb: a notifier block to be registered.
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900510 *
511 * Note that the second parameter given to the callback of nb (val) is
512 * "old_state", not the current state. The current state can be retrieved
513 * by looking at the third pameter (edev pointer)'s state value.
Donggeun Kim74c5d092012-04-20 14:16:24 +0900514 */
515int extcon_register_notifier(struct extcon_dev *edev,
516 struct notifier_block *nb)
517{
518 return raw_notifier_chain_register(&edev->nh, nb);
519}
520EXPORT_SYMBOL_GPL(extcon_register_notifier);
521
522/**
Peter Meerwaldc338bb02012-08-23 09:11:54 +0900523 * extcon_unregister_notifier() - Unregister a notifiee from the extcon device.
Donggeun Kim74c5d092012-04-20 14:16:24 +0900524 * @edev: the extcon device.
525 * @nb: a registered notifier block to be unregistered.
526 */
527int extcon_unregister_notifier(struct extcon_dev *edev,
528 struct notifier_block *nb)
529{
530 return raw_notifier_chain_unregister(&edev->nh, nb);
531}
532EXPORT_SYMBOL_GPL(extcon_unregister_notifier);
533
Greg Kroah-Hartmanaf01da02013-07-24 15:05:10 -0700534static struct attribute *extcon_attrs[] = {
535 &dev_attr_state.attr,
536 &dev_attr_name.attr,
537 NULL,
MyungJoo Hamde55d872012-04-20 14:16:22 +0900538};
Greg Kroah-Hartmanaf01da02013-07-24 15:05:10 -0700539ATTRIBUTE_GROUPS(extcon);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900540
541static int create_extcon_class(void)
542{
543 if (!extcon_class) {
544 extcon_class = class_create(THIS_MODULE, "extcon");
545 if (IS_ERR(extcon_class))
546 return PTR_ERR(extcon_class);
Greg Kroah-Hartmanaf01da02013-07-24 15:05:10 -0700547 extcon_class->dev_groups = extcon_groups;
MyungJoo Hamde55d872012-04-20 14:16:22 +0900548
MyungJoo Ham449a2bf2012-04-23 20:19:57 +0900549#if defined(CONFIG_ANDROID)
MyungJoo Hamde55d872012-04-20 14:16:22 +0900550 switch_class = class_compat_register("switch");
551 if (WARN(!switch_class, "cannot allocate"))
552 return -ENOMEM;
MyungJoo Ham449a2bf2012-04-23 20:19:57 +0900553#endif /* CONFIG_ANDROID */
MyungJoo Hamde55d872012-04-20 14:16:22 +0900554 }
555
556 return 0;
557}
558
MyungJoo Hamde55d872012-04-20 14:16:22 +0900559static void extcon_dev_release(struct device *dev)
560{
anish kumar57e7cd32012-10-22 09:43:33 +0900561 kfree(dev);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900562}
563
MyungJoo Hambde68e62012-04-20 14:16:26 +0900564static const char *muex_name = "mutually_exclusive";
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900565static void dummy_sysfs_dev_release(struct device *dev)
566{
567}
568
MyungJoo Hamde55d872012-04-20 14:16:22 +0900569/**
570 * extcon_dev_register() - Register a new extcon device
571 * @edev : the new extcon device (should be allocated before calling)
572 * @dev : the parent device for this extcon device.
573 *
574 * Among the members of edev struct, please set the "user initializing data"
575 * in any case and set the "optional callbacks" if required. However, please
576 * do not set the values of "internal data", which are initialized by
577 * this function.
578 */
579int extcon_dev_register(struct extcon_dev *edev, struct device *dev)
580{
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900581 int ret, index = 0;
MyungJoo Hamde55d872012-04-20 14:16:22 +0900582
583 if (!extcon_class) {
584 ret = create_extcon_class();
585 if (ret < 0)
586 return ret;
587 }
588
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900589 if (edev->supported_cable) {
590 /* Get size of array */
591 for (index = 0; edev->supported_cable[index]; index++)
592 ;
593 edev->max_supported = index;
594 } else {
595 edev->max_supported = 0;
596 }
597
598 if (index > SUPPORTED_CABLE_MAX) {
599 dev_err(edev->dev, "extcon: maximum number of supported cables exceeded.\n");
600 return -EINVAL;
601 }
602
MyungJoo Hamde55d872012-04-20 14:16:22 +0900603 edev->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
Dan Carpentera1d26ac2012-04-25 11:47:02 +0300604 if (!edev->dev)
605 return -ENOMEM;
MyungJoo Hamde55d872012-04-20 14:16:22 +0900606 edev->dev->parent = dev;
607 edev->dev->class = extcon_class;
608 edev->dev->release = extcon_dev_release;
609
Kishon Vijay Abraham I6eee5b32013-06-11 20:48:02 +0900610 edev->name = edev->name ? edev->name : dev_name(dev);
611 dev_set_name(edev->dev, "%s", edev->name);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900612
613 if (edev->max_supported) {
614 char buf[10];
615 char *str;
616 struct extcon_cable *cable;
617
618 edev->cables = kzalloc(sizeof(struct extcon_cable) *
619 edev->max_supported, GFP_KERNEL);
620 if (!edev->cables) {
621 ret = -ENOMEM;
622 goto err_sysfs_alloc;
623 }
624 for (index = 0; index < edev->max_supported; index++) {
625 cable = &edev->cables[index];
626
627 snprintf(buf, 10, "cable.%d", index);
628 str = kzalloc(sizeof(char) * (strlen(buf) + 1),
629 GFP_KERNEL);
630 if (!str) {
631 for (index--; index >= 0; index--) {
632 cable = &edev->cables[index];
633 kfree(cable->attr_g.name);
634 }
635 ret = -ENOMEM;
636
637 goto err_alloc_cables;
638 }
639 strcpy(str, buf);
640
641 cable->edev = edev;
642 cable->cable_index = index;
643 cable->attrs[0] = &cable->attr_name.attr;
644 cable->attrs[1] = &cable->attr_state.attr;
645 cable->attrs[2] = NULL;
646 cable->attr_g.name = str;
647 cable->attr_g.attrs = cable->attrs;
648
Mark Brown9baf3222012-08-16 20:03:21 +0100649 sysfs_attr_init(&cable->attr_name.attr);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900650 cable->attr_name.attr.name = "name";
651 cable->attr_name.attr.mode = 0444;
652 cable->attr_name.show = cable_name_show;
653
Mark Brown9baf3222012-08-16 20:03:21 +0100654 sysfs_attr_init(&cable->attr_state.attr);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900655 cable->attr_state.attr.name = "state";
Chanwoo Choiea9dd9d2013-05-22 19:31:59 +0900656 cable->attr_state.attr.mode = 0444;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900657 cable->attr_state.show = cable_state_show;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900658 }
659 }
660
MyungJoo Hambde68e62012-04-20 14:16:26 +0900661 if (edev->max_supported && edev->mutually_exclusive) {
662 char buf[80];
663 char *name;
664
665 /* Count the size of mutually_exclusive array */
666 for (index = 0; edev->mutually_exclusive[index]; index++)
667 ;
668
669 edev->attrs_muex = kzalloc(sizeof(struct attribute *) *
670 (index + 1), GFP_KERNEL);
671 if (!edev->attrs_muex) {
672 ret = -ENOMEM;
673 goto err_muex;
674 }
675
676 edev->d_attrs_muex = kzalloc(sizeof(struct device_attribute) *
677 index, GFP_KERNEL);
678 if (!edev->d_attrs_muex) {
679 ret = -ENOMEM;
680 kfree(edev->attrs_muex);
681 goto err_muex;
682 }
683
684 for (index = 0; edev->mutually_exclusive[index]; index++) {
685 sprintf(buf, "0x%x", edev->mutually_exclusive[index]);
686 name = kzalloc(sizeof(char) * (strlen(buf) + 1),
687 GFP_KERNEL);
688 if (!name) {
689 for (index--; index >= 0; index--) {
690 kfree(edev->d_attrs_muex[index].attr.
691 name);
692 }
693 kfree(edev->d_attrs_muex);
694 kfree(edev->attrs_muex);
695 ret = -ENOMEM;
696 goto err_muex;
697 }
698 strcpy(name, buf);
Mark Brown9baf3222012-08-16 20:03:21 +0100699 sysfs_attr_init(&edev->d_attrs_muex[index].attr);
MyungJoo Hambde68e62012-04-20 14:16:26 +0900700 edev->d_attrs_muex[index].attr.name = name;
701 edev->d_attrs_muex[index].attr.mode = 0000;
702 edev->attrs_muex[index] = &edev->d_attrs_muex[index]
703 .attr;
704 }
705 edev->attr_g_muex.name = muex_name;
706 edev->attr_g_muex.attrs = edev->attrs_muex;
707
708 }
709
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900710 if (edev->max_supported) {
711 edev->extcon_dev_type.groups =
712 kzalloc(sizeof(struct attribute_group *) *
MyungJoo Hambde68e62012-04-20 14:16:26 +0900713 (edev->max_supported + 2), GFP_KERNEL);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900714 if (!edev->extcon_dev_type.groups) {
715 ret = -ENOMEM;
716 goto err_alloc_groups;
717 }
718
719 edev->extcon_dev_type.name = dev_name(edev->dev);
720 edev->extcon_dev_type.release = dummy_sysfs_dev_release;
721
722 for (index = 0; index < edev->max_supported; index++)
723 edev->extcon_dev_type.groups[index] =
724 &edev->cables[index].attr_g;
MyungJoo Hambde68e62012-04-20 14:16:26 +0900725 if (edev->mutually_exclusive)
726 edev->extcon_dev_type.groups[index] =
727 &edev->attr_g_muex;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900728
729 edev->dev->type = &edev->extcon_dev_type;
730 }
731
MyungJoo Hamde55d872012-04-20 14:16:22 +0900732 ret = device_register(edev->dev);
733 if (ret) {
734 put_device(edev->dev);
735 goto err_dev;
736 }
MyungJoo Ham449a2bf2012-04-23 20:19:57 +0900737#if defined(CONFIG_ANDROID)
MyungJoo Hamde55d872012-04-20 14:16:22 +0900738 if (switch_class)
739 ret = class_compat_create_link(switch_class, edev->dev,
Mark Brownc3b15452012-06-05 16:43:53 +0100740 NULL);
MyungJoo Ham449a2bf2012-04-23 20:19:57 +0900741#endif /* CONFIG_ANDROID */
MyungJoo Hamde55d872012-04-20 14:16:22 +0900742
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900743 spin_lock_init(&edev->lock);
744
Donggeun Kim74c5d092012-04-20 14:16:24 +0900745 RAW_INIT_NOTIFIER_HEAD(&edev->nh);
746
MyungJoo Hamde55d872012-04-20 14:16:22 +0900747 dev_set_drvdata(edev->dev, edev);
748 edev->state = 0;
Donggeun Kim74c5d092012-04-20 14:16:24 +0900749
750 mutex_lock(&extcon_dev_list_lock);
751 list_add(&edev->entry, &extcon_dev_list);
752 mutex_unlock(&extcon_dev_list_lock);
753
MyungJoo Hamde55d872012-04-20 14:16:22 +0900754 return 0;
755
756err_dev:
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900757 if (edev->max_supported)
758 kfree(edev->extcon_dev_type.groups);
759err_alloc_groups:
MyungJoo Hambde68e62012-04-20 14:16:26 +0900760 if (edev->max_supported && edev->mutually_exclusive) {
761 for (index = 0; edev->mutually_exclusive[index]; index++)
762 kfree(edev->d_attrs_muex[index].attr.name);
763 kfree(edev->d_attrs_muex);
764 kfree(edev->attrs_muex);
765 }
766err_muex:
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900767 for (index = 0; index < edev->max_supported; index++)
768 kfree(edev->cables[index].attr_g.name);
769err_alloc_cables:
770 if (edev->max_supported)
771 kfree(edev->cables);
772err_sysfs_alloc:
MyungJoo Hamde55d872012-04-20 14:16:22 +0900773 kfree(edev->dev);
774 return ret;
775}
776EXPORT_SYMBOL_GPL(extcon_dev_register);
777
778/**
779 * extcon_dev_unregister() - Unregister the extcon device.
Peter Meerwaldc338bb02012-08-23 09:11:54 +0900780 * @edev: the extcon device instance to be unregistered.
MyungJoo Hamde55d872012-04-20 14:16:22 +0900781 *
782 * Note that this does not call kfree(edev) because edev was not allocated
783 * by this class.
784 */
785void extcon_dev_unregister(struct extcon_dev *edev)
786{
anish kumar57e7cd32012-10-22 09:43:33 +0900787 int index;
788
789 mutex_lock(&extcon_dev_list_lock);
790 list_del(&edev->entry);
791 mutex_unlock(&extcon_dev_list_lock);
792
793 if (IS_ERR_OR_NULL(get_device(edev->dev))) {
794 dev_err(edev->dev, "Failed to unregister extcon_dev (%s)\n",
795 dev_name(edev->dev));
796 return;
797 }
798
799 if (edev->mutually_exclusive && edev->max_supported) {
800 for (index = 0; edev->mutually_exclusive[index];
801 index++)
802 kfree(edev->d_attrs_muex[index].attr.name);
803 kfree(edev->d_attrs_muex);
804 kfree(edev->attrs_muex);
805 }
806
807 for (index = 0; index < edev->max_supported; index++)
808 kfree(edev->cables[index].attr_g.name);
809
810 if (edev->max_supported) {
811 kfree(edev->extcon_dev_type.groups);
812 kfree(edev->cables);
813 }
814
815#if defined(CONFIG_ANDROID)
816 if (switch_class)
817 class_compat_remove_link(switch_class, edev->dev, NULL);
818#endif
819 device_unregister(edev->dev);
820 put_device(edev->dev);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900821}
822EXPORT_SYMBOL_GPL(extcon_dev_unregister);
823
824static int __init extcon_class_init(void)
825{
826 return create_extcon_class();
827}
828module_init(extcon_class_init);
829
830static void __exit extcon_class_exit(void)
831{
Peter Huewe0dc77b62012-09-24 15:32:31 +0900832#if defined(CONFIG_ANDROID)
833 class_compat_unregister(switch_class);
834#endif
MyungJoo Hamde55d872012-04-20 14:16:22 +0900835 class_destroy(extcon_class);
836}
837module_exit(extcon_class_exit);
838
839MODULE_AUTHOR("Mike Lockwood <lockwood@android.com>");
840MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>");
841MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
842MODULE_DESCRIPTION("External connector (extcon) class driver");
843MODULE_LICENSE("GPL");