1、device_create 

如果成功,它将会在 /dev 目录下产生 /dev/mdev 设备

2. device_create_file

使用这个函数时要引用 device_create 所返回的 device* 指针,作用是在 /sys/class/ 下创建一个属性文件,从而通过对这个属性文件进行读写就能完成对应的数据操作。

/**
 * 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, ...)



/**
 * 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)



/*********************** 代码例子 *****************************/
struct mydev {
    const char *name;
    struct device *dev;
    int id;
    int index;
};

struct class mdev_class = class_create(THIS_HODULE, "mydevice"); // 创建class
struct mydev *mdev;


int mdev_register(struct mdev *sdev)
{
	int ret;
	sdev->index = atomic_inc_return(&device_count);
	sdev->dev = device_create(mdev_class , NULL, MKDEV(0, sdev->index), NULL, sdev->name);
	if (IS_ERR(sdev->dev))
		return PTR_ERR(sdev->dev);
	ret = device_create_file(sdev->dev, &dev_attr_id);
	if (ret < 0)
		goto err_create_file_1;
	ret = device_create_file(sdev->dev, &dev_attr_name);
	if (ret < 0)
		goto err_create_file_2;
	dev_set_drvdata(sdev->dev, sdev);
	sdev->id = 0;
	return 0;

err_create_file_2:
	device_remove_file(sdev->dev, &dev_attr_id);
	return ret;

err_create_file_1:
	device_destroy(criticallog_class, MKDEV(0, sdev->index));

	return ret;
}
EXPORT_SYMBOL_GPL(mdev_register);

 

GitHub 加速计划 / li / linux-dash
10.39 K
1.2 K
下载
A beautiful web dashboard for Linux
最近提交(Master分支:2 个月前 )
186a802e added ecosystem file for PM2 4 年前
5def40a3 Add host customization support for the NodeJS version 4 年前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐