1 #ifndef _LINUX_FUNC_H_
2 #define	_LINUX_FUNC_H_
3 
4 #include <sys/endian.h>
5 
6 int	zero_nop(void);
7 
8 #define	device_create_file(...) zero_nop()
9 #define	device_remove_file(...) zero_nop()
10 #define	device_create_bin_file(...) zero_nop()
11 #define	device_remove_bin_file(...) zero_nop()
12 #define	device_set_wakeup_enable(...) zero_nop()
13 #define	device_set_wakeup_capable(...) zero_nop()
14 #define	device_set_wakeup_disable(...) zero_nop()
15 #define	device_property_read_u32(a,b,c) ({ *(c) = 0; -EOPNOTSUPP; })
16 #define	device_property_read_bool(a,b) ({ false; })
17 
18 #define	driver_create_file(...) zero_nop()
19 #define	driver_remove_file(...) zero_nop()
20 #define	driver_attach(...) zero_nop()
21 
22 #define	platform_device_register_data(...) ERR_PTR(-EOPNOTSUPP)
23 #define	platform_device_unregister(...) __nop
24 #define	platform_get_device_id(...) NULL
25 #define	platform_get_drvdata(...) NULL
26 #define	platform_set_drvdata(...) __nop
27 #define	module_platform_driver(...)
28 
29 int	driver_register(struct device_driver *);
30 int	driver_unregister(struct device_driver *);
31 
32 int	printk_nop(void);
33 
34 #define	le16_to_cpu(x) le16toh(x)
35 #define	be16_to_cpu(x) be16toh(x)
36 
37 #define	cpu_to_le16(x) htole16(x)
38 #define	cpu_to_be16(x) htobe16(x)
39 
40 #define	le32_to_cpu(x) le32toh(x)
41 #define	be32_to_cpu(x) be32toh(x)
42 
43 #define	le64_to_cpu(x) le64toh(x)
44 #define	be64_to_cpu(x) be64toh(x)
45 
46 #define	cpu_to_le32(x) htole32(x)
47 #define	cpu_to_be32(x) htobe32(x)
48 
49 #define	cpu_to_le64(x) htole64(x)
50 #define	cpu_to_be64(x) htobe64(x)
51 
52 uint16_t le16_to_cpup(uint16_t *p);
53 uint16_t be16_to_cpup(uint16_t *p);
54 
55 uint16_t cpu_to_le16p(uint16_t *p);
56 uint16_t cpu_to_be16p(uint16_t *p);
57 
58 uint32_t le32_to_cpup(uint32_t *p);
59 uint32_t be32_to_cpup(uint32_t *p);
60 
61 uint32_t cpu_to_le32p(uint32_t *p);
62 uint32_t cpu_to_be32p(uint32_t *p);
63 
64 uint64_t le64_to_cpup(uint64_t *p);
65 uint64_t be64_to_cpup(uint64_t *p);
66 
67 uint64_t cpu_to_le64p(uint64_t *p);
68 uint64_t cpu_to_be64p(uint64_t *p);
69 
70 void	le16_to_cpus(uint16_t *p);
71 void	le32_to_cpus(uint32_t *p);
72 void	le64_to_cpus(uint64_t *p);
73 
74 void	put_unaligned_le64(uint64_t, void *);
75 void	put_unaligned_be64(uint64_t, void *);
76 void	put_unaligned_le32(uint32_t, void *);
77 void	put_unaligned_be32(uint32_t, void *);
78 void	put_unaligned_be16(uint16_t, void *);
79 void	put_unaligned_le16(uint16_t, void *);
80 
81 uint64_t get_unaligned_le64(const void *);
82 uint64_t get_unaligned_be64(const void *);
83 uint32_t get_unaligned_le32(const void *);
84 uint32_t get_unaligned_be32(const void *);
85 uint16_t get_unaligned_be16(const void *);
86 uint16_t get_unaligned_le16(const void *);
87 
88 void   *devm_kcalloc(struct device *dev, size_t n, size_t size, gfp_t gfp);
89 void   *devm_kzalloc(struct device *dev, size_t size, gfp_t gfp);
90 void   *devm_kmalloc(struct device *dev, size_t size, gfp_t gfp);
91 void   *devm_kmalloc_array(struct device *, size_t, size_t, gfp_t);
92 void   *devm_kmemdup(struct device *, const void *, size_t, gfp_t);
93 void	devm_kfree(struct device *dev, void *ptr);
94 int	devm_add_action(struct device *, void (*)(void *), void *);
95 int	devm_add_action_or_reset(struct device *, void (*action)(void *), void *);
96 #define	devm_request_threaded_irq(...) -EOPNOTSUPP
97 struct clk *devm_clk_get(struct device *, const char *);
98 void   *dev_get_drvdata(const struct device *dev);
99 void	dev_set_drvdata(struct device *dev, void *data);
100 const char *dev_name(const struct device *dev);
101 int	vm_insert_page(struct vm_area_struct *vma, unsigned long start, struct page *page);
102 int	remap_pfn_range(struct vm_area_struct *, unsigned long addr, unsigned long pfn, unsigned long size, int);
103 void	vfree(void *);
104 unsigned long copy_to_user(void *to, const void *from, unsigned long n);
105 unsigned long copy_from_user(void *to, const void *from, unsigned long n);
106 unsigned long clear_user(void *to, unsigned long n);
107 void	schedule(void);
108 int	atomic_inc(atomic_t *);
109 int	atomic_dec(atomic_t *);
110 int	atomic_add(int i, atomic_t *);
111 int	atomic_add_unless(atomic_t *, int, int);
112 void	atomic_set(atomic_t *, int);
113 int	atomic_read(const atomic_t *);
114 int	atomic_dec_and_test(atomic_t *);
115 int	atomic_cmpxchg(atomic_t *, int, int);
116 
117 #define	xchg(ptr, new) ({			\
118 	union {					\
119 		__typeof(*(ptr)) val;		\
120 	} __ret, __new = { .val = (new) };	\
121 						\
122 	atomic_lock();				\
123 	__ret.val = READ_ONCE(*ptr);		\
124 	WRITE_ONCE(*ptr, __new.val);		\
125 	atomic_unlock();			\
126 	__ret.val;				\
127 })
128 
129 uint64_t atomic64_read(atomic64_t *);
130 void atomic64_or(uint64_t, atomic64_t *);
131 void atomic64_xor(uint64_t, atomic64_t *);
132 void atomic64_and(uint64_t, atomic64_t *);
133 void atomic64_andnot(uint64_t, atomic64_t *);
134 
135 /* Bit-operations */
136 int	test_bit(int nr, const void *addr);
137 
138 #define	__test_bit(a,b) test_bit(a,b)
139 int	test_and_set_bit(int nr, volatile unsigned long *addr);
140 
141 #define	__test_and_set_bit(a,b) test_and_set_bit(a,b)
142 int	test_and_clear_bit(int nr, volatile unsigned long *addr);
143 
144 #define	__test_and_clear_bit(a,b) test_and_clear_bit(a,b)
145 void	set_bit(int nr, volatile unsigned long *addr);
146 
147 #define	__set_bit(a,b) set_bit(a,b)
148 void	clear_bit(int nr, volatile unsigned long *addr);
149 
150 #define	__clear_bit(a,b) clear_bit(a,b)
151 void	change_bit(int nr, volatile unsigned long *addr);
152 
153 #define	__change_bit(a,b) change_bit(a,b)
154 
155 struct cdev *cdev_alloc(void);
156 void	cdev_del(struct cdev *);
157 int	cdev_add(struct cdev *cdev, dev_t mm, unsigned count);
158 void	cdev_set_parent(struct cdev *, struct kobject *);
159 int	cdev_device_add(struct cdev *, struct device *);
160 void	cdev_device_del(struct cdev *, struct device *);
161 
162 int	register_chrdev(dev_t mm, const char *desc, const struct file_operations *fops);
163 int	unregister_chrdev(dev_t mm, const char *desc);
164 
165 uint8_t	bitrev8(uint8_t a);
166 uint16_t bitrev16(uint16_t a);
167 size_t	memweight(const void *, size_t);
168 unsigned int hweight8(unsigned int w);
169 unsigned int hweight16(unsigned int w);
170 unsigned int hweight32(unsigned int w);
171 unsigned long hweight64(uint64_t w);
172 unsigned long copy_to_user(void *to, const void *from, unsigned long n);
173 unsigned long copy_from_user(void *to, const void *from, unsigned long n);
174 void	kref_get(struct kref *kref);
175 int	kref_get_unless_zero(struct kref *kref);
176 int	kref_put(struct kref *kref, void (*release) (struct kref *kref));
177 void	kref_init(struct kref *kref);
178 struct device *get_device(struct device *dev);
179 void	put_device(struct device *dev);
180 int	device_move(struct device *dev, struct device *new_parent, int how);
181 int	bus_register(struct bus_type *);
182 int	bus_unregister(struct bus_type *);
183 int	device_add(struct device *dev);
184 void	device_del(struct device *dev);
185 int	device_register(struct device *dev);
186 void	device_unregister(struct device *dev);
187 struct device *device_create_vargs(struct class *class, struct device *parent, dev_t devt, void *drvdata, const char *fmt, va_list args);
188 struct device *device_create(struct class *class, struct device *parent, dev_t devt, void *drvdata, const char *fmt,...);
189 int	device_enable_async_suspend(struct device *);
190 void	device_destroy(struct class *class, dev_t devt);
191 void	module_put(struct module *module);
192 int	try_module_get(struct module *module);
193 
194 #define	__module_get module_get
195 void	module_get(struct module *module);
196 void   *ERR_PTR(long);
197 long	PTR_ERR(const void *);
198 long	IS_ERR(const void *);
199 int	PTR_ERR_OR_ZERO(const void *);
200 
201 #define	TK_OFFS_REAL 0
202 #define	TK_OFFS_BOOT 1
203 
204 int	__ffs(int x);
205 
206 #define	ffz(x) __ffz(x)
207 int	__ffz(int x);
208 
209 #define	__fls(x) fls(x)
210 int	fls (int mask);
211 unsigned long find_next_bit(const unsigned long *addr, unsigned long size, unsigned long offset);
212 unsigned long find_next_zero_bit(const unsigned long *addr, unsigned long size, unsigned long offset);
213 void	sort(void *base, size_t num, size_t size, int (*cmp) (const void *, const void *), void (*swap) (void *, void *, int size));
214 u32	crc32_le(u32 crc, unsigned char const *p, size_t len);
215 u32	crc32_be(u32 crc, unsigned char const *p, size_t len);
216 void   *vmalloc(size_t size);
217 void   *vzalloc(size_t size);
218 void   *kcalloc(size_t n, size_t size, int flags);
219 
220 #define	kmalloc_array(...) kcalloc(__VA_ARGS__)
221 long	__get_free_page(int);
222 void	free_page(long);
223 struct class *class_get(struct class *class);
224 struct class *class_put(struct class *class);
225 int	class_register(struct class *class);
226 void	class_unregister(struct class *class);
227 void	class_destroy(struct class *class);
228 int	alloc_chrdev_region(dev_t *, unsigned, unsigned, const char *);
229 int	register_chrdev_region(dev_t from, unsigned count, const char *name);
230 void	unregister_chrdev_region(dev_t from, unsigned count);
231 int	remap_vmalloc_range(struct vm_area_struct *vma, void *addr, unsigned long pgoff);
232 void	jiffies_to_timeval(uint64_t j, struct timeval *tv);
233 uint64_t round_jiffies_relative(uint64_t j);
234 uint64_t sched_clock(void);
235 int	do_gettimeofday(struct timeval *tp);
236 void	poll_initwait(struct poll_wqueues *pwq);
237 void	poll_freewait(struct poll_wqueues *pwq);
238 void	poll_schedule(struct poll_wqueues *pwq, int flag);
239 
240 #define	poll_requested_events(...) 0xFFFFFFFFU
241 
242 unsigned long *bitmap_alloc(unsigned int, gfp_t);
243 unsigned long *bitmap_zalloc(unsigned int, gfp_t);
244 void	bitmap_free(unsigned long *);
245 void	bitmap_copy(unsigned long *, const unsigned long *, unsigned int);
246 int	bitmap_weight(const unsigned long *, unsigned int);
247 int	bitmap_andnot(unsigned long *, const unsigned long *, const unsigned long *, int);
248 int	bitmap_and(unsigned long *, const unsigned long *, const unsigned long *, int);
249 void	bitmap_or(unsigned long *, const unsigned long *, const unsigned long *, int);
250 void	bitmap_xor(unsigned long *, const unsigned long *, const unsigned long *, int);
251 void	bitmap_zero(unsigned long *, unsigned int);
252 void	bitmap_fill(unsigned long *, unsigned int);
253 int	bitmap_subset(const unsigned long *, const unsigned long *, int);
254 int	bitmap_full(const unsigned long *, int);
255 void	bitmap_clear(unsigned long *, int, int);
256 void	bitmap_shift_right(unsigned long *, const unsigned long *, int, int);
257 void	bitmap_shift_left(unsigned long *, const unsigned long *, int, int);
258 int	bitmap_equal(const unsigned long *, const unsigned long *, unsigned);
259 int	bitmap_empty(const unsigned long *, unsigned);
260 int	bitmap_intersects(const unsigned long *, const unsigned long *, unsigned);
261 int32_t	div_round_closest_s32(int32_t rem, int32_t div);
262 uint32_t div_round_closest_u32(uint32_t rem, uint32_t div);
263 int64_t	div_round_closest_s64(int64_t rem, int64_t div);
264 uint64_t div_round_closest_u64(uint64_t rem, uint64_t div);
265 #define	ktime_to_timespec64(ktime) ktime
266 struct timespec ktime_mono_to_real(struct timespec);
267 struct timespec ktime_get_boottime(void);
268 struct timespec ktime_get_real(void);
269 struct timespec ktime_get(void);
270 int64_t ktime_get_ns(void);
271 struct timespec ktime_mono_to_any(struct timespec, int);
272 struct timeval ktime_to_timeval(const struct timespec);
273 void	ktime_get_ts(struct timespec *);
274 void	ktime_get_real_ts(struct timespec *);
275 int64_t	ktime_to_ns(const struct timespec ts);
276 struct timespec ktime_sub(const struct timespec a, const struct timespec b);
277 struct timespec ktime_add(const struct timespec a, const struct timespec b);
278 struct timespec ktime_get_monotonic_offset(void);
279 struct timespec ktime_add_us(const struct timespec, const uint64_t);
280 int64_t	ktime_us_delta(const struct timespec, const struct timespec);
281 int64_t	ktime_to_us(const struct timespec);
282 int64_t	ktime_to_ms(const struct timespec);
283 struct timespec ktime_set(const s64, const unsigned long);
284 int64_t ktime_ms_delta(const ktime_t, const ktime_t);
285 int	ktime_compare(const ktime_t, const ktime_t);
286 u64	timeval_to_ns(const struct timeval *);
287 struct timeval	ns_to_timeval(u64);
288 void	msleep(uint32_t ms);
289 void	ssleep(uint32_t s);
290 uint32_t msleep_interruptible(uint32_t ms);
291 int	request_module(const char *fmt,...);
292 int	request_module_nowait(const char *fmt,...);
293 int	device_can_wakeup(struct device *dev);
294 void	device_init_wakeup(struct device *dev, int flags);
295 void	device_initialize(struct device *dev);
296 int	dmi_check_system(const struct dmi_system_id *list);
297 void	swab16s(uint16_t *ptr);
298 uint16_t swab16(uint16_t temp);
299 void	swab32s(uint32_t *ptr);
300 uint32_t swab32(uint32_t temp);
301 int	scnprintf(char *buf, size_t size, const char *fmt,...);
302 #define	vscnprintf(...) \
303 	scnprintf(__VA_ARGS__)
304 char   *devm_kasprintf(struct device *, gfp_t, const char *,...);
305 #define	seq_printf(...) __nop
306 struct timespec current_kernel_time(void);
307 int64_t	timespec_to_ns(const struct timespec *);
308 struct timespec ns_to_timespec(const int64_t);
309 #define	ns_to_timespec64(x) ns_to_timespec(x)
310 struct timespec timespec_add(struct timespec, struct timespec);
311 struct timespec timespec_sub(struct timespec, struct timespec);
312 uint32_t do_div(uint64_t *rem, uint32_t div);
313 uint64_t div64_u64(uint64_t, uint64_t);
314 int64_t	div64_s64(int64_t, int64_t);
315 uint64_t div_u64_rem(uint64_t, uint32_t, uint32_t *);
316 int64_t	div_s64_rem(int64_t, int32_t, int32_t *);
317 uint64_t div_u64(uint64_t, uint32_t);
318 int64_t	div_s64(int64_t, int32_t);
319 
320 #define	do_div(r,d) do_div(&(r),(d))
321 int	sysfs_create_group(struct kobject *, const struct attribute_group *);
322 void	sysfs_remove_group(struct kobject *, const struct attribute_group *);
323 int	sysfs_create_bin_file(struct kobject *, struct bin_attribute *);
324 int	sysfs_remove_bin_file(struct kobject *, struct bin_attribute *);
325 #define	sysfs_emit(...) 0
326 void   *pci_alloc_consistent(struct pci_dev *hwdev, size_t size, dma_addr_t *dma_addr);
327 void   *pci_zalloc_consistent(struct pci_dev *hwdev, size_t size, dma_addr_t *dma_addr);
328 void	pci_free_consistent(struct pci_dev *hwdev, size_t size, void *vaddr, dma_addr_t dma_handle);
329 int	add_uevent_var(struct kobj_uevent_env *env, const char *format,...);
330 struct class *class_create(struct module *owner, const char *name);
331 void	cdev_init(struct cdev *cdev, const struct file_operations *fops);
332 struct cdev *cdev_get_device(unsigned int f_v4b);
333 uint32_t cdev_get_mm(unsigned int f_v4b);
334 int	usb_register_dev(struct usb_interface *, struct usb_class_driver *);
335 void	usb_deregister_dev(struct usb_interface *, struct usb_class_driver *);
336 struct usb_interface *usb_find_interface(struct usb_driver *, int);
337 
338 int	is_vmalloc_addr(void *);
339 void   *malloc_vm(size_t);
340 void	free_vm(void *);
341 int	pidfile_create(int bus, int addr, int index);
342 
343 void   *kmemdup(const void *src, size_t len, gfp_t gfp);
344 void   *memdup_user(const void *src, size_t len);
345 
346 unsigned long rounddown_pow_of_two(unsigned long);
347 unsigned long roundup_pow_of_two(unsigned long);
348 const char *skip_spaces(const char *str);
349 
350 int	kobject_set_name(struct kobject *kobj, const char *fmt,...);
351 
352 int	nonseekable_open(struct inode *inode, struct file *filp);
353 int	stream_open(struct inode *inode, struct file *filp);
354 
355 int	kstrtos16(const char *, unsigned int, int16_t *);
356 int	kstrtou16(const char *, unsigned int, uint16_t *);
357 int	kstrtos8(const char *, unsigned int, int8_t *);
358 int	kstrtou8(const char *, unsigned int, uint8_t *);
359 int	kstrtoul(const char *, unsigned int, unsigned long *);
360 int	kstrtouint(const char *, unsigned int, unsigned int *);
361 int	kstrtoint(const char *, unsigned int, int *);
362 int	kstrtobool(const char *, bool *);
363 
strtobool(const char * str,bool * ret)364 static inline int strtobool(const char *str, bool *ret)
365 {
366         return (kstrtobool(str, ret));
367 }
368 
369 ssize_t	simple_write_to_buffer(void *, size_t, loff_t *, const void __user *, size_t);
370 ssize_t	simple_read_from_buffer(void __user *, size_t, loff_t *, const void *, size_t);
371 
372 uint64_t int_sqrt(uint64_t a);
373 
374 void   *devres_alloc(dr_release_t, size_t, gfp_t);
375 void	devres_free(void *);
376 void	devres_add(struct device *, void *);
377 int	devres_destroy(struct device *, dr_release_t, dr_match_t, void *);
378 void   *devres_open_group(struct device *, void *, gfp_t);
379 void	devres_close_group(struct device *, void *);
380 int	devres_release_group(struct device *, void *);
381 
382 int	dma_buf_fd(struct dma_buf *, int);
383 struct dma_buf *dma_buf_get(int);
384 void	dma_buf_put(struct dma_buf *);
385 
386 struct dma_buf_map {
387 	void *vaddr;
388 };
389 
390 #define	DMA_BUF_MAP_INIT_VADDR(x) { x }
391 
392 int	dma_buf_vmap(struct dma_buf *, struct dma_buf_map *);
393 void	dma_buf_vunmap(struct dma_buf *, struct dma_buf_map *);
394 
395 uint32_t ror32(uint32_t, uint8_t);
396 unsigned long gcd(unsigned long, unsigned long);
397 void	get_random_bytes(void *, int);
398 u32	prandom_u32_max(u32);
399 
400 const char *dev_driver_string(const struct device *);
401 
402 s32	sign_extend32(u32, int);
403 
404 void	eth_zero_addr(u8 *addr);
405 
406 struct device *kobj_to_dev(struct kobject *);
407 
408 void	*memscan(void *, int, size_t);
409 
410 int	refcount_read(refcount_t *);
411 bool	refcount_dec_and_test(refcount_t *);
412 void	refcount_set(refcount_t *, int);
413 void	refcount_inc(refcount_t *);
414 
415 size_t	array_size(size_t, size_t);
416 size_t	array3_size(size_t, size_t, size_t);
417 size_t	struct_size_sub(size_t, size_t, size_t);
418 ssize_t	strscpy(char *, const char *, size_t);
419 
420 #define	struct_size(p, member, n)				\
421 	struct_size_sub(n, sizeof(*(p)->member), sizeof(*(p)))
422 
423 ssize_t	memory_read_from_buffer(void *, size_t, loff_t *, const void *, size_t);
424 
425 #define	mmap_read_lock(...) __nop
426 #define	mmap_read_unlock(...) __nop
427 
428 #define	debugfs_create_file(...) __nop
429 #define	debugfs_create_dir(...) NULL
430 #define	debugfs_remove_recursive(...) __nop
431 
432 #define	dma_sync_sgtable_for_device(...) __nop
433 #define	dma_sync_sgtable_for_cpu(...) __nop
434 #define	invalidate_kernel_vmap_range(...) __nop
435 
436 void *dma_vmap_noncontiguous(struct device *, size_t, struct sg_table *);
437 #define	dma_vunmap_noncontiguous(...) __nop
438 struct sg_table *dma_alloc_noncontiguous(struct device *, size_t, enum dma_data_direction, gfp_t, unsigned long);
439 void dma_free_noncontiguous(struct device *, size_t, struct sg_table *, enum dma_data_direction);
440 
441 #endif					/* _LINUX_FUNC_H_ */
442