1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * ALSA sequencer device management
6 *----------------------------------------------------------------
8 * This device handler separates the card driver module from sequencer
9 * stuff (sequencer core, synth drivers, etc), so that user can avoid
10 * to spend unnecessary resources e.g. if he needs only listening to
13 * The card (or lowlevel) driver creates a sequencer device entry
14 * via snd_seq_device_new(). This is an entry pointer to communicate
15 * with the sequencer device "driver", which is involved with the
16 * actual part to communicate with the sequencer core.
17 * Each sequencer device entry has an id string and the corresponding
18 * driver with the same id is loaded when required. For example,
19 * lowlevel codes to access emu8000 chip on sbawe card are included in
20 * emu8000-synth module. To activate this module, the hardware
21 * resources like i/o port are passed via snd_seq_device argument.
24 #include <linux/device.h>
25 #include <linux/init.h>
26 #include <linux/module.h>
27 #include <sound/core.h>
28 #include <sound/info.h>
29 #include <sound/seq_device.h>
30 #include <sound/seq_kernel.h>
31 #include <sound/initval.h>
32 #include <linux/kmod.h>
33 #include <linux/slab.h>
34 #include <linux/mutex.h>
37 MODULE_DESCRIPTION("ALSA sequencer device management");
38 MODULE_LICENSE("GPL");
43 static int snd_seq_bus_match(struct device *dev, struct device_driver *drv)
45 struct snd_seq_device *sdev = to_seq_dev(dev);
46 struct snd_seq_driver *sdrv = to_seq_drv(drv);
48 return strcmp(sdrv->id, sdev->id) == 0 &&
49 sdrv->argsize == sdev->argsize;
52 static struct bus_type snd_seq_bus_type = {
54 .match = snd_seq_bus_match,
58 * proc interface -- just for compatibility
60 #ifdef CONFIG_SND_PROC_FS
61 static struct snd_info_entry *info_entry;
63 static int print_dev_info(struct device *dev, void *data)
65 struct snd_seq_device *sdev = to_seq_dev(dev);
66 struct snd_info_buffer *buffer = data;
68 snd_iprintf(buffer, "snd-%s,%s,%d\n", sdev->id,
69 dev->driver ? "loaded" : "empty",
74 static void snd_seq_device_info(struct snd_info_entry *entry,
75 struct snd_info_buffer *buffer)
77 bus_for_each_dev(&snd_seq_bus_type, NULL, buffer, print_dev_info);
82 * load all registered drivers (called from seq_clientmgr.c)
86 /* flag to block auto-loading */
87 static atomic_t snd_seq_in_init = ATOMIC_INIT(1); /* blocked as default */
89 static int request_seq_drv(struct device *dev, void *data)
91 struct snd_seq_device *sdev = to_seq_dev(dev);
94 request_module("snd-%s", sdev->id);
98 static void autoload_drivers(struct work_struct *work)
100 /* avoid reentrance */
101 if (atomic_inc_return(&snd_seq_in_init) == 1)
102 bus_for_each_dev(&snd_seq_bus_type, NULL, NULL,
104 atomic_dec(&snd_seq_in_init);
107 static DECLARE_WORK(autoload_work, autoload_drivers);
109 static void queue_autoload_drivers(void)
111 schedule_work(&autoload_work);
114 void snd_seq_autoload_init(void)
116 atomic_dec(&snd_seq_in_init);
117 #ifdef CONFIG_SND_SEQUENCER_MODULE
118 /* initial autoload only when snd-seq is a module */
119 queue_autoload_drivers();
122 EXPORT_SYMBOL(snd_seq_autoload_init);
124 void snd_seq_autoload_exit(void)
126 atomic_inc(&snd_seq_in_init);
128 EXPORT_SYMBOL(snd_seq_autoload_exit);
130 void snd_seq_device_load_drivers(void)
132 queue_autoload_drivers();
133 flush_work(&autoload_work);
135 EXPORT_SYMBOL(snd_seq_device_load_drivers);
137 static inline void cancel_autoload_drivers(void)
139 cancel_work_sync(&autoload_work);
142 static inline void queue_autoload_drivers(void)
146 static inline void cancel_autoload_drivers(void)
154 static int snd_seq_device_dev_free(struct snd_device *device)
156 struct snd_seq_device *dev = device->device_data;
158 cancel_autoload_drivers();
159 if (dev->private_free)
160 dev->private_free(dev);
161 put_device(&dev->dev);
165 static int snd_seq_device_dev_register(struct snd_device *device)
167 struct snd_seq_device *dev = device->device_data;
170 err = device_add(&dev->dev);
173 if (!dev->dev.driver)
174 queue_autoload_drivers();
178 static int snd_seq_device_dev_disconnect(struct snd_device *device)
180 struct snd_seq_device *dev = device->device_data;
182 device_del(&dev->dev);
186 static void snd_seq_dev_release(struct device *dev)
188 kfree(to_seq_dev(dev));
192 * register a sequencer device
194 * device = device number (if any)
196 * result = return pointer (NULL allowed if unnecessary)
198 int snd_seq_device_new(struct snd_card *card, int device, const char *id,
199 int argsize, struct snd_seq_device **result)
201 struct snd_seq_device *dev;
203 static const struct snd_device_ops dops = {
204 .dev_free = snd_seq_device_dev_free,
205 .dev_register = snd_seq_device_dev_register,
206 .dev_disconnect = snd_seq_device_dev_disconnect,
215 dev = kzalloc(sizeof(*dev) + argsize, GFP_KERNEL);
219 /* set up device info */
221 dev->device = device;
223 dev->argsize = argsize;
225 device_initialize(&dev->dev);
226 dev->dev.parent = &card->card_dev;
227 dev->dev.bus = &snd_seq_bus_type;
228 dev->dev.release = snd_seq_dev_release;
229 dev_set_name(&dev->dev, "%s-%d-%d", dev->id, card->number, device);
231 /* add this device to the list */
232 err = snd_device_new(card, SNDRV_DEV_SEQUENCER, dev, &dops);
234 put_device(&dev->dev);
243 EXPORT_SYMBOL(snd_seq_device_new);
246 * driver registration
248 int __snd_seq_driver_register(struct snd_seq_driver *drv, struct module *mod)
250 if (WARN_ON(!drv->driver.name || !drv->id))
252 drv->driver.bus = &snd_seq_bus_type;
253 drv->driver.owner = mod;
254 return driver_register(&drv->driver);
256 EXPORT_SYMBOL_GPL(__snd_seq_driver_register);
258 void snd_seq_driver_unregister(struct snd_seq_driver *drv)
260 driver_unregister(&drv->driver);
262 EXPORT_SYMBOL_GPL(snd_seq_driver_unregister);
268 static int __init seq_dev_proc_init(void)
270 #ifdef CONFIG_SND_PROC_FS
271 info_entry = snd_info_create_module_entry(THIS_MODULE, "drivers",
273 if (info_entry == NULL)
275 info_entry->content = SNDRV_INFO_CONTENT_TEXT;
276 info_entry->c.text.read = snd_seq_device_info;
277 if (snd_info_register(info_entry) < 0) {
278 snd_info_free_entry(info_entry);
285 static int __init alsa_seq_device_init(void)
289 err = bus_register(&snd_seq_bus_type);
292 err = seq_dev_proc_init();
294 bus_unregister(&snd_seq_bus_type);
298 static void __exit alsa_seq_device_exit(void)
300 #ifdef CONFIG_MODULES
301 cancel_work_sync(&autoload_work);
303 #ifdef CONFIG_SND_PROC_FS
304 snd_info_free_entry(info_entry);
306 bus_unregister(&snd_seq_bus_type);
309 subsys_initcall(alsa_seq_device_init)
310 module_exit(alsa_seq_device_exit)