init.c 19 KB

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