Skip to content

Commit

Permalink
whitespaces to tab. Adding header and comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
bax-smile committed Aug 29, 2024
1 parent e1a5d20 commit b7a2a5e
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 109 deletions.
115 changes: 61 additions & 54 deletions uio_app.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/*
* This application is part of a UIO driver demonstration
* It opens the /dev/uio0 device and waits for events.
*
*/

#include <stdlib.h> // EXIT codes
#include <stdio.h> // printf
#include <unistd.h> // sysconf
Expand All @@ -9,64 +15,65 @@

int main()
{
int uiofd;
int err = 0;
unsigned int i;
fd_set uiofd_set;
struct timeval tv;
int interrupt_count;
char* mem;
int uiofd;
int err = 0;
unsigned int i;
fd_set uiofd_set;
struct timeval tv;
int interrupt_count;
char* mem;


// open uio0
uiofd = open("/dev/uio0", O_RDONLY);
if (uiofd < 0) {
perror("uio open:");
return errno;
}

// create mmap on uio0 / map0
mem = mmap(NULL, sysconf(_SC_PAGE_SIZE), PROT_READ, MAP_SHARED, uiofd , 0);
if (mem == MAP_FAILED){
perror("mmap failed");
close(uiofd);
exit(EXIT_FAILURE);
}

// prepare a set of FD for select with uiofd only
FD_ZERO(&uiofd_set);
FD_SET(uiofd, &uiofd_set);

for(i = 0;i < 10; ++i) {
// five second timeout (reset each time)
tv.tv_sec = 5;
tv.tv_usec = 0;

// open uio0
uiofd = open("/dev/uio0", O_RDONLY);
if (uiofd < 0) {
perror("uio open:");
return errno;
}

// create mmap on uio0 / map0
mem = mmap(NULL, sysconf(_SC_PAGE_SIZE), PROT_READ, MAP_SHARED, uiofd , 0);
if (mem == MAP_FAILED){
perror("mmap failed");
close(uiofd);
exit(EXIT_FAILURE);
}
// select waits for a group of file descriptor
// uiofd + 1 is the max_fd(uiofd_set) +1
err = select(uiofd+1, &uiofd_set, NULL, NULL, &tv);
if (err < 0){
perror("select()");
break;
}else if(err == 0){
printf("Timeout, exiting\n");
break;
}

// prepare select structure
FD_ZERO(&uiofd_set);
FD_SET(uiofd, &uiofd_set);
// actually read, consumes the interrupt in uio driver.
char buf[4];
err = read(uiofd, buf, 4);
if (err !=4){
perror("Read error.");
break;
}

for(i = 0;i < 10; ++i) {
// five second timeout (reset each time)
tv.tv_sec = 5;
tv.tv_usec = 0;
// read interrupt count from memory mapping
memcpy(&interrupt_count, mem, sizeof(interrupt_count));
printf("Read interrupt count : %d \n", interrupt_count);

// wait for interrupt
err = select(uiofd+1, &uiofd_set, NULL, NULL, &tv);
if (err < 0){
perror("select()");
break;
}else if(err == 0){
printf("Timeout, exiting\n");
break;
}

// actually read, acknowledges the interrupt
char buf[4];
err = read(uiofd, buf, 4);
if (err !=4){
perror("Read error.");
break;
}

// read interrupt count from memory mapping
memcpy(&interrupt_count, mem, sizeof(interrupt_count));
printf("Read interrupt count : %d \n", interrupt_count);
}

}

munmap(mem, sysconf(_SC_PAGE_SIZE));
close(uiofd);
exit(err);
munmap(mem, sysconf(_SC_PAGE_SIZE));
close(uiofd);
exit(err);
}
116 changes: 61 additions & 55 deletions uio_kbd.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* UIO demonstration driver catching keyboard events
*
* Copyright (C) 2024 Smile
* Author: Antoine Bax de keating <[email protected]>
*/

#include <linux/kernel.h> /* printk() */
#include <linux/module.h> /* modules */
#include <linux/init.h> /* module_{init,exit}() */
#include <linux/uio_driver.h>
#include <linux/device.h>
#include <linux/device.h>
#include <linux/slab.h> /* kzalloc */

MODULE_LICENSE("GPL");
Expand All @@ -22,12 +30,13 @@ char *mem_area;

static irqreturn_t uio_handler(int irq, struct uio_info *dev_info)
{
static unsigned int irq_count = 0;
static unsigned int irq_count;

pr_info("UIO handler");
irq_count++;
memcpy(mem_area, &irq_count, sizeof(irq_count));

irq_count++;
memcpy(mem_area, &irq_count, sizeof(irq_count));

return IRQ_HANDLED;
}

Expand All @@ -41,60 +50,57 @@ static void uio_release(struct device *dev)
*/
static int __init uio_kbd_init(void)
{
int ret;

dev = kzalloc(sizeof(struct device), GFP_KERNEL);
dev_set_name(dev, "uio_kbd_device");
dev->release = uio_release;
ret = device_register(dev);
if (ret < 0){
kfree(dev);
pr_warn("Failing to register uio_kbd device\n");
return ret;
}



mem_area = kzalloc(PAGE_SIZE, GFP_KERNEL);

info = kzalloc(sizeof(struct uio_info), GFP_KERNEL);
info->name = "uio_kbd_device";
info->version = "0.0.1";
info->irq = irq;
info->irq_flags = IRQF_SHARED;
info->handler = uio_handler;

info->mem[0].name = "basic_mem_map";
info->mem[0].memtype = UIO_MEM_LOGICAL;
info->mem[0].addr = (phys_addr_t) mem_area;
info->mem[0].size = sizeof(mem_area);


ret = uio_register_device(dev, info);
if (ret < 0) {
device_unregister(dev);
kfree(dev);
kfree(info);
kfree(mem_area);
pr_warn("Failing to register uio_kbd UIO device\n");
return ret;
}

pr_info( "Registered UIO handler for IRQ=%d\n", irq);
return 0;

int ret;

dev = kzalloc(sizeof(struct device), GFP_KERNEL);
dev_set_name(dev, "uio_kbd_device");
dev->release = uio_release;
ret = device_register(dev);
if (ret < 0) {
kfree(dev);
pr_warn("Failing to register uio_kbd device\n");
return ret;
}



mem_area = kzalloc(PAGE_SIZE, GFP_KERNEL);

info = kzalloc(sizeof(struct uio_info), GFP_KERNEL);
info->name = "uio_kbd_device";
info->version = "0.0.1";
info->irq = irq;
info->irq_flags = IRQF_SHARED;
info->handler = uio_handler;

info->mem[0].name = "basic_mem_map";
info->mem[0].memtype = UIO_MEM_LOGICAL;
info->mem[0].addr = (phys_addr_t) mem_area;
info->mem[0].size = sizeof(mem_area);

ret = uio_register_device(dev, info);
if (ret < 0) {
device_unregister(dev);
kfree(dev);
kfree(info);
kfree(mem_area);
pr_warn("Failing to register uio_kbd UIO device\n");
return ret;
}

pr_info("Registered UIO handler for IRQ=%d\n", irq);
return 0;
}

static void __exit uio_kbd_exit(void)
{
uio_unregister_device(info);
device_unregister(dev);
pr_info("Un-Registered UIO handler for IRQ=%d\n", irq);
kfree(info);
kfree(dev);
kfree(mem_area);
uio_unregister_device(info);
device_unregister(dev);
pr_info("Un-Registered UIO handler for IRQ=%d\n", irq);
kfree(info);
kfree(dev);
kfree(mem_area);
}

module_init(uio_kbd_init);
module_exit(uio_kbd_exit);

0 comments on commit b7a2a5e

Please sign in to comment.