init.c 20 KB

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