init.c 21 KB

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