init.c 20 KB

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