seq_device.c 14 KB

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