init.c 20 KB

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