seq_device.c 13 KB

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