Jorge,
O jeito mais fácil de compilar o módulo é com o make...
Abaixo um exemplo do Makefile:
#---Inicio--------Makefile----------------------------------------------------
TARGET = memory
OBJS = memory.o
MDIR = drivers/misc
EXTRA_CFLAGS = -DEXPORT_SYMTAB
CURRENT = $(shell uname -r)
KDIR = /lib/modules/$(CURRENT)/build
PWD = $(shell pwd)
DEST = /lib/modules/$(CURRENT)/kernel/$(MDIR)
obj-m := $(TARGET).o
default:
make -C $(KDIR) M=$(PWD) modules
$(TARGET).o: $(OBJS)
$(LD) $(LD_RFLAG) -r -o $@ $(OBJS)
ifneq (,$(findstring 2.4.,$(CURRENT)))
install:
su -c "cp -v $(TARGET).o $(DEST) && /sbin/depmod -a"
else
install:
su -c "cp -v $(TARGET).ko $(DEST) && /sbin/depmod -a"
endif
clean:
-rm -f *.o *.ko .*.cmd .*.flags *.mod.c
-include $(KDIR)/Rules.make
#---Fim-----------Makefile----------------------------------------------------
REMOVA o #include <linux/config.h>
Apenas testei a compilação no meu kernel 2.6.38.4 e funcionou (mas gerou
advertências).
$ make
make -C /lib/modules/2.6.38.4/build M=/home/andre/projects/linux/memory
modules
make[1]: Entering directory `/usr/src/linux-2.6.38'
CC [M] /home/andre/projects/linux/memory/memory.o
/home/andre/projects/linux/memory/memory.c:29:2: warning: initialization
from incompatible pointer type
/home/andre/projects/linux/memory/memory.c: In function 'memory_write':
/home/andre/projects/linux/memory/memory.c:135:16: warning: ignoring return
value of 'copy_from_user', declared with attribute warn_unused_result
/home/andre/projects/linux/memory/memory.c: In function 'memory_read':
/home/andre/projects/linux/memory/memory.c:114:14: warning: ignoring return
value of 'copy_to_user', declared with attribute warn_unused_result
Building modules, stage 2.
MODPOST 1 modules
WARNING: "memory_release" [/home/andre/projects/linux/memory/memory.ko]
undefined!
CC /home/andre/projects/linux/memory/memory.mod.o
LD [M] /home/andre/projects/linux/memory/memory.ko
make[1]: Leaving directory `/usr/src/linux-2.6.38'
2011/8/28 Jorge Barros de Abreu <[email protected]>
> On Saturday 27 August 2011 21:07:31 André Barboza wrote:
> > Qual a versão do kernel?
>
> 2.6.37.6
>
> > Qual erro?
>
> gcc memory.c
> memory.c:2:24: fatal error: linux/init.h: Arquivo ou diretório não
> encontrado
> compilation terminated.
>
>
> gcc paralelport.c
> paralelport.c:2:24: fatal error: linux/init.h: Arquivo ou diretório não
> encontrado
> compilation terminated.
>
> Os fontes do kernel estão instalados em /usr/src/linux.
>
>
> >
> > 2011/8/27 Jorge Barros de Abreu <[email protected]>
> >
> > > Olá.
> > >
> > > Preciso de ajuda com esse tutorial. O título é:
> > >
> > > Writing device drivers in Linux: A brief tutorial
> > >
> > > O link é:
> > >
> > > http://www.freesoftwaremagazine.com/node/1238/pdf
> > >
> > > Não consegui fazer a compilação do memory.c e nem do paralelport.c
> > >
> > > nem com gcc nem com makefile.
> > >
> > > memory.c
> > > ###################
> > > /* Necessary includes for device drivers */
> > > #include <linux/init.h>
> > > #include <linux/config.h>
> > > #include <linux/module.h>
> > > #include <linux/kernel.h> /* printk() */
> > > #include <linux/slab.h> /* kmalloc() */
> > > #include <linux/fs.h> /* everything... */
> > > #include <linux/errno.h> /* error codes */
> > > #include <linux/types.h> /* size_t */
> > > #include <linux/proc_fs.h>
> > > #include <linux/fcntl.h> /* O_ACCMODE */
> > > #include <asm/system.h> /* cli(), *_flags */
> > > #include <asm/uaccess.h> /* copy_from/to_user */
> > >
> > > MODULE_LICENSE("Dual BSD/GPL");
> > >
> > > /* Declaration of memory.c functions */
> > > int memory_open(struct inode *inode, struct file *filp);
> > > int memory_release(struct inode *inode, struct file *filp);
> > > ssize_t memory_read(struct file *filp, char *buf, size_t count, loff_t
> > > *f_pos);
> > > ssize_t memory_write(struct file *filp, char *buf, size_t count, loff_t
> > > *f_pos);
> > > void memory_exit(void);
> > > int memory_init(void);
> > >
> > > /* Structure that declares the usual file */
> > > /* access functions */
> > > struct file_operations memory_fops = {
> > >
> > > read: memory_read,
> > > write: memory_write,
> > > open: memory_open,
> > > release: memory_release
> > >
> > > };
> > >
> > > /* Declaration of the init and exit functions */
> > > module_init(memory_init);
> > > module_exit(memory_exit);
> > >
> > > /* Global variables of the driver */
> > > /* Major number */
> > > int memory_major = 60;
> > > /* Buffer to store data */
> > > char *memory_buffer;
> > >
> > >
> > >
> > >
> > >
> > >
> > > int memory_init(void) {
> > >
> > > int result;
> > >
> > > /* Registering device */
> > > result = register_chrdev(memory_major, "memory", &memory_fops);
> > > if (result < 0) {
> > >
> > > printk(
> > >
> > > "<1>memory: cannot obtain major number %d\n", memory_major);
> > >
> > > return result;
> > >
> > > }
> > >
> > > /* Allocating memory for the buffer */
> > > memory_buffer = kmalloc(1, GFP_KERNEL);
> > > if (!memory_buffer) {
> > >
> > > result = -ENOMEM;
> > > goto fail;
> > >
> > > }
> > > memset(memory_buffer, 0, 1);
> > >
> > > printk("<1>Inserting memory module\n");
> > > return 0;
> > >
> > > fail:
> > > memory_exit();
> > > return result;
> > >
> > > }
> > >
> > >
> > >
> > >
> > >
> > >
> > > void memory_exit(void) {
> > >
> > > /* Freeing the major number */
> > > unregister_chrdev(memory_major, "memory");
> > >
> > > /* Freeing buffer memory */
> > > if (memory_buffer) {
> > >
> > > kfree(memory_buffer);
> > >
> > > }
> > >
> > > printk("<1>Removing memory module\n");
> > >
> > > }
> > >
> > >
> > >
> > >
> > >
> > >
> > > int memory_open(struct inode *inode, struct file *filp) {
> > >
> > > /* Success */
> > > return 0;
> > >
> > > }
> > >
> > >
> > >
> > >
> > >
> > >
> > > ssize_t memory_read(struct file *filp, char *buf,
> > >
> > > size_t count, loff_t *f_pos) {
> > >
> > > /* Transfering data to user space */
> > > copy_to_user(buf,memory_buffer,1);
> > >
> > > /* Changing reading position as best suits */
> > > if (*f_pos == 0) {
> > >
> > > *f_pos+=1;
> > > return 1;
> > >
> > > } else {
> > >
> > > return 0;
> > >
> > > }
> > >
> > > }
> > >
> > >
> > >
> > >
> > >
> > > ssize_t memory_write( struct file *filp, char *buf,
> > >
> > > size_t count, loff_t *f_pos) {
> > >
> > > char *tmp;
> > >
> > > tmp=buf+count-1;
> > > copy_from_user(memory_buffer,tmp,1);
> > > return 1;
> > >
> > > }
> > >
> > >
> > >
> > > Paralelport.c
> > > #################
> > > /* Necessary includes for drivers */
> > > #include <linux/init.h>
> > > #include <linux/config.h>
> > > #include <linux/module.h>
> > > #include <linux/kernel.h> /* printk() */
> > > #include <linux/slab.h> /* kmalloc() */
> > > #include <linux/fs.h> /* everything... */
> > > #include <linux/errno.h> /* error codes */
> > > #include <linux/types.h> /* size_t */
> > > #include <linux/proc_fs.h>
> > > #include <linux/fcntl.h> /* O_ACCMODE */
> > > #include <linux/ioport.h>
> > > #include <asm/system.h> /* cli(), *_flags */
> > > #include <asm/uaccess.h> /* copy_from/to_user */
> > > #include <asm/io.h> /* inb, outb */
> > >
> > > MODULE_LICENSE("Dual BSD/GPL");
> > >
> > > /* Function declaration of parlelport.c */
> > > int parlelport_open(struct inode *inode, struct file *filp);
> > > int parlelport_release(struct inode *inode, struct file *filp);
> > > ssize_t parlelport_read(struct file *filp, char *buf,
> > >
> > > size_t count, loff_t *f_pos);
> > >
> > > ssize_t parlelport_write(struct file *filp, char *buf,
> > >
> > > size_t count, loff_t *f_pos);
> > >
> > > void parlelport_exit(void);
> > > int parlelport_init(void);
> > >
> > > /* Structure that declares the common */
> > > /* file access fcuntions */
> > > struct file_operations parlelport_fops = {
> > >
> > > read: parlelport_read,
> > > write: parlelport_write,
> > > open: parlelport_open,
> > > release: parlelport_release
> > >
> > > };
> > >
> > > /* Driver global variables */
> > > /* Major number */
> > > int parlelport_major = 61;
> > >
> > > /* Control variable for memory */
> > > /* reservation of the parallel port*/
> > > int port;
> > >
> > > module_init(parlelport_init);
> > > module_exit(parlelport_exit);
> > >
> > >
> > >
> > >
> > > int parlelport_init(void) {
> > >
> > > int result;
> > > /* Registering device */
> > > result = register_chrdev(parlelport_major, "parlelport",
> > >
> > > &parlelport_fops);
> > >
> > > if (result < 0) {
> > >
> > > printk(
> > >
> > > "<1>parlelport: cannot obtain major number %d\n",
> > > parlelport_major);
> > >
> > > return result;
> > >
> > > }
> > > <parlelport modified init module>
> > > printk("<1>Inserting parlelport module\n");
> > > return 0;
> > >
> > > fail:
> > > parlelport_exit();
> > > return result;
> > >
> > > }
> > >
> > >
> > >
> > >
> > >
> > > void parlelport_exit(void) {
> > >
> > > /* Make major number free! */
> > > unregister_chrdev(parlelport_major, "parlelport");
> > > <parlelport modified exit module>
> > > printk("<1>Removing parlelport module\n");
> > >
> > > }
> > >
> > >
> > >
> > >
> > >
> > >
> > > int parlelport_open(struct inode *inode, struct file *filp) {
> > >
> > > /* Success */
> > > return 0;
> > >
> > > }
> > >
> > >
> > >
> > >
> > >
> > > int parlelport_release(struct inode *inode, struct file *filp) {
> > >
> > > /* Success */
> > > return 0;
> > >
> > > }
> > >
> > >
> > >
> > >
> > >
> > > ssize_t parlelport_read(struct file *filp, char *buf,
> > >
> > > size_t count, loff_t *f_pos) {
> > > /* Buffer to read the device */
> > > char parlelport_buffer;
> > > <parlelport inport>
> > > /* We transfer data to user space */
> > > copy_to_user(buf,&parlelport_buffer,1);
> > > /* We change the reading position as best suits */
> > > if (*f_pos == 0) {
> > >
> > > *f_pos+=1;
> > > return 1;
> > >
> > > } else {
> > >
> > > return 0;
> > >
> > > }
> > >
> > > }
> > >
> > >
> > >
> > >
> > >
> > >
> > > ssize_t parlelport_write( struct file *filp, char *buf,
> > > size_t count, loff_t *f_pos) {
> > >
> > > char *tmp;
> > > /* Buffer writing to the device */
> > > char parlelport_buffer;
> > >
> > > }
> > >
> > >
> > >
> > >
> > >
> > > tmp=buf+count-1;
> > > copy_from_user(&parlelport_buffer,tmp,1);
> > >
> > > <parlelport outport>
> > > return 1;
> > >
> > > }
> > >
> > >
> > >
> > > --
> > > Data Estelar 2455801,305336
> > > http://sites.google.com/site/ficmatinf
> > > Desejo-lhe Paz, Vida Longa e Prosperidade.
> > > São Bem Vindas Mensagens no Formato texto UTF-8 com Acentos.
> > >
> > > --
> > > GUS-BR - Grupo de Usuários de Slackware Brasil
> > > http://www.slackwarebrasil.org/
> > > http://groups.google.com/group/slack-users-br
> > >
> > > Antes de perguntar:
> > > http://www.istf.com.br/perguntas/
> > >
> > > Para sair da lista envie um e-mail para:
> > > [email protected]
>
> --
> Data Estelar 2455802,425509
> http://sites.google.com/site/ficmatinf
> Desejo-lhe Paz, Vida Longa e Prosperidade.
> São Bem Vindas Mensagens no Formato texto UTF-8 com Acentos.
>
--
GUS-BR - Grupo de Usuários de Slackware Brasil
http://www.slackwarebrasil.org/
http://groups.google.com/group/slack-users-br
Antes de perguntar:
http://www.istf.com.br/perguntas/
Para sair da lista envie um e-mail para:
[email protected]