Пример #1
0
/*模块加载方法*/
static int __init hello_init(void){
    int err = -1;
    dev_t dev = 0;
    struct device* temp = NULL;

    printk(KERN_ALERT"Initializing hello device.\n");

    /*动态分配主设备和从设备号*/
    err = alloc_chrdev_region(&dev, 0, 1, HELLO_DEVICE_NODE_NAME);
    if(err < 0) {
        printk(KERN_ALERT"Failed to alloc char dev region.\n");
        goto fail;
    }

    hello_major = MAJOR(dev);
    hello_minor = MINOR(dev);

    /* 分配hello设备结构体变量 */
    hello_dev = kmalloc(sizeof(struct hello_android_dev), GFP_KERNEL);
    if(!hello_dev) {
        err = -ENOMEM;
        printk(KERN_ALERT"Failed to alloc hello_dev.\n");
        goto unregister;
    }

    /*初始化设备*/
    err = __hello_setup_dev(hello_dev);
    if(err) {
        printk(KERN_ALERT"Failed to setup dev: %d.\n", err);
        goto cleanup;
    }

#ifdef HELLO_DEVFS_BUILD
    /*在/sys/class/目录下创建设备类别目录hello*/
    hello_class = class_create(THIS_MODULE, HELLO_DEVICE_CLASS_NAME);
    if(IS_ERR(hello_class)) {
        err = PTR_ERR(hello_class);
        printk(KERN_ALERT"Failed to create hello class.\n");
        goto destroy_cdev;
    }

    /*在/dev/目录和/sys/class/hello目录下分别创建设备文件hello*/
    temp = device_create(hello_class, NULL, dev, "%s", HELLO_DEVICE_FILE_NAME);
    if(IS_ERR(temp)) {
        err = PTR_ERR(temp);
        printk(KERN_ALERT"Failed to create hello device.");
        goto destroy_class;
    }

    /*在/sys/class/hello/hello目录下创建属性文件val*/
    err = device_create_file(temp, &dev_attr_val);
    if(err < 0) {
        printk(KERN_ALERT"Failed to create attribute val.");
        goto destroy_device;
    }

    dev_set_drvdata(temp, hello_dev);
#endif  /*  #ifdef HELLO_DEVFS_BUILD    */

#ifdef  HELLO_PROC_BUILD
    /*创建/proc/hello文件*/
    hello_create_proc();
#endif

    printk(KERN_ALERT"Succedded to initialize hello device.\n");
    return 0;

destroy_device:
    device_destroy(hello_class, dev);

destroy_class:
    class_destroy(hello_class);

destroy_cdev:
    cdev_del(&(hello_dev->dev));

cleanup:
    kfree(hello_dev);

unregister:
    unregister_chrdev_region(MKDEV(hello_major, hello_minor), 1);

fail:
    return err;
}
Пример #2
0
static int __init hello_init(void) {
	int err = -1;
	dev_t dev = 0;
	struct device* temp = NULL;

	printk(KERN_ALERT "Initializing hello device.\n");

	err = alloc_chrdev_region(&dev, 0, 1, HELLO_DEVICE_NODE_NAME);
	if (err < 0) {
		printk(KERN_ALERT "Failed to alloc char dev region. \n");
		goto fail;
	}

	hello_major = MAJOR(dev);
	hello_minor = MINOR(dev);

	hello_dev = kmalloc(sizeof(struct hello_android_dev), GFP_KERNEL);
	if (!hello_dev) {
		err = -ENOMEM;
		printk(KERN_ALERT "Failed to alloc hello_dev.\n");
		goto unregister;
	}

	err = __hello_setup_dev(hello_dev);
	if (err) {
		printk(KERN_ALERT "Failed to setup dev: %d.\n", err);
		goto cleanup;
	}

	/* create the hello directory under directory /sys/class */
	hello_class = class_create(THIS_MODULE, HELLO_DEVICE_CLASS_NAME);
	if (IS_ERR(hello_class)) {
		err = PTR_ERR(hello_class);
		printk(KERN_ALERT "Failed to create hello class.\n");
		goto destroy_cdev;
	}

	/* create the hello file under directory /dev/ and /sys/class/hello */
	temp = device_create(hello_class, NULL, dev, NULL, "%s", HELLO_DEVICE_FILE_NAME);
	if (IS_ERR(temp)) {
		err = PTR_ERR(temp);
		printk(KERN_ALERT "Failed to create hello device.\n");
		goto destroy_class;
	}

	/* crate the property file val under directory /sys/class/hello/hello */
	err = device_create_file(temp, &dev_attr_val);
	if (err < 0) {
		printk(KERN_ALERT "Failed to create attribute val.\n");
		goto destroy_device;
	}

	dev_set_drvdata(temp, hello_dev);

	hello_create_proc();
	
	printk(KERN_ALERT "Succedded to initialize hello device.\n");
	return 0;

destroy_device:
	device_destroy(hello_class, dev);

destroy_class:
	class_destroy(hello_class);

destroy_cdev:
	cdev_del(&(hello_dev->dev));

cleanup:
	kfree(hello_dev);

unregister:
	unregister_chrdev_region(MKDEV(hello_major, hello_minor), 1);

fail:
	return err;
}