Search code examples
linuxdriversalsa

How to access the node created in user space from kernel?


I am doing an exercise in which I want to access the node /dev/snd/controlC0 of the sound card from the kernel to set up the tinymix control from the kernel space instead of user space using a tinymix utility. Now from the flow of the tinymix utility I have found that at the start its execution it will call a mixer_open() function from the user space which internally calls the snd_ctl_open() function of the kernel.Now as I have to do the same thing from kernel my module need to call the same function. The issue what I am facing is function snd_ctl_open() have arguments struct inode *inode, struct file *file. So how can I get the inode information of the node in my module?

The file mixer.c is located in the Android AOSP code at external/tinyalsa/mixer.c. snd_ctl_open() is located in the Linux kernel at sound/core/control.c.

I found one in build kernel API filp_open() which can do the task above mentioned but it can be used after the file system mount only. I want to open a node before the file system mounts.

In short, my goal is to understand how I can access or open a device node from the kernel before the file system mounts.


Solution

  • Thanks for the reply scott.

    I am able to access the device node /dev/controlC0 as well as able to set the mixer control settings from kernel module. I have followed the below approch.

    1. There is one global structure "static struct snd_minor *snd_minors[SNDRV_OS_MINORS]" which is defined in sound/core/sound.c. This structure has the information with respect to minor number of all the sound devices.
    2. I have found a method to find the device node and file pointer structure from the device minor number. So what I have done is I call a function find_control_device() from my driver which in turn iterate for all the devices and check for the SNDRV_DEVICE_TYPE_CONTROL(Sound Control Device) device type. Function find_control_device() returns the minor number of the control device.
    3. From the device minor no I am able to get the device node and file structure information.
    4. Once I got the above information I call the snd_ctl_open() which will open a device node for the operation.
    5. As User space application interact with the kernel space using the IOCTL. To set the mixer setting tinymix calls the snd_ctl_ioctl() function. So I have made one wrapper function which will call internally call snd_ctl_ioctl() and set the mixer control value.

    I have done all the above step in the module_init function section in the driver.So as soon as driver gets register in the kernel tree the required tinymix controls are getting set as per the requirement.

    I have referred a code of tinymix.c from AOSP code tree to manipulate the arguments of the IOCTL.

    Thanks,

    Hemant