Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
extcon-class.c
Go to the documentation of this file.
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 <[email protected]>
8  * Author: MyungJoo Ham <[email protected]>
9  *
10  * based on android/drivers/switch/switch_class.c
11  * Copyright (C) 2008 Google, Inc.
12  * Author: Mike Lockwood <[email protected]>
13  *
14  * This software is licensed under the terms of the GNU General Public
15  * License version 2, as published by the Free Software Foundation, and
16  * may be copied, distributed, and modified under those terms.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23 */
24 
25 #include <linux/module.h>
26 #include <linux/types.h>
27 #include <linux/init.h>
28 #include <linux/device.h>
29 #include <linux/fs.h>
30 #include <linux/err.h>
31 #include <linux/extcon.h>
32 #include <linux/slab.h>
33 #include <linux/sysfs.h>
34 
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  */
44 const char extcon_cable_name[][CABLE_NAME_MAX + 1] = {
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",
64  [EXTCON_MECHANICAL] = "Mechanical",
65 };
66 
67 static struct class *extcon_class;
68 #if defined(CONFIG_ANDROID)
69 static struct class_compat *switch_class;
70 #endif /* CONFIG_ANDROID */
71 
72 static LIST_HEAD(extcon_dev_list);
73 static DEFINE_MUTEX(extcon_dev_list_lock);
74 
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 weight;
93  u32 correspondants = new_state & edev->mutually_exclusive[i];
94 
95  /* calculate the total number of bits set */
96  weight = hweight32(correspondants);
97  if (weight > 1)
98  return i + 1;
99  }
100 
101  return 0;
102 }
103 
104 static ssize_t state_show(struct device *dev, struct device_attribute *attr,
105  char *buf)
106 {
107  int i, count = 0;
108  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  }
117 
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 
132 int extcon_set_state(struct extcon_dev *edev, u32 state);
133 static 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
144  ret = extcon_set_state(edev, state);
145 
146  if (ret < 0)
147  return ret;
148 
149  return count;
150 }
151 
152 static 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 
167 static 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 
177 static 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",
185  cable->cable_index));
186 }
187 
188 static 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 
223 int extcon_update_state(struct extcon_dev *edev, u32 mask, u32 state)
224 {
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;
231  unsigned long flags;
232 
233  spin_lock_irqsave(&edev->lock, flags);
234 
235  if (edev->state != ((edev->state & ~mask) | (state & mask))) {
236  u32 old_state = edev->state;
237 
238  if (check_mutually_exclusive(edev, (edev->state & ~mask) |
239  (state & mask))) {
240  spin_unlock_irqrestore(&edev->lock, flags);
241  return -EPERM;
242  }
243 
244  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);
251  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;
269  /* Unlock early before uevent */
270  spin_unlock_irqrestore(&edev->lock, flags);
271 
272  kobject_uevent_env(&edev->dev->kobj, KOBJ_CHANGE, envp);
273  free_page((unsigned long)prop_buf);
274  } else {
275  /* Unlock early before uevent */
276  spin_unlock_irqrestore(&edev->lock, flags);
277 
278  dev_err(edev->dev, "out of memory in extcon_set_state\n");
279  kobject_uevent(&edev->dev->kobj, KOBJ_CHANGE);
280  }
281  } else {
282  /* No changes */
283  spin_unlock_irqrestore(&edev->lock, flags);
284  }
285 
286  return 0;
287 }
289 
298 int extcon_set_state(struct extcon_dev *edev, u32 state)
299 {
300  return extcon_update_state(edev, 0xffffffff, state);
301 }
303 
314 int 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 }
329 
336 {
337  if (index < 0 || (edev->max_supported && edev->max_supported <= index))
338  return -EINVAL;
339 
340  return !!(edev->state & (1 << index));
341 }
343 
351 int extcon_get_cable_state(struct extcon_dev *edev, const char *cable_name)
352 {
354  (edev, cable_name));
355 }
357 
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;
374  return extcon_update_state(edev, 1 << index, state);
375 }
377 
388  const char *cable_name, bool cable_state)
389 {
391  (edev, cable_name), cable_state);
392 }
394 
399 struct 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;
409 out:
410  mutex_unlock(&extcon_dev_list_lock);
411  return sd;
412 }
414 
415 static 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,
420  struct extcon_dev *edev = ptr;
421 
422  if ((val & (1 << obj->cable_index)) !=
423  (edev->state & (1 << obj->cable_index))) {
424  bool cable_state = true;
425 
426  obj->previous_value = val;
427 
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);
433  }
434 
435  return NOTIFY_OK;
436 }
437 
462  const char *extcon_name, const char *cable_name,
463  struct notifier_block *nb)
464 {
465  if (!obj || !cable_name || !nb)
466  return -EINVAL;
467 
468  if (extcon_name) {
469  obj->edev = extcon_get_extcon_dev(extcon_name);
470  if (!obj->edev)
471  return -ENODEV;
472 
473  obj->cable_index = extcon_find_cable_index(obj->edev, cable_name);
474  if (obj->cable_index < 0)
475  return -ENODEV;
476 
477  obj->user_nb = nb;
478 
479  obj->internal_nb.notifier_call = _call_per_cable;
480 
481  return raw_notifier_chain_register(&obj->edev->nh, &obj->internal_nb);
482  } else {
483  struct class_dev_iter iter;
484  struct extcon_dev *extd;
485  struct device *dev;
486 
487  if (!extcon_class)
488  return -ENODEV;
489  class_dev_iter_init(&iter, extcon_class, NULL, NULL);
490  while ((dev = class_dev_iter_next(&iter))) {
491  extd = (struct extcon_dev *)dev_get_drvdata(dev);
492 
493  if (extcon_find_cable_index(extd, cable_name) < 0)
494  continue;
495 
496  class_dev_iter_exit(&iter);
497  return extcon_register_interest(obj, extd->name,
498  cable_name, nb);
499  }
500 
501  return -ENODEV;
502  }
503 }
504 
512 {
513  if (!obj)
514  return -EINVAL;
515 
516  return raw_notifier_chain_unregister(&obj->edev->nh, &obj->internal_nb);
517 }
518 
530  struct notifier_block *nb)
531 {
532  return raw_notifier_chain_register(&edev->nh, nb);
533 }
535 
542  struct notifier_block *nb)
543 {
544  return raw_notifier_chain_unregister(&edev->nh, nb);
545 }
547 
548 static struct device_attribute extcon_attrs[] = {
549  __ATTR(state, S_IRUGO | S_IWUSR, state_show, state_store),
550  __ATTR_RO(name),
551  __ATTR_NULL,
552 };
553 
554 static int create_extcon_class(void)
555 {
556  if (!extcon_class) {
557  extcon_class = class_create(THIS_MODULE, "extcon");
558  if (IS_ERR(extcon_class))
559  return PTR_ERR(extcon_class);
560  extcon_class->dev_attrs = extcon_attrs;
561 
562 #if defined(CONFIG_ANDROID)
563  switch_class = class_compat_register("switch");
564  if (WARN(!switch_class, "cannot allocate"))
565  return -ENOMEM;
566 #endif /* CONFIG_ANDROID */
567  }
568 
569  return 0;
570 }
571 
572 static void extcon_dev_release(struct device *dev)
573 {
574  kfree(dev);
575 }
576 
577 static const char *muex_name = "mutually_exclusive";
578 static void dummy_sysfs_dev_release(struct device *dev)
579 {
580 }
581 
592 int extcon_dev_register(struct extcon_dev *edev, struct device *dev)
593 {
594  int ret, index = 0;
595 
596  if (!extcon_class) {
597  ret = create_extcon_class();
598  if (ret < 0)
599  return ret;
600  }
601 
602  if (edev->supported_cable) {
603  /* Get size of array */
604  for (index = 0; edev->supported_cable[index]; index++)
605  ;
606  edev->max_supported = index;
607  } else {
608  edev->max_supported = 0;
609  }
610 
611  if (index > SUPPORTED_CABLE_MAX) {
612  dev_err(edev->dev, "extcon: maximum number of supported cables exceeded.\n");
613  return -EINVAL;
614  }
615 
616  edev->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
617  if (!edev->dev)
618  return -ENOMEM;
619  edev->dev->parent = dev;
620  edev->dev->class = extcon_class;
621  edev->dev->release = extcon_dev_release;
622 
623  dev_set_name(edev->dev, edev->name ? edev->name : dev_name(dev));
624 
625  if (edev->max_supported) {
626  char buf[10];
627  char *str;
628  struct extcon_cable *cable;
629 
630  edev->cables = kzalloc(sizeof(struct extcon_cable) *
631  edev->max_supported, GFP_KERNEL);
632  if (!edev->cables) {
633  ret = -ENOMEM;
634  goto err_sysfs_alloc;
635  }
636  for (index = 0; index < edev->max_supported; index++) {
637  cable = &edev->cables[index];
638 
639  snprintf(buf, 10, "cable.%d", index);
640  str = kzalloc(sizeof(char) * (strlen(buf) + 1),
641  GFP_KERNEL);
642  if (!str) {
643  for (index--; index >= 0; index--) {
644  cable = &edev->cables[index];
645  kfree(cable->attr_g.name);
646  }
647  ret = -ENOMEM;
648 
649  goto err_alloc_cables;
650  }
651  strcpy(str, buf);
652 
653  cable->edev = edev;
654  cable->cable_index = index;
655  cable->attrs[0] = &cable->attr_name.attr;
656  cable->attrs[1] = &cable->attr_state.attr;
657  cable->attrs[2] = NULL;
658  cable->attr_g.name = str;
659  cable->attr_g.attrs = cable->attrs;
660 
661  sysfs_attr_init(&cable->attr_name.attr);
662  cable->attr_name.attr.name = "name";
663  cable->attr_name.attr.mode = 0444;
664  cable->attr_name.show = cable_name_show;
665 
666  sysfs_attr_init(&cable->attr_state.attr);
667  cable->attr_state.attr.name = "state";
668  cable->attr_state.attr.mode = 0644;
669  cable->attr_state.show = cable_state_show;
670  cable->attr_state.store = cable_state_store;
671  }
672  }
673 
674  if (edev->max_supported && edev->mutually_exclusive) {
675  char buf[80];
676  char *name;
677 
678  /* Count the size of mutually_exclusive array */
679  for (index = 0; edev->mutually_exclusive[index]; index++)
680  ;
681 
682  edev->attrs_muex = kzalloc(sizeof(struct attribute *) *
683  (index + 1), GFP_KERNEL);
684  if (!edev->attrs_muex) {
685  ret = -ENOMEM;
686  goto err_muex;
687  }
688 
689  edev->d_attrs_muex = kzalloc(sizeof(struct device_attribute) *
690  index, GFP_KERNEL);
691  if (!edev->d_attrs_muex) {
692  ret = -ENOMEM;
693  kfree(edev->attrs_muex);
694  goto err_muex;
695  }
696 
697  for (index = 0; edev->mutually_exclusive[index]; index++) {
698  sprintf(buf, "0x%x", edev->mutually_exclusive[index]);
699  name = kzalloc(sizeof(char) * (strlen(buf) + 1),
700  GFP_KERNEL);
701  if (!name) {
702  for (index--; index >= 0; index--) {
703  kfree(edev->d_attrs_muex[index].attr.
704  name);
705  }
706  kfree(edev->d_attrs_muex);
707  kfree(edev->attrs_muex);
708  ret = -ENOMEM;
709  goto err_muex;
710  }
711  strcpy(name, buf);
712  sysfs_attr_init(&edev->d_attrs_muex[index].attr);
713  edev->d_attrs_muex[index].attr.name = name;
714  edev->d_attrs_muex[index].attr.mode = 0000;
715  edev->attrs_muex[index] = &edev->d_attrs_muex[index]
716  .attr;
717  }
718  edev->attr_g_muex.name = muex_name;
719  edev->attr_g_muex.attrs = edev->attrs_muex;
720 
721  }
722 
723  if (edev->max_supported) {
724  edev->extcon_dev_type.groups =
725  kzalloc(sizeof(struct attribute_group *) *
726  (edev->max_supported + 2), GFP_KERNEL);
727  if (!edev->extcon_dev_type.groups) {
728  ret = -ENOMEM;
729  goto err_alloc_groups;
730  }
731 
732  edev->extcon_dev_type.name = dev_name(edev->dev);
733  edev->extcon_dev_type.release = dummy_sysfs_dev_release;
734 
735  for (index = 0; index < edev->max_supported; index++)
736  edev->extcon_dev_type.groups[index] =
737  &edev->cables[index].attr_g;
738  if (edev->mutually_exclusive)
739  edev->extcon_dev_type.groups[index] =
740  &edev->attr_g_muex;
741 
742  edev->dev->type = &edev->extcon_dev_type;
743  }
744 
745  ret = device_register(edev->dev);
746  if (ret) {
747  put_device(edev->dev);
748  goto err_dev;
749  }
750 #if defined(CONFIG_ANDROID)
751  if (switch_class)
752  ret = class_compat_create_link(switch_class, edev->dev,
753  NULL);
754 #endif /* CONFIG_ANDROID */
755 
756  spin_lock_init(&edev->lock);
757 
758  RAW_INIT_NOTIFIER_HEAD(&edev->nh);
759 
760  dev_set_drvdata(edev->dev, edev);
761  edev->state = 0;
762 
763  mutex_lock(&extcon_dev_list_lock);
764  list_add(&edev->entry, &extcon_dev_list);
765  mutex_unlock(&extcon_dev_list_lock);
766 
767  return 0;
768 
769 err_dev:
770  if (edev->max_supported)
771  kfree(edev->extcon_dev_type.groups);
772 err_alloc_groups:
773  if (edev->max_supported && edev->mutually_exclusive) {
774  for (index = 0; edev->mutually_exclusive[index]; index++)
775  kfree(edev->d_attrs_muex[index].attr.name);
776  kfree(edev->d_attrs_muex);
777  kfree(edev->attrs_muex);
778  }
779 err_muex:
780  for (index = 0; index < edev->max_supported; index++)
781  kfree(edev->cables[index].attr_g.name);
782 err_alloc_cables:
783  if (edev->max_supported)
784  kfree(edev->cables);
785 err_sysfs_alloc:
786  kfree(edev->dev);
787  return ret;
788 }
790 
799 {
800  int index;
801 
802  mutex_lock(&extcon_dev_list_lock);
803  list_del(&edev->entry);
804  mutex_unlock(&extcon_dev_list_lock);
805 
806  if (IS_ERR_OR_NULL(get_device(edev->dev))) {
807  dev_err(edev->dev, "Failed to unregister extcon_dev (%s)\n",
808  dev_name(edev->dev));
809  return;
810  }
811 
812  if (edev->mutually_exclusive && edev->max_supported) {
813  for (index = 0; edev->mutually_exclusive[index];
814  index++)
815  kfree(edev->d_attrs_muex[index].attr.name);
816  kfree(edev->d_attrs_muex);
817  kfree(edev->attrs_muex);
818  }
819 
820  for (index = 0; index < edev->max_supported; index++)
821  kfree(edev->cables[index].attr_g.name);
822 
823  if (edev->max_supported) {
824  kfree(edev->extcon_dev_type.groups);
825  kfree(edev->cables);
826  }
827 
828 #if defined(CONFIG_ANDROID)
829  if (switch_class)
830  class_compat_remove_link(switch_class, edev->dev, NULL);
831 #endif
832  device_unregister(edev->dev);
833  put_device(edev->dev);
834 }
836 
837 static int __init extcon_class_init(void)
838 {
839  return create_extcon_class();
840 }
841 module_init(extcon_class_init);
842 
843 static void __exit extcon_class_exit(void)
844 {
845 #if defined(CONFIG_ANDROID)
846  class_compat_unregister(switch_class);
847 #endif
848  class_destroy(extcon_class);
849 }
850 module_exit(extcon_class_exit);
851 
852 MODULE_AUTHOR("Mike Lockwood <[email protected]>");
853 MODULE_AUTHOR("Donggeun Kim <[email protected]>");
854 MODULE_AUTHOR("MyungJoo Ham <[email protected]>");
855 MODULE_DESCRIPTION("External connector (extcon) class driver");
856 MODULE_LICENSE("GPL");