init.c 21 KB

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