init.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774
  1. /*
  2. * Initialization routines
  3. * Copyright (c) by Jaroslav Kysela <perex@suse.cz>
  4. *
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. *
  20. */
  21. #include <sound/driver.h>
  22. #include <linux/init.h>
  23. #include <linux/sched.h>
  24. #include <linux/file.h>
  25. #include <linux/slab.h>
  26. #include <linux/time.h>
  27. #include <linux/ctype.h>
  28. #include <linux/pci.h>
  29. #include <linux/pm.h>
  30. #include <sound/core.h>
  31. #include <sound/control.h>
  32. #include <sound/info.h>
  33. struct snd_shutdown_f_ops {
  34. struct file_operations f_ops;
  35. struct snd_shutdown_f_ops *next;
  36. };
  37. static unsigned int snd_cards_lock; /* locked for registering/using */
  38. struct snd_card *snd_cards[SNDRV_CARDS];
  39. EXPORT_SYMBOL(snd_cards);
  40. static DEFINE_MUTEX(snd_card_mutex);
  41. #if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
  42. int (*snd_mixer_oss_notify_callback)(struct snd_card *card, int free_flag);
  43. EXPORT_SYMBOL(snd_mixer_oss_notify_callback);
  44. #endif
  45. #ifdef CONFIG_PROC_FS
  46. static void snd_card_id_read(struct snd_info_entry *entry,
  47. struct snd_info_buffer *buffer)
  48. {
  49. snd_iprintf(buffer, "%s\n", entry->card->id);
  50. }
  51. static inline int init_info_for_card(struct snd_card *card)
  52. {
  53. int err;
  54. struct snd_info_entry *entry;
  55. if ((err = snd_info_card_register(card)) < 0) {
  56. snd_printd("unable to create card info\n");
  57. return err;
  58. }
  59. if ((entry = snd_info_create_card_entry(card, "id", card->proc_root)) == NULL) {
  60. snd_printd("unable to create card entry\n");
  61. return err;
  62. }
  63. entry->c.text.read = snd_card_id_read;
  64. if (snd_info_register(entry) < 0) {
  65. snd_info_free_entry(entry);
  66. entry = NULL;
  67. }
  68. card->proc_id = entry;
  69. return 0;
  70. }
  71. #else /* !CONFIG_PROC_FS */
  72. #define init_info_for_card(card)
  73. #endif
  74. /**
  75. * snd_card_new - create and initialize a soundcard structure
  76. * @idx: card index (address) [0 ... (SNDRV_CARDS-1)]
  77. * @xid: card identification (ASCII string)
  78. * @module: top level module for locking
  79. * @extra_size: allocate this extra size after the main soundcard structure
  80. *
  81. * Creates and initializes a soundcard structure.
  82. *
  83. * Returns kmallocated snd_card structure. Creates the ALSA control interface
  84. * (which is blocked until snd_card_register function is called).
  85. */
  86. struct snd_card *snd_card_new(int idx, const char *xid,
  87. struct module *module, int extra_size)
  88. {
  89. struct snd_card *card;
  90. int err;
  91. if (extra_size < 0)
  92. extra_size = 0;
  93. card = kzalloc(sizeof(*card) + extra_size, GFP_KERNEL);
  94. if (card == NULL)
  95. return NULL;
  96. if (xid) {
  97. if (!snd_info_check_reserved_words(xid))
  98. goto __error;
  99. strlcpy(card->id, xid, sizeof(card->id));
  100. }
  101. err = 0;
  102. mutex_lock(&snd_card_mutex);
  103. if (idx < 0) {
  104. int idx2;
  105. for (idx2 = 0; idx2 < SNDRV_CARDS; idx2++)
  106. if (~snd_cards_lock & idx & 1<<idx2) {
  107. idx = idx2;
  108. if (idx >= snd_ecards_limit)
  109. snd_ecards_limit = idx + 1;
  110. break;
  111. }
  112. } else if (idx < snd_ecards_limit) {
  113. if (snd_cards_lock & (1 << idx))
  114. err = -ENODEV; /* invalid */
  115. } else if (idx < SNDRV_CARDS)
  116. snd_ecards_limit = idx + 1; /* increase the limit */
  117. else
  118. err = -ENODEV;
  119. if (idx < 0 || err < 0) {
  120. mutex_unlock(&snd_card_mutex);
  121. snd_printk(KERN_ERR "cannot find the slot for index %d (range 0-%i)\n", idx, snd_ecards_limit - 1);
  122. goto __error;
  123. }
  124. snd_cards_lock |= 1 << idx; /* lock it */
  125. mutex_unlock(&snd_card_mutex);
  126. card->number = idx;
  127. card->module = module;
  128. INIT_LIST_HEAD(&card->devices);
  129. init_rwsem(&card->controls_rwsem);
  130. rwlock_init(&card->ctl_files_rwlock);
  131. INIT_LIST_HEAD(&card->controls);
  132. INIT_LIST_HEAD(&card->ctl_files);
  133. spin_lock_init(&card->files_lock);
  134. init_waitqueue_head(&card->shutdown_sleep);
  135. #ifdef CONFIG_PM
  136. mutex_init(&card->power_lock);
  137. init_waitqueue_head(&card->power_sleep);
  138. #endif
  139. /* the control interface cannot be accessed from the user space until */
  140. /* snd_cards_bitmask and snd_cards are set with snd_card_register */
  141. if ((err = snd_ctl_create(card)) < 0) {
  142. snd_printd("unable to register control minors\n");
  143. goto __error;
  144. }
  145. if ((err = snd_info_card_create(card)) < 0) {
  146. snd_printd("unable to create card info\n");
  147. goto __error_ctl;
  148. }
  149. if (extra_size > 0)
  150. card->private_data = (char *)card + sizeof(struct snd_card);
  151. return card;
  152. __error_ctl:
  153. snd_device_free_all(card, SNDRV_DEV_CMD_PRE);
  154. __error:
  155. kfree(card);
  156. return NULL;
  157. }
  158. EXPORT_SYMBOL(snd_card_new);
  159. /* return non-zero if a card is already locked */
  160. int snd_card_locked(int card)
  161. {
  162. int locked;
  163. mutex_lock(&snd_card_mutex);
  164. locked = snd_cards_lock & (1 << card);
  165. mutex_unlock(&snd_card_mutex);
  166. return locked;
  167. }
  168. static loff_t snd_disconnect_llseek(struct file *file, loff_t offset, int orig)
  169. {
  170. return -ENODEV;
  171. }
  172. static ssize_t snd_disconnect_read(struct file *file, char __user *buf,
  173. size_t count, loff_t *offset)
  174. {
  175. return -ENODEV;
  176. }
  177. static ssize_t snd_disconnect_write(struct file *file, const char __user *buf,
  178. size_t count, loff_t *offset)
  179. {
  180. return -ENODEV;
  181. }
  182. static unsigned int snd_disconnect_poll(struct file * file, poll_table * wait)
  183. {
  184. return POLLERR | POLLNVAL;
  185. }
  186. static long snd_disconnect_ioctl(struct file *file,
  187. unsigned int cmd, unsigned long arg)
  188. {
  189. return -ENODEV;
  190. }
  191. static int snd_disconnect_mmap(struct file *file, struct vm_area_struct *vma)
  192. {
  193. return -ENODEV;
  194. }
  195. static int snd_disconnect_fasync(int fd, struct file *file, int on)
  196. {
  197. return -ENODEV;
  198. }
  199. /**
  200. * snd_card_disconnect - disconnect all APIs from the file-operations (user space)
  201. * @card: soundcard structure
  202. *
  203. * Disconnects all APIs from the file-operations (user space).
  204. *
  205. * Returns zero, otherwise a negative error code.
  206. *
  207. * Note: The current implementation replaces all active file->f_op with special
  208. * dummy file operations (they do nothing except release).
  209. */
  210. int snd_card_disconnect(struct snd_card *card)
  211. {
  212. struct snd_monitor_file *mfile;
  213. struct file *file;
  214. struct snd_shutdown_f_ops *s_f_ops;
  215. struct file_operations *f_ops;
  216. const struct file_operations *old_f_ops;
  217. int err;
  218. spin_lock(&card->files_lock);
  219. if (card->shutdown) {
  220. spin_unlock(&card->files_lock);
  221. return 0;
  222. }
  223. card->shutdown = 1;
  224. spin_unlock(&card->files_lock);
  225. /* phase 1: disable fops (user space) operations for ALSA API */
  226. mutex_lock(&snd_card_mutex);
  227. snd_cards[card->number] = NULL;
  228. mutex_unlock(&snd_card_mutex);
  229. /* phase 2: replace file->f_op with special dummy operations */
  230. spin_lock(&card->files_lock);
  231. mfile = card->files;
  232. while (mfile) {
  233. file = mfile->file;
  234. /* it's critical part, use endless loop */
  235. /* we have no room to fail */
  236. s_f_ops = kmalloc(sizeof(struct snd_shutdown_f_ops), GFP_ATOMIC);
  237. if (s_f_ops == NULL)
  238. panic("Atomic allocation failed for snd_shutdown_f_ops!");
  239. f_ops = &s_f_ops->f_ops;
  240. memset(f_ops, 0, sizeof(*f_ops));
  241. f_ops->owner = file->f_op->owner;
  242. f_ops->release = file->f_op->release;
  243. f_ops->llseek = snd_disconnect_llseek;
  244. f_ops->read = snd_disconnect_read;
  245. f_ops->write = snd_disconnect_write;
  246. f_ops->poll = snd_disconnect_poll;
  247. f_ops->unlocked_ioctl = snd_disconnect_ioctl;
  248. #ifdef CONFIG_COMPAT
  249. f_ops->compat_ioctl = snd_disconnect_ioctl;
  250. #endif
  251. f_ops->mmap = snd_disconnect_mmap;
  252. f_ops->fasync = snd_disconnect_fasync;
  253. s_f_ops->next = card->s_f_ops;
  254. card->s_f_ops = s_f_ops;
  255. f_ops = fops_get(f_ops);
  256. old_f_ops = file->f_op;
  257. file->f_op = f_ops; /* must be atomic */
  258. fops_put(old_f_ops);
  259. mfile = mfile->next;
  260. }
  261. spin_unlock(&card->files_lock);
  262. /* phase 3: notify all connected devices about disconnection */
  263. /* at this point, they cannot respond to any calls except release() */
  264. #if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
  265. if (snd_mixer_oss_notify_callback)
  266. snd_mixer_oss_notify_callback(card, SND_MIXER_OSS_NOTIFY_DISCONNECT);
  267. #endif
  268. /* notify all devices that we are disconnected */
  269. err = snd_device_disconnect_all(card);
  270. if (err < 0)
  271. snd_printk(KERN_ERR "not all devices for card %i can be disconnected\n", card->number);
  272. snd_info_card_disconnect(card);
  273. return 0;
  274. }
  275. EXPORT_SYMBOL(snd_card_disconnect);
  276. /**
  277. * snd_card_free - frees given soundcard structure
  278. * @card: soundcard structure
  279. *
  280. * This function releases the soundcard structure and the all assigned
  281. * devices automatically. That is, you don't have to release the devices
  282. * by yourself.
  283. *
  284. * Returns zero. Frees all associated devices and frees the control
  285. * interface associated to given soundcard.
  286. */
  287. static int snd_card_do_free(struct snd_card *card)
  288. {
  289. struct snd_shutdown_f_ops *s_f_ops;
  290. #if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
  291. if (snd_mixer_oss_notify_callback)
  292. snd_mixer_oss_notify_callback(card, SND_MIXER_OSS_NOTIFY_FREE);
  293. #endif
  294. if (snd_device_free_all(card, SNDRV_DEV_CMD_PRE) < 0) {
  295. snd_printk(KERN_ERR "unable to free all devices (pre)\n");
  296. /* Fatal, but this situation should never occur */
  297. }
  298. if (snd_device_free_all(card, SNDRV_DEV_CMD_NORMAL) < 0) {
  299. snd_printk(KERN_ERR "unable to free all devices (normal)\n");
  300. /* Fatal, but this situation should never occur */
  301. }
  302. if (snd_device_free_all(card, SNDRV_DEV_CMD_POST) < 0) {
  303. snd_printk(KERN_ERR "unable to free all devices (post)\n");
  304. /* Fatal, but this situation should never occur */
  305. }
  306. if (card->private_free)
  307. card->private_free(card);
  308. snd_info_free_entry(card->proc_id);
  309. if (snd_info_card_free(card) < 0) {
  310. snd_printk(KERN_WARNING "unable to free card info\n");
  311. /* Not fatal error */
  312. }
  313. while (card->s_f_ops) {
  314. s_f_ops = card->s_f_ops;
  315. card->s_f_ops = s_f_ops->next;
  316. kfree(s_f_ops);
  317. }
  318. kfree(card);
  319. return 0;
  320. }
  321. static int snd_card_free_prepare(struct snd_card *card)
  322. {
  323. if (card == NULL)
  324. return -EINVAL;
  325. (void) snd_card_disconnect(card);
  326. mutex_lock(&snd_card_mutex);
  327. snd_cards[card->number] = NULL;
  328. snd_cards_lock &= ~(1 << card->number);
  329. mutex_unlock(&snd_card_mutex);
  330. #ifdef CONFIG_PM
  331. wake_up(&card->power_sleep);
  332. #endif
  333. return 0;
  334. }
  335. int snd_card_free_when_closed(struct snd_card *card)
  336. {
  337. int free_now = 0;
  338. int ret = snd_card_free_prepare(card);
  339. if (ret)
  340. return ret;
  341. spin_lock(&card->files_lock);
  342. if (card->files == NULL)
  343. free_now = 1;
  344. else
  345. card->free_on_last_close = 1;
  346. spin_unlock(&card->files_lock);
  347. if (free_now)
  348. snd_card_do_free(card);
  349. return 0;
  350. }
  351. EXPORT_SYMBOL(snd_card_free_when_closed);
  352. int snd_card_free(struct snd_card *card)
  353. {
  354. int ret = snd_card_free_prepare(card);
  355. if (ret)
  356. return ret;
  357. /* wait, until all devices are ready for the free operation */
  358. wait_event(card->shutdown_sleep, card->files == NULL);
  359. snd_card_do_free(card);
  360. return 0;
  361. }
  362. EXPORT_SYMBOL(snd_card_free);
  363. static void choose_default_id(struct snd_card *card)
  364. {
  365. int i, len, idx_flag = 0, loops = SNDRV_CARDS;
  366. char *id, *spos;
  367. id = spos = card->shortname;
  368. while (*id != '\0') {
  369. if (*id == ' ')
  370. spos = id + 1;
  371. id++;
  372. }
  373. id = card->id;
  374. while (*spos != '\0' && !isalnum(*spos))
  375. spos++;
  376. if (isdigit(*spos))
  377. *id++ = isalpha(card->shortname[0]) ? card->shortname[0] : 'D';
  378. while (*spos != '\0' && (size_t)(id - card->id) < sizeof(card->id) - 1) {
  379. if (isalnum(*spos))
  380. *id++ = *spos;
  381. spos++;
  382. }
  383. *id = '\0';
  384. id = card->id;
  385. if (*id == '\0')
  386. strcpy(id, "default");
  387. while (1) {
  388. if (loops-- == 0) {
  389. snd_printk(KERN_ERR "unable to choose default card id (%s)\n", id);
  390. strcpy(card->id, card->proc_root->name);
  391. return;
  392. }
  393. if (!snd_info_check_reserved_words(id))
  394. goto __change;
  395. for (i = 0; i < snd_ecards_limit; i++) {
  396. if (snd_cards[i] && !strcmp(snd_cards[i]->id, id))
  397. goto __change;
  398. }
  399. break;
  400. __change:
  401. len = strlen(id);
  402. if (idx_flag) {
  403. if (id[len-1] != '9')
  404. id[len-1]++;
  405. else
  406. id[len-1] = 'A';
  407. } else if ((size_t)len <= sizeof(card->id) - 3) {
  408. strcat(id, "_1");
  409. idx_flag++;
  410. } else {
  411. spos = id + len - 2;
  412. if ((size_t)len <= sizeof(card->id) - 2)
  413. spos++;
  414. *spos++ = '_';
  415. *spos++ = '1';
  416. *spos++ = '\0';
  417. idx_flag++;
  418. }
  419. }
  420. }
  421. /**
  422. * snd_card_register - register the soundcard
  423. * @card: soundcard structure
  424. *
  425. * This function registers all the devices assigned to the soundcard.
  426. * Until calling this, the ALSA control interface is blocked from the
  427. * external accesses. Thus, you should call this function at the end
  428. * of the initialization of the card.
  429. *
  430. * Returns zero otherwise a negative error code if the registrain failed.
  431. */
  432. int snd_card_register(struct snd_card *card)
  433. {
  434. int err;
  435. snd_assert(card != NULL, return -EINVAL);
  436. if ((err = snd_device_register_all(card)) < 0)
  437. return err;
  438. mutex_lock(&snd_card_mutex);
  439. if (snd_cards[card->number]) {
  440. /* already registered */
  441. mutex_unlock(&snd_card_mutex);
  442. return 0;
  443. }
  444. if (card->id[0] == '\0')
  445. choose_default_id(card);
  446. snd_cards[card->number] = card;
  447. mutex_unlock(&snd_card_mutex);
  448. init_info_for_card(card);
  449. #if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
  450. if (snd_mixer_oss_notify_callback)
  451. snd_mixer_oss_notify_callback(card, SND_MIXER_OSS_NOTIFY_REGISTER);
  452. #endif
  453. return 0;
  454. }
  455. EXPORT_SYMBOL(snd_card_register);
  456. #ifdef CONFIG_PROC_FS
  457. static struct snd_info_entry *snd_card_info_entry;
  458. static void snd_card_info_read(struct snd_info_entry *entry,
  459. struct snd_info_buffer *buffer)
  460. {
  461. int idx, count;
  462. struct snd_card *card;
  463. for (idx = count = 0; idx < SNDRV_CARDS; idx++) {
  464. mutex_lock(&snd_card_mutex);
  465. if ((card = snd_cards[idx]) != NULL) {
  466. count++;
  467. snd_iprintf(buffer, "%2i [%-15s]: %s - %s\n",
  468. idx,
  469. card->id,
  470. card->driver,
  471. card->shortname);
  472. snd_iprintf(buffer, " %s\n",
  473. card->longname);
  474. }
  475. mutex_unlock(&snd_card_mutex);
  476. }
  477. if (!count)
  478. snd_iprintf(buffer, "--- no soundcards ---\n");
  479. }
  480. #ifdef CONFIG_SND_OSSEMUL
  481. void snd_card_info_read_oss(struct snd_info_buffer *buffer)
  482. {
  483. int idx, count;
  484. struct snd_card *card;
  485. for (idx = count = 0; idx < SNDRV_CARDS; idx++) {
  486. mutex_lock(&snd_card_mutex);
  487. if ((card = snd_cards[idx]) != NULL) {
  488. count++;
  489. snd_iprintf(buffer, "%s\n", card->longname);
  490. }
  491. mutex_unlock(&snd_card_mutex);
  492. }
  493. if (!count) {
  494. snd_iprintf(buffer, "--- no soundcards ---\n");
  495. }
  496. }
  497. #endif
  498. #ifdef MODULE
  499. static struct snd_info_entry *snd_card_module_info_entry;
  500. static void snd_card_module_info_read(struct snd_info_entry *entry,
  501. struct snd_info_buffer *buffer)
  502. {
  503. int idx;
  504. struct snd_card *card;
  505. for (idx = 0; idx < SNDRV_CARDS; idx++) {
  506. mutex_lock(&snd_card_mutex);
  507. if ((card = snd_cards[idx]) != NULL)
  508. snd_iprintf(buffer, "%2i %s\n",
  509. idx, card->module->name);
  510. mutex_unlock(&snd_card_mutex);
  511. }
  512. }
  513. #endif
  514. int __init snd_card_info_init(void)
  515. {
  516. struct snd_info_entry *entry;
  517. entry = snd_info_create_module_entry(THIS_MODULE, "cards", NULL);
  518. if (! entry)
  519. return -ENOMEM;
  520. entry->c.text.read = snd_card_info_read;
  521. if (snd_info_register(entry) < 0) {
  522. snd_info_free_entry(entry);
  523. return -ENOMEM;
  524. }
  525. snd_card_info_entry = entry;
  526. #ifdef MODULE
  527. entry = snd_info_create_module_entry(THIS_MODULE, "modules", NULL);
  528. if (entry) {
  529. entry->c.text.read = snd_card_module_info_read;
  530. if (snd_info_register(entry) < 0)
  531. snd_info_free_entry(entry);
  532. else
  533. snd_card_module_info_entry = entry;
  534. }
  535. #endif
  536. return 0;
  537. }
  538. int __exit snd_card_info_done(void)
  539. {
  540. snd_info_free_entry(snd_card_info_entry);
  541. #ifdef MODULE
  542. snd_info_free_entry(snd_card_module_info_entry);
  543. #endif
  544. return 0;
  545. }
  546. #endif /* CONFIG_PROC_FS */
  547. /**
  548. * snd_component_add - add a component string
  549. * @card: soundcard structure
  550. * @component: the component id string
  551. *
  552. * This function adds the component id string to the supported list.
  553. * The component can be referred from the alsa-lib.
  554. *
  555. * Returns zero otherwise a negative error code.
  556. */
  557. int snd_component_add(struct snd_card *card, const char *component)
  558. {
  559. char *ptr;
  560. int len = strlen(component);
  561. ptr = strstr(card->components, component);
  562. if (ptr != NULL) {
  563. if (ptr[len] == '\0' || ptr[len] == ' ') /* already there */
  564. return 1;
  565. }
  566. if (strlen(card->components) + 1 + len + 1 > sizeof(card->components)) {
  567. snd_BUG();
  568. return -ENOMEM;
  569. }
  570. if (card->components[0] != '\0')
  571. strcat(card->components, " ");
  572. strcat(card->components, component);
  573. return 0;
  574. }
  575. EXPORT_SYMBOL(snd_component_add);
  576. /**
  577. * snd_card_file_add - add the file to the file list of the card
  578. * @card: soundcard structure
  579. * @file: file pointer
  580. *
  581. * This function adds the file to the file linked-list of the card.
  582. * This linked-list is used to keep tracking the connection state,
  583. * and to avoid the release of busy resources by hotplug.
  584. *
  585. * Returns zero or a negative error code.
  586. */
  587. int snd_card_file_add(struct snd_card *card, struct file *file)
  588. {
  589. struct snd_monitor_file *mfile;
  590. mfile = kmalloc(sizeof(*mfile), GFP_KERNEL);
  591. if (mfile == NULL)
  592. return -ENOMEM;
  593. mfile->file = file;
  594. mfile->next = NULL;
  595. spin_lock(&card->files_lock);
  596. if (card->shutdown) {
  597. spin_unlock(&card->files_lock);
  598. kfree(mfile);
  599. return -ENODEV;
  600. }
  601. mfile->next = card->files;
  602. card->files = mfile;
  603. spin_unlock(&card->files_lock);
  604. return 0;
  605. }
  606. EXPORT_SYMBOL(snd_card_file_add);
  607. /**
  608. * snd_card_file_remove - remove the file from the file list
  609. * @card: soundcard structure
  610. * @file: file pointer
  611. *
  612. * This function removes the file formerly added to the card via
  613. * snd_card_file_add() function.
  614. * If all files are removed and snd_card_free_when_closed() was
  615. * called beforehand, it processes the pending release of
  616. * resources.
  617. *
  618. * Returns zero or a negative error code.
  619. */
  620. int snd_card_file_remove(struct snd_card *card, struct file *file)
  621. {
  622. struct snd_monitor_file *mfile, *pfile = NULL;
  623. int last_close = 0;
  624. spin_lock(&card->files_lock);
  625. mfile = card->files;
  626. while (mfile) {
  627. if (mfile->file == file) {
  628. if (pfile)
  629. pfile->next = mfile->next;
  630. else
  631. card->files = mfile->next;
  632. break;
  633. }
  634. pfile = mfile;
  635. mfile = mfile->next;
  636. }
  637. if (card->files == NULL)
  638. last_close = 1;
  639. spin_unlock(&card->files_lock);
  640. if (last_close) {
  641. wake_up(&card->shutdown_sleep);
  642. if (card->free_on_last_close)
  643. snd_card_do_free(card);
  644. }
  645. if (!mfile) {
  646. snd_printk(KERN_ERR "ALSA card file remove problem (%p)\n", file);
  647. return -ENOENT;
  648. }
  649. kfree(mfile);
  650. return 0;
  651. }
  652. EXPORT_SYMBOL(snd_card_file_remove);
  653. #ifdef CONFIG_PM
  654. /**
  655. * snd_power_wait - wait until the power-state is changed.
  656. * @card: soundcard structure
  657. * @power_state: expected power state
  658. *
  659. * Waits until the power-state is changed.
  660. *
  661. * Note: the power lock must be active before call.
  662. */
  663. int snd_power_wait(struct snd_card *card, unsigned int power_state)
  664. {
  665. wait_queue_t wait;
  666. int result = 0;
  667. /* fastpath */
  668. if (snd_power_get_state(card) == power_state)
  669. return 0;
  670. init_waitqueue_entry(&wait, current);
  671. add_wait_queue(&card->power_sleep, &wait);
  672. while (1) {
  673. if (card->shutdown) {
  674. result = -ENODEV;
  675. break;
  676. }
  677. if (snd_power_get_state(card) == power_state)
  678. break;
  679. set_current_state(TASK_UNINTERRUPTIBLE);
  680. snd_power_unlock(card);
  681. schedule_timeout(30 * HZ);
  682. snd_power_lock(card);
  683. }
  684. remove_wait_queue(&card->power_sleep, &wait);
  685. return result;
  686. }
  687. EXPORT_SYMBOL(snd_power_wait);
  688. #endif /* CONFIG_PM */