init.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  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. unsigned int snd_cards_lock = 0; /* locked for registering/using */
  38. struct snd_card *snd_cards[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] = NULL};
  39. DEFINE_RWLOCK(snd_card_rwlock);
  40. #if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
  41. int (*snd_mixer_oss_notify_callback)(struct snd_card *card, int free_flag);
  42. #endif
  43. #ifdef CONFIG_PROC_FS
  44. static void snd_card_id_read(struct snd_info_entry *entry,
  45. struct snd_info_buffer *buffer)
  46. {
  47. snd_iprintf(buffer, "%s\n", entry->card->id);
  48. }
  49. static inline int init_info_for_card(struct snd_card *card)
  50. {
  51. int err;
  52. struct snd_info_entry *entry;
  53. if ((err = snd_info_card_register(card)) < 0) {
  54. snd_printd("unable to create card info\n");
  55. return err;
  56. }
  57. if ((entry = snd_info_create_card_entry(card, "id", card->proc_root)) == NULL) {
  58. snd_printd("unable to create card entry\n");
  59. return err;
  60. }
  61. entry->c.text.read_size = PAGE_SIZE;
  62. entry->c.text.read = snd_card_id_read;
  63. if (snd_info_register(entry) < 0) {
  64. snd_info_free_entry(entry);
  65. entry = NULL;
  66. }
  67. card->proc_id = entry;
  68. return 0;
  69. }
  70. #else /* !CONFIG_PROC_FS */
  71. #define init_info_for_card(card)
  72. #endif
  73. static void snd_card_free_thread(void * __card);
  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. write_lock(&snd_card_rwlock);
  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. write_unlock(&snd_card_rwlock);
  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. write_unlock(&snd_card_rwlock);
  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. INIT_WORK(&card->free_workq, snd_card_free_thread, card);
  136. #ifdef CONFIG_PM
  137. init_MUTEX(&card->power_lock);
  138. init_waitqueue_head(&card->power_sleep);
  139. #endif
  140. /* the control interface cannot be accessed from the user space until */
  141. /* snd_cards_bitmask and snd_cards are set with snd_card_register */
  142. if ((err = snd_ctl_create(card)) < 0) {
  143. snd_printd("unable to register control minors\n");
  144. goto __error;
  145. }
  146. if ((err = snd_info_card_create(card)) < 0) {
  147. snd_printd("unable to create card info\n");
  148. goto __error_ctl;
  149. }
  150. if (extra_size > 0)
  151. card->private_data = (char *)card + sizeof(struct snd_card);
  152. return card;
  153. __error_ctl:
  154. snd_device_free_all(card, SNDRV_DEV_CMD_PRE);
  155. __error:
  156. kfree(card);
  157. return NULL;
  158. }
  159. static unsigned int snd_disconnect_poll(struct file * file, poll_table * wait)
  160. {
  161. return POLLERR | POLLNVAL;
  162. }
  163. /**
  164. * snd_card_disconnect - disconnect all APIs from the file-operations (user space)
  165. * @card: soundcard structure
  166. *
  167. * Disconnects all APIs from the file-operations (user space).
  168. *
  169. * Returns zero, otherwise a negative error code.
  170. *
  171. * Note: The current implementation replaces all active file->f_op with special
  172. * dummy file operations (they do nothing except release).
  173. */
  174. int snd_card_disconnect(struct snd_card *card)
  175. {
  176. struct snd_monitor_file *mfile;
  177. struct file *file;
  178. struct snd_shutdown_f_ops *s_f_ops;
  179. struct file_operations *f_ops, *old_f_ops;
  180. int err;
  181. spin_lock(&card->files_lock);
  182. if (card->shutdown) {
  183. spin_unlock(&card->files_lock);
  184. return 0;
  185. }
  186. card->shutdown = 1;
  187. spin_unlock(&card->files_lock);
  188. /* phase 1: disable fops (user space) operations for ALSA API */
  189. write_lock(&snd_card_rwlock);
  190. snd_cards[card->number] = NULL;
  191. write_unlock(&snd_card_rwlock);
  192. /* phase 2: replace file->f_op with special dummy operations */
  193. spin_lock(&card->files_lock);
  194. mfile = card->files;
  195. while (mfile) {
  196. file = mfile->file;
  197. /* it's critical part, use endless loop */
  198. /* we have no room to fail */
  199. s_f_ops = kmalloc(sizeof(struct snd_shutdown_f_ops), GFP_ATOMIC);
  200. if (s_f_ops == NULL)
  201. panic("Atomic allocation failed for snd_shutdown_f_ops!");
  202. f_ops = &s_f_ops->f_ops;
  203. memset(f_ops, 0, sizeof(*f_ops));
  204. f_ops->owner = file->f_op->owner;
  205. f_ops->release = file->f_op->release;
  206. f_ops->poll = snd_disconnect_poll;
  207. s_f_ops->next = card->s_f_ops;
  208. card->s_f_ops = s_f_ops;
  209. f_ops = fops_get(f_ops);
  210. old_f_ops = file->f_op;
  211. file->f_op = f_ops; /* must be atomic */
  212. fops_put(old_f_ops);
  213. mfile = mfile->next;
  214. }
  215. spin_unlock(&card->files_lock);
  216. /* phase 3: notify all connected devices about disconnection */
  217. /* at this point, they cannot respond to any calls except release() */
  218. #if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
  219. if (snd_mixer_oss_notify_callback)
  220. snd_mixer_oss_notify_callback(card, SND_MIXER_OSS_NOTIFY_DISCONNECT);
  221. #endif
  222. /* notify all devices that we are disconnected */
  223. err = snd_device_disconnect_all(card);
  224. if (err < 0)
  225. snd_printk(KERN_ERR "not all devices for card %i can be disconnected\n", card->number);
  226. return 0;
  227. }
  228. /**
  229. * snd_card_free - frees given soundcard structure
  230. * @card: soundcard structure
  231. *
  232. * This function releases the soundcard structure and the all assigned
  233. * devices automatically. That is, you don't have to release the devices
  234. * by yourself.
  235. *
  236. * Returns zero. Frees all associated devices and frees the control
  237. * interface associated to given soundcard.
  238. */
  239. int snd_card_free(struct snd_card *card)
  240. {
  241. struct snd_shutdown_f_ops *s_f_ops;
  242. if (card == NULL)
  243. return -EINVAL;
  244. write_lock(&snd_card_rwlock);
  245. snd_cards[card->number] = NULL;
  246. write_unlock(&snd_card_rwlock);
  247. #ifdef CONFIG_PM
  248. wake_up(&card->power_sleep);
  249. #endif
  250. /* wait, until all devices are ready for the free operation */
  251. wait_event(card->shutdown_sleep, card->files == NULL);
  252. #if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
  253. if (snd_mixer_oss_notify_callback)
  254. snd_mixer_oss_notify_callback(card, SND_MIXER_OSS_NOTIFY_FREE);
  255. #endif
  256. if (snd_device_free_all(card, SNDRV_DEV_CMD_PRE) < 0) {
  257. snd_printk(KERN_ERR "unable to free all devices (pre)\n");
  258. /* Fatal, but this situation should never occur */
  259. }
  260. if (snd_device_free_all(card, SNDRV_DEV_CMD_NORMAL) < 0) {
  261. snd_printk(KERN_ERR "unable to free all devices (normal)\n");
  262. /* Fatal, but this situation should never occur */
  263. }
  264. if (snd_device_free_all(card, SNDRV_DEV_CMD_POST) < 0) {
  265. snd_printk(KERN_ERR "unable to free all devices (post)\n");
  266. /* Fatal, but this situation should never occur */
  267. }
  268. if (card->private_free)
  269. card->private_free(card);
  270. snd_info_unregister(card->proc_id);
  271. if (snd_info_card_free(card) < 0) {
  272. snd_printk(KERN_WARNING "unable to free card info\n");
  273. /* Not fatal error */
  274. }
  275. while (card->s_f_ops) {
  276. s_f_ops = card->s_f_ops;
  277. card->s_f_ops = s_f_ops->next;
  278. kfree(s_f_ops);
  279. }
  280. write_lock(&snd_card_rwlock);
  281. snd_cards_lock &= ~(1 << card->number);
  282. write_unlock(&snd_card_rwlock);
  283. kfree(card);
  284. return 0;
  285. }
  286. static void snd_card_free_thread(void * __card)
  287. {
  288. struct snd_card *card = __card;
  289. struct module * module = card->module;
  290. if (!try_module_get(module)) {
  291. snd_printk(KERN_ERR "unable to lock toplevel module for card %i in free thread\n", card->number);
  292. module = NULL;
  293. }
  294. snd_card_free(card);
  295. module_put(module);
  296. }
  297. /**
  298. * snd_card_free_in_thread - call snd_card_free() in thread
  299. * @card: soundcard structure
  300. *
  301. * This function schedules the call of snd_card_free() function in a
  302. * work queue. When all devices are released (non-busy), the work
  303. * is woken up and calls snd_card_free().
  304. *
  305. * When a card can be disconnected at any time by hotplug service,
  306. * this function should be used in disconnect (or detach) callback
  307. * instead of calling snd_card_free() directly.
  308. *
  309. * Returns - zero otherwise a negative error code if the start of thread failed.
  310. */
  311. int snd_card_free_in_thread(struct snd_card *card)
  312. {
  313. if (card->files == NULL) {
  314. snd_card_free(card);
  315. return 0;
  316. }
  317. if (schedule_work(&card->free_workq))
  318. return 0;
  319. snd_printk(KERN_ERR "schedule_work() failed in snd_card_free_in_thread for card %i\n", card->number);
  320. /* try to free the structure immediately */
  321. snd_card_free(card);
  322. return -EFAULT;
  323. }
  324. static void choose_default_id(struct snd_card *card)
  325. {
  326. int i, len, idx_flag = 0, loops = SNDRV_CARDS;
  327. char *id, *spos;
  328. id = spos = card->shortname;
  329. while (*id != '\0') {
  330. if (*id == ' ')
  331. spos = id + 1;
  332. id++;
  333. }
  334. id = card->id;
  335. while (*spos != '\0' && !isalnum(*spos))
  336. spos++;
  337. if (isdigit(*spos))
  338. *id++ = isalpha(card->shortname[0]) ? card->shortname[0] : 'D';
  339. while (*spos != '\0' && (size_t)(id - card->id) < sizeof(card->id) - 1) {
  340. if (isalnum(*spos))
  341. *id++ = *spos;
  342. spos++;
  343. }
  344. *id = '\0';
  345. id = card->id;
  346. if (*id == '\0')
  347. strcpy(id, "default");
  348. while (1) {
  349. if (loops-- == 0) {
  350. snd_printk(KERN_ERR "unable to choose default card id (%s)\n", id);
  351. strcpy(card->id, card->proc_root->name);
  352. return;
  353. }
  354. if (!snd_info_check_reserved_words(id))
  355. goto __change;
  356. for (i = 0; i < snd_ecards_limit; i++) {
  357. if (snd_cards[i] && !strcmp(snd_cards[i]->id, id))
  358. goto __change;
  359. }
  360. break;
  361. __change:
  362. len = strlen(id);
  363. if (idx_flag) {
  364. if (id[len-1] != '9')
  365. id[len-1]++;
  366. else
  367. id[len-1] = 'A';
  368. } else if ((size_t)len <= sizeof(card->id) - 3) {
  369. strcat(id, "_1");
  370. idx_flag++;
  371. } else {
  372. spos = id + len - 2;
  373. if ((size_t)len <= sizeof(card->id) - 2)
  374. spos++;
  375. *spos++ = '_';
  376. *spos++ = '1';
  377. *spos++ = '\0';
  378. idx_flag++;
  379. }
  380. }
  381. }
  382. /**
  383. * snd_card_register - register the soundcard
  384. * @card: soundcard structure
  385. *
  386. * This function registers all the devices assigned to the soundcard.
  387. * Until calling this, the ALSA control interface is blocked from the
  388. * external accesses. Thus, you should call this function at the end
  389. * of the initialization of the card.
  390. *
  391. * Returns zero otherwise a negative error code if the registrain failed.
  392. */
  393. int snd_card_register(struct snd_card *card)
  394. {
  395. int err;
  396. snd_assert(card != NULL, return -EINVAL);
  397. if ((err = snd_device_register_all(card)) < 0)
  398. return err;
  399. write_lock(&snd_card_rwlock);
  400. if (snd_cards[card->number]) {
  401. /* already registered */
  402. write_unlock(&snd_card_rwlock);
  403. return 0;
  404. }
  405. if (card->id[0] == '\0')
  406. choose_default_id(card);
  407. snd_cards[card->number] = card;
  408. write_unlock(&snd_card_rwlock);
  409. init_info_for_card(card);
  410. #if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
  411. if (snd_mixer_oss_notify_callback)
  412. snd_mixer_oss_notify_callback(card, SND_MIXER_OSS_NOTIFY_REGISTER);
  413. #endif
  414. return 0;
  415. }
  416. #ifdef CONFIG_PROC_FS
  417. static struct snd_info_entry *snd_card_info_entry = NULL;
  418. static void snd_card_info_read(struct snd_info_entry *entry,
  419. struct snd_info_buffer *buffer)
  420. {
  421. int idx, count;
  422. struct snd_card *card;
  423. for (idx = count = 0; idx < SNDRV_CARDS; idx++) {
  424. read_lock(&snd_card_rwlock);
  425. if ((card = snd_cards[idx]) != NULL) {
  426. count++;
  427. snd_iprintf(buffer, "%2i [%-15s]: %s - %s\n",
  428. idx,
  429. card->id,
  430. card->driver,
  431. card->shortname);
  432. snd_iprintf(buffer, " %s\n",
  433. card->longname);
  434. }
  435. read_unlock(&snd_card_rwlock);
  436. }
  437. if (!count)
  438. snd_iprintf(buffer, "--- no soundcards ---\n");
  439. }
  440. #ifdef CONFIG_SND_OSSEMUL
  441. void snd_card_info_read_oss(struct snd_info_buffer *buffer)
  442. {
  443. int idx, count;
  444. struct snd_card *card;
  445. for (idx = count = 0; idx < SNDRV_CARDS; idx++) {
  446. read_lock(&snd_card_rwlock);
  447. if ((card = snd_cards[idx]) != NULL) {
  448. count++;
  449. snd_iprintf(buffer, "%s\n", card->longname);
  450. }
  451. read_unlock(&snd_card_rwlock);
  452. }
  453. if (!count) {
  454. snd_iprintf(buffer, "--- no soundcards ---\n");
  455. }
  456. }
  457. #endif
  458. #ifdef MODULE
  459. static struct snd_info_entry *snd_card_module_info_entry;
  460. static void snd_card_module_info_read(struct snd_info_entry *entry,
  461. struct snd_info_buffer *buffer)
  462. {
  463. int idx;
  464. struct snd_card *card;
  465. for (idx = 0; idx < SNDRV_CARDS; idx++) {
  466. read_lock(&snd_card_rwlock);
  467. if ((card = snd_cards[idx]) != NULL)
  468. snd_iprintf(buffer, "%2i %s\n",
  469. idx, card->module->name);
  470. read_unlock(&snd_card_rwlock);
  471. }
  472. }
  473. #endif
  474. int __init snd_card_info_init(void)
  475. {
  476. struct snd_info_entry *entry;
  477. entry = snd_info_create_module_entry(THIS_MODULE, "cards", NULL);
  478. if (! entry)
  479. return -ENOMEM;
  480. entry->c.text.read_size = PAGE_SIZE;
  481. entry->c.text.read = snd_card_info_read;
  482. if (snd_info_register(entry) < 0) {
  483. snd_info_free_entry(entry);
  484. return -ENOMEM;
  485. }
  486. snd_card_info_entry = entry;
  487. #ifdef MODULE
  488. entry = snd_info_create_module_entry(THIS_MODULE, "modules", NULL);
  489. if (entry) {
  490. entry->c.text.read_size = PAGE_SIZE;
  491. entry->c.text.read = snd_card_module_info_read;
  492. if (snd_info_register(entry) < 0)
  493. snd_info_free_entry(entry);
  494. else
  495. snd_card_module_info_entry = entry;
  496. }
  497. #endif
  498. return 0;
  499. }
  500. int __exit snd_card_info_done(void)
  501. {
  502. snd_info_unregister(snd_card_info_entry);
  503. #ifdef MODULE
  504. snd_info_unregister(snd_card_module_info_entry);
  505. #endif
  506. return 0;
  507. }
  508. #endif /* CONFIG_PROC_FS */
  509. /**
  510. * snd_component_add - add a component string
  511. * @card: soundcard structure
  512. * @component: the component id string
  513. *
  514. * This function adds the component id string to the supported list.
  515. * The component can be referred from the alsa-lib.
  516. *
  517. * Returns zero otherwise a negative error code.
  518. */
  519. int snd_component_add(struct snd_card *card, const char *component)
  520. {
  521. char *ptr;
  522. int len = strlen(component);
  523. ptr = strstr(card->components, component);
  524. if (ptr != NULL) {
  525. if (ptr[len] == '\0' || ptr[len] == ' ') /* already there */
  526. return 1;
  527. }
  528. if (strlen(card->components) + 1 + len + 1 > sizeof(card->components)) {
  529. snd_BUG();
  530. return -ENOMEM;
  531. }
  532. if (card->components[0] != '\0')
  533. strcat(card->components, " ");
  534. strcat(card->components, component);
  535. return 0;
  536. }
  537. /**
  538. * snd_card_file_add - add the file to the file list of the card
  539. * @card: soundcard structure
  540. * @file: file pointer
  541. *
  542. * This function adds the file to the file linked-list of the card.
  543. * This linked-list is used to keep tracking the connection state,
  544. * and to avoid the release of busy resources by hotplug.
  545. *
  546. * Returns zero or a negative error code.
  547. */
  548. int snd_card_file_add(struct snd_card *card, struct file *file)
  549. {
  550. struct snd_monitor_file *mfile;
  551. mfile = kmalloc(sizeof(*mfile), GFP_KERNEL);
  552. if (mfile == NULL)
  553. return -ENOMEM;
  554. mfile->file = file;
  555. mfile->next = NULL;
  556. spin_lock(&card->files_lock);
  557. if (card->shutdown) {
  558. spin_unlock(&card->files_lock);
  559. kfree(mfile);
  560. return -ENODEV;
  561. }
  562. mfile->next = card->files;
  563. card->files = mfile;
  564. spin_unlock(&card->files_lock);
  565. return 0;
  566. }
  567. /**
  568. * snd_card_file_remove - remove the file from the file list
  569. * @card: soundcard structure
  570. * @file: file pointer
  571. *
  572. * This function removes the file formerly added to the card via
  573. * snd_card_file_add() function.
  574. * If all files are removed and the release of the card is
  575. * scheduled, it will wake up the the thread to call snd_card_free()
  576. * (see snd_card_free_in_thread() function).
  577. *
  578. * Returns zero or a negative error code.
  579. */
  580. int snd_card_file_remove(struct snd_card *card, struct file *file)
  581. {
  582. struct snd_monitor_file *mfile, *pfile = NULL;
  583. spin_lock(&card->files_lock);
  584. mfile = card->files;
  585. while (mfile) {
  586. if (mfile->file == file) {
  587. if (pfile)
  588. pfile->next = mfile->next;
  589. else
  590. card->files = mfile->next;
  591. break;
  592. }
  593. pfile = mfile;
  594. mfile = mfile->next;
  595. }
  596. spin_unlock(&card->files_lock);
  597. if (card->files == NULL)
  598. wake_up(&card->shutdown_sleep);
  599. if (!mfile) {
  600. snd_printk(KERN_ERR "ALSA card file remove problem (%p)\n", file);
  601. return -ENOENT;
  602. }
  603. kfree(mfile);
  604. return 0;
  605. }
  606. #ifdef CONFIG_PM
  607. /**
  608. * snd_power_wait - wait until the power-state is changed.
  609. * @card: soundcard structure
  610. * @power_state: expected power state
  611. * @file: file structure for the O_NONBLOCK check (optional)
  612. *
  613. * Waits until the power-state is changed.
  614. *
  615. * Note: the power lock must be active before call.
  616. */
  617. int snd_power_wait(struct snd_card *card, unsigned int power_state, struct file *file)
  618. {
  619. wait_queue_t wait;
  620. int result = 0;
  621. /* fastpath */
  622. if (snd_power_get_state(card) == power_state)
  623. return 0;
  624. init_waitqueue_entry(&wait, current);
  625. add_wait_queue(&card->power_sleep, &wait);
  626. while (1) {
  627. if (card->shutdown) {
  628. result = -ENODEV;
  629. break;
  630. }
  631. if (snd_power_get_state(card) == power_state)
  632. break;
  633. #if 0 /* block all devices */
  634. if (file && (file->f_flags & O_NONBLOCK)) {
  635. result = -EAGAIN;
  636. break;
  637. }
  638. #endif
  639. set_current_state(TASK_UNINTERRUPTIBLE);
  640. snd_power_unlock(card);
  641. schedule_timeout(30 * HZ);
  642. snd_power_lock(card);
  643. }
  644. remove_wait_queue(&card->power_sleep, &wait);
  645. return result;
  646. }
  647. #endif /* CONFIG_PM */