Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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).
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.
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.
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:
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.
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.
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.
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 ?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.