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