From 9c2acf6dd9ba8734992820404ac4e840ce37eaa6 Mon Sep 17 00:00:00 2001 From: Wenchao Hao Date: Tue, 14 Mar 2023 11:18:03 +0800 Subject: [PATCH] init --- .gitignore | 174 +++++++++++++++++++++++++++++++++++++++++++++++++++++ Makefile | 10 +++ demo.c | 86 ++++++++++++++++++++++++++ mknod.sh | 6 ++ 4 files changed, 276 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 demo.c create mode 100755 mknod.sh diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8fe465f --- /dev/null +++ b/.gitignore @@ -0,0 +1,174 @@ +# SPDX-License-Identifier: GPL-2.0-only +# +# NOTE! Don't add files that are generated in specific +# subdirectories here. Add them in the ".gitignore" file +# in that subdirectory instead. +# +# NOTE! Please use 'git ls-files -i -c --exclude-per-directory=.gitignore' +# command after changing this file, to see if there are +# any tracked files which get ignored after the change. +# +# Normal rules (sorted alphabetically) +# +.* +*.a +*.asn1.[ch] +*.bin +*.bz2 +*.c.[012]*.* +*.cover +*.dt.yaml +*.dtb +*.dtbo +*.dtb.S +*.dtbo.S +*.dwo +*.elf +*.gcno +*.gz +*.i +*.ko +*.lex.c +*.ll +*.lst +*.lz4 +*.lzma +*.lzo +*.mbx +*.mod +*.mod.c +*.o +*.o.* +*.patch +*.rmeta +*.rpm +*.rsi +*.s +*.so +*.so.dbg +*.su +*.symtypes +*.symversions +*.tab.[ch] +*.tar +*.usyms +*.xz +*.zst +Module.symvers +modules.order + +# +# Top-level generic files +# +/linux +/modules-only.symvers +/vmlinux +/vmlinux.32 +/vmlinux.map +/vmlinux.symvers +/vmlinux-gdb.py +/vmlinuz +/System.map +/Module.markers +/modules.builtin +/modules.builtin.modinfo +/modules.nsdeps + +# +# RPM spec file (make rpm-pkg) +# +/*.spec + +# +# Debian directory (make deb-pkg) +# +/debian/ + +# +# Snap directory (make snap-pkg) +# +/snap/ + +# +# tar directory (make tar*-pkg) +# +/tar-install/ + +# +# We don't want to ignore the following even if they are dot-files +# +!.clang-format +!.cocciconfig +!.get_maintainer.ignore +!.gitattributes +!.gitignore +!.mailmap +!.rustfmt.toml + +# +# Generated include files +# +/include/config/ +/include/generated/ +/include/ksym/ +/arch/*/include/generated/ + +# stgit generated dirs +patches-* + +# quilt's files +patches +series + +# ctags files +tags +TAGS + +# cscope files +cscope.* +ncscope.* + +# gnu global files +GPATH +GRTAGS +GSYMS +GTAGS + +# id-utils files +ID + +*.orig +*~ +\#*# + +# +# Leavings from module signing +# +extra_certificates +signing_key.pem +signing_key.priv +signing_key.x509 +x509.genkey + +# Kconfig presets +/all.config +/alldef.config +/allmod.config +/allno.config +/allrandom.config +/allyes.config + +# Kconfig savedefconfig output +/defconfig + +# Kdevelop4 +*.kdev4 + +# Clang's compilation database file +/compile_commands.json + +# Documentation toolchain +sphinx_*/ + +# Rust analyzer configuration +/rust-project.json diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..923d9b7 --- /dev/null +++ b/Makefile @@ -0,0 +1,10 @@ +KDIR=/usr/src/kernels/$(shell uname -r) + +obj-m += demo.o + +all: + make -C $(KDIR) M=$(shell pwd) modules + +clean: + make -C $(KDIR) M=$(shell pwd) clean + diff --git a/demo.c b/demo.c new file mode 100644 index 0000000..03c6e01 --- /dev/null +++ b/demo.c @@ -0,0 +1,86 @@ +#include +#include +#include + +MODULE_LICENSE("GPL v2"); + +static ssize_t demo_read(struct file *filp, char __user *buff, size_t size, loff_t *pos) +{ + printk("%s %d\n", __func__, __LINE__); + return size; +} + +static ssize_t demo_write(struct file *filp, const char __user *buff, size_t size, loff_t *pos) +{ + printk("%s %d\n", __func__, __LINE__); + return size; +} + +static int demo_open(struct inode *inode, struct file *filp) +{ + printk("%s %d\n", __func__, __LINE__); + return 0; +} + +static int demo_release(struct inode *inode, struct file *filp) +{ + printk("%s %d\n", __func__, __LINE__); + return 0; +} + +struct file_operations demo_fops = { + .owner = THIS_MODULE, + .read = demo_read, + .write = demo_write, + .open = demo_open, + .release = demo_release, +}; + +static dev_t dev; +static struct cdev *cdev; + +int demo_init(void) +{ + int rc; + + rc = alloc_chrdev_region(&dev, 0, 1, "demo"); + if (rc) { + printk("failed to alloc chrdev number\n"); + return rc; + } + + cdev = cdev_alloc(); + if (!cdev) { + printk("failed to alloc cdev\n"); + rc = -ENOMEM; + goto out_unregister_chrdev; + } + + cdev->owner = THIS_MODULE; + cdev_init(cdev, &demo_fops); + + rc = cdev_add(cdev, dev, 1); + if (rc) { + printk("failed to add char device\n"); + goto out_free_cdev; + } + + return 0; + +out_free_cdev: + kobject_put(&cdev->kobj); + +out_unregister_chrdev: + unregister_chrdev_region(dev, 1); + + return rc; +} + +void demo_exit(void) +{ + cdev_del(cdev); + unregister_chrdev_region(dev, 1); +} + +module_init(demo_init); +module_exit(demo_exit); diff --git a/mknod.sh b/mknod.sh new file mode 100755 index 0000000..982b644 --- /dev/null +++ b/mknod.sh @@ -0,0 +1,6 @@ +#!/bin/sh + +major=$(cat /proc/devices | grep demo) + +mknode /dev/demo c $major 0 +