ALSA的ioctl - hwdep

hwdep模块简述


字符设备驱动中,ioctl是一个很常见的IO设备操作函数,可以自定义cmd命令字并实现对应的设备IO控制。

音频设备的控制有所不同:驱动层大部分控制操作定义各种snd_kcontrol_new,然后注册到SNDRV_DEV_CONTROL模块中(sound\core\control.c),详见snd_kcontrol探究;而上层调用alsa-lib的snd_ctl_open/snd_mixer_open来打开底层的SNDRV_DEV_CONTROL模块,详见DAPM之二:audio paths与dapm kcontrol。这方法常见于mixer-control,如音量调整、部件开关、通路连接等等。

除此之外,alsa还是可以实现类似于ioctl的函数的,只不过它封装成一个设备模块SNDRV_DEV_HWDEP,代码sound\core\ hwdep.c。该模块实现了read/write/ioctl/llseek/poll/mmap等接口。hwdep是Hardware Dependant Interface的简称。


题外话:如果想看自己板上的alsa有什么类型的设备可以cat /proc/asound/devices,如

  1. ~ # cat /proc/asound/devices   
  2.   0: [ 0]   : control  
  3.   4: [ 0- 0]: hardware dependent  
  4.  16: [ 0- 0]: digital audio playback  
  5.  24: [ 0- 0]: digital audio capture  
  6.  33:        : timer  
设备节点号minor=0是control,=4是hwdep,=16是pcm- playback,=24是pcm-capture,=33是timer。


如下简单分析ioctl:

  1. // 套接字接口函数集  
  2. static const struct file_operations snd_hwdep_f_ops =  
  3. {  
  4.     .owner =    THIS_MODULE,  
  5.     .llseek =   snd_hwdep_llseek,  
  6.     .read =     snd_hwdep_read,  
  7.     .write =    snd_hwdep_write,  
  8.     .open =     snd_hwdep_open,  
  9.     .release =  snd_hwdep_release,  
  10.     .poll =     snd_hwdep_poll,  
  11.     .unlocked_ioctl =   snd_hwdep_ioctl,  
  12.     .compat_ioctl = snd_hwdep_ioctl_compat,  
  13.     .mmap =     snd_hwdep_mmap,  
  14. };  
  15.   
  16. static long snd_hwdep_ioctl(struct file * file, unsigned int cmd,  
  17.                 unsigned long arg)  
  18. {  
  19.     struct snd_hwdep *hw = file->private_data;  
  20.     void __user *argp = (void __user *)arg;  
  21.     switch (cmd) {  
  22.     case SNDRV_HWDEP_IOCTL_PVERSION:  
  23.         return put_user(SNDRV_HWDEP_VERSION, (int __user *)argp);  
  24.     case SNDRV_HWDEP_IOCTL_INFO:  
  25.         return snd_hwdep_info(hw, argp);  
  26.     case SNDRV_HWDEP_IOCTL_DSP_STATUS:  
  27.         return snd_hwdep_dsp_status(hw, argp);  
  28.     case SNDRV_HWDEP_IOCTL_DSP_LOAD:  
  29.         return snd_hwdep_dsp_load(hw, argp);  
  30.     }  
  31.     if (hw->ops.ioctl)  
  32.         return hw->ops.ioctl(hw, file, cmd, arg);  
  33.     return -ENOTTY;  
  34. }  
从snd_hwdep_ioctl可以看出,系统默认只有4个cmd,功能主要是download dsp image。从return hw->ops.ioctl(hw, file, cmd, arg)语句可以看出,我们可自定义cmd和ioctl函数。


实现自定义的hwdep操作函数


1、 首先实现需要的操作函数:

  1. static int my_hwdep_open(struct snd_hwdep * hw, struct file *file)  
  2. {  
  3.     printk(KERN_INFO "my_hwdep_open\n");  
  4.     return 0;  
  5. }  
  6.   
  7. static int my_hwdep_ioctl(struct snd_hwdep * hw, struct file *file, unsigned int cmd, unsigned long arg)  
  8. {  
  9. #define MY_SOC_IOCTL_SET_CALL_PATH   _IOWR('H', 0x10, int)  
  10.   
  11.     switch (cmd) {  
  12.     case MY_SOC_IOCTL_SET_CALL_PATH:  
  13.         //设置电话语音通路  
  14.         return 0;  
  15.         break;  
  16.     //......  
  17.     }  
  18.       
  19.     err("Not supported ioctl for MY-HWDEP");  
  20.     return -ENOIOCTLCMD;  
  21. }  

2、 注册操作函数到hwdep模块:
  1. struct snd_hwdep *hwdep;  
  2.   
  3. if (snd_hwdep_new(codec->card, "MY-HWDEP", 0, &hwdep) < 0) {  
  4.     printk(KERN_ERR "create MY-HWDEP fail");  
  5.     return;  
  6. }  
  7.   
  8. sprintf(hwdep->name, "MY-HWDEP %d", 0);  
  9.   
  10. hwdep->iface = SNDRV_HWDEP_IFACE_WMT;  
  11. hwdep->ops.open = wmt_hwdep_open;  
  12. hwdep->ops.ioctl = wmt_hwdep_ioctl;  
这里摘录snd_hwdep_new的代码注释,让大家更明白上面的注册过程:
  1. /** 
  2.  * snd_hwdep_new - create a new hwdep instance 
  3.  * @card: the card instance 
  4.  * @id: the id string 
  5.  * @device: the device index (zero-based) 
  6.  * @rhwdep: the pointer to store the new hwdep instance 
  7.  * 
  8.  * Creates a new hwdep instance with the given index on the card. 
  9.  * The callbacks (hwdep->ops) must be set on the returned instance 
  10.  * after this call manually by the caller. 
  11.  * 
  12.  * Returns zero if successful, or a negative error code on failure. 
  13.  */  
按照以上实现hwdep ioctl后,上层可以通过alsa-lib的相关接口来调用。


上层调用范例


  1. #include <fcntl.h>  
  2. #include <sys/ioctl.h>  
  3. #include <alsa/hwdep.h>  
  4. #include <alsa/error.h>  
  5. #include <stdio.h>  
  6.   
  7. #define MY_SOC_IOCTL_SET_CALL_PATH   _IOWR('H', 0x10, int)  
  8.   
  9. int main()  
  10. {  
  11.     const char *devicename = "hw:0,0";  
  12.     snd_hwdep_t *hwdep;  
  13.     int err;  
  14.     int enable = 1;  
  15.       
  16.     if ((err = snd_hwdep_open(&hwdep, devicename, O_RDWR)) < 0) {  
  17.         printf("hwdep interface open error: %s \n", snd_strerror(err));  
  18.         return -1;  
  19.     }  
  20.       
  21.     if ((err = snd_hwdep_ioctl(hwdep, MY_SOC_IOCTL_SET_CALL_PATH, &enable)) < 0) {  
  22.         printf("hwdep ioctl error: %s \n", snd_strerror(err));  
  23.     }  
  24.       
  25.     snd_hwdep_close(hwdep);  
  26.       
  27.     return 0;  
  28. }  

总结


可以看出hwdep的本意主要是用于download dsp image,但通过它也可实现类似于其他字符设备的ioctl。我说过音频大多控制是通过snd_kcontrol,但有些功能如果使用这种方式会比较繁琐且模块太过耦合。

举个例子:电话语音通路,它不同于音乐回放通路,通话时才需要打开。如果用snd_kcontrol,则上层需要调用多个control.set,并且更换CODEC芯片的话,上层也要跟着修改control name;如果使用hwdep ioctl的话,就没有这个问题,只需要保证命令字cmd一致,底层如何管理通话通路的一系列部件开关,上层都不需要关心。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值