device.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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)
  51. return -ENOMEM;
  52. INIT_LIST_HEAD(&dev->list);
  53. dev->card = card;
  54. dev->type = type;
  55. dev->state = SNDRV_DEV_BUILD;
  56. dev->device_data = device_data;
  57. dev->ops = ops;
  58. /* insert the entry in an incrementally sorted list */
  59. list_for_each_prev(p, &card->devices) {
  60. struct snd_device *pdev = list_entry(p, struct snd_device, list);
  61. if ((unsigned int)pdev->type <= (unsigned int)type)
  62. break;
  63. }
  64. list_add(&dev->list, p);
  65. return 0;
  66. }
  67. EXPORT_SYMBOL(snd_device_new);
  68. static void __snd_device_disconnect(struct snd_device *dev)
  69. {
  70. if (dev->state == SNDRV_DEV_REGISTERED) {
  71. if (dev->ops->dev_disconnect &&
  72. dev->ops->dev_disconnect(dev))
  73. dev_err(dev->card->dev, "device disconnect failure\n");
  74. dev->state = SNDRV_DEV_DISCONNECTED;
  75. }
  76. }
  77. static void __snd_device_free(struct snd_device *dev)
  78. {
  79. /* unlink */
  80. list_del(&dev->list);
  81. __snd_device_disconnect(dev);
  82. if (dev->ops->dev_free) {
  83. if (dev->ops->dev_free(dev))
  84. dev_err(dev->card->dev, "device free failure\n");
  85. }
  86. kfree(dev);
  87. }
  88. static struct snd_device *look_for_dev(struct snd_card *card, void *device_data)
  89. {
  90. struct snd_device *dev;
  91. list_for_each_entry(dev, &card->devices, list)
  92. if (dev->device_data == device_data)
  93. return dev;
  94. return NULL;
  95. }
  96. /**
  97. * snd_device_disconnect - disconnect the device
  98. * @card: the card instance
  99. * @device_data: the data pointer to disconnect
  100. *
  101. * Turns the device into the disconnection state, invoking
  102. * dev_disconnect callback, if the device was already registered.
  103. *
  104. * Usually called from snd_card_disconnect().
  105. *
  106. * Return: Zero if successful, or a negative error code on failure or if the
  107. * device not found.
  108. */
  109. void snd_device_disconnect(struct snd_card *card, void *device_data)
  110. {
  111. struct snd_device *dev;
  112. if (snd_BUG_ON(!card || !device_data))
  113. return;
  114. dev = look_for_dev(card, device_data);
  115. if (dev)
  116. __snd_device_disconnect(dev);
  117. else
  118. dev_dbg(card->dev, "device disconnect %p (from %pF), not found\n",
  119. device_data, __builtin_return_address(0));
  120. }
  121. EXPORT_SYMBOL_GPL(snd_device_disconnect);
  122. /**
  123. * snd_device_free - release the device from the card
  124. * @card: the card instance
  125. * @device_data: the data pointer to release
  126. *
  127. * Removes the device from the list on the card and invokes the
  128. * callbacks, dev_disconnect and dev_free, corresponding to the state.
  129. * Then release the device.
  130. */
  131. void snd_device_free(struct snd_card *card, void *device_data)
  132. {
  133. struct snd_device *dev;
  134. if (snd_BUG_ON(!card || !device_data))
  135. return;
  136. dev = look_for_dev(card, device_data);
  137. if (dev)
  138. __snd_device_free(dev);
  139. else
  140. dev_dbg(card->dev, "device free %p (from %pF), not found\n",
  141. device_data, __builtin_return_address(0));
  142. }
  143. EXPORT_SYMBOL(snd_device_free);
  144. static int __snd_device_register(struct snd_device *dev)
  145. {
  146. if (dev->state == SNDRV_DEV_BUILD) {
  147. if (dev->ops->dev_register) {
  148. int err = dev->ops->dev_register(dev);
  149. if (err < 0)
  150. return err;
  151. }
  152. dev->state = SNDRV_DEV_REGISTERED;
  153. }
  154. return 0;
  155. }
  156. /**
  157. * snd_device_register - register the device
  158. * @card: the card instance
  159. * @device_data: the data pointer to register
  160. *
  161. * Registers the device which was already created via
  162. * snd_device_new(). Usually this is called from snd_card_register(),
  163. * but it can be called later if any new devices are created after
  164. * invocation of snd_card_register().
  165. *
  166. * Return: Zero if successful, or a negative error code on failure or if the
  167. * device not found.
  168. */
  169. int snd_device_register(struct snd_card *card, void *device_data)
  170. {
  171. struct snd_device *dev;
  172. if (snd_BUG_ON(!card || !device_data))
  173. return -ENXIO;
  174. dev = look_for_dev(card, device_data);
  175. if (dev)
  176. return __snd_device_register(dev);
  177. snd_BUG();
  178. return -ENXIO;
  179. }
  180. EXPORT_SYMBOL(snd_device_register);
  181. /*
  182. * register all the devices on the card.
  183. * called from init.c
  184. */
  185. int snd_device_register_all(struct snd_card *card)
  186. {
  187. struct snd_device *dev;
  188. int err;
  189. if (snd_BUG_ON(!card))
  190. return -ENXIO;
  191. list_for_each_entry(dev, &card->devices, list) {
  192. err = __snd_device_register(dev);
  193. if (err < 0)
  194. return err;
  195. }
  196. return 0;
  197. }
  198. /*
  199. * disconnect all the devices on the card.
  200. * called from init.c
  201. */
  202. void snd_device_disconnect_all(struct snd_card *card)
  203. {
  204. struct snd_device *dev;
  205. if (snd_BUG_ON(!card))
  206. return;
  207. list_for_each_entry_reverse(dev, &card->devices, list)
  208. __snd_device_disconnect(dev);
  209. }
  210. /*
  211. * release all the devices on the card.
  212. * called from init.c
  213. */
  214. void snd_device_free_all(struct snd_card *card)
  215. {
  216. struct snd_device *dev, *next;
  217. if (snd_BUG_ON(!card))
  218. return;
  219. list_for_each_entry_safe_reverse(dev, next, &card->devices, list)
  220. __snd_device_free(dev);
  221. }