device.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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(snd_card_t *card, snd_device_type_t type,
  43. void *device_data, snd_device_ops_t *ops)
  44. {
  45. snd_device_t *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 = kcalloc(1, sizeof(*dev), GFP_KERNEL);
  50. if (dev == NULL)
  51. return -ENOMEM;
  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. /**
  61. * snd_device_free - release the device from the card
  62. * @card: the card instance
  63. * @device_data: the data pointer to release
  64. *
  65. * Removes the device from the list on the card and invokes the
  66. * callback, dev_unregister or dev_free, corresponding to the state.
  67. * Then release the device.
  68. *
  69. * Returns zero if successful, or a negative error code on failure or if the
  70. * device not found.
  71. */
  72. int snd_device_free(snd_card_t *card, void *device_data)
  73. {
  74. struct list_head *list;
  75. snd_device_t *dev;
  76. snd_assert(card != NULL, return -ENXIO);
  77. snd_assert(device_data != NULL, return -ENXIO);
  78. list_for_each(list, &card->devices) {
  79. dev = snd_device(list);
  80. if (dev->device_data != device_data)
  81. continue;
  82. /* unlink */
  83. list_del(&dev->list);
  84. if ((dev->state == SNDRV_DEV_REGISTERED || dev->state == SNDRV_DEV_DISCONNECTED) &&
  85. dev->ops->dev_unregister) {
  86. if (dev->ops->dev_unregister(dev))
  87. snd_printk(KERN_ERR "device unregister failure\n");
  88. } else {
  89. if (dev->ops->dev_free) {
  90. if (dev->ops->dev_free(dev))
  91. snd_printk(KERN_ERR "device free failure\n");
  92. }
  93. }
  94. kfree(dev);
  95. return 0;
  96. }
  97. snd_printd("device free %p (from %p), not found\n", device_data, __builtin_return_address(0));
  98. return -ENXIO;
  99. }
  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(snd_card_t *card, void *device_data)
  114. {
  115. struct list_head *list;
  116. snd_device_t *dev;
  117. snd_assert(card != NULL, return -ENXIO);
  118. snd_assert(device_data != NULL, return -ENXIO);
  119. list_for_each(list, &card->devices) {
  120. dev = snd_device(list);
  121. if (dev->device_data != device_data)
  122. continue;
  123. if (dev->state == SNDRV_DEV_REGISTERED && dev->ops->dev_disconnect) {
  124. if (dev->ops->dev_disconnect(dev))
  125. snd_printk(KERN_ERR "device disconnect failure\n");
  126. dev->state = SNDRV_DEV_DISCONNECTED;
  127. }
  128. return 0;
  129. }
  130. snd_printd("device disconnect %p (from %p), not found\n", device_data, __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(snd_card_t *card, void *device_data)
  147. {
  148. struct list_head *list;
  149. snd_device_t *dev;
  150. int err;
  151. snd_assert(card != NULL, return -ENXIO);
  152. snd_assert(device_data != NULL, return -ENXIO);
  153. list_for_each(list, &card->devices) {
  154. dev = snd_device(list);
  155. if (dev->device_data != device_data)
  156. continue;
  157. if (dev->state == SNDRV_DEV_BUILD && dev->ops->dev_register) {
  158. if ((err = dev->ops->dev_register(dev)) < 0)
  159. return err;
  160. dev->state = SNDRV_DEV_REGISTERED;
  161. return 0;
  162. }
  163. return -EBUSY;
  164. }
  165. snd_BUG();
  166. return -ENXIO;
  167. }
  168. /*
  169. * register all the devices on the card.
  170. * called from init.c
  171. */
  172. int snd_device_register_all(snd_card_t *card)
  173. {
  174. struct list_head *list;
  175. snd_device_t *dev;
  176. int err;
  177. snd_assert(card != NULL, return -ENXIO);
  178. list_for_each(list, &card->devices) {
  179. dev = snd_device(list);
  180. if (dev->state == SNDRV_DEV_BUILD && dev->ops->dev_register) {
  181. if ((err = dev->ops->dev_register(dev)) < 0)
  182. return err;
  183. dev->state = SNDRV_DEV_REGISTERED;
  184. }
  185. }
  186. return 0;
  187. }
  188. /*
  189. * disconnect all the devices on the card.
  190. * called from init.c
  191. */
  192. int snd_device_disconnect_all(snd_card_t *card)
  193. {
  194. snd_device_t *dev;
  195. struct list_head *list;
  196. int err = 0;
  197. snd_assert(card != NULL, return -ENXIO);
  198. list_for_each(list, &card->devices) {
  199. dev = snd_device(list);
  200. if (snd_device_disconnect(card, dev->device_data) < 0)
  201. err = -ENXIO;
  202. }
  203. return err;
  204. }
  205. /*
  206. * release all the devices on the card.
  207. * called from init.c
  208. */
  209. int snd_device_free_all(snd_card_t *card, snd_device_cmd_t cmd)
  210. {
  211. snd_device_t *dev;
  212. struct list_head *list;
  213. int err;
  214. unsigned int range_low, range_high;
  215. snd_assert(card != NULL, return -ENXIO);
  216. range_low = cmd * SNDRV_DEV_TYPE_RANGE_SIZE;
  217. range_high = range_low + SNDRV_DEV_TYPE_RANGE_SIZE - 1;
  218. __again:
  219. list_for_each(list, &card->devices) {
  220. dev = snd_device(list);
  221. if (dev->type >= range_low && dev->type <= range_high) {
  222. if ((err = snd_device_free(card, dev->device_data)) < 0)
  223. return err;
  224. goto __again;
  225. }
  226. }
  227. return 0;
  228. }