Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * ALSA sequencer device management |
| 3 | * Copyright (c) 1999 by Takashi Iwai <tiwai@suse.de> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 | * |
| 19 | * |
| 20 | *---------------------------------------------------------------- |
| 21 | * |
| 22 | * This device handler separates the card driver module from sequencer |
| 23 | * stuff (sequencer core, synth drivers, etc), so that user can avoid |
| 24 | * to spend unnecessary resources e.g. if he needs only listening to |
| 25 | * MP3s. |
| 26 | * |
| 27 | * The card (or lowlevel) driver creates a sequencer device entry |
| 28 | * via snd_seq_device_new(). This is an entry pointer to communicate |
| 29 | * with the sequencer device "driver", which is involved with the |
| 30 | * actual part to communicate with the sequencer core. |
| 31 | * Each sequencer device entry has an id string and the corresponding |
| 32 | * driver with the same id is loaded when required. For example, |
| 33 | * lowlevel codes to access emu8000 chip on sbawe card are included in |
| 34 | * emu8000-synth module. To activate this module, the hardware |
| 35 | * resources like i/o port are passed via snd_seq_device argument. |
| 36 | * |
| 37 | */ |
| 38 | |
| 39 | #include <sound/driver.h> |
| 40 | #include <linux/init.h> |
| 41 | #include <sound/core.h> |
| 42 | #include <sound/info.h> |
| 43 | #include <sound/seq_device.h> |
| 44 | #include <sound/seq_kernel.h> |
| 45 | #include <sound/initval.h> |
| 46 | #include <linux/kmod.h> |
| 47 | #include <linux/slab.h> |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 48 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | |
| 50 | MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>"); |
| 51 | MODULE_DESCRIPTION("ALSA sequencer device management"); |
| 52 | MODULE_LICENSE("GPL"); |
| 53 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | /* driver state */ |
| 55 | #define DRIVER_EMPTY 0 |
| 56 | #define DRIVER_LOADED (1<<0) |
| 57 | #define DRIVER_REQUESTED (1<<1) |
| 58 | #define DRIVER_LOCKED (1<<2) |
| 59 | |
| 60 | struct ops_list { |
| 61 | char id[ID_LEN]; /* driver id */ |
| 62 | int driver; /* driver state */ |
| 63 | int used; /* reference counter */ |
| 64 | int argsize; /* argument size */ |
| 65 | |
| 66 | /* operators */ |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 67 | struct snd_seq_dev_ops ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | |
| 69 | /* registred devices */ |
| 70 | struct list_head dev_list; /* list of devices */ |
| 71 | int num_devices; /* number of associated devices */ |
| 72 | int num_init_devices; /* number of initialized devices */ |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 73 | struct mutex reg_mutex; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | |
| 75 | struct list_head list; /* next driver */ |
| 76 | }; |
| 77 | |
| 78 | |
| 79 | static LIST_HEAD(opslist); |
| 80 | static int num_ops; |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 81 | static DEFINE_MUTEX(ops_mutex); |
Takashi Iwai | 04f141a | 2005-12-01 10:43:51 +0100 | [diff] [blame] | 82 | #ifdef CONFIG_PROC_FS |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 83 | static struct snd_info_entry *info_entry = NULL; |
Takashi Iwai | 04f141a | 2005-12-01 10:43:51 +0100 | [diff] [blame] | 84 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | |
| 86 | /* |
| 87 | * prototypes |
| 88 | */ |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 89 | static int snd_seq_device_free(struct snd_seq_device *dev); |
| 90 | static int snd_seq_device_dev_free(struct snd_device *device); |
| 91 | static int snd_seq_device_dev_register(struct snd_device *device); |
| 92 | static int snd_seq_device_dev_disconnect(struct snd_device *device); |
| 93 | static int snd_seq_device_dev_unregister(struct snd_device *device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 95 | static int init_device(struct snd_seq_device *dev, struct ops_list *ops); |
| 96 | static int free_device(struct snd_seq_device *dev, struct ops_list *ops); |
| 97 | static struct ops_list *find_driver(char *id, int create_if_empty); |
| 98 | static struct ops_list *create_driver(char *id); |
| 99 | static void unlock_driver(struct ops_list *ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | static void remove_drivers(void); |
| 101 | |
| 102 | /* |
| 103 | * show all drivers and their status |
| 104 | */ |
| 105 | |
Takashi Iwai | 04f141a | 2005-12-01 10:43:51 +0100 | [diff] [blame] | 106 | #ifdef CONFIG_PROC_FS |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 107 | static void snd_seq_device_info(struct snd_info_entry *entry, |
| 108 | struct snd_info_buffer *buffer) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | { |
| 110 | struct list_head *head; |
| 111 | |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 112 | mutex_lock(&ops_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | list_for_each(head, &opslist) { |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 114 | struct ops_list *ops = list_entry(head, struct ops_list, list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | snd_iprintf(buffer, "snd-%s%s%s%s,%d\n", |
| 116 | ops->id, |
| 117 | ops->driver & DRIVER_LOADED ? ",loaded" : (ops->driver == DRIVER_EMPTY ? ",empty" : ""), |
| 118 | ops->driver & DRIVER_REQUESTED ? ",requested" : "", |
| 119 | ops->driver & DRIVER_LOCKED ? ",locked" : "", |
| 120 | ops->num_devices); |
| 121 | } |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 122 | mutex_unlock(&ops_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | } |
Takashi Iwai | 04f141a | 2005-12-01 10:43:51 +0100 | [diff] [blame] | 124 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | |
| 126 | /* |
| 127 | * load all registered drivers (called from seq_clientmgr.c) |
| 128 | */ |
| 129 | |
| 130 | #ifdef CONFIG_KMOD |
| 131 | /* avoid auto-loading during module_init() */ |
| 132 | static int snd_seq_in_init; |
| 133 | void snd_seq_autoload_lock(void) |
| 134 | { |
| 135 | snd_seq_in_init++; |
| 136 | } |
| 137 | |
| 138 | void snd_seq_autoload_unlock(void) |
| 139 | { |
| 140 | snd_seq_in_init--; |
| 141 | } |
| 142 | #endif |
| 143 | |
| 144 | void snd_seq_device_load_drivers(void) |
| 145 | { |
| 146 | #ifdef CONFIG_KMOD |
| 147 | struct list_head *head; |
| 148 | |
| 149 | /* Calling request_module during module_init() |
| 150 | * may cause blocking. |
| 151 | */ |
| 152 | if (snd_seq_in_init) |
| 153 | return; |
| 154 | |
| 155 | if (! current->fs->root) |
| 156 | return; |
| 157 | |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 158 | mutex_lock(&ops_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | list_for_each(head, &opslist) { |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 160 | struct ops_list *ops = list_entry(head, struct ops_list, list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | if (! (ops->driver & DRIVER_LOADED) && |
| 162 | ! (ops->driver & DRIVER_REQUESTED)) { |
| 163 | ops->used++; |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 164 | mutex_unlock(&ops_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | ops->driver |= DRIVER_REQUESTED; |
| 166 | request_module("snd-%s", ops->id); |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 167 | mutex_lock(&ops_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | ops->used--; |
| 169 | } |
| 170 | } |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 171 | mutex_unlock(&ops_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | #endif |
| 173 | } |
| 174 | |
| 175 | /* |
| 176 | * register a sequencer device |
| 177 | * card = card info (NULL allowed) |
| 178 | * device = device number (if any) |
| 179 | * id = id of driver |
| 180 | * result = return pointer (NULL allowed if unnecessary) |
| 181 | */ |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 182 | int snd_seq_device_new(struct snd_card *card, int device, char *id, int argsize, |
| 183 | struct snd_seq_device **result) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | { |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 185 | struct snd_seq_device *dev; |
| 186 | struct ops_list *ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | int err; |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 188 | static struct snd_device_ops dops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | .dev_free = snd_seq_device_dev_free, |
| 190 | .dev_register = snd_seq_device_dev_register, |
| 191 | .dev_disconnect = snd_seq_device_dev_disconnect, |
| 192 | .dev_unregister = snd_seq_device_dev_unregister |
| 193 | }; |
| 194 | |
| 195 | if (result) |
| 196 | *result = NULL; |
| 197 | |
| 198 | snd_assert(id != NULL, return -EINVAL); |
| 199 | |
| 200 | ops = find_driver(id, 1); |
| 201 | if (ops == NULL) |
| 202 | return -ENOMEM; |
| 203 | |
Takashi Iwai | ecca82b | 2005-09-09 14:20:49 +0200 | [diff] [blame] | 204 | dev = kzalloc(sizeof(*dev)*2 + argsize, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | if (dev == NULL) { |
| 206 | unlock_driver(ops); |
| 207 | return -ENOMEM; |
| 208 | } |
| 209 | |
| 210 | /* set up device info */ |
| 211 | dev->card = card; |
| 212 | dev->device = device; |
| 213 | strlcpy(dev->id, id, sizeof(dev->id)); |
| 214 | dev->argsize = argsize; |
| 215 | dev->status = SNDRV_SEQ_DEVICE_FREE; |
| 216 | |
| 217 | /* add this device to the list */ |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 218 | mutex_lock(&ops->reg_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | list_add_tail(&dev->list, &ops->dev_list); |
| 220 | ops->num_devices++; |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 221 | mutex_unlock(&ops->reg_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | |
| 223 | unlock_driver(ops); |
| 224 | |
| 225 | if ((err = snd_device_new(card, SNDRV_DEV_SEQUENCER, dev, &dops)) < 0) { |
| 226 | snd_seq_device_free(dev); |
| 227 | return err; |
| 228 | } |
| 229 | |
| 230 | if (result) |
| 231 | *result = dev; |
| 232 | |
| 233 | return 0; |
| 234 | } |
| 235 | |
| 236 | /* |
| 237 | * free the existing device |
| 238 | */ |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 239 | static int snd_seq_device_free(struct snd_seq_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | { |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 241 | struct ops_list *ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | |
| 243 | snd_assert(dev != NULL, return -EINVAL); |
| 244 | |
| 245 | ops = find_driver(dev->id, 0); |
| 246 | if (ops == NULL) |
| 247 | return -ENXIO; |
| 248 | |
| 249 | /* remove the device from the list */ |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 250 | mutex_lock(&ops->reg_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | list_del(&dev->list); |
| 252 | ops->num_devices--; |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 253 | mutex_unlock(&ops->reg_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | |
| 255 | free_device(dev, ops); |
| 256 | if (dev->private_free) |
| 257 | dev->private_free(dev); |
| 258 | kfree(dev); |
| 259 | |
| 260 | unlock_driver(ops); |
| 261 | |
| 262 | return 0; |
| 263 | } |
| 264 | |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 265 | static int snd_seq_device_dev_free(struct snd_device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | { |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 267 | struct snd_seq_device *dev = device->device_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | return snd_seq_device_free(dev); |
| 269 | } |
| 270 | |
| 271 | /* |
| 272 | * register the device |
| 273 | */ |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 274 | static int snd_seq_device_dev_register(struct snd_device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | { |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 276 | struct snd_seq_device *dev = device->device_data; |
| 277 | struct ops_list *ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | |
| 279 | ops = find_driver(dev->id, 0); |
| 280 | if (ops == NULL) |
| 281 | return -ENOENT; |
| 282 | |
| 283 | /* initialize this device if the corresponding driver was |
| 284 | * already loaded |
| 285 | */ |
| 286 | if (ops->driver & DRIVER_LOADED) |
| 287 | init_device(dev, ops); |
| 288 | |
| 289 | unlock_driver(ops); |
| 290 | return 0; |
| 291 | } |
| 292 | |
| 293 | /* |
| 294 | * disconnect the device |
| 295 | */ |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 296 | static int snd_seq_device_dev_disconnect(struct snd_device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | { |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 298 | struct snd_seq_device *dev = device->device_data; |
| 299 | struct ops_list *ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | |
| 301 | ops = find_driver(dev->id, 0); |
| 302 | if (ops == NULL) |
| 303 | return -ENOENT; |
| 304 | |
| 305 | free_device(dev, ops); |
| 306 | |
| 307 | unlock_driver(ops); |
| 308 | return 0; |
| 309 | } |
| 310 | |
| 311 | /* |
| 312 | * unregister the existing device |
| 313 | */ |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 314 | static int snd_seq_device_dev_unregister(struct snd_device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | { |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 316 | struct snd_seq_device *dev = device->device_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | return snd_seq_device_free(dev); |
| 318 | } |
| 319 | |
| 320 | /* |
| 321 | * register device driver |
| 322 | * id = driver id |
| 323 | * entry = driver operators - duplicated to each instance |
| 324 | */ |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 325 | int snd_seq_device_register_driver(char *id, struct snd_seq_dev_ops *entry, |
| 326 | int argsize) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | { |
| 328 | struct list_head *head; |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 329 | struct ops_list *ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | |
| 331 | if (id == NULL || entry == NULL || |
| 332 | entry->init_device == NULL || entry->free_device == NULL) |
| 333 | return -EINVAL; |
| 334 | |
| 335 | snd_seq_autoload_lock(); |
| 336 | ops = find_driver(id, 1); |
| 337 | if (ops == NULL) { |
| 338 | snd_seq_autoload_unlock(); |
| 339 | return -ENOMEM; |
| 340 | } |
| 341 | if (ops->driver & DRIVER_LOADED) { |
| 342 | snd_printk(KERN_WARNING "driver_register: driver '%s' already exists\n", id); |
| 343 | unlock_driver(ops); |
| 344 | snd_seq_autoload_unlock(); |
| 345 | return -EBUSY; |
| 346 | } |
| 347 | |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 348 | mutex_lock(&ops->reg_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | /* copy driver operators */ |
| 350 | ops->ops = *entry; |
| 351 | ops->driver |= DRIVER_LOADED; |
| 352 | ops->argsize = argsize; |
| 353 | |
| 354 | /* initialize existing devices if necessary */ |
| 355 | list_for_each(head, &ops->dev_list) { |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 356 | struct snd_seq_device *dev = list_entry(head, struct snd_seq_device, list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | init_device(dev, ops); |
| 358 | } |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 359 | mutex_unlock(&ops->reg_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | |
| 361 | unlock_driver(ops); |
| 362 | snd_seq_autoload_unlock(); |
| 363 | |
| 364 | return 0; |
| 365 | } |
| 366 | |
| 367 | |
| 368 | /* |
| 369 | * create driver record |
| 370 | */ |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 371 | static struct ops_list * create_driver(char *id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | { |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 373 | struct ops_list *ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | |
| 375 | ops = kmalloc(sizeof(*ops), GFP_KERNEL); |
| 376 | if (ops == NULL) |
| 377 | return ops; |
| 378 | memset(ops, 0, sizeof(*ops)); |
| 379 | |
| 380 | /* set up driver entry */ |
| 381 | strlcpy(ops->id, id, sizeof(ops->id)); |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 382 | mutex_init(&ops->reg_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | ops->driver = DRIVER_EMPTY; |
| 384 | INIT_LIST_HEAD(&ops->dev_list); |
| 385 | /* lock this instance */ |
| 386 | ops->used = 1; |
| 387 | |
| 388 | /* register driver entry */ |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 389 | mutex_lock(&ops_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | list_add_tail(&ops->list, &opslist); |
| 391 | num_ops++; |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 392 | mutex_unlock(&ops_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | |
| 394 | return ops; |
| 395 | } |
| 396 | |
| 397 | |
| 398 | /* |
| 399 | * unregister the specified driver |
| 400 | */ |
| 401 | int snd_seq_device_unregister_driver(char *id) |
| 402 | { |
| 403 | struct list_head *head; |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 404 | struct ops_list *ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | |
| 406 | ops = find_driver(id, 0); |
| 407 | if (ops == NULL) |
| 408 | return -ENXIO; |
| 409 | if (! (ops->driver & DRIVER_LOADED) || |
| 410 | (ops->driver & DRIVER_LOCKED)) { |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 411 | snd_printk(KERN_ERR "driver_unregister: cannot unload driver '%s': status=%x\n", |
| 412 | id, ops->driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | unlock_driver(ops); |
| 414 | return -EBUSY; |
| 415 | } |
| 416 | |
| 417 | /* close and release all devices associated with this driver */ |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 418 | mutex_lock(&ops->reg_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | ops->driver |= DRIVER_LOCKED; /* do not remove this driver recursively */ |
| 420 | list_for_each(head, &ops->dev_list) { |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 421 | struct snd_seq_device *dev = list_entry(head, struct snd_seq_device, list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | free_device(dev, ops); |
| 423 | } |
| 424 | |
| 425 | ops->driver = 0; |
| 426 | if (ops->num_init_devices > 0) |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 427 | snd_printk(KERN_ERR "free_driver: init_devices > 0!! (%d)\n", |
| 428 | ops->num_init_devices); |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 429 | mutex_unlock(&ops->reg_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | |
| 431 | unlock_driver(ops); |
| 432 | |
| 433 | /* remove empty driver entries */ |
| 434 | remove_drivers(); |
| 435 | |
| 436 | return 0; |
| 437 | } |
| 438 | |
| 439 | |
| 440 | /* |
| 441 | * remove empty driver entries |
| 442 | */ |
| 443 | static void remove_drivers(void) |
| 444 | { |
| 445 | struct list_head *head; |
| 446 | |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 447 | mutex_lock(&ops_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | head = opslist.next; |
| 449 | while (head != &opslist) { |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 450 | struct ops_list *ops = list_entry(head, struct ops_list, list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | if (! (ops->driver & DRIVER_LOADED) && |
| 452 | ops->used == 0 && ops->num_devices == 0) { |
| 453 | head = head->next; |
| 454 | list_del(&ops->list); |
| 455 | kfree(ops); |
| 456 | num_ops--; |
| 457 | } else |
| 458 | head = head->next; |
| 459 | } |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 460 | mutex_unlock(&ops_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | /* |
| 464 | * initialize the device - call init_device operator |
| 465 | */ |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 466 | static int init_device(struct snd_seq_device *dev, struct ops_list *ops) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | { |
| 468 | if (! (ops->driver & DRIVER_LOADED)) |
| 469 | return 0; /* driver is not loaded yet */ |
| 470 | if (dev->status != SNDRV_SEQ_DEVICE_FREE) |
| 471 | return 0; /* already initialized */ |
| 472 | if (ops->argsize != dev->argsize) { |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 473 | snd_printk(KERN_ERR "incompatible device '%s' for plug-in '%s' (%d %d)\n", |
| 474 | dev->name, ops->id, ops->argsize, dev->argsize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | return -EINVAL; |
| 476 | } |
| 477 | if (ops->ops.init_device(dev) >= 0) { |
| 478 | dev->status = SNDRV_SEQ_DEVICE_REGISTERED; |
| 479 | ops->num_init_devices++; |
| 480 | } else { |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 481 | snd_printk(KERN_ERR "init_device failed: %s: %s\n", |
| 482 | dev->name, dev->id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | return 0; |
| 486 | } |
| 487 | |
| 488 | /* |
| 489 | * release the device - call free_device operator |
| 490 | */ |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 491 | static int free_device(struct snd_seq_device *dev, struct ops_list *ops) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | { |
| 493 | int result; |
| 494 | |
| 495 | if (! (ops->driver & DRIVER_LOADED)) |
| 496 | return 0; /* driver is not loaded yet */ |
| 497 | if (dev->status != SNDRV_SEQ_DEVICE_REGISTERED) |
| 498 | return 0; /* not registered */ |
| 499 | if (ops->argsize != dev->argsize) { |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 500 | snd_printk(KERN_ERR "incompatible device '%s' for plug-in '%s' (%d %d)\n", |
| 501 | dev->name, ops->id, ops->argsize, dev->argsize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | return -EINVAL; |
| 503 | } |
| 504 | if ((result = ops->ops.free_device(dev)) >= 0 || result == -ENXIO) { |
| 505 | dev->status = SNDRV_SEQ_DEVICE_FREE; |
| 506 | dev->driver_data = NULL; |
| 507 | ops->num_init_devices--; |
| 508 | } else { |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 509 | snd_printk(KERN_ERR "free_device failed: %s: %s\n", |
| 510 | dev->name, dev->id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | return 0; |
| 514 | } |
| 515 | |
| 516 | /* |
| 517 | * find the matching driver with given id |
| 518 | */ |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 519 | static struct ops_list * find_driver(char *id, int create_if_empty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | { |
| 521 | struct list_head *head; |
| 522 | |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 523 | mutex_lock(&ops_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | list_for_each(head, &opslist) { |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 525 | struct ops_list *ops = list_entry(head, struct ops_list, list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | if (strcmp(ops->id, id) == 0) { |
| 527 | ops->used++; |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 528 | mutex_unlock(&ops_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | return ops; |
| 530 | } |
| 531 | } |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 532 | mutex_unlock(&ops_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 533 | if (create_if_empty) |
| 534 | return create_driver(id); |
| 535 | return NULL; |
| 536 | } |
| 537 | |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 538 | static void unlock_driver(struct ops_list *ops) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | { |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 540 | mutex_lock(&ops_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | ops->used--; |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 542 | mutex_unlock(&ops_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | |
| 546 | /* |
| 547 | * module part |
| 548 | */ |
| 549 | |
| 550 | static int __init alsa_seq_device_init(void) |
| 551 | { |
Takashi Iwai | 04f141a | 2005-12-01 10:43:51 +0100 | [diff] [blame] | 552 | #ifdef CONFIG_PROC_FS |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 553 | info_entry = snd_info_create_module_entry(THIS_MODULE, "drivers", |
| 554 | snd_seq_root); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | if (info_entry == NULL) |
| 556 | return -ENOMEM; |
| 557 | info_entry->content = SNDRV_INFO_CONTENT_TEXT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | info_entry->c.text.read = snd_seq_device_info; |
| 559 | if (snd_info_register(info_entry) < 0) { |
| 560 | snd_info_free_entry(info_entry); |
| 561 | return -ENOMEM; |
| 562 | } |
Takashi Iwai | 04f141a | 2005-12-01 10:43:51 +0100 | [diff] [blame] | 563 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | return 0; |
| 565 | } |
| 566 | |
| 567 | static void __exit alsa_seq_device_exit(void) |
| 568 | { |
| 569 | remove_drivers(); |
Takashi Iwai | 04f141a | 2005-12-01 10:43:51 +0100 | [diff] [blame] | 570 | #ifdef CONFIG_PROC_FS |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | snd_info_unregister(info_entry); |
Takashi Iwai | 04f141a | 2005-12-01 10:43:51 +0100 | [diff] [blame] | 572 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 573 | if (num_ops) |
| 574 | snd_printk(KERN_ERR "drivers not released (%d)\n", num_ops); |
| 575 | } |
| 576 | |
| 577 | module_init(alsa_seq_device_init) |
| 578 | module_exit(alsa_seq_device_exit) |
| 579 | |
| 580 | EXPORT_SYMBOL(snd_seq_device_load_drivers); |
| 581 | EXPORT_SYMBOL(snd_seq_device_new); |
| 582 | EXPORT_SYMBOL(snd_seq_device_register_driver); |
| 583 | EXPORT_SYMBOL(snd_seq_device_unregister_driver); |
| 584 | #ifdef CONFIG_KMOD |
| 585 | EXPORT_SYMBOL(snd_seq_autoload_lock); |
| 586 | EXPORT_SYMBOL(snd_seq_autoload_unlock); |
| 587 | #endif |