#author("2024-11-14T14:26:41+09:00","default:honma","honma")
#author("2024-11-14T14:32:22+09:00","default:honma","honma")
* Linuxデバイスドライバ開発 class_create [#oa9eec21]

#seo(description,Linuxのデバイスドライバの書き方をメモ)
#seo(keywords,Linux, Device Driver)

以前の[[Linuxデバイスドライバ開発 デバイスクラス]] などで、使用していた class_create が APIの仕様変更で使えなくなっていたのに気が付いたので書き直しのメモ。~

#highlight(c){{
$ make
make -C /lib/modules/6.8.0-45-generic/build M=/home/developer/work/skel_drv modules
make[1]: ディレクトリ '/usr/src/linux-headers-6.8.0-45-generic' に入ります
warning: the compiler differs from the one used to build the kernel
  The kernel was built by: x86_64-linux-gnu-gcc-13 (Ubuntu 13.2.0-23ubuntu4) 13.2.0
  You are using:           gcc-13 (Ubuntu 13.2.0-23ubuntu4) 13.2.0
  CC [M]  /home/developer/work/skel_drv/skel_drv.o
In file included from /home/developer/work/skel_drv/skel_drv.c:1:
/home/developer/work/skel_drv/skel_drv.c: In function ‘skel_init’:
./include/linux/init.h:184:22: error: passing argument 1 of ‘class_create’ from incompatible pointer type [-Werror=incompatible-pointer-types]
  184 | #define THIS_MODULE (&__this_module)
      |                     ~^~~~~~~~~~~~~~~
      |                      |
      |                      struct module *
/home/developer/work/skel_drv/skel_drv.c:64:39: note: in expansion of macro ‘THIS_MODULE’
   64 |         skel_drv_class = class_create(THIS_MODULE, SKEL_DRV_NAME);
      |                                       ^~~~~~~~~~~
In file included from ./include/linux/device.h:31,
                 from /home/developer/work/skel_drv/skel_drv.c:6:
./include/linux/device/class.h:228:54: note: expected ‘const char *’ but argument is of type ‘struct module *’
  228 | struct class * __must_check class_create(const char *name);
      |                                          ~~~~~~~~~~~~^~~~
/home/developer/work/skel_drv/skel_drv.c:64:26: error: too many arguments to function ‘class_create’
   64 |         skel_drv_class = class_create(THIS_MODULE, SKEL_DRV_NAME);
      |                          ^~~~~~~~~~~~
./include/linux/device/class.h:228:29: note: declared here
  228 | struct class * __must_check class_create(const char *name);
      |                             ^~~~~~~~~~~~
cc1: some warnings being treated as errors
make[3]: *** [scripts/Makefile.build:243: /home/developer/work/skel_drv/skel_drv.o] エラー 1
make[2]: *** [/usr/src/linux-headers-6.8.0-45-generic/Makefile:1925: /home/developer/work/skel_drv] エラー 2
make[1]: *** [Makefile:240: __sub-make] エラー 2
make[1]: ディレクトリ '/usr/src/linux-headers-6.8.0-45-generic' から出ます
make: *** [Makefile:7: default] エラー 2
}}

修正は大層なものではなく、THIS_MODULEの引数が不要になっただけ。

#highlight(c){{
--- skel_drv.c.orig	2022-05-12 17:31:49.000000000 +0900
+++ skel_drv.c	2024-11-14 14:31:22.708723534 +0900
@@ -61,7 +61,9 @@
 	}
 
 	/* デバイスクラスを作成する */
-	skel_drv_class = class_create(THIS_MODULE, SKEL_DRV_NAME);
+//	skel_drv_class = class_create(THIS_MODULE, SKEL_DRV_NAME);
+	/* kernel-6.4 以降はAPI仕様が変更 */
+	skel_drv_class = class_create(SKEL_DRV_NAME);
 	if (IS_ERR(skel_drv_class)) {
 		pr_err("SKEL_DRV: class_create failed\n");
 		goto unregister_region;
}}
#highlight(end)


#br
#include(Linuxデバイスドライバ開発,notitle)

#br
#htmlinsert(amazon_book.html);

トップ   編集 差分 履歴 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS