init.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794
  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. if (card->dev)
  322. device_unregister(card->dev);
  323. kfree(card);
  324. return 0;
  325. }
  326. static int snd_card_free_prepare(struct snd_card *card)
  327. {
  328. if (card == NULL)
  329. return -EINVAL;
  330. (void) snd_card_disconnect(card);
  331. mutex_lock(&snd_card_mutex);
  332. snd_cards[card->number] = NULL;
  333. snd_cards_lock &= ~(1 << card->number);
  334. mutex_unlock(&snd_card_mutex);
  335. #ifdef CONFIG_PM
  336. wake_up(&card->power_sleep);
  337. #endif
  338. return 0;
  339. }
  340. int snd_card_free_when_closed(struct snd_card *card)
  341. {
  342. int free_now = 0;
  343. int ret = snd_card_free_prepare(card);
  344. if (ret)
  345. return ret;
  346. spin_lock(&card->files_lock);
  347. if (card->files == NULL)
  348. free_now = 1;
  349. else
  350. card->free_on_last_close = 1;
  351. spin_unlock(&card->files_lock);
  352. if (free_now)
  353. snd_card_do_free(card);
  354. return 0;
  355. }
  356. EXPORT_SYMBOL(snd_card_free_when_closed);
  357. int snd_card_free(struct snd_card *card)
  358. {
  359. int ret = snd_card_free_prepare(card);
  360. if (ret)
  361. return ret;
  362. /* wait, until all devices are ready for the free operation */
  363. wait_event(card->shutdown_sleep, card->files == NULL);
  364. snd_card_do_free(card);
  365. return 0;
  366. }
  367. EXPORT_SYMBOL(snd_card_free);
  368. static void choose_default_id(struct snd_card *card)
  369. {
  370. int i, len, idx_flag = 0, loops = SNDRV_CARDS;
  371. char *id, *spos;
  372. id = spos = card->shortname;
  373. while (*id != '\0') {
  374. if (*id == ' ')
  375. spos = id + 1;
  376. id++;
  377. }
  378. id = card->id;
  379. while (*spos != '\0' && !isalnum(*spos))
  380. spos++;
  381. if (isdigit(*spos))
  382. *id++ = isalpha(card->shortname[0]) ? card->shortname[0] : 'D';
  383. while (*spos != '\0' && (size_t)(id - card->id) < sizeof(card->id) - 1) {
  384. if (isalnum(*spos))
  385. *id++ = *spos;
  386. spos++;
  387. }
  388. *id = '\0';
  389. id = card->id;
  390. if (*id == '\0')
  391. strcpy(id, "default");
  392. while (1) {
  393. if (loops-- == 0) {
  394. snd_printk(KERN_ERR "unable to choose default card id (%s)\n", id);
  395. strcpy(card->id, card->proc_root->name);
  396. return;
  397. }
  398. if (!snd_info_check_reserved_words(id))
  399. goto __change;
  400. for (i = 0; i < snd_ecards_limit; i++) {
  401. if (snd_cards[i] && !strcmp(snd_cards[i]->id, id))
  402. goto __change;
  403. }
  404. break;
  405. __change:
  406. len = strlen(id);
  407. if (idx_flag) {
  408. if (id[len-1] != '9')
  409. id[len-1]++;
  410. else
  411. id[len-1] = 'A';
  412. } else if ((size_t)len <= sizeof(card->id) - 3) {
  413. strcat(id, "_1");
  414. idx_flag++;
  415. } else {
  416. spos = id + len - 2;
  417. if ((size_t)len <= sizeof(card->id) - 2)
  418. spos++;
  419. *spos++ = '_';
  420. *spos++ = '1';
  421. *spos++ = '\0';
  422. idx_flag++;
  423. }
  424. }
  425. }
  426. /**
  427. * snd_card_register - register the soundcard
  428. * @card: soundcard structure
  429. *
  430. * This function registers all the devices assigned to the soundcard.
  431. * Until calling this, the ALSA control interface is blocked from the
  432. * external accesses. Thus, you should call this function at the end
  433. * of the initialization of the card.
  434. *
  435. * Returns zero otherwise a negative error code if the registrain failed.
  436. */
  437. int snd_card_register(struct snd_card *card)
  438. {
  439. int err;
  440. snd_assert(card != NULL, return -EINVAL);
  441. if (!card->dev) {
  442. card->dev = device_create(sound_class, card->parent, 0,
  443. "card%i", card->number);
  444. if (IS_ERR(card->dev))
  445. card->dev = NULL;
  446. }
  447. if ((err = snd_device_register_all(card)) < 0)
  448. return err;
  449. mutex_lock(&snd_card_mutex);
  450. if (snd_cards[card->number]) {
  451. /* already registered */
  452. mutex_unlock(&snd_card_mutex);
  453. return 0;
  454. }
  455. if (card->id[0] == '\0')
  456. choose_default_id(card);
  457. snd_cards[card->number] = card;
  458. mutex_unlock(&snd_card_mutex);
  459. init_info_for_card(card);
  460. #if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
  461. if (snd_mixer_oss_notify_callback)
  462. snd_mixer_oss_notify_callback(card, SND_MIXER_OSS_NOTIFY_REGISTER);
  463. #endif
  464. return 0;
  465. }
  466. EXPORT_SYMBOL(snd_card_register);
  467. #ifdef CONFIG_PROC_FS
  468. static struct snd_info_entry *snd_card_info_entry;
  469. static void snd_card_info_read(struct snd_info_entry *entry,
  470. struct snd_info_buffer *buffer)
  471. {
  472. int idx, count;
  473. struct snd_card *card;
  474. for (idx = count = 0; idx < SNDRV_CARDS; idx++) {
  475. mutex_lock(&snd_card_mutex);
  476. if ((card = snd_cards[idx]) != NULL) {
  477. count++;
  478. snd_iprintf(buffer, "%2i [%-15s]: %s - %s\n",
  479. idx,
  480. card->id,
  481. card->driver,
  482. card->shortname);
  483. snd_iprintf(buffer, " %s\n",
  484. card->longname);
  485. }
  486. mutex_unlock(&snd_card_mutex);
  487. }
  488. if (!count)
  489. snd_iprintf(buffer, "--- no soundcards ---\n");
  490. }
  491. #ifdef CONFIG_SND_OSSEMUL
  492. void snd_card_info_read_oss(struct snd_info_buffer *buffer)
  493. {
  494. int idx, count;
  495. struct snd_card *card;
  496. for (idx = count = 0; idx < SNDRV_CARDS; idx++) {
  497. mutex_lock(&snd_card_mutex);
  498. if ((card = snd_cards[idx]) != NULL) {
  499. count++;
  500. snd_iprintf(buffer, "%s\n", card->longname);
  501. }
  502. mutex_unlock(&snd_card_mutex);
  503. }
  504. if (!count) {
  505. snd_iprintf(buffer, "--- no soundcards ---\n");
  506. }
  507. }
  508. #endif
  509. #ifdef MODULE
  510. static struct snd_info_entry *snd_card_module_info_entry;
  511. static void snd_card_module_info_read(struct snd_info_entry *entry,
  512. struct snd_info_buffer *buffer)
  513. {
  514. int idx;
  515. struct snd_card *card;
  516. for (idx = 0; idx < SNDRV_CARDS; idx++) {
  517. mutex_lock(&snd_card_mutex);
  518. if ((card = snd_cards[idx]) != NULL)
  519. snd_iprintf(buffer, "%2i %s\n",
  520. idx, card->module->name);
  521. mutex_unlock(&snd_card_mutex);
  522. }
  523. }
  524. #endif
  525. int __init snd_card_info_init(void)
  526. {
  527. struct snd_info_entry *entry;
  528. entry = snd_info_create_module_entry(THIS_MODULE, "cards", NULL);
  529. if (! entry)
  530. return -ENOMEM;
  531. entry->c.text.read = snd_card_info_read;
  532. if (snd_info_register(entry) < 0) {
  533. snd_info_free_entry(entry);
  534. return -ENOMEM;
  535. }
  536. snd_card_info_entry = entry;
  537. #ifdef MODULE
  538. entry = snd_info_create_module_entry(THIS_MODULE, "modules", NULL);
  539. if (entry) {
  540. entry->c.text.read = snd_card_module_info_read;
  541. if (snd_info_register(entry) < 0)
  542. snd_info_free_entry(entry);
  543. else
  544. snd_card_module_info_entry = entry;
  545. }
  546. #endif
  547. return 0;
  548. }
  549. int __exit snd_card_info_done(void)
  550. {
  551. snd_info_free_entry(snd_card_info_entry);
  552. #ifdef MODULE
  553. snd_info_free_entry(snd_card_module_info_entry);
  554. #endif
  555. return 0;
  556. }
  557. #endif /* CONFIG_PROC_FS */
  558. /**
  559. * snd_component_add - add a component string
  560. * @card: soundcard structure
  561. * @component: the component id string
  562. *
  563. * This function adds the component id string to the supported list.
  564. * The component can be referred from the alsa-lib.
  565. *
  566. * Returns zero otherwise a negative error code.
  567. */
  568. int snd_component_add(struct snd_card *card, const char *component)
  569. {
  570. char *ptr;
  571. int len = strlen(component);
  572. ptr = strstr(card->components, component);
  573. if (ptr != NULL) {
  574. if (ptr[len] == '\0' || ptr[len] == ' ') /* already there */
  575. return 1;
  576. }
  577. if (strlen(card->components) + 1 + len + 1 > sizeof(card->components)) {
  578. snd_BUG();
  579. return -ENOMEM;
  580. }
  581. if (card->components[0] != '\0')
  582. strcat(card->components, " ");
  583. strcat(card->components, component);
  584. return 0;
  585. }
  586. EXPORT_SYMBOL(snd_component_add);
  587. /**
  588. * snd_card_file_add - add the file to the file list of the card
  589. * @card: soundcard structure
  590. * @file: file pointer
  591. *
  592. * This function adds the file to the file linked-list of the card.
  593. * This linked-list is used to keep tracking the connection state,
  594. * and to avoid the release of busy resources by hotplug.
  595. *
  596. * Returns zero or a negative error code.
  597. */
  598. int snd_card_file_add(struct snd_card *card, struct file *file)
  599. {
  600. struct snd_monitor_file *mfile;
  601. mfile = kmalloc(sizeof(*mfile), GFP_KERNEL);
  602. if (mfile == NULL)
  603. return -ENOMEM;
  604. mfile->file = file;
  605. mfile->disconnected_f_op = NULL;
  606. mfile->next = NULL;
  607. spin_lock(&card->files_lock);
  608. if (card->shutdown) {
  609. spin_unlock(&card->files_lock);
  610. kfree(mfile);
  611. return -ENODEV;
  612. }
  613. mfile->next = card->files;
  614. card->files = mfile;
  615. spin_unlock(&card->files_lock);
  616. return 0;
  617. }
  618. EXPORT_SYMBOL(snd_card_file_add);
  619. /**
  620. * snd_card_file_remove - remove the file from the file list
  621. * @card: soundcard structure
  622. * @file: file pointer
  623. *
  624. * This function removes the file formerly added to the card via
  625. * snd_card_file_add() function.
  626. * If all files are removed and snd_card_free_when_closed() was
  627. * called beforehand, it processes the pending release of
  628. * resources.
  629. *
  630. * Returns zero or a negative error code.
  631. */
  632. int snd_card_file_remove(struct snd_card *card, struct file *file)
  633. {
  634. struct snd_monitor_file *mfile, *pfile = NULL;
  635. int last_close = 0;
  636. spin_lock(&card->files_lock);
  637. mfile = card->files;
  638. while (mfile) {
  639. if (mfile->file == file) {
  640. if (pfile)
  641. pfile->next = mfile->next;
  642. else
  643. card->files = mfile->next;
  644. break;
  645. }
  646. pfile = mfile;
  647. mfile = mfile->next;
  648. }
  649. if (mfile && mfile->disconnected_f_op) {
  650. fops_put(mfile->disconnected_f_op);
  651. spin_lock(&shutdown_lock);
  652. list_del(&mfile->shutdown_list);
  653. spin_unlock(&shutdown_lock);
  654. }
  655. if (card->files == NULL)
  656. last_close = 1;
  657. spin_unlock(&card->files_lock);
  658. if (last_close) {
  659. wake_up(&card->shutdown_sleep);
  660. if (card->free_on_last_close)
  661. snd_card_do_free(card);
  662. }
  663. if (!mfile) {
  664. snd_printk(KERN_ERR "ALSA card file remove problem (%p)\n", file);
  665. return -ENOENT;
  666. }
  667. kfree(mfile);
  668. return 0;
  669. }
  670. EXPORT_SYMBOL(snd_card_file_remove);
  671. #ifdef CONFIG_PM
  672. /**
  673. * snd_power_wait - wait until the power-state is changed.
  674. * @card: soundcard structure
  675. * @power_state: expected power state
  676. *
  677. * Waits until the power-state is changed.
  678. *
  679. * Note: the power lock must be active before call.
  680. */
  681. int snd_power_wait(struct snd_card *card, unsigned int power_state)
  682. {
  683. wait_queue_t wait;
  684. int result = 0;
  685. /* fastpath */
  686. if (snd_power_get_state(card) == power_state)
  687. return 0;
  688. init_waitqueue_entry(&wait, current);
  689. add_wait_queue(&card->power_sleep, &wait);
  690. while (1) {
  691. if (card->shutdown) {
  692. result = -ENODEV;
  693. break;
  694. }
  695. if (snd_power_get_state(card) == power_state)
  696. break;
  697. set_current_state(TASK_UNINTERRUPTIBLE);
  698. snd_power_unlock(card);
  699. schedule_timeout(30 * HZ);
  700. snd_power_lock(card);
  701. }
  702. remove_wait_queue(&card->power_sleep, &wait);
  703. return result;
  704. }
  705. EXPORT_SYMBOL(snd_power_wait);
  706. #endif /* CONFIG_PM */