Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /******************************************************************************
0003 
0004     AudioScience HPI driver
0005     Copyright (C) 1997-2012  AudioScience Inc. <support@audioscience.com>
0006 
0007 
0008 HPI Operating System function implementation for Linux
0009 
0010 (C) Copyright AudioScience Inc. 1997-2003
0011 ******************************************************************************/
0012 #define SOURCEFILE_NAME "hpios.c"
0013 #include "hpi_internal.h"
0014 #include "hpidebug.h"
0015 #include <linux/delay.h>
0016 #include <linux/sched.h>
0017 
0018 void hpios_delay_micro_seconds(u32 num_micro_sec)
0019 {
0020     if ((usecs_to_jiffies(num_micro_sec) > 1) && !in_interrupt()) {
0021         /* MUST NOT SCHEDULE IN INTERRUPT CONTEXT! */
0022         schedule_timeout_uninterruptible(usecs_to_jiffies
0023             (num_micro_sec));
0024     } else if (num_micro_sec <= 2000)
0025         udelay(num_micro_sec);
0026     else
0027         mdelay(num_micro_sec / 1000);
0028 
0029 }
0030 
0031 /** Allocate an area of locked memory for bus master DMA operations.
0032 
0033 If allocation fails, return 1, and *pMemArea.size = 0
0034 */
0035 u16 hpios_locked_mem_alloc(struct consistent_dma_area *p_mem_area, u32 size,
0036     struct pci_dev *pdev)
0037 {
0038     /*?? any benefit in using managed dmam_alloc_coherent? */
0039     p_mem_area->vaddr =
0040         dma_alloc_coherent(&pdev->dev, size, &p_mem_area->dma_handle,
0041         GFP_KERNEL);
0042 
0043     if (p_mem_area->vaddr) {
0044         HPI_DEBUG_LOG(DEBUG, "allocated %d bytes, dma 0x%x vma %p\n",
0045             size, (unsigned int)p_mem_area->dma_handle,
0046             p_mem_area->vaddr);
0047         p_mem_area->pdev = &pdev->dev;
0048         p_mem_area->size = size;
0049         return 0;
0050     } else {
0051         HPI_DEBUG_LOG(WARNING,
0052             "failed to allocate %d bytes locked memory\n", size);
0053         p_mem_area->size = 0;
0054         return 1;
0055     }
0056 }
0057 
0058 u16 hpios_locked_mem_free(struct consistent_dma_area *p_mem_area)
0059 {
0060     if (p_mem_area->size) {
0061         dma_free_coherent(p_mem_area->pdev, p_mem_area->size,
0062             p_mem_area->vaddr, p_mem_area->dma_handle);
0063         HPI_DEBUG_LOG(DEBUG, "freed %lu bytes, dma 0x%x vma %p\n",
0064             (unsigned long)p_mem_area->size,
0065             (unsigned int)p_mem_area->dma_handle,
0066             p_mem_area->vaddr);
0067         p_mem_area->size = 0;
0068         return 0;
0069     } else {
0070         return 1;
0071     }
0072 }