seq_device.c 7.6 KB

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