Killertechno
Posts: 130
Joined: Wed Jan 02, 2013 8:28 am

[kernel] Permissions of character device?

Mon Feb 19, 2018 4:58 pm

Hi to all, I wrote a kernel driver some time ago, now not working at all (I suppose due kernel/OS changements).

After some copy&paste&try work I finally got my kernel driver working (more or less) driving GPIO pins as I need.

Code: Select all


...
static dev_t first; 
static struct cdev c_dev; 
static struct class *cl;
...

static int __init qcnsync_init(void) 
{
	int ret;
	struct device *dev_ret;
	contatore = 0;	// lo uso io, per debug

	if ((ret = alloc_chrdev_region(&first, 0, 1, "my_chrdev_region")) < 0)
	{
		return ret;
	}
	
	if (IS_ERR(cl = class_create(THIS_MODULE, "my_class")))
	{
		unregister_chrdev_region(first, 1);
		return PTR_ERR(cl);
	}

		
	if (IS_ERR(dev_ret = device_create(cl, NULL, first, NULL, "qcnsync")))
	{
		class_destroy(cl);
		unregister_chrdev_region(first, 1);
		return PTR_ERR(dev_ret);
	}

	cdev_init(&c_dev, &fops);
	
	if ((ret = cdev_add(&c_dev, first, 1)) < 0)
	{
		device_destroy(cl, first);
		class_destroy(cl);
		unregister_chrdev_region(first, 1);
		return ret;
	}
	
	printk(KERN_INFO "[qcnsync] <qcnsync_init>: driver initializing sequence done");
	return 0;
}

In this case I can create /dev/qcnsync character device, and I can read it:

Code: Select all

sudo cat /dev/qcnsync
pi@raspberrypi ~/mykernel2/qcnsync $ sudo cat /dev/qcnsync
Hello world!
pi@raspberrypi ~/mykernel2/qcnsync $ 

Now I would like create character device I can read from common user (without sudo).
I'm confused about major/minor.

I found this code:

Code: Select all

// see linux/Documentation/devices.txt (240-254 char = LOCAL/EXPERIMENTAL USE)
// use 0 for dynamic
#define DEV_MAJOR	240
#define DEVICE_DESC 	"QCN sync"


static char *qcnsync_devnode(struct device *dev, umode_t *mode)
{
        if (!mode)
		
            return NULL;
		
        if (dev->devt == MKDEV(DEV_MAJOR, 0) || dev->devt == MKDEV(DEV_MAJOR, 2))
                *mode = 0666;
		
        return NULL;
}
...
static int __init qcnsync_init(void) 
{
...
	if (IS_ERR(cl = class_create(THIS_MODULE, "my_class")))
	{
		unregister_chrdev_region(first, 1);
		return PTR_ERR(cl);
	}
	// per i permessi di lettura
	cl->devnode = qcnsync_devnode;
...
}
I still need to use sudo, while:

Code: Select all

static char *qcnsync_devnode(struct device *dev, umode_t *mode)
{
        if (!mode)
		
            return NULL;
		
      
                *mode = 0666;
		
        return NULL;
}
Correctly works without sudo, but I suppose isn't safe.
Sho what's correct code?
Thansk.

Return to “Interfacing (DSI, CSI, I2C, etc.)”

Who is online

Users browsing this forum: No registered users and 10 guests