hpios.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /******************************************************************************
  2. AudioScience HPI driver
  3. Copyright (C) 1997-2010 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. void hpios_locked_mem_init(void)
  34. {
  35. }
  36. /** Allocated an area of locked memory for bus master DMA operations.
  37. On error, return -ENOMEM, and *pMemArea.size = 0
  38. */
  39. u16 hpios_locked_mem_alloc(struct consistent_dma_area *p_mem_area, u32 size,
  40. struct pci_dev *pdev)
  41. {
  42. /*?? any benefit in using managed dmam_alloc_coherent? */
  43. p_mem_area->vaddr =
  44. dma_alloc_coherent(&pdev->dev, size, &p_mem_area->dma_handle,
  45. GFP_DMA32 | GFP_KERNEL);
  46. if (p_mem_area->vaddr) {
  47. HPI_DEBUG_LOG(DEBUG, "allocated %d bytes, dma 0x%x vma %p\n",
  48. size, (unsigned int)p_mem_area->dma_handle,
  49. p_mem_area->vaddr);
  50. p_mem_area->pdev = &pdev->dev;
  51. p_mem_area->size = size;
  52. return 0;
  53. } else {
  54. HPI_DEBUG_LOG(WARNING,
  55. "failed to allocate %d bytes locked memory\n", size);
  56. p_mem_area->size = 0;
  57. return -ENOMEM;
  58. }
  59. }
  60. u16 hpios_locked_mem_free(struct consistent_dma_area *p_mem_area)
  61. {
  62. if (p_mem_area->size) {
  63. dma_free_coherent(p_mem_area->pdev, p_mem_area->size,
  64. p_mem_area->vaddr, p_mem_area->dma_handle);
  65. HPI_DEBUG_LOG(DEBUG, "freed %lu bytes, dma 0x%x vma %p\n",
  66. (unsigned long)p_mem_area->size,
  67. (unsigned int)p_mem_area->dma_handle,
  68. p_mem_area->vaddr);
  69. p_mem_area->size = 0;
  70. return 0;
  71. } else {
  72. return 1;
  73. }
  74. }
  75. void hpios_locked_mem_free_all(void)
  76. {
  77. }
  78. void __iomem *hpios_map_io(struct pci_dev *pci_dev, int idx,
  79. unsigned int length)
  80. {
  81. HPI_DEBUG_LOG(DEBUG, "mapping %d %s %08llx-%08llx %04llx len 0x%x\n",
  82. idx, pci_dev->resource[idx].name,
  83. (unsigned long long)pci_resource_start(pci_dev, idx),
  84. (unsigned long long)pci_resource_end(pci_dev, idx),
  85. (unsigned long long)pci_resource_flags(pci_dev, idx), length);
  86. if (!(pci_resource_flags(pci_dev, idx) & IORESOURCE_MEM)) {
  87. HPI_DEBUG_LOG(ERROR, "not an io memory resource\n");
  88. return NULL;
  89. }
  90. if (length > pci_resource_len(pci_dev, idx)) {
  91. HPI_DEBUG_LOG(ERROR, "resource too small for requested %d \n",
  92. length);
  93. return NULL;
  94. }
  95. return ioremap(pci_resource_start(pci_dev, idx), length);
  96. }