sysfs attribute example
Hello all,
Can someone point me to an example of exporting a sysfs attribute from a driver? (Or tell me if I shouldn't.) I'm trying to get a value from my USB device driver's probe() into an attribute so my udev .rules file can pick it up.
I've tried to follow what's in LDD 3rd ed. (page 374) "...simply fill in an attribute and pass it to sysfs_create_file()"
From my probe() function:
mydev->my_attribute.name = "my_attribute_name"; mydev->my_attribute.owner = THIS_MODULE; mydev->my_attribute.mode = S_IRUGO; sysfs_create_file(&(interface->class_dev->kobj), &(mydev->my_attribute));
When a device is probed, the file "my_attribute_name" shows up in sysfs, but I get a "kernel NULL pointer dereference at virtual address 00000004" (address changes with the USB port I plug into).
I'm also not seeing how to attach a value to the attribute. So, clearly, I'm missing something crucial here :-)
Thanks,
Stephen Morgan
-------------------------------------------------------
This SF.Net email is sponsored by: NEC IT Guy Games. How far can you shotput a projector? How fast can you ride your desk chair down the office luge track? If you want to score the big prize, get to know the little guy. Play to win an NEC 61" plasma display: http://www.necitguy.com/?r
_______________________________________________
Linux-hotplug-devel mailing list http://linux-hotplug.sourceforge.net
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel
Quoted 18 lines · Stephen Morgan
Hello all,
Can someone point me to an example of exporting a sysfs attribute from a driver? (Or tell me if I shouldn't.) I'm trying to get a value from my USB device driver's
probe()into an attribute so my udev .rules file can pick it up.I've tried to follow what's in LDD 3rd ed. (page 374) "...simply fill in an attribute and pass it to
sysfs_create_file()"From my
probe()function:mydev->my_attribute.name = "my_attribute_name"; mydev->my_attribute.owner = THIS_MODULE; mydev->my_attribute.mode = S_IRUGO; sysfs_create_file(&(interface->class_dev->kobj), &(mydev->my_attribute));
Quoted 6 lines
When a device is probed, the file "
my_attribute_name" shows up in sysfs, but I get a "kernel NULL pointer dereference at virtual address 00000004" (address changes with the USB port I plug into).I'm also not seeing how to attach a value to the attribute. So, clearly, I'm missing something crucial here :-)
Sure, you do. :)
Look for: DEVICE_ATTR() in the kernel code.
Good luck,
Kay
-------------------------------------------------------
This SF.Net email is sponsored by: NEC IT Guy Games. How far can you shotput a projector? How fast can you ride your desk chair down the office luge track? If you want to score the big prize, get to know the little guy. Play to win an NEC 61" plasma display: http://www.necitguy.com/?r
_______________________________________________
Linux-hotplug-devel mailing list http://linux-hotplug.sourceforge.net
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel
Quoted 38 lines
-----Original Message-----
From: Kay Sievers [mailto:[email protected]]
Sent: Friday, June 10, 2005 11:10 AM
To: Stephen Morgan
Cc: linux-hotplug-devel
Subject: Re: sysfs attribute exampleQuoted 4 lines · Stephen Morgan
Hello all,
Can someone point me to an example of exporting a sysfs attribute from a driver? (Or tell me if I shouldn't.) I'm trying to get a
value from my USB
Quoted 1 line
device driver's
probe()into an attribute so my udev .rulesfile can pick it
Quoted 3 lines
up.
I've tried to follow what's in LDD 3rd ed. (page 374)
"...simply fill in an
Quoted 9 lines
attribute and pass it to
sysfs_create_file()"From my
probe()function:mydev->my_attribute.name = "my_attribute_name"; mydev->my_attribute.owner = THIS_MODULE; mydev->my_attribute.mode = S_IRUGO;sysfs_create_file(&(interface->class_dev->kobj),
&(mydev->my_attribute));
Quoted 2 lines
When a device is probed, the file "
my_attribute_name" shows upin sysfs, but
Quoted 4 lines
I get a "kernel NULL pointer dereference at virtual address 00000004" (address changes with the USB port I plug into).
I'm also not seeing how to attach a value to the attribute.
So, clearly,
Quoted 9 lines
Quoted 1 line
I'm missing something crucial here :-)
Sure, you do. :)
Look for:
DEVICE_ATTR()in the kernel code.Good luck,
Kay
Hi Kay,
Thanks for your help.
This has gotten me much closer. However, the show() function seems to be receiving a pointer to the usb_interface's class_device instead of its device. Any ideas why?
Thanks,
Stephen
-------------------------------------------------------
This SF.Net email is sponsored by: NEC IT Guy Games. How far can you shotput a projector? How fast can you ride your desk chair down the office luge track? If you want to score the big prize, get to know the little guy. Play to win an NEC 61" plasma display: http://www.necitguy.com/?r
_______________________________________________
Linux-hotplug-devel mailing list http://linux-hotplug.sourceforge.net
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel
Quoted 48 lines · Stephen Morgan
Quoted 38 lines
-----Original Message-----
From: Kay Sievers [mailto:[email protected]]
Sent: Friday, June 10, 2005 11:10 AM
To: Stephen Morgan
Cc: linux-hotplug-devel
Subject: Re: sysfs attribute exampleQuoted 4 lines · Stephen Morgan
Hello all,
Can someone point me to an example of exporting a sysfs attribute from a driver? (Or tell me if I shouldn't.) I'm trying to get a
value from my USB
Quoted 1 line
device driver's
probe()into an attribute so my udev .rulesfile can pick it
Quoted 3 lines
up.
I've tried to follow what's in LDD 3rd ed. (page 374)
"...simply fill in an
Quoted 9 lines
attribute and pass it to
sysfs_create_file()"From my
probe()function:mydev->my_attribute.name = "my_attribute_name"; mydev->my_attribute.owner = THIS_MODULE; mydev->my_attribute.mode = S_IRUGO;sysfs_create_file(&(interface->class_dev->kobj),
&(mydev->my_attribute));
Quoted 2 lines
When a device is probed, the file "
my_attribute_name" shows upin sysfs, but
Quoted 4 lines
I get a "kernel NULL pointer dereference at virtual address 00000004" (address changes with the USB port I plug into).
I'm also not seeing how to attach a value to the attribute.
So, clearly,
Quoted 9 lines
Quoted 1 line
I'm missing something crucial here :-)
Sure, you do. :)
Look for:
DEVICE_ATTR()in the kernel code.Good luck,
Kay
Quoted 7 lines
Hi Kay,
Thanks for your help.
This has gotten me much closer. However, the
show()function seems to be receiving a pointer to the usb_interface'sclass_deviceinstead of its device. Any ideas why?
Because that's where you registered the attribute? :)
Care to show us your patch?
thanks,
greg k-h
-------------------------------------------------------
This SF.Net email is sponsored by: NEC IT Guy Games. How far can you shotput a projector? How fast can you ride your desk chair down the office luge track? If you want to score the big prize, get to know the little guy. Play to win an NEC 61" plasma display: http://www.necitguy.com/?r
_______________________________________________
Linux-hotplug-devel mailing list http://linux-hotplug.sourceforge.net
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel
Quoted 61 lines
-----Original Message-----
From: Greg KH [mailto:[email protected]]
Sent: Monday, June 13, 2005 6:54 PM
To: Stephen Morgan
Cc: Kay Sievers; linux-hotplug-devel
Subject: Re: sysfs attribute exampleQuoted 8 lines · Stephen Morgan
Quoted 8 lines
-----Original Message-----
From: Kay Sievers [mailto:[email protected]]
Sent: Friday, June 10, 2005 11:10 AM
To: Stephen Morgan
Cc: linux-hotplug-devel
Subject: Re: sysfs attribute exampleQuoted 4 lines
Quoted 4 lines · Stephen Morgan
Quoted 3 lines
Hello all,
Can someone point me to an example of exporting a sysfs
attribute from a
Quoted 16 lines
Quoted 16 lines
Quoted 1 line
driver? (Or tell me if I shouldn't.) I'm trying to get a
value from my USB
Quoted 1 line
device driver's
probe()into an attribute so my udev .rulesfile can pick it
Quoted 3 lines
up.
I've tried to follow what's in LDD 3rd ed. (page 374)
"...simply fill in an
Quoted 8 lines
attribute and pass it to
sysfs_create_file()"From my
probe()function:mydev->my_attribute.name = "my_attribute_name"; mydev->my_attribute.owner = THIS_MODULE; mydev->my_attribute.mode = S_IRUGO;Quoted 6 lines
Quoted 6 lines
Quoted 1 line
sysfs_create_file(&(interface->class_dev->kobj),
&(mydev->my_attribute));
Quoted 2 lines
When a device is probed, the file "
my_attribute_name" shows upin sysfs, but
Quoted 1 line
I get a "kernel NULL pointer dereference at virtual address
00000004"
Quoted 16 lines
Quoted 4 lines
Quoted 3 lines
(address changes with the USB port I plug into).
I'm also not seeing how to attach a value to the attribute.
So, clearly,
Quoted 9 lines
Quoted 1 line
I'm missing something crucial here :-)
Sure, you do. :)
Look for:
DEVICE_ATTR()in the kernel code.Good luck,
KayHi Kay,
Quoted 8 lines
Quoted 3 lines
Thanks for your help.
This has gotten me much closer. However, the
show()functionseems to be
Quoted 2 lines
receiving a pointer to the usb_interface's
class_deviceinstead of its device. Any ideas why?Because that's where you registered the attribute? :)
Okay, but if you register with the device (as opposed to the class_device), you don't get the SYSFS attribute created.
Quoted 2 lines
Care to show us your patch?
Here are the pertinent lines (the interim solution used here is to cast the device pointer to a class_device pointer):
**********************************************
static ssize_t mydev_number_show(struct device *dev, char *buf)
{
int retval = 0;
struct my_dev *mydev = NULL;
int index;
struct class_device * cdev = (struct class_device *) dev;
printk(" dev pointer: %p\n", cdev->dev);
printk(" kobject name: %s\n", cdev->dev->kobj.k_name);
printk(" bus_id: %s\n", cdev->dev->bus_id);
mydev = cdev->dev->driver_data;
index = mydev->index;
printk(" my device index: %d\n", index);
return sprintf(buf, "%d\n",index);}
static DEVICE_ATTR(mydev_number, S_IRUGO, mydev_number_show, NULL);
And in my probe() function:
usb_register_dev(interface, &my_class); usb_set_intfdata(interface, mydev); sysfs_create_file(&(interface->class_dev->kobj),
&(dev_attr_mydev_number.attr));
************************************************
Thanks,
Stephen
-------------------------------------------------------
SF.Net email is sponsored by: Discover Easy Linux Migration Strategies from IBM. Find simple to follow Roadmaps, straightforward articles, informative Webcasts and more! Get everything you need to get up to speed, fast. http://ads.osdn.com/?ad_idt77&alloc_id492&op=click
_______________________________________________
Linux-hotplug-devel mailing list http://linux-hotplug.sourceforge.net
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel
Quoted 16 lines · Stephen Morgan
Quoted 16 lines · Stephen Morgan
Quoted 4 lines
Quoted 4 lines · Stephen Morgan
Quoted 3 lines
Hello all,
Can someone point me to an example of exporting a sysfs
attribute from a
Quoted 10 lines
Quoted 10 lines
Quoted 1 line
driver? (Or tell me if I shouldn't.) I'm trying to get a
value from my USB
Quoted 1 line
device driver's
probe()into an attribute so my udev .rulesfile can pick it
Quoted 3 lines
up.
I've tried to follow what's in LDD 3rd ed. (page 374)
"...simply fill in an
Quoted 2 lines
attribute and pass it to
sysfs_create_file()"
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 7 lines
From my
probe()function:mydev->my_attribute.name = "my_attribute_name"; mydev->my_attribute.owner = THIS_MODULE; mydev->my_attribute.mode = S_IRUGO;sysfs_create_file(&(interface->class_dev->kobj),
&(mydev->my_attribute));
Z> > > > >
Quoted 23 lines
Quoted 23 lines
Quoted 3 lines
Quoted 3 lines
Quoted 1 line
When a device is probed, the file "
my_attribute_name" shows upin sysfs, but
Quoted 1 line
I get a "kernel NULL pointer dereference at virtual address
00000004"
Quoted 19 lines
Quoted 4 lines
Quoted 3 lines
(address changes with the USB port I plug into).
I'm also not seeing how to attach a value to the attribute.
So, clearly,
Quoted 9 lines
Quoted 1 line
I'm missing something crucial here :-)
Sure, you do. :)
Look for:
DEVICE_ATTR()in the kernel code.Good luck,
KayHi Kay,
Thanks for your help.
This has gotten me much closer. However, the
show()function
Quoted 8 lines
Quoted 5 lines
seems to be
Quoted 2 lines
receiving a pointer to the usb_interface's
class_deviceinstead of its device. Any ideas why?Because that's where you registered the attribute? :)
Okay, but if you register with the device (as opposed to the
class_device), you don't get the SYSFS attribute created.
Not true, are you sure you are looking in the proper place in sysfs?
Quoted 5 lines
Quoted 2 lines
Care to show us your patch?
Here are the pertinent lines (the interim solution used here is to cast the device pointer to a
class_devicepointer):
ick ick ick, don't do that, it's wrong.
Quoted 19 lines
**********************************************
staticssize_tmydev_number_show(struct device *dev, char *buf){ int retval = 0; struct my_dev *mydev = NULL; int index; struct class_device * cdev = (struct class_device *) dev; printk(" dev pointer: %p\n", cdev->dev); printk(" kobject name: %s\n", cdev->dev->kobj.k_name); printk(" bus_id: %s\n", cdev->dev->bus_id); mydev = cdev->dev->driver_data; index = mydev->index; printk(" my device index: %d\n", index); return sprintf(buf, "%d\n",index);}
Quoted 8 lines
static DEVICE_ATTR(mydev_number, S_IRUGO, mydev_number_show, NULL);And in my
probe()function:
usb_register_dev(interface, &my_class);usb_set_intfdata(interface, mydev); sysfs_create_file(&(interface->class_dev->kobj),
&(dev_attr_mydev_number.attr));
Do:
device_create_file(&interface->dev, &dev_attr_mydev_number.attr);
instead.
Or if you want it to show up in the class device directory, use CLASS_DEVICE_ATTR() and call class_device_create_file() instead. No casting is needed in your show function, that's a big sign that something is wrong.
I'd really suggest you read the driver model chapter in the Linux Device Drivers, Third edition book. It's free online if you don't want to buy it.
thanks,
greg k-h
-------------------------------------------------------
SF.Net email is sponsored by: Discover Easy Linux Migration Strategies from IBM. Find simple to follow Roadmaps, straightforward articles, informative Webcasts and more! Get everything you need to get up to speed, fast. http://ads.osdn.com/?ad_idt77&alloc_id492&op=click
_______________________________________________
Linux-hotplug-devel mailing list http://linux-hotplug.sourceforge.net
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel
Quoted 6 lines
Quoted 1 line
Okay, but if you register with the device (as opposed to the
class_device),Quoted 1 line
you don't get the SYSFS attribute created.
Not true, are you sure you are looking in the proper place in sysfs?
<doh> Thank you, Greg. I'll spare you the details; suffice it to say it's much better to <finish> coding before you <begin> testing. Sorry for the waste of neurons and keystrokes and bandwidth.
Stephen
-------------------------------------------------------
SF.Net email is sponsored by: Discover Easy Linux Migration Strategies from IBM. Find simple to follow Roadmaps, straightforward articles, informative Webcasts and more! Get everything you need to get up to speed, fast. http://ads.osdn.com/?ad_idt77&alloc_id492&op=click
_______________________________________________
Linux-hotplug-devel mailing list http://linux-hotplug.sourceforge.net
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel