Skip to content

Instantly share code, notes, and snippets.

@M0nteCarl0
Created July 25, 2023 05:55
Show Gist options
  • Save M0nteCarl0/89fff4ab8341fb9d5d46a50a7ebc08ef to your computer and use it in GitHub Desktop.
Save M0nteCarl0/89fff4ab8341fb9d5d46a50a7ebc08ef to your computer and use it in GitHub Desktop.
Linux camera emulation module
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/types.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/slab.h>
#define MODULE_NAME "camera_emulator"
#define DEVICE_NAME "camera"
static dev_t dev;
static struct cdev cdev;
static struct class *dev_class;
static int width = 8;
static int height = 8;
static int camera_open(struct inode *inode, struct file *filp) {
printk(KERN_INFO "%s: Camera device opened\n", MODULE_NAME);
return 0;
}
static int camera_release(struct inode *inode, struct file *filp) {
printk(KERN_INFO "%s: Camera device closed\n", MODULE_NAME);
return 0;
}
static ssize_t camera_read(struct file *filp, char __user *buf, size_t count, loff_t *f_pos) {
char *framebuffer;
size_t framebuffer_size;
int i, j;
int pixel;
if (*f_pos >= width * height)
return 0;
framebuffer_size = width * height;
framebuffer = kmalloc(framebuffer_size, GFP_KERNEL);
if (!framebuffer)
return -ENOMEM;
for (i = 0; i < height; i++) {
for (j = 0; j < width; j++) {
pixel = (i + j) % 2 == 0 ? 255 : 0;
framebuffer[i * width + j] = pixel;
}
}
if (copy_to_user(buf, framebuffer, framebuffer_size)) {
kfree(framebuffer);
return -EFAULT;
}
kfree(framebuffer);
*f_pos += framebuffer_size;
return framebuffer_size;
}
static struct file_operations camera_fops = {
.owner = THIS_MODULE,
.open = camera_open,
.release = camera_release,
.read = camera_read,
};
static int __init camera_init(void) {
int ret;
ret = alloc_chrdev_region(&dev, 0, 1, DEVICE_NAME);
if (ret < 0) {
printk(KERN_ERR "%s: Failed to allocate device number\n", MODULE_NAME);
return ret;
}
cdev_init(&cdev, &camera_fops);
cdev.owner = THIS_MODULE;
ret = cdev_add(&cdev, dev, 1);
if (ret < 0) {
printk(KERN_ERR "%s: Failed to add character device\n", MODULE_NAME);
unregister_chrdev_region(dev, 1);
return ret;
}
dev_class = class_create(THIS_MODULE, DEVICE_NAME);
if (IS_ERR(dev_class)) {
printk(KERN_ERR "%s: Failed to create device class\n", MODULE_NAME);
cdev_del(&cdev);
unregister_chrdev_region(dev, 1);
return PTR_ERR(dev_class);
}
device_create(dev_class, NULL, dev, NULL, DEVICE_NAME);
printk(KERN_INFO "%s: Camera emulator module loaded\n", MODULE_NAME);
return 0;
}
static void __exit camera_exit(void) {
device_destroy(dev_class, dev);
class_destroy(dev_class);
cdev_del(&cdev);
unregister_chrdev_region(dev, 1);
printk(KERN_INFO "%s: Camera emulator module unloaded\n", MODULE_NAME);
}
module_init(camera_init);
module_exit(camera_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Your Name");
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#define DEVICE_PATH "/dev/camera"
int main() {
int fd;
unsigned char *buffer;
size_t buffer_size = 8 * 8; // Размер буфера соответствует ширине и высоте шахматной доски
// Открываем файл драйвера
fd = open(DEVICE_PATH, O_RDONLY);
if (fd < 0) {
perror("Failed to open camera device");
return EXIT_FAILURE;
}
// Выделяем память для буфера
buffer = (unsigned char*)malloc(buffer_size);
if (!buffer) {
perror("Failed to allocate buffer");
close(fd);
return EXIT_FAILURE;
}
// Читаем данные с драйвера
ssize_t bytes_read = read(fd, buffer, buffer_size);
if (bytes_read < 0) {
perror("Failed to read from camera device");
free(buffer);
close(fd);
return EXIT_FAILURE;
}
// Выводим данные на экран
for (size_t i = 0; i < buffer_size; i++) {
printf("%d ", buffer[i]);
}
printf("\n");
// Освобождаем ресурсы
free(buffer);
close(fd);
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment