init.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904
  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_assert(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. if (! entry)
  470. return -ENOMEM;
  471. entry->c.text.read_size = PAGE_SIZE;
  472. entry->c.text.read = snd_card_info_read;
  473. if (snd_info_register(entry) < 0) {
  474. snd_info_free_entry(entry);
  475. return -ENOMEM;
  476. }
  477. snd_card_info_entry = entry;
  478. #ifdef MODULE
  479. entry = snd_info_create_module_entry(THIS_MODULE, "modules", NULL);
  480. if (entry) {
  481. entry->c.text.read_size = PAGE_SIZE;
  482. entry->c.text.read = snd_card_module_info_read;
  483. if (snd_info_register(entry) < 0)
  484. snd_info_free_entry(entry);
  485. else
  486. snd_card_module_info_entry = entry;
  487. }
  488. #endif
  489. return 0;
  490. }
  491. int __exit snd_card_info_done(void)
  492. {
  493. if (snd_card_info_entry)
  494. snd_info_unregister(snd_card_info_entry);
  495. #ifdef MODULE
  496. if (snd_card_module_info_entry)
  497. snd_info_unregister(snd_card_module_info_entry);
  498. #endif
  499. return 0;
  500. }
  501. /**
  502. * snd_component_add - add a component string
  503. * @card: soundcard structure
  504. * @component: the component id string
  505. *
  506. * This function adds the component id string to the supported list.
  507. * The component can be referred from the alsa-lib.
  508. *
  509. * Returns zero otherwise a negative error code.
  510. */
  511. int snd_component_add(snd_card_t *card, const char *component)
  512. {
  513. char *ptr;
  514. int len = strlen(component);
  515. ptr = strstr(card->components, component);
  516. if (ptr != NULL) {
  517. if (ptr[len] == '\0' || ptr[len] == ' ') /* already there */
  518. return 1;
  519. }
  520. if (strlen(card->components) + 1 + len + 1 > sizeof(card->components)) {
  521. snd_BUG();
  522. return -ENOMEM;
  523. }
  524. if (card->components[0] != '\0')
  525. strcat(card->components, " ");
  526. strcat(card->components, component);
  527. return 0;
  528. }
  529. /**
  530. * snd_card_file_add - add the file to the file list of the card
  531. * @card: soundcard structure
  532. * @file: file pointer
  533. *
  534. * This function adds the file to the file linked-list of the card.
  535. * This linked-list is used to keep tracking the connection state,
  536. * and to avoid the release of busy resources by hotplug.
  537. *
  538. * Returns zero or a negative error code.
  539. */
  540. int snd_card_file_add(snd_card_t *card, struct file *file)
  541. {
  542. struct snd_monitor_file *mfile;
  543. mfile = kmalloc(sizeof(*mfile), GFP_KERNEL);
  544. if (mfile == NULL)
  545. return -ENOMEM;
  546. mfile->file = file;
  547. mfile->next = NULL;
  548. spin_lock(&card->files_lock);
  549. if (card->shutdown) {
  550. spin_unlock(&card->files_lock);
  551. kfree(mfile);
  552. return -ENODEV;
  553. }
  554. mfile->next = card->files;
  555. card->files = mfile;
  556. spin_unlock(&card->files_lock);
  557. return 0;
  558. }
  559. /**
  560. * snd_card_file_remove - remove the file from the file list
  561. * @card: soundcard structure
  562. * @file: file pointer
  563. *
  564. * This function removes the file formerly added to the card via
  565. * snd_card_file_add() function.
  566. * If all files are removed and the release of the card is
  567. * scheduled, it will wake up the the thread to call snd_card_free()
  568. * (see snd_card_free_in_thread() function).
  569. *
  570. * Returns zero or a negative error code.
  571. */
  572. int snd_card_file_remove(snd_card_t *card, struct file *file)
  573. {
  574. struct snd_monitor_file *mfile, *pfile = NULL;
  575. spin_lock(&card->files_lock);
  576. mfile = card->files;
  577. while (mfile) {
  578. if (mfile->file == file) {
  579. if (pfile)
  580. pfile->next = mfile->next;
  581. else
  582. card->files = mfile->next;
  583. break;
  584. }
  585. pfile = mfile;
  586. mfile = mfile->next;
  587. }
  588. spin_unlock(&card->files_lock);
  589. if (card->files == NULL)
  590. wake_up(&card->shutdown_sleep);
  591. if (!mfile) {
  592. snd_printk(KERN_ERR "ALSA card file remove problem (%p)\n", file);
  593. return -ENOENT;
  594. }
  595. kfree(mfile);
  596. return 0;
  597. }
  598. #ifdef CONFIG_SND_GENERIC_DRIVER
  599. /*
  600. * generic device without a proper bus using platform_device
  601. * (e.g. ISA)
  602. */
  603. struct snd_generic_device {
  604. struct platform_device pdev;
  605. snd_card_t *card;
  606. };
  607. #define get_snd_generic_card(dev) container_of(dev, struct snd_generic_device, pdev)->card
  608. #define SND_GENERIC_NAME "snd_generic"
  609. #ifdef CONFIG_PM
  610. static int snd_generic_suspend(struct platform_device *dev, pm_message_t state);
  611. static int snd_generic_resume(struct platform_device *dev);
  612. #endif
  613. /* initialized in sound.c */
  614. struct platform_driver snd_generic_driver = {
  615. #ifdef CONFIG_PM
  616. .suspend = snd_generic_suspend,
  617. .resume = snd_generic_resume,
  618. #endif
  619. .driver = {
  620. .name = SND_GENERIC_NAME,
  621. },
  622. };
  623. void snd_generic_device_release(struct device *dev)
  624. {
  625. }
  626. static int snd_generic_device_register(snd_card_t *card)
  627. {
  628. struct snd_generic_device *dev;
  629. int err;
  630. if (card->generic_dev)
  631. return 0; /* already registered */
  632. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  633. if (! dev) {
  634. snd_printk(KERN_ERR "can't allocate generic_device\n");
  635. return -ENOMEM;
  636. }
  637. dev->pdev.name = SND_GENERIC_NAME;
  638. dev->pdev.id = card->number;
  639. dev->pdev.dev.release = snd_generic_device_release;
  640. dev->card = card;
  641. if ((err = platform_device_register(&dev->pdev)) < 0) {
  642. kfree(dev);
  643. return err;
  644. }
  645. card->generic_dev = dev;
  646. return 0;
  647. }
  648. static void snd_generic_device_unregister(snd_card_t *card)
  649. {
  650. struct snd_generic_device *dev = card->generic_dev;
  651. if (dev) {
  652. platform_device_unregister(&dev->pdev);
  653. kfree(dev);
  654. card->generic_dev = NULL;
  655. }
  656. }
  657. /**
  658. * snd_card_set_generic_dev - assign the generic device to the card
  659. * @card: soundcard structure
  660. *
  661. * Assigns a generic device to the card. This function is provided as the
  662. * last resort, for devices without any proper bus. Thus this won't override
  663. * the device already assigned to the card.
  664. *
  665. * Returns zero if successful, or a negative error code.
  666. */
  667. int snd_card_set_generic_dev(snd_card_t *card)
  668. {
  669. int err;
  670. if ((err = snd_generic_device_register(card)) < 0)
  671. return err;
  672. if (! card->dev)
  673. snd_card_set_dev(card, &card->generic_dev->pdev.dev);
  674. return 0;
  675. }
  676. #endif /* CONFIG_SND_GENERIC_DRIVER */
  677. #ifdef CONFIG_PM
  678. /**
  679. * snd_power_wait - wait until the power-state is changed.
  680. * @card: soundcard structure
  681. * @power_state: expected power state
  682. * @file: file structure for the O_NONBLOCK check (optional)
  683. *
  684. * Waits until the power-state is changed.
  685. *
  686. * Note: the power lock must be active before call.
  687. */
  688. int snd_power_wait(snd_card_t *card, unsigned int power_state, struct file *file)
  689. {
  690. wait_queue_t wait;
  691. int result = 0;
  692. /* fastpath */
  693. if (snd_power_get_state(card) == power_state)
  694. return 0;
  695. init_waitqueue_entry(&wait, current);
  696. add_wait_queue(&card->power_sleep, &wait);
  697. while (1) {
  698. if (card->shutdown) {
  699. result = -ENODEV;
  700. break;
  701. }
  702. if (snd_power_get_state(card) == power_state)
  703. break;
  704. #if 0 /* block all devices */
  705. if (file && (file->f_flags & O_NONBLOCK)) {
  706. result = -EAGAIN;
  707. break;
  708. }
  709. #endif
  710. set_current_state(TASK_UNINTERRUPTIBLE);
  711. snd_power_unlock(card);
  712. schedule_timeout(30 * HZ);
  713. snd_power_lock(card);
  714. }
  715. remove_wait_queue(&card->power_sleep, &wait);
  716. return result;
  717. }
  718. /**
  719. * snd_card_set_pm_callback - set the PCI power-management callbacks
  720. * @card: soundcard structure
  721. * @suspend: suspend callback function
  722. * @resume: resume callback function
  723. * @private_data: private data to pass to the callback functions
  724. *
  725. * Sets the power-management callback functions of the card.
  726. * These callbacks are called from ALSA's common PCI suspend/resume
  727. * handler and from the control API.
  728. */
  729. int snd_card_set_pm_callback(snd_card_t *card,
  730. int (*suspend)(snd_card_t *, pm_message_t),
  731. int (*resume)(snd_card_t *),
  732. void *private_data)
  733. {
  734. card->pm_suspend = suspend;
  735. card->pm_resume = resume;
  736. card->pm_private_data = private_data;
  737. return 0;
  738. }
  739. #ifdef CONFIG_SND_GENERIC_DRIVER
  740. /* suspend/resume callbacks for snd_generic platform device */
  741. static int snd_generic_suspend(struct platform_device *dev, pm_message_t state)
  742. {
  743. snd_card_t *card;
  744. card = get_snd_generic_card(dev);
  745. if (card->power_state == SNDRV_CTL_POWER_D3hot)
  746. return 0;
  747. if (card->pm_suspend)
  748. card->pm_suspend(card, PMSG_SUSPEND);
  749. snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
  750. return 0;
  751. }
  752. static int snd_generic_resume(struct platform_device *dev)
  753. {
  754. snd_card_t *card;
  755. card = get_snd_generic_card(dev);
  756. if (card->power_state == SNDRV_CTL_POWER_D0)
  757. return 0;
  758. if (card->pm_resume)
  759. card->pm_resume(card);
  760. snd_power_change_state(card, SNDRV_CTL_POWER_D0);
  761. return 0;
  762. }
  763. /**
  764. * snd_card_set_generic_pm_callback - set the generic power-management callbacks
  765. * @card: soundcard structure
  766. * @suspend: suspend callback function
  767. * @resume: resume callback function
  768. * @private_data: private data to pass to the callback functions
  769. *
  770. * Registers the power-management and sets the lowlevel callbacks for
  771. * the given card. These callbacks are called from the ALSA's common
  772. * PM handler and from the control API.
  773. */
  774. int snd_card_set_generic_pm_callback(snd_card_t *card,
  775. int (*suspend)(snd_card_t *, pm_message_t),
  776. int (*resume)(snd_card_t *),
  777. void *private_data)
  778. {
  779. int err;
  780. if ((err = snd_generic_device_register(card)) < 0)
  781. return err;
  782. return snd_card_set_pm_callback(card, suspend, resume, private_data);
  783. }
  784. #endif /* CONFIG_SND_GENERIC_DRIVER */
  785. #ifdef CONFIG_PCI
  786. int snd_card_pci_suspend(struct pci_dev *dev, pm_message_t state)
  787. {
  788. snd_card_t *card = pci_get_drvdata(dev);
  789. int err;
  790. if (! card || ! card->pm_suspend)
  791. return 0;
  792. if (card->power_state == SNDRV_CTL_POWER_D3hot)
  793. return 0;
  794. err = card->pm_suspend(card, PMSG_SUSPEND);
  795. pci_save_state(dev);
  796. snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
  797. return err;
  798. }
  799. int snd_card_pci_resume(struct pci_dev *dev)
  800. {
  801. snd_card_t *card = pci_get_drvdata(dev);
  802. if (! card || ! card->pm_resume)
  803. return 0;
  804. if (card->power_state == SNDRV_CTL_POWER_D0)
  805. return 0;
  806. /* restore the PCI config space */
  807. pci_restore_state(dev);
  808. card->pm_resume(card);
  809. snd_power_change_state(card, SNDRV_CTL_POWER_D0);
  810. return 0;
  811. }
  812. #endif
  813. #endif /* CONFIG_PM */