init.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803
  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/pm.h>
  29. #include <sound/core.h>
  30. #include <sound/control.h>
  31. #include <sound/info.h>
  32. static DEFINE_SPINLOCK(shutdown_lock);
  33. static LIST_HEAD(shutdown_files);
  34. static const struct file_operations snd_shutdown_f_ops;
  35. static unsigned int snd_cards_lock; /* locked for registering/using */
  36. struct snd_card *snd_cards[SNDRV_CARDS];
  37. EXPORT_SYMBOL(snd_cards);
  38. static DEFINE_MUTEX(snd_card_mutex);
  39. #if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
  40. int (*snd_mixer_oss_notify_callback)(struct snd_card *card, int free_flag);
  41. EXPORT_SYMBOL(snd_mixer_oss_notify_callback);
  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 = snd_card_id_read;
  62. if (snd_info_register(entry) < 0) {
  63. snd_info_free_entry(entry);
  64. entry = NULL;
  65. }
  66. card->proc_id = entry;
  67. return 0;
  68. }
  69. #else /* !CONFIG_PROC_FS */
  70. #define init_info_for_card(card)
  71. #endif
  72. /**
  73. * snd_card_new - create and initialize a soundcard structure
  74. * @idx: card index (address) [0 ... (SNDRV_CARDS-1)]
  75. * @xid: card identification (ASCII string)
  76. * @module: top level module for locking
  77. * @extra_size: allocate this extra size after the main soundcard structure
  78. *
  79. * Creates and initializes a soundcard structure.
  80. *
  81. * Returns kmallocated snd_card structure. Creates the ALSA control interface
  82. * (which is blocked until snd_card_register function is called).
  83. */
  84. struct snd_card *snd_card_new(int idx, const char *xid,
  85. struct module *module, int extra_size)
  86. {
  87. struct snd_card *card;
  88. int err;
  89. if (extra_size < 0)
  90. extra_size = 0;
  91. card = kzalloc(sizeof(*card) + extra_size, GFP_KERNEL);
  92. if (card == NULL)
  93. return NULL;
  94. if (xid) {
  95. if (!snd_info_check_reserved_words(xid))
  96. goto __error;
  97. strlcpy(card->id, xid, sizeof(card->id));
  98. }
  99. err = 0;
  100. mutex_lock(&snd_card_mutex);
  101. if (idx < 0) {
  102. int idx2;
  103. for (idx2 = 0; idx2 < SNDRV_CARDS; idx2++)
  104. /* idx == -1 == 0xffff means: take any free slot */
  105. if (~snd_cards_lock & idx & 1<<idx2) {
  106. idx = idx2;
  107. if (idx >= snd_ecards_limit)
  108. snd_ecards_limit = idx + 1;
  109. break;
  110. }
  111. } else {
  112. if (idx < snd_ecards_limit) {
  113. if (snd_cards_lock & (1 << idx))
  114. err = -EBUSY; /* invalid */
  115. } else {
  116. if (idx < SNDRV_CARDS)
  117. snd_ecards_limit = idx + 1; /* increase the limit */
  118. else
  119. err = -ENODEV;
  120. }
  121. }
  122. if (idx < 0 || err < 0) {
  123. mutex_unlock(&snd_card_mutex);
  124. snd_printk(KERN_ERR "cannot find the slot for index %d (range 0-%i), error: %d\n",
  125. idx, snd_ecards_limit - 1, err);
  126. goto __error;
  127. }
  128. snd_cards_lock |= 1 << idx; /* lock it */
  129. mutex_unlock(&snd_card_mutex);
  130. card->number = idx;
  131. card->module = module;
  132. INIT_LIST_HEAD(&card->devices);
  133. init_rwsem(&card->controls_rwsem);
  134. rwlock_init(&card->ctl_files_rwlock);
  135. INIT_LIST_HEAD(&card->controls);
  136. INIT_LIST_HEAD(&card->ctl_files);
  137. spin_lock_init(&card->files_lock);
  138. init_waitqueue_head(&card->shutdown_sleep);
  139. #ifdef CONFIG_PM
  140. mutex_init(&card->power_lock);
  141. init_waitqueue_head(&card->power_sleep);
  142. #endif
  143. /* the control interface cannot be accessed from the user space until */
  144. /* snd_cards_bitmask and snd_cards are set with snd_card_register */
  145. if ((err = snd_ctl_create(card)) < 0) {
  146. snd_printd("unable to register control minors\n");
  147. goto __error;
  148. }
  149. if ((err = snd_info_card_create(card)) < 0) {
  150. snd_printd("unable to create card info\n");
  151. goto __error_ctl;
  152. }
  153. if (extra_size > 0)
  154. card->private_data = (char *)card + sizeof(struct snd_card);
  155. return card;
  156. __error_ctl:
  157. snd_device_free_all(card, SNDRV_DEV_CMD_PRE);
  158. __error:
  159. kfree(card);
  160. return NULL;
  161. }
  162. EXPORT_SYMBOL(snd_card_new);
  163. /* return non-zero if a card is already locked */
  164. int snd_card_locked(int card)
  165. {
  166. int locked;
  167. mutex_lock(&snd_card_mutex);
  168. locked = snd_cards_lock & (1 << card);
  169. mutex_unlock(&snd_card_mutex);
  170. return locked;
  171. }
  172. static loff_t snd_disconnect_llseek(struct file *file, loff_t offset, int orig)
  173. {
  174. return -ENODEV;
  175. }
  176. static ssize_t snd_disconnect_read(struct file *file, char __user *buf,
  177. size_t count, loff_t *offset)
  178. {
  179. return -ENODEV;
  180. }
  181. static ssize_t snd_disconnect_write(struct file *file, const char __user *buf,
  182. size_t count, loff_t *offset)
  183. {
  184. return -ENODEV;
  185. }
  186. static int snd_disconnect_release(struct inode *inode, struct file *file)
  187. {
  188. struct snd_monitor_file *df = NULL, *_df;
  189. spin_lock(&shutdown_lock);
  190. list_for_each_entry(_df, &shutdown_files, shutdown_list) {
  191. if (_df->file == file) {
  192. df = _df;
  193. break;
  194. }
  195. }
  196. spin_unlock(&shutdown_lock);
  197. if (likely(df))
  198. return df->disconnected_f_op->release(inode, file);
  199. panic("%s(%p, %p) failed!", __FUNCTION__, inode, file);
  200. }
  201. static unsigned int snd_disconnect_poll(struct file * file, poll_table * wait)
  202. {
  203. return POLLERR | POLLNVAL;
  204. }
  205. static long snd_disconnect_ioctl(struct file *file,
  206. unsigned int cmd, unsigned long arg)
  207. {
  208. return -ENODEV;
  209. }
  210. static int snd_disconnect_mmap(struct file *file, struct vm_area_struct *vma)
  211. {
  212. return -ENODEV;
  213. }
  214. static int snd_disconnect_fasync(int fd, struct file *file, int on)
  215. {
  216. return -ENODEV;
  217. }
  218. static const struct file_operations snd_shutdown_f_ops =
  219. {
  220. .owner = THIS_MODULE,
  221. .llseek = snd_disconnect_llseek,
  222. .read = snd_disconnect_read,
  223. .write = snd_disconnect_write,
  224. .release = snd_disconnect_release,
  225. .poll = snd_disconnect_poll,
  226. .unlocked_ioctl = snd_disconnect_ioctl,
  227. #ifdef CONFIG_COMPAT
  228. .compat_ioctl = snd_disconnect_ioctl,
  229. #endif
  230. .mmap = snd_disconnect_mmap,
  231. .fasync = snd_disconnect_fasync
  232. };
  233. /**
  234. * snd_card_disconnect - disconnect all APIs from the file-operations (user space)
  235. * @card: soundcard structure
  236. *
  237. * Disconnects all APIs from the file-operations (user space).
  238. *
  239. * Returns zero, otherwise a negative error code.
  240. *
  241. * Note: The current implementation replaces all active file->f_op with special
  242. * dummy file operations (they do nothing except release).
  243. */
  244. int snd_card_disconnect(struct snd_card *card)
  245. {
  246. struct snd_monitor_file *mfile;
  247. struct file *file;
  248. int err;
  249. spin_lock(&card->files_lock);
  250. if (card->shutdown) {
  251. spin_unlock(&card->files_lock);
  252. return 0;
  253. }
  254. card->shutdown = 1;
  255. spin_unlock(&card->files_lock);
  256. /* phase 1: disable fops (user space) operations for ALSA API */
  257. mutex_lock(&snd_card_mutex);
  258. snd_cards[card->number] = NULL;
  259. mutex_unlock(&snd_card_mutex);
  260. /* phase 2: replace file->f_op with special dummy operations */
  261. spin_lock(&card->files_lock);
  262. mfile = card->files;
  263. while (mfile) {
  264. file = mfile->file;
  265. /* it's critical part, use endless loop */
  266. /* we have no room to fail */
  267. mfile->disconnected_f_op = mfile->file->f_op;
  268. spin_lock(&shutdown_lock);
  269. list_add(&mfile->shutdown_list, &shutdown_files);
  270. spin_unlock(&shutdown_lock);
  271. fops_get(&snd_shutdown_f_ops);
  272. mfile->file->f_op = &snd_shutdown_f_ops;
  273. mfile = mfile->next;
  274. }
  275. spin_unlock(&card->files_lock);
  276. /* phase 3: notify all connected devices about disconnection */
  277. /* at this point, they cannot respond to any calls except release() */
  278. #if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
  279. if (snd_mixer_oss_notify_callback)
  280. snd_mixer_oss_notify_callback(card, SND_MIXER_OSS_NOTIFY_DISCONNECT);
  281. #endif
  282. /* notify all devices that we are disconnected */
  283. err = snd_device_disconnect_all(card);
  284. if (err < 0)
  285. snd_printk(KERN_ERR "not all devices for card %i can be disconnected\n", card->number);
  286. snd_info_card_disconnect(card);
  287. return 0;
  288. }
  289. EXPORT_SYMBOL(snd_card_disconnect);
  290. /**
  291. * snd_card_free - frees given soundcard structure
  292. * @card: soundcard structure
  293. *
  294. * This function releases the soundcard structure and the all assigned
  295. * devices automatically. That is, you don't have to release the devices
  296. * by yourself.
  297. *
  298. * Returns zero. Frees all associated devices and frees the control
  299. * interface associated to given soundcard.
  300. */
  301. static int snd_card_do_free(struct snd_card *card)
  302. {
  303. #if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
  304. if (snd_mixer_oss_notify_callback)
  305. snd_mixer_oss_notify_callback(card, SND_MIXER_OSS_NOTIFY_FREE);
  306. #endif
  307. if (snd_device_free_all(card, SNDRV_DEV_CMD_PRE) < 0) {
  308. snd_printk(KERN_ERR "unable to free all devices (pre)\n");
  309. /* Fatal, but this situation should never occur */
  310. }
  311. if (snd_device_free_all(card, SNDRV_DEV_CMD_NORMAL) < 0) {
  312. snd_printk(KERN_ERR "unable to free all devices (normal)\n");
  313. /* Fatal, but this situation should never occur */
  314. }
  315. if (snd_device_free_all(card, SNDRV_DEV_CMD_POST) < 0) {
  316. snd_printk(KERN_ERR "unable to free all devices (post)\n");
  317. /* Fatal, but this situation should never occur */
  318. }
  319. if (card->private_free)
  320. card->private_free(card);
  321. snd_info_free_entry(card->proc_id);
  322. if (snd_info_card_free(card) < 0) {
  323. snd_printk(KERN_WARNING "unable to free card info\n");
  324. /* Not fatal error */
  325. }
  326. #ifndef CONFIG_SYSFS_DEPRECATED
  327. if (card->card_dev)
  328. device_unregister(card->card_dev);
  329. #endif
  330. kfree(card);
  331. return 0;
  332. }
  333. static int snd_card_free_prepare(struct snd_card *card)
  334. {
  335. if (card == NULL)
  336. return -EINVAL;
  337. (void) snd_card_disconnect(card);
  338. mutex_lock(&snd_card_mutex);
  339. snd_cards[card->number] = NULL;
  340. snd_cards_lock &= ~(1 << card->number);
  341. mutex_unlock(&snd_card_mutex);
  342. #ifdef CONFIG_PM
  343. wake_up(&card->power_sleep);
  344. #endif
  345. return 0;
  346. }
  347. int snd_card_free_when_closed(struct snd_card *card)
  348. {
  349. int free_now = 0;
  350. int ret = snd_card_free_prepare(card);
  351. if (ret)
  352. return ret;
  353. spin_lock(&card->files_lock);
  354. if (card->files == NULL)
  355. free_now = 1;
  356. else
  357. card->free_on_last_close = 1;
  358. spin_unlock(&card->files_lock);
  359. if (free_now)
  360. snd_card_do_free(card);
  361. return 0;
  362. }
  363. EXPORT_SYMBOL(snd_card_free_when_closed);
  364. int snd_card_free(struct snd_card *card)
  365. {
  366. int ret = snd_card_free_prepare(card);
  367. if (ret)
  368. return ret;
  369. /* wait, until all devices are ready for the free operation */
  370. wait_event(card->shutdown_sleep, card->files == NULL);
  371. snd_card_do_free(card);
  372. return 0;
  373. }
  374. EXPORT_SYMBOL(snd_card_free);
  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. #ifndef CONFIG_SYSFS_DEPRECATED
  449. if (!card->card_dev) {
  450. card->card_dev = device_create(sound_class, card->dev, 0,
  451. "card%i", card->number);
  452. if (IS_ERR(card->card_dev))
  453. card->card_dev = NULL;
  454. }
  455. #endif
  456. if ((err = snd_device_register_all(card)) < 0)
  457. return err;
  458. mutex_lock(&snd_card_mutex);
  459. if (snd_cards[card->number]) {
  460. /* already registered */
  461. mutex_unlock(&snd_card_mutex);
  462. return 0;
  463. }
  464. if (card->id[0] == '\0')
  465. choose_default_id(card);
  466. snd_cards[card->number] = card;
  467. mutex_unlock(&snd_card_mutex);
  468. init_info_for_card(card);
  469. #if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
  470. if (snd_mixer_oss_notify_callback)
  471. snd_mixer_oss_notify_callback(card, SND_MIXER_OSS_NOTIFY_REGISTER);
  472. #endif
  473. return 0;
  474. }
  475. EXPORT_SYMBOL(snd_card_register);
  476. #ifdef CONFIG_PROC_FS
  477. static struct snd_info_entry *snd_card_info_entry;
  478. static void snd_card_info_read(struct snd_info_entry *entry,
  479. struct snd_info_buffer *buffer)
  480. {
  481. int idx, count;
  482. struct snd_card *card;
  483. for (idx = count = 0; idx < SNDRV_CARDS; idx++) {
  484. mutex_lock(&snd_card_mutex);
  485. if ((card = snd_cards[idx]) != NULL) {
  486. count++;
  487. snd_iprintf(buffer, "%2i [%-15s]: %s - %s\n",
  488. idx,
  489. card->id,
  490. card->driver,
  491. card->shortname);
  492. snd_iprintf(buffer, " %s\n",
  493. card->longname);
  494. }
  495. mutex_unlock(&snd_card_mutex);
  496. }
  497. if (!count)
  498. snd_iprintf(buffer, "--- no soundcards ---\n");
  499. }
  500. #ifdef CONFIG_SND_OSSEMUL
  501. void snd_card_info_read_oss(struct snd_info_buffer *buffer)
  502. {
  503. int idx, count;
  504. struct snd_card *card;
  505. for (idx = count = 0; idx < SNDRV_CARDS; idx++) {
  506. mutex_lock(&snd_card_mutex);
  507. if ((card = snd_cards[idx]) != NULL) {
  508. count++;
  509. snd_iprintf(buffer, "%s\n", card->longname);
  510. }
  511. mutex_unlock(&snd_card_mutex);
  512. }
  513. if (!count) {
  514. snd_iprintf(buffer, "--- no soundcards ---\n");
  515. }
  516. }
  517. #endif
  518. #ifdef MODULE
  519. static struct snd_info_entry *snd_card_module_info_entry;
  520. static void snd_card_module_info_read(struct snd_info_entry *entry,
  521. struct snd_info_buffer *buffer)
  522. {
  523. int idx;
  524. struct snd_card *card;
  525. for (idx = 0; idx < SNDRV_CARDS; idx++) {
  526. mutex_lock(&snd_card_mutex);
  527. if ((card = snd_cards[idx]) != NULL)
  528. snd_iprintf(buffer, "%2i %s\n",
  529. idx, card->module->name);
  530. mutex_unlock(&snd_card_mutex);
  531. }
  532. }
  533. #endif
  534. int __init snd_card_info_init(void)
  535. {
  536. struct snd_info_entry *entry;
  537. entry = snd_info_create_module_entry(THIS_MODULE, "cards", NULL);
  538. if (! entry)
  539. return -ENOMEM;
  540. entry->c.text.read = snd_card_info_read;
  541. if (snd_info_register(entry) < 0) {
  542. snd_info_free_entry(entry);
  543. return -ENOMEM;
  544. }
  545. snd_card_info_entry = entry;
  546. #ifdef MODULE
  547. entry = snd_info_create_module_entry(THIS_MODULE, "modules", NULL);
  548. if (entry) {
  549. entry->c.text.read = snd_card_module_info_read;
  550. if (snd_info_register(entry) < 0)
  551. snd_info_free_entry(entry);
  552. else
  553. snd_card_module_info_entry = entry;
  554. }
  555. #endif
  556. return 0;
  557. }
  558. int __exit snd_card_info_done(void)
  559. {
  560. snd_info_free_entry(snd_card_info_entry);
  561. #ifdef MODULE
  562. snd_info_free_entry(snd_card_module_info_entry);
  563. #endif
  564. return 0;
  565. }
  566. #endif /* CONFIG_PROC_FS */
  567. /**
  568. * snd_component_add - add a component string
  569. * @card: soundcard structure
  570. * @component: the component id string
  571. *
  572. * This function adds the component id string to the supported list.
  573. * The component can be referred from the alsa-lib.
  574. *
  575. * Returns zero otherwise a negative error code.
  576. */
  577. int snd_component_add(struct snd_card *card, const char *component)
  578. {
  579. char *ptr;
  580. int len = strlen(component);
  581. ptr = strstr(card->components, component);
  582. if (ptr != NULL) {
  583. if (ptr[len] == '\0' || ptr[len] == ' ') /* already there */
  584. return 1;
  585. }
  586. if (strlen(card->components) + 1 + len + 1 > sizeof(card->components)) {
  587. snd_BUG();
  588. return -ENOMEM;
  589. }
  590. if (card->components[0] != '\0')
  591. strcat(card->components, " ");
  592. strcat(card->components, component);
  593. return 0;
  594. }
  595. EXPORT_SYMBOL(snd_component_add);
  596. /**
  597. * snd_card_file_add - add the file to the file list of the card
  598. * @card: soundcard structure
  599. * @file: file pointer
  600. *
  601. * This function adds the file to the file linked-list of the card.
  602. * This linked-list is used to keep tracking the connection state,
  603. * and to avoid the release of busy resources by hotplug.
  604. *
  605. * Returns zero or a negative error code.
  606. */
  607. int snd_card_file_add(struct snd_card *card, struct file *file)
  608. {
  609. struct snd_monitor_file *mfile;
  610. mfile = kmalloc(sizeof(*mfile), GFP_KERNEL);
  611. if (mfile == NULL)
  612. return -ENOMEM;
  613. mfile->file = file;
  614. mfile->disconnected_f_op = NULL;
  615. mfile->next = NULL;
  616. spin_lock(&card->files_lock);
  617. if (card->shutdown) {
  618. spin_unlock(&card->files_lock);
  619. kfree(mfile);
  620. return -ENODEV;
  621. }
  622. mfile->next = card->files;
  623. card->files = mfile;
  624. spin_unlock(&card->files_lock);
  625. return 0;
  626. }
  627. EXPORT_SYMBOL(snd_card_file_add);
  628. /**
  629. * snd_card_file_remove - remove the file from the file list
  630. * @card: soundcard structure
  631. * @file: file pointer
  632. *
  633. * This function removes the file formerly added to the card via
  634. * snd_card_file_add() function.
  635. * If all files are removed and snd_card_free_when_closed() was
  636. * called beforehand, it processes the pending release of
  637. * resources.
  638. *
  639. * Returns zero or a negative error code.
  640. */
  641. int snd_card_file_remove(struct snd_card *card, struct file *file)
  642. {
  643. struct snd_monitor_file *mfile, *pfile = NULL;
  644. int last_close = 0;
  645. spin_lock(&card->files_lock);
  646. mfile = card->files;
  647. while (mfile) {
  648. if (mfile->file == file) {
  649. if (pfile)
  650. pfile->next = mfile->next;
  651. else
  652. card->files = mfile->next;
  653. break;
  654. }
  655. pfile = mfile;
  656. mfile = mfile->next;
  657. }
  658. if (mfile && mfile->disconnected_f_op) {
  659. fops_put(mfile->disconnected_f_op);
  660. spin_lock(&shutdown_lock);
  661. list_del(&mfile->shutdown_list);
  662. spin_unlock(&shutdown_lock);
  663. }
  664. if (card->files == NULL)
  665. last_close = 1;
  666. spin_unlock(&card->files_lock);
  667. if (last_close) {
  668. wake_up(&card->shutdown_sleep);
  669. if (card->free_on_last_close)
  670. snd_card_do_free(card);
  671. }
  672. if (!mfile) {
  673. snd_printk(KERN_ERR "ALSA card file remove problem (%p)\n", file);
  674. return -ENOENT;
  675. }
  676. kfree(mfile);
  677. return 0;
  678. }
  679. EXPORT_SYMBOL(snd_card_file_remove);
  680. #ifdef CONFIG_PM
  681. /**
  682. * snd_power_wait - wait until the power-state is changed.
  683. * @card: soundcard structure
  684. * @power_state: expected power state
  685. *
  686. * Waits until the power-state is changed.
  687. *
  688. * Note: the power lock must be active before call.
  689. */
  690. int snd_power_wait(struct snd_card *card, unsigned int power_state)
  691. {
  692. wait_queue_t wait;
  693. int result = 0;
  694. /* fastpath */
  695. if (snd_power_get_state(card) == power_state)
  696. return 0;
  697. init_waitqueue_entry(&wait, current);
  698. add_wait_queue(&card->power_sleep, &wait);
  699. while (1) {
  700. if (card->shutdown) {
  701. result = -ENODEV;
  702. break;
  703. }
  704. if (snd_power_get_state(card) == power_state)
  705. break;
  706. set_current_state(TASK_UNINTERRUPTIBLE);
  707. snd_power_unlock(card);
  708. schedule_timeout(30 * HZ);
  709. snd_power_lock(card);
  710. }
  711. remove_wait_queue(&card->power_sleep, &wait);
  712. return result;
  713. }
  714. EXPORT_SYMBOL(snd_power_wait);
  715. #endif /* CONFIG_PM */