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