Discussion:
[Question: Drivers/base/core.c] Why dev->init_name = NULL in device_add function ?
Aniroop Mathur
2014-08-01 17:13:23 UTC
Permalink
Dear Mr. Greg Kroah-Hartman and Linux Community,
Greetings of the day !! :)

I am Aniroop Mathur working on Linux Kernel for last two years.
I am stuck at one point and could not find the solution over internet.
I posted on linuxquestions.org too.
So I need your help and suggestion for it.

Can you please help in answering my query as below:

=====================================================
In function device_add of /drivers/base/core.c file, it is mentioned:
/*
* for statically allocated devices, which should all be converted
* some day, we need to initialize the name. We prevent reading back
* the name, and force the use of dev_name()
*/
if (dev->init_name) {
dev_set_name(dev, "%s", dev->init_name);
dev->init_name = NULL;
}


Except forcing the use of dev_name to read device name,
Is there any other reason to make init_name as NULL ?
And if it is not made NULL, is there any problem or side-effect ?

=======================================================

Link at linuxquestions.org:
http://www.linuxquestions.org/questions/linux-kernel-70/why-dev-init_name-%3D-null-in-device_add-function-4175504749/


Thanks a lot in advance !

Best Regards,
Aniroop Mathur
--
To unsubscribe from this list: send the line "unsubscribe linux-hotplug" in
the body of a message to ***@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Greg KH
2014-08-01 19:23:00 UTC
Permalink
Post by Aniroop Mathur
Dear Mr. Greg Kroah-Hartman and Linux Community,
Greetings of the day !! :)
I am Aniroop Mathur working on Linux Kernel for last two years.
I am stuck at one point and could not find the solution over internet.
I posted on linuxquestions.org too.
So I need your help and suggestion for it.
=====================================================
/*
* for statically allocated devices, which should all be converted
* some day, we need to initialize the name. We prevent reading back
* the name, and force the use of dev_name()
*/
if (dev->init_name) {
dev_set_name(dev, "%s", dev->init_name);
dev->init_name = NULL;
}
Except forcing the use of dev_name to read device name,
Is there any other reason to make init_name as NULL ?
Why would you want init_name to not be NULL?
Post by Aniroop Mathur
And if it is not made NULL, is there any problem or side-effect ?
Yes, people would start to use it thinking it was the real name of the
device, when it might not be.

greg k-h
Aniroop Mathur
2014-08-01 22:24:32 UTC
Permalink
Post by Greg KH
Post by Aniroop Mathur
Dear Mr. Greg Kroah-Hartman and Linux Community,
Greetings of the day !! :)
I am Aniroop Mathur working on Linux Kernel for last two years.
I am stuck at one point and could not find the solution over internet.
I posted on linuxquestions.org too.
So I need your help and suggestion for it.
=====================================================
/*
* for statically allocated devices, which should all be converted
* some day, we need to initialize the name. We prevent reading back
* the name, and force the use of dev_name()
*/
if (dev->init_name) {
dev_set_name(dev, "%s", dev->init_name);
dev->init_name = NULL;
}
Except forcing the use of dev_name to read device name,
Is there any other reason to make init_name as NULL ?
Why would you want init_name to not be NULL?
Currently in kernel, we cannot set name of event node.

If dev->init_name is not set as NULL in device_add(),
then I can easily set name of event node in evdev_dev.c
file as below:

if(dev->init_name) {
sprintf(dev->init_name, "event_%s", dev->init_name);
}
error = device_add(&evdev->dev);

And in some input device driver code, I will use like below:
dev->init_name = "accelerometer";
input_register_device(dev);

So, overall output will be
/dev/input/event<x> --> /dev/input/event_accelerometer
sys/class/input/input<x> --> sys/class/input/accelerometer

In short, input and event node names are set just by
adding one line, which i found quite efficient.
There is other way also to set name of event node but
it involves using extra variable and little more code,
So I am looking for best solution possible. :)
Post by Greg KH
Post by Aniroop Mathur
And if it is not made NULL, is there any problem or side-effect ?
Yes, people would start to use it thinking it was the real name of the
device, when it might not be.
As the name itself nicely suggests, it is just a initial name.
It may not be the final name as developer can change the name
using dev_set_name. Also dev_name api is available to get
final/current name of the device.

Thanks and Regards,
Aniroop Mathur
--
To unsubscribe from this list: send the line "unsubscribe linux-hotplug" in
the body of a message to ***@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Greg KH
2014-08-01 22:41:27 UTC
Permalink
Post by Aniroop Mathur
Post by Greg KH
Post by Aniroop Mathur
Dear Mr. Greg Kroah-Hartman and Linux Community,
Greetings of the day !! :)
I am Aniroop Mathur working on Linux Kernel for last two years.
I am stuck at one point and could not find the solution over internet.
I posted on linuxquestions.org too.
So I need your help and suggestion for it.
=====================================================
/*
* for statically allocated devices, which should all be converted
* some day, we need to initialize the name. We prevent reading back
* the name, and force the use of dev_name()
*/
if (dev->init_name) {
dev_set_name(dev, "%s", dev->init_name);
dev->init_name = NULL;
}
Except forcing the use of dev_name to read device name,
Is there any other reason to make init_name as NULL ?
Why would you want init_name to not be NULL?
Currently in kernel, we cannot set name of event node.
What do you mean by "event node"?
Post by Aniroop Mathur
If dev->init_name is not set as NULL in device_add(),
then I can easily set name of event node in evdev_dev.c
if(dev->init_name) {
sprintf(dev->init_name, "event_%s", dev->init_name);
}
error = device_add(&evdev->dev);
dev->init_name = "accelerometer";
input_register_device(dev);
What's wrong with:
dev_set_name(dev, "%s", "accelerometer");
input_register_device(dev);
Post by Aniroop Mathur
So, overall output will be
/dev/input/event<x> --> /dev/input/event_accelerometer
sys/class/input/input<x> --> sys/class/input/accelerometer
In short, input and event node names are set just by
adding one line, which i found quite efficient.
There is other way also to set name of event node but
it involves using extra variable and little more code,
So I am looking for best solution possible. :)
Only use init_name for static struct devices, for a dynamic one, jsut
set the name like everyone else does, how is that "more" code than
anything else?
Post by Aniroop Mathur
Post by Greg KH
Post by Aniroop Mathur
And if it is not made NULL, is there any problem or side-effect ?
Yes, people would start to use it thinking it was the real name of the
device, when it might not be.
As the name itself nicely suggests, it is just a initial name.
So please do not use it, someday it will go away...

thanks,

greg k-h
--
To unsubscribe from this list: send the line "unsubscribe linux-hotplug" in
the body of a message to ***@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Aniroop Mathur
2014-08-01 23:44:44 UTC
Permalink
Post by Greg KH
Post by Aniroop Mathur
Post by Greg KH
Post by Aniroop Mathur
Dear Mr. Greg Kroah-Hartman and Linux Community,
Greetings of the day !! :)
I am Aniroop Mathur working on Linux Kernel for last two years.
I am stuck at one point and could not find the solution over internet.
I posted on linuxquestions.org too.
So I need your help and suggestion for it.
=====================================================
/*
* for statically allocated devices, which should all be converted
* some day, we need to initialize the name. We prevent reading back
* the name, and force the use of dev_name()
*/
if (dev->init_name) {
dev_set_name(dev, "%s", dev->init_name);
dev->init_name = NULL;
}
Except forcing the use of dev_name to read device name,
Is there any other reason to make init_name as NULL ?
Why would you want init_name to not be NULL?
Currently in kernel, we cannot set name of event node.
What do you mean by "event node"?
I am referring to the device node which HAL/Application uses
as an interface to interact with kernel for read/write data operation.
like /dev/input/event0, /dev/input/event1, etc

int fd = open("/dev/input/event0", O_RDONLY);
int res = read(fd, &input_event, sizeof(input_event));
Post by Greg KH
Post by Aniroop Mathur
If dev->init_name is not set as NULL in device_add(),
then I can easily set name of event node in evdev_dev.c
if(dev->init_name) {
sprintf(dev->init_name, "event_%s", dev->init_name);
}
error = device_add(&evdev->dev);
dev->init_name = "accelerometer";
input_register_device(dev);
dev_set_name(dev, "%s", "accelerometer");
input_register_device(dev);
It is good way for setting name of "Input node".
But it will not set name of "Event node".

input_register_device call sets name of two nodes
1. Input node - sys/class/input/input<x>
2. Event node - /dev/input/event<x> or sys/class/input/event<x>

Here, input<x> and event<x> are default names set without using
dev_set_name in driver.
input<x> - kobject name of struct input_dev;
event<x> - kobject name of struct evdev;

In input_register_device(dev) function call,
dev refers to struct input_dev and not struct evdev.

struct input_dev is defined in input.h and
struct evdev is defined in evdev.c (not a header file).

So driver can use struct input_dev and set its name
using dev_set_name(dev, "%s", "accelerometer");
But struct evdev is not accessible to driver as it is defined in
evdev.c file. So driver cannot use dev_set_name for evdev.
It can only be used in evdev.c file.

So above method output will be
sys/class/input/accelerometer
/dev/input/event<x> and sys/class/input/event<x>
(event node name not set)
Post by Greg KH
Post by Aniroop Mathur
So, overall output will be
/dev/input/event<x> --> /dev/input/event_accelerometer
sys/class/input/input<x> --> sys/class/input/accelerometer
In short, input and event node names are set just by
adding one line, which i found quite efficient.
There is other way also to set name of event node but
it involves using extra variable and little more code,
So I am looking for best solution possible. :)
Only use init_name for static struct devices, for a dynamic one, jsut
set the name like everyone else does, how is that "more" code than
anything else?
Can you please elaborate what do you mean by static struct devices ?
Can I consider embedded accelerometer, proximity, gyro sensor,
touch panel in android mobile devices as static devices ?

Input subsystem is setting default name of input and event node.
This is the normal method everyone uses.

I do not want to use default name like event0, event4, input2, etc
My aim is to set name of input and event node through driver as desired.
So after discussion of other ways to set name through driver,
we can compare which way is better.
In one other method, there is a need to add extra variable in
struct input_dev and hence more memory size.
Post by Greg KH
Post by Aniroop Mathur
Post by Greg KH
Post by Aniroop Mathur
And if it is not made NULL, is there any problem or side-effect ?
Yes, people would start to use it thinking it was the real name of the
device, when it might not be.
As the name itself nicely suggests, it is just a initial name.
So please do not use it, someday it will go away...
thanks,
Thanks and Regards,
Aniroop Mathur
--
To unsubscribe from this list: send the line "unsubscribe linux-hotplug" in
the body of a message to ***@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Aniroop Mathur
2014-08-03 16:13:30 UTC
Permalink
Dear Mr. Greg KH and Linux Kernel Community,
Greetings of the day ! :)

I need to mark one open linux thread as solved as it is open for a
long time now.
Kindly help in answering my below two queries. :)
Post by Aniroop Mathur
Post by Greg KH
Post by Aniroop Mathur
Post by Greg KH
Post by Aniroop Mathur
Dear Mr. Greg Kroah-Hartman and Linux Community,
Greetings of the day !! :)
I am Aniroop Mathur working on Linux Kernel for last two years.
I am stuck at one point and could not find the solution over internet.
I posted on linuxquestions.org too.
So I need your help and suggestion for it.
=====================================================
/*
* for statically allocated devices, which should all be converted
* some day, we need to initialize the name. We prevent reading back
* the name, and force the use of dev_name()
*/
if (dev->init_name) {
dev_set_name(dev, "%s", dev->init_name);
dev->init_name = NULL;
}
Except forcing the use of dev_name to read device name,
Is there any other reason to make init_name as NULL ?
Why would you want init_name to not be NULL?
Currently in kernel, we cannot set name of event node.
What do you mean by "event node"?
I am referring to the device node which HAL/Application uses
as an interface to interact with kernel for read/write data operation.
like /dev/input/event0, /dev/input/event1, etc
int fd = open("/dev/input/event0", O_RDONLY);
int res = read(fd, &input_event, sizeof(input_event));
Post by Greg KH
Post by Aniroop Mathur
If dev->init_name is not set as NULL in device_add(),
then I can easily set name of event node in evdev_dev.c
if(dev->init_name) {
sprintf(dev->init_name, "event_%s", dev->init_name);
}
error = device_add(&evdev->dev);
dev->init_name = "accelerometer";
input_register_device(dev);
dev_set_name(dev, "%s", "accelerometer");
input_register_device(dev);
It is good way for setting name of "Input node".
But it will not set name of "Event node".
input_register_device call sets name of two nodes
1. Input node - sys/class/input/input<x>
2. Event node - /dev/input/event<x> or sys/class/input/event<x>
Here, input<x> and event<x> are default names set without using
dev_set_name in driver.
input<x> - kobject name of struct input_dev;
event<x> - kobject name of struct evdev;
In input_register_device(dev) function call,
dev refers to struct input_dev and not struct evdev.
struct input_dev is defined in input.h and
struct evdev is defined in evdev.c (not a header file).
So driver can use struct input_dev and set its name
using dev_set_name(dev, "%s", "accelerometer");
But struct evdev is not accessible to driver as it is defined in
evdev.c file. So driver cannot use dev_set_name for evdev.
It can only be used in evdev.c file.
So above method output will be
sys/class/input/accelerometer
/dev/input/event<x> and sys/class/input/event<x>
(event node name not set)
Post by Greg KH
Post by Aniroop Mathur
So, overall output will be
/dev/input/event<x> --> /dev/input/event_accelerometer
sys/class/input/input<x> --> sys/class/input/accelerometer
In short, input and event node names are set just by
adding one line, which i found quite efficient.
There is other way also to set name of event node but
it involves using extra variable and little more code,
So I am looking for best solution possible. :)
Only use init_name for static struct devices, for a dynamic one, jsut
set the name like everyone else does, how is that "more" code than
anything else?
Can you please elaborate what do you mean by static struct devices ?
Can I consider embedded accelerometer, proximity, gyro sensor,
touch panel in android mobile devices as static devices ?
1. What are statically allocated devices and dynamically allocated devices ?
Post by Aniroop Mathur
Input subsystem is setting default name of input and event node.
This is the normal method everyone uses.
I do not want to use default name like event0, event4, input2, etc
My aim is to set name of input and event node through driver as desired.
So after discussion of other ways to set name through driver,
we can compare which way is better.
In one other method, there is a need to add extra variable in
struct input_dev and hence more memory size.
Post by Greg KH
Post by Aniroop Mathur
Post by Greg KH
Post by Aniroop Mathur
And if it is not made NULL, is there any problem or side-effect ?
Yes, people would start to use it thinking it was the real name of the
device, when it might not be.
As the name itself nicely suggests, it is just a initial name.
So please do not use it, someday it will go away...
2. Why init_name field will be removed from struct device someday ?
Post by Aniroop Mathur
Post by Greg KH
thanks,
Thanks,
Aniroop
--
To unsubscribe from this list: send the line "unsubscribe linux-hotplug" in
the body of a message to ***@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Greg KH
2014-08-02 02:39:58 UTC
Permalink
Post by Aniroop Mathur
Post by Greg KH
Post by Aniroop Mathur
So, overall output will be
/dev/input/event<x> --> /dev/input/event_accelerometer
sys/class/input/input<x> --> sys/class/input/accelerometer
In short, input and event node names are set just by
adding one line, which i found quite efficient.
There is other way also to set name of event node but
it involves using extra variable and little more code,
So I am looking for best solution possible. :)
Only use init_name for static struct devices, for a dynamic one, jsut
set the name like everyone else does, how is that "more" code than
anything else?
Can you please elaborate what do you mean by static struct devices ?
Exactly what you wrote there, 'static' is a C thing, right?
Post by Aniroop Mathur
Can I consider embedded accelerometer, proximity, gyro sensor,
touch panel in android mobile devices as static devices ?
Nope, because in my Android phone, I can yank out those and add new ones
on the fly while the phone is running. The kernel is fully dynamic that
way.
Post by Aniroop Mathur
Input subsystem is setting default name of input and event node.
As it should.
Post by Aniroop Mathur
This is the normal method everyone uses.
And so should you :)
Post by Aniroop Mathur
I do not want to use default name like event0, event4, input2, etc
Yes, you really do.
Post by Aniroop Mathur
My aim is to set name of input and event node through driver as desired.
Nope, that's userspace's job, just use udev to rename the device node,
or even better yet, create a symlink like /dev/input/by-id/ has if you
really need more than that.

Don't mess with kernel device names, we can't do that without breaking
tons of things, there's a reason that we standardized on something,
please use it.

good luck,

greg k-h
--
To unsubscribe from this list: send the line "unsubscribe linux-hotplug" in
the body of a message to ***@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Aniroop Mathur
2014-08-03 18:54:06 UTC
Permalink
Post by Greg KH
Post by Aniroop Mathur
Post by Greg KH
Post by Aniroop Mathur
So, overall output will be
/dev/input/event<x> --> /dev/input/event_accelerometer
sys/class/input/input<x> --> sys/class/input/accelerometer
In short, input and event node names are set just by
adding one line, which i found quite efficient.
There is other way also to set name of event node but
it involves using extra variable and little more code,
So I am looking for best solution possible. :)
Only use init_name for static struct devices, for a dynamic one, jsut
set the name like everyone else does, how is that "more" code than
anything else?
Can you please elaborate what do you mean by static struct devices ?
Exactly what you wrote there, 'static' is a C thing, right?
Post by Aniroop Mathur
Can I consider embedded accelerometer, proximity, gyro sensor,
touch panel in android mobile devices as static devices ?
Nope, because in my Android phone, I can yank out those and add new ones
on the fly while the phone is running. The kernel is fully dynamic that
way.
Post by Aniroop Mathur
Input subsystem is setting default name of input and event node.
As it should.
Post by Aniroop Mathur
This is the normal method everyone uses.
And so should you :)
Post by Aniroop Mathur
I do not want to use default name like event0, event4, input2, etc
Yes, you really do.
Post by Aniroop Mathur
My aim is to set name of input and event node through driver as desired.
Nope, that's userspace's job, just use udev to rename the device node,
or even better yet, create a symlink like /dev/input/by-id/ has if you
really need more than that.
Don't mess with kernel device names, we can't do that without breaking
tons of things, there's a reason that we standardized on something,
please use it.
Okay, will follow the same. :)

One last thing,
Why init_name will go away some day ?

Is it because now we have dev_set_name,
so there is no need of it anymore.
Moreover as you said,
"people would start to use it thinking it was the real name of the
device, when it might not be."
So, there remains no purpose of init_name anymore, right ?
Post by Greg KH
good luck,
greg k-h
Thanks,
Aniroop
Greg KH
2014-08-04 04:35:41 UTC
Permalink
Post by Aniroop Mathur
Post by Greg KH
Post by Aniroop Mathur
Post by Greg KH
Post by Aniroop Mathur
So, overall output will be
/dev/input/event<x> --> /dev/input/event_accelerometer
sys/class/input/input<x> --> sys/class/input/accelerometer
In short, input and event node names are set just by
adding one line, which i found quite efficient.
There is other way also to set name of event node but
it involves using extra variable and little more code,
So I am looking for best solution possible. :)
Only use init_name for static struct devices, for a dynamic one, jsut
set the name like everyone else does, how is that "more" code than
anything else?
Can you please elaborate what do you mean by static struct devices ?
Exactly what you wrote there, 'static' is a C thing, right?
Post by Aniroop Mathur
Can I consider embedded accelerometer, proximity, gyro sensor,
touch panel in android mobile devices as static devices ?
Nope, because in my Android phone, I can yank out those and add new ones
on the fly while the phone is running. The kernel is fully dynamic that
way.
Post by Aniroop Mathur
Input subsystem is setting default name of input and event node.
As it should.
Post by Aniroop Mathur
This is the normal method everyone uses.
And so should you :)
Post by Aniroop Mathur
I do not want to use default name like event0, event4, input2, etc
Yes, you really do.
Post by Aniroop Mathur
My aim is to set name of input and event node through driver as desired.
Nope, that's userspace's job, just use udev to rename the device node,
or even better yet, create a symlink like /dev/input/by-id/ has if you
really need more than that.
Don't mess with kernel device names, we can't do that without breaking
tons of things, there's a reason that we standardized on something,
please use it.
Okay, will follow the same. :)
One last thing,
Why init_name will go away some day ?
Because you should never have a static struct device in the kernel.
It's a reference counted object that needs to always be dynamically
allocated in order to properly work. Unfortunatly, there are some users
who have it allocated statically (on the stack) instead of dynamically
(on the heap). I'll get rid of them someday, it's _way_ down my list of
things to do...

thanks,

greg k-h
--
To unsubscribe from this list: send the line "unsubscribe linux-hotplug" in
the body of a message to ***@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Aniroop Mathur
2014-08-04 16:59:07 UTC
Permalink
Post by Greg KH
Post by Aniroop Mathur
Post by Greg KH
Post by Aniroop Mathur
Post by Greg KH
Post by Aniroop Mathur
So, overall output will be
/dev/input/event<x> --> /dev/input/event_accelerometer
sys/class/input/input<x> --> sys/class/input/accelerometer
In short, input and event node names are set just by
adding one line, which i found quite efficient.
There is other way also to set name of event node but
it involves using extra variable and little more code,
So I am looking for best solution possible. :)
Only use init_name for static struct devices, for a dynamic one, jsut
set the name like everyone else does, how is that "more" code than
anything else?
Can you please elaborate what do you mean by static struct devices ?
Exactly what you wrote there, 'static' is a C thing, right?
Post by Aniroop Mathur
Can I consider embedded accelerometer, proximity, gyro sensor,
touch panel in android mobile devices as static devices ?
Nope, because in my Android phone, I can yank out those and add new ones
on the fly while the phone is running. The kernel is fully dynamic that
way.
Post by Aniroop Mathur
Input subsystem is setting default name of input and event node.
As it should.
Post by Aniroop Mathur
This is the normal method everyone uses.
And so should you :)
Post by Aniroop Mathur
I do not want to use default name like event0, event4, input2, etc
Yes, you really do.
Post by Aniroop Mathur
My aim is to set name of input and event node through driver as desired.
Nope, that's userspace's job, just use udev to rename the device node,
or even better yet, create a symlink like /dev/input/by-id/ has if you
really need more than that.
Don't mess with kernel device names, we can't do that without breaking
tons of things, there's a reason that we standardized on something,
please use it.
Okay, will follow the same. :)
One last thing,
Why init_name will go away some day ?
Because you should never have a static struct device in the kernel.
It's a reference counted object that needs to always be dynamically
allocated in order to properly work. Unfortunatly, there are some users
who have it allocated statically (on the stack) instead of dynamically
(on the heap). I'll get rid of them someday, it's _way_ down my list of
things to do...
thanks,
greg k-h
Thank you so much for your support and answering my queries. :)
Now, the thread has also been marked as solved.

Thanks,
Aniroop

Loading...