seq_device.c 7.6 KB

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