Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <linux/init.h>
- #include <linux/module.h>
- #include <linux/kernel.h>
- #include <linux/ioport.h>
- #include <linux/fs.h>
- #include <linux/cdev.h>
- #include <linux/device.h>
- //inkludiert in UE4
- #include <asm/arch/irqs.h>
- #include <linux/interrupt.h>
- #include <linux/workqueue.h>
- #include <asm/io.h>
- #include "hwaccess.h"
- #include "gpio_defs.h"
- #define SWITCH0_IRQ IRQ_GPIO(15)
- MODULE_LICENSE("Dual BSD/GPL");
- //WORKQUEUE BEGIN
- static void mykmod_work_handler(struct work_struct *w);
- static struct workqueue_struct *wq = 0;
- static DECLARE_DELAYED_WORK(mykmod_work, mykmod_work_handler);
- static unsigned long onesec;
- static void
- mykmod_work_handler(struct work_struct *w)
- {
- GpioRegs gpioRegs;
- setLeds(&gpioRegs, 0x08);
- setLeds(&gpioRegs, 0x00);
- }
- // WORKQUEUE END
- //TASKLET BEGIN
- char my_tasklet_data[]="my_tasklet_function was called";
- /* Bottom Half Function */
- void my_tasklet_function( unsigned long data )
- {
- GpioRegs gpioRegs;
- setLeds(&gpioRegs, 0x04);
- setLeds(&gpioRegs, 0x00);
- //printk( "%s\n", (char *)data );
- return;
- }
- // TASKLET END
- DECLARE_TASKLET( my_tasklet, my_tasklet_function,
- (unsigned long) &my_tasklet_data );
- irqreturn_t irq_handler(int irq, void *dev_id, struct pt_regs *regs)
- {
- //tasklet_schedule( &my_tasklet );
- if (!wq)
- wq = create_singlethread_workqueue("mykmod");
- if (wq)
- queue_delayed_work(wq, &mykmod_work, onesec);
- schedule_work(&wq);
- return 0;
- }
- static int sample_cdev_open(struct inode *inode, struct file *file) {
- printk(KERN_INFO "open sample char device\n");
- return 0;
- }
- static int sample_cdev_release(struct inode *inode, struct file *file) {
- printk(KERN_INFO "release sample char device\n");
- return 0;
- }
- static ssize_t sample_cdev_read(struct file *file, char __user *data, size_t size, loff_t *offset) {
- return size;
- }
- static ssize_t sample_cdev_write(struct file *file, const char __user *data, size_t size, loff_t *offset) {
- return size;
- }
- static struct file_operations sample_cdev_fops = {
- .owner = THIS_MODULE,
- .open = sample_cdev_open,
- .release = sample_cdev_release,
- .read = sample_cdev_read,
- .write = sample_cdev_write,
- };
- static struct cdev* sample_cdev;
- static dev_t dev = 0;
- static void * virtAddr=0;
- static int my_init(void) {
- int PageSize = 4096;
- int result=0;
- // static struct cdev* sample_cdev;
- GpioRegs gpioRegs;
- printk("<1> Hello world!\n");
- if(request_mem_region(GPIO_BASE, sizeof(GpioRegs), "Name") == 0)
- {
- goto fail_request_mem_region;
- }
- virtAddr=ioremap_nocache(GPIO_BASE, PageSize);
- if (alloc_chrdev_region(&dev,GPIO_FIRST_MINOR,1,"Name") < 0)
- {
- goto fail_ioremap_nocache;
- }
- sample_cdev = cdev_alloc();
- if(!sample_cdev)
- {
- printk(KERN_ERR "failed to alloc cdev\n");
- goto fail_alloc_chrdev_region;
- }
- cdev_init(sample_cdev, &sample_cdev_fops);
- if(cdev_add(sample_cdev, dev, 1) < 0)
- {
- printk(KERN_ERR "failed to add cdev\n");
- goto fail_cdev_add;
- }
- initLeds(&gpioRegs);
- initSwitches(&gpioRegs);
- set_irq_type(SWITCH0_IRQ, IRQT_RISING); // Interrupt nur für die steigende Flanke
- result = request_irq(SWITCH0_IRQ, irq_handler,
- SA_INTERRUPT, "myIRQ", NULL);
- return 0;
- fail_cdev_add:
- unregister_chrdev_region(dev, 1);
- fail_alloc_chrdev_region:
- iounmap((void*)GPIO_BASE);
- fail_ioremap_nocache:
- release_mem_region(GPIO_BASE, sizeof(GpioRegs));
- fail_request_mem_region:
- return 1;
- }
- static void my_exit(void) {
- printk("<1> Bye, cruel world\n");
- cdev_del(sample_cdev);
- unregister_chrdev_region(dev, 1);
- iounmap(virtAddr);
- release_mem_region(GPIO_BASE, sizeof(GpioRegs));
- /* Stop the tasklet before we exit */
- tasklet_kill( &my_tasklet );
- }
- module_init(my_init);
- module_exit(my_exit);
Advertisement
Add Comment
Please, Sign In to add comment