加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

在Linux中注册声卡驱动程序

发布时间:2020-12-14 01:41:59 所属栏目:Linux 来源:网络整理
导读:我想写一个虚拟声卡驱动程序,linux系统将使用它来进行音频播放和捕获.驱动程序应使用缓冲区进行音频数据读/写.我写了以下基本驱动程序: #include linux/module.h#include linux/kernel.h#include linux/sound.h#include linux/sysctl.h#include linux/devic
我想写一个虚拟声卡驱动程序,linux系统将使用它来进行音频播放和捕获.驱动程序应使用缓冲区进行音频数据读/写.我写了以下基本驱动程序:

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/sound.h>

#include <linux/sysctl.h>
#include <linux/device.h>

#include <linux/slab.h> /* kmalloc() */
#include <linux/gfp.h>
#include <asm/uaccess.h> /* copy_from/to_user */
#include <linux/major.h>
#include <linux/fs.h>
#include <linux/pci.h>
#include <linux/err.h>

#include <sound/core.h>

static char* mod_name = "prosip";

MODULE_LICENSE("GPL");
MODULE_VERSION("0.0.1111");
MODULE_AUTHOR("DD-DDD");
MODULE_DESCRIPTION("proSip Virtual Sound Card");

//
static int ver_major = 133;

static int ver_minor = 3;

//
static int buffer_size = 0;
static char* buffer;
static int read_count = 0;

/* Declaration of memory.c functions */
int prosip_open(struct inode *inode,struct file *filp);
int prosip_release(struct inode *inode,struct file *filp);

//
ssize_t prosip_read(struct file *filp,char *buf,size_t count,loff_t *f_pos);
ssize_t prosip_write(struct file *filp,const char *buf,loff_t *f_pos);

//
int prosip_ioctl(struct inode *inode,struct file *file,unsigned int ioctl_num,unsigned long ioctl_param);

//
static int  __init prosip_init(void);
static void __exit prosip_exit(void);

/* Structure that declares the usual file access functions */
struct file_operations sound_fops =
{
owner:
    THIS_MODULE,read:
    prosip_read,write:
    prosip_write,open:
    prosip_open,release:
    prosip_release,ioctl:
    prosip_ioctl
};


/* Declaration of the init and exit functions */
module_init(prosip_init);
module_exit(prosip_exit);

static int __init prosip_init(void)
{
    int ret = -1;
    buffer_size = 0;

    printk("<1>[prosip] Init...!n");

    ret = register_sound_dsp(&sound_fops,ver_minor);

    if(ret < 0)
    {
        printk("<1> [prosip] Registration failuren");
        //
        return ret;
    }
    else
    {
        ver_minor = ret;
        //
        printk("<1> [prosip] DSP Registered succesfully with id %dn",ret);
    }

    buffer = kmalloc(101,GFP_KERNEL);

    if(buffer == 0)
    {
        printk("<1>[prosip] Failed to allocate buffer !!!n");
        //
        return -ENOMEM;
    }

    //
    return 0;
}

static void __exit prosip_exit(void)
{
    printk("<1> [prosip] Sound Exit...n");

    unregister_sound_special(ver_minor);

    if(buffer)
    {
        kfree(buffer);
    }

}

/* Declaration of memory.c functions */
int prosip_open(struct inode *inode,struct file *filp)
{
    printk("<1> [prosip] Sound Open... n");

    try_module_get(THIS_MODULE);

    return 0;
}

//
int prosip_release(struct inode *inode,struct file *filp)
{
    printk("<1> [MySound] Sound Release... n");

    module_put(THIS_MODULE);

    return 0;
}

//
ssize_t prosip_read(struct file *filp,loff_t *f_pos)
{
    printk("<1> [prosip] Sound read...n");
    printk("<1> [prosip] Writing Count: %dn",count);

    if(buffer == 0)
    {
        printk("<1> NULL buffer!!! Unable to read");
        return 0;
    }

    //
    count =  buffer_size;

    if(read_count == 0)
    {
        read_count = buffer_size;
    }
    else
    {
        read_count = 0;
    }

    copy_to_user(buf,buffer,buffer_size);

    printk("<1> [prosip] Buffer: %s,buf: %s,Count: %dn",buf,count);

    return read_count;
}

//
ssize_t prosip_write(struct file *filp,loff_t *f_pos)
{
    printk("<1> [prosip] Sound write...!n");
    printk("<1> [prosip] Writing Count: %dn",count);

    //
    if(buffer == 0)
    {
        printk("<1> NULL buffer!!! Unable to write");

        return 0;
    }

    copy_from_user(buffer,count);

    buffer[count] = 0;
    buffer_size = count;

    printk("<1> [MySound] Writing Buffer: %s,count);

    return count;
}

/*
* This function is called whenever a process tries to do an ioctl on our
* device file.
*
*/
int prosip_ioctl(struct inode *inode,unsigned long ioctl_param)
{
    //
    return 0;
}

insmoding这个模块在/ dev / dsp创建一个驱动程序.它也可以在/ sys / devices / virtual / sound / dsp /中找到,因此它被系统识别为虚拟音频驱动程序.

我无法选择此设备进行播放和从应用程序捕获.我还需要做些什么才能使音频应用程序枚举此驱动程序?

解决方法

/ / dev / dsp是linux中旧OSS音响系统下用于声卡的设备节点,但现在几乎所有内容都默认为基于较新的ALSA音响系统寻找设备.

某些软件仍支持OSS,但您可能需要为其提供特殊选项或更改配置,以告知它使用OSS而不是ALSA.

ALSA设备位于/ dev / snd下,通常不直接访问,因为它们比旧的OSS设备更复杂.相反,libasound通常用于与它们进行交互.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读