xref: /linux/drivers/s390/char/hmcdrv_dev.c (revision 7fc2cd2e4b398c57c9cf961cfea05eadbf34c05c)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *    HMC Drive CD/DVD Device
4  *
5  *    Copyright IBM Corp. 2013
6  *    Author(s): Ralf Hoppe (rhoppe@de.ibm.com)
7  *
8  *    This file provides a Linux "misc" character device for access to an
9  *    assigned HMC drive CD/DVD-ROM. It works as follows: First create the
10  *    device by calling hmcdrv_dev_init(). After open() a lseek(fd, 0,
11  *    SEEK_END) indicates that a new FTP command follows (not needed on the
12  *    first command after open). Then write() the FTP command ASCII string
13  *    to it, e.g. "dir /" or "nls <directory>" or "get <filename>". At the
14  *    end read() the response.
15  */
16 
17 #define pr_fmt(fmt) "hmcdrv: " fmt
18 
19 #include <linux/kernel.h>
20 #include <linux/module.h>
21 #include <linux/slab.h>
22 #include <linux/fs.h>
23 #include <linux/cdev.h>
24 #include <linux/miscdevice.h>
25 #include <linux/device.h>
26 #include <linux/capability.h>
27 #include <linux/delay.h>
28 #include <linux/uaccess.h>
29 
30 #include "hmcdrv_dev.h"
31 #include "hmcdrv_ftp.h"
32 
33 /* If the following macro is defined, then the HMC device creates it's own
34  * separated device class (and dynamically assigns a major number). If not
35  * defined then the HMC device is assigned to the "misc" class devices.
36  *
37 #define HMCDRV_DEV_CLASS "hmcftp"
38  */
39 
40 #define HMCDRV_DEV_NAME  "hmcdrv"
41 #define HMCDRV_DEV_BUSY_DELAY	 500 /* delay between -EBUSY trials in ms */
42 #define HMCDRV_DEV_BUSY_RETRIES  3   /* number of retries on -EBUSY */
43 
44 struct hmcdrv_dev_node {
45 
46 #ifdef HMCDRV_DEV_CLASS
47 	struct cdev dev; /* character device structure */
48 	umode_t mode;	 /* mode of device node (unused, zero) */
49 #else
50 	struct miscdevice dev; /* "misc" device structure */
51 #endif
52 
53 };
54 
55 static int hmcdrv_dev_open(struct inode *inode, struct file *fp);
56 static int hmcdrv_dev_release(struct inode *inode, struct file *fp);
57 static loff_t hmcdrv_dev_seek(struct file *fp, loff_t pos, int whence);
58 static ssize_t hmcdrv_dev_read(struct file *fp, char __user *ubuf,
59 			       size_t len, loff_t *pos);
60 static ssize_t hmcdrv_dev_write(struct file *fp, const char __user *ubuf,
61 				size_t len, loff_t *pos);
62 static ssize_t hmcdrv_dev_transfer(char __kernel *cmd, loff_t offset,
63 				   char __user *buf, size_t len);
64 
65 /*
66  * device operations
67  */
68 static const struct file_operations hmcdrv_dev_fops = {
69 	.open = hmcdrv_dev_open,
70 	.llseek = hmcdrv_dev_seek,
71 	.release = hmcdrv_dev_release,
72 	.read = hmcdrv_dev_read,
73 	.write = hmcdrv_dev_write,
74 };
75 
76 static struct hmcdrv_dev_node hmcdrv_dev; /* HMC device struct (static) */
77 
78 #ifdef HMCDRV_DEV_CLASS
79 
80 static struct class *hmcdrv_dev_class; /* device class pointer */
81 static dev_t hmcdrv_dev_no; /* device number (major/minor) */
82 
83 /**
84  * hmcdrv_dev_name() - provides a naming hint for a device node in /dev
85  * @dev: device for which the naming/mode hint is
86  * @mode: file mode for device node created in /dev
87  *
88  * See: devtmpfs.c, function devtmpfs_create_node()
89  *
90  * Return: recommended device file name in /dev
91  */
92 static char *hmcdrv_dev_name(const struct device *dev, umode_t *mode)
93 {
94 	char *nodename = NULL;
95 	const char *devname = dev_name(dev); /* kernel device name */
96 
97 	if (devname)
98 		nodename = kasprintf(GFP_KERNEL, "%s", devname);
99 
100 	/* on device destroy (rmmod) the mode pointer may be NULL
101 	 */
102 	if (mode)
103 		*mode = hmcdrv_dev.mode;
104 
105 	return nodename;
106 }
107 
108 #endif	/* HMCDRV_DEV_CLASS */
109 
110 /*
111  * open()
112  */
113 static int hmcdrv_dev_open(struct inode *inode, struct file *fp)
114 {
115 	int rc;
116 
117 	/* check for non-blocking access, which is really unsupported
118 	 */
119 	if (fp->f_flags & O_NONBLOCK)
120 		return -EINVAL;
121 
122 	/* Because it makes no sense to open this device read-only (then a
123 	 * FTP command cannot be emitted), we respond with an error.
124 	 */
125 	if ((fp->f_flags & O_ACCMODE) == O_RDONLY)
126 		return -EINVAL;
127 
128 	/* prevent unloading this module as long as anyone holds the
129 	 * device file open - so increment the reference count here
130 	 */
131 	if (!try_module_get(THIS_MODULE))
132 		return -ENODEV;
133 
134 	fp->private_data = NULL; /* no command yet */
135 	rc = hmcdrv_ftp_startup();
136 	if (rc)
137 		module_put(THIS_MODULE);
138 
139 	pr_debug("open file '/dev/%pD' with return code %d\n", fp, rc);
140 	return rc;
141 }
142 
143 /*
144  * release()
145  */
146 static int hmcdrv_dev_release(struct inode *inode, struct file *fp)
147 {
148 	pr_debug("closing file '/dev/%pD'\n", fp);
149 	kfree(fp->private_data);
150 	fp->private_data = NULL;
151 	hmcdrv_ftp_shutdown();
152 	module_put(THIS_MODULE);
153 	return 0;
154 }
155 
156 /*
157  * lseek()
158  */
159 static loff_t hmcdrv_dev_seek(struct file *fp, loff_t pos, int whence)
160 {
161 	switch (whence) {
162 	case SEEK_CUR: /* relative to current file position */
163 		pos += fp->f_pos; /* new position stored in 'pos' */
164 		break;
165 
166 	case SEEK_SET: /* absolute (relative to beginning of file) */
167 		break; /* SEEK_SET */
168 
169 		/* We use SEEK_END as a special indicator for a SEEK_SET
170 		 * (set absolute position), combined with a FTP command
171 		 * clear.
172 		 */
173 	case SEEK_END:
174 		if (fp->private_data) {
175 			kfree(fp->private_data);
176 			fp->private_data = NULL;
177 		}
178 
179 		break; /* SEEK_END */
180 
181 	default: /* SEEK_DATA, SEEK_HOLE: unsupported */
182 		return -EINVAL;
183 	}
184 
185 	if (pos < 0)
186 		return -EINVAL;
187 
188 	fp->f_pos = pos;
189 	return pos;
190 }
191 
192 /*
193  * transfer (helper function)
194  */
195 static ssize_t hmcdrv_dev_transfer(char __kernel *cmd, loff_t offset,
196 				   char __user *buf, size_t len)
197 {
198 	ssize_t retlen;
199 	unsigned trials = HMCDRV_DEV_BUSY_RETRIES;
200 
201 	do {
202 		retlen = hmcdrv_ftp_cmd(cmd, offset, buf, len);
203 
204 		if (retlen != -EBUSY)
205 			break;
206 
207 		msleep(HMCDRV_DEV_BUSY_DELAY);
208 
209 	} while (--trials > 0);
210 
211 	return retlen;
212 }
213 
214 /*
215  * read()
216  */
217 static ssize_t hmcdrv_dev_read(struct file *fp, char __user *ubuf,
218 			       size_t len, loff_t *pos)
219 {
220 	ssize_t retlen;
221 
222 	if (((fp->f_flags & O_ACCMODE) == O_WRONLY) ||
223 	    (fp->private_data == NULL)) { /* no FTP cmd defined ? */
224 		return -EBADF;
225 	}
226 
227 	retlen = hmcdrv_dev_transfer((char *) fp->private_data,
228 				     *pos, ubuf, len);
229 
230 	pr_debug("read from file '/dev/%pD' at %lld returns %zd/%zu\n",
231 		 fp, (long long) *pos, retlen, len);
232 
233 	if (retlen > 0)
234 		*pos += retlen;
235 
236 	return retlen;
237 }
238 
239 /*
240  * write()
241  */
242 static ssize_t hmcdrv_dev_write(struct file *fp, const char __user *ubuf,
243 				size_t len, loff_t *pos)
244 {
245 	ssize_t retlen;
246 	void *pdata;
247 
248 	pr_debug("writing file '/dev/%pD' at pos. %lld with length %zd\n",
249 		 fp, (long long) *pos, len);
250 
251 	if (!fp->private_data) { /* first expect a cmd write */
252 		pdata = memdup_user_nul(ubuf, len);
253 		if (IS_ERR(pdata))
254 			return PTR_ERR(pdata);
255 		fp->private_data = pdata;
256 		return len;
257 	}
258 
259 	retlen = hmcdrv_dev_transfer((char *) fp->private_data,
260 				     *pos, (char __user *) ubuf, len);
261 	if (retlen > 0)
262 		*pos += retlen;
263 
264 	pr_debug("write to file '/dev/%pD' returned %zd\n", fp, retlen);
265 
266 	return retlen;
267 }
268 
269 /**
270  * hmcdrv_dev_init() - creates a HMC drive CD/DVD device
271  *
272  * This function creates a HMC drive CD/DVD kernel device and an associated
273  * device under /dev, using a dynamically allocated major number.
274  *
275  * Return: 0 on success, else an error code.
276  */
277 int hmcdrv_dev_init(void)
278 {
279 	int rc;
280 
281 #ifdef HMCDRV_DEV_CLASS
282 	struct device *dev;
283 
284 	rc = alloc_chrdev_region(&hmcdrv_dev_no, 0, 1, HMCDRV_DEV_NAME);
285 
286 	if (rc)
287 		goto out_err;
288 
289 	cdev_init(&hmcdrv_dev.dev, &hmcdrv_dev_fops);
290 	hmcdrv_dev.dev.owner = THIS_MODULE;
291 	rc = cdev_add(&hmcdrv_dev.dev, hmcdrv_dev_no, 1);
292 
293 	if (rc)
294 		goto out_unreg;
295 
296 	/* At this point the character device exists in the kernel (see
297 	 * /proc/devices), but not under /dev nor /sys/devices/virtual. So
298 	 * we have to create an associated class (see /sys/class).
299 	 */
300 	hmcdrv_dev_class = class_create(HMCDRV_DEV_CLASS);
301 
302 	if (IS_ERR(hmcdrv_dev_class)) {
303 		rc = PTR_ERR(hmcdrv_dev_class);
304 		goto out_devdel;
305 	}
306 
307 	/* Finally a device node in /dev has to be established (as 'mkdev'
308 	 * does from the command line). Notice that assignment of a device
309 	 * node name/mode function is optional (only for mode != 0600).
310 	 */
311 	hmcdrv_dev.mode = 0; /* "unset" */
312 	hmcdrv_dev_class->devnode = hmcdrv_dev_name;
313 
314 	dev = device_create(hmcdrv_dev_class, NULL, hmcdrv_dev_no, NULL,
315 			    "%s", HMCDRV_DEV_NAME);
316 	if (!IS_ERR(dev))
317 		return 0;
318 
319 	rc = PTR_ERR(dev);
320 	class_destroy(hmcdrv_dev_class);
321 	hmcdrv_dev_class = NULL;
322 
323 out_devdel:
324 	cdev_del(&hmcdrv_dev.dev);
325 
326 out_unreg:
327 	unregister_chrdev_region(hmcdrv_dev_no, 1);
328 
329 out_err:
330 
331 #else  /* !HMCDRV_DEV_CLASS */
332 	hmcdrv_dev.dev.minor = MISC_DYNAMIC_MINOR;
333 	hmcdrv_dev.dev.name = HMCDRV_DEV_NAME;
334 	hmcdrv_dev.dev.fops = &hmcdrv_dev_fops;
335 	hmcdrv_dev.dev.mode = 0; /* finally produces 0600 */
336 	rc = misc_register(&hmcdrv_dev.dev);
337 #endif	/* HMCDRV_DEV_CLASS */
338 
339 	return rc;
340 }
341 
342 /**
343  * hmcdrv_dev_exit() - destroys a HMC drive CD/DVD device
344  */
345 void hmcdrv_dev_exit(void)
346 {
347 #ifdef HMCDRV_DEV_CLASS
348 	if (!IS_ERR_OR_NULL(hmcdrv_dev_class)) {
349 		device_destroy(hmcdrv_dev_class, hmcdrv_dev_no);
350 		class_destroy(hmcdrv_dev_class);
351 	}
352 
353 	cdev_del(&hmcdrv_dev.dev);
354 	unregister_chrdev_region(hmcdrv_dev_no, 1);
355 #else  /* !HMCDRV_DEV_CLASS */
356 	misc_deregister(&hmcdrv_dev.dev);
357 #endif	/* HMCDRV_DEV_CLASS */
358 }
359