Root/PS2_INTERFACE/drivers/kbps2.c

1/*
2 * Driver preliminar y de pruebas de teclado ps2 kbps2
3 *
4 * Author: Ari AndrĂ©s Bejarano H.
5 *
6 */
7
8#include <linux/module.h> /* Needed by all modules */
9#include <linux/kernel.h> /* Needed for KERN_INFO, printk() */
10#include <linux/ioport.h>
11#include <linux/device.h>
12#include <linux/interrupt.h> /* We want an interrupt */
13#include <linux/irq.h> /* We want an interrupt */
14#include <linux/platform_device.h>
15#include <linux/fs.h>
16#include <asm/uaccess.h>
17#include <asm/io.h> /*ioremap ioremap_nocache iounmap iowriteXX ioreadXX*/
18#include <linux/gpio.h>
19#include <asm/mach-jz4740/gpio.h>
20#include<asm/system.h> /*rmb*/
21
22#define FPGA_IRQ_PIN JZ_GPIO_PORTC(15)
23#define FPGA_CS JZ_GPIO_PORTB(26)
24#define FPGA_BASE_BEGIN 0x15000000
25#define FPGA_BASE_END 0x17FFFFFF
26#define SUCCESS 0
27#define DEVICE_NAME "kbps2" /* Dev name as it appears in /proc/devices */
28
29MODULE_LICENSE("GPL");
30MODULE_AUTHOR("Ari");
31MODULE_DESCRIPTION("Keyboard ps2");
32MODULE_VERSION("1.0");
33
34/* Declaration of kbps2.c functions */
35static irqreturn_t irq_handler(int irq, void *dev_id);
36static int kbps2_open(struct inode *, struct file *);
37static int kbps2_release(struct inode *, struct file *);
38static ssize_t kbps2_read(struct file *, char *, size_t, loff_t *);
39static ssize_t kbps2_write(struct file *, const char *, size_t, loff_t *);
40static int __init kbps2_init(void);
41static void __exit kbps2_exit(void);
42
43/* Structure that declares the usual file */
44/* access functions */
45struct file_operations fops = {
46  .owner = THIS_MODULE,
47  .read = kbps2_read,
48  .write = kbps2_write,
49  .open = kbps2_open,
50  .release = kbps2_release
51};
52
53/* Declaration of the init and exit functions */
54module_init(kbps2_init);
55module_exit(kbps2_exit);
56
57/* Global variables of the driver */
58static int irq_enabled = 0;
59static int is_device_open = 0; /* Is device open? Used to prevent multiple access to device */
60static int Major=65; /* Major number */
61static DECLARE_WAIT_QUEUE_HEAD(wq);
62static void __iomem *ioaddress;
63static unsigned int interrupt_counter = 0;
64
65static int __init kbps2_init(void)
66{
67  int res, irq, result;
68  
69  barrier();
70  
71  printk(KERN_INFO "FPGA module is Up.\n");
72  interrupt_counter = 0;
73  
74  /* Registering device */
75  result = register_chrdev(Major, DEVICE_NAME, &fops);
76  if (result < 0) {
77      printk("<1>memory: cannot obtain major number %d\n", Major);
78    return result;
79  }
80  
81  /* Set up the FGPA irq line */
82  irq = gpio_to_irq(FPGA_IRQ_PIN);
83  res = request_irq(irq, irq_handler, IRQF_DISABLED | IRQF_TRIGGER_RISING, "FPGA - IRQ", NULL); // IRQF_TRIGGER_FALLING
84  irq_enabled = 1;
85  printk(KERN_INFO "FPGA irq_enabled...\n");
86  
87  /* Set GPIOB26 as part of External Memory Controller*/
88  jz_gpio_set_function (FPGA_CS, JZ_GPIO_FUNC_NONE);
89  /* Use ioremap to get a handle on our region */
90  ioaddress = __ioremap(FPGA_BASE_BEGIN, FPGA_BASE_END - FPGA_BASE_BEGIN, _CACHE_UNCACHED);
91
92  return 0;
93}
94
95static void __exit kbps2_exit(void)
96{
97// int ret;
98  /*Tho order for free_irq, iounmap & unregister is very important */
99  free_irq(FPGA_IRQ_PIN, NULL);
100  __iounmap(ioaddress);
101  unregister_chrdev(Major, DEVICE_NAME);
102  printk(KERN_INFO "FPGA driver is down...\n");
103}
104
105static irqreturn_t irq_handler(int irq, void *dev_id)
106{
107  unsigned int red;
108  if(irq_enabled)
109  {
110    interrupt_counter++;
111    printk(KERN_INFO "interrupt_counter=%d\n",interrupt_counter);
112    red=ioread32(ioaddress)& 0XFF;
113    rmb();
114    printk("%X \n", red);
115    wake_up_interruptible(&wq);
116  }
117  
118  return IRQ_HANDLED;
119}
120
121static int kbps2_open(struct inode *inode, struct file *file)
122{
123  if (is_device_open)
124    return -EBUSY;
125
126  is_device_open = 1;
127
128  try_module_get(THIS_MODULE);
129
130  return SUCCESS;
131}
132
133static int kbps2_release(struct inode *inode, struct file *file)
134{
135  is_device_open = 0;
136
137  module_put(THIS_MODULE);
138
139  return 0;
140}
141
142static ssize_t kbps2_read(struct file *filp, char *buffer, size_t count, loff_t *offset)
143{
144  unsigned int red;
145   
146  printk(KERN_INFO "read______-_-\n");
147  red=ioread32(ioaddress)& 0XFF;
148  rmb();
149  printk("%X\n", red);
150  return 0;
151}
152
153static ssize_t kbps2_write(struct file *filp, const char *buff, size_t count, loff_t * off)
154{
155
156  printk(KERN_INFO "write______-_-\n");
157  printk("%X\n", buff[0]);
158  iowrite32(buff[0],ioaddress);
159  wmb();
160  
161  return 1;
162}
163

Archive Download this file

Branches:
master



interactive