init.c 20 KB

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