public
Description: Sample kernel code / Código de exemplo do kernel
Homepage: http://br.kernelnewbies.org/
Clone URL: git://github.com/ehabkost/kernel-samples.git
kernel-samples / chardev-mostrador.c
100644 77 lines (60 sloc) 1.487 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/miscdevice.h>
 
#include <asm/io.h>
 
MODULE_AUTHOR("Eduardo Habkost");
MODULE_LICENSE("GPL");
 
#define PORT_ADDRESS 120
#define PORT_DATA_ADDRESS 124
#define ID_FISL_DEV 0xdead
 
#define FISL_INICIALIZAR 1
#define FISL_DESLIGA 2
#define FISL_BUFFER_PRONTO 0xff
 
 
static ssize_t mostrador_write(struct file *f, const char __user *buf, size_t len, loff_t *loff)
{
char c, i;
for (i = 0 ; i < len; i++) {
if (get_user(c, buf + i))
return -EFAULT;
 
if (c == '\n')
outl(FISL_BUFFER_PRONTO, PORT_ADDRESS);
else
outl(c, PORT_DATA_ADDRESS);
}
return len;
}
 
static const struct file_operations mostrador_fops = {
.owner = THIS_MODULE,
.write = mostrador_write,
};
 
static struct miscdevice mostrador_dev = {
.minor = MISC_DYNAMIC_MINOR,
.name = "mostrador",
.fops = &mostrador_fops,
};
 
static int __init cdev_init(void)
{
u16 data;
int r;
 
printk("Procurando dispositivo na porta %d\n", PORT_ADDRESS);
data = inl(PORT_ADDRESS);
if (data != ID_FISL_DEV) {
printk("Dispositivo FISL nao encontrado\n");
return -ENODEV;
}
 
outl(FISL_INICIALIZAR, PORT_ADDRESS);
printk("Dispositivo inicializado\n");
 
r = misc_register(&mostrador_dev);
if (r < 0)
return r;
 
return 0;
}
 
static void __exit cdev_exit(void)
{
misc_deregister(&mostrador_dev);
outl(FISL_DESLIGA, PORT_ADDRESS);
printk("Dispositivo FISL desligado\n");
}
 
module_init(cdev_init);
module_exit(cdev_exit);