devdma.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. * linux/sound/arm/devdma.c
  3. *
  4. * Copyright (C) 2003-2004 Russell King, All rights reserved.
  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 version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * ARM DMA shim for ALSA.
  11. */
  12. #include <linux/device.h>
  13. #include <linux/dma-mapping.h>
  14. #include <sound/driver.h>
  15. #include <sound/core.h>
  16. #include <sound/pcm.h>
  17. #include "devdma.h"
  18. void devdma_hw_free(struct device *dev, struct snd_pcm_substream *substream)
  19. {
  20. struct snd_pcm_runtime *runtime = substream->runtime;
  21. struct snd_dma_buffer *buf = runtime->dma_buffer_p;
  22. if (runtime->dma_area == NULL)
  23. return;
  24. if (buf != &substream->dma_buffer) {
  25. dma_free_coherent(buf->dev.dev, buf->bytes, buf->area, buf->addr);
  26. kfree(runtime->dma_buffer_p);
  27. }
  28. snd_pcm_set_runtime_buffer(substream, NULL);
  29. }
  30. int devdma_hw_alloc(struct device *dev, struct snd_pcm_substream *substream, size_t size)
  31. {
  32. struct snd_pcm_runtime *runtime = substream->runtime;
  33. struct snd_dma_buffer *buf = runtime->dma_buffer_p;
  34. int ret = 0;
  35. if (buf) {
  36. if (buf->bytes >= size)
  37. goto out;
  38. devdma_hw_free(dev, substream);
  39. }
  40. if (substream->dma_buffer.area != NULL && substream->dma_buffer.bytes >= size) {
  41. buf = &substream->dma_buffer;
  42. } else {
  43. buf = kmalloc(sizeof(struct snd_dma_buffer), GFP_KERNEL);
  44. if (!buf)
  45. goto nomem;
  46. buf->dev.type = SNDRV_DMA_TYPE_DEV;
  47. buf->dev.dev = dev;
  48. buf->area = dma_alloc_coherent(dev, size, &buf->addr, GFP_KERNEL);
  49. buf->bytes = size;
  50. buf->private_data = NULL;
  51. if (!buf->area)
  52. goto free;
  53. }
  54. snd_pcm_set_runtime_buffer(substream, buf);
  55. ret = 1;
  56. out:
  57. runtime->dma_bytes = size;
  58. return ret;
  59. free:
  60. kfree(buf);
  61. nomem:
  62. return -ENOMEM;
  63. }
  64. int devdma_mmap(struct device *dev, struct snd_pcm_substream *substream, struct vm_area_struct *vma)
  65. {
  66. struct snd_pcm_runtime *runtime = substream->runtime;
  67. return dma_mmap_coherent(dev, vma, runtime->dma_area, runtime->dma_addr, runtime->dma_bytes);
  68. }