1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright IBM Corp. 2004 4 * 5 * Tape class device support 6 * 7 * Author: Stefan Bader <shbader@de.ibm.com> 8 * Based on simple class device code by Greg K-H 9 */ 10 11 #define KMSG_COMPONENT "tape" 12 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt 13 14 #include <linux/export.h> 15 #include <linux/slab.h> 16 17 #include "tape_class.h" 18 19 MODULE_AUTHOR("Stefan Bader <shbader@de.ibm.com>"); 20 MODULE_DESCRIPTION( 21 "Copyright IBM Corp. 2004 All Rights Reserved.\n" 22 "tape_class.c" 23 ); 24 MODULE_LICENSE("GPL"); 25 26 static const struct class tape_class = { 27 .name = "tape390", 28 }; 29 30 /* 31 * Register a tape device and return a pointer to the cdev structure. 32 * 33 * device 34 * The pointer to the struct device of the physical (base) device. 35 * drivername 36 * The pointer to the drivers name for it's character devices. 37 * dev 38 * The intended major/minor number. The major number may be 0 to 39 * get a dynamic major number. 40 * fops 41 * The pointer to the drivers file operations for the tape device. 42 * devname 43 * The pointer to the name of the character device. 44 */ 45 struct tape_class_device *register_tape_dev( 46 struct device * device, 47 dev_t dev, 48 const struct file_operations *fops, 49 char * device_name, 50 char * mode_name) 51 { 52 struct tape_class_device * tcd; 53 int rc; 54 char * s; 55 56 tcd = kzalloc(sizeof(struct tape_class_device), GFP_KERNEL); 57 if (!tcd) 58 return ERR_PTR(-ENOMEM); 59 60 strscpy(tcd->device_name, device_name, TAPECLASS_NAME_LEN); 61 for (s = strchr(tcd->device_name, '/'); s; s = strchr(s, '/')) 62 *s = '!'; 63 strscpy(tcd->mode_name, mode_name, TAPECLASS_NAME_LEN); 64 for (s = strchr(tcd->mode_name, '/'); s; s = strchr(s, '/')) 65 *s = '!'; 66 67 tcd->char_device = cdev_alloc(); 68 if (!tcd->char_device) { 69 rc = -ENOMEM; 70 goto fail_with_tcd; 71 } 72 73 tcd->char_device->owner = fops->owner; 74 tcd->char_device->ops = fops; 75 76 rc = cdev_add(tcd->char_device, dev, 1); 77 if (rc) 78 goto fail_with_cdev; 79 80 tcd->class_device = device_create(&tape_class, device, 81 tcd->char_device->dev, NULL, 82 "%s", tcd->device_name); 83 rc = PTR_ERR_OR_ZERO(tcd->class_device); 84 if (rc) 85 goto fail_with_cdev; 86 rc = sysfs_create_link( 87 &device->kobj, 88 &tcd->class_device->kobj, 89 tcd->mode_name 90 ); 91 if (rc) 92 goto fail_with_class_device; 93 94 return tcd; 95 96 fail_with_class_device: 97 device_destroy(&tape_class, tcd->char_device->dev); 98 99 fail_with_cdev: 100 cdev_del(tcd->char_device); 101 102 fail_with_tcd: 103 kfree(tcd); 104 105 return ERR_PTR(rc); 106 } 107 EXPORT_SYMBOL(register_tape_dev); 108 109 void unregister_tape_dev(struct device *device, struct tape_class_device *tcd) 110 { 111 if (tcd != NULL && !IS_ERR(tcd)) { 112 sysfs_remove_link(&device->kobj, tcd->mode_name); 113 device_destroy(&tape_class, tcd->char_device->dev); 114 cdev_del(tcd->char_device); 115 kfree(tcd); 116 } 117 } 118 EXPORT_SYMBOL(unregister_tape_dev); 119 120 121 static int __init tape_init(void) 122 { 123 return class_register(&tape_class); 124 } 125 126 static void __exit tape_exit(void) 127 { 128 class_unregister(&tape_class); 129 } 130 131 postcore_initcall(tape_init); 132 module_exit(tape_exit); 133