init.c 20 KB

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