Exemple #1
0
static int startup(void)
{
    dev_t dev = MKDEV(major, minor);
    int result = 0;
    memory = NULL;
    

    printk(KERN_INFO "hello: startup\n");
    
    if (! request_region(lpt_port, SHORT_NR_PORTS, "MY_LPT"))  {
            printk(KERN_INFO "hello: can't get I/O mem address 0x%lx\n", lpt_port);
            return -ENODEV;
    }
    printk(KERN_WARNING "hello: request_region: port 0x%lx hooked\n", lpt_port);

    // get a driver number
    if (major) {
            dev = MKDEV(major, minor);
            result = register_chrdev_region(dev, 1, "hello");
    } else {
            result = alloc_chrdev_region(&dev, minor, 1, "hello");
            major = MAJOR(dev);
    }
    
    if (result < 0) {
            printk(KERN_WARNING "hello: can't get version %d:%d\n", major, minor);
            return result;
    }

       
    // Initialize the device.	
    my_cdev = cdev_alloc();
    if(!my_cdev) {
            printk(KERN_WARNING "hello:  cdev_alloc failed");
            return -1;
    }

    my_cdev->ops = &my_ops;
    
    if(cdev_add(my_cdev, dev, 1) < 0) {
            printk(KERN_WARNING "hello:  cdev_add failed");
            return -1;
    }

    
    hello_create_proc(); // proc debugging

    printk(KERN_WARNING "hello: got version %d:%d\n", major, minor);
    printk(KERN_WARNING "hello: my_cdev allocated\n");
    printk(KERN_WARNING "hello: my_cdev added\n");

    return 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;
}
Exemple #3
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;
}