device.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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/errno.h>
  24. #include <sound/core.h>
  25. /**
  26. * snd_device_new - create an ALSA device component
  27. * @card: the card instance
  28. * @type: the device type, SNDRV_DEV_XXX
  29. * @device_data: the data pointer of this device
  30. * @ops: the operator table
  31. *
  32. * Creates a new device component for the given data pointer.
  33. * The device will be assigned to the card and managed together
  34. * by the card.
  35. *
  36. * The data pointer plays a role as the identifier, too, so the
  37. * pointer address must be unique and unchanged.
  38. *
  39. * Returns zero if successful, or a negative error code on failure.
  40. */
  41. int snd_device_new(struct snd_card *card, snd_device_type_t type,
  42. void *device_data, struct snd_device_ops *ops)
  43. {
  44. struct snd_device *dev;
  45. if (snd_BUG_ON(!card || !device_data || !ops))
  46. return -ENXIO;
  47. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  48. if (dev == NULL) {
  49. snd_printk(KERN_ERR "Cannot allocate device\n");
  50. return -ENOMEM;
  51. }
  52. dev->card = card;
  53. dev->type = type;
  54. dev->state = SNDRV_DEV_BUILD;
  55. dev->device_data = device_data;
  56. dev->ops = ops;
  57. list_add(&dev->list, &card->devices); /* add to the head of list */
  58. return 0;
  59. }
  60. EXPORT_SYMBOL(snd_device_new);
  61. /**
  62. * snd_device_free - release the device from the card
  63. * @card: the card instance
  64. * @device_data: the data pointer to release
  65. *
  66. * Removes the device from the list on the card and invokes the
  67. * callbacks, dev_disconnect and dev_free, corresponding to the state.
  68. * Then release the device.
  69. *
  70. * Returns zero if successful, or a negative error code on failure or if the
  71. * device not found.
  72. */
  73. int snd_device_free(struct snd_card *card, void *device_data)
  74. {
  75. struct snd_device *dev;
  76. if (snd_BUG_ON(!card || !device_data))
  77. return -ENXIO;
  78. list_for_each_entry(dev, &card->devices, list) {
  79. if (dev->device_data != device_data)
  80. continue;
  81. /* unlink */
  82. list_del(&dev->list);
  83. if (dev->state == SNDRV_DEV_REGISTERED &&
  84. dev->ops->dev_disconnect)
  85. if (dev->ops->dev_disconnect(dev))
  86. snd_printk(KERN_ERR
  87. "device disconnect failure\n");
  88. if (dev->ops->dev_free) {
  89. if (dev->ops->dev_free(dev))
  90. snd_printk(KERN_ERR "device free failure\n");
  91. }
  92. kfree(dev);
  93. return 0;
  94. }
  95. snd_printd("device free %p (from %pF), not found\n", device_data,
  96. __builtin_return_address(0));
  97. return -ENXIO;
  98. }
  99. EXPORT_SYMBOL(snd_device_free);
  100. /**
  101. * snd_device_disconnect - disconnect the device
  102. * @card: the card instance
  103. * @device_data: the data pointer to disconnect
  104. *
  105. * Turns the device into the disconnection state, invoking
  106. * dev_disconnect callback, if the device was already registered.
  107. *
  108. * Usually called from snd_card_disconnect().
  109. *
  110. * Returns zero if successful, or a negative error code on failure or if the
  111. * device not found.
  112. */
  113. int snd_device_disconnect(struct snd_card *card, void *device_data)
  114. {
  115. struct snd_device *dev;
  116. if (snd_BUG_ON(!card || !device_data))
  117. return -ENXIO;
  118. list_for_each_entry(dev, &card->devices, list) {
  119. if (dev->device_data != device_data)
  120. continue;
  121. if (dev->state == SNDRV_DEV_REGISTERED &&
  122. dev->ops->dev_disconnect) {
  123. if (dev->ops->dev_disconnect(dev))
  124. snd_printk(KERN_ERR "device disconnect failure\n");
  125. dev->state = SNDRV_DEV_DISCONNECTED;
  126. }
  127. return 0;
  128. }
  129. snd_printd("device disconnect %p (from %pF), not found\n", device_data,
  130. __builtin_return_address(0));
  131. return -ENXIO;
  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. * Returns 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. int err;
  150. if (snd_BUG_ON(!card || !device_data))
  151. return -ENXIO;
  152. list_for_each_entry(dev, &card->devices, list) {
  153. if (dev->device_data != device_data)
  154. continue;
  155. if (dev->state == SNDRV_DEV_BUILD && dev->ops->dev_register) {
  156. if ((err = dev->ops->dev_register(dev)) < 0)
  157. return err;
  158. dev->state = SNDRV_DEV_REGISTERED;
  159. return 0;
  160. }
  161. snd_printd("snd_device_register busy\n");
  162. return -EBUSY;
  163. }
  164. snd_BUG();
  165. return -ENXIO;
  166. }
  167. EXPORT_SYMBOL(snd_device_register);
  168. /*
  169. * register all the devices on the card.
  170. * called from init.c
  171. */
  172. int snd_device_register_all(struct snd_card *card)
  173. {
  174. struct snd_device *dev;
  175. int err;
  176. if (snd_BUG_ON(!card))
  177. return -ENXIO;
  178. list_for_each_entry(dev, &card->devices, list) {
  179. if (dev->state == SNDRV_DEV_BUILD && dev->ops->dev_register) {
  180. if ((err = dev->ops->dev_register(dev)) < 0)
  181. return err;
  182. dev->state = SNDRV_DEV_REGISTERED;
  183. }
  184. }
  185. return 0;
  186. }
  187. /*
  188. * disconnect all the devices on the card.
  189. * called from init.c
  190. */
  191. int snd_device_disconnect_all(struct snd_card *card)
  192. {
  193. struct snd_device *dev;
  194. int err = 0;
  195. if (snd_BUG_ON(!card))
  196. return -ENXIO;
  197. list_for_each_entry(dev, &card->devices, list) {
  198. if (snd_device_disconnect(card, dev->device_data) < 0)
  199. err = -ENXIO;
  200. }
  201. return err;
  202. }
  203. /*
  204. * release all the devices on the card.
  205. * called from init.c
  206. */
  207. int snd_device_free_all(struct snd_card *card, snd_device_cmd_t cmd)
  208. {
  209. struct snd_device *dev;
  210. int err;
  211. unsigned int range_low, range_high, type;
  212. if (snd_BUG_ON(!card))
  213. return -ENXIO;
  214. range_low = (__force unsigned int)cmd * SNDRV_DEV_TYPE_RANGE_SIZE;
  215. range_high = range_low + SNDRV_DEV_TYPE_RANGE_SIZE - 1;
  216. __again:
  217. list_for_each_entry(dev, &card->devices, list) {
  218. type = (__force unsigned int)dev->type;
  219. if (type >= range_low && type <= range_high) {
  220. if ((err = snd_device_free(card, dev->device_data)) < 0)
  221. return err;
  222. goto __again;
  223. }
  224. }
  225. return 0;
  226. }