- 论坛徽章:
- 0
|
我在Redhat9 下内核2.4-20.8 , Linux 下 PowerPC 交叉环境安装在/opt/crosstool/ 目录下, 我现在编译我的第一个字符驱动,源代码为网上下载:
chardev.c 的源代码如下:
#include <linux/kernel.h>
#include <linux/module.h>
#include <asm/uaccess.h>
/*#include <asm-ppc/uaccess.h>*/
#include <linux/fs.h>
#include <linux/ioctl.h>
/*
* Prototypes - this would normally go in a .h file*/
//int init_module(void);
//void cleanup_module(void);
static int device_open(struct inode *, struct file *);
static int device_release(struct inode *,struct file *);
static ssize_t device_read(struct file *, char * , size_t, loff_t *);
static ssize_t device_write(struct file *, const char *, size_t, loff_t *);
#define SUCCESS 0
#define DEVICE_NAME "chardev" /* Dev name as it appears in /proc/devices */
#define BUF_LEN 80 /* Max length of the message from the device */
/*
* Global variables are declared as static, so are global within the file.
*/
static int Major; /* Major number assigned to our device driver */
static int Device_Open = 0; /* Is device open ?
* Used to prevernt multiple access to device */
static char msg[BUF_LEN]; /* The msg the device will give when asked */
static char *msg_Ptr;
static struct file_operations fops = {
.read = device_read,
.write = device_write,
.open = device_open,
.release = device_release
};
//MODULE_LICENCE("dyt/GPL");
/*
* This function is called when the module is loaded
*/
int __init init_module(void)
{
Major = register_chrdev(251,DEVICE_NAME, &fops);
if (Major < 0){
printk(KERN_ALERT "Registering char device failed with %d\n",Major);
return Major;
}
printk(KERN_INFO "I was assigned major number %d. To talk to \n",Major);
printk(KERN_INFO "the driver,create a dev file with\n");
printk(KERN_INFO "'mknod /dev/%s c %d 0' .\n",DEVICE_NAME,Major);
printk(KERN_INFO "Try various minor numbers.Try to cat and echo to \n");
printk(KERN_INFO "the device file .\n");
printk(KERN_INFO "Remove the device file and module when done. \n");
return SUCCESS;
}
/*
* This function is called when the module is unloaded
*/
void __exit cleanup_module(void)
{
/*
* Unregister the device
*/
/*int ret = unregister_chrdev(Major, DEVICE_NAME);
if (ret < 0)
printk(KERN_ALERT "Error in unregister_chrdev:%d\n",ret);*/
printk(KERN_ALERT,"unregister_chrdev:%s\n",DEVICE_NAME);
unregister_chrdev(Major, DEVICE_NAME);
}
/*
* Methods
*/
/*
* Called when a process tries to open the device file, like
* 'cat /dev/mycharfile'
*/
static int device_open(struct inode * inode, struct file *file)
{
static int counter = 0;
if (Device_Open)
return -EBUSY;
Device_Open++;
sprintf(msg, "I alread told you %d times Hello world!\n", counter++);
msg_Ptr = msg;
//try_module_get(THIS_MODULE);
return SUCCESS;
}
/*
* Called when a process closes the device file.
*/
static int device_release(struct inode * inode, struct file *file)
{
Device_Open--; /* We're now read for our next caller */
/*
* Decrement the usage count, or else once you opened the file, you'll
* never get rid of the module.
*/
//module_put(THIS_MODULE);
return 0;
}
/* Called when a process, which already opened the dev file,attempts to
* read from it
*/
static ssize_t device_read(struct file * filp, /* see include/linux/fs.h*/
char * buffer, /* buffer to fill with data*/
size_t length, /* length of the buffer */
loff_t * offset)
{
/*
* Number of bytes actually written to the buffer
*/
int bytes_read = 0;
/*
* If we're at the end of the message,
* return 0 signifying end of file
*/
if(*msg_Ptr == 0)
return 0;
/*
* Actually put te data into the buffer
*/
while (length && * msg_Ptr){
/*
* The buffer is in the user data segment,not the kernel
* segment so "*" assignment won't work.We have to use
* put_user which copies data from the kernel data segment to
* the user data segment
*/
put_user(* (msg_Ptr++), buffer++);
length--;
bytes_read++;
}
/*
* Most read functions return the number of bytes put into the buffer
*/
return bytes_read;
}
/*
*Called when a process writes to dev file; echo "hi" > /dev/hello
*/
static ssize_t device_write(struct file * filp, const char * buff, size_t len, loff_t * off)
{
printk(KERN_ALERT "Sorry, this operation isn't supported.\n");
return -EINVAL;
}
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>问题>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
用gcc -D__KERNEL__ -DMODULE -I/usr/src/linux-2.4.20-8/include -Wall -c -o chardev.o chardev.c 编译没问题 (x86 环境下的没问题),可以产生chardev.o 文件
但是我想在PPC下编译, 用命令: powerpc-603-linux-gnu-gcc -D__KERNEL__ -DMODULE -I/usr/src/linux-2.4.20-8/include -Wall -c -o chardev.o chardev.c
编译不过去, 许多错误,
是否我用的头文件不对,应该用: -I/opt/crosstool/gcc-4.1.0-glibc-2.3.6/powerpc-603-linux-gnu/powerpc-603-linux-gnu/include 呢
还是把 chardev.c 里面的 #include <asm/uaccess.h> 改成 #include <asm-ppc/uaccess.h> , 我都试过了一堆编译错误!
错误如下:
powerpc-603-linux-gnu-gcc -DMODULE -D__KERNEL__ -I/usr/src/linux-2.4.20-8/include -Wall -c chardev.c
In file included from /usr/src/linux-2.4.20-8/include/linux/kernel.h:15,
from chardev.c:1:
/usr/src/linux-2.4.20-8/include/asm/byteorder.h:14: warning: type qualifiers ignored on function return type
/usr/src/linux-2.4.20-8/include/asm/byteorder.h:28: warning: type qualifiers ignored on function return type
In file included from /usr/src/linux-2.4.20-8/include/linux/byteorder/little_endian.h:11,
from /usr/src/linux-2.4.20-8/include/asm/byteorder.h:45,
from /usr/src/linux-2.4.20-8/include/linux/kernel.h:15,
from chardev.c:1:
/usr/src/linux-2.4.20-8/include/linux/byteorder/swab.h:160: warning: type qualifiers ignored on function return type
/usr/src/linux-2.4.20-8/include/linux/byteorder/swab.h:173: warning: type qualifiers ignored on function return type
/usr/src/linux-2.4.20-8/include/linux/byteorder/swab.h:186: warning: type qualifiers ignored on function return type
/usr/src/linux-2.4.20-8/include/linux/byteorder/swab.h:200: warning: type qualifiers ignored on function return type
In file included from /usr/src/linux-2.4.20-8/include/linux/prefetch.h:13,
from /usr/src/linux-2.4.20-8/include/linux/list.h:6,
from /usr/src/linux-2.4.20-8/include/linux/module.h:12,
from chardev.c:2:
/usr/src/linux-2.4.20-8/include/asm/processor.h:83: error: array type has incomplete element type
In file included from /usr/src/linux-2.4.20-8/include/linux/fs.h:319,
from /usr/src/linux-2.4.20-8/include/linux/capability.h:17,
from /usr/src/linux-2.4.20-8/include/linux/binfmts.h:4,
from /usr/src/linux-2.4.20-8/include/linux/sched.h:10,
from /usr/src/linux-2.4.20-8/include/asm/uaccess.h:8,
from chardev.c:3:
/usr/src/linux-2.4.20-8/include/linux/ncp_fs_i.h:26: warning: 'packed' attribute ignored for field of type '__u8'
/usr/src/linux-2.4.20-8/include/linux/ncp_fs_i.h:27: warning: 'packed' attribute ignored for field of type '__u8[5u]'
In file included from /usr/src/linux-2.4.20-8/include/linux/ncp_mount.h:12,
from /usr/src/linux-2.4.20-8/include/linux/ncp_fs_sb.h:12,
from /usr/src/linux-2.4.20-8/include/linux/fs.h:733,
from /usr/src/linux-2.4.20-8/include/linux/capability.h:17,
from /usr/src/linux-2.4.20-8/include/linux/binfmts.h:4,
from /usr/src/linux-2.4.20-8/include/linux/sched.h:10,
from /usr/src/linux-2.4.20-8/include/asm/uaccess.h:8,
from chardev.c:3:
/usr/src/linux-2.4.20-8/include/linux/ncp.h:24: warning: 'packed' attribute ignored for field of type '__u8'
/usr/src/linux-2.4.20-8/include/linux/ncp.h:25: warning: 'packed' attribute ignored for field of type '__u8'
/usr/src/linux-2.4.20-8/include/linux/ncp.h:26: warning: 'packed' attribute ignored for field of type '__u8'
/usr/src/linux-2.4.20-8/include/linux/ncp.h:27: warning: 'packed' attribute ignored for field of type '__u8'
/usr/src/linux-2.4.20-8/include/linux/ncp.h:28: warning: 'packed' attribute ignored for field of type '__u8'
/usr/src/linux-2.4.20-8/include/linux/ncp.h:29: warning: 'packed' attribute ignored for field of type '__u8[]'
/usr/src/linux-2.4.20-8/include/linux/ncp.h:37: warning: 'packed' attribute ignored for field of type '__u8'
/usr/src/linux-2.4.20-8/include/linux/ncp.h:38: warning: 'packed' attribute ignored for field of type '__u8'
/usr/src/linux-2.4.20-8/include/linux/ncp.h:39: warning: 'packed' attribute ignored for field of type '__u8'
/usr/src/linux-2.4.20-8/include/linux/ncp.h:40: warning: 'packed' attribute ignored for field of type '__u8'
/usr/src/linux-2.4.20-8/include/linux/ncp.h:41: warning: 'packed' attribute ignored for field of type '__u8'
/usr/src/linux-2.4.20-8/include/linux/ncp.h:42: warning: 'packed' attribute ignored for field of type '__u8'
/usr/src/linux-2.4.20-8/include/linux/ncp.h:43: warning: 'packed' attribute ignored for field of type '__u8[]'
/usr/src/linux-2.4.20-8/include/linux/ncp.h:137: warning: 'packed' attribute ignored for field of type '__u8'
/usr/src/linux-2.4.20-8/include/linux/ncp.h:138: warning: 'packed' attribute ignored for field of type '__u8[255u]'
/usr/src/linux-2.4.20-8/include/linux/ncp.h:174: warning: 'packed' attribute ignored for field of type '__u8'
chardev.c: In function 'cleanup_module':
chardev.c:67: warning: too many arguments for format
chardev.c:153:31: warning: no newline at end of file
make: *** [chardev.o] Error 1
[ 本帖最后由 nflx 于 2009-4-16 21:54 编辑 ] |
|