dev_err() function definition - linux

I can see that dev_*() family of functions such as dev_err() are given as prototype in include/linux/device.h, but no where I could find its definition. I have visited sites like lxr.free-electrons, but without success. Used tags in the source code of linux kernel, even then failed.
What I am trying to find is how the dev_err(const struct device *dev, const char *fmt, ...) is able to get the device information such as pci bus, etc from just giving const struct device *dev as argument to print in logs.

Description of the device is constructed in function create_syslog_header, defined in drivers/base/core.c. The function just extracts some fields from struct device object, and emits them via snprintf() into the string.
The function dev_err is implemented via define_dev_printk_level macro in the same file (drivers/base/core.c).

Related

'ioctl' signature for device mapper

The question may seem naive, but I'm new to kernel/driver programming. I created a device mapper over a block device, which is working fine. It's constructor/destructor and map methods are called.
Now, I'm trying to write an ioctl for this mapper. When ioctl is written for a device, it has the following signature:
int ioctl(int d, /* other args */);
A file structure/descriptor is expected in ioctl. This can be easily used by application process as it has access to file.
But the ioctl for device mapper has the following signature ( in struct target_type):
typedef int (*dm_ioctl_fn) (struct dm_target *ti, unsigned int cmd,
unsigned long arg);
How can user application get access to device mapper with ioctl without having knowledge of struct dm_target ?
-Ioctl which stand for Input Output control is a system call used in linux to implement system calls which are not be available in the
kernel by default.
-The major use of this is in case of handling some specific operations of a device for which the kernel does not have a system call by default. For eg: Ejecting the media from a "CD" drive. An ioctl command is implemented to give the eject system call to the cd drive.
-ioctl(fd, cmd , INPARAM or OUTPARAM);
|
3rd argument is INPARAM or OUTPARAM i.e we don't have to read a device, then how how to interact with device ? use ioctl.
-open ioctl.h and check you will get more information
#define "ioctl name" __IOX("magic number","command number","argument type")
static long char_dev_ioctl( struct file *filp, unsigned int cmd, unsigned long arg)
{
/* verify argument using access_ok() */
/* impliment support of ioctl commands */
}

register_netdevice_notifier callback does not provide valid net_device

I work on Ubuntu kernel-mode netfilter module and need information about all network interfaces and their properties in module code.
Inside of init_module() I use register_netdevice_notifier() for that purpose. When callback function is called I can see correct event codes like up/down and other, but it seems that third parameter void* casted to net_device* provides object with invalid properties. ->name is empty string, ->if index is some nonsense number etc.
I tried debug version of module on kernel 3.19 and rebuild also on 4.2. Result is the same, I cannot read properties of net_device relating to event.
What can be problem ?
From what I can see from LXR, you need to call netdev_notifier_info_to_dev on the last parameter to get your net_device * (see here)

How do you link a device to a custom sysfs class?

I'm writing drivers for several pieces of custom hardware. All of the devices are attached via PCIe to a host computer. For convenience I would like to group all of these custom devices together into a sysfs class (which I believe is an acceptable thing to do?). Unfortunately the information in LDD3 is way out of date and I'm having trouble finding current documentation that discusses what I'm attempting to do.
Creating my custom class is easy enough:
struct class MY_CLASS = class_create(THIS_MODULE, "myclass")
And inside of my probe calls I've got access to the struct dev:
static int probe(struct pci_dev *pcidev, const struct pci_device_id *id)
{
...
struct dev *my_dev = &pcidev->dev;
...
}
My question is this: now that I've got the class and the dev, how do I create a link between the two?
The device_create() basically does what I want, but since I've already got a struct dev my understanding is that I shouldn't call device_create (i.e. create a new device) again.
I've done a little more tracing and found that device_add() which is called by device_create(), calls device_add_class_symlinks() (not exported unfortunately) which does something like this:
...
sysfs_create_link(&dev->class->p->subsys.kobj,&dev->kobj, dev_name(dev));
...
I tried something like this directly in my drivers to create the links I want but I can't get it to compile because struct subsys_private (the "p" member in the class struct) is not exposed anywhere?
Any help is greatly appreciated!
Are your drivers sitting on the specific bus? If no, what purpose of the specific class?
Anyway, for starter
struct class devclass = {…}
probe()
{
struct device *dev = …
dev->class = &devclass;
}
init()
{
class_register(&devclass);
}
I've encountered the same issue.
if i called device_register with my class pointer assigned to the device class member. it will create a subsystem in my device directory, which is what device_add_class_symlinks do. but in my device directory there is already subsystem directory which is the link to the bus my device is attached to.
don't know if you got a method

Linux device driver for multiple busses

I am trying to get I2C bus driver working for my embedded product.
The driver is currently working fine for bus 0 but it is not working for bus 1.
I see that there is a file named i2c-product.c (I have replaced the name of the chip i2c controller with 'product') and I believe that is the driver for the controller.
It has a function product_i2c_probe(struct platform_device *pd). I see that this is a callback assigned in the platform_driver structure to .probe member. I guess kernel calls into it to setup the driver. Makes sense.
Inside the product_i2c_probe(struct platform_device *pd) function there are following lines to setup the adapter:
drv_data->adapter.dev.parent = &pd->dev;
drv_data->adapter.algo = &product_i2c_algo;
drv_data->adapter.owner = THIS_MODULE;
drv_data->adapter.class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
drv_data->adapter.timeout = msecs_to_jiffies(pdata->timeout);
drv_data->adapter.nr = pd->id;
From exploring the code in i2c-core.c and i2c-dev.c, it appears like nr member of the adapter needs to contain the bus id, because that is what gets the minor from the device node. Note I have also created the node using mknod command for both buses. So I have a device /dev/i2c-0 and /dev/i2c-1.
Later in the product_i2c_probe(struct platform_device *pd) function, it adds the adapter to the device using i2c_add_numbered_adapter(&drv_data->adapter).
At the end of the file, I see it registers the device:
static int __init
product_i2c_init(void)
{
return platform_driver_register(&product_i2c_driver);
}
And finally
module_init(product_i2c_init);
So my question is, who calls into the probe() function and passes the platform_device *pd. Clearly this object has the pd->id value that becomes the bus id for the adapter in the line:
drv_data->adapter.nr = pd->id;
Is there a configuration file somewhere? I searched all over but could not find it including the menuconfig.
Any help will be appreciated. As you can probably guess, I am a newbie to device drivers. Am I on the right track, or totally off?
Thanks.

get pointer for existing device class (struct class) in Linux kernel module

get pointer for existing device class (struct class) in Linux kernel module
Hi all!
I am trying to register a device in an existing device class, but I am having trouble getting the pointer to an existing class. Kernel version is Linux 3.3.6.
I can create a class in a module, get the pointer to it and then use it to register the device with:
*cl = class_create(THIS_MODULE, className);
dev_ret = device_create(*cl, NULL, *dev, NULL, driverName);
However I need to register another device in the same class with another module, but I couldn't find a way to get the pointer to an existing class. And I can not create the class again in the other module, because since class already exists class_create returns NULL and not the pointer to the class required by device_create.
I found in:
http://lwn.net/Articles/102500/
A function that returns a pointer to a class by its name:
struct class * class_find(char * name)
However when I try to compile the function compiler says it does not exist.
I thought this function was exported by the kernel (my module have license GPL) but it appears it is not.
Maybe I need to include some header?
I tried to rewrite this function since, its code is list in the above link. But when I try to iterate over class_subsys with:
list_for_each_entry(this_class, &class_subsys.kset.list, subsys.kset.kobj.entry)
now symbol class_subsys is not found. Again I thought it is exported to the kernel.
I am not sure what is missing. Some header?
Am I doing it the wrong way?
There is another function to do it?
I suppose if I could traverse sysfs from start I could get a pointer to an existing class.
But I also did not find how to start traversing sysfs.
All functions I have seen requires a pointer to kobject or kset to start traversing. But I have no pointer even to the root of sysfs or kernel objects, so I can not start traversing the tree to get a class pointer.
Can anyone point me in the right direction please?
I think input core is done this way, here is the snippet
Take a look at: https://github.com/torvalds/linux/blob/master/drivers/input/input.c#L1720
Best regards!

Resources