Messages in this thread | | | Date | Sun, 27 Sep 2015 05:16:41 -0500 | Subject | [script] New driver boilerplace, please check hints welcomed. | From | Mike Mestnik <> |
| |
Hello, I wonder if this perl script creates a good foundation for starting a new driver. I specifically wonder if I managed to choose the right includes and understood the folder scheme.
I must apologize English is my native language, but I can normally only communicate with computers. That's why I wrote a script instead of an article describing the steps I've taken.
http://pastebin.com/46NSXaBD
#!/usr/bin/env perl
use common::sense; use Getopt::Std; use File::Path qw(make_path remove_tree); use File::Copy;
our($opt_n, $opt_v, $opt_s, $opt_H, $opt_l); getopts('n:v:sH:l:');
$opt_n && $opt_v || die 'n and v requiered.'; my $p = ($opt_s ? '/usr' : '~') . "/src/${opt_n}-${opt_v}"; make_path("$p/patches") >= 0 || die "make_path: Failed to create $p: $!";
my $optl = $opt_l ? "/kernel/$opt_l" : "/kernel/drivers/$opt_n"; my $o; open($o, ">$p/dkms.conf") or die "Can't open dkms.conf: $!\n"; print $o <<here; PACKAGE_NAME="$opt_n" PACKAGE_VERSION="$opt_v" BUILT_MODULE_NAME[0]="$opt_n" DEST_MODULE_LOCATION[0]="$optl" here close $o;
open($o, ">$p/${opt_n}.c") or die "Can't open ${opt_n}.c: $!\n"; print $o <<'here'; /* * here if($opt_H){ open my $fh, '<', \$opt_H or die $!; print $o qq{ *${("\n"," $_")[$_ ne '']}} while (<$fh>); close $fh; } else { print $o " * \n"; } print $o <<'here'; * */
here close $o;
copy("$p/${opt_n}.c","$p/${opt_n}.h") or die "Can't copy ${opt_n}.c: $!\n";
my $_optn = $opt_n; $_optn =~ y/-/_/s; my $u_optn = uc$_optn; open($o, ">>$p/${opt_n}.h") or die "Can't open ${opt_n}.h: $!\n"; print $o qq{#define ${u_optn}_VERSION "$opt_v"\n\n}; close $o;
open($o, ">>$p/${opt_n}.c") or die "Can't reopen ${opt_n}.c: $!\n"; print $o <<here; #include <linux/kernel.h> #include <linux/module.h> #include <linux/slab.h> #include <linux/init.h> #include <linux/types.h>
#include "${opt_n}.h"
MODULE_AUTHOR(); MODULE_DESCRIPTION(); MODULE_LICENSE(); MODULE_VERSION(${u_optn}_VERSION);
// static ... // module_param(..., uint, 0); // MODULE_PARM_DESC(..., "");
static void ${_optn}_exit(void) {
return; }
static void ${_optn}_init(void) {
return; }
module_init(${_optn}_init); module_exit(${_optn}_exit); here close $o;
open($o, ">$p/Kconfig") or die "Can't open ${opt_n}.c: $!\n"; print $o <<here; # # $opt_n $opt_v #
config ${u_optn} tristate "$opt_n $opt_v" # depends on # select ---help--- Some inspiering text.
here close $o;
open($o, ">$p/Makefile") or die "Can't open ${opt_n}.c: $!\n"; print $o <<here; obj-m := ${opt_n}.o
KVER ?= \$(shell uname -r)
KDIR ?= /lib/modules/\$(KVER)/build MDIR ?= /lib/modules/\$(KVER)$optl
PWD := \$(shell pwd)
default: \$(MAKE) -C \$(KDIR) SUBDIRS=\$(PWD) \$(obj-m:.o=.ko)
install: install -d \$(MDIR) install -m 644 -c \$(obj-m:.o=.ko) \$(MDIR) depmod -a
clean: \$(MAKE) -C \$(KDIR) SUBDIRS=\$(PWD) \$(obj-m:.o=.ko) clean rm -rf *.mod.c *.o *.ko .tmp_versions Module.symvers .\$(obj-m:.o=)* rm -rf linux/.*o.cmd linux/*.o
here close $o;
exit;
1; __END__
| |