device.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. * Device management 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/slab.h>
  23. #include <linux/time.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. * Returns zero if successful, or a negative error code on failure.
  41. */
  42. int snd_device_new(struct snd_card *card, snd_device_type_t type,
  43. void *device_data, struct snd_device_ops *ops)
  44. {
  45. struct snd_device *dev;
  46. snd_assert(card != NULL, return -ENXIO);
  47. snd_assert(device_data != NULL, return -ENXIO);
  48. snd_assert(ops != NULL, return -ENXIO);
  49. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  50. if (dev == NULL) {
  51. snd_printk(KERN_ERR "Cannot allocate device\n");
  52. return -ENOMEM;
  53. }
  54. dev->card = card;
  55. dev->type = type;
  56. dev->state = SNDRV_DEV_BUILD;
  57. dev->device_data = device_data;
  58. dev->ops = ops;
  59. list_add(&dev->list, &card->devices); /* add to the head of list */
  60. return 0;
  61. }
  62. EXPORT_SYMBOL(snd_device_new);
  63. /**
  64. * snd_device_free - release the device from the card
  65. * @card: the card instance
  66. * @device_data: the data pointer to release
  67. *
  68. * Removes the device from the list on the card and invokes the
  69. * callback, dev_unregister or dev_free, corresponding to the state.
  70. * Then release the device.
  71. *
  72. * Returns zero if successful, or a negative error code on failure or if the
  73. * device not found.
  74. */
  75. int snd_device_free(struct snd_card *card, void *device_data)
  76. {
  77. struct list_head *list;
  78. struct snd_device *dev;
  79. snd_assert(card != NULL, return -ENXIO);
  80. snd_assert(device_data != NULL, return -ENXIO);
  81. list_for_each(list, &card->devices) {
  82. dev = snd_device(list);
  83. if (dev->device_data != device_data)
  84. continue;
  85. /* unlink */
  86. list_del(&dev->list);
  87. if ((dev->state == SNDRV_DEV_REGISTERED ||
  88. dev->state == SNDRV_DEV_DISCONNECTED) &&
  89. dev->ops->dev_unregister) {
  90. if (dev->ops->dev_unregister(dev))
  91. snd_printk(KERN_ERR "device unregister failure\n");
  92. } else {
  93. if (dev->ops->dev_free) {
  94. if (dev->ops->dev_free(dev))
  95. snd_printk(KERN_ERR "device free failure\n");
  96. }
  97. }
  98. kfree(dev);
  99. return 0;
  100. }
  101. snd_printd("device free %p (from %p), not found\n", device_data,
  102. __builtin_return_address(0));
  103. return -ENXIO;
  104. }
  105. EXPORT_SYMBOL(snd_device_free);
  106. /**
  107. * snd_device_disconnect - disconnect the device
  108. * @card: the card instance
  109. * @device_data: the data pointer to disconnect
  110. *
  111. * Turns the device into the disconnection state, invoking
  112. * dev_disconnect callback, if the device was already registered.
  113. *
  114. * Usually called from snd_card_disconnect().
  115. *
  116. * Returns zero if successful, or a negative error code on failure or if the
  117. * device not found.
  118. */
  119. int snd_device_disconnect(struct snd_card *card, void *device_data)
  120. {
  121. struct list_head *list;
  122. struct snd_device *dev;
  123. snd_assert(card != NULL, return -ENXIO);
  124. snd_assert(device_data != NULL, return -ENXIO);
  125. list_for_each(list, &card->devices) {
  126. dev = snd_device(list);
  127. if (dev->device_data != device_data)
  128. continue;
  129. if (dev->state == SNDRV_DEV_REGISTERED &&
  130. dev->ops->dev_disconnect) {
  131. if (dev->ops->dev_disconnect(dev))
  132. snd_printk(KERN_ERR "device disconnect failure\n");
  133. dev->state = SNDRV_DEV_DISCONNECTED;
  134. }
  135. return 0;
  136. }
  137. snd_printd("device disconnect %p (from %p), not found\n", device_data,
  138. __builtin_return_address(0));
  139. return -ENXIO;
  140. }
  141. /**
  142. * snd_device_register - register the device
  143. * @card: the card instance
  144. * @device_data: the data pointer to register
  145. *
  146. * Registers the device which was already created via
  147. * snd_device_new(). Usually this is called from snd_card_register(),
  148. * but it can be called later if any new devices are created after
  149. * invocation of snd_card_register().
  150. *
  151. * Returns zero if successful, or a negative error code on failure or if the
  152. * device not found.
  153. */
  154. int snd_device_register(struct snd_card *card, void *device_data)
  155. {
  156. struct list_head *list;
  157. struct snd_device *dev;
  158. int err;
  159. snd_assert(card != NULL, return -ENXIO);
  160. snd_assert(device_data != NULL, return -ENXIO);
  161. list_for_each(list, &card->devices) {
  162. dev = snd_device(list);
  163. if (dev->device_data != device_data)
  164. continue;
  165. if (dev->state == SNDRV_DEV_BUILD && dev->ops->dev_register) {
  166. if ((err = dev->ops->dev_register(dev)) < 0)
  167. return err;
  168. dev->state = SNDRV_DEV_REGISTERED;
  169. return 0;
  170. }
  171. snd_printd("snd_device_register busy\n");
  172. return -EBUSY;
  173. }
  174. snd_BUG();
  175. return -ENXIO;
  176. }
  177. EXPORT_SYMBOL(snd_device_register);
  178. /*
  179. * register all the devices on the card.
  180. * called from init.c
  181. */
  182. int snd_device_register_all(struct snd_card *card)
  183. {
  184. struct list_head *list;
  185. struct snd_device *dev;
  186. int err;
  187. snd_assert(card != NULL, return -ENXIO);
  188. list_for_each(list, &card->devices) {
  189. dev = snd_device(list);
  190. if (dev->state == SNDRV_DEV_BUILD && dev->ops->dev_register) {
  191. if ((err = dev->ops->dev_register(dev)) < 0)
  192. return err;
  193. dev->state = SNDRV_DEV_REGISTERED;
  194. }
  195. }
  196. return 0;
  197. }
  198. /*
  199. * disconnect all the devices on the card.
  200. * called from init.c
  201. */
  202. int snd_device_disconnect_all(struct snd_card *card)
  203. {
  204. struct snd_device *dev;
  205. struct list_head *list;
  206. int err = 0;
  207. snd_assert(card != NULL, return -ENXIO);
  208. list_for_each(list, &card->devices) {
  209. dev = snd_device(list);
  210. if (snd_device_disconnect(card, dev->device_data) < 0)
  211. err = -ENXIO;
  212. }
  213. return err;
  214. }
  215. /*
  216. * release all the devices on the card.
  217. * called from init.c
  218. */
  219. int snd_device_free_all(struct snd_card *card, snd_device_cmd_t cmd)
  220. {
  221. struct snd_device *dev;
  222. struct list_head *list;
  223. int err;
  224. unsigned int range_low, range_high;
  225. snd_assert(card != NULL, return -ENXIO);
  226. range_low = cmd * SNDRV_DEV_TYPE_RANGE_SIZE;
  227. range_high = range_low + SNDRV_DEV_TYPE_RANGE_SIZE - 1;
  228. __again:
  229. list_for_each(list, &card->devices) {
  230. dev = snd_device(list);
  231. if (dev->type >= range_low && dev->type <= range_high) {
  232. if ((err = snd_device_free(card, dev->device_data)) < 0)
  233. return err;
  234. goto __again;
  235. }
  236. }
  237. return 0;
  238. }