LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-07-2009, 07:04 PM   #1
saurabhchokshi
Member
 
Registered: May 2007
Posts: 35

Rep: Reputation: 15
Create a device file using udev


Hello Folks,

I want to create a device file without using mknod command. However, I want to create a device file using udev framework in Linux Kernel.

Could you please help me out to implement the device driver in the probe function that create the device file dynamically in /dev directory?

Thanks,
Saurabh
 
Old 07-07-2009, 07:50 PM   #2
Uncle_Theodore
Member
 
Registered: Dec 2007
Location: Charleston WV, USA
Distribution: Slackware 12.2, Arch Linux Amd64
Posts: 896

Rep: Reputation: 71
Well, you can read here
http://reactivated.net/writing_udev_rules.html
or, in a shorter version, here
http://www.linuxjournal.com/article/7316

udev also has documentation files, but they are even harder to read...
 
Old 07-07-2009, 09:06 PM   #3
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
It’s unclear if you are in userspace or kernelspace…

In the kernel, you have various options, depending on how low-level you want to get. You can use (in order of decreasing abstractness), device_create(), device_register(), device_add(), or even kobject_add().

See the kernel’s docbook guide entitled “Linux Device Drivers” (try “make htmldocs” from the kernel sourcetree).
 
Old 07-08-2009, 11:30 AM   #4
saurabhchokshi
Member
 
Registered: May 2007
Posts: 35

Original Poster
Rep: Reputation: 15
Hi,

I am in Kernel Space.

I have a character device file and I do not want to execute the mknod command to create device file. I want to create device file using kernel module.

In addition to that, I want to create an entry inside sys directory.

For Example, I have chardev character device file and I want to create the chardev file without mknod command. Also, I need to create a file inside /sys/dev/char/chardev using the kernel module of my device driver.

Actually I know that you can use device_create(), device_register(), device_add(), or even kobject_add() but for this API we need to have "struct kobject *kobj", struct pointer of subsystem like bus, devices, dev, module, etc. , struct pointer of struct class, and struct pointer of attributes (this can be define easily).

Whenever, I build the character device driver, I am using register_chrdev() to register the character device based on Major Num and File Operation pointer. However, I do not know how to get the pointer of kobject of any class, subsystem or device.

Could you please suggest me how to do that?

Thanks,
Saurabh Chokshi
 
Old 07-10-2009, 04:28 PM   #5
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
I don’t quite understand your problem with device_create(). We might need some sample code. Here is a simple chardev module which returns "foo\n" whenever read from.
Code:
$ cat > foo.c << EOF
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <asm/uaccess.h>
#include <linux/device.h>

MODULE_LICENSE("GPL");

#define foo "foo"
#define foolen 4

static ssize_t read_foo(struct file *f, char __user *buf,
			size_t len, loff_t *off)
{
	size_t i = min_t(size_t, len, foolen);
	return copy_to_user(buf, foo "\n", i) ? -EFAULT : i;
}

static int major;
static struct class *class_foo;
static struct device *dev_foo;
static struct file_operations f = { .read = read_foo };

int init_module(void)
{
	void *ptr_err;
	if ((major = register_chrdev(0, foo, &f)) < 0)
		return major;

	class_foo = class_create(THIS_MODULE, foo);
	if (IS_ERR(ptr_err = class_foo))
		goto err2;

	dev_foo = device_create(class_foo, NULL, MKDEV(major, 0), NULL, foo);
	if (IS_ERR(ptr_err = dev_foo))
		goto err;

	/* struct kobject *play_with_this = &dev_foo->kobj; */

	return 0;
err:
	class_destroy(class_foo);
err2:
	unregister_chrdev(major, foo);
	return PTR_ERR(ptr_err);
}

void cleanup_module(void)
{
	device_destroy(class_foo, MKDEV(major, 0));
	class_destroy(class_foo);
	return unregister_chrdev(major, foo);
}
EOF
$ cat > Makefile << EOF
obj-m = foo.o
EOF
$ make M=$PWD -C /lib/modules/`uname -r`/build
make: Entering directory `/usr/src/linux-2.6.30'
  LD      /opt/dvds/src/lq/saurabhchokshi/built-in.o
  CC [M]  /opt/dvds/src/lq/saurabhchokshi/foo.o
  Building modules, stage 2.
  MODPOST 1 modules
  CC      /opt/dvds/src/lq/saurabhchokshi/foo.mod.o
  LD [M]  /opt/dvds/src/lq/saurabhchokshi/foo.ko
make: Leaving directory `/usr/src/linux-2.6.30'
$ head /dev/foo
head: cannot open `/dev/foo' for reading: No such file or directory
$ sudo insmod foo.ko
$ ls -l /dev/foo
crw-rw---- 1 root root 248, 0 2009-07-10 17:28 /dev/foo
$ sudo head /dev/foo
foo
foo
foo
foo
foo
foo
foo
foo
foo
foo
$ ls /sys/class/foo/ /sys/dev/char/248\:0/subsystem/
/sys/class/foo/:
foo

/sys/dev/char/248:0/subsystem/:
foo
What more do you want?
 
Old 07-11-2009, 02:34 PM   #6
saurabhchokshi
Member
 
Registered: May 2007
Posts: 35

Original Poster
Rep: Reputation: 15
Hi ,

Thanks for that help. However, I have already implemented that two days back.

However, I have to create the symbolic link of the device in the same class.

For Example,

/dev/device1
/dev/device2

/sys/class/myClass/mydevice1
/sys/class/myClass/mydevice2
/sys/class/myClass/mydevice -> mydevice1

I want to create the symbolic link of mydevice1 as mydevice.

I am using sysfs_create_link(&mydevice_class->dev_kobj,&mydevice2->kobj, "mydevice3")

mydevice_class is the class struct object.
mydevice2 is the device struct object after creating device_create().

I got the error while creating the link.

I just want to confirm, am I passing wrong parameter inside the sysfs_create_link function.

Please help me out for this issue.

Thanks,
Saurabh
 
Old 07-12-2009, 06:59 PM   #7
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
Quote:
Originally Posted by saurabhchokshi View Post
However, I have to create the symbolic link of the device in the same class.
I’m having a hard time understanding why you want this. In any case, the code you’ve provided is wrong. You are taking the address of a pointer to struct kobject, whereas the pointer alone is required. E.g., change this
Code:
&mydevice_class->dev_kobj
to this
Code:
mydevice_class->dev_kobj
On the other hand, I do not think this is kobject you are looking for. In fact, I know of no clean method to get at the kobject for a device’s class (i.e., entry in /sys/class).

For example, in the code I posted above, you have the following:
Code:
&dev_foo->kobj /* refers to the path    /sys/devices/virtual/foo/foo */
class_foo->dev_kobj /* is for the path  /sys/dev/char */
dev_foo->class->dev_kobj /* is also for /sys/dev/char */
If you want a hackish, subject-to-change method you might try:
Code:
&((struct kset*)class_foo->p)->kobj /* /sys/class/foo */
The only other way (which I can think of) to obtain a kobject for /sys/class/foo is to do a dirent lookup manually.

Last edited by osor; 07-13-2009 at 05:15 PM.
 
Old 07-12-2009, 07:20 PM   #8
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,139

Rep: Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122
According to this use container_of() macro.
 
Old 07-12-2009, 11:44 PM   #9
saurabhchokshi
Member
 
Registered: May 2007
Posts: 35

Original Poster
Rep: Reputation: 15
Hi ,

I am doing because I want to point the default driver name called "device" as device1. For that, I want to create a symbolic link to point the default device as device1.

Sorry I was passing the reference of kobj for struct class.

Thanks a lot for your help. It works!!!

Saurabh
 
Old 07-13-2009, 05:15 PM   #10
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
Quote:
Originally Posted by syg00 View Post
According to this use container_of() macro.
Although my original was not as succinct as it could have been (this has since been edited), you can’t use container_of() or other similar macros here for a few reasons, the least of which is that the type of the element class_foo->p is incomplete. It is a private struct whose implementation is not supposed to be visible those outside the class creation code. In fact, the entire kset corresponding to /sys/class is static (unlike those for e.g., /sys/kernel or /sys/power), so you can’t even use it from built-in kernel code without modifying the relevant file.
 
Old 07-16-2009, 03:41 PM   #11
saurabhchokshi
Member
 
Registered: May 2007
Posts: 35

Original Poster
Rep: Reputation: 15
Hi,

Through device_create(), device can be created in the /dev/ and the device would be registered in the /sys/dev/char/.

Is there anyway to create the symbolic link in the /dev/ directory.

For Example,

/dev/mydevice -> /dev/mydevice0
/dev/mydevice0
/dev/mydevice1

Thanks,
Saurabh Chokshi
 
Old 07-20-2009, 11:17 AM   #12
kdawgud
LQ Newbie
 
Registered: Mar 2009
Posts: 4

Rep: Reputation: 0
Functions without GPL?

Quote:
Originally Posted by osor View Post
In the kernel, you have various options, depending on how low-level you want to get. You can use (in order of decreasing abstractness), device_create(), device_register(), device_add(), or even kobject_add().
Is there a way to accomplish device file creation without declaring the module GPL? Currently I use a mknod script, but it would be great to have the module do this automatically when loaded. GPL may be an issue, however.

Thanks!
 
Old 09-03-2009, 12:10 PM   #13
saurabhchokshi
Member
 
Registered: May 2007
Posts: 35

Original Poster
Rep: Reputation: 15
Can one create symlinks in /dev ?

Hello Folks,

Is there anyway to create the symbolic link in the /dev/ directory.

For Example,

/dev/mydevice -> /dev/mydevice0
/dev/mydevice0
/dev/mydevice1

Thanks,
Saurabh Chokshi
 
Old 09-03-2009, 03:49 PM   #14
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
I split your hi-jack attempt out into its own thread.

And to answer your question: yes, there is.
Several, indeed.
ln -s to begin with.
And then of course via udev-rules (more work, more elegant ;})



Cheers,
Tink
 
Old 09-03-2009, 04:16 PM   #15
seaquest
LQ Newbie
 
Registered: Sep 2009
Posts: 1

Rep: Reputation: 0
device file permission

OSOR:

The way you posted to create a device file under /dev works, but in
my case, the default permission to the device file is crw-------, and the file is owned by root.

How can I change the permission to crw-rw-rw- from within the
kernen module code, I mean, after calling class_create() and device_create(), what function call would change the device file permission ?

Many thanks.


--Seaquest
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to create a device file in linux? chaitanya1982 Linux - Newbie 3 09-24-2008 12:42 PM
udev, create raw disk device, how? thllgo Red Hat 2 01-23-2008 10:50 AM
udev doesn't create scd* device nodes for scsi cdrom kejava Slackware 3 09-15-2005 02:04 PM
udev does not create scsi device node maenho Linux - Software 0 11-27-2004 05:08 AM
How can we create file device? l_9_l Linux - General 1 01-11-2002 10:50 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:53 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration