seq_device.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  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. MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>");
  48. MODULE_DESCRIPTION("ALSA sequencer device management");
  49. MODULE_LICENSE("GPL");
  50. /* driver state */
  51. #define DRIVER_EMPTY 0
  52. #define DRIVER_LOADED (1<<0)
  53. #define DRIVER_REQUESTED (1<<1)
  54. #define DRIVER_LOCKED (1<<2)
  55. struct ops_list {
  56. char id[ID_LEN]; /* driver id */
  57. int driver; /* driver state */
  58. int used; /* reference counter */
  59. int argsize; /* argument size */
  60. /* operators */
  61. struct snd_seq_dev_ops ops;
  62. /* registred devices */
  63. struct list_head dev_list; /* list of devices */
  64. int num_devices; /* number of associated devices */
  65. int num_init_devices; /* number of initialized devices */
  66. struct semaphore reg_mutex;
  67. struct list_head list; /* next driver */
  68. };
  69. static LIST_HEAD(opslist);
  70. static int num_ops;
  71. static DECLARE_MUTEX(ops_mutex);
  72. #ifdef CONFIG_PROC_FS
  73. static struct snd_info_entry *info_entry = NULL;
  74. #endif
  75. /*
  76. * prototypes
  77. */
  78. static int snd_seq_device_free(struct snd_seq_device *dev);
  79. static int snd_seq_device_dev_free(struct snd_device *device);
  80. static int snd_seq_device_dev_register(struct snd_device *device);
  81. static int snd_seq_device_dev_disconnect(struct snd_device *device);
  82. static int snd_seq_device_dev_unregister(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. down(&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. up(&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. down(&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. up(&ops_mutex);
  143. ops->driver |= DRIVER_REQUESTED;
  144. request_module("snd-%s", ops->id);
  145. down(&ops_mutex);
  146. ops->used--;
  147. }
  148. }
  149. up(&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. .dev_unregister = snd_seq_device_dev_unregister
  170. };
  171. if (result)
  172. *result = NULL;
  173. snd_assert(id != NULL, return -EINVAL);
  174. ops = find_driver(id, 1);
  175. if (ops == NULL)
  176. return -ENOMEM;
  177. dev = kzalloc(sizeof(*dev)*2 + argsize, GFP_KERNEL);
  178. if (dev == NULL) {
  179. unlock_driver(ops);
  180. return -ENOMEM;
  181. }
  182. /* set up device info */
  183. dev->card = card;
  184. dev->device = device;
  185. strlcpy(dev->id, id, sizeof(dev->id));
  186. dev->argsize = argsize;
  187. dev->status = SNDRV_SEQ_DEVICE_FREE;
  188. /* add this device to the list */
  189. down(&ops->reg_mutex);
  190. list_add_tail(&dev->list, &ops->dev_list);
  191. ops->num_devices++;
  192. up(&ops->reg_mutex);
  193. unlock_driver(ops);
  194. if ((err = snd_device_new(card, SNDRV_DEV_SEQUENCER, dev, &dops)) < 0) {
  195. snd_seq_device_free(dev);
  196. return err;
  197. }
  198. if (result)
  199. *result = dev;
  200. return 0;
  201. }
  202. /*
  203. * free the existing device
  204. */
  205. static int snd_seq_device_free(struct snd_seq_device *dev)
  206. {
  207. struct ops_list *ops;
  208. snd_assert(dev != NULL, return -EINVAL);
  209. ops = find_driver(dev->id, 0);
  210. if (ops == NULL)
  211. return -ENXIO;
  212. /* remove the device from the list */
  213. down(&ops->reg_mutex);
  214. list_del(&dev->list);
  215. ops->num_devices--;
  216. up(&ops->reg_mutex);
  217. free_device(dev, ops);
  218. if (dev->private_free)
  219. dev->private_free(dev);
  220. kfree(dev);
  221. unlock_driver(ops);
  222. return 0;
  223. }
  224. static int snd_seq_device_dev_free(struct snd_device *device)
  225. {
  226. struct snd_seq_device *dev = device->device_data;
  227. return snd_seq_device_free(dev);
  228. }
  229. /*
  230. * register the device
  231. */
  232. static int snd_seq_device_dev_register(struct snd_device *device)
  233. {
  234. struct snd_seq_device *dev = device->device_data;
  235. struct ops_list *ops;
  236. ops = find_driver(dev->id, 0);
  237. if (ops == NULL)
  238. return -ENOENT;
  239. /* initialize this device if the corresponding driver was
  240. * already loaded
  241. */
  242. if (ops->driver & DRIVER_LOADED)
  243. init_device(dev, ops);
  244. unlock_driver(ops);
  245. return 0;
  246. }
  247. /*
  248. * disconnect the device
  249. */
  250. static int snd_seq_device_dev_disconnect(struct snd_device *device)
  251. {
  252. struct snd_seq_device *dev = device->device_data;
  253. struct ops_list *ops;
  254. ops = find_driver(dev->id, 0);
  255. if (ops == NULL)
  256. return -ENOENT;
  257. free_device(dev, ops);
  258. unlock_driver(ops);
  259. return 0;
  260. }
  261. /*
  262. * unregister the existing device
  263. */
  264. static int snd_seq_device_dev_unregister(struct snd_device *device)
  265. {
  266. struct snd_seq_device *dev = device->device_data;
  267. return snd_seq_device_free(dev);
  268. }
  269. /*
  270. * register device driver
  271. * id = driver id
  272. * entry = driver operators - duplicated to each instance
  273. */
  274. int snd_seq_device_register_driver(char *id, struct snd_seq_dev_ops *entry,
  275. int argsize)
  276. {
  277. struct list_head *head;
  278. struct ops_list *ops;
  279. if (id == NULL || entry == NULL ||
  280. entry->init_device == NULL || entry->free_device == NULL)
  281. return -EINVAL;
  282. snd_seq_autoload_lock();
  283. ops = find_driver(id, 1);
  284. if (ops == NULL) {
  285. snd_seq_autoload_unlock();
  286. return -ENOMEM;
  287. }
  288. if (ops->driver & DRIVER_LOADED) {
  289. snd_printk(KERN_WARNING "driver_register: driver '%s' already exists\n", id);
  290. unlock_driver(ops);
  291. snd_seq_autoload_unlock();
  292. return -EBUSY;
  293. }
  294. down(&ops->reg_mutex);
  295. /* copy driver operators */
  296. ops->ops = *entry;
  297. ops->driver |= DRIVER_LOADED;
  298. ops->argsize = argsize;
  299. /* initialize existing devices if necessary */
  300. list_for_each(head, &ops->dev_list) {
  301. struct snd_seq_device *dev = list_entry(head, struct snd_seq_device, list);
  302. init_device(dev, ops);
  303. }
  304. up(&ops->reg_mutex);
  305. unlock_driver(ops);
  306. snd_seq_autoload_unlock();
  307. return 0;
  308. }
  309. /*
  310. * create driver record
  311. */
  312. static struct ops_list * create_driver(char *id)
  313. {
  314. struct ops_list *ops;
  315. ops = kmalloc(sizeof(*ops), GFP_KERNEL);
  316. if (ops == NULL)
  317. return ops;
  318. memset(ops, 0, sizeof(*ops));
  319. /* set up driver entry */
  320. strlcpy(ops->id, id, sizeof(ops->id));
  321. init_MUTEX(&ops->reg_mutex);
  322. ops->driver = DRIVER_EMPTY;
  323. INIT_LIST_HEAD(&ops->dev_list);
  324. /* lock this instance */
  325. ops->used = 1;
  326. /* register driver entry */
  327. down(&ops_mutex);
  328. list_add_tail(&ops->list, &opslist);
  329. num_ops++;
  330. up(&ops_mutex);
  331. return ops;
  332. }
  333. /*
  334. * unregister the specified driver
  335. */
  336. int snd_seq_device_unregister_driver(char *id)
  337. {
  338. struct list_head *head;
  339. struct ops_list *ops;
  340. ops = find_driver(id, 0);
  341. if (ops == NULL)
  342. return -ENXIO;
  343. if (! (ops->driver & DRIVER_LOADED) ||
  344. (ops->driver & DRIVER_LOCKED)) {
  345. snd_printk(KERN_ERR "driver_unregister: cannot unload driver '%s': status=%x\n",
  346. id, ops->driver);
  347. unlock_driver(ops);
  348. return -EBUSY;
  349. }
  350. /* close and release all devices associated with this driver */
  351. down(&ops->reg_mutex);
  352. ops->driver |= DRIVER_LOCKED; /* do not remove this driver recursively */
  353. list_for_each(head, &ops->dev_list) {
  354. struct snd_seq_device *dev = list_entry(head, struct snd_seq_device, list);
  355. free_device(dev, ops);
  356. }
  357. ops->driver = 0;
  358. if (ops->num_init_devices > 0)
  359. snd_printk(KERN_ERR "free_driver: init_devices > 0!! (%d)\n",
  360. ops->num_init_devices);
  361. up(&ops->reg_mutex);
  362. unlock_driver(ops);
  363. /* remove empty driver entries */
  364. remove_drivers();
  365. return 0;
  366. }
  367. /*
  368. * remove empty driver entries
  369. */
  370. static void remove_drivers(void)
  371. {
  372. struct list_head *head;
  373. down(&ops_mutex);
  374. head = opslist.next;
  375. while (head != &opslist) {
  376. struct ops_list *ops = list_entry(head, struct ops_list, list);
  377. if (! (ops->driver & DRIVER_LOADED) &&
  378. ops->used == 0 && ops->num_devices == 0) {
  379. head = head->next;
  380. list_del(&ops->list);
  381. kfree(ops);
  382. num_ops--;
  383. } else
  384. head = head->next;
  385. }
  386. up(&ops_mutex);
  387. }
  388. /*
  389. * initialize the device - call init_device operator
  390. */
  391. static int init_device(struct snd_seq_device *dev, struct ops_list *ops)
  392. {
  393. if (! (ops->driver & DRIVER_LOADED))
  394. return 0; /* driver is not loaded yet */
  395. if (dev->status != SNDRV_SEQ_DEVICE_FREE)
  396. return 0; /* already initialized */
  397. if (ops->argsize != dev->argsize) {
  398. snd_printk(KERN_ERR "incompatible device '%s' for plug-in '%s' (%d %d)\n",
  399. dev->name, ops->id, ops->argsize, dev->argsize);
  400. return -EINVAL;
  401. }
  402. if (ops->ops.init_device(dev) >= 0) {
  403. dev->status = SNDRV_SEQ_DEVICE_REGISTERED;
  404. ops->num_init_devices++;
  405. } else {
  406. snd_printk(KERN_ERR "init_device failed: %s: %s\n",
  407. dev->name, dev->id);
  408. }
  409. return 0;
  410. }
  411. /*
  412. * release the device - call free_device operator
  413. */
  414. static int free_device(struct snd_seq_device *dev, struct ops_list *ops)
  415. {
  416. int result;
  417. if (! (ops->driver & DRIVER_LOADED))
  418. return 0; /* driver is not loaded yet */
  419. if (dev->status != SNDRV_SEQ_DEVICE_REGISTERED)
  420. return 0; /* not registered */
  421. if (ops->argsize != dev->argsize) {
  422. snd_printk(KERN_ERR "incompatible device '%s' for plug-in '%s' (%d %d)\n",
  423. dev->name, ops->id, ops->argsize, dev->argsize);
  424. return -EINVAL;
  425. }
  426. if ((result = ops->ops.free_device(dev)) >= 0 || result == -ENXIO) {
  427. dev->status = SNDRV_SEQ_DEVICE_FREE;
  428. dev->driver_data = NULL;
  429. ops->num_init_devices--;
  430. } else {
  431. snd_printk(KERN_ERR "free_device failed: %s: %s\n",
  432. dev->name, dev->id);
  433. }
  434. return 0;
  435. }
  436. /*
  437. * find the matching driver with given id
  438. */
  439. static struct ops_list * find_driver(char *id, int create_if_empty)
  440. {
  441. struct list_head *head;
  442. down(&ops_mutex);
  443. list_for_each(head, &opslist) {
  444. struct ops_list *ops = list_entry(head, struct ops_list, list);
  445. if (strcmp(ops->id, id) == 0) {
  446. ops->used++;
  447. up(&ops_mutex);
  448. return ops;
  449. }
  450. }
  451. up(&ops_mutex);
  452. if (create_if_empty)
  453. return create_driver(id);
  454. return NULL;
  455. }
  456. static void unlock_driver(struct ops_list *ops)
  457. {
  458. down(&ops_mutex);
  459. ops->used--;
  460. up(&ops_mutex);
  461. }
  462. /*
  463. * module part
  464. */
  465. static int __init alsa_seq_device_init(void)
  466. {
  467. #ifdef CONFIG_PROC_FS
  468. info_entry = snd_info_create_module_entry(THIS_MODULE, "drivers",
  469. snd_seq_root);
  470. if (info_entry == NULL)
  471. return -ENOMEM;
  472. info_entry->content = SNDRV_INFO_CONTENT_TEXT;
  473. info_entry->c.text.read_size = 2048;
  474. info_entry->c.text.read = snd_seq_device_info;
  475. if (snd_info_register(info_entry) < 0) {
  476. snd_info_free_entry(info_entry);
  477. return -ENOMEM;
  478. }
  479. #endif
  480. return 0;
  481. }
  482. static void __exit alsa_seq_device_exit(void)
  483. {
  484. remove_drivers();
  485. #ifdef CONFIG_PROC_FS
  486. snd_info_unregister(info_entry);
  487. #endif
  488. if (num_ops)
  489. snd_printk(KERN_ERR "drivers not released (%d)\n", num_ops);
  490. }
  491. module_init(alsa_seq_device_init)
  492. module_exit(alsa_seq_device_exit)
  493. EXPORT_SYMBOL(snd_seq_device_load_drivers);
  494. EXPORT_SYMBOL(snd_seq_device_new);
  495. EXPORT_SYMBOL(snd_seq_device_register_driver);
  496. EXPORT_SYMBOL(snd_seq_device_unregister_driver);
  497. #ifdef CONFIG_KMOD
  498. EXPORT_SYMBOL(snd_seq_autoload_lock);
  499. EXPORT_SYMBOL(snd_seq_autoload_unlock);
  500. #endif