core.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * soundbus
  3. *
  4. * Copyright 2006 Johannes Berg <johannes@sipsolutions.net>
  5. *
  6. * GPL v2, can be found in COPYING.
  7. */
  8. #include <linux/module.h>
  9. #include "soundbus.h"
  10. MODULE_AUTHOR("Johannes Berg <johannes@sipsolutions.net>");
  11. MODULE_LICENSE("GPL");
  12. MODULE_DESCRIPTION("Apple Soundbus");
  13. struct soundbus_dev *soundbus_dev_get(struct soundbus_dev *dev)
  14. {
  15. struct device *tmp;
  16. if (!dev)
  17. return NULL;
  18. tmp = get_device(&dev->ofdev.dev);
  19. if (tmp)
  20. return to_soundbus_device(tmp);
  21. else
  22. return NULL;
  23. }
  24. EXPORT_SYMBOL_GPL(soundbus_dev_get);
  25. void soundbus_dev_put(struct soundbus_dev *dev)
  26. {
  27. if (dev)
  28. put_device(&dev->ofdev.dev);
  29. }
  30. EXPORT_SYMBOL_GPL(soundbus_dev_put);
  31. static int soundbus_probe(struct device *dev)
  32. {
  33. int error = -ENODEV;
  34. struct soundbus_driver *drv;
  35. struct soundbus_dev *soundbus_dev;
  36. drv = to_soundbus_driver(dev->driver);
  37. soundbus_dev = to_soundbus_device(dev);
  38. if (!drv->probe)
  39. return error;
  40. soundbus_dev_get(soundbus_dev);
  41. error = drv->probe(soundbus_dev);
  42. if (error)
  43. soundbus_dev_put(soundbus_dev);
  44. return error;
  45. }
  46. static int soundbus_uevent(struct device *dev, struct kobj_uevent_env *env)
  47. {
  48. struct soundbus_dev * soundbus_dev;
  49. struct platform_device * of;
  50. const char *compat;
  51. int retval = 0;
  52. int cplen, seen = 0;
  53. if (!dev)
  54. return -ENODEV;
  55. soundbus_dev = to_soundbus_device(dev);
  56. if (!soundbus_dev)
  57. return -ENODEV;
  58. of = &soundbus_dev->ofdev;
  59. /* stuff we want to pass to /sbin/hotplug */
  60. retval = add_uevent_var(env, "OF_NAME=%s", of->dev.of_node->name);
  61. if (retval)
  62. return retval;
  63. retval = add_uevent_var(env, "OF_TYPE=%s", of->dev.of_node->type);
  64. if (retval)
  65. return retval;
  66. /* Since the compatible field can contain pretty much anything
  67. * it's not really legal to split it out with commas. We split it
  68. * up using a number of environment variables instead. */
  69. compat = of_get_property(of->dev.of_node, "compatible", &cplen);
  70. while (compat && cplen > 0) {
  71. int tmp = env->buflen;
  72. retval = add_uevent_var(env, "OF_COMPATIBLE_%d=%s", seen, compat);
  73. if (retval)
  74. return retval;
  75. compat += env->buflen - tmp;
  76. cplen -= env->buflen - tmp;
  77. seen += 1;
  78. }
  79. retval = add_uevent_var(env, "OF_COMPATIBLE_N=%d", seen);
  80. if (retval)
  81. return retval;
  82. retval = add_uevent_var(env, "MODALIAS=%s", soundbus_dev->modalias);
  83. return retval;
  84. }
  85. static int soundbus_device_remove(struct device *dev)
  86. {
  87. struct soundbus_dev * soundbus_dev = to_soundbus_device(dev);
  88. struct soundbus_driver * drv = to_soundbus_driver(dev->driver);
  89. if (dev->driver && drv->remove)
  90. drv->remove(soundbus_dev);
  91. soundbus_dev_put(soundbus_dev);
  92. return 0;
  93. }
  94. static void soundbus_device_shutdown(struct device *dev)
  95. {
  96. struct soundbus_dev * soundbus_dev = to_soundbus_device(dev);
  97. struct soundbus_driver * drv = to_soundbus_driver(dev->driver);
  98. if (dev->driver && drv->shutdown)
  99. drv->shutdown(soundbus_dev);
  100. }
  101. /* soundbus_dev_attrs is declared in sysfs.c */
  102. ATTRIBUTE_GROUPS(soundbus_dev);
  103. static struct bus_type soundbus_bus_type = {
  104. .name = "aoa-soundbus",
  105. .probe = soundbus_probe,
  106. .uevent = soundbus_uevent,
  107. .remove = soundbus_device_remove,
  108. .shutdown = soundbus_device_shutdown,
  109. .dev_groups = soundbus_dev_groups,
  110. };
  111. int soundbus_add_one(struct soundbus_dev *dev)
  112. {
  113. static int devcount;
  114. /* sanity checks */
  115. if (!dev->attach_codec ||
  116. !dev->ofdev.dev.of_node ||
  117. dev->pcmname ||
  118. dev->pcmid != -1) {
  119. printk(KERN_ERR "soundbus: adding device failed sanity check!\n");
  120. return -EINVAL;
  121. }
  122. dev_set_name(&dev->ofdev.dev, "soundbus:%x", ++devcount);
  123. dev->ofdev.dev.bus = &soundbus_bus_type;
  124. return of_device_register(&dev->ofdev);
  125. }
  126. EXPORT_SYMBOL_GPL(soundbus_add_one);
  127. void soundbus_remove_one(struct soundbus_dev *dev)
  128. {
  129. of_device_unregister(&dev->ofdev);
  130. }
  131. EXPORT_SYMBOL_GPL(soundbus_remove_one);
  132. int soundbus_register_driver(struct soundbus_driver *drv)
  133. {
  134. /* initialize common driver fields */
  135. drv->driver.name = drv->name;
  136. drv->driver.bus = &soundbus_bus_type;
  137. /* register with core */
  138. return driver_register(&drv->driver);
  139. }
  140. EXPORT_SYMBOL_GPL(soundbus_register_driver);
  141. void soundbus_unregister_driver(struct soundbus_driver *drv)
  142. {
  143. driver_unregister(&drv->driver);
  144. }
  145. EXPORT_SYMBOL_GPL(soundbus_unregister_driver);
  146. static int __init soundbus_init(void)
  147. {
  148. return bus_register(&soundbus_bus_type);
  149. }
  150. static void __exit soundbus_exit(void)
  151. {
  152. bus_unregister(&soundbus_bus_type);
  153. }
  154. subsys_initcall(soundbus_init);
  155. module_exit(soundbus_exit);