seq_device.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * ALSA sequencer device management
  4. * Copyright (c) 1999 by Takashi Iwai <tiwai@suse.de>
  5. *
  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.
  22. */
  23. #include <linux/device.h>
  24. #include <linux/init.h>
  25. #include <linux/module.h>
  26. #include <sound/core.h>
  27. #include <sound/info.h>
  28. #include <sound/seq_device.h>
  29. #include <sound/seq_kernel.h>
  30. #include <sound/initval.h>
  31. #include <linux/kmod.h>
  32. #include <linux/slab.h>
  33. #include <linux/mutex.h>
  34. MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>");
  35. MODULE_DESCRIPTION("ALSA sequencer device management");
  36. MODULE_LICENSE("GPL");
  37. /*
  38. * bus definition
  39. */
  40. static int snd_seq_bus_match(struct device *dev, struct device_driver *drv)
  41. {
  42. struct snd_seq_device *sdev = to_seq_dev(dev);
  43. struct snd_seq_driver *sdrv = to_seq_drv(drv);
  44. return strcmp(sdrv->id, sdev->id) == 0 &&
  45. sdrv->argsize == sdev->argsize;
  46. }
  47. static struct bus_type snd_seq_bus_type = {
  48. .name = "snd_seq",
  49. .match = snd_seq_bus_match,
  50. };
  51. /*
  52. * proc interface -- just for compatibility
  53. */
  54. #ifdef CONFIG_SND_PROC_FS
  55. static struct snd_info_entry *info_entry;
  56. static int print_dev_info(struct device *dev, void *data)
  57. {
  58. struct snd_seq_device *sdev = to_seq_dev(dev);
  59. struct snd_info_buffer *buffer = data;
  60. snd_iprintf(buffer, "snd-%s,%s,%d\n", sdev->id,
  61. dev->driver ? "loaded" : "empty",
  62. dev->driver ? 1 : 0);
  63. return 0;
  64. }
  65. static void snd_seq_device_info(struct snd_info_entry *entry,
  66. struct snd_info_buffer *buffer)
  67. {
  68. bus_for_each_dev(&snd_seq_bus_type, NULL, buffer, print_dev_info);
  69. }
  70. #endif
  71. /*
  72. * load all registered drivers (called from seq_clientmgr.c)
  73. */
  74. #ifdef CONFIG_MODULES
  75. /* flag to block auto-loading */
  76. static atomic_t snd_seq_in_init = ATOMIC_INIT(1); /* blocked as default */
  77. static int request_seq_drv(struct device *dev, void *data)
  78. {
  79. struct snd_seq_device *sdev = to_seq_dev(dev);
  80. if (!dev->driver)
  81. request_module("snd-%s", sdev->id);
  82. return 0;
  83. }
  84. static void autoload_drivers(struct work_struct *work)
  85. {
  86. /* avoid reentrance */
  87. if (atomic_inc_return(&snd_seq_in_init) == 1)
  88. bus_for_each_dev(&snd_seq_bus_type, NULL, NULL,
  89. request_seq_drv);
  90. atomic_dec(&snd_seq_in_init);
  91. }
  92. static DECLARE_WORK(autoload_work, autoload_drivers);
  93. static void queue_autoload_drivers(void)
  94. {
  95. schedule_work(&autoload_work);
  96. }
  97. void snd_seq_autoload_init(void)
  98. {
  99. atomic_dec(&snd_seq_in_init);
  100. #ifdef CONFIG_SND_SEQUENCER_MODULE
  101. /* initial autoload only when snd-seq is a module */
  102. queue_autoload_drivers();
  103. #endif
  104. }
  105. EXPORT_SYMBOL(snd_seq_autoload_init);
  106. void snd_seq_autoload_exit(void)
  107. {
  108. atomic_inc(&snd_seq_in_init);
  109. }
  110. EXPORT_SYMBOL(snd_seq_autoload_exit);
  111. void snd_seq_device_load_drivers(void)
  112. {
  113. queue_autoload_drivers();
  114. flush_work(&autoload_work);
  115. }
  116. EXPORT_SYMBOL(snd_seq_device_load_drivers);
  117. #define cancel_autoload_drivers() cancel_work_sync(&autoload_work)
  118. #else
  119. #define queue_autoload_drivers() /* NOP */
  120. #define cancel_autoload_drivers() /* NOP */
  121. #endif
  122. /*
  123. * device management
  124. */
  125. static int snd_seq_device_dev_free(struct snd_device *device)
  126. {
  127. struct snd_seq_device *dev = device->device_data;
  128. cancel_autoload_drivers();
  129. if (dev->private_free)
  130. dev->private_free(dev);
  131. put_device(&dev->dev);
  132. return 0;
  133. }
  134. static int snd_seq_device_dev_register(struct snd_device *device)
  135. {
  136. struct snd_seq_device *dev = device->device_data;
  137. int err;
  138. err = device_add(&dev->dev);
  139. if (err < 0)
  140. return err;
  141. if (!dev->dev.driver)
  142. queue_autoload_drivers();
  143. return 0;
  144. }
  145. static int snd_seq_device_dev_disconnect(struct snd_device *device)
  146. {
  147. struct snd_seq_device *dev = device->device_data;
  148. device_del(&dev->dev);
  149. return 0;
  150. }
  151. static void snd_seq_dev_release(struct device *dev)
  152. {
  153. kfree(to_seq_dev(dev));
  154. }
  155. /*
  156. * register a sequencer device
  157. * card = card info
  158. * device = device number (if any)
  159. * id = id of driver
  160. * result = return pointer (NULL allowed if unnecessary)
  161. */
  162. int snd_seq_device_new(struct snd_card *card, int device, const char *id,
  163. int argsize, struct snd_seq_device **result)
  164. {
  165. struct snd_seq_device *dev;
  166. int err;
  167. static const struct snd_device_ops dops = {
  168. .dev_free = snd_seq_device_dev_free,
  169. .dev_register = snd_seq_device_dev_register,
  170. .dev_disconnect = snd_seq_device_dev_disconnect,
  171. };
  172. if (result)
  173. *result = NULL;
  174. if (snd_BUG_ON(!id))
  175. return -EINVAL;
  176. dev = kzalloc(sizeof(*dev) + argsize, GFP_KERNEL);
  177. if (!dev)
  178. return -ENOMEM;
  179. /* set up device info */
  180. dev->card = card;
  181. dev->device = device;
  182. dev->id = id;
  183. dev->argsize = argsize;
  184. device_initialize(&dev->dev);
  185. dev->dev.parent = &card->card_dev;
  186. dev->dev.bus = &snd_seq_bus_type;
  187. dev->dev.release = snd_seq_dev_release;
  188. dev_set_name(&dev->dev, "%s-%d-%d", dev->id, card->number, device);
  189. /* add this device to the list */
  190. err = snd_device_new(card, SNDRV_DEV_SEQUENCER, dev, &dops);
  191. if (err < 0) {
  192. put_device(&dev->dev);
  193. return err;
  194. }
  195. if (result)
  196. *result = dev;
  197. return 0;
  198. }
  199. EXPORT_SYMBOL(snd_seq_device_new);
  200. /*
  201. * driver registration
  202. */
  203. int __snd_seq_driver_register(struct snd_seq_driver *drv, struct module *mod)
  204. {
  205. if (WARN_ON(!drv->driver.name || !drv->id))
  206. return -EINVAL;
  207. drv->driver.bus = &snd_seq_bus_type;
  208. drv->driver.owner = mod;
  209. return driver_register(&drv->driver);
  210. }
  211. EXPORT_SYMBOL_GPL(__snd_seq_driver_register);
  212. void snd_seq_driver_unregister(struct snd_seq_driver *drv)
  213. {
  214. driver_unregister(&drv->driver);
  215. }
  216. EXPORT_SYMBOL_GPL(snd_seq_driver_unregister);
  217. /*
  218. * module part
  219. */
  220. static int __init seq_dev_proc_init(void)
  221. {
  222. #ifdef CONFIG_SND_PROC_FS
  223. info_entry = snd_info_create_module_entry(THIS_MODULE, "drivers",
  224. snd_seq_root);
  225. if (info_entry == NULL)
  226. return -ENOMEM;
  227. info_entry->content = SNDRV_INFO_CONTENT_TEXT;
  228. info_entry->c.text.read = snd_seq_device_info;
  229. if (snd_info_register(info_entry) < 0) {
  230. snd_info_free_entry(info_entry);
  231. return -ENOMEM;
  232. }
  233. #endif
  234. return 0;
  235. }
  236. static int __init alsa_seq_device_init(void)
  237. {
  238. int err;
  239. err = bus_register(&snd_seq_bus_type);
  240. if (err < 0)
  241. return err;
  242. err = seq_dev_proc_init();
  243. if (err < 0)
  244. bus_unregister(&snd_seq_bus_type);
  245. return err;
  246. }
  247. static void __exit alsa_seq_device_exit(void)
  248. {
  249. #ifdef CONFIG_MODULES
  250. cancel_work_sync(&autoload_work);
  251. #endif
  252. #ifdef CONFIG_SND_PROC_FS
  253. snd_info_free_entry(info_entry);
  254. #endif
  255. bus_unregister(&snd_seq_bus_type);
  256. }
  257. subsys_initcall(alsa_seq_device_init)
  258. module_exit(alsa_seq_device_exit)