xref: /haiku/headers/os/drivers/pcmcia/k_compat.h (revision 1e36cfc2721ef13a187c6f7354dc9cbc485e89d3)
1 /*
2  * BeOS kernel compatibility layer
3  *
4  * The contents of this file are subject to the Mozilla Public License
5  * Version 1.0 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License
7  * at http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific language governing rights and
12  * limitations under the License.
13  *
14  * The initial developer of the original code is David A. Hinds
15  * <dhinds@hyper.stanford.edu>.  Portions created by David A. Hinds
16  *  are Copyright (C) 1998 David A. Hinds.  All Rights Reserved.
17  */
18 
19 #ifndef _BE_K_COMPAT_H
20 #define _BE_K_COMPAT_H
21 
22 #include <KernelExport.h>
23 #include <ISA.h>
24 #include <PCI.h>
25 #include <Drivers.h>
26 #include <ByteOrder.h>
27 #include <SupportDefs.h>
28 #include <config_manager.h>
29 #include <stdio.h>
30 #include <string.h>
31 
32 #define u32 uint32
33 #define u16 uint16
34 #define u8 uint8
35 
36 #define __KERNEL__
37 #define __init
38 #define __exit
39 
40 /* IO port access */
41 #define inb(p)			(isa->read_io_8)(p)
42 #define inw(p)			(isa->read_io_16)(p)
43 #define inl(p)			(isa->read_io_32)(p)
44 #define outb(d,p)		(isa->write_io_8)(p,d)
45 #define outw(d,p)		(isa->write_io_16)(p,d)
46 #define outl(d,p)		(isa->write_io_32)(p,d)
47 
48 /* Memory-mapped IO access: unlike Linux, BeOS allows dereferencing
49    pointers to mapped devices */
50 #define readb(p)		(*(volatile u_char *)(p))
51 #define readw(p)		(*(volatile u_short *)(p))
52 #define readl(p)		(*(volatile u_int *)(p))
53 #define writeb(b, p)		(*(volatile u_char *)(p) = (b))
54 #define writew(w, p)		(*(volatile u_short *)(p) = (w))
55 #define writel(l, p)		(*(volatile u_int *)(p) = (l))
56 #define memcpy_fromio(a, b, c)	memcpy((a), (void *)(b), (c))
57 #define memcpy_toio(a, b, c)	memcpy((void *)(a), (b), (c))
58 
59 /* Byte swapping */
60 #define le16_to_cpu		B_LENDIAN_TO_HOST_INT16
61 #define le32_to_cpu		B_LENDIAN_TO_HOST_INT32
62 #define cpu_to_le16		B_HOST_TO_LENDIAN_INT16
63 #define cpu_to_le32		B_HOST_TO_LENDIAN_INT32
64 #define writew_ns		writew
65 #define readw_ns		readw
66 
67 /* Copying data between kernel and user space: BeOS can directly
68    dereference user pointers in kernel mode */
69 #define get_user(x, p)		((x) = *(p))
70 #define put_user(x, p)		(*(p) = (x))
71 #define copy_from_user		memcpy
72 #define copy_to_user		memcpy
73 
74 /* Virtual memory mapping: this is somewhat inelegant, but lets us
75    use drop-in replacements for the Linux equivalents */
76 // #define PAGE_SIZE		(0x1000)
77 static inline void *ioremap(u_long base, u_long size)
78 {
79     char tag[B_OS_NAME_LENGTH];
80     area_id id;
81     void *virt;
82     sprintf(tag, "pccard %08lx", base);
83     id = map_physical_memory(tag, (void *)base,
84 			     size, B_ANY_KERNEL_ADDRESS,
85 			     B_READ_AREA | B_WRITE_AREA, &virt);
86     return (id < 0) ? NULL : virt;
87 }
88 static inline void iounmap(void *virt)
89 {
90     area_id id = area_for(virt);
91     if (id >= 0) delete_area(id);
92 }
93 
94 /* Resource management: use helper functions from the PCMCIA resource
95    manager module.  RSRC_MGR needs to be defined appropriately if the
96    calls are via a module_info structure. */
97 #define request_region(base, num, name) \
98 	(RSRC_MGR register_resource(B_IO_PORT_RESOURCE, (base), (num)))
99 #define vacate_region release_region
100 #define vacate_mem_region release_mem_region
101 #define release_region(base, num) \
102 	(RSRC_MGR release_resource(B_IO_PORT_RESOURCE, (base), (num)))
103 #define check_region(base, num) \
104 	(RSRC_MGR check_resource(B_IO_PORT_RESOURCE, (base), (num)))
105 #define request_mem_region(base, num, name) \
106 	(RSRC_MGR register_resource(B_MEMORY_RESOURCE, (base), (num)))
107 #define release_mem_region(base, num) \
108 	(RSRC_MGR release_resource(B_MEMORY_RESOURCE, (base), (num)))
109 #define check_mem_region(base, num) \
110 	(RSRC_MGR check_resource(B_MEMORY_RESOURCE, (base), (num)))
111 #define register_irq(irq) \
112 	(RSRC_MGR register_resource(B_IRQ_RESOURCE, (irq), 0))
113 #define release_irq(irq) \
114 	(RSRC_MGR release_resource(B_IRQ_RESOURCE, (irq), 0))
115 #define check_irq(irq) \
116 	(RSRC_MGR check_resource(B_IRQ_RESOURCE, (irq), 0))
117 #define ACQUIRE_RESOURCE_LOCK \
118     do { module_info *m; \
119     get_module(B_CONFIG_MANAGER_FOR_DRIVER_MODULE_NAME, &m); } while (0)
120 #define RELEASE_RESOURCE_LOCK \
121     put_module(B_CONFIG_MANAGER_FOR_DRIVER_MODULE_NAME)
122 
123 /* Memory allocation.  BeOS doesn't have an atomic malloc. */
124 #define kmalloc(s,f)		malloc(s)
125 #define kfree(p)		free(p)
126 #define kfree_s(p,s)		free(p)
127 void *malloc();
128 void free(void *);
129 
130 /* PCI configuration register access */
131 #define pcibios_present() (1)
132 #define pcibios_read_config_byte(b,df,o,v) \
133 	((*(v) = pci->read_pci_config(b,(df)>>3,((df)&7),o,1)),0)
134 #define pcibios_read_config_word(b,df,o,v) \
135 	((*(v) = pci->read_pci_config(b,(df)>>3,((df)&7),o,2)),0)
136 #define pcibios_read_config_dword(b,df,o,v) \
137 	((*(v) = pci->read_pci_config(b,(df)>>3,((df)&7),o,4)),0)
138 #define pcibios_write_config_byte(b,df,o,v) \
139 	(pci->write_pci_config(b,(df)>>3,((df)&7),o,1,v),0)
140 #define pcibios_write_config_word(b,df,o,v) \
141 	(pci->write_pci_config(b,(df)>>3,((df)&7),o,2,v),0)
142 #define pcibios_write_config_dword(b,df,o,v) \
143 	(pci->write_pci_config(b,(df)>>3,((df)&7),o,4,v),0)
144 #define PCI_VENDOR_ID		PCI_vendor_id
145 #define PCI_DEVICE_ID		PCI_device_id
146 #define PCI_COMMAND		PCI_command
147 #define  PCI_COMMAND_IO		PCI_command_io
148 #define  PCI_COMMAND_MEMORY	PCI_command_memory
149 #define  PCI_COMMAND_MASTER	PCI_command_master
150 #define  PCI_COMMAND_WAIT	PCI_command_address_step
151 #define PCI_STATUS		PCI_status
152 #define PCI_CLASS_REVISION  PCI_revision
153 #define PCI_CACHE_LINE_SIZE	PCI_line_size
154 #define PCI_LATENCY_TIMER	PCI_latency
155 #define PCI_INTERRUPT_LINE	PCI_interrupt_line
156 #define PCI_INTERRUPT_PIN	PCI_interrupt_pin
157 #define PCI_HEADER_TYPE		PCI_header_type
158 #define PCI_BASE_ADDRESS_0	PCI_base_registers
159 #define  PCI_BASE_ADDRESS_SPACE	PCI_address_space
160 #define  PCI_BASE_ADDRESS_MEM_MASK	PCI_address_memory_32_mask
161 #define  PCI_BASE_ADDRESS_IO_MASK	PCI_address_io_mask
162 #define  PCI_BASE_ADDRESS_MEM_PREFETCH	PCI_address_prefetchable
163 #define PCI_FUNC(devfn)		((devfn)&7)
164 #define PCI_SLOT(devfn)		((devfn)>>3)
165 #define PCI_DEVFN(dev,fn)	(((dev)<<3)|((fn)&7))
166 #define PCI_CLASS_BRIDGE_PCMCIA	0x0605
167 #define PCI_CLASS_BRIDGE_CARDBUS 0x0607
168 
169 /* Atomic test-and-set */
170 #define test_and_set_bit(b,p)	(((atomic_or(p,(1<<(b))))>>(b))&1)
171 
172 /* Spin locks */
173 #define __SMP__
174 #define spinlock_t		spinlock
175 #define USE_SPIN_LOCKS
176 #define SPIN_LOCK_UNLOCKED	0
177 #define spin_lock_irqsave(l,f) \
178     do { f = disable_interrupts(); acquire_spinlock(l); } while (0)
179 #define spin_unlock_irqrestore(l,f) \
180     do { release_spinlock(l); restore_interrupts(f); } while (0)
181 
182 /* Interrupt handling */
183 #define request_irq(i,h,f,n,d)	install_io_interrupt_handler(i,h,d,0)
184 #define free_irq(i,h)	remove_io_interrupt(i,h)
185 //#define REQUEST_IRQ(i,h,f,n,d)	install_io_interrupt_handler(i,h,d,0)
186 //#define FREE_IRQ(i,h,d)		remove_io_interrupt(i,h)
187 //#define IRQ(i,d,r)		(d)
188 #define IRQ
189 #define DEV_ID			dev_id
190 #define NR_IRQS			16
191 #define SA_SHIRQ		1
192 
193 #define init_waitqueue(w)	memset((w), 0, sizeof(*w))
194 #define init_waitqueue_head(w) memset((w), 0, sizeof(*w))
195 #define signal_pending(a)	has_signals_pending(NULL)
196 
197 /* Miscellaneous services */
198 typedef long long k_time_t;
199 #define schedule_timeout(x) snooze(x)
200 #define udelay(d)		spin(d)
201 #define mdelay(d) \
202     do { int i; for (i=0;i<d;i++) spin(1000); } while (0)
203 #define printk			dprintf
204 #define KERN_ERR		""
205 #define KERN_NOTICE		""
206 #define KERN_INFO		""
207 #define KERN_WARNING		""
208 #define KERN_DEBUG		""
209 
210 #ifndef ENODATA
211 #define ENODATA ENOSPC
212 #endif
213 
214 #include <pcmcia/cs_timer.h>
215 #define add_timer my_add_timer
216 #define del_timer my_del_timer
217 
218 /* Module handling stuff */
219 #define MODULE_AUTHOR(x)
220 #define MODULE_DESCRIPTION(x)
221 #define MODULE_LICENSE(x)
222 #define MODULE_PARM(a,b)	extern int __dummy_decl
223 #define MOD_INC_USE_COUNT
224 #define MOD_DEC_USE_COUNT
225 
226 #endif /* _BE_K_COMPAT_H */
227