testing audio
This commit is contained in:
parent
5cb3fd2d1e
commit
45d0e66630
@ -3,7 +3,7 @@ KERNELDIR=/lib/modules/`uname -r`/build
|
||||
# This the the Makefile for the device driver (not your demonstration program)
|
||||
|
||||
#Change the base name here to your module file name - no extension
|
||||
BASENAME=helloworld
|
||||
BASENAME= audio_mixer
|
||||
|
||||
|
||||
MODULES = $(BASENAME).ko
|
||||
|
||||
155
Module/audio_mixer.c
Normal file
155
Module/audio_mixer.c
Normal file
@ -0,0 +1,155 @@
|
||||
#include <linux/module.h>
|
||||
#include <linux/kernel.h>
|
||||
#include <linux/init.h>
|
||||
#include <linux/fs.h>
|
||||
#include <linux/uaccess.h>
|
||||
#include <linux/ioctl.h>
|
||||
|
||||
#define DEVICE_NAME "audio_mixer"
|
||||
#define CLASS_NAME "audio"
|
||||
|
||||
MODULE_LICENSE("GPL");
|
||||
MODULE_AUTHOR("Your Name");
|
||||
MODULE_DESCRIPTION("A virtual audio mixer device driver");
|
||||
MODULE_VERSION("0.1");
|
||||
|
||||
static int major_number;
|
||||
static char *audio_channels[5] = {"Channel 1", "Channel 2", "Channel 3", "Channel 4", "Channel 5"};
|
||||
static int volumes[5] = {50, 50, 50, 50, 50}; // Volume for each channel
|
||||
static int muted[5] = {0, 0, 0, 0, 0}; // Mute state for each channel (0: not muted, 1: muted)
|
||||
|
||||
#define AUDIO_IOC_MAGIC 'A'
|
||||
#define AUDIO_IOC_SET_VOLUME _IOW(AUDIO_IOC_MAGIC, 0, int)
|
||||
#define AUDIO_IOC_MUTE _IOW(AUDIO_IOC_MAGIC, 1, int)
|
||||
#define AUDIO_IOC_RESET _IO(AUDIO_IOC_MAGIC, 2)
|
||||
|
||||
static struct class *audio_class = NULL;
|
||||
static struct device *audio_device = NULL;
|
||||
|
||||
static int device_open(struct inode *inodep, struct file *filep) {
|
||||
printk(KERN_INFO "Audio Mixer: Device opened\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int device_release(struct inode *inodep, struct file *filep) {
|
||||
printk(KERN_INFO "Audio Mixer: Device closed\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
static ssize_t device_read(struct file *filep, char *buffer, size_t len, loff_t *offset) {
|
||||
int i;
|
||||
char msg[256];
|
||||
int msg_len = 0;
|
||||
|
||||
for (i = 0; i < 5; i++) {
|
||||
msg_len += snprintf(msg + msg_len, sizeof(msg) - msg_len,
|
||||
"%s: Volume=%d%%, Muted=%d\n",
|
||||
audio_channels[i], volumes[i], muted[i]);
|
||||
}
|
||||
|
||||
if (*offset >= msg_len)
|
||||
return 0;
|
||||
|
||||
if (len > msg_len - *offset)
|
||||
len = msg_len - *offset;
|
||||
|
||||
if (copy_to_user(buffer, msg + *offset, len)) {
|
||||
printk(KERN_ERR "Audio Mixer: Failed to send data to user\n");
|
||||
return -EFAULT;
|
||||
}
|
||||
|
||||
*offset += len;
|
||||
return len;
|
||||
}
|
||||
|
||||
static ssize_t device_write(struct file *filep, const char *buffer, size_t len, loff_t *offset) {
|
||||
|
||||
char msg[256];
|
||||
if (copy_from_user(msg, buffer, len)) {
|
||||
printk(KERN_ERR "Audio Mixer: Failed to receive data from user\n");
|
||||
return -EFAULT;
|
||||
}
|
||||
|
||||
if (sscanf(msg, "CHANNEL1=%d", &volumes[0]) == 1) {
|
||||
printk(KERN_INFO "Audio Mixer: Set Channel 1 volume to %d%%\n", volumes[0]);
|
||||
}
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
static long device_ioctl(struct file *filep, unsigned int cmd, unsigned long arg) {
|
||||
int i;
|
||||
switch (cmd) {
|
||||
case AUDIO_IOC_SET_VOLUME:
|
||||
if (arg >= 0 && arg <= 100) {
|
||||
for (i = 0; i < 5; i++) {
|
||||
volumes[i] = arg;
|
||||
}
|
||||
printk(KERN_INFO "Audio Mixer: Set all channel volumes to %d%%\n", arg);
|
||||
}
|
||||
break;
|
||||
case AUDIO_IOC_MUTE:
|
||||
for (i = 0; i < 5; i++) {
|
||||
muted[i] = 1; // Mute all channels
|
||||
}
|
||||
printk(KERN_INFO "Audio Mixer: Muted all channels\n");
|
||||
break;
|
||||
case AUDIO_IOC_RESET:
|
||||
for (i = 0; i < 5; i++) {
|
||||
volumes[i] = 50; // Reset to default volume
|
||||
muted[i] = 0; // Unmute all
|
||||
}
|
||||
printk(KERN_INFO "Audio Mixer: Reset all channels to default settings\n");
|
||||
break;
|
||||
default:
|
||||
return -EINVAL;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static struct file_operations fops = {
|
||||
.open = device_open,
|
||||
.release = device_release,
|
||||
.read = device_read,
|
||||
.write = device_write,
|
||||
.unlocked_ioctl = device_ioctl,
|
||||
};
|
||||
|
||||
static int __init audio_mixer_init(void) {
|
||||
printk(KERN_INFO "Audio Mixer: Initializing the audio mixer driver\n");
|
||||
|
||||
major_number = register_chrdev(0, DEVICE_NAME, &fops);
|
||||
if (major_number < 0) {
|
||||
printk(KERN_ALERT "Audio Mixer: Failed to register a major number\n");
|
||||
return major_number;
|
||||
}
|
||||
|
||||
audio_class = class_create(THIS_MODULE, CLASS_NAME);
|
||||
if (IS_ERR(audio_class)) {
|
||||
unregister_chrdev(major_number, DEVICE_NAME);
|
||||
printk(KERN_ALERT "Audio Mixer: Failed to register device class\n");
|
||||
return PTR_ERR(audio_class);
|
||||
}
|
||||
|
||||
audio_device = device_create(audio_class, NULL, MKDEV(major_number, 0), NULL, DEVICE_NAME);
|
||||
if (IS_ERR(audio_device)) {
|
||||
class_destroy(audio_class);
|
||||
unregister_chrdev(major_number, DEVICE_NAME);
|
||||
printk(KERN_ALERT "Audio Mixer: Failed to create the device\n");
|
||||
return PTR_ERR(audio_device);
|
||||
}
|
||||
|
||||
printk(KERN_INFO "Audio Mixer: Device created successfully with major number %d\n", major_number);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void __exit audio_mixer_exit(void) {
|
||||
device_destroy(audio_class, MKDEV(major_number, 0));
|
||||
class_unregister(audio_class);
|
||||
class_destroy(audio_class);
|
||||
unregister_chrdev(major_number, DEVICE_NAME);
|
||||
printk(KERN_INFO "Audio Mixer: Device driver unloaded\n");
|
||||
}
|
||||
|
||||
module_init(audio_mixer_init);
|
||||
module_exit(audio_mixer_exit);
|
||||
@ -33,8 +33,8 @@
|
||||
# will delete the executable and any object files in your directory.
|
||||
#
|
||||
|
||||
FIRSTNAME=
|
||||
LASTNAME=
|
||||
FIRSTNAME= Michelle
|
||||
LASTNAME= Nguyen
|
||||
|
||||
ROOTNAME=$(LASTNAME)_$(FIRSTNAME)_HW
|
||||
HW=6
|
||||
@ -61,3 +61,13 @@ run: $(ROOTNAME)$(HW)$(FOPTION)
|
||||
vrun: $(ROOTNAME)$(HW)$(FOPTION)
|
||||
valgrind ./$(ROOTNAME)$(HW)$(FOPTION) $(RUNOPTIONS)
|
||||
|
||||
# Additional target for audio_test
|
||||
CC = gcc
|
||||
CFLAGS = -Wall -Wextra -Werror
|
||||
|
||||
audio_test: audio_test.c
|
||||
$(CC) $(CFLAGS) -o audio_test audio_test.c
|
||||
|
||||
clean:
|
||||
rm -f audio_test
|
||||
|
||||
|
||||
42
Test/audio_test.c
Normal file
42
Test/audio_test.c
Normal file
@ -0,0 +1,42 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <errno.h>
|
||||
|
||||
#define DEVICE_NAME "/dev/audio_mixer"
|
||||
#define AUDIO_IOC_MAGIC 'A'
|
||||
#define AUDIO_IOC_SET_VOLUME _IOW(AUDIO_IOC_MAGIC, 0, int)
|
||||
#define AUDIO_IOC_MUTE _IOW(AUDIO_IOC_MAGIC, 1, int)
|
||||
#define AUDIO_IOC_RESET _IO(AUDIO_IOC_MAGIC, 2)
|
||||
|
||||
int main() {
|
||||
int fd = open(DEVICE_NAME, O_RDWR);
|
||||
if (fd < 0) {
|
||||
perror("Failed to open the device");
|
||||
return errno;
|
||||
}
|
||||
|
||||
int volume = 75;
|
||||
if (ioctl(fd, AUDIO_IOC_SET_VOLUME, &volume) < 0) {
|
||||
perror("Failed to set volume");
|
||||
close(fd);
|
||||
return errno;
|
||||
}
|
||||
|
||||
if (ioctl(fd, AUDIO_IOC_MUTE) < 0) {
|
||||
perror("Failed to mute channels");
|
||||
close(fd);
|
||||
return errno;
|
||||
}
|
||||
|
||||
if (ioctl(fd, AUDIO_IOC_RESET) < 0) {
|
||||
perror("Failed to reset channels");
|
||||
close(fd);
|
||||
return errno;
|
||||
}
|
||||
|
||||
close(fd);
|
||||
return 0;
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user