hpios.c 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /******************************************************************************
  2. AudioScience HPI driver
  3. Copyright (C) 1997-2012 AudioScience Inc. <support@audioscience.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of version 2 of the GNU General Public License as
  6. published by the Free Software Foundation;
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  14. HPI Operating System function implementation for Linux
  15. (C) Copyright AudioScience Inc. 1997-2003
  16. ******************************************************************************/
  17. #define SOURCEFILE_NAME "hpios.c"
  18. #include "hpi_internal.h"
  19. #include "hpidebug.h"
  20. #include <linux/delay.h>
  21. #include <linux/sched.h>
  22. void hpios_delay_micro_seconds(u32 num_micro_sec)
  23. {
  24. if ((usecs_to_jiffies(num_micro_sec) > 1) && !in_interrupt()) {
  25. /* MUST NOT SCHEDULE IN INTERRUPT CONTEXT! */
  26. schedule_timeout_uninterruptible(usecs_to_jiffies
  27. (num_micro_sec));
  28. } else if (num_micro_sec <= 2000)
  29. udelay(num_micro_sec);
  30. else
  31. mdelay(num_micro_sec / 1000);
  32. }
  33. /** Allocate an area of locked memory for bus master DMA operations.
  34. If allocation fails, return 1, and *pMemArea.size = 0
  35. */
  36. u16 hpios_locked_mem_alloc(struct consistent_dma_area *p_mem_area, u32 size,
  37. struct pci_dev *pdev)
  38. {
  39. /*?? any benefit in using managed dmam_alloc_coherent? */
  40. p_mem_area->vaddr =
  41. dma_alloc_coherent(&pdev->dev, size, &p_mem_area->dma_handle,
  42. GFP_DMA32 | GFP_KERNEL);
  43. if (p_mem_area->vaddr) {
  44. HPI_DEBUG_LOG(DEBUG, "allocated %d bytes, dma 0x%x vma %p\n",
  45. size, (unsigned int)p_mem_area->dma_handle,
  46. p_mem_area->vaddr);
  47. p_mem_area->pdev = &pdev->dev;
  48. p_mem_area->size = size;
  49. return 0;
  50. } else {
  51. HPI_DEBUG_LOG(WARNING,
  52. "failed to allocate %d bytes locked memory\n", size);
  53. p_mem_area->size = 0;
  54. return 1;
  55. }
  56. }
  57. u16 hpios_locked_mem_free(struct consistent_dma_area *p_mem_area)
  58. {
  59. if (p_mem_area->size) {
  60. dma_free_coherent(p_mem_area->pdev, p_mem_area->size,
  61. p_mem_area->vaddr, p_mem_area->dma_handle);
  62. HPI_DEBUG_LOG(DEBUG, "freed %lu bytes, dma 0x%x vma %p\n",
  63. (unsigned long)p_mem_area->size,
  64. (unsigned int)p_mem_area->dma_handle,
  65. p_mem_area->vaddr);
  66. p_mem_area->size = 0;
  67. return 0;
  68. } else {
  69. return 1;
  70. }
  71. }