hawxgamer

Untitled

Nov 24th, 2015
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.90 KB | None | 0 0
  1. #include <linux/init.h>
  2. #include <linux/module.h>
  3. #include <linux/kernel.h>
  4. #include <linux/ioport.h>
  5. #include <linux/fs.h>
  6. #include <linux/cdev.h>
  7. #include <linux/device.h>
  8.  
  9. //inkludiert in UE4
  10. #include <asm/arch/irqs.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/workqueue.h>
  13.  
  14. #include <asm/io.h>
  15. #include "hwaccess.h"
  16. #include "gpio_defs.h"
  17.  
  18. #define SWITCH0_IRQ IRQ_GPIO(15)
  19.  
  20.  
  21.  
  22. MODULE_LICENSE("Dual BSD/GPL");
  23.  
  24.  
  25. //WORKQUEUE BEGIN
  26. static void mykmod_work_handler(struct work_struct *w);
  27.  
  28. static struct workqueue_struct *wq = 0;
  29. static DECLARE_DELAYED_WORK(mykmod_work, mykmod_work_handler);
  30. static unsigned long onesec;
  31.  
  32. static void
  33. mykmod_work_handler(struct work_struct *w)
  34. {
  35. GpioRegs gpioRegs;
  36.  
  37. setLeds(&gpioRegs, 0x08);
  38. setLeds(&gpioRegs, 0x00);
  39. }
  40. // WORKQUEUE END
  41.  
  42.  
  43. //TASKLET BEGIN
  44. char my_tasklet_data[]="my_tasklet_function was called";
  45.  
  46. /* Bottom Half Function */
  47. void my_tasklet_function( unsigned long data )
  48. {
  49. GpioRegs gpioRegs;
  50.  
  51. setLeds(&gpioRegs, 0x04);
  52. setLeds(&gpioRegs, 0x00);
  53. //printk( "%s\n", (char *)data );
  54. return;
  55. }
  56. // TASKLET END
  57.  
  58. DECLARE_TASKLET( my_tasklet, my_tasklet_function,
  59. (unsigned long) &my_tasklet_data );
  60.  
  61. irqreturn_t irq_handler(int irq, void *dev_id, struct pt_regs *regs)
  62. {
  63. //tasklet_schedule( &my_tasklet );
  64.  
  65. if (!wq)
  66. wq = create_singlethread_workqueue("mykmod");
  67. if (wq)
  68. queue_delayed_work(wq, &mykmod_work, onesec);
  69.  
  70. schedule_work(&wq);
  71.  
  72. return 0;
  73. }
  74.  
  75.  
  76.  
  77. static int sample_cdev_open(struct inode *inode, struct file *file) {
  78. printk(KERN_INFO "open sample char device\n");
  79. return 0;
  80. }
  81.  
  82. static int sample_cdev_release(struct inode *inode, struct file *file) {
  83. printk(KERN_INFO "release sample char device\n");
  84. return 0;
  85. }
  86.  
  87. static ssize_t sample_cdev_read(struct file *file, char __user *data, size_t size, loff_t *offset) {
  88. return size;
  89. }
  90.  
  91. static ssize_t sample_cdev_write(struct file *file, const char __user *data, size_t size, loff_t *offset) {
  92. return size;
  93. }
  94.  
  95.  
  96.  
  97. static struct file_operations sample_cdev_fops = {
  98. .owner = THIS_MODULE,
  99. .open = sample_cdev_open,
  100. .release = sample_cdev_release,
  101. .read = sample_cdev_read,
  102. .write = sample_cdev_write,
  103. };
  104.  
  105. static struct cdev* sample_cdev;
  106. static dev_t dev = 0;
  107. static void * virtAddr=0;
  108.  
  109. static int my_init(void) {
  110. int PageSize = 4096;
  111. int result=0;
  112. // static struct cdev* sample_cdev;
  113. GpioRegs gpioRegs;
  114. printk("<1> Hello world!\n");
  115. if(request_mem_region(GPIO_BASE, sizeof(GpioRegs), "Name") == 0)
  116. {
  117. goto fail_request_mem_region;
  118. }
  119.  
  120.  
  121.  
  122.  
  123. virtAddr=ioremap_nocache(GPIO_BASE, PageSize);
  124. if (alloc_chrdev_region(&dev,GPIO_FIRST_MINOR,1,"Name") < 0)
  125. {
  126. goto fail_ioremap_nocache;
  127. }
  128.  
  129.  
  130. sample_cdev = cdev_alloc();
  131. if(!sample_cdev)
  132. {
  133. printk(KERN_ERR "failed to alloc cdev\n");
  134. goto fail_alloc_chrdev_region;
  135. }
  136. cdev_init(sample_cdev, &sample_cdev_fops);
  137.  
  138.  
  139. if(cdev_add(sample_cdev, dev, 1) < 0)
  140. {
  141. printk(KERN_ERR "failed to add cdev\n");
  142. goto fail_cdev_add;
  143. }
  144.  
  145. initLeds(&gpioRegs);
  146. initSwitches(&gpioRegs);
  147.  
  148.  
  149. set_irq_type(SWITCH0_IRQ, IRQT_RISING); // Interrupt nur für die steigende Flanke
  150.  
  151. result = request_irq(SWITCH0_IRQ, irq_handler,
  152. SA_INTERRUPT, "myIRQ", NULL);
  153.  
  154.  
  155.  
  156.  
  157.  
  158.  
  159.  
  160.  
  161.  
  162.  
  163.  
  164.  
  165. return 0;
  166.  
  167. fail_cdev_add:
  168. unregister_chrdev_region(dev, 1);
  169. fail_alloc_chrdev_region:
  170. iounmap((void*)GPIO_BASE);
  171. fail_ioremap_nocache:
  172. release_mem_region(GPIO_BASE, sizeof(GpioRegs));
  173. fail_request_mem_region:
  174. return 1;
  175. }
  176.  
  177. static void my_exit(void) {
  178. printk("<1> Bye, cruel world\n");
  179. cdev_del(sample_cdev);
  180. unregister_chrdev_region(dev, 1);
  181. iounmap(virtAddr);
  182. release_mem_region(GPIO_BASE, sizeof(GpioRegs));
  183.  
  184. /* Stop the tasklet before we exit */
  185. tasklet_kill( &my_tasklet );
  186.  
  187. }
  188.  
  189. module_init(my_init);
  190. module_exit(my_exit);
Advertisement
Add Comment
Please, Sign In to add comment