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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | #include <linux/init.h> |
Paul Gortmaker | da155d5 | 2011-07-15 12:38:28 -0400 | [diff] [blame] | 40 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | 6581f4e | 2006-05-17 17:14:51 +0200 | [diff] [blame] | 83 | static struct snd_info_entry *info_entry; |
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); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 94 | static int init_device(struct snd_seq_device *dev, struct ops_list *ops); |
| 95 | static int free_device(struct snd_seq_device *dev, struct ops_list *ops); |
| 96 | static struct ops_list *find_driver(char *id, int create_if_empty); |
| 97 | static struct ops_list *create_driver(char *id); |
| 98 | static void unlock_driver(struct ops_list *ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | static void remove_drivers(void); |
| 100 | |
| 101 | /* |
| 102 | * show all drivers and their status |
| 103 | */ |
| 104 | |
Takashi Iwai | 04f141a | 2005-12-01 10:43:51 +0100 | [diff] [blame] | 105 | #ifdef CONFIG_PROC_FS |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 106 | static void snd_seq_device_info(struct snd_info_entry *entry, |
| 107 | struct snd_info_buffer *buffer) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | { |
Johannes Berg | 9244b2c | 2006-10-05 16:02:22 +0200 | [diff] [blame] | 109 | struct ops_list *ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 111 | mutex_lock(&ops_mutex); |
Johannes Berg | 9244b2c | 2006-10-05 16:02:22 +0200 | [diff] [blame] | 112 | list_for_each_entry(ops, &opslist, list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | snd_iprintf(buffer, "snd-%s%s%s%s,%d\n", |
| 114 | ops->id, |
| 115 | ops->driver & DRIVER_LOADED ? ",loaded" : (ops->driver == DRIVER_EMPTY ? ",empty" : ""), |
| 116 | ops->driver & DRIVER_REQUESTED ? ",requested" : "", |
| 117 | ops->driver & DRIVER_LOCKED ? ",locked" : "", |
| 118 | ops->num_devices); |
| 119 | } |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 120 | mutex_unlock(&ops_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | } |
Takashi Iwai | 04f141a | 2005-12-01 10:43:51 +0100 | [diff] [blame] | 122 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | |
| 124 | /* |
| 125 | * load all registered drivers (called from seq_clientmgr.c) |
| 126 | */ |
| 127 | |
Johannes Berg | ee2da99 | 2008-07-09 10:28:41 +0200 | [diff] [blame] | 128 | #ifdef CONFIG_MODULES |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | /* avoid auto-loading during module_init() */ |
| 130 | static int snd_seq_in_init; |
| 131 | void snd_seq_autoload_lock(void) |
| 132 | { |
| 133 | snd_seq_in_init++; |
| 134 | } |
| 135 | |
| 136 | void snd_seq_autoload_unlock(void) |
| 137 | { |
| 138 | snd_seq_in_init--; |
| 139 | } |
| 140 | #endif |
| 141 | |
| 142 | void snd_seq_device_load_drivers(void) |
| 143 | { |
Johannes Berg | ee2da99 | 2008-07-09 10:28:41 +0200 | [diff] [blame] | 144 | #ifdef CONFIG_MODULES |
Johannes Berg | 9244b2c | 2006-10-05 16:02:22 +0200 | [diff] [blame] | 145 | struct ops_list *ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | |
| 147 | /* Calling request_module during module_init() |
| 148 | * may cause blocking. |
| 149 | */ |
| 150 | if (snd_seq_in_init) |
| 151 | return; |
| 152 | |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 153 | mutex_lock(&ops_mutex); |
Johannes Berg | 9244b2c | 2006-10-05 16:02:22 +0200 | [diff] [blame] | 154 | list_for_each_entry(ops, &opslist, list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | if (! (ops->driver & DRIVER_LOADED) && |
| 156 | ! (ops->driver & DRIVER_REQUESTED)) { |
| 157 | ops->used++; |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 158 | mutex_unlock(&ops_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | ops->driver |= DRIVER_REQUESTED; |
| 160 | request_module("snd-%s", ops->id); |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 161 | mutex_lock(&ops_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | ops->used--; |
| 163 | } |
| 164 | } |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 165 | mutex_unlock(&ops_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | #endif |
| 167 | } |
| 168 | |
| 169 | /* |
| 170 | * register a sequencer device |
| 171 | * card = card info (NULL allowed) |
| 172 | * device = device number (if any) |
| 173 | * id = id of driver |
| 174 | * result = return pointer (NULL allowed if unnecessary) |
| 175 | */ |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 176 | int snd_seq_device_new(struct snd_card *card, int device, char *id, int argsize, |
| 177 | struct snd_seq_device **result) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | { |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 179 | struct snd_seq_device *dev; |
| 180 | struct ops_list *ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | int err; |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 182 | static struct snd_device_ops dops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | .dev_free = snd_seq_device_dev_free, |
| 184 | .dev_register = snd_seq_device_dev_register, |
| 185 | .dev_disconnect = snd_seq_device_dev_disconnect, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | }; |
| 187 | |
| 188 | if (result) |
| 189 | *result = NULL; |
| 190 | |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 191 | if (snd_BUG_ON(!id)) |
| 192 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | |
| 194 | ops = find_driver(id, 1); |
| 195 | if (ops == NULL) |
| 196 | return -ENOMEM; |
| 197 | |
Takashi Iwai | ecca82b | 2005-09-09 14:20:49 +0200 | [diff] [blame] | 198 | dev = kzalloc(sizeof(*dev)*2 + argsize, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | if (dev == NULL) { |
| 200 | unlock_driver(ops); |
| 201 | return -ENOMEM; |
| 202 | } |
| 203 | |
| 204 | /* set up device info */ |
| 205 | dev->card = card; |
| 206 | dev->device = device; |
| 207 | strlcpy(dev->id, id, sizeof(dev->id)); |
| 208 | dev->argsize = argsize; |
| 209 | dev->status = SNDRV_SEQ_DEVICE_FREE; |
| 210 | |
| 211 | /* add this device to the list */ |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 212 | mutex_lock(&ops->reg_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | list_add_tail(&dev->list, &ops->dev_list); |
| 214 | ops->num_devices++; |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 215 | mutex_unlock(&ops->reg_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | |
| 217 | unlock_driver(ops); |
| 218 | |
| 219 | if ((err = snd_device_new(card, SNDRV_DEV_SEQUENCER, dev, &dops)) < 0) { |
| 220 | snd_seq_device_free(dev); |
| 221 | return err; |
| 222 | } |
| 223 | |
| 224 | if (result) |
| 225 | *result = dev; |
| 226 | |
| 227 | return 0; |
| 228 | } |
| 229 | |
| 230 | /* |
| 231 | * free the existing device |
| 232 | */ |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 233 | static int snd_seq_device_free(struct snd_seq_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | { |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 235 | struct ops_list *ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 237 | if (snd_BUG_ON(!dev)) |
| 238 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | |
| 240 | ops = find_driver(dev->id, 0); |
| 241 | if (ops == NULL) |
| 242 | return -ENXIO; |
| 243 | |
| 244 | /* remove the device from the list */ |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 245 | mutex_lock(&ops->reg_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | list_del(&dev->list); |
| 247 | ops->num_devices--; |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 248 | mutex_unlock(&ops->reg_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | |
| 250 | free_device(dev, ops); |
| 251 | if (dev->private_free) |
| 252 | dev->private_free(dev); |
| 253 | kfree(dev); |
| 254 | |
| 255 | unlock_driver(ops); |
| 256 | |
| 257 | return 0; |
| 258 | } |
| 259 | |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 260 | static int snd_seq_device_dev_free(struct snd_device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | { |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 262 | struct snd_seq_device *dev = device->device_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | return snd_seq_device_free(dev); |
| 264 | } |
| 265 | |
| 266 | /* |
| 267 | * register the device |
| 268 | */ |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 269 | static int snd_seq_device_dev_register(struct snd_device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | { |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 271 | struct snd_seq_device *dev = device->device_data; |
| 272 | struct ops_list *ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | |
| 274 | ops = find_driver(dev->id, 0); |
| 275 | if (ops == NULL) |
| 276 | return -ENOENT; |
| 277 | |
| 278 | /* initialize this device if the corresponding driver was |
| 279 | * already loaded |
| 280 | */ |
| 281 | if (ops->driver & DRIVER_LOADED) |
| 282 | init_device(dev, ops); |
| 283 | |
| 284 | unlock_driver(ops); |
| 285 | return 0; |
| 286 | } |
| 287 | |
| 288 | /* |
| 289 | * disconnect the device |
| 290 | */ |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 291 | static int snd_seq_device_dev_disconnect(struct snd_device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | { |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 293 | struct snd_seq_device *dev = device->device_data; |
| 294 | struct ops_list *ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | |
| 296 | ops = find_driver(dev->id, 0); |
| 297 | if (ops == NULL) |
| 298 | return -ENOENT; |
| 299 | |
| 300 | free_device(dev, ops); |
| 301 | |
| 302 | unlock_driver(ops); |
| 303 | return 0; |
| 304 | } |
| 305 | |
| 306 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | * register device driver |
| 308 | * id = driver id |
| 309 | * entry = driver operators - duplicated to each instance |
| 310 | */ |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 311 | int snd_seq_device_register_driver(char *id, struct snd_seq_dev_ops *entry, |
| 312 | int argsize) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | { |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 314 | struct ops_list *ops; |
Johannes Berg | 9244b2c | 2006-10-05 16:02:22 +0200 | [diff] [blame] | 315 | struct snd_seq_device *dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | |
| 317 | if (id == NULL || entry == NULL || |
| 318 | entry->init_device == NULL || entry->free_device == NULL) |
| 319 | return -EINVAL; |
| 320 | |
| 321 | snd_seq_autoload_lock(); |
| 322 | ops = find_driver(id, 1); |
| 323 | if (ops == NULL) { |
| 324 | snd_seq_autoload_unlock(); |
| 325 | return -ENOMEM; |
| 326 | } |
| 327 | if (ops->driver & DRIVER_LOADED) { |
| 328 | snd_printk(KERN_WARNING "driver_register: driver '%s' already exists\n", id); |
| 329 | unlock_driver(ops); |
| 330 | snd_seq_autoload_unlock(); |
| 331 | return -EBUSY; |
| 332 | } |
| 333 | |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 334 | mutex_lock(&ops->reg_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | /* copy driver operators */ |
| 336 | ops->ops = *entry; |
| 337 | ops->driver |= DRIVER_LOADED; |
| 338 | ops->argsize = argsize; |
| 339 | |
| 340 | /* initialize existing devices if necessary */ |
Johannes Berg | 9244b2c | 2006-10-05 16:02:22 +0200 | [diff] [blame] | 341 | list_for_each_entry(dev, &ops->dev_list, list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | init_device(dev, ops); |
| 343 | } |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 344 | mutex_unlock(&ops->reg_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | |
| 346 | unlock_driver(ops); |
| 347 | snd_seq_autoload_unlock(); |
| 348 | |
| 349 | return 0; |
| 350 | } |
| 351 | |
| 352 | |
| 353 | /* |
| 354 | * create driver record |
| 355 | */ |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 356 | static struct ops_list * create_driver(char *id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | { |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 358 | struct ops_list *ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | |
Panagiotis Issaris | 59feddb | 2006-07-25 15:28:03 +0200 | [diff] [blame] | 360 | ops = kzalloc(sizeof(*ops), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | if (ops == NULL) |
| 362 | return ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | |
| 364 | /* set up driver entry */ |
| 365 | strlcpy(ops->id, id, sizeof(ops->id)); |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 366 | mutex_init(&ops->reg_mutex); |
Arjan van de Ven | 933a2ef | 2006-07-03 00:25:22 -0700 | [diff] [blame] | 367 | /* |
| 368 | * The ->reg_mutex locking rules are per-driver, so we create |
| 369 | * separate per-driver lock classes: |
| 370 | */ |
| 371 | lockdep_set_class(&ops->reg_mutex, (struct lock_class_key *)id); |
| 372 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | ops->driver = DRIVER_EMPTY; |
| 374 | INIT_LIST_HEAD(&ops->dev_list); |
| 375 | /* lock this instance */ |
| 376 | ops->used = 1; |
| 377 | |
| 378 | /* register driver entry */ |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 379 | mutex_lock(&ops_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | list_add_tail(&ops->list, &opslist); |
| 381 | num_ops++; |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 382 | mutex_unlock(&ops_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | |
| 384 | return ops; |
| 385 | } |
| 386 | |
| 387 | |
| 388 | /* |
| 389 | * unregister the specified driver |
| 390 | */ |
| 391 | int snd_seq_device_unregister_driver(char *id) |
| 392 | { |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 393 | struct ops_list *ops; |
Johannes Berg | 9244b2c | 2006-10-05 16:02:22 +0200 | [diff] [blame] | 394 | struct snd_seq_device *dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | |
| 396 | ops = find_driver(id, 0); |
| 397 | if (ops == NULL) |
| 398 | return -ENXIO; |
| 399 | if (! (ops->driver & DRIVER_LOADED) || |
| 400 | (ops->driver & DRIVER_LOCKED)) { |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 401 | snd_printk(KERN_ERR "driver_unregister: cannot unload driver '%s': status=%x\n", |
| 402 | id, ops->driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | unlock_driver(ops); |
| 404 | return -EBUSY; |
| 405 | } |
| 406 | |
| 407 | /* close and release all devices associated with this driver */ |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 408 | mutex_lock(&ops->reg_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | ops->driver |= DRIVER_LOCKED; /* do not remove this driver recursively */ |
Johannes Berg | 9244b2c | 2006-10-05 16:02:22 +0200 | [diff] [blame] | 410 | list_for_each_entry(dev, &ops->dev_list, list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | free_device(dev, ops); |
| 412 | } |
| 413 | |
| 414 | ops->driver = 0; |
| 415 | if (ops->num_init_devices > 0) |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 416 | snd_printk(KERN_ERR "free_driver: init_devices > 0!! (%d)\n", |
| 417 | ops->num_init_devices); |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 418 | mutex_unlock(&ops->reg_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | |
| 420 | unlock_driver(ops); |
| 421 | |
| 422 | /* remove empty driver entries */ |
| 423 | remove_drivers(); |
| 424 | |
| 425 | return 0; |
| 426 | } |
| 427 | |
| 428 | |
| 429 | /* |
| 430 | * remove empty driver entries |
| 431 | */ |
| 432 | static void remove_drivers(void) |
| 433 | { |
| 434 | struct list_head *head; |
| 435 | |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 436 | mutex_lock(&ops_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | head = opslist.next; |
| 438 | while (head != &opslist) { |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 439 | struct ops_list *ops = list_entry(head, struct ops_list, list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | if (! (ops->driver & DRIVER_LOADED) && |
| 441 | ops->used == 0 && ops->num_devices == 0) { |
| 442 | head = head->next; |
| 443 | list_del(&ops->list); |
| 444 | kfree(ops); |
| 445 | num_ops--; |
| 446 | } else |
| 447 | head = head->next; |
| 448 | } |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 449 | mutex_unlock(&ops_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | /* |
| 453 | * initialize the device - call init_device operator |
| 454 | */ |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 455 | 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] | 456 | { |
| 457 | if (! (ops->driver & DRIVER_LOADED)) |
| 458 | return 0; /* driver is not loaded yet */ |
| 459 | if (dev->status != SNDRV_SEQ_DEVICE_FREE) |
| 460 | return 0; /* already initialized */ |
| 461 | if (ops->argsize != dev->argsize) { |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 462 | snd_printk(KERN_ERR "incompatible device '%s' for plug-in '%s' (%d %d)\n", |
| 463 | dev->name, ops->id, ops->argsize, dev->argsize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | return -EINVAL; |
| 465 | } |
| 466 | if (ops->ops.init_device(dev) >= 0) { |
| 467 | dev->status = SNDRV_SEQ_DEVICE_REGISTERED; |
| 468 | ops->num_init_devices++; |
| 469 | } else { |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 470 | snd_printk(KERN_ERR "init_device failed: %s: %s\n", |
| 471 | dev->name, dev->id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | return 0; |
| 475 | } |
| 476 | |
| 477 | /* |
| 478 | * release the device - call free_device operator |
| 479 | */ |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 480 | 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] | 481 | { |
| 482 | int result; |
| 483 | |
| 484 | if (! (ops->driver & DRIVER_LOADED)) |
| 485 | return 0; /* driver is not loaded yet */ |
| 486 | if (dev->status != SNDRV_SEQ_DEVICE_REGISTERED) |
| 487 | return 0; /* not registered */ |
| 488 | if (ops->argsize != dev->argsize) { |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 489 | snd_printk(KERN_ERR "incompatible device '%s' for plug-in '%s' (%d %d)\n", |
| 490 | dev->name, ops->id, ops->argsize, dev->argsize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | return -EINVAL; |
| 492 | } |
| 493 | if ((result = ops->ops.free_device(dev)) >= 0 || result == -ENXIO) { |
| 494 | dev->status = SNDRV_SEQ_DEVICE_FREE; |
| 495 | dev->driver_data = NULL; |
| 496 | ops->num_init_devices--; |
| 497 | } else { |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 498 | snd_printk(KERN_ERR "free_device failed: %s: %s\n", |
| 499 | dev->name, dev->id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | return 0; |
| 503 | } |
| 504 | |
| 505 | /* |
| 506 | * find the matching driver with given id |
| 507 | */ |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 508 | static struct ops_list * find_driver(char *id, int create_if_empty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | { |
Johannes Berg | 9244b2c | 2006-10-05 16:02:22 +0200 | [diff] [blame] | 510 | struct ops_list *ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 512 | mutex_lock(&ops_mutex); |
Johannes Berg | 9244b2c | 2006-10-05 16:02:22 +0200 | [diff] [blame] | 513 | list_for_each_entry(ops, &opslist, list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | if (strcmp(ops->id, id) == 0) { |
| 515 | ops->used++; |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 516 | mutex_unlock(&ops_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | return ops; |
| 518 | } |
| 519 | } |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 520 | mutex_unlock(&ops_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | if (create_if_empty) |
| 522 | return create_driver(id); |
| 523 | return NULL; |
| 524 | } |
| 525 | |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 526 | static void unlock_driver(struct ops_list *ops) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | { |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 528 | mutex_lock(&ops_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | ops->used--; |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 530 | mutex_unlock(&ops_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | |
| 534 | /* |
| 535 | * module part |
| 536 | */ |
| 537 | |
| 538 | static int __init alsa_seq_device_init(void) |
| 539 | { |
Takashi Iwai | 04f141a | 2005-12-01 10:43:51 +0100 | [diff] [blame] | 540 | #ifdef CONFIG_PROC_FS |
Takashi Iwai | c7e0b5b | 2005-11-17 14:04:02 +0100 | [diff] [blame] | 541 | info_entry = snd_info_create_module_entry(THIS_MODULE, "drivers", |
| 542 | snd_seq_root); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | if (info_entry == NULL) |
| 544 | return -ENOMEM; |
| 545 | info_entry->content = SNDRV_INFO_CONTENT_TEXT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | info_entry->c.text.read = snd_seq_device_info; |
| 547 | if (snd_info_register(info_entry) < 0) { |
| 548 | snd_info_free_entry(info_entry); |
| 549 | return -ENOMEM; |
| 550 | } |
Takashi Iwai | 04f141a | 2005-12-01 10:43:51 +0100 | [diff] [blame] | 551 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | return 0; |
| 553 | } |
| 554 | |
| 555 | static void __exit alsa_seq_device_exit(void) |
| 556 | { |
| 557 | remove_drivers(); |
Takashi Iwai | 04f141a | 2005-12-01 10:43:51 +0100 | [diff] [blame] | 558 | #ifdef CONFIG_PROC_FS |
Takashi Iwai | 746d4a0 | 2006-06-23 14:37:59 +0200 | [diff] [blame] | 559 | snd_info_free_entry(info_entry); |
Takashi Iwai | 04f141a | 2005-12-01 10:43:51 +0100 | [diff] [blame] | 560 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | if (num_ops) |
| 562 | snd_printk(KERN_ERR "drivers not released (%d)\n", num_ops); |
| 563 | } |
| 564 | |
| 565 | module_init(alsa_seq_device_init) |
| 566 | module_exit(alsa_seq_device_exit) |
| 567 | |
| 568 | EXPORT_SYMBOL(snd_seq_device_load_drivers); |
| 569 | EXPORT_SYMBOL(snd_seq_device_new); |
| 570 | EXPORT_SYMBOL(snd_seq_device_register_driver); |
| 571 | EXPORT_SYMBOL(snd_seq_device_unregister_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | EXPORT_SYMBOL(snd_seq_autoload_lock); |
| 573 | EXPORT_SYMBOL(snd_seq_autoload_unlock); |