init.c 24 KB

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