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