init.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  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 = 8;
  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. id[len-1]++;
  341. else if ((size_t)len <= sizeof(card->id) - 3) {
  342. strcat(id, "_1");
  343. idx_flag++;
  344. } else {
  345. spos = id + len - 2;
  346. if ((size_t)len <= sizeof(card->id) - 2)
  347. spos++;
  348. *spos++ = '_';
  349. *spos++ = '1';
  350. *spos++ = '\0';
  351. idx_flag++;
  352. }
  353. }
  354. }
  355. /**
  356. * snd_card_register - register the soundcard
  357. * @card: soundcard structure
  358. *
  359. * This function registers all the devices assigned to the soundcard.
  360. * Until calling this, the ALSA control interface is blocked from the
  361. * external accesses. Thus, you should call this function at the end
  362. * of the initialization of the card.
  363. *
  364. * Returns zero otherwise a negative error code if the registrain failed.
  365. */
  366. int snd_card_register(struct snd_card *card)
  367. {
  368. int err;
  369. struct snd_info_entry *entry;
  370. snd_assert(card != NULL, return -EINVAL);
  371. if ((err = snd_device_register_all(card)) < 0)
  372. return err;
  373. write_lock(&snd_card_rwlock);
  374. if (snd_cards[card->number]) {
  375. /* already registered */
  376. write_unlock(&snd_card_rwlock);
  377. return 0;
  378. }
  379. if (card->id[0] == '\0')
  380. choose_default_id(card);
  381. snd_cards[card->number] = card;
  382. write_unlock(&snd_card_rwlock);
  383. if ((err = snd_info_card_register(card)) < 0) {
  384. snd_printd("unable to create card info\n");
  385. goto __skip_info;
  386. }
  387. if ((entry = snd_info_create_card_entry(card, "id", card->proc_root)) == NULL) {
  388. snd_printd("unable to create card entry\n");
  389. goto __skip_info;
  390. }
  391. entry->c.text.read_size = PAGE_SIZE;
  392. entry->c.text.read = snd_card_id_read;
  393. if (snd_info_register(entry) < 0) {
  394. snd_info_free_entry(entry);
  395. entry = NULL;
  396. }
  397. card->proc_id = entry;
  398. __skip_info:
  399. #if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
  400. if (snd_mixer_oss_notify_callback)
  401. snd_mixer_oss_notify_callback(card, SND_MIXER_OSS_NOTIFY_REGISTER);
  402. #endif
  403. return 0;
  404. }
  405. static struct snd_info_entry *snd_card_info_entry = NULL;
  406. static void snd_card_info_read(struct snd_info_entry *entry,
  407. struct snd_info_buffer *buffer)
  408. {
  409. int idx, count;
  410. struct snd_card *card;
  411. for (idx = count = 0; idx < SNDRV_CARDS; idx++) {
  412. read_lock(&snd_card_rwlock);
  413. if ((card = snd_cards[idx]) != NULL) {
  414. count++;
  415. snd_iprintf(buffer, "%i [%-15s]: %s - %s\n",
  416. idx,
  417. card->id,
  418. card->driver,
  419. card->shortname);
  420. snd_iprintf(buffer, " %s\n",
  421. card->longname);
  422. }
  423. read_unlock(&snd_card_rwlock);
  424. }
  425. if (!count)
  426. snd_iprintf(buffer, "--- no soundcards ---\n");
  427. }
  428. #if defined(CONFIG_SND_OSSEMUL) && defined(CONFIG_PROC_FS)
  429. void snd_card_info_read_oss(struct snd_info_buffer *buffer)
  430. {
  431. int idx, count;
  432. struct snd_card *card;
  433. for (idx = count = 0; idx < SNDRV_CARDS; idx++) {
  434. read_lock(&snd_card_rwlock);
  435. if ((card = snd_cards[idx]) != NULL) {
  436. count++;
  437. snd_iprintf(buffer, "%s\n", card->longname);
  438. }
  439. read_unlock(&snd_card_rwlock);
  440. }
  441. if (!count) {
  442. snd_iprintf(buffer, "--- no soundcards ---\n");
  443. }
  444. }
  445. #endif
  446. #ifdef MODULE
  447. static struct snd_info_entry *snd_card_module_info_entry;
  448. static void snd_card_module_info_read(struct snd_info_entry *entry,
  449. struct snd_info_buffer *buffer)
  450. {
  451. int idx;
  452. struct snd_card *card;
  453. for (idx = 0; idx < SNDRV_CARDS; idx++) {
  454. read_lock(&snd_card_rwlock);
  455. if ((card = snd_cards[idx]) != NULL)
  456. snd_iprintf(buffer, "%i %s\n", idx, card->module->name);
  457. read_unlock(&snd_card_rwlock);
  458. }
  459. }
  460. #endif
  461. int __init snd_card_info_init(void)
  462. {
  463. struct snd_info_entry *entry;
  464. entry = snd_info_create_module_entry(THIS_MODULE, "cards", NULL);
  465. if (! entry)
  466. return -ENOMEM;
  467. entry->c.text.read_size = PAGE_SIZE;
  468. entry->c.text.read = snd_card_info_read;
  469. if (snd_info_register(entry) < 0) {
  470. snd_info_free_entry(entry);
  471. return -ENOMEM;
  472. }
  473. snd_card_info_entry = entry;
  474. #ifdef MODULE
  475. entry = snd_info_create_module_entry(THIS_MODULE, "modules", NULL);
  476. if (entry) {
  477. entry->c.text.read_size = PAGE_SIZE;
  478. entry->c.text.read = snd_card_module_info_read;
  479. if (snd_info_register(entry) < 0)
  480. snd_info_free_entry(entry);
  481. else
  482. snd_card_module_info_entry = entry;
  483. }
  484. #endif
  485. return 0;
  486. }
  487. int __exit snd_card_info_done(void)
  488. {
  489. if (snd_card_info_entry)
  490. snd_info_unregister(snd_card_info_entry);
  491. #ifdef MODULE
  492. if (snd_card_module_info_entry)
  493. snd_info_unregister(snd_card_module_info_entry);
  494. #endif
  495. return 0;
  496. }
  497. /**
  498. * snd_component_add - add a component string
  499. * @card: soundcard structure
  500. * @component: the component id string
  501. *
  502. * This function adds the component id string to the supported list.
  503. * The component can be referred from the alsa-lib.
  504. *
  505. * Returns zero otherwise a negative error code.
  506. */
  507. int snd_component_add(struct snd_card *card, const char *component)
  508. {
  509. char *ptr;
  510. int len = strlen(component);
  511. ptr = strstr(card->components, component);
  512. if (ptr != NULL) {
  513. if (ptr[len] == '\0' || ptr[len] == ' ') /* already there */
  514. return 1;
  515. }
  516. if (strlen(card->components) + 1 + len + 1 > sizeof(card->components)) {
  517. snd_BUG();
  518. return -ENOMEM;
  519. }
  520. if (card->components[0] != '\0')
  521. strcat(card->components, " ");
  522. strcat(card->components, component);
  523. return 0;
  524. }
  525. /**
  526. * snd_card_file_add - add the file to the file list of the card
  527. * @card: soundcard structure
  528. * @file: file pointer
  529. *
  530. * This function adds the file to the file linked-list of the card.
  531. * This linked-list is used to keep tracking the connection state,
  532. * and to avoid the release of busy resources by hotplug.
  533. *
  534. * Returns zero or a negative error code.
  535. */
  536. int snd_card_file_add(struct snd_card *card, struct file *file)
  537. {
  538. struct snd_monitor_file *mfile;
  539. mfile = kmalloc(sizeof(*mfile), GFP_KERNEL);
  540. if (mfile == NULL)
  541. return -ENOMEM;
  542. mfile->file = file;
  543. mfile->next = NULL;
  544. spin_lock(&card->files_lock);
  545. if (card->shutdown) {
  546. spin_unlock(&card->files_lock);
  547. kfree(mfile);
  548. return -ENODEV;
  549. }
  550. mfile->next = card->files;
  551. card->files = mfile;
  552. spin_unlock(&card->files_lock);
  553. return 0;
  554. }
  555. /**
  556. * snd_card_file_remove - remove the file from the file list
  557. * @card: soundcard structure
  558. * @file: file pointer
  559. *
  560. * This function removes the file formerly added to the card via
  561. * snd_card_file_add() function.
  562. * If all files are removed and the release of the card is
  563. * scheduled, it will wake up the the thread to call snd_card_free()
  564. * (see snd_card_free_in_thread() function).
  565. *
  566. * Returns zero or a negative error code.
  567. */
  568. int snd_card_file_remove(struct snd_card *card, struct file *file)
  569. {
  570. struct snd_monitor_file *mfile, *pfile = NULL;
  571. spin_lock(&card->files_lock);
  572. mfile = card->files;
  573. while (mfile) {
  574. if (mfile->file == file) {
  575. if (pfile)
  576. pfile->next = mfile->next;
  577. else
  578. card->files = mfile->next;
  579. break;
  580. }
  581. pfile = mfile;
  582. mfile = mfile->next;
  583. }
  584. spin_unlock(&card->files_lock);
  585. if (card->files == NULL)
  586. wake_up(&card->shutdown_sleep);
  587. if (!mfile) {
  588. snd_printk(KERN_ERR "ALSA card file remove problem (%p)\n", file);
  589. return -ENOENT;
  590. }
  591. kfree(mfile);
  592. return 0;
  593. }
  594. #ifdef CONFIG_PM
  595. /**
  596. * snd_power_wait - wait until the power-state is changed.
  597. * @card: soundcard structure
  598. * @power_state: expected power state
  599. * @file: file structure for the O_NONBLOCK check (optional)
  600. *
  601. * Waits until the power-state is changed.
  602. *
  603. * Note: the power lock must be active before call.
  604. */
  605. int snd_power_wait(struct snd_card *card, unsigned int power_state, struct file *file)
  606. {
  607. wait_queue_t wait;
  608. int result = 0;
  609. /* fastpath */
  610. if (snd_power_get_state(card) == power_state)
  611. return 0;
  612. init_waitqueue_entry(&wait, current);
  613. add_wait_queue(&card->power_sleep, &wait);
  614. while (1) {
  615. if (card->shutdown) {
  616. result = -ENODEV;
  617. break;
  618. }
  619. if (snd_power_get_state(card) == power_state)
  620. break;
  621. #if 0 /* block all devices */
  622. if (file && (file->f_flags & O_NONBLOCK)) {
  623. result = -EAGAIN;
  624. break;
  625. }
  626. #endif
  627. set_current_state(TASK_UNINTERRUPTIBLE);
  628. snd_power_unlock(card);
  629. schedule_timeout(30 * HZ);
  630. snd_power_lock(card);
  631. }
  632. remove_wait_queue(&card->power_sleep, &wait);
  633. return result;
  634. }
  635. #endif /* CONFIG_PM */