xref: /OK3568_Linux_fs/kernel/sound/pci/asihpi/hpios.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0-only */
2*4882a593Smuzhiyun /******************************************************************************
3*4882a593Smuzhiyun 
4*4882a593Smuzhiyun     AudioScience HPI driver
5*4882a593Smuzhiyun     Copyright (C) 1997-2011  AudioScience Inc. <support@audioscience.com>
6*4882a593Smuzhiyun 
7*4882a593Smuzhiyun 
8*4882a593Smuzhiyun HPI Operating System Specific macros for Linux Kernel driver
9*4882a593Smuzhiyun 
10*4882a593Smuzhiyun (C) Copyright AudioScience Inc. 1997-2003
11*4882a593Smuzhiyun ******************************************************************************/
12*4882a593Smuzhiyun #ifndef _HPIOS_H_
13*4882a593Smuzhiyun #define _HPIOS_H_
14*4882a593Smuzhiyun 
15*4882a593Smuzhiyun #undef HPI_OS_LINUX_KERNEL
16*4882a593Smuzhiyun #define HPI_OS_LINUX_KERNEL
17*4882a593Smuzhiyun 
18*4882a593Smuzhiyun #define HPI_OS_DEFINED
19*4882a593Smuzhiyun #define HPI_BUILD_KERNEL_MODE
20*4882a593Smuzhiyun 
21*4882a593Smuzhiyun #include <linux/io.h>
22*4882a593Smuzhiyun #include <linux/ioctl.h>
23*4882a593Smuzhiyun #include <linux/kernel.h>
24*4882a593Smuzhiyun #include <linux/string.h>
25*4882a593Smuzhiyun #include <linux/device.h>
26*4882a593Smuzhiyun #include <linux/firmware.h>
27*4882a593Smuzhiyun #include <linux/interrupt.h>
28*4882a593Smuzhiyun #include <linux/pci.h>
29*4882a593Smuzhiyun #include <linux/mutex.h>
30*4882a593Smuzhiyun 
31*4882a593Smuzhiyun #define HPI_NO_OS_FILE_OPS
32*4882a593Smuzhiyun 
33*4882a593Smuzhiyun /** Details of a memory area allocated with  pci_alloc_consistent
34*4882a593Smuzhiyun Need all info for parameters to pci_free_consistent
35*4882a593Smuzhiyun */
36*4882a593Smuzhiyun struct consistent_dma_area {
37*4882a593Smuzhiyun 	struct device *pdev;
38*4882a593Smuzhiyun 	/* looks like dma-mapping dma_devres ?! */
39*4882a593Smuzhiyun 	size_t size;
40*4882a593Smuzhiyun 	void *vaddr;
41*4882a593Smuzhiyun 	dma_addr_t dma_handle;
42*4882a593Smuzhiyun };
43*4882a593Smuzhiyun 
hpios_locked_mem_get_phys_addr(struct consistent_dma_area * locked_mem_handle,u32 * p_physical_addr)44*4882a593Smuzhiyun static inline u16 hpios_locked_mem_get_phys_addr(struct consistent_dma_area
45*4882a593Smuzhiyun 	*locked_mem_handle, u32 *p_physical_addr)
46*4882a593Smuzhiyun {
47*4882a593Smuzhiyun 	*p_physical_addr = locked_mem_handle->dma_handle;
48*4882a593Smuzhiyun 	return 0;
49*4882a593Smuzhiyun }
50*4882a593Smuzhiyun 
hpios_locked_mem_get_virt_addr(struct consistent_dma_area * locked_mem_handle,void ** pp_virtual_addr)51*4882a593Smuzhiyun static inline u16 hpios_locked_mem_get_virt_addr(struct consistent_dma_area
52*4882a593Smuzhiyun 	*locked_mem_handle, void **pp_virtual_addr)
53*4882a593Smuzhiyun {
54*4882a593Smuzhiyun 	*pp_virtual_addr = locked_mem_handle->vaddr;
55*4882a593Smuzhiyun 	return 0;
56*4882a593Smuzhiyun }
57*4882a593Smuzhiyun 
hpios_locked_mem_valid(struct consistent_dma_area * locked_mem_handle)58*4882a593Smuzhiyun static inline u16 hpios_locked_mem_valid(struct consistent_dma_area
59*4882a593Smuzhiyun 	*locked_mem_handle)
60*4882a593Smuzhiyun {
61*4882a593Smuzhiyun 	return locked_mem_handle->size != 0;
62*4882a593Smuzhiyun }
63*4882a593Smuzhiyun 
64*4882a593Smuzhiyun struct hpi_ioctl_linux {
65*4882a593Smuzhiyun 	void __user *phm;
66*4882a593Smuzhiyun 	void __user *phr;
67*4882a593Smuzhiyun };
68*4882a593Smuzhiyun 
69*4882a593Smuzhiyun /* Conflict?: H is already used by a number of drivers hid, bluetooth hci,
70*4882a593Smuzhiyun    and some sound drivers sb16, hdsp, emu10k. AFAIK 0xFC is unused command
71*4882a593Smuzhiyun */
72*4882a593Smuzhiyun #define HPI_IOCTL_LINUX _IOWR('H', 0xFC, struct hpi_ioctl_linux)
73*4882a593Smuzhiyun 
74*4882a593Smuzhiyun #define HPI_DEBUG_FLAG_ERROR   KERN_ERR
75*4882a593Smuzhiyun #define HPI_DEBUG_FLAG_WARNING KERN_WARNING
76*4882a593Smuzhiyun #define HPI_DEBUG_FLAG_NOTICE  KERN_NOTICE
77*4882a593Smuzhiyun #define HPI_DEBUG_FLAG_INFO    KERN_INFO
78*4882a593Smuzhiyun #define HPI_DEBUG_FLAG_DEBUG   KERN_DEBUG
79*4882a593Smuzhiyun #define HPI_DEBUG_FLAG_VERBOSE KERN_DEBUG	/* kernel has no verbose */
80*4882a593Smuzhiyun 
81*4882a593Smuzhiyun #include <linux/spinlock.h>
82*4882a593Smuzhiyun 
83*4882a593Smuzhiyun #define HPI_LOCKING
84*4882a593Smuzhiyun 
85*4882a593Smuzhiyun struct hpios_spinlock {
86*4882a593Smuzhiyun 	spinlock_t lock;	/* SEE hpios_spinlock */
87*4882a593Smuzhiyun 	int lock_context;
88*4882a593Smuzhiyun };
89*4882a593Smuzhiyun 
90*4882a593Smuzhiyun /* The reason for all this evilness is that ALSA calls some of a drivers
91*4882a593Smuzhiyun  * operators in atomic context, and some not.  But all our functions channel
92*4882a593Smuzhiyun  * through the HPI_Message conduit, so we can't handle the different context
93*4882a593Smuzhiyun  * per function
94*4882a593Smuzhiyun  */
95*4882a593Smuzhiyun #define IN_LOCK_BH 1
96*4882a593Smuzhiyun #define IN_LOCK_IRQ 0
cond_lock(struct hpios_spinlock * l)97*4882a593Smuzhiyun static inline void cond_lock(struct hpios_spinlock *l)
98*4882a593Smuzhiyun {
99*4882a593Smuzhiyun 	if (irqs_disabled()) {
100*4882a593Smuzhiyun 		/* NO bh or isr can execute on this processor,
101*4882a593Smuzhiyun 		   so ordinary lock will do
102*4882a593Smuzhiyun 		 */
103*4882a593Smuzhiyun 		spin_lock(&((l)->lock));
104*4882a593Smuzhiyun 		l->lock_context = IN_LOCK_IRQ;
105*4882a593Smuzhiyun 	} else {
106*4882a593Smuzhiyun 		spin_lock_bh(&((l)->lock));
107*4882a593Smuzhiyun 		l->lock_context = IN_LOCK_BH;
108*4882a593Smuzhiyun 	}
109*4882a593Smuzhiyun }
110*4882a593Smuzhiyun 
cond_unlock(struct hpios_spinlock * l)111*4882a593Smuzhiyun static inline void cond_unlock(struct hpios_spinlock *l)
112*4882a593Smuzhiyun {
113*4882a593Smuzhiyun 	if (l->lock_context == IN_LOCK_BH)
114*4882a593Smuzhiyun 		spin_unlock_bh(&((l)->lock));
115*4882a593Smuzhiyun 	else
116*4882a593Smuzhiyun 		spin_unlock(&((l)->lock));
117*4882a593Smuzhiyun }
118*4882a593Smuzhiyun 
119*4882a593Smuzhiyun #define hpios_msgxlock_init(obj)      spin_lock_init(&(obj)->lock)
120*4882a593Smuzhiyun #define hpios_msgxlock_lock(obj)   cond_lock(obj)
121*4882a593Smuzhiyun #define hpios_msgxlock_unlock(obj) cond_unlock(obj)
122*4882a593Smuzhiyun 
123*4882a593Smuzhiyun #define hpios_dsplock_init(obj)       spin_lock_init(&(obj)->dsp_lock.lock)
124*4882a593Smuzhiyun #define hpios_dsplock_lock(obj)    cond_lock(&(obj)->dsp_lock)
125*4882a593Smuzhiyun #define hpios_dsplock_unlock(obj)  cond_unlock(&(obj)->dsp_lock)
126*4882a593Smuzhiyun 
127*4882a593Smuzhiyun #ifdef CONFIG_SND_DEBUG
128*4882a593Smuzhiyun #define HPI_BUILD_DEBUG
129*4882a593Smuzhiyun #endif
130*4882a593Smuzhiyun 
131*4882a593Smuzhiyun #define HPI_ALIST_LOCKING
132*4882a593Smuzhiyun #define hpios_alistlock_init(obj)    spin_lock_init(&((obj)->list_lock.lock))
133*4882a593Smuzhiyun #define hpios_alistlock_lock(obj) spin_lock(&((obj)->list_lock.lock))
134*4882a593Smuzhiyun #define hpios_alistlock_unlock(obj) spin_unlock(&((obj)->list_lock.lock))
135*4882a593Smuzhiyun 
136*4882a593Smuzhiyun struct snd_card;
137*4882a593Smuzhiyun 
138*4882a593Smuzhiyun /** pci drvdata points to an instance of this struct */
139*4882a593Smuzhiyun struct hpi_adapter {
140*4882a593Smuzhiyun 	struct hpi_adapter_obj *adapter;
141*4882a593Smuzhiyun 	struct snd_card *snd_card;
142*4882a593Smuzhiyun 
143*4882a593Smuzhiyun 	int irq;
144*4882a593Smuzhiyun 	int interrupt_mode;
145*4882a593Smuzhiyun 	void (*interrupt_callback) (struct hpi_adapter *);
146*4882a593Smuzhiyun 
147*4882a593Smuzhiyun 	/* mutex prevents contention for one card
148*4882a593Smuzhiyun 	   between multiple user programs (via ioctl) */
149*4882a593Smuzhiyun 	struct mutex mutex;
150*4882a593Smuzhiyun 	char *p_buffer;
151*4882a593Smuzhiyun 	size_t buffer_size;
152*4882a593Smuzhiyun };
153*4882a593Smuzhiyun 
154*4882a593Smuzhiyun #endif
155