seq_device.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  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 <sound/driver.h>
  39. #include <linux/init.h>
  40. #include <sound/core.h>
  41. #include <sound/info.h>
  42. #include <sound/seq_device.h>
  43. #include <sound/seq_kernel.h>
  44. #include <sound/initval.h>
  45. #include <linux/kmod.h>
  46. #include <linux/slab.h>
  47. #include <linux/mutex.h>
  48. MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>");
  49. MODULE_DESCRIPTION("ALSA sequencer device management");
  50. MODULE_LICENSE("GPL");
  51. /* driver state */
  52. #define DRIVER_EMPTY 0
  53. #define DRIVER_LOADED (1<<0)
  54. #define DRIVER_REQUESTED (1<<1)
  55. #define DRIVER_LOCKED (1<<2)
  56. struct ops_list {
  57. char id[ID_LEN]; /* driver id */
  58. int driver; /* driver state */
  59. int used; /* reference counter */
  60. int argsize; /* argument size */
  61. /* operators */
  62. struct snd_seq_dev_ops ops;
  63. /* registred devices */
  64. struct list_head dev_list; /* list of devices */
  65. int num_devices; /* number of associated devices */
  66. int num_init_devices; /* number of initialized devices */
  67. struct mutex reg_mutex;
  68. struct list_head list; /* next driver */
  69. };
  70. static LIST_HEAD(opslist);
  71. static int num_ops;
  72. static DEFINE_MUTEX(ops_mutex);
  73. #ifdef CONFIG_PROC_FS
  74. static struct snd_info_entry *info_entry;
  75. #endif
  76. /*
  77. * prototypes
  78. */
  79. static int snd_seq_device_free(struct snd_seq_device *dev);
  80. static int snd_seq_device_dev_free(struct snd_device *device);
  81. static int snd_seq_device_dev_register(struct snd_device *device);
  82. static int snd_seq_device_dev_disconnect(struct snd_device *device);
  83. static int init_device(struct snd_seq_device *dev, struct ops_list *ops);
  84. static int free_device(struct snd_seq_device *dev, struct ops_list *ops);
  85. static struct ops_list *find_driver(char *id, int create_if_empty);
  86. static struct ops_list *create_driver(char *id);
  87. static void unlock_driver(struct ops_list *ops);
  88. static void remove_drivers(void);
  89. /*
  90. * show all drivers and their status
  91. */
  92. #ifdef CONFIG_PROC_FS
  93. static void snd_seq_device_info(struct snd_info_entry *entry,
  94. struct snd_info_buffer *buffer)
  95. {
  96. struct ops_list *ops;
  97. mutex_lock(&ops_mutex);
  98. list_for_each_entry(ops, &opslist, list) {
  99. snd_iprintf(buffer, "snd-%s%s%s%s,%d\n",
  100. ops->id,
  101. ops->driver & DRIVER_LOADED ? ",loaded" : (ops->driver == DRIVER_EMPTY ? ",empty" : ""),
  102. ops->driver & DRIVER_REQUESTED ? ",requested" : "",
  103. ops->driver & DRIVER_LOCKED ? ",locked" : "",
  104. ops->num_devices);
  105. }
  106. mutex_unlock(&ops_mutex);
  107. }
  108. #endif
  109. /*
  110. * load all registered drivers (called from seq_clientmgr.c)
  111. */
  112. #ifdef CONFIG_KMOD
  113. /* avoid auto-loading during module_init() */
  114. static int snd_seq_in_init;
  115. void snd_seq_autoload_lock(void)
  116. {
  117. snd_seq_in_init++;
  118. }
  119. void snd_seq_autoload_unlock(void)
  120. {
  121. snd_seq_in_init--;
  122. }
  123. #endif
  124. void snd_seq_device_load_drivers(void)
  125. {
  126. #ifdef CONFIG_KMOD
  127. struct ops_list *ops;
  128. /* Calling request_module during module_init()
  129. * may cause blocking.
  130. */
  131. if (snd_seq_in_init)
  132. return;
  133. if (! current->fs->root)
  134. return;
  135. mutex_lock(&ops_mutex);
  136. list_for_each_entry(ops, &opslist, list) {
  137. if (! (ops->driver & DRIVER_LOADED) &&
  138. ! (ops->driver & DRIVER_REQUESTED)) {
  139. ops->used++;
  140. mutex_unlock(&ops_mutex);
  141. ops->driver |= DRIVER_REQUESTED;
  142. request_module("snd-%s", ops->id);
  143. mutex_lock(&ops_mutex);
  144. ops->used--;
  145. }
  146. }
  147. mutex_unlock(&ops_mutex);
  148. #endif
  149. }
  150. /*
  151. * register a sequencer device
  152. * card = card info (NULL allowed)
  153. * device = device number (if any)
  154. * id = id of driver
  155. * result = return pointer (NULL allowed if unnecessary)
  156. */
  157. int snd_seq_device_new(struct snd_card *card, int device, char *id, int argsize,
  158. struct snd_seq_device **result)
  159. {
  160. struct snd_seq_device *dev;
  161. struct ops_list *ops;
  162. int err;
  163. static struct snd_device_ops dops = {
  164. .dev_free = snd_seq_device_dev_free,
  165. .dev_register = snd_seq_device_dev_register,
  166. .dev_disconnect = snd_seq_device_dev_disconnect,
  167. };
  168. if (result)
  169. *result = NULL;
  170. snd_assert(id != NULL, return -EINVAL);
  171. ops = find_driver(id, 1);
  172. if (ops == NULL)
  173. return -ENOMEM;
  174. dev = kzalloc(sizeof(*dev)*2 + argsize, GFP_KERNEL);
  175. if (dev == NULL) {
  176. unlock_driver(ops);
  177. return -ENOMEM;
  178. }
  179. /* set up device info */
  180. dev->card = card;
  181. dev->device = device;
  182. strlcpy(dev->id, id, sizeof(dev->id));
  183. dev->argsize = argsize;
  184. dev->status = SNDRV_SEQ_DEVICE_FREE;
  185. /* add this device to the list */
  186. mutex_lock(&ops->reg_mutex);
  187. list_add_tail(&dev->list, &ops->dev_list);
  188. ops->num_devices++;
  189. mutex_unlock(&ops->reg_mutex);
  190. unlock_driver(ops);
  191. if ((err = snd_device_new(card, SNDRV_DEV_SEQUENCER, dev, &dops)) < 0) {
  192. snd_seq_device_free(dev);
  193. return err;
  194. }
  195. if (result)
  196. *result = dev;
  197. return 0;
  198. }
  199. /*
  200. * free the existing device
  201. */
  202. static int snd_seq_device_free(struct snd_seq_device *dev)
  203. {
  204. struct ops_list *ops;
  205. snd_assert(dev != NULL, return -EINVAL);
  206. ops = find_driver(dev->id, 0);
  207. if (ops == NULL)
  208. return -ENXIO;
  209. /* remove the device from the list */
  210. mutex_lock(&ops->reg_mutex);
  211. list_del(&dev->list);
  212. ops->num_devices--;
  213. mutex_unlock(&ops->reg_mutex);
  214. free_device(dev, ops);
  215. if (dev->private_free)
  216. dev->private_free(dev);
  217. kfree(dev);
  218. unlock_driver(ops);
  219. return 0;
  220. }
  221. static int snd_seq_device_dev_free(struct snd_device *device)
  222. {
  223. struct snd_seq_device *dev = device->device_data;
  224. return snd_seq_device_free(dev);
  225. }
  226. /*
  227. * register the device
  228. */
  229. static int snd_seq_device_dev_register(struct snd_device *device)
  230. {
  231. struct snd_seq_device *dev = device->device_data;
  232. struct ops_list *ops;
  233. ops = find_driver(dev->id, 0);
  234. if (ops == NULL)
  235. return -ENOENT;
  236. /* initialize this device if the corresponding driver was
  237. * already loaded
  238. */
  239. if (ops->driver & DRIVER_LOADED)
  240. init_device(dev, ops);
  241. unlock_driver(ops);
  242. return 0;
  243. }
  244. /*
  245. * disconnect the device
  246. */
  247. static int snd_seq_device_dev_disconnect(struct snd_device *device)
  248. {
  249. struct snd_seq_device *dev = device->device_data;
  250. struct ops_list *ops;
  251. ops = find_driver(dev->id, 0);
  252. if (ops == NULL)
  253. return -ENOENT;
  254. free_device(dev, ops);
  255. unlock_driver(ops);
  256. return 0;
  257. }
  258. /*
  259. * register device driver
  260. * id = driver id
  261. * entry = driver operators - duplicated to each instance
  262. */
  263. int snd_seq_device_register_driver(char *id, struct snd_seq_dev_ops *entry,
  264. int argsize)
  265. {
  266. struct ops_list *ops;
  267. struct snd_seq_device *dev;
  268. if (id == NULL || entry == NULL ||
  269. entry->init_device == NULL || entry->free_device == NULL)
  270. return -EINVAL;
  271. snd_seq_autoload_lock();
  272. ops = find_driver(id, 1);
  273. if (ops == NULL) {
  274. snd_seq_autoload_unlock();
  275. return -ENOMEM;
  276. }
  277. if (ops->driver & DRIVER_LOADED) {
  278. snd_printk(KERN_WARNING "driver_register: driver '%s' already exists\n", id);
  279. unlock_driver(ops);
  280. snd_seq_autoload_unlock();
  281. return -EBUSY;
  282. }
  283. mutex_lock(&ops->reg_mutex);
  284. /* copy driver operators */
  285. ops->ops = *entry;
  286. ops->driver |= DRIVER_LOADED;
  287. ops->argsize = argsize;
  288. /* initialize existing devices if necessary */
  289. list_for_each_entry(dev, &ops->dev_list, list) {
  290. init_device(dev, ops);
  291. }
  292. mutex_unlock(&ops->reg_mutex);
  293. unlock_driver(ops);
  294. snd_seq_autoload_unlock();
  295. return 0;
  296. }
  297. /*
  298. * create driver record
  299. */
  300. static struct ops_list * create_driver(char *id)
  301. {
  302. struct ops_list *ops;
  303. ops = kzalloc(sizeof(*ops), GFP_KERNEL);
  304. if (ops == NULL)
  305. return ops;
  306. /* set up driver entry */
  307. strlcpy(ops->id, id, sizeof(ops->id));
  308. mutex_init(&ops->reg_mutex);
  309. /*
  310. * The ->reg_mutex locking rules are per-driver, so we create
  311. * separate per-driver lock classes:
  312. */
  313. lockdep_set_class(&ops->reg_mutex, (struct lock_class_key *)id);
  314. ops->driver = DRIVER_EMPTY;
  315. INIT_LIST_HEAD(&ops->dev_list);
  316. /* lock this instance */
  317. ops->used = 1;
  318. /* register driver entry */
  319. mutex_lock(&ops_mutex);
  320. list_add_tail(&ops->list, &opslist);
  321. num_ops++;
  322. mutex_unlock(&ops_mutex);
  323. return ops;
  324. }
  325. /*
  326. * unregister the specified driver
  327. */
  328. int snd_seq_device_unregister_driver(char *id)
  329. {
  330. struct ops_list *ops;
  331. struct snd_seq_device *dev;
  332. ops = find_driver(id, 0);
  333. if (ops == NULL)
  334. return -ENXIO;
  335. if (! (ops->driver & DRIVER_LOADED) ||
  336. (ops->driver & DRIVER_LOCKED)) {
  337. snd_printk(KERN_ERR "driver_unregister: cannot unload driver '%s': status=%x\n",
  338. id, ops->driver);
  339. unlock_driver(ops);
  340. return -EBUSY;
  341. }
  342. /* close and release all devices associated with this driver */
  343. mutex_lock(&ops->reg_mutex);
  344. ops->driver |= DRIVER_LOCKED; /* do not remove this driver recursively */
  345. list_for_each_entry(dev, &ops->dev_list, list) {
  346. free_device(dev, ops);
  347. }
  348. ops->driver = 0;
  349. if (ops->num_init_devices > 0)
  350. snd_printk(KERN_ERR "free_driver: init_devices > 0!! (%d)\n",
  351. ops->num_init_devices);
  352. mutex_unlock(&ops->reg_mutex);
  353. unlock_driver(ops);
  354. /* remove empty driver entries */
  355. remove_drivers();
  356. return 0;
  357. }
  358. /*
  359. * remove empty driver entries
  360. */
  361. static void remove_drivers(void)
  362. {
  363. struct list_head *head;
  364. mutex_lock(&ops_mutex);
  365. head = opslist.next;
  366. while (head != &opslist) {
  367. struct ops_list *ops = list_entry(head, struct ops_list, list);
  368. if (! (ops->driver & DRIVER_LOADED) &&
  369. ops->used == 0 && ops->num_devices == 0) {
  370. head = head->next;
  371. list_del(&ops->list);
  372. kfree(ops);
  373. num_ops--;
  374. } else
  375. head = head->next;
  376. }
  377. mutex_unlock(&ops_mutex);
  378. }
  379. /*
  380. * initialize the device - call init_device operator
  381. */
  382. static int init_device(struct snd_seq_device *dev, struct ops_list *ops)
  383. {
  384. if (! (ops->driver & DRIVER_LOADED))
  385. return 0; /* driver is not loaded yet */
  386. if (dev->status != SNDRV_SEQ_DEVICE_FREE)
  387. return 0; /* already initialized */
  388. if (ops->argsize != dev->argsize) {
  389. snd_printk(KERN_ERR "incompatible device '%s' for plug-in '%s' (%d %d)\n",
  390. dev->name, ops->id, ops->argsize, dev->argsize);
  391. return -EINVAL;
  392. }
  393. if (ops->ops.init_device(dev) >= 0) {
  394. dev->status = SNDRV_SEQ_DEVICE_REGISTERED;
  395. ops->num_init_devices++;
  396. } else {
  397. snd_printk(KERN_ERR "init_device failed: %s: %s\n",
  398. dev->name, dev->id);
  399. }
  400. return 0;
  401. }
  402. /*
  403. * release the device - call free_device operator
  404. */
  405. static int free_device(struct snd_seq_device *dev, struct ops_list *ops)
  406. {
  407. int result;
  408. if (! (ops->driver & DRIVER_LOADED))
  409. return 0; /* driver is not loaded yet */
  410. if (dev->status != SNDRV_SEQ_DEVICE_REGISTERED)
  411. return 0; /* not registered */
  412. if (ops->argsize != dev->argsize) {
  413. snd_printk(KERN_ERR "incompatible device '%s' for plug-in '%s' (%d %d)\n",
  414. dev->name, ops->id, ops->argsize, dev->argsize);
  415. return -EINVAL;
  416. }
  417. if ((result = ops->ops.free_device(dev)) >= 0 || result == -ENXIO) {
  418. dev->status = SNDRV_SEQ_DEVICE_FREE;
  419. dev->driver_data = NULL;
  420. ops->num_init_devices--;
  421. } else {
  422. snd_printk(KERN_ERR "free_device failed: %s: %s\n",
  423. dev->name, dev->id);
  424. }
  425. return 0;
  426. }
  427. /*
  428. * find the matching driver with given id
  429. */
  430. static struct ops_list * find_driver(char *id, int create_if_empty)
  431. {
  432. struct ops_list *ops;
  433. mutex_lock(&ops_mutex);
  434. list_for_each_entry(ops, &opslist, list) {
  435. if (strcmp(ops->id, id) == 0) {
  436. ops->used++;
  437. mutex_unlock(&ops_mutex);
  438. return ops;
  439. }
  440. }
  441. mutex_unlock(&ops_mutex);
  442. if (create_if_empty)
  443. return create_driver(id);
  444. return NULL;
  445. }
  446. static void unlock_driver(struct ops_list *ops)
  447. {
  448. mutex_lock(&ops_mutex);
  449. ops->used--;
  450. mutex_unlock(&ops_mutex);
  451. }
  452. /*
  453. * module part
  454. */
  455. static int __init alsa_seq_device_init(void)
  456. {
  457. #ifdef CONFIG_PROC_FS
  458. info_entry = snd_info_create_module_entry(THIS_MODULE, "drivers",
  459. snd_seq_root);
  460. if (info_entry == NULL)
  461. return -ENOMEM;
  462. info_entry->content = SNDRV_INFO_CONTENT_TEXT;
  463. info_entry->c.text.read = snd_seq_device_info;
  464. if (snd_info_register(info_entry) < 0) {
  465. snd_info_free_entry(info_entry);
  466. return -ENOMEM;
  467. }
  468. #endif
  469. return 0;
  470. }
  471. static void __exit alsa_seq_device_exit(void)
  472. {
  473. remove_drivers();
  474. #ifdef CONFIG_PROC_FS
  475. snd_info_free_entry(info_entry);
  476. #endif
  477. if (num_ops)
  478. snd_printk(KERN_ERR "drivers not released (%d)\n", num_ops);
  479. }
  480. module_init(alsa_seq_device_init)
  481. module_exit(alsa_seq_device_exit)
  482. EXPORT_SYMBOL(snd_seq_device_load_drivers);
  483. EXPORT_SYMBOL(snd_seq_device_new);
  484. EXPORT_SYMBOL(snd_seq_device_register_driver);
  485. EXPORT_SYMBOL(snd_seq_device_unregister_driver);
  486. #ifdef CONFIG_KMOD
  487. EXPORT_SYMBOL(snd_seq_autoload_lock);
  488. EXPORT_SYMBOL(snd_seq_autoload_unlock);
  489. #endif