]>
Commit | Line | Data |
---|---|---|
de55d871 MH |
1 | /* |
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> | |
9baf3220 | 33 | #include <linux/sysfs.h> |
de55d871 | 34 | |
806d9dd7 MH |
35 | /* |
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 | */ | |
0cf6ad8a | 44 | const char extcon_cable_name[][CABLE_NAME_MAX + 1] = { |
806d9dd7 MH |
45 | [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", | |
0e1507c8 | 64 | [EXTCON_MECHANICAL] = "Mechanical", |
806d9dd7 MH |
65 | }; |
66 | ||
be3a07f7 | 67 | static struct class *extcon_class; |
449a2bf5 | 68 | #if defined(CONFIG_ANDROID) |
de55d871 | 69 | static struct class_compat *switch_class; |
449a2bf5 | 70 | #endif /* CONFIG_ANDROID */ |
de55d871 | 71 | |
74c5d09b DK |
72 | static LIST_HEAD(extcon_dev_list); |
73 | static DEFINE_MUTEX(extcon_dev_list_lock); | |
74 | ||
bde68e60 MH |
75 | /** |
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 | */ | |
84 | static int check_mutually_exclusive(struct extcon_dev *edev, u32 new_state) | |
85 | { | |
86 | int i = 0; | |
87 | ||
88 | if (!edev->mutually_exclusive) | |
89 | return 0; | |
90 | ||
91 | for (i = 0; edev->mutually_exclusive[i]; i++) { | |
92 | int count = 0, j; | |
93 | u32 correspondants = new_state & edev->mutually_exclusive[i]; | |
94 | u32 exp = 1; | |
95 | ||
96 | for (j = 0; j < 32; j++) { | |
97 | if (exp & correspondants) | |
98 | count++; | |
99 | if (count > 1) | |
100 | return i + 1; | |
101 | exp <<= 1; | |
102 | } | |
103 | } | |
104 | ||
105 | return 0; | |
106 | } | |
107 | ||
de55d871 MH |
108 | static ssize_t state_show(struct device *dev, struct device_attribute *attr, |
109 | char *buf) | |
110 | { | |
806d9dd7 | 111 | int i, count = 0; |
de55d871 MH |
112 | struct extcon_dev *edev = (struct extcon_dev *) dev_get_drvdata(dev); |
113 | ||
114 | if (edev->print_state) { | |
115 | int ret = edev->print_state(edev, buf); | |
116 | ||
117 | if (ret >= 0) | |
118 | return ret; | |
119 | /* Use default if failed */ | |
120 | } | |
806d9dd7 MH |
121 | |
122 | if (edev->max_supported == 0) | |
123 | return sprintf(buf, "%u\n", edev->state); | |
124 | ||
125 | for (i = 0; i < SUPPORTED_CABLE_MAX; i++) { | |
126 | if (!edev->supported_cable[i]) | |
127 | break; | |
128 | count += sprintf(buf + count, "%s=%d\n", | |
129 | edev->supported_cable[i], | |
130 | !!(edev->state & (1 << i))); | |
131 | } | |
132 | ||
133 | return count; | |
134 | } | |
135 | ||
bde68e60 | 136 | int extcon_set_state(struct extcon_dev *edev, u32 state); |
806d9dd7 MH |
137 | static ssize_t state_store(struct device *dev, struct device_attribute *attr, |
138 | const char *buf, size_t count) | |
139 | { | |
140 | u32 state; | |
141 | ssize_t ret = 0; | |
142 | struct extcon_dev *edev = (struct extcon_dev *) dev_get_drvdata(dev); | |
143 | ||
144 | ret = sscanf(buf, "0x%x", &state); | |
145 | if (ret == 0) | |
146 | ret = -EINVAL; | |
147 | else | |
bde68e60 | 148 | ret = extcon_set_state(edev, state); |
806d9dd7 MH |
149 | |
150 | if (ret < 0) | |
151 | return ret; | |
152 | ||
153 | return count; | |
de55d871 MH |
154 | } |
155 | ||
156 | static ssize_t name_show(struct device *dev, struct device_attribute *attr, | |
157 | char *buf) | |
158 | { | |
159 | struct extcon_dev *edev = (struct extcon_dev *) dev_get_drvdata(dev); | |
160 | ||
161 | /* Optional callback given by the user */ | |
162 | if (edev->print_name) { | |
163 | int ret = edev->print_name(edev, buf); | |
164 | if (ret >= 0) | |
165 | return ret; | |
166 | } | |
167 | ||
168 | return sprintf(buf, "%s\n", dev_name(edev->dev)); | |
169 | } | |
170 | ||
806d9dd7 MH |
171 | static ssize_t cable_name_show(struct device *dev, |
172 | struct device_attribute *attr, char *buf) | |
173 | { | |
174 | struct extcon_cable *cable = container_of(attr, struct extcon_cable, | |
175 | attr_name); | |
176 | ||
177 | return sprintf(buf, "%s\n", | |
178 | cable->edev->supported_cable[cable->cable_index]); | |
179 | } | |
180 | ||
181 | static ssize_t cable_state_show(struct device *dev, | |
182 | struct device_attribute *attr, char *buf) | |
183 | { | |
184 | struct extcon_cable *cable = container_of(attr, struct extcon_cable, | |
185 | attr_state); | |
186 | ||
187 | return sprintf(buf, "%d\n", | |
188 | extcon_get_cable_state_(cable->edev, | |
189 | cable->cable_index)); | |
190 | } | |
191 | ||
192 | static ssize_t cable_state_store(struct device *dev, | |
193 | struct device_attribute *attr, const char *buf, | |
194 | size_t count) | |
195 | { | |
196 | struct extcon_cable *cable = container_of(attr, struct extcon_cable, | |
197 | attr_state); | |
198 | int ret, state; | |
199 | ||
200 | ret = sscanf(buf, "%d", &state); | |
201 | if (ret == 0) | |
202 | ret = -EINVAL; | |
203 | else | |
204 | ret = extcon_set_cable_state_(cable->edev, cable->cable_index, | |
205 | state); | |
206 | ||
207 | if (ret < 0) | |
208 | return ret; | |
209 | return count; | |
210 | } | |
211 | ||
de55d871 | 212 | /** |
806d9dd7 MH |
213 | * extcon_update_state() - Update the cable attach states of the extcon device |
214 | * only for the masked bits. | |
de55d871 | 215 | * @edev: the extcon device |
806d9dd7 | 216 | * @mask: the bit mask to designate updated bits. |
de55d871 MH |
217 | * @state: new cable attach status for @edev |
218 | * | |
219 | * Changing the state sends uevent with environment variable containing | |
220 | * the name of extcon device (envp[0]) and the state output (envp[1]). | |
221 | * Tizen uses this format for extcon device to get events from ports. | |
222 | * Android uses this format as well. | |
74c5d09b | 223 | * |
806d9dd7 MH |
224 | * Note that the notifier provides which bits are changed in the state |
225 | * variable with the val parameter (second) to the callback. | |
de55d871 | 226 | */ |
bde68e60 | 227 | int extcon_update_state(struct extcon_dev *edev, u32 mask, u32 state) |
de55d871 MH |
228 | { |
229 | char name_buf[120]; | |
230 | char state_buf[120]; | |
231 | char *prop_buf; | |
232 | char *envp[3]; | |
233 | int env_offset = 0; | |
234 | int length; | |
806d9dd7 | 235 | unsigned long flags; |
de55d871 | 236 | |
806d9dd7 | 237 | spin_lock_irqsave(&edev->lock, flags); |
de55d871 | 238 | |
806d9dd7 MH |
239 | if (edev->state != ((edev->state & ~mask) | (state & mask))) { |
240 | u32 old_state = edev->state; | |
74c5d09b | 241 | |
bde68e60 MH |
242 | if (check_mutually_exclusive(edev, (edev->state & ~mask) | |
243 | (state & mask))) { | |
244 | spin_unlock_irqrestore(&edev->lock, flags); | |
245 | return -EPERM; | |
246 | } | |
247 | ||
806d9dd7 MH |
248 | edev->state &= ~mask; |
249 | edev->state |= state & mask; | |
250 | ||
251 | raw_notifier_call_chain(&edev->nh, old_state, edev); | |
252 | ||
253 | /* This could be in interrupt handler */ | |
254 | prop_buf = (char *)get_zeroed_page(GFP_ATOMIC); | |
de55d871 MH |
255 | if (prop_buf) { |
256 | length = name_show(edev->dev, NULL, prop_buf); | |
257 | if (length > 0) { | |
258 | if (prop_buf[length - 1] == '\n') | |
259 | prop_buf[length - 1] = 0; | |
260 | snprintf(name_buf, sizeof(name_buf), | |
261 | "NAME=%s", prop_buf); | |
262 | envp[env_offset++] = name_buf; | |
263 | } | |
264 | length = state_show(edev->dev, NULL, prop_buf); | |
265 | if (length > 0) { | |
266 | if (prop_buf[length - 1] == '\n') | |
267 | prop_buf[length - 1] = 0; | |
268 | snprintf(state_buf, sizeof(state_buf), | |
269 | "STATE=%s", prop_buf); | |
270 | envp[env_offset++] = state_buf; | |
271 | } | |
272 | envp[env_offset] = NULL; | |
806d9dd7 MH |
273 | /* Unlock early before uevent */ |
274 | spin_unlock_irqrestore(&edev->lock, flags); | |
275 | ||
de55d871 MH |
276 | kobject_uevent_env(&edev->dev->kobj, KOBJ_CHANGE, envp); |
277 | free_page((unsigned long)prop_buf); | |
278 | } else { | |
806d9dd7 MH |
279 | /* Unlock early before uevent */ |
280 | spin_unlock_irqrestore(&edev->lock, flags); | |
281 | ||
de55d871 MH |
282 | dev_err(edev->dev, "out of memory in extcon_set_state\n"); |
283 | kobject_uevent(&edev->dev->kobj, KOBJ_CHANGE); | |
284 | } | |
806d9dd7 MH |
285 | } else { |
286 | /* No changes */ | |
287 | spin_unlock_irqrestore(&edev->lock, flags); | |
de55d871 | 288 | } |
bde68e60 MH |
289 | |
290 | return 0; | |
de55d871 | 291 | } |
806d9dd7 MH |
292 | EXPORT_SYMBOL_GPL(extcon_update_state); |
293 | ||
294 | /** | |
295 | * extcon_set_state() - Set the cable attach states of the extcon device. | |
296 | * @edev: the extcon device | |
297 | * @state: new cable attach status for @edev | |
298 | * | |
299 | * Note that notifier provides which bits are changed in the state | |
300 | * variable with the val parameter (second) to the callback. | |
301 | */ | |
bde68e60 | 302 | int extcon_set_state(struct extcon_dev *edev, u32 state) |
806d9dd7 | 303 | { |
bde68e60 | 304 | return extcon_update_state(edev, 0xffffffff, state); |
806d9dd7 | 305 | } |
de55d871 MH |
306 | EXPORT_SYMBOL_GPL(extcon_set_state); |
307 | ||
806d9dd7 MH |
308 | /** |
309 | * extcon_find_cable_index() - Get the cable index based on the cable name. | |
310 | * @edev: the extcon device that has the cable. | |
311 | * @cable_name: cable name to be searched. | |
312 | * | |
313 | * Note that accessing a cable state based on cable_index is faster than | |
314 | * cable_name because using cable_name induces a loop with strncmp(). | |
315 | * Thus, when get/set_cable_state is repeatedly used, using cable_index | |
316 | * is recommended. | |
317 | */ | |
318 | int extcon_find_cable_index(struct extcon_dev *edev, const char *cable_name) | |
319 | { | |
320 | int i; | |
321 | ||
322 | if (edev->supported_cable) { | |
323 | for (i = 0; edev->supported_cable[i]; i++) { | |
324 | if (!strncmp(edev->supported_cable[i], | |
325 | cable_name, CABLE_NAME_MAX)) | |
326 | return i; | |
327 | } | |
328 | } | |
329 | ||
330 | return -EINVAL; | |
331 | } | |
332 | EXPORT_SYMBOL_GPL(extcon_find_cable_index); | |
333 | ||
334 | /** | |
335 | * extcon_get_cable_state_() - Get the status of a specific cable. | |
336 | * @edev: the extcon device that has the cable. | |
337 | * @index: cable index that can be retrieved by extcon_find_cable_index(). | |
338 | */ | |
339 | int extcon_get_cable_state_(struct extcon_dev *edev, int index) | |
340 | { | |
341 | if (index < 0 || (edev->max_supported && edev->max_supported <= index)) | |
342 | return -EINVAL; | |
343 | ||
344 | return !!(edev->state & (1 << index)); | |
345 | } | |
346 | EXPORT_SYMBOL_GPL(extcon_get_cable_state_); | |
347 | ||
348 | /** | |
349 | * extcon_get_cable_state() - Get the status of a specific cable. | |
350 | * @edev: the extcon device that has the cable. | |
351 | * @cable_name: cable name. | |
352 | * | |
353 | * Note that this is slower than extcon_get_cable_state_. | |
354 | */ | |
355 | int extcon_get_cable_state(struct extcon_dev *edev, const char *cable_name) | |
356 | { | |
357 | return extcon_get_cable_state_(edev, extcon_find_cable_index | |
358 | (edev, cable_name)); | |
359 | } | |
360 | EXPORT_SYMBOL_GPL(extcon_get_cable_state); | |
361 | ||
362 | /** | |
363 | * extcon_get_cable_state_() - Set the status of a specific cable. | |
364 | * @edev: the extcon device that has the cable. | |
365 | * @index: cable index that can be retrieved by extcon_find_cable_index(). | |
366 | * @cable_state: the new cable status. The default semantics is | |
367 | * true: attached / false: detached. | |
368 | */ | |
369 | int extcon_set_cable_state_(struct extcon_dev *edev, | |
370 | int index, bool cable_state) | |
371 | { | |
372 | u32 state; | |
373 | ||
374 | if (index < 0 || (edev->max_supported && edev->max_supported <= index)) | |
375 | return -EINVAL; | |
376 | ||
377 | state = cable_state ? (1 << index) : 0; | |
bde68e60 | 378 | return extcon_update_state(edev, 1 << index, state); |
806d9dd7 MH |
379 | } |
380 | EXPORT_SYMBOL_GPL(extcon_set_cable_state_); | |
381 | ||
382 | /** | |
383 | * extcon_get_cable_state() - Set the status of a specific cable. | |
384 | * @edev: the extcon device that has the cable. | |
385 | * @cable_name: cable name. | |
386 | * @cable_state: the new cable status. The default semantics is | |
387 | * true: attached / false: detached. | |
388 | * | |
389 | * Note that this is slower than extcon_set_cable_state_. | |
390 | */ | |
391 | int extcon_set_cable_state(struct extcon_dev *edev, | |
392 | const char *cable_name, bool cable_state) | |
393 | { | |
394 | return extcon_set_cable_state_(edev, extcon_find_cable_index | |
395 | (edev, cable_name), cable_state); | |
396 | } | |
397 | EXPORT_SYMBOL_GPL(extcon_set_cable_state); | |
398 | ||
74c5d09b DK |
399 | /** |
400 | * extcon_get_extcon_dev() - Get the extcon device instance from the name | |
401 | * @extcon_name: The extcon name provided with extcon_dev_register() | |
402 | */ | |
403 | struct extcon_dev *extcon_get_extcon_dev(const char *extcon_name) | |
404 | { | |
405 | struct extcon_dev *sd; | |
406 | ||
407 | mutex_lock(&extcon_dev_list_lock); | |
408 | list_for_each_entry(sd, &extcon_dev_list, entry) { | |
409 | if (!strcmp(sd->name, extcon_name)) | |
410 | goto out; | |
411 | } | |
412 | sd = NULL; | |
413 | out: | |
414 | mutex_unlock(&extcon_dev_list_lock); | |
415 | return sd; | |
416 | } | |
417 | EXPORT_SYMBOL_GPL(extcon_get_extcon_dev); | |
418 | ||
806d9dd7 MH |
419 | static int _call_per_cable(struct notifier_block *nb, unsigned long val, |
420 | void *ptr) | |
421 | { | |
422 | struct extcon_specific_cable_nb *obj = container_of(nb, | |
423 | struct extcon_specific_cable_nb, internal_nb); | |
424 | struct extcon_dev *edev = ptr; | |
425 | ||
426 | if ((val & (1 << obj->cable_index)) != | |
427 | (edev->state & (1 << obj->cable_index))) { | |
f4cce696 CC |
428 | bool cable_state = true; |
429 | ||
806d9dd7 | 430 | obj->previous_value = val; |
f4cce696 CC |
431 | |
432 | if (val & (1 << obj->cable_index)) | |
433 | cable_state = false; | |
434 | ||
435 | return obj->user_nb->notifier_call(obj->user_nb, | |
436 | cable_state, ptr); | |
806d9dd7 MH |
437 | } |
438 | ||
439 | return NOTIFY_OK; | |
440 | } | |
441 | ||
442 | /** | |
443 | * extcon_register_interest() - Register a notifier for a state change of a | |
c338bb03 | 444 | * specific cable, not an entier set of cables of a |
806d9dd7 MH |
445 | * extcon device. |
446 | * @obj: an empty extcon_specific_cable_nb object to be returned. | |
447 | * @extcon_name: the name of extcon device. | |
448 | * @cable_name: the target cable name. | |
449 | * @nb: the notifier block to get notified. | |
450 | * | |
451 | * Provide an empty extcon_specific_cable_nb. extcon_register_interest() sets | |
452 | * the struct for you. | |
453 | * | |
454 | * extcon_register_interest is a helper function for those who want to get | |
455 | * notification for a single specific cable's status change. If a user wants | |
456 | * to get notification for any changes of all cables of a extcon device, | |
457 | * he/she should use the general extcon_register_notifier(). | |
458 | * | |
459 | * Note that the second parameter given to the callback of nb (val) is | |
460 | * "old_state", not the current state. The current state can be retrieved | |
461 | * by looking at the third pameter (edev pointer)'s state value. | |
462 | */ | |
463 | int extcon_register_interest(struct extcon_specific_cable_nb *obj, | |
464 | const char *extcon_name, const char *cable_name, | |
465 | struct notifier_block *nb) | |
466 | { | |
467 | if (!obj || !extcon_name || !cable_name || !nb) | |
468 | return -EINVAL; | |
469 | ||
470 | obj->edev = extcon_get_extcon_dev(extcon_name); | |
471 | if (!obj->edev) | |
472 | return -ENODEV; | |
473 | ||
474 | obj->cable_index = extcon_find_cable_index(obj->edev, cable_name); | |
475 | if (obj->cable_index < 0) | |
476 | return -ENODEV; | |
477 | ||
478 | obj->user_nb = nb; | |
479 | ||
480 | obj->internal_nb.notifier_call = _call_per_cable; | |
481 | ||
482 | return raw_notifier_chain_register(&obj->edev->nh, &obj->internal_nb); | |
483 | } | |
484 | ||
485 | /** | |
486 | * extcon_unregister_interest() - Unregister the notifier registered by | |
487 | * extcon_register_interest(). | |
488 | * @obj: the extcon_specific_cable_nb object returned by | |
489 | * extcon_register_interest(). | |
490 | */ | |
491 | int 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 | ||
74c5d09b | 499 | /** |
c338bb03 | 500 | * extcon_register_notifier() - Register a notifiee to get notified by |
74c5d09b DK |
501 | * any attach status changes from the extcon. |
502 | * @edev: the extcon device. | |
503 | * @nb: a notifier block to be registered. | |
806d9dd7 MH |
504 | * |
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. | |
74c5d09b DK |
508 | */ |
509 | int extcon_register_notifier(struct extcon_dev *edev, | |
510 | struct notifier_block *nb) | |
511 | { | |
512 | return raw_notifier_chain_register(&edev->nh, nb); | |
513 | } | |
514 | EXPORT_SYMBOL_GPL(extcon_register_notifier); | |
515 | ||
516 | /** | |
c338bb03 | 517 | * extcon_unregister_notifier() - Unregister a notifiee from the extcon device. |
74c5d09b DK |
518 | * @edev: the extcon device. |
519 | * @nb: a registered notifier block to be unregistered. | |
520 | */ | |
521 | int extcon_unregister_notifier(struct extcon_dev *edev, | |
522 | struct notifier_block *nb) | |
523 | { | |
524 | return raw_notifier_chain_unregister(&edev->nh, nb); | |
525 | } | |
526 | EXPORT_SYMBOL_GPL(extcon_unregister_notifier); | |
527 | ||
de55d871 | 528 | static struct device_attribute extcon_attrs[] = { |
806d9dd7 | 529 | __ATTR(state, S_IRUGO | S_IWUSR, state_show, state_store), |
de55d871 | 530 | __ATTR_RO(name), |
806d9dd7 | 531 | __ATTR_NULL, |
de55d871 MH |
532 | }; |
533 | ||
534 | static 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 | ||
449a2bf5 | 542 | #if defined(CONFIG_ANDROID) |
de55d871 MH |
543 | switch_class = class_compat_register("switch"); |
544 | if (WARN(!switch_class, "cannot allocate")) | |
545 | return -ENOMEM; | |
449a2bf5 | 546 | #endif /* CONFIG_ANDROID */ |
de55d871 MH |
547 | } |
548 | ||
549 | return 0; | |
550 | } | |
551 | ||
552 | static void extcon_cleanup(struct extcon_dev *edev, bool skip) | |
553 | { | |
74c5d09b DK |
554 | mutex_lock(&extcon_dev_list_lock); |
555 | list_del(&edev->entry); | |
556 | mutex_unlock(&extcon_dev_list_lock); | |
557 | ||
de55d871 | 558 | if (!skip && get_device(edev->dev)) { |
806d9dd7 MH |
559 | int index; |
560 | ||
bde68e60 MH |
561 | if (edev->mutually_exclusive && edev->max_supported) { |
562 | for (index = 0; edev->mutually_exclusive[index]; | |
563 | index++) | |
564 | kfree(edev->d_attrs_muex[index].attr.name); | |
565 | kfree(edev->d_attrs_muex); | |
566 | kfree(edev->attrs_muex); | |
567 | } | |
568 | ||
806d9dd7 MH |
569 | for (index = 0; index < edev->max_supported; index++) |
570 | kfree(edev->cables[index].attr_g.name); | |
571 | ||
572 | if (edev->max_supported) { | |
573 | kfree(edev->extcon_dev_type.groups); | |
574 | kfree(edev->cables); | |
575 | } | |
576 | ||
de55d871 MH |
577 | device_unregister(edev->dev); |
578 | put_device(edev->dev); | |
579 | } | |
580 | ||
581 | kfree(edev->dev); | |
582 | } | |
583 | ||
584 | static void extcon_dev_release(struct device *dev) | |
585 | { | |
586 | struct extcon_dev *edev = (struct extcon_dev *) dev_get_drvdata(dev); | |
587 | ||
588 | extcon_cleanup(edev, true); | |
589 | } | |
590 | ||
bde68e60 | 591 | static const char *muex_name = "mutually_exclusive"; |
806d9dd7 MH |
592 | static void dummy_sysfs_dev_release(struct device *dev) |
593 | { | |
594 | } | |
595 | ||
de55d871 MH |
596 | /** |
597 | * extcon_dev_register() - Register a new extcon device | |
598 | * @edev : the new extcon device (should be allocated before calling) | |
599 | * @dev : the parent device for this extcon device. | |
600 | * | |
601 | * Among the members of edev struct, please set the "user initializing data" | |
602 | * in any case and set the "optional callbacks" if required. However, please | |
603 | * do not set the values of "internal data", which are initialized by | |
604 | * this function. | |
605 | */ | |
606 | int extcon_dev_register(struct extcon_dev *edev, struct device *dev) | |
607 | { | |
806d9dd7 | 608 | int ret, index = 0; |
de55d871 MH |
609 | |
610 | if (!extcon_class) { | |
611 | ret = create_extcon_class(); | |
612 | if (ret < 0) | |
613 | return ret; | |
614 | } | |
615 | ||
806d9dd7 MH |
616 | if (edev->supported_cable) { |
617 | /* Get size of array */ | |
618 | for (index = 0; edev->supported_cable[index]; index++) | |
619 | ; | |
620 | edev->max_supported = index; | |
621 | } else { | |
622 | edev->max_supported = 0; | |
623 | } | |
624 | ||
625 | if (index > SUPPORTED_CABLE_MAX) { | |
626 | dev_err(edev->dev, "extcon: maximum number of supported cables exceeded.\n"); | |
627 | return -EINVAL; | |
628 | } | |
629 | ||
de55d871 | 630 | edev->dev = kzalloc(sizeof(struct device), GFP_KERNEL); |
a1d26ac0 DC |
631 | if (!edev->dev) |
632 | return -ENOMEM; | |
de55d871 MH |
633 | edev->dev->parent = dev; |
634 | edev->dev->class = extcon_class; | |
635 | edev->dev->release = extcon_dev_release; | |
636 | ||
637 | dev_set_name(edev->dev, edev->name ? edev->name : dev_name(dev)); | |
806d9dd7 MH |
638 | |
639 | if (edev->max_supported) { | |
640 | char buf[10]; | |
641 | char *str; | |
642 | struct extcon_cable *cable; | |
643 | ||
644 | edev->cables = kzalloc(sizeof(struct extcon_cable) * | |
645 | edev->max_supported, GFP_KERNEL); | |
646 | if (!edev->cables) { | |
647 | ret = -ENOMEM; | |
648 | goto err_sysfs_alloc; | |
649 | } | |
650 | for (index = 0; index < edev->max_supported; index++) { | |
651 | cable = &edev->cables[index]; | |
652 | ||
653 | snprintf(buf, 10, "cable.%d", index); | |
654 | str = kzalloc(sizeof(char) * (strlen(buf) + 1), | |
655 | GFP_KERNEL); | |
656 | if (!str) { | |
657 | for (index--; index >= 0; index--) { | |
658 | cable = &edev->cables[index]; | |
659 | kfree(cable->attr_g.name); | |
660 | } | |
661 | ret = -ENOMEM; | |
662 | ||
663 | goto err_alloc_cables; | |
664 | } | |
665 | strcpy(str, buf); | |
666 | ||
667 | cable->edev = edev; | |
668 | cable->cable_index = index; | |
669 | cable->attrs[0] = &cable->attr_name.attr; | |
670 | cable->attrs[1] = &cable->attr_state.attr; | |
671 | cable->attrs[2] = NULL; | |
672 | cable->attr_g.name = str; | |
673 | cable->attr_g.attrs = cable->attrs; | |
674 | ||
9baf3220 | 675 | sysfs_attr_init(&cable->attr_name.attr); |
806d9dd7 MH |
676 | cable->attr_name.attr.name = "name"; |
677 | cable->attr_name.attr.mode = 0444; | |
678 | cable->attr_name.show = cable_name_show; | |
679 | ||
9baf3220 | 680 | sysfs_attr_init(&cable->attr_state.attr); |
806d9dd7 MH |
681 | cable->attr_state.attr.name = "state"; |
682 | cable->attr_state.attr.mode = 0644; | |
683 | cable->attr_state.show = cable_state_show; | |
684 | cable->attr_state.store = cable_state_store; | |
685 | } | |
686 | } | |
687 | ||
bde68e60 MH |
688 | if (edev->max_supported && edev->mutually_exclusive) { |
689 | char buf[80]; | |
690 | char *name; | |
691 | ||
692 | /* Count the size of mutually_exclusive array */ | |
693 | for (index = 0; edev->mutually_exclusive[index]; index++) | |
694 | ; | |
695 | ||
696 | edev->attrs_muex = kzalloc(sizeof(struct attribute *) * | |
697 | (index + 1), GFP_KERNEL); | |
698 | if (!edev->attrs_muex) { | |
699 | ret = -ENOMEM; | |
700 | goto err_muex; | |
701 | } | |
702 | ||
703 | edev->d_attrs_muex = kzalloc(sizeof(struct device_attribute) * | |
704 | index, GFP_KERNEL); | |
705 | if (!edev->d_attrs_muex) { | |
706 | ret = -ENOMEM; | |
707 | kfree(edev->attrs_muex); | |
708 | goto err_muex; | |
709 | } | |
710 | ||
711 | for (index = 0; edev->mutually_exclusive[index]; index++) { | |
712 | sprintf(buf, "0x%x", edev->mutually_exclusive[index]); | |
713 | name = kzalloc(sizeof(char) * (strlen(buf) + 1), | |
714 | GFP_KERNEL); | |
715 | if (!name) { | |
716 | for (index--; index >= 0; index--) { | |
717 | kfree(edev->d_attrs_muex[index].attr. | |
718 | name); | |
719 | } | |
720 | kfree(edev->d_attrs_muex); | |
721 | kfree(edev->attrs_muex); | |
722 | ret = -ENOMEM; | |
723 | goto err_muex; | |
724 | } | |
725 | strcpy(name, buf); | |
9baf3220 | 726 | sysfs_attr_init(&edev->d_attrs_muex[index].attr); |
bde68e60 MH |
727 | edev->d_attrs_muex[index].attr.name = name; |
728 | edev->d_attrs_muex[index].attr.mode = 0000; | |
729 | edev->attrs_muex[index] = &edev->d_attrs_muex[index] | |
730 | .attr; | |
731 | } | |
732 | edev->attr_g_muex.name = muex_name; | |
733 | edev->attr_g_muex.attrs = edev->attrs_muex; | |
734 | ||
735 | } | |
736 | ||
806d9dd7 MH |
737 | if (edev->max_supported) { |
738 | edev->extcon_dev_type.groups = | |
739 | kzalloc(sizeof(struct attribute_group *) * | |
bde68e60 | 740 | (edev->max_supported + 2), GFP_KERNEL); |
806d9dd7 MH |
741 | if (!edev->extcon_dev_type.groups) { |
742 | ret = -ENOMEM; | |
743 | goto err_alloc_groups; | |
744 | } | |
745 | ||
746 | edev->extcon_dev_type.name = dev_name(edev->dev); | |
747 | edev->extcon_dev_type.release = dummy_sysfs_dev_release; | |
748 | ||
749 | for (index = 0; index < edev->max_supported; index++) | |
750 | edev->extcon_dev_type.groups[index] = | |
751 | &edev->cables[index].attr_g; | |
bde68e60 MH |
752 | if (edev->mutually_exclusive) |
753 | edev->extcon_dev_type.groups[index] = | |
754 | &edev->attr_g_muex; | |
806d9dd7 MH |
755 | |
756 | edev->dev->type = &edev->extcon_dev_type; | |
757 | } | |
758 | ||
de55d871 MH |
759 | ret = device_register(edev->dev); |
760 | if (ret) { | |
761 | put_device(edev->dev); | |
762 | goto err_dev; | |
763 | } | |
449a2bf5 | 764 | #if defined(CONFIG_ANDROID) |
de55d871 MH |
765 | if (switch_class) |
766 | ret = class_compat_create_link(switch_class, edev->dev, | |
c3b15452 | 767 | NULL); |
449a2bf5 | 768 | #endif /* CONFIG_ANDROID */ |
de55d871 | 769 | |
806d9dd7 MH |
770 | spin_lock_init(&edev->lock); |
771 | ||
74c5d09b DK |
772 | RAW_INIT_NOTIFIER_HEAD(&edev->nh); |
773 | ||
de55d871 MH |
774 | dev_set_drvdata(edev->dev, edev); |
775 | edev->state = 0; | |
74c5d09b DK |
776 | |
777 | mutex_lock(&extcon_dev_list_lock); | |
778 | list_add(&edev->entry, &extcon_dev_list); | |
779 | mutex_unlock(&extcon_dev_list_lock); | |
780 | ||
de55d871 MH |
781 | return 0; |
782 | ||
783 | err_dev: | |
806d9dd7 MH |
784 | if (edev->max_supported) |
785 | kfree(edev->extcon_dev_type.groups); | |
786 | err_alloc_groups: | |
bde68e60 MH |
787 | if (edev->max_supported && edev->mutually_exclusive) { |
788 | for (index = 0; edev->mutually_exclusive[index]; index++) | |
789 | kfree(edev->d_attrs_muex[index].attr.name); | |
790 | kfree(edev->d_attrs_muex); | |
791 | kfree(edev->attrs_muex); | |
792 | } | |
793 | err_muex: | |
806d9dd7 MH |
794 | for (index = 0; index < edev->max_supported; index++) |
795 | kfree(edev->cables[index].attr_g.name); | |
796 | err_alloc_cables: | |
797 | if (edev->max_supported) | |
798 | kfree(edev->cables); | |
799 | err_sysfs_alloc: | |
de55d871 MH |
800 | kfree(edev->dev); |
801 | return ret; | |
802 | } | |
803 | EXPORT_SYMBOL_GPL(extcon_dev_register); | |
804 | ||
805 | /** | |
806 | * extcon_dev_unregister() - Unregister the extcon device. | |
c338bb03 | 807 | * @edev: the extcon device instance to be unregistered. |
de55d871 MH |
808 | * |
809 | * Note that this does not call kfree(edev) because edev was not allocated | |
810 | * by this class. | |
811 | */ | |
812 | void extcon_dev_unregister(struct extcon_dev *edev) | |
813 | { | |
814 | extcon_cleanup(edev, false); | |
815 | } | |
816 | EXPORT_SYMBOL_GPL(extcon_dev_unregister); | |
817 | ||
818 | static int __init extcon_class_init(void) | |
819 | { | |
820 | return create_extcon_class(); | |
821 | } | |
822 | module_init(extcon_class_init); | |
823 | ||
824 | static void __exit extcon_class_exit(void) | |
825 | { | |
826 | class_destroy(extcon_class); | |
827 | } | |
828 | module_exit(extcon_class_exit); | |
829 | ||
830 | MODULE_AUTHOR("Mike Lockwood <lockwood@android.com>"); | |
831 | MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>"); | |
832 | MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>"); | |
833 | MODULE_DESCRIPTION("External connector (extcon) class driver"); | |
834 | MODULE_LICENSE("GPL"); |