Class create, device create, device create file
리 눅 스 장치 드라이버 를 쓰기 시 작 했 을 때 mknod 명령 을 이용 하여 장치 노드 를 수 동 으로 만 드 는 경우 가 많 았 습 니 다 (ldd 3 를 포함 한 많은 사례 도 마찬가지 입 니 다). 실제로 현재 리 눅 스 커 널 은 모듈 로 불 러 올 때 자동 으로 / dev 디 렉 터 리 에서 해당 장치 노드 를 만 들 고 모듈 을 마 운 트 해제 할 때 이 노드 를 삭제 할 수 있 는 함 수 를 제공 합 니 다.
커 널 에 서 는 struct class 구조 체 를 정의 합 니 다. 말 그대로 하나의 struct class 구조 체 유형 변 수 는 하나의 클래스 에 대응 하고 커 널 은 class 를 제공 합 니 다.create (...) 함 수 는 클래스 를 만 들 수 있 습 니 다. 이 클래스 는 sysfs 아래 에 저장 되 어 있 습 니 다. 이 클래스 를 만 들 면 device 를 호출 합 니 다.create (...) 함 수 는 / dev 디 렉 터 리 에 해당 하 는 장치 노드 를 만 듭 니 다.이렇게 하면 모듈 을 불 러 올 때 사용자 공간의 udev 는 device 에 자동 으로 응답 합 니 다.create (...) 함수, / sysfs 에서 해당 하 는 클래스 를 찾 아 장치 노드 를 만 듭 니 다.
그 밖 에 device 를 이용 하여create_file 함 수 는 / sys / class / 에서 해당 하 는 속성 파일 을 만 들 고 이 파일 의 읽 기와 쓰 기 를 통 해 특정한 데이터 조작 을 실현 할 수 있 습 니 다.
공식 설명:
/* This is a #define to keep the compiler from merging different
 * instances of the __key variable */
#define class_create(owner, name)		\
({						\
	static struct lock_class_key __key;	\
	__class_create(owner, name, &__key);	\
})
/**
 * class_create - create a struct class structure
 * @owner: pointer to the module that is to "own" this struct class
 * @name: pointer to a string for the name of this class.
 * @key: the lock_class_key for this class; used by mutex lock debugging
 *
 * This is used to create a struct class pointer that can then be used
 * in calls to device_create().
 *
 * Returns &struct class pointer on success, or ERR_PTR() on error.
 *
 * Note, the pointer created here is to be destroyed when finished by
 * making a call to class_destroy().
 */
struct class *__class_create(struct module *owner, const char *name,
			     struct lock_class_key *key)중요 한 한 마디 는:
 * This is used to create a struct class pointer that can then be used
 * in calls to device_create().
 -->          struct class      ,       device_create()     。
즉, 이 함 수 는 주로 device 를 호출 하고 있 습 니 다.create () 전에 사용 하여 struct class 형식의 변 수 를 만 들 고 지침 을 되 돌려 줍 니 다.
2. devicecreate
공식 설명:
/**
 * device_create - creates a device and registers it with sysfs
 * @class: pointer to the struct class that this device should be registered to
 * @parent: pointer to the parent struct device of this new device, if any
 * @devt: the dev_t for the char device to be added
 * @drvdata: the data to be added to the device for callbacks
 * @fmt: string for the device's name
 *
 * This function can be used by char device classes.  A struct device
 * will be created in sysfs, registered to the specified class.
 *
 * A "dev" file will be created, showing the dev_t for the device, if
 * the dev_t is not 0,0.
 * If a pointer to a parent struct device is passed in, the newly created
 * struct device will be a child of that device in sysfs.
 * The pointer to the struct device will be returned from the call.
 * Any further sysfs files that might be required can be created using this
 * pointer.
 *
 * Returns &struct device pointer on success, or ERR_PTR() on error.
 *
 * Note: the struct class passed to this function must have previously
 * been created with a call to class_create().
 */
struct device *device_create(struct class *class, struct device *parent,
			     dev_t devt, void *drvdata, const char *fmt, ...)먼저 "sysfs" 를 설명 하 십시오. sysfs 는 linux 2.6 이 제공 하 는 가상 파일 시스템 입 니 다.장치 모델 에서 sysfs 파일 시스템 은 장치 의 구 조 를 나타 내 고 장치 의 차원 구조 이미 지 를 사용자 공간 에 반영 하여 sysfs 의 파일 속성 을 수정 하여 장치 의 속성 값 을 수정 할 수 있 습 니 다.sysfs 는 루트 디 렉 터 리 의 "/ sys" 폴 더 에 마 운 트 되 었 습 니 다.
3. devicecreate_file
공식 설명:
/**
 * device_create_file - create sysfs attribute file for device.
 * @dev: device.
 * @attr: device attribute descriptor.
 */
int device_create_file(struct device *dev,
		       const struct device_attribute *attr)이 함 수 를 사용 할 때 device 를 참조 합 니 다.create 가 되 돌아 오 는 device * 지침 은 / sys / class / 에서 속성 파일 을 만 들 고 이 속성 파일 을 읽 고 쓰 면 해당 하 는 데이터 작업 을 수행 할 수 있 습 니 다.
예:
a. 드라이버 에서 device 사용 하기create_파일 생 성 속성 파일
static DEVICE_ATTR(val, S_IRUGO | S_IWUSR, hello_val_show, hello_val_store);  
/*     val      buf ,    */  
static ssize_t __hello_get_val(struct xxx_dev* dev, char* buf) {  
    int val = 0;          
  
    /*    */  
    if(down_interruptible(&(dev->sem))) {                  
        return -ERESTARTSYS;          
    }          
  
    val = dev->val;          
    up(&(dev->sem));          
  
    return snprintf(buf, PAGE_SIZE, "%d/n", val);  
}  
  
/*    buf         val  ,    */  
static ssize_t __hello_set_val(struct xxx_dev* dev, const char* buf, size_t count) {  
    int val = 0;          
  
    /*         */          
    val = simple_strtol(buf, NULL, 10);          
  
    /*    */          
    if(down_interruptible(&(dev->sem))) {                  
        return -ERESTARTSYS;          
    }          
  
    dev->val = val;          
    up(&(dev->sem));  
  
    return count;  
}  
  
/*      val*/  
static ssize_t hello_val_show(struct device* dev, struct device_attribute* attr, char* buf) {  
    struct xxx_dev* hdev = (struct xxx_dev*)dev_get_drvdata(dev);          
  
    return __hello_get_val(hdev, buf);  
}  
  
/*     val*/  
static ssize_t hello_val_store(struct device* dev, struct device_attribute* attr, const char* buf, size_t count) {   
    struct xxx_dev* hdev = (struct xxx_dev*)dev_get_drvdata(dev);    
      
    return __hello_set_val(hdev, buf, count);  
} 
/*      */  
static int __init xxx_init(void){   
    ... 
  
    /* /sys/class/xxx/xxx         val*/  
    err = device_create_file(temp, &dev_attr_val);  
    if(err < 0) {  
        printk(KERN_ALERT"Failed to create attribute val.");                  
        goto destroy_device;  
    }
  
    ...
} b. 사용자 공간 에서 속성 읽 기
...
read(dev->fd, val, sizeof(*val));
...
write(dev->fd, &val, sizeof(val));
...사용 예시
    /* /sys/class/           xxx*/ 
    g_vircdev_class = class_create(THIS_MODULE, VIRCDEV_CLASS_NAME);
    if(IS_ERR(g_vircdev_class)) {  
        err = PTR_ERR(g_vircdev_class);  
        printk(KERN_ALERT "Failed to create class.
");  
        goto CLASS_CREATE_ERR;  
    }
    
    /* /dev/   /sys/class/xxx           xxx*/
    dev = device_create(g_vircdev_class, NULL, devt, NULL, VIRCDEV_DEVICE_NAME);
    if(IS_ERR(dev)) {  
        err = PTR_ERR(dev);  
        printk(KERN_ALERT "Failed to create device.
");  
        goto DEVICE_CREATE_ERR;  
    }
    
    /* /sys/class/xxx/xxx         val*/ 
    err = device_create_file(dev, attr);  
    if(err < 0) {  
        printk(KERN_ALERT"Failed to create attribute file.");                  
        goto DEVICE_CREATE_FILE_ERR;  
    }참고 자료:
sysfs:
http://blog.csdn.net/dianhuiren/article/details/6928500
http://tech.ccidnet.com/art/302/20080304/1379211_1.html
http://blog.csdn.net/chchchdx123/article/details/6248486
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.