1 // SPDX-License-Identifier: GPL-2.0 or BSD-3-Clause 2 /* 3 * Copyright(c) 2015, 2016 Intel Corporation. 4 */ 5 6 #include <linux/cdev.h> 7 #include <linux/device.h> 8 #include <linux/fs.h> 9 10 #include "hfi.h" 11 #include "device.h" 12 13 static struct class *class; 14 static struct class *user_class; 15 static dev_t hfi1_dev; 16 17 int hfi1_cdev_init(int minor, const char *name, 18 const struct file_operations *fops, 19 struct cdev *cdev, struct device **devp, 20 bool user_accessible, 21 struct kobject *parent) 22 { 23 const dev_t dev = MKDEV(MAJOR(hfi1_dev), minor); 24 struct device *device = NULL; 25 int ret; 26 27 cdev_init(cdev, fops); 28 cdev->owner = THIS_MODULE; 29 cdev_set_parent(cdev, parent); 30 kobject_set_name(&cdev->kobj, name); 31 32 ret = cdev_add(cdev, dev, 1); 33 if (ret < 0) { 34 pr_err("Could not add cdev for minor %d, %s (err %d)\n", 35 minor, name, -ret); 36 goto done; 37 } 38 39 if (user_accessible) 40 device = device_create(user_class, NULL, dev, NULL, "%s", name); 41 else 42 device = device_create(class, NULL, dev, NULL, "%s", name); 43 44 if (IS_ERR(device)) { 45 ret = PTR_ERR(device); 46 device = NULL; 47 pr_err("Could not create device for minor %d, %s (err %d)\n", 48 minor, name, -ret); 49 cdev_del(cdev); 50 } 51 done: 52 *devp = device; 53 return ret; 54 } 55 56 void hfi1_cdev_cleanup(struct cdev *cdev, struct device **devp) 57 { 58 struct device *device = *devp; 59 60 if (device) { 61 device_unregister(device); 62 *devp = NULL; 63 64 cdev_del(cdev); 65 } 66 } 67 68 static const char *hfi1_class_name = "hfi1"; 69 70 const char *class_name(void) 71 { 72 return hfi1_class_name; 73 } 74 75 static char *hfi1_devnode(const struct device *dev, umode_t *mode) 76 { 77 if (mode) 78 *mode = 0600; 79 return kasprintf(GFP_KERNEL, "%s", dev_name(dev)); 80 } 81 82 static const char *hfi1_class_name_user = "hfi1_user"; 83 static const char *class_name_user(void) 84 { 85 return hfi1_class_name_user; 86 } 87 88 static char *hfi1_user_devnode(const struct device *dev, umode_t *mode) 89 { 90 if (mode) 91 *mode = 0666; 92 return kasprintf(GFP_KERNEL, "%s", dev_name(dev)); 93 } 94 95 int __init dev_init(void) 96 { 97 int ret; 98 99 ret = alloc_chrdev_region(&hfi1_dev, 0, HFI1_NMINORS, DRIVER_NAME); 100 if (ret < 0) { 101 pr_err("Could not allocate chrdev region (err %d)\n", -ret); 102 goto done; 103 } 104 105 class = class_create(class_name()); 106 if (IS_ERR(class)) { 107 ret = PTR_ERR(class); 108 pr_err("Could not create device class (err %d)\n", -ret); 109 unregister_chrdev_region(hfi1_dev, HFI1_NMINORS); 110 goto done; 111 } 112 class->devnode = hfi1_devnode; 113 114 user_class = class_create(class_name_user()); 115 if (IS_ERR(user_class)) { 116 ret = PTR_ERR(user_class); 117 pr_err("Could not create device class for user accessible files (err %d)\n", 118 -ret); 119 class_destroy(class); 120 class = NULL; 121 user_class = NULL; 122 unregister_chrdev_region(hfi1_dev, HFI1_NMINORS); 123 goto done; 124 } 125 user_class->devnode = hfi1_user_devnode; 126 127 done: 128 return ret; 129 } 130 131 void dev_cleanup(void) 132 { 133 class_destroy(class); 134 class = NULL; 135 136 class_destroy(user_class); 137 user_class = NULL; 138 139 unregister_chrdev_region(hfi1_dev, HFI1_NMINORS); 140 } 141