ALSA: hda/realtek: Enable 4-speaker output for Dell Precision 5560 laptop
[linux-block.git] / sound / core / seq_device.c
CommitLineData
1a59d1b8 1// SPDX-License-Identifier: GPL-2.0-or-later
1da177e4
LT
2/*
3 * ALSA sequencer device management
4 * Copyright (c) 1999 by Takashi Iwai <tiwai@suse.de>
5 *
1da177e4
LT
6 *----------------------------------------------------------------
7 *
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
11 * MP3s.
12 *
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.
1da177e4
LT
22 */
23
7c37ae5c 24#include <linux/device.h>
1da177e4 25#include <linux/init.h>
da155d5b 26#include <linux/module.h>
1da177e4
LT
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>
1a60d4c5 34#include <linux/mutex.h>
1da177e4
LT
35
36MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>");
37MODULE_DESCRIPTION("ALSA sequencer device management");
38MODULE_LICENSE("GPL");
39
7c37ae5c
TI
40/*
41 * bus definition
42 */
43static int snd_seq_bus_match(struct device *dev, struct device_driver *drv)
44{
45 struct snd_seq_device *sdev = to_seq_dev(dev);
46 struct snd_seq_driver *sdrv = to_seq_drv(drv);
1da177e4 47
7c37ae5c
TI
48 return strcmp(sdrv->id, sdev->id) == 0 &&
49 sdrv->argsize == sdev->argsize;
50}
1da177e4 51
7c37ae5c
TI
52static struct bus_type snd_seq_bus_type = {
53 .name = "snd_seq",
54 .match = snd_seq_bus_match,
1da177e4
LT
55};
56
7c37ae5c
TI
57/*
58 * proc interface -- just for compatibility
59 */
cd6a6503 60#ifdef CONFIG_SND_PROC_FS
6581f4e7 61static struct snd_info_entry *info_entry;
1da177e4 62
7c37ae5c
TI
63static int print_dev_info(struct device *dev, void *data)
64{
65 struct snd_seq_device *sdev = to_seq_dev(dev);
66 struct snd_info_buffer *buffer = data;
1da177e4 67
7c37ae5c
TI
68 snd_iprintf(buffer, "snd-%s,%s,%d\n", sdev->id,
69 dev->driver ? "loaded" : "empty",
70 dev->driver ? 1 : 0);
71 return 0;
72}
1da177e4 73
c7e0b5bf
TI
74static void snd_seq_device_info(struct snd_info_entry *entry,
75 struct snd_info_buffer *buffer)
1da177e4 76{
7c37ae5c 77 bus_for_each_dev(&snd_seq_bus_type, NULL, buffer, print_dev_info);
1da177e4 78}
04f141a8 79#endif
7c37ae5c 80
1da177e4
LT
81/*
82 * load all registered drivers (called from seq_clientmgr.c)
83 */
84
ee2da997 85#ifdef CONFIG_MODULES
54a721ab 86/* flag to block auto-loading */
68ab6108 87static atomic_t snd_seq_in_init = ATOMIC_INIT(1); /* blocked as default */
1da177e4 88
7c37ae5c 89static int request_seq_drv(struct device *dev, void *data)
1da177e4 90{
7c37ae5c 91 struct snd_seq_device *sdev = to_seq_dev(dev);
1da177e4 92
7c37ae5c
TI
93 if (!dev->driver)
94 request_module("snd-%s", sdev->id);
95 return 0;
68ab6108 96}
1da177e4 97
7c37ae5c 98static void autoload_drivers(struct work_struct *work)
68ab6108 99{
7c37ae5c
TI
100 /* avoid reentrance */
101 if (atomic_inc_return(&snd_seq_in_init) == 1)
102 bus_for_each_dev(&snd_seq_bus_type, NULL, NULL,
103 request_seq_drv);
104 atomic_dec(&snd_seq_in_init);
68ab6108
TI
105}
106
7c37ae5c
TI
107static DECLARE_WORK(autoload_work, autoload_drivers);
108
68ab6108
TI
109static void queue_autoload_drivers(void)
110{
7c37ae5c 111 schedule_work(&autoload_work);
68ab6108
TI
112}
113
114void snd_seq_autoload_init(void)
115{
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();
120#endif
121}
b6a42670 122EXPORT_SYMBOL(snd_seq_autoload_init);
b6a42670 123
54a721ab
TI
124void snd_seq_autoload_exit(void)
125{
126 atomic_inc(&snd_seq_in_init);
127}
128EXPORT_SYMBOL(snd_seq_autoload_exit);
129
68ab6108
TI
130void snd_seq_device_load_drivers(void)
131{
68ab6108
TI
132 queue_autoload_drivers();
133 flush_work(&autoload_work);
1da177e4 134}
b6a42670 135EXPORT_SYMBOL(snd_seq_device_load_drivers);
940ba1f5
AB
136
137static inline void cancel_autoload_drivers(void)
138{
139 cancel_work_sync(&autoload_work);
140}
72496edc 141#else
940ba1f5
AB
142static inline void queue_autoload_drivers(void)
143{
144}
145
146static inline void cancel_autoload_drivers(void)
147{
148}
72496edc 149#endif
1da177e4 150
7c37ae5c
TI
151/*
152 * device management
153 */
154static int snd_seq_device_dev_free(struct snd_device *device)
155{
156 struct snd_seq_device *dev = device->device_data;
157
fc27fe7e 158 cancel_autoload_drivers();
7c37ae5c
TI
159 put_device(&dev->dev);
160 return 0;
161}
162
163static int snd_seq_device_dev_register(struct snd_device *device)
164{
165 struct snd_seq_device *dev = device->device_data;
166 int err;
167
168 err = device_add(&dev->dev);
169 if (err < 0)
170 return err;
171 if (!dev->dev.driver)
172 queue_autoload_drivers();
173 return 0;
174}
175
176static int snd_seq_device_dev_disconnect(struct snd_device *device)
177{
178 struct snd_seq_device *dev = device->device_data;
179
180 device_del(&dev->dev);
181 return 0;
182}
183
184static void snd_seq_dev_release(struct device *dev)
185{
186 struct snd_seq_device *sdev = to_seq_dev(dev);
187
188 if (sdev->private_free)
189 sdev->private_free(sdev);
190 kfree(sdev);
191}
192
1da177e4
LT
193/*
194 * register a sequencer device
f2f9307a 195 * card = card info
1da177e4
LT
196 * device = device number (if any)
197 * id = id of driver
198 * result = return pointer (NULL allowed if unnecessary)
199 */
af03c243
TI
200int snd_seq_device_new(struct snd_card *card, int device, const char *id,
201 int argsize, struct snd_seq_device **result)
1da177e4 202{
c7e0b5bf 203 struct snd_seq_device *dev;
1da177e4 204 int err;
f15ee210 205 static const struct snd_device_ops dops = {
1da177e4
LT
206 .dev_free = snd_seq_device_dev_free,
207 .dev_register = snd_seq_device_dev_register,
208 .dev_disconnect = snd_seq_device_dev_disconnect,
1da177e4
LT
209 };
210
211 if (result)
212 *result = NULL;
213
7eaa943c
TI
214 if (snd_BUG_ON(!id))
215 return -EINVAL;
1da177e4 216
7c37ae5c
TI
217 dev = kzalloc(sizeof(*dev) + argsize, GFP_KERNEL);
218 if (!dev)
1da177e4 219 return -ENOMEM;
1da177e4
LT
220
221 /* set up device info */
222 dev->card = card;
223 dev->device = device;
af03c243 224 dev->id = id;
1da177e4 225 dev->argsize = argsize;
1da177e4 226
7c37ae5c
TI
227 device_initialize(&dev->dev);
228 dev->dev.parent = &card->card_dev;
229 dev->dev.bus = &snd_seq_bus_type;
230 dev->dev.release = snd_seq_dev_release;
231 dev_set_name(&dev->dev, "%s-%d-%d", dev->id, card->number, device);
1da177e4 232
7c37ae5c
TI
233 /* add this device to the list */
234 err = snd_device_new(card, SNDRV_DEV_SEQUENCER, dev, &dops);
235 if (err < 0) {
236 put_device(&dev->dev);
1da177e4
LT
237 return err;
238 }
239
240 if (result)
241 *result = dev;
242
243 return 0;
244}
b6a42670 245EXPORT_SYMBOL(snd_seq_device_new);
1da177e4
LT
246
247/*
05662205 248 * driver registration
1da177e4 249 */
05662205 250int __snd_seq_driver_register(struct snd_seq_driver *drv, struct module *mod)
1da177e4 251{
05662205 252 if (WARN_ON(!drv->driver.name || !drv->id))
1da177e4 253 return -EINVAL;
05662205
TI
254 drv->driver.bus = &snd_seq_bus_type;
255 drv->driver.owner = mod;
256 return driver_register(&drv->driver);
7c37ae5c 257}
05662205 258EXPORT_SYMBOL_GPL(__snd_seq_driver_register);
1da177e4 259
05662205 260void snd_seq_driver_unregister(struct snd_seq_driver *drv)
1da177e4 261{
05662205 262 driver_unregister(&drv->driver);
1da177e4 263}
05662205 264EXPORT_SYMBOL_GPL(snd_seq_driver_unregister);
1da177e4
LT
265
266/*
267 * module part
268 */
269
7c37ae5c 270static int __init seq_dev_proc_init(void)
1da177e4 271{
b816db9d 272#ifdef CONFIG_SND_PROC_FS
c7e0b5bf
TI
273 info_entry = snd_info_create_module_entry(THIS_MODULE, "drivers",
274 snd_seq_root);
1da177e4
LT
275 if (info_entry == NULL)
276 return -ENOMEM;
277 info_entry->content = SNDRV_INFO_CONTENT_TEXT;
1da177e4
LT
278 info_entry->c.text.read = snd_seq_device_info;
279 if (snd_info_register(info_entry) < 0) {
280 snd_info_free_entry(info_entry);
281 return -ENOMEM;
282 }
04f141a8 283#endif
1da177e4
LT
284 return 0;
285}
286
7c37ae5c
TI
287static int __init alsa_seq_device_init(void)
288{
289 int err;
290
291 err = bus_register(&snd_seq_bus_type);
292 if (err < 0)
293 return err;
294 err = seq_dev_proc_init();
295 if (err < 0)
296 bus_unregister(&snd_seq_bus_type);
297 return err;
298}
299
1da177e4
LT
300static void __exit alsa_seq_device_exit(void)
301{
68ab6108
TI
302#ifdef CONFIG_MODULES
303 cancel_work_sync(&autoload_work);
304#endif
b816db9d 305#ifdef CONFIG_SND_PROC_FS
746d4a02 306 snd_info_free_entry(info_entry);
04f141a8 307#endif
7c37ae5c 308 bus_unregister(&snd_seq_bus_type);
1da177e4
LT
309}
310
4945f1fd 311subsys_initcall(alsa_seq_device_init)
1da177e4 312module_exit(alsa_seq_device_exit)