Moved to Fred\'s server
This commit is contained in:
1 parent
81ec8810b4
commit
e93351570b
5 files changed
+996
-230
No files matched your search
+105
@@ -0,0 +1,105 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <errno.h>
|
||||
|
||||
#define DEV_IN "/dev/dma_in"
|
||||
#define DEV_OUT "/dev/dma_out"
|
||||
#define BUF_SIZE 4096
|
||||
|
||||
void test_write_operation(int fd_out) {
|
||||
const char *test_data = "DMA_TEST_PATTERN_1234";
|
||||
ssize_t ret;
|
||||
|
||||
printf("\n=== Testing WRITE ===\n");
|
||||
printf("Writing test pattern: '%s'\n", test_data);
|
||||
|
||||
ret = write(fd_out, test_data, strlen(test_data));
|
||||
if (ret < 0) {
|
||||
perror("Write failed");
|
||||
} else {
|
||||
printf("Successfully wrote %zd bytes\n", ret);
|
||||
|
||||
// Verify kernel received it
|
||||
system("dmesg | tail -n 3 | grep 'DMA_OUT:'");
|
||||
}
|
||||
}
|
||||
|
||||
void test_read_operation(int fd_in) {
|
||||
char buf[BUF_SIZE];
|
||||
ssize_t ret;
|
||||
|
||||
printf("\n=== Testing READ ===\n");
|
||||
printf("Reading from device...\n");
|
||||
|
||||
ret = read(fd_in, buf, sizeof(buf));
|
||||
if (ret < 0) {
|
||||
perror("Read failed");
|
||||
} else {
|
||||
printf("Read %zd bytes:\n", ret);
|
||||
|
||||
// Print first 32 bytes as hex and ASCII
|
||||
for (int i = 0; i < ret && i < 32; i++) {
|
||||
if (i % 16 == 0) printf("\n%04x: ", i);
|
||||
printf("%02x ", buf[i]);
|
||||
}
|
||||
printf("\n");
|
||||
|
||||
// Print as string (null-terminate first)
|
||||
buf[ret < BUF_SIZE ? ret : BUF_SIZE-1] = '\0';
|
||||
printf("As string: %.32s%s\n", buf, ret > 32 ? "..." : "");
|
||||
}
|
||||
}
|
||||
|
||||
void test_ioctl_control(int fd) {
|
||||
printf("\n=== Testing IOCTL ===\n");
|
||||
// Replace with your actual IOCTL commands
|
||||
#define DMA_START _IO('D', 1)
|
||||
#define DMA_STATUS _IOR('D', 2, int)
|
||||
|
||||
int status;
|
||||
if (ioctl(fd, DMA_START) {
|
||||
perror("IOCTL DMA_START failed");
|
||||
} else {
|
||||
printf("DMA started successfully\n");
|
||||
}
|
||||
|
||||
if (ioctl(fd, DMA_STATUS, &status) {
|
||||
perror("IOCTL DMA_STATUS failed");
|
||||
} else {
|
||||
printf("DMA status: %d\n", status);
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
int fd_in, fd_out;
|
||||
|
||||
printf("Opening devices...\n");
|
||||
fd_out = open(DEV_OUT, O_WRONLY);
|
||||
if (fd_out < 0) {
|
||||
perror("Failed to open output device");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
fd_in = open(DEV_IN, O_RDONLY);
|
||||
if (fd_in < 0) {
|
||||
perror("Failed to open input device");
|
||||
close(fd_out);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
// Run test sequence
|
||||
test_write_operation(fd_out);
|
||||
test_read_operation(fd_in);
|
||||
test_ioctl_control(fd_out); // Can use either fd
|
||||
|
||||
printf("\n=== Verification Complete ===\n");
|
||||
|
||||
close(fd_in);
|
||||
close(fd_out);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,152 @@
|
||||
#include <linux/fs.h>
|
||||
#include <linux/cdev.h>
|
||||
#include <linux/device.h>
|
||||
#include <linux/module.h>
|
||||
|
||||
/* File operations structures */
|
||||
static int dma_in_open(struct inode *inode, struct file *file) {
|
||||
printk(KERN_INFO "DMA: Input device opened\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
static ssize_t dma_in_read(struct file *file, char __user *buf, size_t count, loff_t *ppos) {
|
||||
printk(KERN_INFO "DMA: Read %zu bytes from input\n", count);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static const struct file_operations fops_in = {
|
||||
.owner = THIS_MODULE,
|
||||
.open = dma_in_open,
|
||||
.read = dma_in_read,
|
||||
};
|
||||
|
||||
static int dma_out_open(struct inode *inode, struct file *file) {
|
||||
printk(KERN_INFO "DMA: Output device opened\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
static ssize_t dma_out_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos) {
|
||||
printk(KERN_INFO "DMA: Wrote %zu bytes to output\n", count);
|
||||
return count;
|
||||
}
|
||||
|
||||
static const struct file_operations fops_out = {
|
||||
.owner = THIS_MODULE,
|
||||
.open = dma_out_open,
|
||||
.write = dma_out_write,
|
||||
};
|
||||
|
||||
/* Global variables */
|
||||
static struct class *dma_class;
|
||||
static struct device *dev_in, *dev_out;
|
||||
static struct cdev dma_cdev_in, dma_cdev_out;
|
||||
static int major_in, major_out;
|
||||
|
||||
static int __init mod_init(void)
|
||||
{
|
||||
dev_t dev_num_in, dev_num_out;
|
||||
int ret;
|
||||
|
||||
printk(KERN_INFO "DMA: Initializing dual-channel module\n");
|
||||
|
||||
/* --- Input Device Setup --- */
|
||||
ret = alloc_chrdev_region(&dev_num_in, 0, 1, "dma_in");
|
||||
if (ret < 0) {
|
||||
printk(KERN_ERR "DMA: Failed to allocate input device numbers\n");
|
||||
return ret;
|
||||
}
|
||||
major_in = MAJOR(dev_num_in);
|
||||
|
||||
/* Correct class_create() for modern kernels */
|
||||
dma_class = class_create("dma_devices");
|
||||
if (IS_ERR(dma_class)) {
|
||||
ret = PTR_ERR(dma_class);
|
||||
printk(KERN_ERR "DMA: Failed to create device class (%d)\n", ret);
|
||||
goto err_unreg_in;
|
||||
}
|
||||
|
||||
dev_in = device_create(dma_class, NULL, dev_num_in, NULL, "dma_in");
|
||||
if (IS_ERR(dev_in)) {
|
||||
ret = PTR_ERR(dev_in);
|
||||
printk(KERN_ERR "DMA: Failed to create input device (%d)\n", ret);
|
||||
goto err_class;
|
||||
}
|
||||
|
||||
cdev_init(&dma_cdev_in, &fops_in);
|
||||
ret = cdev_add(&dma_cdev_in, dev_num_in, 1);
|
||||
if (ret < 0) {
|
||||
printk(KERN_ERR "DMA: Failed to add input cdev (%d)\n", ret);
|
||||
goto err_dev_in;
|
||||
}
|
||||
|
||||
/* --- Output Device Setup --- */
|
||||
ret = alloc_chrdev_region(&dev_num_out, 0, 1, "dma_out");
|
||||
if (ret < 0) {
|
||||
printk(KERN_ERR "DMA: Failed to allocate output device numbers (%d)\n", ret);
|
||||
goto err_cdev_in;
|
||||
}
|
||||
major_out = MAJOR(dev_num_out);
|
||||
|
||||
dev_out = device_create(dma_class, NULL, dev_num_out, NULL, "dma_out");
|
||||
if (IS_ERR(dev_out)) {
|
||||
ret = PTR_ERR(dev_out);
|
||||
printk(KERN_ERR "DMA: Failed to create output device (%d)\n", ret);
|
||||
goto err_unreg_out;
|
||||
}
|
||||
|
||||
cdev_init(&dma_cdev_out, &fops_out);
|
||||
ret = cdev_add(&dma_cdev_out, dev_num_out, 1);
|
||||
if (ret < 0) {
|
||||
printk(KERN_ERR "DMA: Failed to add output cdev (%d)\n", ret);
|
||||
goto err_dev_out;
|
||||
}
|
||||
|
||||
printk(KERN_INFO "DMA: Successfully created:\n");
|
||||
printk(KERN_INFO " /dev/dma_in (major %d, minor 0)\n", major_in);
|
||||
printk(KERN_INFO " /dev/dma_out (major %d, minor 0)\n", major_out);
|
||||
return 0;
|
||||
|
||||
/* Error handling */
|
||||
err_dev_out:
|
||||
device_destroy(dma_class, dev_num_out);
|
||||
err_unreg_out:
|
||||
unregister_chrdev_region(dev_num_out, 1);
|
||||
err_cdev_in:
|
||||
cdev_del(&dma_cdev_in);
|
||||
err_dev_in:
|
||||
device_destroy(dma_class, dev_num_in);
|
||||
err_class:
|
||||
class_destroy(dma_class);
|
||||
err_unreg_in:
|
||||
unregister_chrdev_region(dev_num_in, 1);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static void __exit mod_exit(void)
|
||||
{
|
||||
dev_t dev_num_in = MKDEV(major_in, 0);
|
||||
dev_t dev_num_out = MKDEV(major_out, 0);
|
||||
|
||||
/* Clean up output device */
|
||||
cdev_del(&dma_cdev_out);
|
||||
device_destroy(dma_class, dev_num_out);
|
||||
unregister_chrdev_region(dev_num_out, 1);
|
||||
|
||||
/* Clean up input device */
|
||||
cdev_del(&dma_cdev_in);
|
||||
device_destroy(dma_class, dev_num_in);
|
||||
unregister_chrdev_region(dev_num_in, 1);
|
||||
|
||||
/* Clean shared resources */
|
||||
class_destroy(dma_class);
|
||||
|
||||
printk(KERN_INFO "DMA: Module unloaded\n");
|
||||
}
|
||||
|
||||
module_init(mod_init);
|
||||
module_exit(mod_exit);
|
||||
|
||||
MODULE_LICENSE("GPL");
|
||||
MODULE_AUTHOR("Your Name");
|
||||
MODULE_DESCRIPTION("Dual-channel DMA device driver");
|
||||
|
||||
Reference in new issue
Block a user