blob: a5a03ae069eed2c302c6434468c728a3265c346f [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}
151
152static ssize_t name_show(struct device *dev, struct device_attribute *attr,
153 char *buf)
154{
155 struct extcon_dev *edev = (struct extcon_dev *) dev_get_drvdata(dev);
156
157 /* Optional callback given by the user */
158 if (edev->print_name) {
159 int ret = edev->print_name(edev, buf);
160 if (ret >= 0)
161 return ret;
162 }
163
164 return sprintf(buf, "%s\n", dev_name(edev->dev));
165}
166
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900167static ssize_t cable_name_show(struct device *dev,
168 struct device_attribute *attr, char *buf)
169{
170 struct extcon_cable *cable = container_of(attr, struct extcon_cable,
171 attr_name);
172
173 return sprintf(buf, "%s\n",
174 cable->edev->supported_cable[cable->cable_index]);
175}
176
177static ssize_t cable_state_show(struct device *dev,
178 struct device_attribute *attr, char *buf)
179{
180 struct extcon_cable *cable = container_of(attr, struct extcon_cable,
181 attr_state);
182
183 return sprintf(buf, "%d\n",
184 extcon_get_cable_state_(cable->edev,
185 cable->cable_index));
186}
187
MyungJoo Hamde55d872012-04-20 14:16:22 +0900188/**
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900189 * extcon_update_state() - Update the cable attach states of the extcon device
190 * only for the masked bits.
MyungJoo Hamde55d872012-04-20 14:16:22 +0900191 * @edev: the extcon device
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900192 * @mask: the bit mask to designate updated bits.
MyungJoo Hamde55d872012-04-20 14:16:22 +0900193 * @state: new cable attach status for @edev
194 *
195 * Changing the state sends uevent with environment variable containing
196 * the name of extcon device (envp[0]) and the state output (envp[1]).
197 * Tizen uses this format for extcon device to get events from ports.
198 * Android uses this format as well.
Donggeun Kim74c5d092012-04-20 14:16:24 +0900199 *
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900200 * Note that the notifier provides which bits are changed in the state
201 * variable with the val parameter (second) to the callback.
MyungJoo Hamde55d872012-04-20 14:16:22 +0900202 */
MyungJoo Hambde68e62012-04-20 14:16:26 +0900203int extcon_update_state(struct extcon_dev *edev, u32 mask, u32 state)
MyungJoo Hamde55d872012-04-20 14:16:22 +0900204{
205 char name_buf[120];
206 char state_buf[120];
207 char *prop_buf;
208 char *envp[3];
209 int env_offset = 0;
210 int length;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900211 unsigned long flags;
MyungJoo Hamde55d872012-04-20 14:16:22 +0900212
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900213 spin_lock_irqsave(&edev->lock, flags);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900214
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900215 if (edev->state != ((edev->state & ~mask) | (state & mask))) {
216 u32 old_state = edev->state;
Donggeun Kim74c5d092012-04-20 14:16:24 +0900217
MyungJoo Hambde68e62012-04-20 14:16:26 +0900218 if (check_mutually_exclusive(edev, (edev->state & ~mask) |
219 (state & mask))) {
220 spin_unlock_irqrestore(&edev->lock, flags);
221 return -EPERM;
222 }
223
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900224 edev->state &= ~mask;
225 edev->state |= state & mask;
226
227 raw_notifier_call_chain(&edev->nh, old_state, edev);
228
229 /* This could be in interrupt handler */
230 prop_buf = (char *)get_zeroed_page(GFP_ATOMIC);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900231 if (prop_buf) {
232 length = name_show(edev->dev, NULL, prop_buf);
233 if (length > 0) {
234 if (prop_buf[length - 1] == '\n')
235 prop_buf[length - 1] = 0;
236 snprintf(name_buf, sizeof(name_buf),
237 "NAME=%s", prop_buf);
238 envp[env_offset++] = name_buf;
239 }
240 length = state_show(edev->dev, NULL, prop_buf);
241 if (length > 0) {
242 if (prop_buf[length - 1] == '\n')
243 prop_buf[length - 1] = 0;
244 snprintf(state_buf, sizeof(state_buf),
245 "STATE=%s", prop_buf);
246 envp[env_offset++] = state_buf;
247 }
248 envp[env_offset] = NULL;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900249 /* Unlock early before uevent */
250 spin_unlock_irqrestore(&edev->lock, flags);
251
MyungJoo Hamde55d872012-04-20 14:16:22 +0900252 kobject_uevent_env(&edev->dev->kobj, KOBJ_CHANGE, envp);
253 free_page((unsigned long)prop_buf);
254 } else {
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900255 /* Unlock early before uevent */
256 spin_unlock_irqrestore(&edev->lock, flags);
257
MyungJoo Hamde55d872012-04-20 14:16:22 +0900258 dev_err(edev->dev, "out of memory in extcon_set_state\n");
259 kobject_uevent(&edev->dev->kobj, KOBJ_CHANGE);
260 }
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900261 } else {
262 /* No changes */
263 spin_unlock_irqrestore(&edev->lock, flags);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900264 }
MyungJoo Hambde68e62012-04-20 14:16:26 +0900265
266 return 0;
MyungJoo Hamde55d872012-04-20 14:16:22 +0900267}
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900268EXPORT_SYMBOL_GPL(extcon_update_state);
269
270/**
271 * extcon_set_state() - Set the cable attach states of the extcon device.
272 * @edev: the extcon device
273 * @state: new cable attach status for @edev
274 *
275 * Note that notifier provides which bits are changed in the state
276 * variable with the val parameter (second) to the callback.
277 */
MyungJoo Hambde68e62012-04-20 14:16:26 +0900278int extcon_set_state(struct extcon_dev *edev, u32 state)
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900279{
MyungJoo Hambde68e62012-04-20 14:16:26 +0900280 return extcon_update_state(edev, 0xffffffff, state);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900281}
MyungJoo Hamde55d872012-04-20 14:16:22 +0900282EXPORT_SYMBOL_GPL(extcon_set_state);
283
Donggeun Kim74c5d092012-04-20 14:16:24 +0900284/**
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900285 * extcon_find_cable_index() - Get the cable index based on the cable name.
286 * @edev: the extcon device that has the cable.
287 * @cable_name: cable name to be searched.
288 *
289 * Note that accessing a cable state based on cable_index is faster than
290 * cable_name because using cable_name induces a loop with strncmp().
291 * Thus, when get/set_cable_state is repeatedly used, using cable_index
292 * is recommended.
293 */
294int extcon_find_cable_index(struct extcon_dev *edev, const char *cable_name)
295{
296 int i;
297
298 if (edev->supported_cable) {
299 for (i = 0; edev->supported_cable[i]; i++) {
300 if (!strncmp(edev->supported_cable[i],
301 cable_name, CABLE_NAME_MAX))
302 return i;
303 }
304 }
305
306 return -EINVAL;
307}
308EXPORT_SYMBOL_GPL(extcon_find_cable_index);
309
310/**
311 * extcon_get_cable_state_() - Get the status of a specific cable.
312 * @edev: the extcon device that has the cable.
313 * @index: cable index that can be retrieved by extcon_find_cable_index().
314 */
315int extcon_get_cable_state_(struct extcon_dev *edev, int index)
316{
317 if (index < 0 || (edev->max_supported && edev->max_supported <= index))
318 return -EINVAL;
319
320 return !!(edev->state & (1 << index));
321}
322EXPORT_SYMBOL_GPL(extcon_get_cable_state_);
323
324/**
325 * extcon_get_cable_state() - Get the status of a specific cable.
326 * @edev: the extcon device that has the cable.
327 * @cable_name: cable name.
328 *
329 * Note that this is slower than extcon_get_cable_state_.
330 */
331int extcon_get_cable_state(struct extcon_dev *edev, const char *cable_name)
332{
333 return extcon_get_cable_state_(edev, extcon_find_cable_index
334 (edev, cable_name));
335}
336EXPORT_SYMBOL_GPL(extcon_get_cable_state);
337
338/**
Axel Lin909f9ec2012-10-04 09:53:45 +0900339 * extcon_set_cable_state_() - Set the status of a specific cable.
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900340 * @edev: the extcon device that has the cable.
341 * @index: cable index that can be retrieved by extcon_find_cable_index().
342 * @cable_state: the new cable status. The default semantics is
343 * true: attached / false: detached.
344 */
345int extcon_set_cable_state_(struct extcon_dev *edev,
346 int index, bool cable_state)
347{
348 u32 state;
349
350 if (index < 0 || (edev->max_supported && edev->max_supported <= index))
351 return -EINVAL;
352
353 state = cable_state ? (1 << index) : 0;
MyungJoo Hambde68e62012-04-20 14:16:26 +0900354 return extcon_update_state(edev, 1 << index, state);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900355}
356EXPORT_SYMBOL_GPL(extcon_set_cable_state_);
357
358/**
Axel Lin909f9ec2012-10-04 09:53:45 +0900359 * extcon_set_cable_state() - Set the status of a specific cable.
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900360 * @edev: the extcon device that has the cable.
361 * @cable_name: cable name.
362 * @cable_state: the new cable status. The default semantics is
363 * true: attached / false: detached.
364 *
365 * Note that this is slower than extcon_set_cable_state_.
366 */
367int extcon_set_cable_state(struct extcon_dev *edev,
368 const char *cable_name, bool cable_state)
369{
370 return extcon_set_cable_state_(edev, extcon_find_cable_index
371 (edev, cable_name), cable_state);
372}
373EXPORT_SYMBOL_GPL(extcon_set_cable_state);
374
375/**
Donggeun Kim74c5d092012-04-20 14:16:24 +0900376 * extcon_get_extcon_dev() - Get the extcon device instance from the name
377 * @extcon_name: The extcon name provided with extcon_dev_register()
378 */
379struct extcon_dev *extcon_get_extcon_dev(const char *extcon_name)
380{
381 struct extcon_dev *sd;
382
383 mutex_lock(&extcon_dev_list_lock);
384 list_for_each_entry(sd, &extcon_dev_list, entry) {
385 if (!strcmp(sd->name, extcon_name))
386 goto out;
387 }
388 sd = NULL;
389out:
390 mutex_unlock(&extcon_dev_list_lock);
391 return sd;
392}
393EXPORT_SYMBOL_GPL(extcon_get_extcon_dev);
394
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900395static int _call_per_cable(struct notifier_block *nb, unsigned long val,
396 void *ptr)
397{
398 struct extcon_specific_cable_nb *obj = container_of(nb,
399 struct extcon_specific_cable_nb, internal_nb);
400 struct extcon_dev *edev = ptr;
401
402 if ((val & (1 << obj->cable_index)) !=
403 (edev->state & (1 << obj->cable_index))) {
Chanwoo Choif4cce692012-04-27 15:17:28 +0900404 bool cable_state = true;
405
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900406 obj->previous_value = val;
Chanwoo Choif4cce692012-04-27 15:17:28 +0900407
408 if (val & (1 << obj->cable_index))
409 cable_state = false;
410
411 return obj->user_nb->notifier_call(obj->user_nb,
412 cable_state, ptr);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900413 }
414
415 return NOTIFY_OK;
416}
417
418/**
419 * extcon_register_interest() - Register a notifier for a state change of a
Peter Meerwaldc338bb02012-08-23 09:11:54 +0900420 * specific cable, not an entier set of cables of a
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900421 * extcon device.
422 * @obj: an empty extcon_specific_cable_nb object to be returned.
423 * @extcon_name: the name of extcon device.
Jenny TC4f2de3b2012-10-18 21:00:32 +0900424 * if NULL, extcon_register_interest will register
425 * every cable with the target cable_name given.
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900426 * @cable_name: the target cable name.
427 * @nb: the notifier block to get notified.
428 *
429 * Provide an empty extcon_specific_cable_nb. extcon_register_interest() sets
430 * the struct for you.
431 *
432 * extcon_register_interest is a helper function for those who want to get
433 * notification for a single specific cable's status change. If a user wants
434 * to get notification for any changes of all cables of a extcon device,
435 * he/she should use the general extcon_register_notifier().
436 *
437 * Note that the second parameter given to the callback of nb (val) is
438 * "old_state", not the current state. The current state can be retrieved
439 * by looking at the third pameter (edev pointer)'s state value.
440 */
441int extcon_register_interest(struct extcon_specific_cable_nb *obj,
442 const char *extcon_name, const char *cable_name,
443 struct notifier_block *nb)
444{
Jenny TC4f2de3b2012-10-18 21:00:32 +0900445 if (!obj || !cable_name || !nb)
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900446 return -EINVAL;
447
Jenny TC4f2de3b2012-10-18 21:00:32 +0900448 if (extcon_name) {
449 obj->edev = extcon_get_extcon_dev(extcon_name);
450 if (!obj->edev)
451 return -ENODEV;
452
453 obj->cable_index = extcon_find_cable_index(obj->edev, cable_name);
454 if (obj->cable_index < 0)
Sachin Kamatc0c078c2012-11-20 16:30:31 +0900455 return obj->cable_index;
Jenny TC4f2de3b2012-10-18 21:00:32 +0900456
457 obj->user_nb = nb;
458
459 obj->internal_nb.notifier_call = _call_per_cable;
460
461 return raw_notifier_chain_register(&obj->edev->nh, &obj->internal_nb);
462 } else {
463 struct class_dev_iter iter;
464 struct extcon_dev *extd;
465 struct device *dev;
466
467 if (!extcon_class)
468 return -ENODEV;
469 class_dev_iter_init(&iter, extcon_class, NULL, NULL);
470 while ((dev = class_dev_iter_next(&iter))) {
471 extd = (struct extcon_dev *)dev_get_drvdata(dev);
472
473 if (extcon_find_cable_index(extd, cable_name) < 0)
474 continue;
475
476 class_dev_iter_exit(&iter);
477 return extcon_register_interest(obj, extd->name,
478 cable_name, nb);
479 }
480
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900481 return -ENODEV;
Jenny TC4f2de3b2012-10-18 21:00:32 +0900482 }
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900483}
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
MyungJoo Hamde55d872012-04-20 14:16:22 +0900552static void extcon_dev_release(struct device *dev)
553{
anish kumar57e7cd32012-10-22 09:43:33 +0900554 kfree(dev);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900555}
556
MyungJoo Hambde68e62012-04-20 14:16:26 +0900557static const char *muex_name = "mutually_exclusive";
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900558static void dummy_sysfs_dev_release(struct device *dev)
559{
560}
561
MyungJoo Hamde55d872012-04-20 14:16:22 +0900562/**
563 * extcon_dev_register() - Register a new extcon device
564 * @edev : the new extcon device (should be allocated before calling)
565 * @dev : the parent device for this extcon device.
566 *
567 * Among the members of edev struct, please set the "user initializing data"
568 * in any case and set the "optional callbacks" if required. However, please
569 * do not set the values of "internal data", which are initialized by
570 * this function.
571 */
572int extcon_dev_register(struct extcon_dev *edev, struct device *dev)
573{
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900574 int ret, index = 0;
MyungJoo Hamde55d872012-04-20 14:16:22 +0900575
576 if (!extcon_class) {
577 ret = create_extcon_class();
578 if (ret < 0)
579 return ret;
580 }
581
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900582 if (edev->supported_cable) {
583 /* Get size of array */
584 for (index = 0; edev->supported_cable[index]; index++)
585 ;
586 edev->max_supported = index;
587 } else {
588 edev->max_supported = 0;
589 }
590
591 if (index > SUPPORTED_CABLE_MAX) {
592 dev_err(edev->dev, "extcon: maximum number of supported cables exceeded.\n");
593 return -EINVAL;
594 }
595
MyungJoo Hamde55d872012-04-20 14:16:22 +0900596 edev->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
Dan Carpentera1d26ac2012-04-25 11:47:02 +0300597 if (!edev->dev)
598 return -ENOMEM;
MyungJoo Hamde55d872012-04-20 14:16:22 +0900599 edev->dev->parent = dev;
600 edev->dev->class = extcon_class;
601 edev->dev->release = extcon_dev_release;
602
603 dev_set_name(edev->dev, edev->name ? edev->name : dev_name(dev));
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900604
605 if (edev->max_supported) {
606 char buf[10];
607 char *str;
608 struct extcon_cable *cable;
609
610 edev->cables = kzalloc(sizeof(struct extcon_cable) *
611 edev->max_supported, GFP_KERNEL);
612 if (!edev->cables) {
613 ret = -ENOMEM;
614 goto err_sysfs_alloc;
615 }
616 for (index = 0; index < edev->max_supported; index++) {
617 cable = &edev->cables[index];
618
619 snprintf(buf, 10, "cable.%d", index);
620 str = kzalloc(sizeof(char) * (strlen(buf) + 1),
621 GFP_KERNEL);
622 if (!str) {
623 for (index--; index >= 0; index--) {
624 cable = &edev->cables[index];
625 kfree(cable->attr_g.name);
626 }
627 ret = -ENOMEM;
628
629 goto err_alloc_cables;
630 }
631 strcpy(str, buf);
632
633 cable->edev = edev;
634 cable->cable_index = index;
635 cable->attrs[0] = &cable->attr_name.attr;
636 cable->attrs[1] = &cable->attr_state.attr;
637 cable->attrs[2] = NULL;
638 cable->attr_g.name = str;
639 cable->attr_g.attrs = cable->attrs;
640
Mark Brown9baf3222012-08-16 20:03:21 +0100641 sysfs_attr_init(&cable->attr_name.attr);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900642 cable->attr_name.attr.name = "name";
643 cable->attr_name.attr.mode = 0444;
644 cable->attr_name.show = cable_name_show;
645
Mark Brown9baf3222012-08-16 20:03:21 +0100646 sysfs_attr_init(&cable->attr_state.attr);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900647 cable->attr_state.attr.name = "state";
Chanwoo Choiea9dd9d2013-05-22 19:31:59 +0900648 cable->attr_state.attr.mode = 0444;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900649 cable->attr_state.show = cable_state_show;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900650 }
651 }
652
MyungJoo Hambde68e62012-04-20 14:16:26 +0900653 if (edev->max_supported && edev->mutually_exclusive) {
654 char buf[80];
655 char *name;
656
657 /* Count the size of mutually_exclusive array */
658 for (index = 0; edev->mutually_exclusive[index]; index++)
659 ;
660
661 edev->attrs_muex = kzalloc(sizeof(struct attribute *) *
662 (index + 1), GFP_KERNEL);
663 if (!edev->attrs_muex) {
664 ret = -ENOMEM;
665 goto err_muex;
666 }
667
668 edev->d_attrs_muex = kzalloc(sizeof(struct device_attribute) *
669 index, GFP_KERNEL);
670 if (!edev->d_attrs_muex) {
671 ret = -ENOMEM;
672 kfree(edev->attrs_muex);
673 goto err_muex;
674 }
675
676 for (index = 0; edev->mutually_exclusive[index]; index++) {
677 sprintf(buf, "0x%x", edev->mutually_exclusive[index]);
678 name = kzalloc(sizeof(char) * (strlen(buf) + 1),
679 GFP_KERNEL);
680 if (!name) {
681 for (index--; index >= 0; index--) {
682 kfree(edev->d_attrs_muex[index].attr.
683 name);
684 }
685 kfree(edev->d_attrs_muex);
686 kfree(edev->attrs_muex);
687 ret = -ENOMEM;
688 goto err_muex;
689 }
690 strcpy(name, buf);
Mark Brown9baf3222012-08-16 20:03:21 +0100691 sysfs_attr_init(&edev->d_attrs_muex[index].attr);
MyungJoo Hambde68e62012-04-20 14:16:26 +0900692 edev->d_attrs_muex[index].attr.name = name;
693 edev->d_attrs_muex[index].attr.mode = 0000;
694 edev->attrs_muex[index] = &edev->d_attrs_muex[index]
695 .attr;
696 }
697 edev->attr_g_muex.name = muex_name;
698 edev->attr_g_muex.attrs = edev->attrs_muex;
699
700 }
701
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900702 if (edev->max_supported) {
703 edev->extcon_dev_type.groups =
704 kzalloc(sizeof(struct attribute_group *) *
MyungJoo Hambde68e62012-04-20 14:16:26 +0900705 (edev->max_supported + 2), GFP_KERNEL);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900706 if (!edev->extcon_dev_type.groups) {
707 ret = -ENOMEM;
708 goto err_alloc_groups;
709 }
710
711 edev->extcon_dev_type.name = dev_name(edev->dev);
712 edev->extcon_dev_type.release = dummy_sysfs_dev_release;
713
714 for (index = 0; index < edev->max_supported; index++)
715 edev->extcon_dev_type.groups[index] =
716 &edev->cables[index].attr_g;
MyungJoo Hambde68e62012-04-20 14:16:26 +0900717 if (edev->mutually_exclusive)
718 edev->extcon_dev_type.groups[index] =
719 &edev->attr_g_muex;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900720
721 edev->dev->type = &edev->extcon_dev_type;
722 }
723
MyungJoo Hamde55d872012-04-20 14:16:22 +0900724 ret = device_register(edev->dev);
725 if (ret) {
726 put_device(edev->dev);
727 goto err_dev;
728 }
MyungJoo Ham449a2bf2012-04-23 20:19:57 +0900729#if defined(CONFIG_ANDROID)
MyungJoo Hamde55d872012-04-20 14:16:22 +0900730 if (switch_class)
731 ret = class_compat_create_link(switch_class, edev->dev,
Mark Brownc3b15452012-06-05 16:43:53 +0100732 NULL);
MyungJoo Ham449a2bf2012-04-23 20:19:57 +0900733#endif /* CONFIG_ANDROID */
MyungJoo Hamde55d872012-04-20 14:16:22 +0900734
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900735 spin_lock_init(&edev->lock);
736
Donggeun Kim74c5d092012-04-20 14:16:24 +0900737 RAW_INIT_NOTIFIER_HEAD(&edev->nh);
738
MyungJoo Hamde55d872012-04-20 14:16:22 +0900739 dev_set_drvdata(edev->dev, edev);
740 edev->state = 0;
Donggeun Kim74c5d092012-04-20 14:16:24 +0900741
742 mutex_lock(&extcon_dev_list_lock);
743 list_add(&edev->entry, &extcon_dev_list);
744 mutex_unlock(&extcon_dev_list_lock);
745
MyungJoo Hamde55d872012-04-20 14:16:22 +0900746 return 0;
747
748err_dev:
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900749 if (edev->max_supported)
750 kfree(edev->extcon_dev_type.groups);
751err_alloc_groups:
MyungJoo Hambde68e62012-04-20 14:16:26 +0900752 if (edev->max_supported && edev->mutually_exclusive) {
753 for (index = 0; edev->mutually_exclusive[index]; index++)
754 kfree(edev->d_attrs_muex[index].attr.name);
755 kfree(edev->d_attrs_muex);
756 kfree(edev->attrs_muex);
757 }
758err_muex:
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900759 for (index = 0; index < edev->max_supported; index++)
760 kfree(edev->cables[index].attr_g.name);
761err_alloc_cables:
762 if (edev->max_supported)
763 kfree(edev->cables);
764err_sysfs_alloc:
MyungJoo Hamde55d872012-04-20 14:16:22 +0900765 kfree(edev->dev);
766 return ret;
767}
768EXPORT_SYMBOL_GPL(extcon_dev_register);
769
770/**
771 * extcon_dev_unregister() - Unregister the extcon device.
Peter Meerwaldc338bb02012-08-23 09:11:54 +0900772 * @edev: the extcon device instance to be unregistered.
MyungJoo Hamde55d872012-04-20 14:16:22 +0900773 *
774 * Note that this does not call kfree(edev) because edev was not allocated
775 * by this class.
776 */
777void extcon_dev_unregister(struct extcon_dev *edev)
778{
anish kumar57e7cd32012-10-22 09:43:33 +0900779 int index;
780
781 mutex_lock(&extcon_dev_list_lock);
782 list_del(&edev->entry);
783 mutex_unlock(&extcon_dev_list_lock);
784
785 if (IS_ERR_OR_NULL(get_device(edev->dev))) {
786 dev_err(edev->dev, "Failed to unregister extcon_dev (%s)\n",
787 dev_name(edev->dev));
788 return;
789 }
790
791 if (edev->mutually_exclusive && edev->max_supported) {
792 for (index = 0; edev->mutually_exclusive[index];
793 index++)
794 kfree(edev->d_attrs_muex[index].attr.name);
795 kfree(edev->d_attrs_muex);
796 kfree(edev->attrs_muex);
797 }
798
799 for (index = 0; index < edev->max_supported; index++)
800 kfree(edev->cables[index].attr_g.name);
801
802 if (edev->max_supported) {
803 kfree(edev->extcon_dev_type.groups);
804 kfree(edev->cables);
805 }
806
807#if defined(CONFIG_ANDROID)
808 if (switch_class)
809 class_compat_remove_link(switch_class, edev->dev, NULL);
810#endif
811 device_unregister(edev->dev);
812 put_device(edev->dev);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900813}
814EXPORT_SYMBOL_GPL(extcon_dev_unregister);
815
816static int __init extcon_class_init(void)
817{
818 return create_extcon_class();
819}
820module_init(extcon_class_init);
821
822static void __exit extcon_class_exit(void)
823{
Peter Huewe0dc77b62012-09-24 15:32:31 +0900824#if defined(CONFIG_ANDROID)
825 class_compat_unregister(switch_class);
826#endif
MyungJoo Hamde55d872012-04-20 14:16:22 +0900827 class_destroy(extcon_class);
828}
829module_exit(extcon_class_exit);
830
831MODULE_AUTHOR("Mike Lockwood <lockwood@android.com>");
832MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>");
833MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
834MODULE_DESCRIPTION("External connector (extcon) class driver");
835MODULE_LICENSE("GPL");