-
Notifications
You must be signed in to change notification settings - Fork 11
/
simple_cdev.c
60 lines (53 loc) · 1.52 KB
/
simple_cdev.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// SPDX-License-Identifier: MIT
/**
Character Driver for Beckhoff BIOS API
Author: Patrick Brünn <[email protected]>
Copyright (C) 2013 - 2018 Beckhoff Automation GmbH & Co. KG
*/
#include <linux/fs.h>
#include <linux/module.h>
#include <linux/version.h>
#include "simple_cdev.h"
int simple_cdev_init(struct simple_cdev *dev, const char *classname,
const char *devicename, struct file_operations *file_ops)
{
if (alloc_chrdev_region(&dev->dev, 0, 1, KBUILD_MODNAME) < 0) {
pr_warn("alloc_chrdev_region() failed!\n");
return -1;
}
cdev_init(&dev->cdev, file_ops);
dev->cdev.owner = THIS_MODULE;
kobject_set_name(&dev->cdev.kobj, "%s", devicename);
if (cdev_add(&dev->cdev, dev->dev, 1) == -1) {
pr_warn("cdev_add() failed!\n");
goto rollback_region;
}
#if (LINUX_VERSION_CODE < KERNEL_VERSION(6,4,0))
if ((dev->class = class_create(THIS_MODULE, classname)) == NULL) {
#else
(void)classname;
if ((dev->class = class_create(devicename)) == NULL) {
#endif
pr_warn("class_create() failed!\n");
goto rollback_cdev;
}
if (device_create(dev->class, NULL, dev->dev, NULL, "%s", devicename) == NULL) {
pr_warn("device_create() failed!\n");
goto rollback_class;
}
return 0;
rollback_class:
class_destroy(dev->class);
rollback_cdev:
cdev_del(&dev->cdev);
rollback_region:
unregister_chrdev_region(dev->dev, 1);
return -1;
}
void simple_cdev_remove(struct simple_cdev *dev)
{
device_destroy(dev->class, dev->dev);
class_destroy(dev->class);
cdev_del(&dev->cdev);
unregister_chrdev_region(dev->dev, 1);
}