Merge /pub/scm/linux/kernel/git/torvalds/linux-2.6
[linux-2.6] / sound / aoa / soundbus / core.c
1 /*
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
12 MODULE_AUTHOR("Johannes Berg <johannes@sipsolutions.net>");
13 MODULE_LICENSE("GPL");
14 MODULE_DESCRIPTION("Apple Soundbus");
15
16 struct 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 }
28 EXPORT_SYMBOL_GPL(soundbus_dev_get);
29
30 void soundbus_dev_put(struct soundbus_dev *dev)
31 {
32         if (dev)
33                 put_device(&dev->ofdev.dev);
34 }
35 EXPORT_SYMBOL_GPL(soundbus_dev_put);
36
37 static 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
59 static int soundbus_uevent(struct device *dev, char **envp, int num_envp,
60                            char *buffer, int buffer_size)
61 {
62         struct soundbus_dev * soundbus_dev;
63         struct of_device * of;
64         const char *compat;
65         int retval = 0, i = 0, length = 0;
66         int cplen, seen = 0;
67
68         if (!dev)
69                 return -ENODEV;
70
71         soundbus_dev = to_soundbus_device(dev);
72         if (!soundbus_dev)
73                 return -ENODEV;
74
75         of = &soundbus_dev->ofdev;
76
77         /* stuff we want to pass to /sbin/hotplug */
78         retval = add_uevent_var(envp, num_envp, &i,
79                                 buffer, buffer_size, &length,
80                                 "OF_NAME=%s", of->node->name);
81         if (retval)
82                 return retval;
83
84         retval = add_uevent_var(envp, num_envp, &i,
85                                 buffer, buffer_size, &length,
86                                 "OF_TYPE=%s", of->node->type);
87         if (retval)
88                 return retval;
89
90         /* Since the compatible field can contain pretty much anything
91          * it's not really legal to split it out with commas. We split it
92          * up using a number of environment variables instead. */
93
94         compat = of_get_property(of->node, "compatible", &cplen);
95         while (compat && cplen > 0) {
96                 int tmp = length;
97                 retval = add_uevent_var(envp, num_envp, &i,
98                                         buffer, buffer_size, &length,
99                                         "OF_COMPATIBLE_%d=%s", seen, compat);
100                 if (retval)
101                         return retval;
102                 compat += length - tmp;
103                 cplen -= length - tmp;
104                 seen += 1;
105         }
106
107         retval = add_uevent_var(envp, num_envp, &i,
108                                 buffer, buffer_size, &length,
109                                 "OF_COMPATIBLE_N=%d", seen);
110         if (retval)
111                 return retval;
112         retval = add_uevent_var(envp, num_envp, &i,
113                                 buffer, buffer_size, &length,
114                                 "MODALIAS=%s", soundbus_dev->modalias);
115
116         envp[i] = NULL;
117
118         return retval;
119 }
120
121 static int soundbus_device_remove(struct device *dev)
122 {
123         struct soundbus_dev * soundbus_dev = to_soundbus_device(dev);
124         struct soundbus_driver * drv = to_soundbus_driver(dev->driver);
125
126         if (dev->driver && drv->remove)
127                 drv->remove(soundbus_dev);
128         soundbus_dev_put(soundbus_dev);
129
130         return 0;
131 }
132
133 static void soundbus_device_shutdown(struct device *dev)
134 {
135         struct soundbus_dev * soundbus_dev = to_soundbus_device(dev);
136         struct soundbus_driver * drv = to_soundbus_driver(dev->driver);
137
138         if (dev->driver && drv->shutdown)
139                 drv->shutdown(soundbus_dev);
140 }
141
142 #ifdef CONFIG_PM
143
144 static int soundbus_device_suspend(struct device *dev, pm_message_t state)
145 {
146         struct soundbus_dev * soundbus_dev = to_soundbus_device(dev);
147         struct soundbus_driver * drv = to_soundbus_driver(dev->driver);
148
149         if (dev->driver && drv->suspend)
150                 return drv->suspend(soundbus_dev, state);
151         return 0;
152 }
153
154 static int soundbus_device_resume(struct device * dev)
155 {
156         struct soundbus_dev * soundbus_dev = to_soundbus_device(dev);
157         struct soundbus_driver * drv = to_soundbus_driver(dev->driver);
158
159         if (dev->driver && drv->resume)
160                 return drv->resume(soundbus_dev);
161         return 0;
162 }
163
164 #endif /* CONFIG_PM */
165
166 extern struct device_attribute soundbus_dev_attrs[];
167
168 static struct bus_type soundbus_bus_type = {
169         .name           = "aoa-soundbus",
170         .probe          = soundbus_probe,
171         .uevent         = soundbus_uevent,
172         .remove         = soundbus_device_remove,
173         .shutdown       = soundbus_device_shutdown,
174 #ifdef CONFIG_PM
175         .suspend        = soundbus_device_suspend,
176         .resume         = soundbus_device_resume,
177 #endif
178         .dev_attrs      = soundbus_dev_attrs,
179 };
180
181 int soundbus_add_one(struct soundbus_dev *dev)
182 {
183         static int devcount;
184
185         /* sanity checks */
186         if (!dev->attach_codec ||
187             !dev->ofdev.node ||
188             dev->pcmname ||
189             dev->pcmid != -1) {
190                 printk(KERN_ERR "soundbus: adding device failed sanity check!\n");
191                 return -EINVAL;
192         }
193
194         snprintf(dev->ofdev.dev.bus_id, BUS_ID_SIZE, "soundbus:%x", ++devcount);
195         dev->ofdev.dev.bus = &soundbus_bus_type;
196         return of_device_register(&dev->ofdev);
197 }
198 EXPORT_SYMBOL_GPL(soundbus_add_one);
199
200 void soundbus_remove_one(struct soundbus_dev *dev)
201 {
202         of_device_unregister(&dev->ofdev);
203 }
204 EXPORT_SYMBOL_GPL(soundbus_remove_one);
205
206 int soundbus_register_driver(struct soundbus_driver *drv)
207 {
208         /* initialize common driver fields */
209         drv->driver.name = drv->name;
210         drv->driver.bus = &soundbus_bus_type;
211
212         /* register with core */
213         return driver_register(&drv->driver);
214 }
215 EXPORT_SYMBOL_GPL(soundbus_register_driver);
216
217 void soundbus_unregister_driver(struct soundbus_driver *drv)
218 {
219         driver_unregister(&drv->driver);
220 }
221 EXPORT_SYMBOL_GPL(soundbus_unregister_driver);
222
223 static int __init soundbus_init(void)
224 {
225         return bus_register(&soundbus_bus_type);
226 }
227
228 static void __exit soundbus_exit(void)
229 {
230         bus_unregister(&soundbus_bus_type);
231 }
232
233 subsys_initcall(soundbus_init);
234 module_exit(soundbus_exit);