init.c 20 KB

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