Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 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; |
Stephen Rothwell | a7edd0e | 2007-04-03 10:52:17 +1000 | [diff] [blame^] | 64 | char *scratch; |
| 65 | const char *compat; |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 66 | int i = 0; |
Stephen Rothwell | a7edd0e | 2007-04-03 10:52:17 +1000 | [diff] [blame^] | 67 | int length, cplen, seen = 0; |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 68 | |
| 69 | if (!dev) |
| 70 | return -ENODEV; |
| 71 | |
| 72 | soundbus_dev = to_soundbus_device(dev); |
| 73 | if (!soundbus_dev) |
| 74 | return -ENODEV; |
| 75 | |
| 76 | of = &soundbus_dev->ofdev; |
| 77 | |
| 78 | /* stuff we want to pass to /sbin/hotplug */ |
| 79 | envp[i++] = scratch = buffer; |
| 80 | length = scnprintf (scratch, buffer_size, "OF_NAME=%s", of->node->name); |
| 81 | ++length; |
| 82 | buffer_size -= length; |
| 83 | if ((buffer_size <= 0) || (i >= num_envp)) |
| 84 | return -ENOMEM; |
| 85 | scratch += length; |
| 86 | |
| 87 | envp[i++] = scratch; |
| 88 | length = scnprintf (scratch, buffer_size, "OF_TYPE=%s", of->node->type); |
| 89 | ++length; |
| 90 | buffer_size -= length; |
| 91 | if ((buffer_size <= 0) || (i >= num_envp)) |
| 92 | return -ENOMEM; |
| 93 | scratch += length; |
| 94 | |
| 95 | /* Since the compatible field can contain pretty much anything |
| 96 | * it's not really legal to split it out with commas. We split it |
| 97 | * up using a number of environment variables instead. */ |
| 98 | |
Stephen Rothwell | a7edd0e | 2007-04-03 10:52:17 +1000 | [diff] [blame^] | 99 | compat = get_property(of->node, "compatible", &cplen); |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 100 | while (compat && cplen > 0) { |
| 101 | envp[i++] = scratch; |
| 102 | length = scnprintf (scratch, buffer_size, |
| 103 | "OF_COMPATIBLE_%d=%s", seen, compat); |
| 104 | ++length; |
| 105 | buffer_size -= length; |
| 106 | if ((buffer_size <= 0) || (i >= num_envp)) |
| 107 | return -ENOMEM; |
| 108 | scratch += length; |
| 109 | length = strlen (compat) + 1; |
| 110 | compat += length; |
| 111 | cplen -= length; |
| 112 | seen++; |
| 113 | } |
| 114 | |
| 115 | envp[i++] = scratch; |
| 116 | length = scnprintf (scratch, buffer_size, "OF_COMPATIBLE_N=%d", seen); |
| 117 | ++length; |
| 118 | buffer_size -= length; |
| 119 | if ((buffer_size <= 0) || (i >= num_envp)) |
| 120 | return -ENOMEM; |
| 121 | scratch += length; |
| 122 | |
| 123 | envp[i++] = scratch; |
| 124 | length = scnprintf (scratch, buffer_size, "MODALIAS=%s", |
| 125 | soundbus_dev->modalias); |
| 126 | |
| 127 | buffer_size -= length; |
| 128 | if ((buffer_size <= 0) || (i >= num_envp)) |
| 129 | return -ENOMEM; |
| 130 | |
| 131 | envp[i] = NULL; |
| 132 | |
| 133 | return 0; |
| 134 | } |
| 135 | |
| 136 | static int soundbus_device_remove(struct device *dev) |
| 137 | { |
| 138 | struct soundbus_dev * soundbus_dev = to_soundbus_device(dev); |
| 139 | struct soundbus_driver * drv = to_soundbus_driver(dev->driver); |
| 140 | |
| 141 | if (dev->driver && drv->remove) |
| 142 | drv->remove(soundbus_dev); |
| 143 | soundbus_dev_put(soundbus_dev); |
| 144 | |
| 145 | return 0; |
| 146 | } |
| 147 | |
| 148 | static void soundbus_device_shutdown(struct device *dev) |
| 149 | { |
| 150 | struct soundbus_dev * soundbus_dev = to_soundbus_device(dev); |
| 151 | struct soundbus_driver * drv = to_soundbus_driver(dev->driver); |
| 152 | |
| 153 | if (dev->driver && drv->shutdown) |
| 154 | drv->shutdown(soundbus_dev); |
| 155 | } |
| 156 | |
| 157 | #ifdef CONFIG_PM |
| 158 | |
| 159 | static int soundbus_device_suspend(struct device *dev, pm_message_t state) |
| 160 | { |
| 161 | struct soundbus_dev * soundbus_dev = to_soundbus_device(dev); |
| 162 | struct soundbus_driver * drv = to_soundbus_driver(dev->driver); |
| 163 | |
| 164 | if (dev->driver && drv->suspend) |
| 165 | return drv->suspend(soundbus_dev, state); |
| 166 | return 0; |
| 167 | } |
| 168 | |
| 169 | static int soundbus_device_resume(struct device * dev) |
| 170 | { |
| 171 | struct soundbus_dev * soundbus_dev = to_soundbus_device(dev); |
| 172 | struct soundbus_driver * drv = to_soundbus_driver(dev->driver); |
| 173 | |
| 174 | if (dev->driver && drv->resume) |
| 175 | return drv->resume(soundbus_dev); |
| 176 | return 0; |
| 177 | } |
| 178 | |
| 179 | #endif /* CONFIG_PM */ |
| 180 | |
| 181 | extern struct device_attribute soundbus_dev_attrs[]; |
| 182 | |
| 183 | static struct bus_type soundbus_bus_type = { |
| 184 | .name = "aoa-soundbus", |
| 185 | .probe = soundbus_probe, |
| 186 | .uevent = soundbus_uevent, |
| 187 | .remove = soundbus_device_remove, |
| 188 | .shutdown = soundbus_device_shutdown, |
| 189 | #ifdef CONFIG_PM |
| 190 | .suspend = soundbus_device_suspend, |
| 191 | .resume = soundbus_device_resume, |
| 192 | #endif |
| 193 | .dev_attrs = soundbus_dev_attrs, |
| 194 | }; |
| 195 | |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 196 | int soundbus_add_one(struct soundbus_dev *dev) |
| 197 | { |
| 198 | static int devcount; |
| 199 | |
| 200 | /* sanity checks */ |
| 201 | if (!dev->attach_codec || |
| 202 | !dev->ofdev.node || |
| 203 | dev->pcmname || |
| 204 | dev->pcmid != -1) { |
| 205 | printk(KERN_ERR "soundbus: adding device failed sanity check!\n"); |
| 206 | return -EINVAL; |
| 207 | } |
| 208 | |
| 209 | snprintf(dev->ofdev.dev.bus_id, BUS_ID_SIZE, "soundbus:%x", ++devcount); |
| 210 | dev->ofdev.dev.bus = &soundbus_bus_type; |
| 211 | return of_device_register(&dev->ofdev); |
| 212 | } |
| 213 | EXPORT_SYMBOL_GPL(soundbus_add_one); |
| 214 | |
| 215 | void soundbus_remove_one(struct soundbus_dev *dev) |
| 216 | { |
| 217 | of_device_unregister(&dev->ofdev); |
| 218 | } |
| 219 | EXPORT_SYMBOL_GPL(soundbus_remove_one); |
| 220 | |
| 221 | int soundbus_register_driver(struct soundbus_driver *drv) |
| 222 | { |
| 223 | /* initialize common driver fields */ |
| 224 | drv->driver.name = drv->name; |
| 225 | drv->driver.bus = &soundbus_bus_type; |
| 226 | |
| 227 | /* register with core */ |
| 228 | return driver_register(&drv->driver); |
| 229 | } |
| 230 | EXPORT_SYMBOL_GPL(soundbus_register_driver); |
| 231 | |
| 232 | void soundbus_unregister_driver(struct soundbus_driver *drv) |
| 233 | { |
| 234 | driver_unregister(&drv->driver); |
| 235 | } |
| 236 | EXPORT_SYMBOL_GPL(soundbus_unregister_driver); |
| 237 | |
Benjamin Herrenschmidt | a08bc4c | 2006-07-10 04:44:35 -0700 | [diff] [blame] | 238 | static int __init soundbus_init(void) |
| 239 | { |
| 240 | return bus_register(&soundbus_bus_type); |
| 241 | } |
| 242 | |
| 243 | static void __exit soundbus_exit(void) |
| 244 | { |
| 245 | bus_unregister(&soundbus_bus_type); |
| 246 | } |
| 247 | |
| 248 | subsys_initcall(soundbus_init); |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 249 | module_exit(soundbus_exit); |