init.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  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, *old_f_ops;
  207. int err;
  208. spin_lock(&card->files_lock);
  209. if (card->shutdown) {
  210. spin_unlock(&card->files_lock);
  211. return 0;
  212. }
  213. card->shutdown = 1;
  214. spin_unlock(&card->files_lock);
  215. /* phase 1: disable fops (user space) operations for ALSA API */
  216. write_lock(&snd_card_rwlock);
  217. snd_cards[card->number] = NULL;
  218. write_unlock(&snd_card_rwlock);
  219. /* phase 2: replace file->f_op with special dummy operations */
  220. spin_lock(&card->files_lock);
  221. mfile = card->files;
  222. while (mfile) {
  223. file = mfile->file;
  224. /* it's critical part, use endless loop */
  225. /* we have no room to fail */
  226. s_f_ops = kmalloc(sizeof(struct snd_shutdown_f_ops), GFP_ATOMIC);
  227. if (s_f_ops == NULL)
  228. panic("Atomic allocation failed for snd_shutdown_f_ops!");
  229. f_ops = &s_f_ops->f_ops;
  230. memset(f_ops, 0, sizeof(*f_ops));
  231. f_ops->owner = file->f_op->owner;
  232. f_ops->release = file->f_op->release;
  233. f_ops->llseek = snd_disconnect_llseek;
  234. f_ops->read = snd_disconnect_read;
  235. f_ops->write = snd_disconnect_write;
  236. f_ops->poll = snd_disconnect_poll;
  237. f_ops->unlocked_ioctl = snd_disconnect_ioctl;
  238. #ifdef CONFIG_COMPAT
  239. f_ops->compat_ioctl = snd_disconnect_ioctl;
  240. #endif
  241. f_ops->mmap = snd_disconnect_mmap;
  242. f_ops->fasync = snd_disconnect_fasync;
  243. s_f_ops->next = card->s_f_ops;
  244. card->s_f_ops = s_f_ops;
  245. f_ops = fops_get(f_ops);
  246. old_f_ops = file->f_op;
  247. file->f_op = f_ops; /* must be atomic */
  248. fops_put(old_f_ops);
  249. mfile = mfile->next;
  250. }
  251. spin_unlock(&card->files_lock);
  252. /* phase 3: notify all connected devices about disconnection */
  253. /* at this point, they cannot respond to any calls except release() */
  254. #if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
  255. if (snd_mixer_oss_notify_callback)
  256. snd_mixer_oss_notify_callback(card, SND_MIXER_OSS_NOTIFY_DISCONNECT);
  257. #endif
  258. /* notify all devices that we are disconnected */
  259. err = snd_device_disconnect_all(card);
  260. if (err < 0)
  261. snd_printk(KERN_ERR "not all devices for card %i can be disconnected\n", card->number);
  262. return 0;
  263. }
  264. /**
  265. * snd_card_free - frees given soundcard structure
  266. * @card: soundcard structure
  267. *
  268. * This function releases the soundcard structure and the all assigned
  269. * devices automatically. That is, you don't have to release the devices
  270. * by yourself.
  271. *
  272. * Returns zero. Frees all associated devices and frees the control
  273. * interface associated to given soundcard.
  274. */
  275. int snd_card_free(struct snd_card *card)
  276. {
  277. struct snd_shutdown_f_ops *s_f_ops;
  278. if (card == NULL)
  279. return -EINVAL;
  280. write_lock(&snd_card_rwlock);
  281. snd_cards[card->number] = NULL;
  282. write_unlock(&snd_card_rwlock);
  283. #ifdef CONFIG_PM
  284. wake_up(&card->power_sleep);
  285. #endif
  286. /* wait, until all devices are ready for the free operation */
  287. wait_event(card->shutdown_sleep, card->files == NULL);
  288. #if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
  289. if (snd_mixer_oss_notify_callback)
  290. snd_mixer_oss_notify_callback(card, SND_MIXER_OSS_NOTIFY_FREE);
  291. #endif
  292. if (snd_device_free_all(card, SNDRV_DEV_CMD_PRE) < 0) {
  293. snd_printk(KERN_ERR "unable to free all devices (pre)\n");
  294. /* Fatal, but this situation should never occur */
  295. }
  296. if (snd_device_free_all(card, SNDRV_DEV_CMD_NORMAL) < 0) {
  297. snd_printk(KERN_ERR "unable to free all devices (normal)\n");
  298. /* Fatal, but this situation should never occur */
  299. }
  300. if (snd_device_free_all(card, SNDRV_DEV_CMD_POST) < 0) {
  301. snd_printk(KERN_ERR "unable to free all devices (post)\n");
  302. /* Fatal, but this situation should never occur */
  303. }
  304. if (card->private_free)
  305. card->private_free(card);
  306. snd_info_unregister(card->proc_id);
  307. if (snd_info_card_free(card) < 0) {
  308. snd_printk(KERN_WARNING "unable to free card info\n");
  309. /* Not fatal error */
  310. }
  311. while (card->s_f_ops) {
  312. s_f_ops = card->s_f_ops;
  313. card->s_f_ops = s_f_ops->next;
  314. kfree(s_f_ops);
  315. }
  316. write_lock(&snd_card_rwlock);
  317. snd_cards_lock &= ~(1 << card->number);
  318. write_unlock(&snd_card_rwlock);
  319. kfree(card);
  320. return 0;
  321. }
  322. static void snd_card_free_thread(void * __card)
  323. {
  324. struct snd_card *card = __card;
  325. struct module * module = card->module;
  326. if (!try_module_get(module)) {
  327. snd_printk(KERN_ERR "unable to lock toplevel module for card %i in free thread\n", card->number);
  328. module = NULL;
  329. }
  330. snd_card_free(card);
  331. module_put(module);
  332. }
  333. /**
  334. * snd_card_free_in_thread - call snd_card_free() in thread
  335. * @card: soundcard structure
  336. *
  337. * This function schedules the call of snd_card_free() function in a
  338. * work queue. When all devices are released (non-busy), the work
  339. * is woken up and calls snd_card_free().
  340. *
  341. * When a card can be disconnected at any time by hotplug service,
  342. * this function should be used in disconnect (or detach) callback
  343. * instead of calling snd_card_free() directly.
  344. *
  345. * Returns - zero otherwise a negative error code if the start of thread failed.
  346. */
  347. int snd_card_free_in_thread(struct snd_card *card)
  348. {
  349. if (card->files == NULL) {
  350. snd_card_free(card);
  351. return 0;
  352. }
  353. if (schedule_work(&card->free_workq))
  354. return 0;
  355. snd_printk(KERN_ERR "schedule_work() failed in snd_card_free_in_thread for card %i\n", card->number);
  356. /* try to free the structure immediately */
  357. snd_card_free(card);
  358. return -EFAULT;
  359. }
  360. static void choose_default_id(struct snd_card *card)
  361. {
  362. int i, len, idx_flag = 0, loops = SNDRV_CARDS;
  363. char *id, *spos;
  364. id = spos = card->shortname;
  365. while (*id != '\0') {
  366. if (*id == ' ')
  367. spos = id + 1;
  368. id++;
  369. }
  370. id = card->id;
  371. while (*spos != '\0' && !isalnum(*spos))
  372. spos++;
  373. if (isdigit(*spos))
  374. *id++ = isalpha(card->shortname[0]) ? card->shortname[0] : 'D';
  375. while (*spos != '\0' && (size_t)(id - card->id) < sizeof(card->id) - 1) {
  376. if (isalnum(*spos))
  377. *id++ = *spos;
  378. spos++;
  379. }
  380. *id = '\0';
  381. id = card->id;
  382. if (*id == '\0')
  383. strcpy(id, "default");
  384. while (1) {
  385. if (loops-- == 0) {
  386. snd_printk(KERN_ERR "unable to choose default card id (%s)\n", id);
  387. strcpy(card->id, card->proc_root->name);
  388. return;
  389. }
  390. if (!snd_info_check_reserved_words(id))
  391. goto __change;
  392. for (i = 0; i < snd_ecards_limit; i++) {
  393. if (snd_cards[i] && !strcmp(snd_cards[i]->id, id))
  394. goto __change;
  395. }
  396. break;
  397. __change:
  398. len = strlen(id);
  399. if (idx_flag) {
  400. if (id[len-1] != '9')
  401. id[len-1]++;
  402. else
  403. id[len-1] = 'A';
  404. } else if ((size_t)len <= sizeof(card->id) - 3) {
  405. strcat(id, "_1");
  406. idx_flag++;
  407. } else {
  408. spos = id + len - 2;
  409. if ((size_t)len <= sizeof(card->id) - 2)
  410. spos++;
  411. *spos++ = '_';
  412. *spos++ = '1';
  413. *spos++ = '\0';
  414. idx_flag++;
  415. }
  416. }
  417. }
  418. /**
  419. * snd_card_register - register the soundcard
  420. * @card: soundcard structure
  421. *
  422. * This function registers all the devices assigned to the soundcard.
  423. * Until calling this, the ALSA control interface is blocked from the
  424. * external accesses. Thus, you should call this function at the end
  425. * of the initialization of the card.
  426. *
  427. * Returns zero otherwise a negative error code if the registrain failed.
  428. */
  429. int snd_card_register(struct snd_card *card)
  430. {
  431. int err;
  432. snd_assert(card != NULL, return -EINVAL);
  433. if ((err = snd_device_register_all(card)) < 0)
  434. return err;
  435. write_lock(&snd_card_rwlock);
  436. if (snd_cards[card->number]) {
  437. /* already registered */
  438. write_unlock(&snd_card_rwlock);
  439. return 0;
  440. }
  441. if (card->id[0] == '\0')
  442. choose_default_id(card);
  443. snd_cards[card->number] = card;
  444. write_unlock(&snd_card_rwlock);
  445. init_info_for_card(card);
  446. #if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
  447. if (snd_mixer_oss_notify_callback)
  448. snd_mixer_oss_notify_callback(card, SND_MIXER_OSS_NOTIFY_REGISTER);
  449. #endif
  450. return 0;
  451. }
  452. #ifdef CONFIG_PROC_FS
  453. static struct snd_info_entry *snd_card_info_entry = NULL;
  454. static void snd_card_info_read(struct snd_info_entry *entry,
  455. struct snd_info_buffer *buffer)
  456. {
  457. int idx, count;
  458. struct snd_card *card;
  459. for (idx = count = 0; idx < SNDRV_CARDS; idx++) {
  460. read_lock(&snd_card_rwlock);
  461. if ((card = snd_cards[idx]) != NULL) {
  462. count++;
  463. snd_iprintf(buffer, "%2i [%-15s]: %s - %s\n",
  464. idx,
  465. card->id,
  466. card->driver,
  467. card->shortname);
  468. snd_iprintf(buffer, " %s\n",
  469. card->longname);
  470. }
  471. read_unlock(&snd_card_rwlock);
  472. }
  473. if (!count)
  474. snd_iprintf(buffer, "--- no soundcards ---\n");
  475. }
  476. #ifdef CONFIG_SND_OSSEMUL
  477. void snd_card_info_read_oss(struct snd_info_buffer *buffer)
  478. {
  479. int idx, count;
  480. struct snd_card *card;
  481. for (idx = count = 0; idx < SNDRV_CARDS; idx++) {
  482. read_lock(&snd_card_rwlock);
  483. if ((card = snd_cards[idx]) != NULL) {
  484. count++;
  485. snd_iprintf(buffer, "%s\n", card->longname);
  486. }
  487. read_unlock(&snd_card_rwlock);
  488. }
  489. if (!count) {
  490. snd_iprintf(buffer, "--- no soundcards ---\n");
  491. }
  492. }
  493. #endif
  494. #ifdef MODULE
  495. static struct snd_info_entry *snd_card_module_info_entry;
  496. static void snd_card_module_info_read(struct snd_info_entry *entry,
  497. struct snd_info_buffer *buffer)
  498. {
  499. int idx;
  500. struct snd_card *card;
  501. for (idx = 0; idx < SNDRV_CARDS; idx++) {
  502. read_lock(&snd_card_rwlock);
  503. if ((card = snd_cards[idx]) != NULL)
  504. snd_iprintf(buffer, "%2i %s\n",
  505. idx, card->module->name);
  506. read_unlock(&snd_card_rwlock);
  507. }
  508. }
  509. #endif
  510. int __init snd_card_info_init(void)
  511. {
  512. struct snd_info_entry *entry;
  513. entry = snd_info_create_module_entry(THIS_MODULE, "cards", NULL);
  514. if (! entry)
  515. return -ENOMEM;
  516. entry->c.text.read_size = PAGE_SIZE;
  517. entry->c.text.read = snd_card_info_read;
  518. if (snd_info_register(entry) < 0) {
  519. snd_info_free_entry(entry);
  520. return -ENOMEM;
  521. }
  522. snd_card_info_entry = entry;
  523. #ifdef MODULE
  524. entry = snd_info_create_module_entry(THIS_MODULE, "modules", NULL);
  525. if (entry) {
  526. entry->c.text.read_size = PAGE_SIZE;
  527. entry->c.text.read = snd_card_module_info_read;
  528. if (snd_info_register(entry) < 0)
  529. snd_info_free_entry(entry);
  530. else
  531. snd_card_module_info_entry = entry;
  532. }
  533. #endif
  534. return 0;
  535. }
  536. int __exit snd_card_info_done(void)
  537. {
  538. snd_info_unregister(snd_card_info_entry);
  539. #ifdef MODULE
  540. snd_info_unregister(snd_card_module_info_entry);
  541. #endif
  542. return 0;
  543. }
  544. #endif /* CONFIG_PROC_FS */
  545. /**
  546. * snd_component_add - add a component string
  547. * @card: soundcard structure
  548. * @component: the component id string
  549. *
  550. * This function adds the component id string to the supported list.
  551. * The component can be referred from the alsa-lib.
  552. *
  553. * Returns zero otherwise a negative error code.
  554. */
  555. int snd_component_add(struct snd_card *card, const char *component)
  556. {
  557. char *ptr;
  558. int len = strlen(component);
  559. ptr = strstr(card->components, component);
  560. if (ptr != NULL) {
  561. if (ptr[len] == '\0' || ptr[len] == ' ') /* already there */
  562. return 1;
  563. }
  564. if (strlen(card->components) + 1 + len + 1 > sizeof(card->components)) {
  565. snd_BUG();
  566. return -ENOMEM;
  567. }
  568. if (card->components[0] != '\0')
  569. strcat(card->components, " ");
  570. strcat(card->components, component);
  571. return 0;
  572. }
  573. /**
  574. * snd_card_file_add - add the file to the file list of the card
  575. * @card: soundcard structure
  576. * @file: file pointer
  577. *
  578. * This function adds the file to the file linked-list of the card.
  579. * This linked-list is used to keep tracking the connection state,
  580. * and to avoid the release of busy resources by hotplug.
  581. *
  582. * Returns zero or a negative error code.
  583. */
  584. int snd_card_file_add(struct snd_card *card, struct file *file)
  585. {
  586. struct snd_monitor_file *mfile;
  587. mfile = kmalloc(sizeof(*mfile), GFP_KERNEL);
  588. if (mfile == NULL)
  589. return -ENOMEM;
  590. mfile->file = file;
  591. mfile->next = NULL;
  592. spin_lock(&card->files_lock);
  593. if (card->shutdown) {
  594. spin_unlock(&card->files_lock);
  595. kfree(mfile);
  596. return -ENODEV;
  597. }
  598. mfile->next = card->files;
  599. card->files = mfile;
  600. spin_unlock(&card->files_lock);
  601. return 0;
  602. }
  603. /**
  604. * snd_card_file_remove - remove the file from the file list
  605. * @card: soundcard structure
  606. * @file: file pointer
  607. *
  608. * This function removes the file formerly added to the card via
  609. * snd_card_file_add() function.
  610. * If all files are removed and the release of the card is
  611. * scheduled, it will wake up the the thread to call snd_card_free()
  612. * (see snd_card_free_in_thread() function).
  613. *
  614. * Returns zero or a negative error code.
  615. */
  616. int snd_card_file_remove(struct snd_card *card, struct file *file)
  617. {
  618. struct snd_monitor_file *mfile, *pfile = NULL;
  619. spin_lock(&card->files_lock);
  620. mfile = card->files;
  621. while (mfile) {
  622. if (mfile->file == file) {
  623. if (pfile)
  624. pfile->next = mfile->next;
  625. else
  626. card->files = mfile->next;
  627. break;
  628. }
  629. pfile = mfile;
  630. mfile = mfile->next;
  631. }
  632. spin_unlock(&card->files_lock);
  633. if (card->files == NULL)
  634. wake_up(&card->shutdown_sleep);
  635. if (!mfile) {
  636. snd_printk(KERN_ERR "ALSA card file remove problem (%p)\n", file);
  637. return -ENOENT;
  638. }
  639. kfree(mfile);
  640. return 0;
  641. }
  642. #ifdef CONFIG_PM
  643. /**
  644. * snd_power_wait - wait until the power-state is changed.
  645. * @card: soundcard structure
  646. * @power_state: expected power state
  647. * @file: file structure for the O_NONBLOCK check (optional)
  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, struct file *file)
  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. #if 0 /* block all devices */
  670. if (file && (file->f_flags & O_NONBLOCK)) {
  671. result = -EAGAIN;
  672. break;
  673. }
  674. #endif
  675. set_current_state(TASK_UNINTERRUPTIBLE);
  676. snd_power_unlock(card);
  677. schedule_timeout(30 * HZ);
  678. snd_power_lock(card);
  679. }
  680. remove_wait_queue(&card->power_sleep, &wait);
  681. return result;
  682. }
  683. #endif /* CONFIG_PM */