misc.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Misc and compatibility things
  4. * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
  5. */
  6. #include <linux/init.h>
  7. #include <linux/export.h>
  8. #include <linux/moduleparam.h>
  9. #include <linux/time.h>
  10. #include <linux/slab.h>
  11. #include <linux/ioport.h>
  12. #include <linux/fs.h>
  13. #include <sound/core.h>
  14. void release_and_free_resource(struct resource *res)
  15. {
  16. if (res) {
  17. release_resource(res);
  18. kfree(res);
  19. }
  20. }
  21. EXPORT_SYMBOL(release_and_free_resource);
  22. #ifdef CONFIG_PCI
  23. #include <linux/pci.h>
  24. /**
  25. * snd_pci_quirk_lookup_id - look up a PCI SSID quirk list
  26. * @vendor: PCI SSV id
  27. * @device: PCI SSD id
  28. * @list: quirk list, terminated by a null entry
  29. *
  30. * Look through the given quirk list and finds a matching entry
  31. * with the same PCI SSID. When subdevice is 0, all subdevice
  32. * values may match.
  33. *
  34. * Returns the matched entry pointer, or NULL if nothing matched.
  35. */
  36. const struct snd_pci_quirk *
  37. snd_pci_quirk_lookup_id(u16 vendor, u16 device,
  38. const struct snd_pci_quirk *list)
  39. {
  40. const struct snd_pci_quirk *q;
  41. for (q = list; q->subvendor || q->subdevice; q++) {
  42. if (q->subvendor != vendor)
  43. continue;
  44. if (!q->subdevice ||
  45. (device & q->subdevice_mask) == q->subdevice)
  46. return q;
  47. }
  48. return NULL;
  49. }
  50. EXPORT_SYMBOL(snd_pci_quirk_lookup_id);
  51. /**
  52. * snd_pci_quirk_lookup - look up a PCI SSID quirk list
  53. * @pci: pci_dev handle
  54. * @list: quirk list, terminated by a null entry
  55. *
  56. * Look through the given quirk list and finds a matching entry
  57. * with the same PCI SSID. When subdevice is 0, all subdevice
  58. * values may match.
  59. *
  60. * Returns the matched entry pointer, or NULL if nothing matched.
  61. */
  62. const struct snd_pci_quirk *
  63. snd_pci_quirk_lookup(struct pci_dev *pci, const struct snd_pci_quirk *list)
  64. {
  65. if (!pci)
  66. return NULL;
  67. return snd_pci_quirk_lookup_id(pci->subsystem_vendor,
  68. pci->subsystem_device,
  69. list);
  70. }
  71. EXPORT_SYMBOL(snd_pci_quirk_lookup);
  72. #endif
  73. /*
  74. * Deferred async signal helpers
  75. *
  76. * Below are a few helper functions to wrap the async signal handling
  77. * in the deferred work. The main purpose is to avoid the messy deadlock
  78. * around tasklist_lock and co at the kill_fasync() invocation.
  79. * fasync_helper() and kill_fasync() are replaced with snd_fasync_helper()
  80. * and snd_kill_fasync(), respectively. In addition, snd_fasync_free() has
  81. * to be called at releasing the relevant file object.
  82. */
  83. struct snd_fasync {
  84. struct fasync_struct *fasync;
  85. int signal;
  86. int poll;
  87. int on;
  88. struct list_head list;
  89. };
  90. static DEFINE_SPINLOCK(snd_fasync_lock);
  91. static LIST_HEAD(snd_fasync_list);
  92. static void snd_fasync_work_fn(struct work_struct *work)
  93. {
  94. struct snd_fasync *fasync;
  95. spin_lock_irq(&snd_fasync_lock);
  96. while (!list_empty(&snd_fasync_list)) {
  97. fasync = list_first_entry(&snd_fasync_list, struct snd_fasync, list);
  98. list_del_init(&fasync->list);
  99. spin_unlock_irq(&snd_fasync_lock);
  100. if (fasync->on)
  101. kill_fasync(&fasync->fasync, fasync->signal, fasync->poll);
  102. spin_lock_irq(&snd_fasync_lock);
  103. }
  104. spin_unlock_irq(&snd_fasync_lock);
  105. }
  106. static DECLARE_WORK(snd_fasync_work, snd_fasync_work_fn);
  107. int snd_fasync_helper(int fd, struct file *file, int on,
  108. struct snd_fasync **fasyncp)
  109. {
  110. struct snd_fasync *fasync = NULL;
  111. if (on) {
  112. fasync = kzalloc(sizeof(*fasync), GFP_KERNEL);
  113. if (!fasync)
  114. return -ENOMEM;
  115. INIT_LIST_HEAD(&fasync->list);
  116. }
  117. spin_lock_irq(&snd_fasync_lock);
  118. if (*fasyncp) {
  119. kfree(fasync);
  120. fasync = *fasyncp;
  121. } else {
  122. if (!fasync) {
  123. spin_unlock_irq(&snd_fasync_lock);
  124. return 0;
  125. }
  126. *fasyncp = fasync;
  127. }
  128. fasync->on = on;
  129. spin_unlock_irq(&snd_fasync_lock);
  130. return fasync_helper(fd, file, on, &fasync->fasync);
  131. }
  132. EXPORT_SYMBOL_GPL(snd_fasync_helper);
  133. void snd_kill_fasync(struct snd_fasync *fasync, int signal, int poll)
  134. {
  135. unsigned long flags;
  136. if (!fasync || !fasync->on)
  137. return;
  138. spin_lock_irqsave(&snd_fasync_lock, flags);
  139. fasync->signal = signal;
  140. fasync->poll = poll;
  141. list_move(&fasync->list, &snd_fasync_list);
  142. schedule_work(&snd_fasync_work);
  143. spin_unlock_irqrestore(&snd_fasync_lock, flags);
  144. }
  145. EXPORT_SYMBOL_GPL(snd_kill_fasync);
  146. void snd_fasync_free(struct snd_fasync *fasync)
  147. {
  148. if (!fasync)
  149. return;
  150. fasync->on = 0;
  151. flush_work(&snd_fasync_work);
  152. kfree(fasync);
  153. }
  154. EXPORT_SYMBOL_GPL(snd_fasync_free);