blob: 075a66c0cc6a4c36b72b33d21110aecfac438b87 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/init.h>
Paul Gortmakerda155d52011-07-15 12:38:28 -040040#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#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 Molnar1a60d4c2006-01-16 16:29:08 +010048#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>");
51MODULE_DESCRIPTION("ALSA sequencer device management");
52MODULE_LICENSE("GPL");
53
Linus Torvalds1da177e2005-04-16 15:20:36 -070054/* 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 Iwai68ab6102014-10-15 14:06:25 +020059#define DRIVER_REQUESTING (1<<3)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61struct 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 Iwaic7e0b5b2005-11-17 14:04:02 +010068 struct snd_seq_dev_ops ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Adam Buchbinderd93cf062012-09-19 21:47:58 -040070 /* registered devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 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 Molnar1a60d4c2006-01-16 16:29:08 +010074 struct mutex reg_mutex;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
76 struct list_head list; /* next driver */
77};
78
79
80static LIST_HEAD(opslist);
81static int num_ops;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +010082static DEFINE_MUTEX(ops_mutex);
Takashi Iwai04f141a2005-12-01 10:43:51 +010083#ifdef CONFIG_PROC_FS
Takashi Iwai6581f4e2006-05-17 17:14:51 +020084static struct snd_info_entry *info_entry;
Takashi Iwai04f141a2005-12-01 10:43:51 +010085#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87/*
88 * prototypes
89 */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +010090static int snd_seq_device_free(struct snd_seq_device *dev);
91static int snd_seq_device_dev_free(struct snd_device *device);
92static int snd_seq_device_dev_register(struct snd_device *device);
93static int snd_seq_device_dev_disconnect(struct snd_device *device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +010095static int init_device(struct snd_seq_device *dev, struct ops_list *ops);
96static int free_device(struct snd_seq_device *dev, struct ops_list *ops);
97static struct ops_list *find_driver(char *id, int create_if_empty);
98static struct ops_list *create_driver(char *id);
99static void unlock_driver(struct ops_list *ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100static void remove_drivers(void);
101
102/*
103 * show all drivers and their status
104 */
105
Takashi Iwai04f141a2005-12-01 10:43:51 +0100106#ifdef CONFIG_PROC_FS
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100107static void snd_seq_device_info(struct snd_info_entry *entry,
108 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109{
Johannes Berg9244b2c2006-10-05 16:02:22 +0200110 struct ops_list *ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100112 mutex_lock(&ops_mutex);
Johannes Berg9244b2c2006-10-05 16:02:22 +0200113 list_for_each_entry(ops, &opslist, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 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 Molnar1a60d4c2006-01-16 16:29:08 +0100121 mutex_unlock(&ops_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122}
Takashi Iwai04f141a2005-12-01 10:43:51 +0100123#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
125/*
126 * load all registered drivers (called from seq_clientmgr.c)
127 */
128
Johannes Bergee2da992008-07-09 10:28:41 +0200129#ifdef CONFIG_MODULES
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130/* avoid auto-loading during module_init() */
Takashi Iwai68ab6102014-10-15 14:06:25 +0200131static atomic_t snd_seq_in_init = ATOMIC_INIT(1); /* blocked as default */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132void snd_seq_autoload_lock(void)
133{
Takashi Iwai54841a02014-10-15 14:00:16 +0200134 atomic_inc(&snd_seq_in_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135}
Takashi Iwaib6a42672015-02-11 22:37:16 +0100136EXPORT_SYMBOL(snd_seq_autoload_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
138void snd_seq_autoload_unlock(void)
139{
Takashi Iwai54841a02014-10-15 14:00:16 +0200140 atomic_dec(&snd_seq_in_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141}
Takashi Iwaib6a42672015-02-11 22:37:16 +0100142EXPORT_SYMBOL(snd_seq_autoload_unlock);
Takashi Iwai68ab6102014-10-15 14:06:25 +0200143
144static 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
167static void call_autoload(struct work_struct *work)
168{
169 autoload_drivers();
170}
171
172static DECLARE_WORK(autoload_work, call_autoload);
173
174static 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
182static 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
192void 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 Iwaib6a42672015-02-11 22:37:16 +0100200EXPORT_SYMBOL(snd_seq_autoload_init);
Takashi Iwaib6a42672015-02-11 22:37:16 +0100201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202void snd_seq_device_load_drivers(void)
203{
Takashi Iwai68ab6102014-10-15 14:06:25 +0200204 queue_autoload_drivers();
205 flush_work(&autoload_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206}
Takashi Iwaib6a42672015-02-11 22:37:16 +0100207EXPORT_SYMBOL(snd_seq_device_load_drivers);
Takashi Iwai72496ed2015-02-11 22:39:51 +0100208#else
209#define try_autoload(ops) /* NOP */
210#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
212/*
213 * register a sequencer device
Takashi Iwaif2f93072014-02-04 18:21:03 +0100214 * card = card info
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 * device = device number (if any)
216 * id = id of driver
217 * result = return pointer (NULL allowed if unnecessary)
218 */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100219int snd_seq_device_new(struct snd_card *card, int device, char *id, int argsize,
220 struct snd_seq_device **result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221{
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100222 struct snd_seq_device *dev;
223 struct ops_list *ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 int err;
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100225 static struct snd_device_ops dops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 .dev_free = snd_seq_device_dev_free,
227 .dev_register = snd_seq_device_dev_register,
228 .dev_disconnect = snd_seq_device_dev_disconnect,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 };
230
231 if (result)
232 *result = NULL;
233
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200234 if (snd_BUG_ON(!id))
235 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
237 ops = find_driver(id, 1);
238 if (ops == NULL)
239 return -ENOMEM;
240
Takashi Iwaiecca82b2005-09-09 14:20:49 +0200241 dev = kzalloc(sizeof(*dev)*2 + argsize, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 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 Molnar1a60d4c2006-01-16 16:29:08 +0100255 mutex_lock(&ops->reg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 list_add_tail(&dev->list, &ops->dev_list);
257 ops->num_devices++;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100258 mutex_unlock(&ops->reg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 if ((err = snd_device_new(card, SNDRV_DEV_SEQUENCER, dev, &dops)) < 0) {
261 snd_seq_device_free(dev);
262 return err;
263 }
264
Takashi Iwai68ab6102014-10-15 14:06:25 +0200265 try_autoload(ops);
266 unlock_driver(ops);
267
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 if (result)
269 *result = dev;
270
271 return 0;
272}
Takashi Iwaib6a42672015-02-11 22:37:16 +0100273EXPORT_SYMBOL(snd_seq_device_new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
275/*
276 * free the existing device
277 */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100278static int snd_seq_device_free(struct snd_seq_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279{
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100280 struct ops_list *ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200282 if (snd_BUG_ON(!dev))
283 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
285 ops = find_driver(dev->id, 0);
286 if (ops == NULL)
287 return -ENXIO;
288
289 /* remove the device from the list */
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100290 mutex_lock(&ops->reg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 list_del(&dev->list);
292 ops->num_devices--;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100293 mutex_unlock(&ops->reg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
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 Iwaic7e0b5b2005-11-17 14:04:02 +0100305static int snd_seq_device_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306{
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100307 struct snd_seq_device *dev = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 return snd_seq_device_free(dev);
309}
310
311/*
312 * register the device
313 */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100314static int snd_seq_device_dev_register(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315{
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100316 struct snd_seq_device *dev = device->device_data;
317 struct ops_list *ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
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 Iwaib6a42672015-02-11 22:37:16 +0100332EXPORT_SYMBOL(snd_seq_device_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
334/*
335 * disconnect the device
336 */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100337static int snd_seq_device_dev_disconnect(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338{
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100339 struct snd_seq_device *dev = device->device_data;
340 struct ops_list *ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
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 Iwaib6a42672015-02-11 22:37:16 +0100351EXPORT_SYMBOL(snd_seq_device_unregister_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
353/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 * register device driver
355 * id = driver id
356 * entry = driver operators - duplicated to each instance
357 */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100358int snd_seq_device_register_driver(char *id, struct snd_seq_dev_ops *entry,
359 int argsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360{
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100361 struct ops_list *ops;
Johannes Berg9244b2c2006-10-05 16:02:22 +0200362 struct snd_seq_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
364 if (id == NULL || entry == NULL ||
365 entry->init_device == NULL || entry->free_device == NULL)
366 return -EINVAL;
367
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 ops = find_driver(id, 1);
Takashi Iwaid5129f32014-10-15 14:09:42 +0200369 if (ops == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 if (ops->driver & DRIVER_LOADED) {
Takashi Iwai04cc79a2014-02-04 18:24:34 +0100372 pr_warn("ALSA: seq: driver_register: driver '%s' already exists\n", id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 unlock_driver(ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 return -EBUSY;
375 }
376
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100377 mutex_lock(&ops->reg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 /* copy driver operators */
379 ops->ops = *entry;
380 ops->driver |= DRIVER_LOADED;
381 ops->argsize = argsize;
382
383 /* initialize existing devices if necessary */
Johannes Berg9244b2c2006-10-05 16:02:22 +0200384 list_for_each_entry(dev, &ops->dev_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 init_device(dev, ops);
386 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100387 mutex_unlock(&ops->reg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
389 unlock_driver(ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
391 return 0;
392}
393
394
395/*
396 * create driver record
397 */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100398static struct ops_list * create_driver(char *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399{
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100400 struct ops_list *ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
Panagiotis Issaris59feddb2006-07-25 15:28:03 +0200402 ops = kzalloc(sizeof(*ops), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 if (ops == NULL)
404 return ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
406 /* set up driver entry */
407 strlcpy(ops->id, id, sizeof(ops->id));
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100408 mutex_init(&ops->reg_mutex);
Arjan van de Ven933a2ef2006-07-03 00:25:22 -0700409 /*
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 Torvalds1da177e2005-04-16 15:20:36 -0700415 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 Molnar1a60d4c2006-01-16 16:29:08 +0100421 mutex_lock(&ops_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 list_add_tail(&ops->list, &opslist);
423 num_ops++;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100424 mutex_unlock(&ops_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
426 return ops;
427}
428
429
430/*
431 * unregister the specified driver
432 */
433int snd_seq_device_unregister_driver(char *id)
434{
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100435 struct ops_list *ops;
Johannes Berg9244b2c2006-10-05 16:02:22 +0200436 struct snd_seq_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
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 Iwai04cc79a2014-02-04 18:24:34 +0100443 pr_err("ALSA: seq: driver_unregister: cannot unload driver '%s': status=%x\n",
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100444 id, ops->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 unlock_driver(ops);
446 return -EBUSY;
447 }
448
449 /* close and release all devices associated with this driver */
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100450 mutex_lock(&ops->reg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 ops->driver |= DRIVER_LOCKED; /* do not remove this driver recursively */
Johannes Berg9244b2c2006-10-05 16:02:22 +0200452 list_for_each_entry(dev, &ops->dev_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 free_device(dev, ops);
454 }
455
456 ops->driver = 0;
457 if (ops->num_init_devices > 0)
Takashi Iwai04cc79a2014-02-04 18:24:34 +0100458 pr_err("ALSA: seq: free_driver: init_devices > 0!! (%d)\n",
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100459 ops->num_init_devices);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100460 mutex_unlock(&ops->reg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
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 */
474static void remove_drivers(void)
475{
476 struct list_head *head;
477
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100478 mutex_lock(&ops_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 head = opslist.next;
480 while (head != &opslist) {
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100481 struct ops_list *ops = list_entry(head, struct ops_list, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 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 Molnar1a60d4c2006-01-16 16:29:08 +0100491 mutex_unlock(&ops_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492}
493
494/*
495 * initialize the device - call init_device operator
496 */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100497static int init_device(struct snd_seq_device *dev, struct ops_list *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498{
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 Iwai04cc79a2014-02-04 18:24:34 +0100504 pr_err("ALSA: seq: incompatible device '%s' for plug-in '%s' (%d %d)\n",
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100505 dev->name, ops->id, ops->argsize, dev->argsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 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 Iwai04cc79a2014-02-04 18:24:34 +0100512 pr_err("ALSA: seq: init_device failed: %s: %s\n",
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100513 dev->name, dev->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 }
515
516 return 0;
517}
518
519/*
520 * release the device - call free_device operator
521 */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100522static int free_device(struct snd_seq_device *dev, struct ops_list *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523{
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 Iwai04cc79a2014-02-04 18:24:34 +0100531 pr_err("ALSA: seq: incompatible device '%s' for plug-in '%s' (%d %d)\n",
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100532 dev->name, ops->id, ops->argsize, dev->argsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 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 Iwai04cc79a2014-02-04 18:24:34 +0100540 pr_err("ALSA: seq: free_device failed: %s: %s\n",
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100541 dev->name, dev->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 }
543
544 return 0;
545}
546
547/*
548 * find the matching driver with given id
549 */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100550static struct ops_list * find_driver(char *id, int create_if_empty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551{
Johannes Berg9244b2c2006-10-05 16:02:22 +0200552 struct ops_list *ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100554 mutex_lock(&ops_mutex);
Johannes Berg9244b2c2006-10-05 16:02:22 +0200555 list_for_each_entry(ops, &opslist, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 if (strcmp(ops->id, id) == 0) {
557 ops->used++;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100558 mutex_unlock(&ops_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 return ops;
560 }
561 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100562 mutex_unlock(&ops_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 if (create_if_empty)
564 return create_driver(id);
565 return NULL;
566}
567
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100568static void unlock_driver(struct ops_list *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569{
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100570 mutex_lock(&ops_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 ops->used--;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100572 mutex_unlock(&ops_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573}
574
575
576/*
577 * module part
578 */
579
580static int __init alsa_seq_device_init(void)
581{
Takashi Iwai04f141a2005-12-01 10:43:51 +0100582#ifdef CONFIG_PROC_FS
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100583 info_entry = snd_info_create_module_entry(THIS_MODULE, "drivers",
584 snd_seq_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 if (info_entry == NULL)
586 return -ENOMEM;
587 info_entry->content = SNDRV_INFO_CONTENT_TEXT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 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 Iwai04f141a2005-12-01 10:43:51 +0100593#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 return 0;
595}
596
597static void __exit alsa_seq_device_exit(void)
598{
Takashi Iwai68ab6102014-10-15 14:06:25 +0200599#ifdef CONFIG_MODULES
600 cancel_work_sync(&autoload_work);
601#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 remove_drivers();
Takashi Iwai04f141a2005-12-01 10:43:51 +0100603#ifdef CONFIG_PROC_FS
Takashi Iwai746d4a02006-06-23 14:37:59 +0200604 snd_info_free_entry(info_entry);
Takashi Iwai04f141a2005-12-01 10:43:51 +0100605#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 if (num_ops)
Takashi Iwai04cc79a2014-02-04 18:24:34 +0100607 pr_err("ALSA: seq: drivers not released (%d)\n", num_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608}
609
610module_init(alsa_seq_device_init)
611module_exit(alsa_seq_device_exit)