⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sysdep.h

📁 HMS30C7202下的CAN驱动
💻 H
📖 第 1 页 / 共 2 页
字号:
/*
 * sysdep.h -- centralizing compatibility issues between 2.0, 2.2, 2.4
 *
 * Copyright (C) 2001 Alessandro Rubini and Jonathan Corbet
 * Copyright (C) 2001 O'Reilly & Associates
 *
 * The source code in this file can be freely used, adapted,
 * and redistributed in source or binary form, so long as an
 * acknowledgment appears in derived source files.  The citation
 * should list that the code comes from the book "Linux Device
 * Drivers" by Alessandro Rubini and Jonathan Corbet, published
 * by O'Reilly & Associates.   No warranty is attached;
 * we cannot take responsibility for errors or fitness for use.
 *
 */

#ifndef _SYSDEP_H_
# define _SYSDEP_H_

# ifndef LINUX_VERSION_CODE
#  include <linux/version.h>
# endif

# ifndef KERNEL_VERSION /* pre-2.1.90 didn't have it */
#  define KERNEL_VERSION(vers,rel,seq) ( ((vers)<<16) | ((rel)<<8) | (seq) )
# endif

/* only allow 2.0.x  2.2.y and 2.4.z */

# if LINUX_VERSION_CODE < KERNEL_VERSION(2,0,0) /* not < 2.0 */
#  error "This kernel is too old: not supported by this file"
# endif
# if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0) /* not > 2.4, by now */
#  error "This kernel is too recent: not supported by this file"
# endif
# if (LINUX_VERSION_CODE & 0xff00) == 1 /* not 2.1 */
#  error "Please don't use linux-2.1, use 2.2 or 2.4 instead"
# endif
# if (LINUX_VERSION_CODE & 0xff00) == 3 /* not 2.3 */
#  error "Please don't use linux-2.3, use 2.4 instead"
# endif

/* remember about the current version */
# if LINUX_VERSION_CODE < KERNEL_VERSION(2,1,0)
#  define LINUX_20
# elif LINUX_VERSION_CODE < KERNEL_VERSION(2,3,0)
#  define LINUX_22
# else
#  define LINUX_24
# endif

/* we can't support versioning in pre-2.4 because we #define some functions */
# if !defined(LINUX_24) && defined(CONFIG_MODVERSIONS)
#  error "This sysdep.h can't support CONFIG_MODVERSIONS"
#  error "and old kernels at the same time."
#  error "Either use 2.4 or avoid using versioning"
# endif

# ifndef LINUX_20 /* include vmalloc.h if this is 2.2/2.4 */
#  ifdef VM_READ /* a typical flag defined by mm.h */
#   include <linux/vmalloc.h>
#  endif
# endif

# include <linux/slab.h>

# include <linux/sched.h>

/* Modularization issues */
# ifdef LINUX_20
#  define __USE_OLD_SYMTAB__
#  define EXPORT_NO_SYMBOLS register_symtab(NULL);
#  define REGISTER_SYMTAB(tab) register_symtab(tab)
# else
#  define REGISTER_SYMTAB(tab) /* nothing */
# endif

# ifdef __USE_OLD_SYMTAB__
#  define __MODULE_STRING(s)         /* nothing */
#  define MODULE_PARM(v,t)           /* nothing */
#  define MODULE_PARM_DESC(v,t)      /* nothing */
#  define MODULE_AUTHOR(n)           /* nothing */
#  define MODULE_DESCRIPTION(d)      /* nothing */
#  define MODULE_SUPPORTED_DEVICE(n) /* nothing */
# endif

/*
 * In version 2.2 (up to 2.2.19, at least), the macro for request_module()
 * when no kmod is there is wrong. It's a "do {} while 0" but it shouldbe int
 */
# ifdef LINUX_22
#  ifndef CONFIG_KMOD
#   undef request_module
#   define request_module(name) -ENOSYS
#  endif
# endif

# ifndef LINUX_20
#  include <linux/init.h>     /* module_init/module_exit */
# endif

# ifndef module_init
#  define module_init(x)        int init_module(void) { return x(); }
#  define module_exit(x)        void cleanup_module(void) { x(); }
# endif

# ifndef SET_MODULE_OWNER
#  define SET_MODULE_OWNER(structure) /* nothing */
# endif

/*
 * "select" changed in 2.1.23. The implementation is twin, but this
 * header is new
 *
 */
# ifdef LINUX_20
#  define __USE_OLD_SELECT__
# else
#  include <linux/poll.h>
# endif

# ifdef LINUX_20
#  define INODE_FROM_F(filp) ((filp)->f_inode)
# else
#  define INODE_FROM_F(filp) ((filp)->f_dentry->d_inode)
# endif

/* Other changes in the fops are solved using wrappers */

/*
 * Wait queues changed with 2.3
 */
# ifndef DECLARE_WAIT_QUEUE_HEAD
#  define DECLARE_WAIT_QUEUE_HEAD(head) struct wait_queue *head = NULL
typedef  struct wait_queue *wait_queue_head_t;
#  define init_waitqueue_head(head) (*(head)) = NULL

/* offer wake_up_sync as an alias for wake_up */
#  define wake_up_sync(head) wake_up(head)
#  define wake_up_interruptible_sync(head) wake_up_interruptible(head)

/* Pretend we have add_wait_queue_exclusive */
#  define add_wait_queue_exclusive(q,entry) add_wait_queue ((q), (entry))

# endif /* no DECLARE_WAIT_QUEUE_HEAD */

/*
 * Define wait_event for 2.0 kernels.  (This ripped off directly from
 * the 2.2.18 sched.h)
 */
# ifdef LINUX_20

#  define __wait_event(wq, condition) 					\
do {									\
	struct wait_queue __wait;					\
									\
	__wait.task = current;						\
	add_wait_queue(&wq, &__wait);					\
	for (;;) {							\
		current->state = TASK_UNINTERRUPTIBLE;			\
		mb();							\
		if (condition)						\
			break;						\
		schedule();						\
	}								\
	current->state = TASK_RUNNING;					\
	remove_wait_queue(&wq, &__wait);				\
} while (0)

#  define wait_event(wq, condition) 					\
do {									\
	if (condition)	 						\
		break;							\
	__wait_event(wq, condition);					\
} while (0)

#  define __wait_event_interruptible(wq, condition, ret)			\
do {									\
	struct wait_queue __wait;					\
									\
	__wait.task = current;						\
	add_wait_queue(&wq, &__wait);					\
	for (;;) {							\
		current->state = TASK_INTERRUPTIBLE;			\
		mb();							\
		if (condition)						\
			break;						\
		if (!signal_pending(current)) {				\
			schedule();					\
			continue;					\
		}							\
		ret = -ERESTARTSYS;					\
		break;							\
	}								\
	current->state = TASK_RUNNING;					\
	remove_wait_queue(&wq, &__wait);				\
} while (0)

#  define wait_event_interruptible(wq, condition)				\
({									\
	int __ret = 0;							\
	if (!(condition))						\
		__wait_event_interruptible(wq, condition, __ret);	\
	__ret;								\
})
# endif

/*
 * 2.3 added tasklets
 */
# ifdef LINUX_24
#  define HAVE_TASKLETS
# endif

/* FIXME: implement the other versions of wake_up etc */

/*
 * access to user space: use the 2.2 functions,
 * and implement them as macros for 2.0
 */

# ifdef LINUX_20
#  include <asm/segment.h>
#  define access_ok(t,a,sz)           (verify_area((t),(void *) (a),(sz)) ? 0 : 1)
#  define verify_area_20              verify_area
#  define   copy_to_user(t,f,n)         (memcpy_tofs((t), (f), (n)), 0)
#  define copy_from_user(t,f,n)       (memcpy_fromfs((t), (f), (n)), 0)
#  define   __copy_to_user(t,f,n)       copy_to_user((t), (f), (n))
#  define __copy_from_user(t,f,n)     copy_from_user((t), (f), (n))

#  define PUT_USER(val,add)           (put_user((val),(add)), 0)
#  define __PUT_USER(val,add)         PUT_USER((val),(add))

#  define GET_USER(dest,add)          ((dest)=get_user((add)), 0)
#  define __GET_USER(dest,add)        GET_USER((dest),(add))
# else
#  include <asm/uaccess.h>
#  include <asm/io.h>
#  define verify_area_20(t,a,sz) (0) /* == success */
#  define   PUT_USER   put_user
#  define __PUT_USER __put_user
#  define   GET_USER   get_user
#  define __GET_USER __get_user
# endif

/*
 * Allocation issues
 */
# ifdef GFP_USER /* only if mm.h has been included */
#  ifdef LINUX_20
#   define __GFP_DMA GFP_DMA /* 2.0 didn't have the leading __ */
#  endif
#  ifndef LINUX_24
#   define __GFP_HIGHMEM  0  /* was not there */
#   define GFP_HIGHUSER   0   /* idem */
#  endif

#  ifdef LINUX_20
#   define __get_free_pages(a,b) __get_free_pages((a),(b),0)
#  endif
#  ifndef LINUX_24
#   define get_zeroed_page get_free_page
#  endif
# endif

/* ioremap */
# if defined(LINUX_20) && defined(_LINUX_MM_H)
#  define ioremap_nocache ioremap
#  ifndef __i386__
   /* This simple approach works for non-PC platforms. */
#   define ioremap vremap
#   define iounmap vfree
#  else /* the PC has <expletive> ISA; 2.2 and 2.4 remap it, 2.0 needs not */
extern inline void *ioremap(unsigned long phys_addr, unsigned long size)
{
   if (phys_addr >= 0xA0000 && phys_addr + size <= 0x100000)
     return (void *)phys_addr;
   return vremap(phys_addr, size);
}

extern inline void iounmap(void *addr)
{
   if ((unsigned long)addr >= 0xA0000
       && (unsigned long)addr < 0x100000)
     return;
   vfree(addr);
}
#  endif
# endif

/* Also, define check_mem_region etc */
# ifndef LINUX_24
#  define check_mem_region(a,b)     0 /* success */
#  define request_mem_region(a,b,c) /* nothing */
#  define release_mem_region(a,b)   /* nothing */
# endif

/* implement capable() for 2.0 */
# ifdef LINUX_20
#  define capable(anything)  suser()
# endif

/* The use_count of exec_domain and binfmt changed in 2.1.23 */

# ifdef LINUX_20
#  define INCRCOUNT(p)  ((p)->module ? __MOD_INC_USE_COUNT((p)->module) : 0)
#  define DECRCOUNT(p)  ((p)->module ? __MOD_DEC_USE_COUNT((p)->module) : 0)
#  define CURRCOUNT(p)  ((p)->module && (p)->module->usecount)
# else
#  define INCRCOUNT(p)  ((p)->use_count++)
#  define DECRCOUNT(p)  ((p)->use_count--)
#  define CURRCOUNT(p)  ((p)->use_count)
# endif

/*
 * /proc has changed a lot across the versions...
 */
# ifdef LINUX_20
#  define USE_PROC_REGISTER
# endif

/*
 * 2.2 didn't have create_proc_{read|info}_entry yet.
 * And it looks like there are no other "interesting" entry point, as
 * the rest is somehow esotique (mknod, symlink, ...)
 */
# ifdef LINUX_22
#  ifdef PROC_SUPER_MAGIC  /* Only if procfs is being used */
extern inline struct proc_dir_entry *create_proc_read_entry(const char *name,
							    mode_t mode, struct proc_dir_entry *base,
							    read_proc_t *read_proc, void * data)
{
   struct proc_dir_entry *res=create_proc_entry(name,mode,base);
   if (res)
     {
        res->read_proc=read_proc;
        res->data=data;
     }
   return res;
}

#   ifndef create_proc_info_entry /* added in 2.2.18 */
typedef int (get_info_t)(char *, char **, off_t, int, int);
extern inline struct proc_dir_entry *create_proc_info_entry(const char *name,
							    mode_t mode, struct proc_dir_entry *base, get_info_t *get_info)
{
   struct proc_dir_entry *res=create_proc_entry(name,mode,base);
   if (res) res->get_info=get_info;
   return res;
}
#   endif  /* no create_proc_info_entry */
#  endif
# endif

# ifdef LINUX_20
#  define test_and_set_bit(nr,addr)  test_bit((nr),(addr))
#  define test_and_clear_bit(nr,addr) clear_bit((nr),(addr))
#  define test_and_change_bit(nr,addr) change_bit((nr),(addr))
# endif

/* 2.0 had no read and write memory barriers, and 2.2 lacks the
   set_ functions */
# ifndef LINUX_24
#  ifdef LINUX_20
#   define wmb() mb() /* this is a big penalty on non-reordering platfs */
#   define rmb() mb() /* this is a big penalty on non-reordering platfs */
#  endif /* LINUX_20 */

#  define set_mb() do { var = value; mb(); } while (0)
#  define set_wmb() do { var = value; wmb(); } while (0)
# endif /* ! LINUX_24 */

/* 2.1.30 removed these functions. Let's define them, just in case */
# ifndef LINUX_20
#  define queue_task_irq      queue_task
#  define queue_task_irq_off  queue_task
# endif

/* 2.1.10 and 2.1.43 introduced new functions. They are worth using */

# ifdef LINUX_20

#  include <asm/byteorder.h>
#  ifdef __LITTLE_ENDIAN
#   define cpu_to_le16(x) (x)
#   define cpu_to_le32(x) (x)
#   define cpu_to_be16(x) htons((x))
#   define cpu_to_be32(x) htonl((x))
#  else
#   define cpu_to_be16(x) (x)
#   define cpu_to_be32(x) (x)
extern inline __u16 cpu_to_le16(__u16 x) { return (x<<8) | (x>>8);}
extern inline __u32 cpu_to_le32(__u32 x)
{
   return (x>>24) |
     ((x>>8)&0xff00) | ((x<<8)&0xff0000) | (x<<24);
}
#  endif

#  define le16_to_cpu(x)  cpu_to_le16(x)
#  define le32_to_cpu(x)  cpu_to_le32(x)
#  define be16_to_cpu(x)  cpu_to_be16(x)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -