From 666fea80ee365a69a65e828c19349d23d0500713 Mon Sep 17 00:00:00 2001 From: John Ogness Date: Fri, 28 Sep 2018 15:56:36 +0200 Subject: hello driver: add x86 support Supporting x86 allows us to quickly show the hello driver in action directly from the x86 trainer image. It also shows how platform devices can be added on architectures that do not support device trees. Signed-off-by: John Ogness --- schulung_tools/drivers/modules/hellodriver/hello.c | 20 ++++++++++++++++++ .../modules/hellodriver/patch-add-sysfs.diff | 10 ++------- schulung_tools/drivers/patch-kernel-hello.diff | 24 ++++++++++++++++++++-- 3 files changed, 44 insertions(+), 10 deletions(-) (limited to 'schulung_tools/drivers') diff --git a/schulung_tools/drivers/modules/hellodriver/hello.c b/schulung_tools/drivers/modules/hellodriver/hello.c index caded6d..328c587 100644 --- a/schulung_tools/drivers/modules/hellodriver/hello.c +++ b/schulung_tools/drivers/modules/hellodriver/hello.c @@ -89,11 +89,15 @@ static int hello_probe(struct platform_device *pdev) return -ENOMEM; } +#ifdef CONFIG_OF ret = of_property_read_u32(pdev->dev.of_node, "index", &hello->minor); if (ret < 0) { dev_err(&pdev->dev, "no index specified\n"); goto err_out1; } +#else + hello->minor = (unsigned)pdev->id; +#endif if (hello->minor >= HELLO_MAX_DEVICES) { dev_err(&pdev->dev, "invalid index: %u\n", hello->minor); @@ -161,6 +165,10 @@ static struct platform_driver hello_driver = { .remove = hello_remove, }; +#ifndef CONFIG_OF +static struct platform_device *pdevs[3]; +#endif + static int __init hello_init(void) { int ret; @@ -182,6 +190,12 @@ static int __init hello_init(void) if (ret != 0) goto err_out2; +#ifndef CONFIG_OF + pdevs[0] = platform_device_register_simple("hello", 1, NULL, 0); + pdevs[1] = platform_device_register_simple("hello", 3, NULL, 0); + pdevs[2] = platform_device_register_simple("hello", 5, NULL, 0); +#endif + return 0; err_out2: @@ -195,6 +209,12 @@ static void __exit hello_exit(void) { printk(KERN_INFO "%s\n", __func__); +#ifndef CONFIG_OF + platform_device_unregister(pdevs[0]); + platform_device_unregister(pdevs[1]); + platform_device_unregister(pdevs[2]); +#endif + platform_driver_unregister(&hello_driver); class_destroy(hello_class); unregister_chrdev_region(hello_devt, HELLO_MAX_DEVICES); diff --git a/schulung_tools/drivers/modules/hellodriver/patch-add-sysfs.diff b/schulung_tools/drivers/modules/hellodriver/patch-add-sysfs.diff index 703b6e0..3e792fb 100644 --- a/schulung_tools/drivers/modules/hellodriver/patch-add-sysfs.diff +++ b/schulung_tools/drivers/modules/hellodriver/patch-add-sysfs.diff @@ -1,6 +1,6 @@ --- a/hello.c 2016-06-08 20:21:26.751180497 +0200 +++ b/hello.c 2016-06-08 20:19:31.655178050 +0200 -@@ -76,6 +76,30 @@ +@@ -76,6 +76,30 @@ static const struct file_operations hello_fops = { .write = hello_write, }; @@ -31,7 +31,7 @@ static int hello_probe(struct platform_device *pdev) { struct hello_dev *hello; -@@ -178,6 +202,8 @@ +@@ -186,6 +210,8 @@ static int __init hello_init(void) goto err_out1; } @@ -40,9 +40,3 @@ ret = platform_driver_register(&hello_driver); if (ret != 0) goto err_out2; -@@ -206,4 +232,4 @@ - MODULE_AUTHOR("John Ogness "); - MODULE_DESCRIPTION("a great module for hello-ing!"); - MODULE_LICENSE("GPL v2"); --MODULE_VERSION("20160607"); -+MODULE_VERSION("20160608"); diff --git a/schulung_tools/drivers/patch-kernel-hello.diff b/schulung_tools/drivers/patch-kernel-hello.diff index ab2949c..d3605c4 100644 --- a/schulung_tools/drivers/patch-kernel-hello.diff +++ b/schulung_tools/drivers/patch-kernel-hello.diff @@ -1,7 +1,7 @@ diff -urNp a/drivers/char/hello.c b/drivers/char/hello.c --- a/drivers/char/hello.c 1970-01-01 01:00:00.000000000 +0100 +++ b/drivers/char/hello.c 2016-06-08 20:46:01.991211855 +0200 -@@ -0,0 +1,235 @@ +@@ -0,0 +1,255 @@ +#include +#include +#include @@ -117,11 +117,15 @@ diff -urNp a/drivers/char/hello.c b/drivers/char/hello.c + return -ENOMEM; + } + ++#ifdef CONFIG_OF + ret = of_property_read_u32(pdev->dev.of_node, "index", &hello->minor); + if (ret < 0) { + dev_err(&pdev->dev, "no index specified\n"); + goto err_out1; + } ++#else ++ hello->minor = (unsigned)pdev->id; ++#endif + + if (hello->minor >= HELLO_MAX_DEVICES) { + dev_err(&pdev->dev, "invalid index: %u\n", hello->minor); @@ -189,6 +193,10 @@ diff -urNp a/drivers/char/hello.c b/drivers/char/hello.c + .remove = hello_remove, +}; + ++#ifndef CONFIG_OF ++static struct platform_device *pdevs[3]; ++#endif ++ +static int __init hello_init(void) +{ + int ret; @@ -212,6 +220,12 @@ diff -urNp a/drivers/char/hello.c b/drivers/char/hello.c + if (ret != 0) + goto err_out2; + ++#ifndef CONFIG_OF ++ pdevs[0] = platform_device_register_simple("hello", 1, NULL, 0); ++ pdevs[1] = platform_device_register_simple("hello", 3, NULL, 0); ++ pdevs[2] = platform_device_register_simple("hello", 5, NULL, 0); ++#endif ++ + return 0; + +err_out2: @@ -225,6 +239,12 @@ diff -urNp a/drivers/char/hello.c b/drivers/char/hello.c +{ + printk(KERN_INFO "%s\n", __func__); + ++#ifndef CONFIG_OF ++ platform_device_unregister(pdevs[0]); ++ platform_device_unregister(pdevs[1]); ++ platform_device_unregister(pdevs[2]); ++#endif ++ + platform_driver_unregister(&hello_driver); + class_destroy(hello_class); + unregister_chrdev_region(hello_devt, HELLO_MAX_DEVICES); @@ -236,7 +256,7 @@ diff -urNp a/drivers/char/hello.c b/drivers/char/hello.c +MODULE_AUTHOR("John Ogness "); +MODULE_DESCRIPTION("a great module for hello-ing!"); +MODULE_LICENSE("GPL v2"); -+MODULE_VERSION("20160608"); ++MODULE_VERSION("20160607"); diff -urNp a/drivers/char/Kconfig b/drivers/char/Kconfig --- a/drivers/char/Kconfig 2016-05-16 00:43:13.000000000 +0200 +++ b/drivers/char/Kconfig 2016-06-08 20:41:14.047205734 +0200 -- cgit v1.2.3