157 lines
4.9 KiB
C
157 lines
4.9 KiB
C
#include <linux/module.h>
|
|
#include <linux/kernel.h>
|
|
#include <linux/init.h>
|
|
#include <linux/fs.h>
|
|
#include <linux/uaccess.h>
|
|
#include <linux/ioctl.h>
|
|
#include <linux/device.h> // Add this header for class_create, device_create, etc.
|
|
|
|
#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] = (int)arg; // Cast arg to int
|
|
}
|
|
printk(KERN_INFO "Audio Mixer: Set all channel volumes to %lu%%\n", arg); // Use %lu for unsigned long
|
|
}
|
|
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(CLASS_NAME); // Use the class name only
|
|
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);
|