device.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * Device management routines
  3. * Copyright (c) by Jaroslav Kysela <perex@perex.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 <linux/slab.h>
  22. #include <linux/time.h>
  23. #include <linux/export.h>
  24. #include <linux/errno.h>
  25. #include <sound/core.h>
  26. /**
  27. * snd_device_new - create an ALSA device component
  28. * @card: the card instance
  29. * @type: the device type, SNDRV_DEV_XXX
  30. * @device_data: the data pointer of this device
  31. * @ops: the operator table
  32. *
  33. * Creates a new device component for the given data pointer.
  34. * The device will be assigned to the card and managed together
  35. * by the card.
  36. *
  37. * The data pointer plays a role as the identifier, too, so the
  38. * pointer address must be unique and unchanged.
  39. *
  40. * Return: Zero if successful, or a negative error code on failure.
  41. */
  42. int snd_device_new(struct snd_card *card, enum snd_device_type type,
  43. void *device_data, struct snd_device_ops *ops)
  44. {
  45. struct snd_device *dev;
  46. struct list_head *p;
  47. if (snd_BUG_ON(!card || !device_data || !ops))
  48. return -ENXIO;
  49. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  50. if (dev == NULL) {
  51. dev_err(card->dev, "Cannot allocate device, type=%d\n", type);
  52. return -ENOMEM;
  53. }
  54. INIT_LIST_HEAD(&dev->list);
  55. dev->card = card;
  56. dev->type = type;
  57. dev->state = SNDRV_DEV_BUILD;
  58. dev->device_data = device_data;
  59. dev->ops = ops;
  60. /* insert the entry in an incrementally sorted list */
  61. list_for_each_prev(p, &card->devices) {
  62. struct snd_device *pdev = list_entry(p, struct snd_device, list);
  63. if ((unsigned int)pdev->type <= (unsigned int)type)
  64. break;
  65. }
  66. list_add(&dev->list, p);
  67. return 0;
  68. }
  69. EXPORT_SYMBOL(snd_device_new);
  70. static int __snd_device_disconnect(struct snd_device *dev)
  71. {
  72. if (dev->state == SNDRV_DEV_REGISTERED) {
  73. if (dev->ops->dev_disconnect &&
  74. dev->ops->dev_disconnect(dev))
  75. dev_err(dev->card->dev, "device disconnect failure\n");
  76. dev->state = SNDRV_DEV_DISCONNECTED;
  77. }
  78. return 0;
  79. }
  80. static void __snd_device_free(struct snd_device *dev)
  81. {
  82. /* unlink */
  83. list_del(&dev->list);
  84. __snd_device_disconnect(dev);
  85. if (dev->ops->dev_free) {
  86. if (dev->ops->dev_free(dev))
  87. dev_err(dev->card->dev, "device free failure\n");
  88. }
  89. kfree(dev);
  90. }
  91. static struct snd_device *look_for_dev(struct snd_card *card, void *device_data)
  92. {
  93. struct snd_device *dev;
  94. list_for_each_entry(dev, &card->devices, list)
  95. if (dev->device_data == device_data)
  96. return dev;
  97. return NULL;
  98. }
  99. /**
  100. * snd_device_free - release the device from the card
  101. * @card: the card instance
  102. * @device_data: the data pointer to release
  103. *
  104. * Removes the device from the list on the card and invokes the
  105. * callbacks, dev_disconnect and dev_free, corresponding to the state.
  106. * Then release the device.
  107. */
  108. void snd_device_free(struct snd_card *card, void *device_data)
  109. {
  110. struct snd_device *dev;
  111. if (snd_BUG_ON(!card || !device_data))
  112. return;
  113. dev = look_for_dev(card, device_data);
  114. if (dev)
  115. __snd_device_free(dev);
  116. else
  117. dev_dbg(card->dev, "device free %p (from %pF), not found\n",
  118. device_data, __builtin_return_address(0));
  119. }
  120. EXPORT_SYMBOL(snd_device_free);
  121. static int __snd_device_register(struct snd_device *dev)
  122. {
  123. if (dev->state == SNDRV_DEV_BUILD) {
  124. if (dev->ops->dev_register) {
  125. int err = dev->ops->dev_register(dev);
  126. if (err < 0)
  127. return err;
  128. }
  129. dev->state = SNDRV_DEV_REGISTERED;
  130. }
  131. return 0;
  132. }
  133. /**
  134. * snd_device_register - register the device
  135. * @card: the card instance
  136. * @device_data: the data pointer to register
  137. *
  138. * Registers the device which was already created via
  139. * snd_device_new(). Usually this is called from snd_card_register(),
  140. * but it can be called later if any new devices are created after
  141. * invocation of snd_card_register().
  142. *
  143. * Return: Zero if successful, or a negative error code on failure or if the
  144. * device not found.
  145. */
  146. int snd_device_register(struct snd_card *card, void *device_data)
  147. {
  148. struct snd_device *dev;
  149. if (snd_BUG_ON(!card || !device_data))
  150. return -ENXIO;
  151. dev = look_for_dev(card, device_data);
  152. if (dev)
  153. return __snd_device_register(dev);
  154. snd_BUG();
  155. return -ENXIO;
  156. }
  157. EXPORT_SYMBOL(snd_device_register);
  158. /*
  159. * register all the devices on the card.
  160. * called from init.c
  161. */
  162. int snd_device_register_all(struct snd_card *card)
  163. {
  164. struct snd_device *dev;
  165. int err;
  166. if (snd_BUG_ON(!card))
  167. return -ENXIO;
  168. list_for_each_entry(dev, &card->devices, list) {
  169. err = __snd_device_register(dev);
  170. if (err < 0)
  171. return err;
  172. }
  173. return 0;
  174. }
  175. /*
  176. * disconnect all the devices on the card.
  177. * called from init.c
  178. */
  179. int snd_device_disconnect_all(struct snd_card *card)
  180. {
  181. struct snd_device *dev;
  182. int err = 0;
  183. if (snd_BUG_ON(!card))
  184. return -ENXIO;
  185. list_for_each_entry_reverse(dev, &card->devices, list) {
  186. if (__snd_device_disconnect(dev) < 0)
  187. err = -ENXIO;
  188. }
  189. return err;
  190. }
  191. /*
  192. * release all the devices on the card.
  193. * called from init.c
  194. */
  195. void snd_device_free_all(struct snd_card *card)
  196. {
  197. struct snd_device *dev, *next;
  198. if (snd_BUG_ON(!card))
  199. return;
  200. list_for_each_entry_safe_reverse(dev, next, &card->devices, list)
  201. __snd_device_free(dev);
  202. }