Pridať komentár

Príspevok, na ktorý odpovedáte

Re: cdev_del a zaznam v /proc/devices 02.08.2010 | 23:25
Avatar m4jkl   Používateľ
jasne, ze nevadi :)
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/init.h>
#include <linux/module.h>

MODULE_DESCRIPTION("sample\n\n"
					"module.");
MODULE_AUTHOR("me");
MODULE_LICENSE("GPL");		// do not 'taint' the running kernel
MODULE_VERSION("0.0.3");	


// playing with paramters
static short test = 0;
module_param(test, short, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
MODULE_PARM_DESC(test, "test test desc");


static dev_t mydev_num = MKDEV(0, 0);		 

static struct cdev *mydev_cdev = NULL;	
				
// file operations. comming soon :)
static struct file_operations mydev_fops = {
	.owner = THIS_MODULE
};

static int __init mymodule_init(void) {
	
	int res = printk(KERN_INFO "mymodule: requesting major/minor numbers\n");
	// get major number and reserve some minor numbers
	// params: dev_t structure, first minor number, number of desired iminor numbers, new device's name
	res = alloc_chrdev_region(&mydev_num, MINOR(mydev_num), 1, "mydev");
	if (res < 0)
		goto alloc_failed;

	printk(KERN_INFO "mymodule: initializing device sctructure\n");
	// initialization of device structure
	mydev_cdev = cdev_alloc();
	if (mydev_cdev == NULL) {
		res = -ENOMEM;
		goto cdev_failed;
	}

	// connect file operations with device
	mydev_cdev->ops = &mydev_fops;

	printk(KERN_INFO "mymodule: adding device to the system\n");
	// add device to the system, if successful then it should be visible in /proc/devices
	// params: device struct, device numbers, number of minor numbers
	res = cdev_add(mydev_cdev, mydev_num, 1);
	if (res < 0)
		goto add_failed;

	printk(KERN_INFO "mymodule: all done :)\n");
	printk(KERN_INFO "mymodule: %hd has been given to module\n", test); // or nothing has been given
	return 0;		// ok, module initialized

	// something's horribly wrong. blame canada
	add_failed:
		// dealocate device
		cdev_del(mydev_cdev);

	cdev_failed:
		// return mojor/minor numbers back to kernel
		unregister_chrdev_region(MINOR(mydev_num), 1);
	
	alloc_failed:
		// ooops, no major/minor numbers for us
	return res;
}

static void __exit mymodule_cleanup(void)
{
	cdev_del(mydev_cdev);
	unregister_chrdev_region(MINOR(mydev_num), 1);
	printk(KERN_INFO "mymodule: successfuly removed\n");
}

module_init(mymodule_init);
module_exit(mymodule_cleanup);
5 + 9 plus tisíc (číslom)

Maximálna veľkosť je: 2,0 MB