seq_device.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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, const struct device_driver *drv)
  41. {
  42. struct snd_seq_device *sdev = to_seq_dev(dev);
  43. const 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 const 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. static inline void cancel_autoload_drivers(void)
  118. {
  119. cancel_work_sync(&autoload_work);
  120. }
  121. #else
  122. static inline void queue_autoload_drivers(void)
  123. {
  124. }
  125. static inline void cancel_autoload_drivers(void)
  126. {
  127. }
  128. #endif
  129. /*
  130. * device management
  131. */
  132. static int snd_seq_device_dev_free(struct snd_device *device)
  133. {
  134. struct snd_seq_device *dev = device->device_data;
  135. cancel_autoload_drivers();
  136. if (dev->private_free)
  137. dev->private_free(dev);
  138. put_device(&dev->dev);
  139. return 0;
  140. }
  141. static int snd_seq_device_dev_register(struct snd_device *device)
  142. {
  143. struct snd_seq_device *dev = device->device_data;
  144. int err;
  145. err = device_add(&dev->dev);
  146. if (err < 0)
  147. return err;
  148. if (!dev->dev.driver)
  149. queue_autoload_drivers();
  150. return 0;
  151. }
  152. static int snd_seq_device_dev_disconnect(struct snd_device *device)
  153. {
  154. struct snd_seq_device *dev = device->device_data;
  155. device_del(&dev->dev);
  156. return 0;
  157. }
  158. static void snd_seq_dev_release(struct device *dev)
  159. {
  160. kfree(to_seq_dev(dev));
  161. }
  162. /*
  163. * register a sequencer device
  164. * card = card info
  165. * device = device number (if any)
  166. * id = id of driver
  167. * result = return pointer (NULL allowed if unnecessary)
  168. */
  169. int snd_seq_device_new(struct snd_card *card, int device, const char *id,
  170. int argsize, struct snd_seq_device **result)
  171. {
  172. struct snd_seq_device *dev;
  173. int err;
  174. static const struct snd_device_ops dops = {
  175. .dev_free = snd_seq_device_dev_free,
  176. .dev_register = snd_seq_device_dev_register,
  177. .dev_disconnect = snd_seq_device_dev_disconnect,
  178. };
  179. if (result)
  180. *result = NULL;
  181. if (snd_BUG_ON(!id))
  182. return -EINVAL;
  183. dev = kzalloc(sizeof(*dev) + argsize, GFP_KERNEL);
  184. if (!dev)
  185. return -ENOMEM;
  186. /* set up device info */
  187. dev->card = card;
  188. dev->device = device;
  189. dev->id = id;
  190. dev->argsize = argsize;
  191. device_initialize(&dev->dev);
  192. dev->dev.parent = &card->card_dev;
  193. dev->dev.bus = &snd_seq_bus_type;
  194. dev->dev.release = snd_seq_dev_release;
  195. dev_set_name(&dev->dev, "%s-%d-%d", dev->id, card->number, device);
  196. /* add this device to the list */
  197. err = snd_device_new(card, SNDRV_DEV_SEQUENCER, dev, &dops);
  198. if (err < 0) {
  199. put_device(&dev->dev);
  200. return err;
  201. }
  202. if (result)
  203. *result = dev;
  204. return 0;
  205. }
  206. EXPORT_SYMBOL(snd_seq_device_new);
  207. /*
  208. * driver registration
  209. */
  210. int __snd_seq_driver_register(struct snd_seq_driver *drv, struct module *mod)
  211. {
  212. if (WARN_ON(!drv->driver.name || !drv->id))
  213. return -EINVAL;
  214. drv->driver.bus = &snd_seq_bus_type;
  215. drv->driver.owner = mod;
  216. return driver_register(&drv->driver);
  217. }
  218. EXPORT_SYMBOL_GPL(__snd_seq_driver_register);
  219. void snd_seq_driver_unregister(struct snd_seq_driver *drv)
  220. {
  221. driver_unregister(&drv->driver);
  222. }
  223. EXPORT_SYMBOL_GPL(snd_seq_driver_unregister);
  224. /*
  225. * module part
  226. */
  227. static int __init seq_dev_proc_init(void)
  228. {
  229. #ifdef CONFIG_SND_PROC_FS
  230. info_entry = snd_info_create_module_entry(THIS_MODULE, "drivers",
  231. snd_seq_root);
  232. if (info_entry == NULL)
  233. return -ENOMEM;
  234. info_entry->content = SNDRV_INFO_CONTENT_TEXT;
  235. info_entry->c.text.read = snd_seq_device_info;
  236. if (snd_info_register(info_entry) < 0) {
  237. snd_info_free_entry(info_entry);
  238. return -ENOMEM;
  239. }
  240. #endif
  241. return 0;
  242. }
  243. static int __init alsa_seq_device_init(void)
  244. {
  245. int err;
  246. err = bus_register(&snd_seq_bus_type);
  247. if (err < 0)
  248. return err;
  249. err = seq_dev_proc_init();
  250. if (err < 0)
  251. bus_unregister(&snd_seq_bus_type);
  252. return err;
  253. }
  254. static void __exit alsa_seq_device_exit(void)
  255. {
  256. #ifdef CONFIG_MODULES
  257. cancel_work_sync(&autoload_work);
  258. #endif
  259. #ifdef CONFIG_SND_PROC_FS
  260. snd_info_free_entry(info_entry);
  261. #endif
  262. bus_unregister(&snd_seq_bus_type);
  263. }
  264. subsys_initcall(alsa_seq_device_init)
  265. module_exit(alsa_seq_device_exit)