init.c 23 KB

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