LINUX驱动开发: UBUNTU下,比如我写了个.C 驱动文件。 之后我要把它放在哪里,我才能得到.ko文件

两种方法:
1,直接添加进内核。
a, 比如进入drivers/char/目录,把xxx.c文件拷贝到该目录
b, 修改drivers/char/Kconfig文件,自定义仿照其他选项,添加自己的选项XXX。
c, 修改drivers/char/Makefile文件,添加obj-$(CONFIG_XXX)   += xxx.o
d, 进入内核根目录,make menuconfig,进入device drivers->character devices在这里面找到XXX选项,配置成“M”,也就是模块编译,会生成xxx.ko
2,随便建一个目录,写Makefile,这个Makefile里面,要指定内核源码根目录,并调用内核源码的Makefile,来编译 当前目录的文件,类似一下格式,可在网上找一个Makefile来修改:
CFILES = xxx.c
DRIVER_NAME = xxxx
KSRC :=  /home/linux/linux-2.6.xxx

TARGET = $(DRIVER_NAME).ko
obj-m += $(DRIVER_NAME).o
$(DRIVER_NAME)-objs := $(CFILES:.c=.o)
default:
      $(MAKE) -C $(KSRC) SUBDIRS=$(shell pwd) modules
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
This bundle contains a modified CP210x driver for the 4.10.0 kernel (Ubuntu 17.04). It contains: - Support for the CP2102N NOTE: This driver is an example of how to perform GPIO operations within the CP210x driver since the driver on kernel.org does not support GPIO at this time. This driver has only been written and tested on the Linux 3.13.0 kernel on Ubuntu 14.04. This driver is a modified version of the existing driver in the Linux 3.13.0 kernel, which is maintained at kernel.org. It is recommened to use the driver there that matches your specific kernel version: www.kernel.org Build instrutions: Ubuntu: 1. make ( your cp210x driver ) 2. cp cp210x.ko to /lib/modules//kernel/drivers/usb/serial 3. insmod /lib/modules/<kernel-version/kernel/drivers/usb/serial/usbserial.ko 4. insmod cp210x.ko RedHat: 1. yum update kernel* //need to update the kernel first otherwise your header won't match 2. yum install kernel-devel kernel-headers //get the devel and header packages. 3. reboot //your build link should be fixed after your system come back 4. make ( your cp210x driver ) // should be able to build successfully at this point 5. cp cp210x.ko to /lib/modules//kernel/drivers/usb/serial 6a. insmod /lib/modules/<kernel-version/kernel/drivers/usb/serial/usbserial.ko 6. insmod cp210x.ko 7. sudo chmod 666 /dev/ttyUSB0 8. sudo chmod 666 /dev/ttyUSB1 GPIO example: This shows how to use the two IOCTLs to set GPIO state. Build instructions: 1. g++ cp210x_gpio_example.c -o cp210x_gpio_example 2. ./cp210x_gpio_example
好的,这是一个关于在Ubuntu 22.04.2 LTS系统下使用C语言编内核模块文件chardev.c和Makefile文件的问题,实现简单的字符设备驱动程序,能对字符设备执行打开、读、、I/O控制和关闭这些基本操作。 首先,我们需要创建一个名为chardev.c的文件,这是我们将在其中编字符设备驱动程序的文件。在此文件中,我们需要包含一些必要的头文件,如linux/fs.h、linux/kernel.h、linux/module.h和linux/uaccess.h。然后,我们需要定义一些必要的变量,如设备名称、设备号和设备文件结构体等。 接下来,我们需要实现设备驱动程序的基本操作函数,包括设备打开、设备读、设备、设备控制和设备关闭。在这些函数中,我们需要使用一些内核函数来执行相应的操作,如copy_from_user()和copy_to_user()函数来移动数据、file_operations结构体来注册设备操作函数等。 最后,我们需要创建一个Makefile文件来编译我们的内核模块。在Makefile文件中,我们需要定义一些编译规则,如编译选项、目标文件等,并使用相应的命令来编译我们的内核模块。 下面是一个简单的chardev.c文件和Makefile文件示例,可以作为参考: chardev.c: ``` #include <linux/fs.h> #include <linux/kernel.h> #include <linux/module.h> #include <linux/uaccess.h> #define DEVICE_NAME "chardev" #define DEVICE_MAJOR 240 static int device_open(struct inode *inode, struct file *file) { printk(KERN_INFO "Device opened\n"); return 0; } static ssize_t device_read(struct file *file, char __user *buf, size_t count, loff_t *offset) { printk(KERN_INFO "Device read\n"); return 0; } static ssize_t device_write(struct file *file, const char __user *buf, size_t count, loff_t *offset) { printk(KERN_INFO "Device write\n"); return count; } static long device_ioctl(struct file *file, unsigned int cmd, unsigned long arg) { printk(KERN_INFO "Device ioctl\n"); return 0; } static int device_release(struct inode *inode, struct file *file) { printk(KERN_INFO "Device closed\n"); return 0; } static struct file_operations fops = { .owner = THIS_MODULE, .open = device_open, .read = device_read, .write = device_write, .unlocked_ioctl = device_ioctl, .release = device_release, }; static int __init chardev_init(void) { int ret; ret = register_chrdev(DEVICE_MAJOR, DEVICE_NAME, &fops); if (ret < 0) { printk(KERN_INFO "Failed to register chardev\n"); return ret; } printk(KERN_INFO "Chardev registered\n"); return 0; } static void __exit chardev_exit(void) { unregister_chrdev(DEVICE_MAJOR, DEVICE_NAME); printk(KERN_INFO "Chardev unregistered\n"); } module_init(chardev_init); module_exit(chardev_exit); MODULE_LICENSE("GPL"); MODULE_AUTHOR("Your name"); MODULE_DESCRIPTION("A simple chardev driver"); ``` Makefile: ``` obj-m := chardev.o KDIR := /lib/modules/$(shell uname -r)/build PWD := $(shell pwd) all: $(MAKE) -C $(KDIR) M=$(PWD) modules clean: $(MAKE) -C $(KDIR) M=$(PWD) clean ``` 我们可以使用以下命令来编译和加载内核模块: ``` make sudo insmod chardev.ko ``` 然后,我们就可以在/dev目录下看到我们的字符设备文件chardev,并对其执行打开、读、、I/O控制和关闭等操作了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值