内核驱动相关
一.关键头文件及内含的结构体,函数:
-
linux/types.h:
> 1.dev_t;
2.int MAJOR(dev_t dev);
3.int MINOR(dev_t dev);
4.dev MKDEV(unsigned int major,unsigned int minor); -
linux/fs.h:
> 1.int register_chrdev_region(dev_t first,unsigned int count,char* name);
2.int alloc_chrdev_region(dev_t* dev,unsigned int firstminor,unsigned int count,char* name);
3.void unregister_chrdev_region(dev_t first,unsigned int count);
4.struct file;
5.struct file_operations;
6.struct inode; -
linux/cdev.h:
> 1.struct cdev cdev_alloc(void);
2.void cdev_init(struct cdev* dev,struct file_operations* fops);
3.int cdev_add(struct cdev* dev,dev_t num,unsigned int count);
4.void cdev_del(struct cdev* dev); -
linux/kernel.h:
> 1.container_of(pointer,type,field); - asm/uaccess.h:
> 1. unsigned long copy_from_user(void* to,void* from,unsigned long count);
- unsigned long copy_to_user(void* to,void* from,unsigned long count);
- asm/semaphore.h:
> 1. DECLARE_MUTEX(name);
- DECLARE_MUTEX_LOCKED(name);
- init_MUTEX(struct semaphore* sem);
- init_MUTEX_LOCKED(struct semaphore* sem);
- void down(struct semaphore* sem);
- int down_interruptible(struct semaphore* sem);
- int down_trylock(struct semaphore* sem);
- void up(struct semaphore* sem);
- struct rw_semaphore;
- init_rwsemaphore(struct rw_semaphore* sem);
- void down_read(struct rw_semaphore* sem);
- int down_read_trylock(struct rw_semaphore* sem);
- void up_read(struct rw_semaphore* sem);
- void down_write(struct rw_semaphore* sem);
- int down_write_trylock(struct rw_semaphore* sem);
- void up_write(struct rw_semaphore* sem);
- void downgrade_write(struct rw_semaphore* sem);
- DECLARE_MUTEX_LOCKED(name);
- linux/complition.h:
> 1. DECLARE_COMPLITION(name);
- init_complition(struct complition* c);
- INIT_COMPLITION(struct complition* c);
- void wait_for_complition(struct complition* c);
- void complition(struct complition* c);
- void complition_all(struct complition* c);
- void complite_and_exit(struct complition*c,long retval);
- init_complition(struct complition* c);
- linux/spinlock.h:
> 1. spinlock_t lock=SPIN_LOCK_UNLOCKED;
- spinlock_init(spinlock_t* lock);
- void spin_lock(spinlock_t* lock);
- void spin_lock_irqsave(spinlock_t* lock,unsigned long flags);
- void spin_lock_irq(spinlock_t* lock);
- void spin_lock_bh(spinlock_t* lock);
- int spin_trylock(spinlock_t* lock);
- int spin_lock_trylock_bh(spinlock_t* lock);
- void spin_unlock(spinlock_t* lock);
- void spin_unlock_irqrestore(spinlock_t* lock,unsigned long flags);
- void spin_unlock_irq(spinlock_t* lock);
- void spin_unlock_bh(spinlock_t* lock);
- rwlock_t lock=RW_LOCK_UNLOCKED;
- rwlock_init(rwlock_t* lock);
- void read_lock(rwlock_t* lock);
- void read_lock_irqsave(rwlock_t* lock,unsigned long flags);
- void read_lock_irq(rwlock_t* lock);
- void read_lock_bh(rwlock_t* lock);
- void read_unlock(rwlock_t* lock);
- void read_unlock_irqrestore(rwlock_t* lock,unsigned long flags);
- void read_unlock_irq(rwlock_t* lock);
- void read_unlock_bh(rwlock_t* lock);
- void write_lock(rwlock_t* lock);
- void write_lock_irqsave(rwlock_t* lock,unsigned long flags);
- void write_lock_irq(rwlock_t* lock);
- void write_lock_bh(rwlock_t* lock);
- void write_unlock(rwlock_t* lock);
- void write_unlock_irqrestore(rwlock_t* lock,unsigned long flags);
- void write_unlock_irq(rwlock_t* lock);
- void write_unlock_bh(rwlock_t* lock);
- spinlock_init(spinlock_t* lock);
- asm/atomic.h:
> 1. atomic_t v=ATOMIC_INIT(value);
- void atomic_set(atomic_t* v,int i);
- int atomic_read(atomic_t* v);
- void atomic_add(int i,atomic_t* v);
- void atomic_sub(int i,atomic_t* v) ;
- void atomic_inc(atomic_t* v);
- void atomic_dec(atomic* v);
- int atomic_inc_and_test(atomic_t* v);
- int atomic_dec_and_test(atomic_t* v);
- int atomic_sub_and_test(atomic_t* v);
- int atomic_add_negative(int i,atomic_t* v);
- int atomic_add_return(int i,atomic_t* v);
- int atomic_sub_return(int i,atomic_t* v);
- int atomic_inc_return(atomic_t* v);
- int atomic_dec_return(atomic_t* v);
- void atomic_set(atomic_t* v,int i);
- asm/bitops.h:
> 1. void set_bit(nr,void* addr);
- void clear_bit(nr,void* addr);
- void change_bit(nr,void* addr);
- test_bit(nr,void* addr);
- int test_and_set_bit(nr,void* addr);
- int test_and_clear_bit(nr,void* addr);
- int test_and_change_bit(nr,void* addr);
- void clear_bit(nr,void* addr);
- linux/seqlock.h:
> 1. seqlock_t lock=SEQLOCK_UNLOCKED;
- seqlock_init(seqlock_t* lock);
- unsigned int read_seqbegin(seqlock_t* lock);
- unsigned int read_seqbegin_irqsave(seqlock_t* lock,unsigned long flags);
- int read_seqretry(seqlock_t* lock,unsigned int seq);
- int read_seqretry_irqrestore(seqlock_t* lock,unsigned int seq,unsigned long flags);
- void write_seqlock(seqlock_t* lock);
- void write_seqlock_irqsave(seqlock_t* lock,unsigned long flags);
- void write_seqlock_irq(seqlock_t* lock);
- void write_seqlock_bh(seqlock_t* lock);
- seqlock_init(seqlock_t* lock);
- linux/rcupdate.h:
> 1. void rcu_read_lock;
- void rcu_read_unlock;
- void call_rcu(struct rcu_head* head,void (func)(void arg),void* arg);
- void rcu_read_unlock;
-
linux/param.h:
> 1. HZ; - linux/jiffies.h:
> 1. volatile unsigned long jiffies;
- u64 jiffies_64;
- int time_after(unsigned long a,unsigned long b);
- int time_before(unsigned long a,unsigned long b);
- int time_after_eq(unsigned long a,unsigned long b);
- int time_before_eq(unsigned long a,unsigned long b);
- u64 get_jiffies_64(void);
- u64 jiffies_64;
- linux/time.h:
> 1. unsigned long timespec_to_jiffies(struct timespec* value);
- void jiffies_to_timespec(unsigned long jiffies,struct timespec* value);
- unsigned long timeval_to_jiffies(struct timeval* value);
- void jiffies_to_timeval(unsigned long jiffies,struct timeval* value);
- void jiffies_to_timespec(unsigned long jiffies,struct timespec* value);
- asm/msr.h:
> 1. rdtsc(low32,high32);
- rdtscl(low32);
- rdtscl(low32);
-
linux/wait.h:
> 1. long wait_event_interruptible_timeout(wait_queue_head_t* q,condition,signed long timeout)//睡眠直到condition为真或超时; -
linux/sched.h:
> 1. signed long schedule_timeout(signed long timeout); - linux/delay.h:
> 1. void ndelay(unsigned long nsecs);
- void udelay(unsigned long usecs);
- void mdelay(unsigned long msecs);
- void msleep(unsigned int millisecs);
- unsigned long msleep_interruptible(unsigned int millisecs);
- void ssleep(unsigned int seconds);
- void udelay(unsigned long usecs);
- asm/hardirq.h:
> 1. int in_interrupt(void);
- int in_atomic(void);
- int in_atomic(void);
- linux/timer.h:
> 1. void init_timer(struct timer_list* timer);
- struct timer_list TIMER_INITIALIZER( function,expires,data);
- void add_timer(struct timer_list* timer);
- int mod_timer(struct timer_list* timer,unsigned long expires);
- int timer_pending(struct timer_list* timer);
- void del_timer(struct timer_list* timer);
- void del_timer_sync(struct timer_list* timer);
- struct timer_list TIMER_INITIALIZER( function,expires,data);
- linux/interrupt.h:
> 1. DECLARE_TASKLET(name,func,data);
- DECLARE_TASKLET_DISABLE(name,func,data);
- DECLARE_TASKLET_DISABLE(name,func,data);
- linux/workqueue.h:
> 1. struct workqueue_struct;
- struct work_struct;
- struct workqueue_struct* create_workqueue(const char* name);
- struct workqueue_struct* create_singlethread_workqueue(const char* name);
- void destroy_workqueue(struct workqueue_struct* queue);
- DECLARE_WORK(name, void (*function)(void *), void *data);
- INIT_WORK(struct work_struct work, void (function)(void *), void *data);
- PREPARE_WORK(struct work_struct work, void (function)(void *), void *data);
- int queue_work(struct workqueue_struct* queue,struct work_struct* work);
- int queue_delayed_work(struct workqueue_struct* queue,struct work_struct* work,unsigned long delay);
- int cancel_delayed_work(struct work_struct* work);
- void flush_workqueue(struct workqueue_struct* queue);
- int schedule_work(struct work_struct* work);
- int schedule_delayed_work(struct work_struct* work,unsigned long delay);
- void flush_scheduled_work(void);
- struct work_struct;
- linux/slab.h:
> 1. void* kmalloc(size_t size,int flags);
- void kfree(void* kobj);
- void kfree(void* kobj);
- linux/mm.h:
> 1. GFP_USER;
- GFP_KERNEL;
- GFP_NOFS;
- GFP_NOIO;
- GFP_ATOMIC;
- GFP_KERNEL;
- linux/malloc.h:
> 1. kmem_cache_t* kmem_cache_create(char* name,size_t size,size_t offset,unsigned long flags,constructor(),destructor());
- int kmem_cache_destory(kmem_cache_t* cache);
- void* kmem_cache_alloc(kmem_cache_t* cache,int flags);
- void kmem_cache_free(kmem_cache_t* cache,const void* obj);
- int kmem_cache_destory(kmem_cache_t* cache);
- linux/mempool.h:
> 1. mempool_t* mempool_create(int min_nr,mempool_alloc_t* alloc_fn,mempool_free_t* free_fn,void* data);
- void mempool_destory(mempool_t* pool);
- void mempool_alloc(mempool_t* pool,int gfp_mask);
- void mempool_free(void* element,mempool_t* pool);
- unsigned long get_zeroed_page(int flags);
- unsigned long __get_free_page(int flags);
- unsigned long __get_free_pages(int flags,unsigned long order);
- int get_order(unsigned long size);
- void free_page(unsigned long addr);
- void free_pages(unsigned long addr,unsigned long order);
- struct page* alloc_pages_node(int nid,unsigned int flags,unsigned int order);
- struct page* alloc_pages(unsigned int flags,unsigned int order);
- struct page* alloc_page(unsigned int flags);
- void __free_page(struct page* page);
- void __free_pages(struct page* page,unsigned int order);
- void free_hot_page(struct page* page);
- void mempool_destory(mempool_t* pool);
- linux/vmalloc.h:
> 1. void* vmalloc(unsigned long size);
- void vfree(void* addr);
- void vfree(void* addr);
- asm/io.h:
> 1. void* ioremap(unsigned long offset,unsigned long size);
- void iounmap(void* addr);
- void iounmap(void* addr);
- linux/percpu.h:
> 1. DEFINE_PER_CPU(type,name);
- DECLARE_PER_CPU(type,name);
- per_cpu(variable,int cpu_id);
- get_cpu_var(variable);
- put_cpu_var(variable);
- void* alloc_percpu(type);
- void* __alloc_percpu(size_t size,size_t align);
- free_percpu(void* variable);
- int get_cpu();
- void put_cpu();
- per_cpu_ptr(void* variable,int cpu_id);
- DECLARE_PER_CPU(type,name);
- linux/bootmem.h:
> 1. void* alloc_bootmem(unsigned long size);
- void* alloc_bootmem_low(unsigned long size);
- void* alloc_bootmem_pages(unsigned long size);
- void* alloc_bootmem_low_pages(unsigned long size);
- void free_bootmem(unsigned long addr,unsigned long size);
- void* alloc_bootmem_low(unsigned long size);
-
linux/interrupt.h:
> 1.int request_irq(unsigned int irq, irqreturn_t (*handler)( ), unsigned long flags, constchar *dev_name, void *dev_id);
2.void free_irq(unsigned int irq, void *dev_id); -
linux/irq.h:
> 1. int can_request_irq(unsigned int irq, unsigned long flags); - asm/signal.h:
> 1. unsigned long probe_irq_on(void);
- int probe_irq_off(unsigned long);
- void disable_irq(int irq);
- void disable_irq_nosync(int irq);
- void enable_irq(int irq);
- void local_irq_save(unsigned long flags);
- void local_irq_restore(unsigned long flags);
- void local_irq_disable(void);
- void local_irq_enable(void);
- int probe_irq_off(unsigned long);