|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
| 2 | + |
| 3 | +/* |
| 4 | + * Copyright 2022 HabanaLabs, Ltd. |
| 5 | + * All Rights Reserved. |
| 6 | + * |
| 7 | + */ |
| 8 | + |
| 9 | +#include <linux/debugfs.h> |
| 10 | +#include <linux/device.h> |
| 11 | + |
| 12 | +#include <drm/drm_accel.h> |
| 13 | +#include <drm/drm_ioctl.h> |
| 14 | +#include <drm/drm_print.h> |
| 15 | + |
| 16 | +static struct dentry *accel_debugfs_root; |
| 17 | +static struct class *accel_class; |
| 18 | + |
| 19 | +static char *accel_devnode(struct device *dev, umode_t *mode) |
| 20 | +{ |
| 21 | + return kasprintf(GFP_KERNEL, "accel/%s", dev_name(dev)); |
| 22 | +} |
| 23 | + |
| 24 | +static int accel_sysfs_init(void) |
| 25 | +{ |
| 26 | + accel_class = class_create(THIS_MODULE, "accel"); |
| 27 | + if (IS_ERR(accel_class)) |
| 28 | + return PTR_ERR(accel_class); |
| 29 | + |
| 30 | + accel_class->devnode = accel_devnode; |
| 31 | + |
| 32 | + return 0; |
| 33 | +} |
| 34 | + |
| 35 | +static void accel_sysfs_destroy(void) |
| 36 | +{ |
| 37 | + if (IS_ERR_OR_NULL(accel_class)) |
| 38 | + return; |
| 39 | + class_destroy(accel_class); |
| 40 | + accel_class = NULL; |
| 41 | +} |
| 42 | + |
| 43 | +static int accel_stub_open(struct inode *inode, struct file *filp) |
| 44 | +{ |
| 45 | + return -EOPNOTSUPP; |
| 46 | +} |
| 47 | + |
| 48 | +static const struct file_operations accel_stub_fops = { |
| 49 | + .owner = THIS_MODULE, |
| 50 | + .open = accel_stub_open, |
| 51 | + .llseek = noop_llseek, |
| 52 | +}; |
| 53 | + |
| 54 | +void accel_core_exit(void) |
| 55 | +{ |
| 56 | + unregister_chrdev(ACCEL_MAJOR, "accel"); |
| 57 | + debugfs_remove(accel_debugfs_root); |
| 58 | + accel_sysfs_destroy(); |
| 59 | +} |
| 60 | + |
| 61 | +int __init accel_core_init(void) |
| 62 | +{ |
| 63 | + int ret; |
| 64 | + |
| 65 | + ret = accel_sysfs_init(); |
| 66 | + if (ret < 0) { |
| 67 | + DRM_ERROR("Cannot create ACCEL class: %d\n", ret); |
| 68 | + goto error; |
| 69 | + } |
| 70 | + |
| 71 | + accel_debugfs_root = debugfs_create_dir("accel", NULL); |
| 72 | + |
| 73 | + ret = register_chrdev(ACCEL_MAJOR, "accel", &accel_stub_fops); |
| 74 | + if (ret < 0) |
| 75 | + DRM_ERROR("Cannot register ACCEL major: %d\n", ret); |
| 76 | + |
| 77 | +error: |
| 78 | + /* |
| 79 | + * Any cleanup due to errors will be done in drm_core_exit() that |
| 80 | + * will call accel_core_exit() |
| 81 | + */ |
| 82 | + return ret; |
| 83 | +} |
0 commit comments