seq_device.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /*
  2. * ALSA sequencer device management
  3. * Copyright (c) 1999 by Takashi Iwai <tiwai@suse.de>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. *
  19. *
  20. *----------------------------------------------------------------
  21. *
  22. * This device handler separates the card driver module from sequencer
  23. * stuff (sequencer core, synth drivers, etc), so that user can avoid
  24. * to spend unnecessary resources e.g. if he needs only listening to
  25. * MP3s.
  26. *
  27. * The card (or lowlevel) driver creates a sequencer device entry
  28. * via snd_seq_device_new(). This is an entry pointer to communicate
  29. * with the sequencer device "driver", which is involved with the
  30. * actual part to communicate with the sequencer core.
  31. * Each sequencer device entry has an id string and the corresponding
  32. * driver with the same id is loaded when required. For example,
  33. * lowlevel codes to access emu8000 chip on sbawe card are included in
  34. * emu8000-synth module. To activate this module, the hardware
  35. * resources like i/o port are passed via snd_seq_device argument.
  36. *
  37. */
  38. #include <linux/device.h>
  39. #include <linux/init.h>
  40. #include <linux/module.h>
  41. #include <sound/core.h>
  42. #include <sound/info.h>
  43. #include <sound/seq_device.h>
  44. #include <sound/seq_kernel.h>
  45. #include <sound/initval.h>
  46. #include <linux/kmod.h>
  47. #include <linux/slab.h>
  48. #include <linux/mutex.h>
  49. MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>");
  50. MODULE_DESCRIPTION("ALSA sequencer device management");
  51. MODULE_LICENSE("GPL");
  52. /*
  53. * bus definition
  54. */
  55. static int snd_seq_bus_match(struct device *dev, struct device_driver *drv)
  56. {
  57. struct snd_seq_device *sdev = to_seq_dev(dev);
  58. struct snd_seq_driver *sdrv = to_seq_drv(drv);
  59. return strcmp(sdrv->id, sdev->id) == 0 &&
  60. sdrv->argsize == sdev->argsize;
  61. }
  62. static struct bus_type snd_seq_bus_type = {
  63. .name = "snd_seq",
  64. .match = snd_seq_bus_match,
  65. };
  66. /*
  67. * proc interface -- just for compatibility
  68. */
  69. #ifdef CONFIG_SND_PROC_FS
  70. static struct snd_info_entry *info_entry;
  71. static int print_dev_info(struct device *dev, void *data)
  72. {
  73. struct snd_seq_device *sdev = to_seq_dev(dev);
  74. struct snd_info_buffer *buffer = data;
  75. snd_iprintf(buffer, "snd-%s,%s,%d\n", sdev->id,
  76. dev->driver ? "loaded" : "empty",
  77. dev->driver ? 1 : 0);
  78. return 0;
  79. }
  80. static void snd_seq_device_info(struct snd_info_entry *entry,
  81. struct snd_info_buffer *buffer)
  82. {
  83. bus_for_each_dev(&snd_seq_bus_type, NULL, buffer, print_dev_info);
  84. }
  85. #endif
  86. /*
  87. * load all registered drivers (called from seq_clientmgr.c)
  88. */
  89. #ifdef CONFIG_MODULES
  90. /* flag to block auto-loading */
  91. static atomic_t snd_seq_in_init = ATOMIC_INIT(1); /* blocked as default */
  92. static int request_seq_drv(struct device *dev, void *data)
  93. {
  94. struct snd_seq_device *sdev = to_seq_dev(dev);
  95. if (!dev->driver)
  96. request_module("snd-%s", sdev->id);
  97. return 0;
  98. }
  99. static void autoload_drivers(struct work_struct *work)
  100. {
  101. /* avoid reentrance */
  102. if (atomic_inc_return(&snd_seq_in_init) == 1)
  103. bus_for_each_dev(&snd_seq_bus_type, NULL, NULL,
  104. request_seq_drv);
  105. atomic_dec(&snd_seq_in_init);
  106. }
  107. static DECLARE_WORK(autoload_work, autoload_drivers);
  108. static void queue_autoload_drivers(void)
  109. {
  110. schedule_work(&autoload_work);
  111. }
  112. void snd_seq_autoload_init(void)
  113. {
  114. atomic_dec(&snd_seq_in_init);
  115. #ifdef CONFIG_SND_SEQUENCER_MODULE
  116. /* initial autoload only when snd-seq is a module */
  117. queue_autoload_drivers();
  118. #endif
  119. }
  120. EXPORT_SYMBOL(snd_seq_autoload_init);
  121. void snd_seq_autoload_exit(void)
  122. {
  123. atomic_inc(&snd_seq_in_init);
  124. }
  125. EXPORT_SYMBOL(snd_seq_autoload_exit);
  126. void snd_seq_device_load_drivers(void)
  127. {
  128. queue_autoload_drivers();
  129. flush_work(&autoload_work);
  130. }
  131. EXPORT_SYMBOL(snd_seq_device_load_drivers);
  132. #define cancel_autoload_drivers() cancel_work_sync(&autoload_work)
  133. #else
  134. #define queue_autoload_drivers() /* NOP */
  135. #define cancel_autoload_drivers() /* NOP */
  136. #endif
  137. /*
  138. * device management
  139. */
  140. static int snd_seq_device_dev_free(struct snd_device *device)
  141. {
  142. struct snd_seq_device *dev = device->device_data;
  143. cancel_autoload_drivers();
  144. put_device(&dev->dev);
  145. return 0;
  146. }
  147. static int snd_seq_device_dev_register(struct snd_device *device)
  148. {
  149. struct snd_seq_device *dev = device->device_data;
  150. int err;
  151. err = device_add(&dev->dev);
  152. if (err < 0)
  153. return err;
  154. if (!dev->dev.driver)
  155. queue_autoload_drivers();
  156. return 0;
  157. }
  158. static int snd_seq_device_dev_disconnect(struct snd_device *device)
  159. {
  160. struct snd_seq_device *dev = device->device_data;
  161. device_del(&dev->dev);
  162. return 0;
  163. }
  164. static void snd_seq_dev_release(struct device *dev)
  165. {
  166. struct snd_seq_device *sdev = to_seq_dev(dev);
  167. if (sdev->private_free)
  168. sdev->private_free(sdev);
  169. kfree(sdev);
  170. }
  171. /*
  172. * register a sequencer device
  173. * card = card info
  174. * device = device number (if any)
  175. * id = id of driver
  176. * result = return pointer (NULL allowed if unnecessary)
  177. */
  178. int snd_seq_device_new(struct snd_card *card, int device, const char *id,
  179. int argsize, struct snd_seq_device **result)
  180. {
  181. struct snd_seq_device *dev;
  182. int err;
  183. static struct snd_device_ops dops = {
  184. .dev_free = snd_seq_device_dev_free,
  185. .dev_register = snd_seq_device_dev_register,
  186. .dev_disconnect = snd_seq_device_dev_disconnect,
  187. };
  188. if (result)
  189. *result = NULL;
  190. if (snd_BUG_ON(!id))
  191. return -EINVAL;
  192. dev = kzalloc(sizeof(*dev) + argsize, GFP_KERNEL);
  193. if (!dev)
  194. return -ENOMEM;
  195. /* set up device info */
  196. dev->card = card;
  197. dev->device = device;
  198. dev->id = id;
  199. dev->argsize = argsize;
  200. device_initialize(&dev->dev);
  201. dev->dev.parent = &card->card_dev;
  202. dev->dev.bus = &snd_seq_bus_type;
  203. dev->dev.release = snd_seq_dev_release;
  204. dev_set_name(&dev->dev, "%s-%d-%d", dev->id, card->number, device);
  205. /* add this device to the list */
  206. err = snd_device_new(card, SNDRV_DEV_SEQUENCER, dev, &dops);
  207. if (err < 0) {
  208. put_device(&dev->dev);
  209. return err;
  210. }
  211. if (result)
  212. *result = dev;
  213. return 0;
  214. }
  215. EXPORT_SYMBOL(snd_seq_device_new);
  216. /*
  217. * driver registration
  218. */
  219. int __snd_seq_driver_register(struct snd_seq_driver *drv, struct module *mod)
  220. {
  221. if (WARN_ON(!drv->driver.name || !drv->id))
  222. return -EINVAL;
  223. drv->driver.bus = &snd_seq_bus_type;
  224. drv->driver.owner = mod;
  225. return driver_register(&drv->driver);
  226. }
  227. EXPORT_SYMBOL_GPL(__snd_seq_driver_register);
  228. void snd_seq_driver_unregister(struct snd_seq_driver *drv)
  229. {
  230. driver_unregister(&drv->driver);
  231. }
  232. EXPORT_SYMBOL_GPL(snd_seq_driver_unregister);
  233. /*
  234. * module part
  235. */
  236. static int __init seq_dev_proc_init(void)
  237. {
  238. #ifdef CONFIG_SND_PROC_FS
  239. info_entry = snd_info_create_module_entry(THIS_MODULE, "drivers",
  240. snd_seq_root);
  241. if (info_entry == NULL)
  242. return -ENOMEM;
  243. info_entry->content = SNDRV_INFO_CONTENT_TEXT;
  244. info_entry->c.text.read = snd_seq_device_info;
  245. if (snd_info_register(info_entry) < 0) {
  246. snd_info_free_entry(info_entry);
  247. return -ENOMEM;
  248. }
  249. #endif
  250. return 0;
  251. }
  252. static int __init alsa_seq_device_init(void)
  253. {
  254. int err;
  255. err = bus_register(&snd_seq_bus_type);
  256. if (err < 0)
  257. return err;
  258. err = seq_dev_proc_init();
  259. if (err < 0)
  260. bus_unregister(&snd_seq_bus_type);
  261. return err;
  262. }
  263. static void __exit alsa_seq_device_exit(void)
  264. {
  265. #ifdef CONFIG_MODULES
  266. cancel_work_sync(&autoload_work);
  267. #endif
  268. #ifdef CONFIG_SND_PROC_FS
  269. snd_info_free_entry(info_entry);
  270. #endif
  271. bus_unregister(&snd_seq_bus_type);
  272. }
  273. subsys_initcall(alsa_seq_device_init)
  274. module_exit(alsa_seq_device_exit)