blob: 70bcaa7f93ddc1ee5bfe719446449d29510abee4 [file] [log] [blame]
Johannes Bergf3d94782006-06-21 15:42:43 +02001/*
2 * soundbus
3 *
4 * Copyright 2006 Johannes Berg <johannes@sipsolutions.net>
5 *
6 * GPL v2, can be found in COPYING.
7 */
8
9#include <linux/module.h>
10#include "soundbus.h"
11
12MODULE_AUTHOR("Johannes Berg <johannes@sipsolutions.net>");
13MODULE_LICENSE("GPL");
14MODULE_DESCRIPTION("Apple Soundbus");
15
16struct soundbus_dev *soundbus_dev_get(struct soundbus_dev *dev)
17{
18 struct device *tmp;
19
20 if (!dev)
21 return NULL;
22 tmp = get_device(&dev->ofdev.dev);
23 if (tmp)
24 return to_soundbus_device(tmp);
25 else
26 return NULL;
27}
28EXPORT_SYMBOL_GPL(soundbus_dev_get);
29
30void soundbus_dev_put(struct soundbus_dev *dev)
31{
32 if (dev)
33 put_device(&dev->ofdev.dev);
34}
35EXPORT_SYMBOL_GPL(soundbus_dev_put);
36
37static int soundbus_probe(struct device *dev)
38{
39 int error = -ENODEV;
40 struct soundbus_driver *drv;
41 struct soundbus_dev *soundbus_dev;
42
43 drv = to_soundbus_driver(dev->driver);
44 soundbus_dev = to_soundbus_device(dev);
45
46 if (!drv->probe)
47 return error;
48
49 soundbus_dev_get(soundbus_dev);
50
51 error = drv->probe(soundbus_dev);
52 if (error)
53 soundbus_dev_put(soundbus_dev);
54
55 return error;
56}
57
58
Kay Sievers7eff2e72007-08-14 15:15:12 +020059static int soundbus_uevent(struct device *dev, struct kobj_uevent_env *env)
Johannes Bergf3d94782006-06-21 15:42:43 +020060{
61 struct soundbus_dev * soundbus_dev;
Grant Likely2dc11582010-08-06 09:25:50 -060062 struct platform_device * of;
Stephen Rothwella7edd0e2007-04-03 10:52:17 +100063 const char *compat;
Kay Sievers7eff2e72007-08-14 15:15:12 +020064 int retval = 0;
Eric Rannaudbf624562007-03-30 22:23:12 -070065 int cplen, seen = 0;
Johannes Bergf3d94782006-06-21 15:42:43 +020066
67 if (!dev)
68 return -ENODEV;
69
70 soundbus_dev = to_soundbus_device(dev);
71 if (!soundbus_dev)
72 return -ENODEV;
73
74 of = &soundbus_dev->ofdev;
75
76 /* stuff we want to pass to /sbin/hotplug */
Grant Likely61c7a082010-04-13 16:12:29 -070077 retval = add_uevent_var(env, "OF_NAME=%s", of->dev.of_node->name);
Eric Rannaudbf624562007-03-30 22:23:12 -070078 if (retval)
79 return retval;
Johannes Bergf3d94782006-06-21 15:42:43 +020080
Grant Likely61c7a082010-04-13 16:12:29 -070081 retval = add_uevent_var(env, "OF_TYPE=%s", of->dev.of_node->type);
Eric Rannaudbf624562007-03-30 22:23:12 -070082 if (retval)
83 return retval;
Johannes Bergf3d94782006-06-21 15:42:43 +020084
85 /* Since the compatible field can contain pretty much anything
86 * it's not really legal to split it out with commas. We split it
87 * up using a number of environment variables instead. */
88
Grant Likely61c7a082010-04-13 16:12:29 -070089 compat = of_get_property(of->dev.of_node, "compatible", &cplen);
Johannes Bergf3d94782006-06-21 15:42:43 +020090 while (compat && cplen > 0) {
Kay Sievers7eff2e72007-08-14 15:15:12 +020091 int tmp = env->buflen;
92 retval = add_uevent_var(env, "OF_COMPATIBLE_%d=%s", seen, compat);
Eric Rannaudbf624562007-03-30 22:23:12 -070093 if (retval)
94 return retval;
Kay Sievers7eff2e72007-08-14 15:15:12 +020095 compat += env->buflen - tmp;
96 cplen -= env->buflen - tmp;
Eric Rannaudbf624562007-03-30 22:23:12 -070097 seen += 1;
Johannes Bergf3d94782006-06-21 15:42:43 +020098 }
99
Kay Sievers7eff2e72007-08-14 15:15:12 +0200100 retval = add_uevent_var(env, "OF_COMPATIBLE_N=%d", seen);
Eric Rannaudbf624562007-03-30 22:23:12 -0700101 if (retval)
102 return retval;
Kay Sievers7eff2e72007-08-14 15:15:12 +0200103 retval = add_uevent_var(env, "MODALIAS=%s", soundbus_dev->modalias);
Johannes Bergf3d94782006-06-21 15:42:43 +0200104
Eric Rannaudbf624562007-03-30 22:23:12 -0700105 return retval;
Johannes Bergf3d94782006-06-21 15:42:43 +0200106}
107
108static int soundbus_device_remove(struct device *dev)
109{
110 struct soundbus_dev * soundbus_dev = to_soundbus_device(dev);
111 struct soundbus_driver * drv = to_soundbus_driver(dev->driver);
112
113 if (dev->driver && drv->remove)
114 drv->remove(soundbus_dev);
115 soundbus_dev_put(soundbus_dev);
116
117 return 0;
118}
119
120static void soundbus_device_shutdown(struct device *dev)
121{
122 struct soundbus_dev * soundbus_dev = to_soundbus_device(dev);
123 struct soundbus_driver * drv = to_soundbus_driver(dev->driver);
124
125 if (dev->driver && drv->shutdown)
126 drv->shutdown(soundbus_dev);
127}
128
Quentin Lamberta038b972015-06-12 10:38:41 +0200129/* soundbus_dev_attrs is declared in sysfs.c */
130ATTRIBUTE_GROUPS(soundbus_dev);
Johannes Bergf3d94782006-06-21 15:42:43 +0200131static struct bus_type soundbus_bus_type = {
132 .name = "aoa-soundbus",
133 .probe = soundbus_probe,
134 .uevent = soundbus_uevent,
135 .remove = soundbus_device_remove,
136 .shutdown = soundbus_device_shutdown,
Quentin Lamberta038b972015-06-12 10:38:41 +0200137 .dev_groups = soundbus_dev_groups,
Johannes Bergf3d94782006-06-21 15:42:43 +0200138};
139
Johannes Bergf3d94782006-06-21 15:42:43 +0200140int soundbus_add_one(struct soundbus_dev *dev)
141{
142 static int devcount;
143
144 /* sanity checks */
145 if (!dev->attach_codec ||
Grant Likely61c7a082010-04-13 16:12:29 -0700146 !dev->ofdev.dev.of_node ||
Johannes Bergf3d94782006-06-21 15:42:43 +0200147 dev->pcmname ||
148 dev->pcmid != -1) {
149 printk(KERN_ERR "soundbus: adding device failed sanity check!\n");
150 return -EINVAL;
151 }
152
Kay Sieversbb072bf2008-11-02 03:50:35 +0100153 dev_set_name(&dev->ofdev.dev, "soundbus:%x", ++devcount);
Johannes Bergf3d94782006-06-21 15:42:43 +0200154 dev->ofdev.dev.bus = &soundbus_bus_type;
155 return of_device_register(&dev->ofdev);
156}
157EXPORT_SYMBOL_GPL(soundbus_add_one);
158
159void soundbus_remove_one(struct soundbus_dev *dev)
160{
161 of_device_unregister(&dev->ofdev);
162}
163EXPORT_SYMBOL_GPL(soundbus_remove_one);
164
165int soundbus_register_driver(struct soundbus_driver *drv)
166{
167 /* initialize common driver fields */
168 drv->driver.name = drv->name;
169 drv->driver.bus = &soundbus_bus_type;
170
171 /* register with core */
172 return driver_register(&drv->driver);
173}
174EXPORT_SYMBOL_GPL(soundbus_register_driver);
175
176void soundbus_unregister_driver(struct soundbus_driver *drv)
177{
178 driver_unregister(&drv->driver);
179}
180EXPORT_SYMBOL_GPL(soundbus_unregister_driver);
181
Benjamin Herrenschmidta08bc4c2006-07-10 04:44:35 -0700182static int __init soundbus_init(void)
183{
184 return bus_register(&soundbus_bus_type);
185}
186
187static void __exit soundbus_exit(void)
188{
189 bus_unregister(&soundbus_bus_type);
190}
191
192subsys_initcall(soundbus_init);
Johannes Bergf3d94782006-06-21 15:42:43 +0200193module_exit(soundbus_exit);