init.c 24 KB

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