免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 4425 | 回复: 16
打印 上一主题 下一主题

Linux 下 PowerPC 交叉环境如何编译字符驱动!! [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-04-16 17:58 |只看该作者 |倒序浏览
我在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 编辑 ]

论坛徽章:
5
2 [报告]
发表于 2009-04-16 20:38 |只看该作者
原帖由 nflx 于 2009/4/16 17:58 发表
还是把 chardev.c 里面的 #include <asm/uaccess.h> 改成 #include <asm-ppc/uaccess.h>  , 我都试过了一堆编译错误!



asm应该是个软链接,指向asm-ppc,所以你这是一样的。

论坛徽章:
5
3 [报告]
发表于 2009-04-16 20:40 |只看该作者
初步估计是Driver和Kernel版本不匹配。
请下载适当的Kernel版本再试。

论坛徽章:
0
4 [报告]
发表于 2009-04-16 21:00 |只看该作者
原帖由 yidou 于 2009-4-16 20:40 发表
初步估计是Driver和Kernel版本不匹配。
请下载适当的Kernel版本再试。


Driver和Kernel版本不匹配?  我的Driver自已写的,没有定议版本啊? 内核版本是2.4.20-8,  关键是用同样的内核,我不用交叉编译器(直接用gcc) 可以编译成功啊

[ 本帖最后由 nflx 于 2009-4-16 21:12 编辑 ]

论坛徽章:
5
摩羯座
日期:2014-07-22 09:03:552015元宵节徽章
日期:2015-03-06 15:50:392015亚冠之大阪钢巴
日期:2015-06-12 16:01:352015年中国系统架构师大会
日期:2015-06-29 16:11:2815-16赛季CBA联赛之四川
日期:2018-12-17 14:10:21
5 [报告]
发表于 2009-04-16 23:53 |只看该作者
头文件指错了
应该指向你的交叉编译的kernel的头文件处

论坛徽章:
0
6 [报告]
发表于 2009-04-17 08:35 |只看该作者
原帖由 T-bagwell 于 2009-4-16 23:53 发表
头文件指错了
应该指向你的交叉编译的kernel的头文件处

我的Makefile
#CC= gcc
CC= powerpc-603-linux-gnu-gcc
CCFLAG= -c
#MODCFLAGS= -DMODULE -D__KERNEL__   -I/usr/src/linux-2.4.20-8/include  -Wall
MODCFLAGS= -DMODULE -D__KERNEL__   -I/opt/crosstool/gcc-3.2.3-glibc-2.3.2/powerpc-603-linux-gnu/powerpc-603-linux-gnu/include  -Wall
chardev.o: chardev.c
        $(CC)  $(MODCFLAGS)  $(CCFLAG) chardev.c
clean:
        rm -f *.o

编译还是出现下列错误:
owerpc-603-linux-gnu-gcc  -DMODULE -D__KERNEL__   -I/opt/crosstool/gcc-3.2.3-glibc-2.3.2/powerpc-603-linux-gnu/powerpc-603-linux-gnu/include -Wall  -c chardev.c
In file included from /opt/crosstool/gcc-3.2.3-glibc-2.3.2/powerpc-603-linux-gnu/powerpc-603-linux-gnu/sys-include/linux/linkage.h:4,
                 from /opt/crosstool/gcc-3.2.3-glibc-2.3.2/powerpc-603-linux-gnu/powerpc-603-linux-gnu/sys-include/linux/kernel.h:11,
                 from chardev.c:1:
/opt/crosstool/gcc-3.2.3-glibc-2.3.2/powerpc-603-linux-gnu/powerpc-603-linux-gnu/sys-include/linux/config.h:4:28: linux/autoconf.h: No such file or directory
In file included from /opt/crosstool/gcc-3.2.3-glibc-2.3.2/powerpc-603-linux-gnu/powerpc-603-linux-gnu/sys-include/linux/module.h:10,
                 from chardev.c:2:
/opt/crosstool/gcc-3.2.3-glibc-2.3.2/powerpc-603-linux-gnu/powerpc-603-linux-gnu/sys-include/linux/sched.h: In function `arch_pick_mmap_layout':
/opt/crosstool/gcc-3.2.3-glibc-2.3.2/powerpc-603-linux-gnu/powerpc-603-linux-gnu/sys-include/linux/sched.h:1021: `CONFIG_TASK_SIZE' undeclared (first use in this function)
/opt/crosstool/gcc-3.2.3-glibc-2.3.2/powerpc-603-linux-gnu/powerpc-603-linux-gnu/sys-include/linux/sched.h:1021: (Each undeclared identifier is reported only once
/opt/crosstool/gcc-3.2.3-glibc-2.3.2/powerpc-603-linux-gnu/powerpc-603-linux-gnu/sys-include/linux/sched.h:1021: for each function it appears in.)
In file included from /opt/crosstool/gcc-3.2.3-glibc-2.3.2/powerpc-603-linux-gnu/powerpc-603-linux-gnu/sys-include/asm/machdep.h:8,
                 from /opt/crosstool/gcc-3.2.3-glibc-2.3.2/powerpc-603-linux-gnu/powerpc-603-linux-gnu/sys-include/asm/irq.h:6,
                 from /opt/crosstool/gcc-3.2.3-glibc-2.3.2/powerpc-603-linux-gnu/powerpc-603-linux-gnu/sys-include/asm/hardirq.h:8,
                 from /opt/crosstool/gcc-3.2.3-glibc-2.3.2/powerpc-603-linux-gnu/powerpc-603-linux-gnu/sys-include/linux/hardirq.h:6,
                 from /opt/crosstool/gcc-3.2.3-glibc-2.3.2/powerpc-603-linux-gnu/powerpc-603-linux-gnu/sys-include/asm-generic/local.h:6,
                 from /opt/crosstool/gcc-3.2.3-glibc-2.3.2/powerpc-603-linux-gnu/powerpc-603-linux-gnu/sys-include/asm/local.h:4,
                 from /opt/crosstool/gcc-3.2.3-glibc-2.3.2/powerpc-603-linux-gnu/powerpc-603-linux-gnu/sys-include/linux/module.h:21,
                 from chardev.c:2:
/opt/crosstool/gcc-3.2.3-glibc-2.3.2/powerpc-603-linux-gnu/powerpc-603-linux-gnu/sys-include/asm/setup.h:8:28: asm-m68k/setup.h: No such file or directory
In file included from chardev.c:3:
/opt/crosstool/gcc-3.2.3-glibc-2.3.2/powerpc-603-linux-gnu/powerpc-603-linux-gnu/sys-include/asm/uaccess.h: In function `copy_from_user':
/opt/crosstool/gcc-3.2.3-glibc-2.3.2/powerpc-603-linux-gnu/powerpc-603-linux-gnu/sys-include/asm/uaccess.h:309: `CONFIG_TASK_SIZE' undeclared (first use in this function)
/opt/crosstool/gcc-3.2.3-glibc-2.3.2/powerpc-603-linux-gnu/powerpc-603-linux-gnu/sys-include/asm/uaccess.h: In function `copy_to_user':
/opt/crosstool/gcc-3.2.3-glibc-2.3.2/powerpc-603-linux-gnu/powerpc-603-linux-gnu/sys-include/asm/uaccess.h:323: `CONFIG_TASK_SIZE' undeclared (first use in this function)
/opt/crosstool/gcc-3.2.3-glibc-2.3.2/powerpc-603-linux-gnu/powerpc-603-linux-gnu/sys-include/asm/uaccess.h: In function `clear_user':
/opt/crosstool/gcc-3.2.3-glibc-2.3.2/powerpc-603-linux-gnu/powerpc-603-linux-gnu/sys-include/asm/uaccess.h:344: `CONFIG_TASK_SIZE' undeclared (first use in this function)
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-17 09:37 编辑 ]

论坛徽章:
5
7 [报告]
发表于 2009-04-17 08:51 |只看该作者
原帖由 nflx 于 2009/4/17 08:35 发表
/opt/crosstool/gcc-3.2.3-glibc-2.3.2/powerpc-603-linux-gnu/powerpc-603-linux-gnu/sys-include/asm/setup.h:8:28: asm-m68k/setup.h: No such file or directory


怎么是asm-m68k/setup.h

论坛徽章:
0
8 [报告]
发表于 2009-04-17 09:24 |只看该作者
原帖由 yidou 于 2009-4-17 08:51 发表


怎么是asm-m68k/setup.h


我就是这样编译的啊
#CC= gcc
CC= powerpc-603-linux-gnu-gcc
CCFLAG= -c
#MODCFLAGS= -DMODULE -D__KERNEL__   -I/usr/src/linux-2.4.20-8/include  -Wall
MODCFLAGS= -DMODULE -D__KERNEL__   -I/opt/crosstool/gcc-3.2.3-glibc-2.3.2/powerpc-603-linux-gnu/powerpc-603-linux-gnu/include  -Wall
chardev.o: chardev.c
        $(CC)  $(MODCFLAGS)  $(CCFLAG) chardev.c
clean:
        rm -f *.o

交叉编译时,内核的头文件就是在/opt/crosstool/gcc-3.2.3-glibc-2.3.2/powerpc-603-linux-gnu/powerpc-603-linux-gnu/include 在这个目录吧,还是我要另外解压内核到其它目录呢?(内核: linux-2.6.9.tar.bz2)

[ 本帖最后由 nflx 于 2009-4-17 09:25 编辑 ]

论坛徽章:
5
9 [报告]
发表于 2009-04-17 09:42 |只看该作者
LZ看看asm这个链接, 指向哪个目录?

哪个文件include asm/setup.h ?

论坛徽章:
0
10 [报告]
发表于 2009-04-17 09:48 |只看该作者
原帖由 yidou 于 2009-4-17 09:42 发表
LZ看看asm这个链接, 指向哪个目录?

哪个文件include asm/setup.h ?


#cd /opt/crosstool/gcc-3.2.3-glibc-2.3.2/powerpc-603-linux-gnu/powerpc-603-linux-gnu/include
#ls -al
-rw-r--r--    1 dyt      dyt          1792 Apr 16 18:18 ar.h
drwxrwxr-x    2 dyt      dyt          4096 Apr 16 18:18 arpa
drwxrwxr-x    2 dyt      dyt          4096 Apr 16 17:56 asm
drwxrwxr-x    2 dyt      dyt          4096 Apr 16 17:56 asm-generic
.......

是真实目录,没有指向其它链接, 为什么会出现从 sys-include 里面找头文件呢, 奇怪?
powerpc-603-linux-gnu-gcc  -DMODULE -D__KERNEL__   -I/opt/crosstool/gcc-3.2.3-glibc-2.3.2/powerpc-603-linux-gnu/powerpc-603-linux-gnu/include -Wall  -c chardev.c
In file included from /opt/crosstool/gcc-3.2.3-glibc-2.3.2/powerpc-603-linux-gnu/powerpc-603-linux-gnu/sys-include/linux/linkage.h:4,
                 from /opt/crosstool/gcc-3.2.3-glibc-2.3.2/powerpc-603-linux-gnu/powerpc-603-linux-gnu/sys-include/linux/kernel.h:11,
                 from chardev.c:1:
/opt/crosstool/gcc-3.2.3-glibc-2.3.2/powerpc-603-linux-gnu/powerpc-603-linux-gnu/sys-include/linux/config.h:4:28: linux/autoconf.h: No such file or directory
In file included from /opt/crosstool/gcc-3.2.3-glibc-2.3.2/powerpc-603-linux-gnu/powerpc-603-linux-gnu/sys-include/linux/module.h:10,
                 from chardev.c:2:
/opt/crosstool/gcc-3.2.3-glibc-2.3.2/powerpc-603-linux-gnu/powerpc-603-linux-gnu/sys-include/linux/sched.h: In function `arch_pick_mmap_layout':
。。。。

[ 本帖最后由 nflx 于 2009-4-17 09:50 编辑 ]
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP