seq_device.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  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 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. /*
  324. * The ->reg_mutex locking rules are per-driver, so we create
  325. * separate per-driver lock classes:
  326. */
  327. lockdep_set_class(&ops->reg_mutex, (struct lock_class_key *)id);
  328. ops->driver = DRIVER_EMPTY;
  329. INIT_LIST_HEAD(&ops->dev_list);
  330. /* lock this instance */
  331. ops->used = 1;
  332. /* register driver entry */
  333. mutex_lock(&ops_mutex);
  334. list_add_tail(&ops->list, &opslist);
  335. num_ops++;
  336. mutex_unlock(&ops_mutex);
  337. return ops;
  338. }
  339. /*
  340. * unregister the specified driver
  341. */
  342. int snd_seq_device_unregister_driver(char *id)
  343. {
  344. struct list_head *head;
  345. struct ops_list *ops;
  346. ops = find_driver(id, 0);
  347. if (ops == NULL)
  348. return -ENXIO;
  349. if (! (ops->driver & DRIVER_LOADED) ||
  350. (ops->driver & DRIVER_LOCKED)) {
  351. snd_printk(KERN_ERR "driver_unregister: cannot unload driver '%s': status=%x\n",
  352. id, ops->driver);
  353. unlock_driver(ops);
  354. return -EBUSY;
  355. }
  356. /* close and release all devices associated with this driver */
  357. mutex_lock(&ops->reg_mutex);
  358. ops->driver |= DRIVER_LOCKED; /* do not remove this driver recursively */
  359. list_for_each(head, &ops->dev_list) {
  360. struct snd_seq_device *dev = list_entry(head, struct snd_seq_device, list);
  361. free_device(dev, ops);
  362. }
  363. ops->driver = 0;
  364. if (ops->num_init_devices > 0)
  365. snd_printk(KERN_ERR "free_driver: init_devices > 0!! (%d)\n",
  366. ops->num_init_devices);
  367. mutex_unlock(&ops->reg_mutex);
  368. unlock_driver(ops);
  369. /* remove empty driver entries */
  370. remove_drivers();
  371. return 0;
  372. }
  373. /*
  374. * remove empty driver entries
  375. */
  376. static void remove_drivers(void)
  377. {
  378. struct list_head *head;
  379. mutex_lock(&ops_mutex);
  380. head = opslist.next;
  381. while (head != &opslist) {
  382. struct ops_list *ops = list_entry(head, struct ops_list, list);
  383. if (! (ops->driver & DRIVER_LOADED) &&
  384. ops->used == 0 && ops->num_devices == 0) {
  385. head = head->next;
  386. list_del(&ops->list);
  387. kfree(ops);
  388. num_ops--;
  389. } else
  390. head = head->next;
  391. }
  392. mutex_unlock(&ops_mutex);
  393. }
  394. /*
  395. * initialize the device - call init_device operator
  396. */
  397. static int init_device(struct snd_seq_device *dev, struct ops_list *ops)
  398. {
  399. if (! (ops->driver & DRIVER_LOADED))
  400. return 0; /* driver is not loaded yet */
  401. if (dev->status != SNDRV_SEQ_DEVICE_FREE)
  402. return 0; /* already initialized */
  403. if (ops->argsize != dev->argsize) {
  404. snd_printk(KERN_ERR "incompatible device '%s' for plug-in '%s' (%d %d)\n",
  405. dev->name, ops->id, ops->argsize, dev->argsize);
  406. return -EINVAL;
  407. }
  408. if (ops->ops.init_device(dev) >= 0) {
  409. dev->status = SNDRV_SEQ_DEVICE_REGISTERED;
  410. ops->num_init_devices++;
  411. } else {
  412. snd_printk(KERN_ERR "init_device failed: %s: %s\n",
  413. dev->name, dev->id);
  414. }
  415. return 0;
  416. }
  417. /*
  418. * release the device - call free_device operator
  419. */
  420. static int free_device(struct snd_seq_device *dev, struct ops_list *ops)
  421. {
  422. int result;
  423. if (! (ops->driver & DRIVER_LOADED))
  424. return 0; /* driver is not loaded yet */
  425. if (dev->status != SNDRV_SEQ_DEVICE_REGISTERED)
  426. return 0; /* not registered */
  427. if (ops->argsize != dev->argsize) {
  428. snd_printk(KERN_ERR "incompatible device '%s' for plug-in '%s' (%d %d)\n",
  429. dev->name, ops->id, ops->argsize, dev->argsize);
  430. return -EINVAL;
  431. }
  432. if ((result = ops->ops.free_device(dev)) >= 0 || result == -ENXIO) {
  433. dev->status = SNDRV_SEQ_DEVICE_FREE;
  434. dev->driver_data = NULL;
  435. ops->num_init_devices--;
  436. } else {
  437. snd_printk(KERN_ERR "free_device failed: %s: %s\n",
  438. dev->name, dev->id);
  439. }
  440. return 0;
  441. }
  442. /*
  443. * find the matching driver with given id
  444. */
  445. static struct ops_list * find_driver(char *id, int create_if_empty)
  446. {
  447. struct list_head *head;
  448. mutex_lock(&ops_mutex);
  449. list_for_each(head, &opslist) {
  450. struct ops_list *ops = list_entry(head, struct ops_list, list);
  451. if (strcmp(ops->id, id) == 0) {
  452. ops->used++;
  453. mutex_unlock(&ops_mutex);
  454. return ops;
  455. }
  456. }
  457. mutex_unlock(&ops_mutex);
  458. if (create_if_empty)
  459. return create_driver(id);
  460. return NULL;
  461. }
  462. static void unlock_driver(struct ops_list *ops)
  463. {
  464. mutex_lock(&ops_mutex);
  465. ops->used--;
  466. mutex_unlock(&ops_mutex);
  467. }
  468. /*
  469. * module part
  470. */
  471. static int __init alsa_seq_device_init(void)
  472. {
  473. #ifdef CONFIG_PROC_FS
  474. info_entry = snd_info_create_module_entry(THIS_MODULE, "drivers",
  475. snd_seq_root);
  476. if (info_entry == NULL)
  477. return -ENOMEM;
  478. info_entry->content = SNDRV_INFO_CONTENT_TEXT;
  479. info_entry->c.text.read = snd_seq_device_info;
  480. if (snd_info_register(info_entry) < 0) {
  481. snd_info_free_entry(info_entry);
  482. return -ENOMEM;
  483. }
  484. #endif
  485. return 0;
  486. }
  487. static void __exit alsa_seq_device_exit(void)
  488. {
  489. remove_drivers();
  490. #ifdef CONFIG_PROC_FS
  491. snd_info_unregister(info_entry);
  492. #endif
  493. if (num_ops)
  494. snd_printk(KERN_ERR "drivers not released (%d)\n", num_ops);
  495. }
  496. module_init(alsa_seq_device_init)
  497. module_exit(alsa_seq_device_exit)
  498. EXPORT_SYMBOL(snd_seq_device_load_drivers);
  499. EXPORT_SYMBOL(snd_seq_device_new);
  500. EXPORT_SYMBOL(snd_seq_device_register_driver);
  501. EXPORT_SYMBOL(snd_seq_device_unregister_driver);
  502. #ifdef CONFIG_KMOD
  503. EXPORT_SYMBOL(snd_seq_autoload_lock);
  504. EXPORT_SYMBOL(snd_seq_autoload_unlock);
  505. #endif