init.c 21 KB

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