打印

scull字符设备之scull.h

[复制链接]
107|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
一亚麻怡|  楼主 | 2018-9-21 15:14 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
/*

* scull.h -- definitions for the char module

*

* 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.

*

* $Id: scull.h,v 1.15 2004/11/04 17:51:18 rubini Exp $

*/



/* scull: Sample Character Utility for Loading Localities */



#ifndef _SCULL_H_

#define _SCULL_H_



#include <linux/ioctl.h> /* needed for the _IOW etc stuff used later */



/*

* Macros to help debugging

*/



// #undef 是在后面取消以前定义的宏定义

// 下面是如何处理打印信息的宏定义

#undef PDEBUG             /* undef it, just in case */

#ifdef SCULL_DEBUG

#  ifdef __KERNEL__

     /* This one if debugging is on, and kernel space */

#    define PDEBUG(fmt, args...) printk( KERN_DEBUG "scull: " fmt, ## args)

#  else

     /* This one for user space */

#    define PDEBUG(fmt, args...) fprintf(stderr, fmt, ## args)

#  endif

#else

#  define PDEBUG(fmt, args...) /* not debugging: nothing */

#endif



#undef PDEBUGG

#define PDEBUGG(fmt, args...) /* nothing: it's a placeholder */



// 主设备号

#ifndef SCULL_MAJOR

#define SCULL_MAJOR 0   /* dynamic major by default */

#endif



// scull 字符设备总数

#ifndef SCULL_NR_DEVS

#define SCULL_NR_DEVS 4    /* scull0 through scull3 */

#endif



// scull_pipe 字符设备总数

#ifndef SCULL_P_NR_DEVS

#define SCULL_P_NR_DEVS 4  /* scullpipe0 through scullpipe3 */

#endif



/*

* The bare device is a variable-length region of memory.

* Use a linked list of indirect blocks.

*

* "scull_dev->data" points to an array of pointers, each

* pointer refers to a memory area of SCULL_QUANTUM bytes.

*

* The array (quantum-set) is SCULL_QSET long.

*/

// 量子大小(字节)

#ifndef SCULL_QUANTUM

#define SCULL_QUANTUM 4000

#endif



// 量子集大小(包含的量子数目)

#ifndef SCULL_QSET

#define SCULL_QSET    1000

#endif



/*

* The pipe device is a simple circular buffer. Here its default size

*/

// scull_pipe 默认缓冲大小

#ifndef SCULL_P_BUFFER

#define SCULL_P_BUFFER 4000

#endif



/*

* Representation of scull quantum sets.

*/

struct scull_qset {

void **data;// 当前量子集首地址

struct scull_qset *next;// 下一个量子集地址

};



struct scull_dev {

// 第一个量子集

struct scull_qset *data;  /* Pointer to first quantum set */

// 每一个量子所占字节数

int quantum;              /* the current quantum size */

// 每一个量子集所含量子数

int qset;                 /* the current array size */

// 存储的数据量

unsigned long size;       /* amount of data stored here */

unsigned int access_key;  /* used by sculluid and scullpriv */

// 信号量

struct semaphore sem;     /* mutual exclusion semaphore     */

// 字符设备结构

struct cdev cdev;   /* Char device structure  */

};



/*

* Split minors in two parts

*/

// 取指定参数的高、低半字

#define TYPE(minor) (((minor) >> 4) & 0xf) /* high nibble */

#define NUM(minor) ((minor) & 0xf)  /* low  nibble */





/*

* The different configurable parameters

*/

extern int scull_major;     /* main.c */

extern int scull_nr_devs;

extern int scull_quantum;

extern int scull_qset;



extern int scull_p_buffer; /* pipe.c */





/*

* Prototypes for shared functions

*/



int     scull_p_init(dev_t dev);

void    scull_p_cleanup(void);

int     scull_access_init(dev_t dev);

void    scull_access_cleanup(void);



int     scull_trim(struct scull_dev *dev);



ssize_t scull_read(struct file *filp, char __user *buf, size_t count,

                   loff_t *f_pos);

ssize_t scull_write(struct file *filp, const char __user *buf, size_t count,

                    loff_t *f_pos);

loff_t  scull_llseek(struct file *filp, loff_t off, int whence);

int     scull_ioctl(struct inode *inode, struct file *filp,

                    unsigned int cmd, unsigned long arg);





/*

* Ioctl definitions

*/



/* Use 'k' as magic number */

// 魔数

#define SCULL_IOC_MAGIC  'k'

/* Please use a different 8-bit number in your code */



// ioctl 方法的命令

#define SCULL_IOCRESET    _IO(SCULL_IOC_MAGIC, 0)



/*

* S means "Set" through a ptr,

* T means "Tell" directly with the argument value

* G means "Get": reply by setting through a pointer

* Q means "Query": response is on the return value

* X means "eXchange": switch G and S atomically

* H means "sHift": switch T and Q atomically

*/

#define SCULL_IOCSQUANTUM _IOW(SCULL_IOC_MAGIC,  1, int)

#define SCULL_IOCSQSET    _IOW(SCULL_IOC_MAGIC,  2, int)

#define SCULL_IOCTQUANTUM _IO(SCULL_IOC_MAGIC,   3)

#define SCULL_IOCTQSET    _IO(SCULL_IOC_MAGIC,   4)

#define SCULL_IOCGQUANTUM _IOR(SCULL_IOC_MAGIC,  5, int)

#define SCULL_IOCGQSET    _IOR(SCULL_IOC_MAGIC,  6, int)

#define SCULL_IOCQQUANTUM _IO(SCULL_IOC_MAGIC,   7)

#define SCULL_IOCQQSET    _IO(SCULL_IOC_MAGIC,   8)

#define SCULL_IOCXQUANTUM _IOWR(SCULL_IOC_MAGIC, 9, int)

#define SCULL_IOCXQSET    _IOWR(SCULL_IOC_MAGIC,10, int)

#define SCULL_IOCHQUANTUM _IO(SCULL_IOC_MAGIC,  11)

#define SCULL_IOCHQSET    _IO(SCULL_IOC_MAGIC,  12)



/*

* The other entities only have "Tell" and "Query", because they're

* not printed in the book, and there's no need to have all six.

* (The previous stuff was only there to show different ways to do it.

*/

#define SCULL_P_IOCTSIZE _IO(SCULL_IOC_MAGIC,   13)

#define SCULL_P_IOCQSIZE _IO(SCULL_IOC_MAGIC,   14)

/* ... more to come */



#define SCULL_IOC_MAXNR 14



#endif /* _SCULL_H_ */

复制代码

使用特权

评论回复

相关帖子

发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

404

主题

404

帖子

0

粉丝