seq_device.c 14 KB

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