device.c 6.6 KB

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