blob: 39d3e4d48472939b7bdf383de0eb7108ccbfac4e [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
188static ssize_t cable_state_store(struct device *dev,
189 struct device_attribute *attr, const char *buf,
190 size_t count)
191{
192 struct extcon_cable *cable = container_of(attr, struct extcon_cable,
193 attr_state);
194 int ret, state;
195
196 ret = sscanf(buf, "%d", &state);
197 if (ret == 0)
198 ret = -EINVAL;
199 else
200 ret = extcon_set_cable_state_(cable->edev, cable->cable_index,
201 state);
202
203 if (ret < 0)
204 return ret;
205 return count;
206}
207
MyungJoo Hamde55d872012-04-20 14:16:22 +0900208/**
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900209 * extcon_update_state() - Update the cable attach states of the extcon device
210 * only for the masked bits.
MyungJoo Hamde55d872012-04-20 14:16:22 +0900211 * @edev: the extcon device
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900212 * @mask: the bit mask to designate updated bits.
MyungJoo Hamde55d872012-04-20 14:16:22 +0900213 * @state: new cable attach status for @edev
214 *
215 * Changing the state sends uevent with environment variable containing
216 * the name of extcon device (envp[0]) and the state output (envp[1]).
217 * Tizen uses this format for extcon device to get events from ports.
218 * Android uses this format as well.
Donggeun Kim74c5d092012-04-20 14:16:24 +0900219 *
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900220 * Note that the notifier provides which bits are changed in the state
221 * variable with the val parameter (second) to the callback.
MyungJoo Hamde55d872012-04-20 14:16:22 +0900222 */
MyungJoo Hambde68e62012-04-20 14:16:26 +0900223int extcon_update_state(struct extcon_dev *edev, u32 mask, u32 state)
MyungJoo Hamde55d872012-04-20 14:16:22 +0900224{
225 char name_buf[120];
226 char state_buf[120];
227 char *prop_buf;
228 char *envp[3];
229 int env_offset = 0;
230 int length;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900231 unsigned long flags;
MyungJoo Hamde55d872012-04-20 14:16:22 +0900232
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900233 spin_lock_irqsave(&edev->lock, flags);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900234
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900235 if (edev->state != ((edev->state & ~mask) | (state & mask))) {
236 u32 old_state = edev->state;
Donggeun Kim74c5d092012-04-20 14:16:24 +0900237
MyungJoo Hambde68e62012-04-20 14:16:26 +0900238 if (check_mutually_exclusive(edev, (edev->state & ~mask) |
239 (state & mask))) {
240 spin_unlock_irqrestore(&edev->lock, flags);
241 return -EPERM;
242 }
243
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900244 edev->state &= ~mask;
245 edev->state |= state & mask;
246
247 raw_notifier_call_chain(&edev->nh, old_state, edev);
248
249 /* This could be in interrupt handler */
250 prop_buf = (char *)get_zeroed_page(GFP_ATOMIC);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900251 if (prop_buf) {
252 length = name_show(edev->dev, NULL, prop_buf);
253 if (length > 0) {
254 if (prop_buf[length - 1] == '\n')
255 prop_buf[length - 1] = 0;
256 snprintf(name_buf, sizeof(name_buf),
257 "NAME=%s", prop_buf);
258 envp[env_offset++] = name_buf;
259 }
260 length = state_show(edev->dev, NULL, prop_buf);
261 if (length > 0) {
262 if (prop_buf[length - 1] == '\n')
263 prop_buf[length - 1] = 0;
264 snprintf(state_buf, sizeof(state_buf),
265 "STATE=%s", prop_buf);
266 envp[env_offset++] = state_buf;
267 }
268 envp[env_offset] = NULL;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900269 /* Unlock early before uevent */
270 spin_unlock_irqrestore(&edev->lock, flags);
271
MyungJoo Hamde55d872012-04-20 14:16:22 +0900272 kobject_uevent_env(&edev->dev->kobj, KOBJ_CHANGE, envp);
273 free_page((unsigned long)prop_buf);
274 } else {
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900275 /* Unlock early before uevent */
276 spin_unlock_irqrestore(&edev->lock, flags);
277
MyungJoo Hamde55d872012-04-20 14:16:22 +0900278 dev_err(edev->dev, "out of memory in extcon_set_state\n");
279 kobject_uevent(&edev->dev->kobj, KOBJ_CHANGE);
280 }
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900281 } else {
282 /* No changes */
283 spin_unlock_irqrestore(&edev->lock, flags);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900284 }
MyungJoo Hambde68e62012-04-20 14:16:26 +0900285
286 return 0;
MyungJoo Hamde55d872012-04-20 14:16:22 +0900287}
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900288EXPORT_SYMBOL_GPL(extcon_update_state);
289
290/**
291 * extcon_set_state() - Set the cable attach states of the extcon device.
292 * @edev: the extcon device
293 * @state: new cable attach status for @edev
294 *
295 * Note that notifier provides which bits are changed in the state
296 * variable with the val parameter (second) to the callback.
297 */
MyungJoo Hambde68e62012-04-20 14:16:26 +0900298int extcon_set_state(struct extcon_dev *edev, u32 state)
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900299{
MyungJoo Hambde68e62012-04-20 14:16:26 +0900300 return extcon_update_state(edev, 0xffffffff, state);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900301}
MyungJoo Hamde55d872012-04-20 14:16:22 +0900302EXPORT_SYMBOL_GPL(extcon_set_state);
303
Donggeun Kim74c5d092012-04-20 14:16:24 +0900304/**
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900305 * extcon_find_cable_index() - Get the cable index based on the cable name.
306 * @edev: the extcon device that has the cable.
307 * @cable_name: cable name to be searched.
308 *
309 * Note that accessing a cable state based on cable_index is faster than
310 * cable_name because using cable_name induces a loop with strncmp().
311 * Thus, when get/set_cable_state is repeatedly used, using cable_index
312 * is recommended.
313 */
314int extcon_find_cable_index(struct extcon_dev *edev, const char *cable_name)
315{
316 int i;
317
318 if (edev->supported_cable) {
319 for (i = 0; edev->supported_cable[i]; i++) {
320 if (!strncmp(edev->supported_cable[i],
321 cable_name, CABLE_NAME_MAX))
322 return i;
323 }
324 }
325
326 return -EINVAL;
327}
328EXPORT_SYMBOL_GPL(extcon_find_cable_index);
329
330/**
331 * extcon_get_cable_state_() - Get the status of a specific cable.
332 * @edev: the extcon device that has the cable.
333 * @index: cable index that can be retrieved by extcon_find_cable_index().
334 */
335int extcon_get_cable_state_(struct extcon_dev *edev, int index)
336{
337 if (index < 0 || (edev->max_supported && edev->max_supported <= index))
338 return -EINVAL;
339
340 return !!(edev->state & (1 << index));
341}
342EXPORT_SYMBOL_GPL(extcon_get_cable_state_);
343
344/**
345 * extcon_get_cable_state() - Get the status of a specific cable.
346 * @edev: the extcon device that has the cable.
347 * @cable_name: cable name.
348 *
349 * Note that this is slower than extcon_get_cable_state_.
350 */
351int extcon_get_cable_state(struct extcon_dev *edev, const char *cable_name)
352{
353 return extcon_get_cable_state_(edev, extcon_find_cable_index
354 (edev, cable_name));
355}
356EXPORT_SYMBOL_GPL(extcon_get_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 * @index: cable index that can be retrieved by extcon_find_cable_index().
362 * @cable_state: the new cable status. The default semantics is
363 * true: attached / false: detached.
364 */
365int extcon_set_cable_state_(struct extcon_dev *edev,
366 int index, bool cable_state)
367{
368 u32 state;
369
370 if (index < 0 || (edev->max_supported && edev->max_supported <= index))
371 return -EINVAL;
372
373 state = cable_state ? (1 << index) : 0;
MyungJoo Hambde68e62012-04-20 14:16:26 +0900374 return extcon_update_state(edev, 1 << index, state);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900375}
376EXPORT_SYMBOL_GPL(extcon_set_cable_state_);
377
378/**
Axel Lin909f9ec2012-10-04 09:53:45 +0900379 * extcon_set_cable_state() - Set the status of a specific cable.
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900380 * @edev: the extcon device that has the cable.
381 * @cable_name: cable name.
382 * @cable_state: the new cable status. The default semantics is
383 * true: attached / false: detached.
384 *
385 * Note that this is slower than extcon_set_cable_state_.
386 */
387int extcon_set_cable_state(struct extcon_dev *edev,
388 const char *cable_name, bool cable_state)
389{
390 return extcon_set_cable_state_(edev, extcon_find_cable_index
391 (edev, cable_name), cable_state);
392}
393EXPORT_SYMBOL_GPL(extcon_set_cable_state);
394
395/**
Donggeun Kim74c5d092012-04-20 14:16:24 +0900396 * extcon_get_extcon_dev() - Get the extcon device instance from the name
397 * @extcon_name: The extcon name provided with extcon_dev_register()
398 */
399struct extcon_dev *extcon_get_extcon_dev(const char *extcon_name)
400{
401 struct extcon_dev *sd;
402
403 mutex_lock(&extcon_dev_list_lock);
404 list_for_each_entry(sd, &extcon_dev_list, entry) {
405 if (!strcmp(sd->name, extcon_name))
406 goto out;
407 }
408 sd = NULL;
409out:
410 mutex_unlock(&extcon_dev_list_lock);
411 return sd;
412}
413EXPORT_SYMBOL_GPL(extcon_get_extcon_dev);
414
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900415static int _call_per_cable(struct notifier_block *nb, unsigned long val,
416 void *ptr)
417{
418 struct extcon_specific_cable_nb *obj = container_of(nb,
419 struct extcon_specific_cable_nb, internal_nb);
420 struct extcon_dev *edev = ptr;
421
422 if ((val & (1 << obj->cable_index)) !=
423 (edev->state & (1 << obj->cable_index))) {
Chanwoo Choif4cce692012-04-27 15:17:28 +0900424 bool cable_state = true;
425
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900426 obj->previous_value = val;
Chanwoo Choif4cce692012-04-27 15:17:28 +0900427
428 if (val & (1 << obj->cable_index))
429 cable_state = false;
430
431 return obj->user_nb->notifier_call(obj->user_nb,
432 cable_state, ptr);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900433 }
434
435 return NOTIFY_OK;
436}
437
438/**
439 * extcon_register_interest() - Register a notifier for a state change of a
Peter Meerwaldc338bb02012-08-23 09:11:54 +0900440 * specific cable, not an entier set of cables of a
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900441 * extcon device.
442 * @obj: an empty extcon_specific_cable_nb object to be returned.
443 * @extcon_name: the name of extcon device.
444 * @cable_name: the target cable name.
445 * @nb: the notifier block to get notified.
446 *
447 * Provide an empty extcon_specific_cable_nb. extcon_register_interest() sets
448 * the struct for you.
449 *
450 * extcon_register_interest is a helper function for those who want to get
451 * notification for a single specific cable's status change. If a user wants
452 * to get notification for any changes of all cables of a extcon device,
453 * he/she should use the general extcon_register_notifier().
454 *
455 * Note that the second parameter given to the callback of nb (val) is
456 * "old_state", not the current state. The current state can be retrieved
457 * by looking at the third pameter (edev pointer)'s state value.
458 */
459int extcon_register_interest(struct extcon_specific_cable_nb *obj,
460 const char *extcon_name, const char *cable_name,
461 struct notifier_block *nb)
462{
463 if (!obj || !extcon_name || !cable_name || !nb)
464 return -EINVAL;
465
466 obj->edev = extcon_get_extcon_dev(extcon_name);
467 if (!obj->edev)
468 return -ENODEV;
469
470 obj->cable_index = extcon_find_cable_index(obj->edev, cable_name);
471 if (obj->cable_index < 0)
Sachin Kamat5cd3c272012-09-26 08:10:03 +0900472 return -EINVAL;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900473
474 obj->user_nb = nb;
475
476 obj->internal_nb.notifier_call = _call_per_cable;
477
478 return raw_notifier_chain_register(&obj->edev->nh, &obj->internal_nb);
479}
480
481/**
482 * extcon_unregister_interest() - Unregister the notifier registered by
483 * extcon_register_interest().
484 * @obj: the extcon_specific_cable_nb object returned by
485 * extcon_register_interest().
486 */
487int extcon_unregister_interest(struct extcon_specific_cable_nb *obj)
488{
489 if (!obj)
490 return -EINVAL;
491
492 return raw_notifier_chain_unregister(&obj->edev->nh, &obj->internal_nb);
493}
494
Donggeun Kim74c5d092012-04-20 14:16:24 +0900495/**
Peter Meerwaldc338bb02012-08-23 09:11:54 +0900496 * extcon_register_notifier() - Register a notifiee to get notified by
Donggeun Kim74c5d092012-04-20 14:16:24 +0900497 * any attach status changes from the extcon.
498 * @edev: the extcon device.
499 * @nb: a notifier block to be registered.
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900500 *
501 * Note that the second parameter given to the callback of nb (val) is
502 * "old_state", not the current state. The current state can be retrieved
503 * by looking at the third pameter (edev pointer)'s state value.
Donggeun Kim74c5d092012-04-20 14:16:24 +0900504 */
505int extcon_register_notifier(struct extcon_dev *edev,
506 struct notifier_block *nb)
507{
508 return raw_notifier_chain_register(&edev->nh, nb);
509}
510EXPORT_SYMBOL_GPL(extcon_register_notifier);
511
512/**
Peter Meerwaldc338bb02012-08-23 09:11:54 +0900513 * extcon_unregister_notifier() - Unregister a notifiee from the extcon device.
Donggeun Kim74c5d092012-04-20 14:16:24 +0900514 * @edev: the extcon device.
515 * @nb: a registered notifier block to be unregistered.
516 */
517int extcon_unregister_notifier(struct extcon_dev *edev,
518 struct notifier_block *nb)
519{
520 return raw_notifier_chain_unregister(&edev->nh, nb);
521}
522EXPORT_SYMBOL_GPL(extcon_unregister_notifier);
523
MyungJoo Hamde55d872012-04-20 14:16:22 +0900524static struct device_attribute extcon_attrs[] = {
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900525 __ATTR(state, S_IRUGO | S_IWUSR, state_show, state_store),
MyungJoo Hamde55d872012-04-20 14:16:22 +0900526 __ATTR_RO(name),
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900527 __ATTR_NULL,
MyungJoo Hamde55d872012-04-20 14:16:22 +0900528};
529
530static int create_extcon_class(void)
531{
532 if (!extcon_class) {
533 extcon_class = class_create(THIS_MODULE, "extcon");
534 if (IS_ERR(extcon_class))
535 return PTR_ERR(extcon_class);
536 extcon_class->dev_attrs = extcon_attrs;
537
MyungJoo Ham449a2bf2012-04-23 20:19:57 +0900538#if defined(CONFIG_ANDROID)
MyungJoo Hamde55d872012-04-20 14:16:22 +0900539 switch_class = class_compat_register("switch");
540 if (WARN(!switch_class, "cannot allocate"))
541 return -ENOMEM;
MyungJoo Ham449a2bf2012-04-23 20:19:57 +0900542#endif /* CONFIG_ANDROID */
MyungJoo Hamde55d872012-04-20 14:16:22 +0900543 }
544
545 return 0;
546}
547
MyungJoo Hamde55d872012-04-20 14:16:22 +0900548static void extcon_dev_release(struct device *dev)
549{
anish kumar57e7cd32012-10-22 09:43:33 +0900550 kfree(dev);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900551}
552
MyungJoo Hambde68e62012-04-20 14:16:26 +0900553static const char *muex_name = "mutually_exclusive";
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900554static void dummy_sysfs_dev_release(struct device *dev)
555{
556}
557
MyungJoo Hamde55d872012-04-20 14:16:22 +0900558/**
559 * extcon_dev_register() - Register a new extcon device
560 * @edev : the new extcon device (should be allocated before calling)
561 * @dev : the parent device for this extcon device.
562 *
563 * Among the members of edev struct, please set the "user initializing data"
564 * in any case and set the "optional callbacks" if required. However, please
565 * do not set the values of "internal data", which are initialized by
566 * this function.
567 */
568int extcon_dev_register(struct extcon_dev *edev, struct device *dev)
569{
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900570 int ret, index = 0;
MyungJoo Hamde55d872012-04-20 14:16:22 +0900571
572 if (!extcon_class) {
573 ret = create_extcon_class();
574 if (ret < 0)
575 return ret;
576 }
577
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900578 if (edev->supported_cable) {
579 /* Get size of array */
580 for (index = 0; edev->supported_cable[index]; index++)
581 ;
582 edev->max_supported = index;
583 } else {
584 edev->max_supported = 0;
585 }
586
587 if (index > SUPPORTED_CABLE_MAX) {
588 dev_err(edev->dev, "extcon: maximum number of supported cables exceeded.\n");
589 return -EINVAL;
590 }
591
MyungJoo Hamde55d872012-04-20 14:16:22 +0900592 edev->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
Dan Carpentera1d26ac2012-04-25 11:47:02 +0300593 if (!edev->dev)
594 return -ENOMEM;
MyungJoo Hamde55d872012-04-20 14:16:22 +0900595 edev->dev->parent = dev;
596 edev->dev->class = extcon_class;
597 edev->dev->release = extcon_dev_release;
598
599 dev_set_name(edev->dev, edev->name ? edev->name : dev_name(dev));
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900600
601 if (edev->max_supported) {
602 char buf[10];
603 char *str;
604 struct extcon_cable *cable;
605
606 edev->cables = kzalloc(sizeof(struct extcon_cable) *
607 edev->max_supported, GFP_KERNEL);
608 if (!edev->cables) {
609 ret = -ENOMEM;
610 goto err_sysfs_alloc;
611 }
612 for (index = 0; index < edev->max_supported; index++) {
613 cable = &edev->cables[index];
614
615 snprintf(buf, 10, "cable.%d", index);
616 str = kzalloc(sizeof(char) * (strlen(buf) + 1),
617 GFP_KERNEL);
618 if (!str) {
619 for (index--; index >= 0; index--) {
620 cable = &edev->cables[index];
621 kfree(cable->attr_g.name);
622 }
623 ret = -ENOMEM;
624
625 goto err_alloc_cables;
626 }
627 strcpy(str, buf);
628
629 cable->edev = edev;
630 cable->cable_index = index;
631 cable->attrs[0] = &cable->attr_name.attr;
632 cable->attrs[1] = &cable->attr_state.attr;
633 cable->attrs[2] = NULL;
634 cable->attr_g.name = str;
635 cable->attr_g.attrs = cable->attrs;
636
Mark Brown9baf3222012-08-16 20:03:21 +0100637 sysfs_attr_init(&cable->attr_name.attr);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900638 cable->attr_name.attr.name = "name";
639 cable->attr_name.attr.mode = 0444;
640 cable->attr_name.show = cable_name_show;
641
Mark Brown9baf3222012-08-16 20:03:21 +0100642 sysfs_attr_init(&cable->attr_state.attr);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900643 cable->attr_state.attr.name = "state";
644 cable->attr_state.attr.mode = 0644;
645 cable->attr_state.show = cable_state_show;
646 cable->attr_state.store = cable_state_store;
647 }
648 }
649
MyungJoo Hambde68e62012-04-20 14:16:26 +0900650 if (edev->max_supported && edev->mutually_exclusive) {
651 char buf[80];
652 char *name;
653
654 /* Count the size of mutually_exclusive array */
655 for (index = 0; edev->mutually_exclusive[index]; index++)
656 ;
657
658 edev->attrs_muex = kzalloc(sizeof(struct attribute *) *
659 (index + 1), GFP_KERNEL);
660 if (!edev->attrs_muex) {
661 ret = -ENOMEM;
662 goto err_muex;
663 }
664
665 edev->d_attrs_muex = kzalloc(sizeof(struct device_attribute) *
666 index, GFP_KERNEL);
667 if (!edev->d_attrs_muex) {
668 ret = -ENOMEM;
669 kfree(edev->attrs_muex);
670 goto err_muex;
671 }
672
673 for (index = 0; edev->mutually_exclusive[index]; index++) {
674 sprintf(buf, "0x%x", edev->mutually_exclusive[index]);
675 name = kzalloc(sizeof(char) * (strlen(buf) + 1),
676 GFP_KERNEL);
677 if (!name) {
678 for (index--; index >= 0; index--) {
679 kfree(edev->d_attrs_muex[index].attr.
680 name);
681 }
682 kfree(edev->d_attrs_muex);
683 kfree(edev->attrs_muex);
684 ret = -ENOMEM;
685 goto err_muex;
686 }
687 strcpy(name, buf);
Mark Brown9baf3222012-08-16 20:03:21 +0100688 sysfs_attr_init(&edev->d_attrs_muex[index].attr);
MyungJoo Hambde68e62012-04-20 14:16:26 +0900689 edev->d_attrs_muex[index].attr.name = name;
690 edev->d_attrs_muex[index].attr.mode = 0000;
691 edev->attrs_muex[index] = &edev->d_attrs_muex[index]
692 .attr;
693 }
694 edev->attr_g_muex.name = muex_name;
695 edev->attr_g_muex.attrs = edev->attrs_muex;
696
697 }
698
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900699 if (edev->max_supported) {
700 edev->extcon_dev_type.groups =
701 kzalloc(sizeof(struct attribute_group *) *
MyungJoo Hambde68e62012-04-20 14:16:26 +0900702 (edev->max_supported + 2), GFP_KERNEL);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900703 if (!edev->extcon_dev_type.groups) {
704 ret = -ENOMEM;
705 goto err_alloc_groups;
706 }
707
708 edev->extcon_dev_type.name = dev_name(edev->dev);
709 edev->extcon_dev_type.release = dummy_sysfs_dev_release;
710
711 for (index = 0; index < edev->max_supported; index++)
712 edev->extcon_dev_type.groups[index] =
713 &edev->cables[index].attr_g;
MyungJoo Hambde68e62012-04-20 14:16:26 +0900714 if (edev->mutually_exclusive)
715 edev->extcon_dev_type.groups[index] =
716 &edev->attr_g_muex;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900717
718 edev->dev->type = &edev->extcon_dev_type;
719 }
720
MyungJoo Hamde55d872012-04-20 14:16:22 +0900721 ret = device_register(edev->dev);
722 if (ret) {
723 put_device(edev->dev);
724 goto err_dev;
725 }
MyungJoo Ham449a2bf2012-04-23 20:19:57 +0900726#if defined(CONFIG_ANDROID)
MyungJoo Hamde55d872012-04-20 14:16:22 +0900727 if (switch_class)
728 ret = class_compat_create_link(switch_class, edev->dev,
Mark Brownc3b15452012-06-05 16:43:53 +0100729 NULL);
MyungJoo Ham449a2bf2012-04-23 20:19:57 +0900730#endif /* CONFIG_ANDROID */
MyungJoo Hamde55d872012-04-20 14:16:22 +0900731
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900732 spin_lock_init(&edev->lock);
733
Donggeun Kim74c5d092012-04-20 14:16:24 +0900734 RAW_INIT_NOTIFIER_HEAD(&edev->nh);
735
MyungJoo Hamde55d872012-04-20 14:16:22 +0900736 dev_set_drvdata(edev->dev, edev);
737 edev->state = 0;
Donggeun Kim74c5d092012-04-20 14:16:24 +0900738
739 mutex_lock(&extcon_dev_list_lock);
740 list_add(&edev->entry, &extcon_dev_list);
741 mutex_unlock(&extcon_dev_list_lock);
742
MyungJoo Hamde55d872012-04-20 14:16:22 +0900743 return 0;
744
745err_dev:
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900746 if (edev->max_supported)
747 kfree(edev->extcon_dev_type.groups);
748err_alloc_groups:
MyungJoo Hambde68e62012-04-20 14:16:26 +0900749 if (edev->max_supported && edev->mutually_exclusive) {
750 for (index = 0; edev->mutually_exclusive[index]; index++)
751 kfree(edev->d_attrs_muex[index].attr.name);
752 kfree(edev->d_attrs_muex);
753 kfree(edev->attrs_muex);
754 }
755err_muex:
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900756 for (index = 0; index < edev->max_supported; index++)
757 kfree(edev->cables[index].attr_g.name);
758err_alloc_cables:
759 if (edev->max_supported)
760 kfree(edev->cables);
761err_sysfs_alloc:
MyungJoo Hamde55d872012-04-20 14:16:22 +0900762 kfree(edev->dev);
763 return ret;
764}
765EXPORT_SYMBOL_GPL(extcon_dev_register);
766
767/**
768 * extcon_dev_unregister() - Unregister the extcon device.
Peter Meerwaldc338bb02012-08-23 09:11:54 +0900769 * @edev: the extcon device instance to be unregistered.
MyungJoo Hamde55d872012-04-20 14:16:22 +0900770 *
771 * Note that this does not call kfree(edev) because edev was not allocated
772 * by this class.
773 */
774void extcon_dev_unregister(struct extcon_dev *edev)
775{
anish kumar57e7cd32012-10-22 09:43:33 +0900776 int index;
777
778 mutex_lock(&extcon_dev_list_lock);
779 list_del(&edev->entry);
780 mutex_unlock(&extcon_dev_list_lock);
781
782 if (IS_ERR_OR_NULL(get_device(edev->dev))) {
783 dev_err(edev->dev, "Failed to unregister extcon_dev (%s)\n",
784 dev_name(edev->dev));
785 return;
786 }
787
788 if (edev->mutually_exclusive && edev->max_supported) {
789 for (index = 0; edev->mutually_exclusive[index];
790 index++)
791 kfree(edev->d_attrs_muex[index].attr.name);
792 kfree(edev->d_attrs_muex);
793 kfree(edev->attrs_muex);
794 }
795
796 for (index = 0; index < edev->max_supported; index++)
797 kfree(edev->cables[index].attr_g.name);
798
799 if (edev->max_supported) {
800 kfree(edev->extcon_dev_type.groups);
801 kfree(edev->cables);
802 }
803
804#if defined(CONFIG_ANDROID)
805 if (switch_class)
806 class_compat_remove_link(switch_class, edev->dev, NULL);
807#endif
808 device_unregister(edev->dev);
809 put_device(edev->dev);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900810}
811EXPORT_SYMBOL_GPL(extcon_dev_unregister);
812
813static int __init extcon_class_init(void)
814{
815 return create_extcon_class();
816}
817module_init(extcon_class_init);
818
819static void __exit extcon_class_exit(void)
820{
Peter Huewe0dc77b62012-09-24 15:32:31 +0900821#if defined(CONFIG_ANDROID)
822 class_compat_unregister(switch_class);
823#endif
MyungJoo Hamde55d872012-04-20 14:16:22 +0900824 class_destroy(extcon_class);
825}
826module_exit(extcon_class_exit);
827
828MODULE_AUTHOR("Mike Lockwood <lockwood@android.com>");
829MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>");
830MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
831MODULE_DESCRIPTION("External connector (extcon) class driver");
832MODULE_LICENSE("GPL");