seq_device.c 13 KB

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