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