Tuesday, May 3, 2011

Create device entries via udev

blog entry
http://ramblings.narrabilis.com/wp/pi/
code
http://ramblings.narrabilis.com/src/pi-0.1.tar.bz2

static int pi_init(void)
{
        struct device *err_dev;

        msg = kmalloc(sizeof(char)*DIGITS, GFP_ATOMIC);

        // register pi as a device
        pi_Major = register_chrdev(0, DEVICE_NAME, &fops);

        if (pi_Major < 0) {
                printk ("Failed to register device %s with error %d\n", DEVICE_NAME, pi_Major);
                return pi_Major;
        }

        /* create /dev/pi
         * we use udev to make the file
         */
        pi_class = class_create(THIS_MODULE,DEVICE_NAME);
        err_dev = device_create(pi_class, NULL, MKDEV(pi_Major,0),NULL,DEVICE_NAME);

        return 0;
}

/* remove the module
*/
static void pi_exit(void)
{
        /* free our memory, then destroy the device
         * then unregister the class and destroy it (unregister before destroy....important
         */
        kfree(msg);
        device_destroy(pi_class,MKDEV(pi_Major,0));
        class_unregister(pi_class);
        class_destroy(pi_class);
        unregister_chrdev(pi_Major, DEVICE_NAME);
}

No comments:

Post a Comment