seq_device.c 14 KB

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