xref: /linux/drivers/s390/char/tape_char.c (revision 52c996d3f40b40f87ef9dc80596903309682acc3)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  *    character device frontend for tape device driver
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  *  S390 and zSeries version
6cced1dd4SMichael Holzheu  *    Copyright IBM Corp. 2001, 2006
71da177e4SLinus Torvalds  *    Author(s): Carsten Otte <cotte@de.ibm.com>
81da177e4SLinus Torvalds  *		 Michael Holzheu <holzheu@de.ibm.com>
91da177e4SLinus Torvalds  *		 Tuan Ngo-Anh <ngoanh@de.ibm.com>
101da177e4SLinus Torvalds  *		 Martin Schwidefsky <schwidefsky@de.ibm.com>
111da177e4SLinus Torvalds  */
121da177e4SLinus Torvalds 
13bb509912SMichael Holzheu #define KMSG_COMPONENT "tape"
14bb509912SMichael Holzheu #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
15bb509912SMichael Holzheu 
161da177e4SLinus Torvalds #include <linux/module.h>
171da177e4SLinus Torvalds #include <linux/types.h>
181da177e4SLinus Torvalds #include <linux/proc_fs.h>
191da177e4SLinus Torvalds #include <linux/mtio.h>
20c5406079SHeiko Carstens #include <linux/compat.h>
211da177e4SLinus Torvalds 
227c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
231da177e4SLinus Torvalds 
241da177e4SLinus Torvalds #define TAPE_DBF_AREA	tape_core_dbf
251da177e4SLinus Torvalds 
261da177e4SLinus Torvalds #include "tape.h"
271da177e4SLinus Torvalds #include "tape_std.h"
281da177e4SLinus Torvalds #include "tape_class.h"
291da177e4SLinus Torvalds 
301da177e4SLinus Torvalds #define TAPECHAR_MAJOR		0	/* get dynamic major */
311da177e4SLinus Torvalds 
321da177e4SLinus Torvalds /*
331da177e4SLinus Torvalds  * file operation structure for tape character frontend
341da177e4SLinus Torvalds  */
351da177e4SLinus Torvalds static ssize_t tapechar_read(struct file *, char __user *, size_t, loff_t *);
361da177e4SLinus Torvalds static ssize_t tapechar_write(struct file *, const char __user *, size_t, loff_t *);
371da177e4SLinus Torvalds static int tapechar_open(struct inode *,struct file *);
381da177e4SLinus Torvalds static int tapechar_release(struct inode *,struct file *);
39369a4632SMartin Schwidefsky static long tapechar_ioctl(struct file *, unsigned int, unsigned long);
40c5406079SHeiko Carstens #ifdef CONFIG_COMPAT
41c5406079SHeiko Carstens static long tapechar_compat_ioctl(struct file *, unsigned int, unsigned long);
42c5406079SHeiko Carstens #endif
431da177e4SLinus Torvalds 
44d54b1fdbSArjan van de Ven static const struct file_operations tape_fops =
451da177e4SLinus Torvalds {
461da177e4SLinus Torvalds 	.owner = THIS_MODULE,
471da177e4SLinus Torvalds 	.read = tapechar_read,
481da177e4SLinus Torvalds 	.write = tapechar_write,
49369a4632SMartin Schwidefsky 	.unlocked_ioctl = tapechar_ioctl,
50c5406079SHeiko Carstens #ifdef CONFIG_COMPAT
51f042e0f8SChristoph Hellwig 	.compat_ioctl = tapechar_compat_ioctl,
52c5406079SHeiko Carstens #endif
531da177e4SLinus Torvalds 	.open = tapechar_open,
541da177e4SLinus Torvalds 	.release = tapechar_release,
551da177e4SLinus Torvalds };
561da177e4SLinus Torvalds 
571da177e4SLinus Torvalds static int tapechar_major = TAPECHAR_MAJOR;
581da177e4SLinus Torvalds 
591da177e4SLinus Torvalds /*
601da177e4SLinus Torvalds  * This function is called for every new tapedevice
611da177e4SLinus Torvalds  */
621da177e4SLinus Torvalds int
631da177e4SLinus Torvalds tapechar_setup_device(struct tape_device * device)
641da177e4SLinus Torvalds {
651da177e4SLinus Torvalds 	char	device_name[20];
661da177e4SLinus Torvalds 
671da177e4SLinus Torvalds 	sprintf(device_name, "ntibm%i", device->first_minor / 2);
681da177e4SLinus Torvalds 	device->nt = register_tape_dev(
691da177e4SLinus Torvalds 		&device->cdev->dev,
701da177e4SLinus Torvalds 		MKDEV(tapechar_major, device->first_minor),
711da177e4SLinus Torvalds 		&tape_fops,
721da177e4SLinus Torvalds 		device_name,
731da177e4SLinus Torvalds 		"non-rewinding"
741da177e4SLinus Torvalds 	);
751da177e4SLinus Torvalds 	device_name[0] = 'r';
761da177e4SLinus Torvalds 	device->rt = register_tape_dev(
771da177e4SLinus Torvalds 		&device->cdev->dev,
781da177e4SLinus Torvalds 		MKDEV(tapechar_major, device->first_minor + 1),
791da177e4SLinus Torvalds 		&tape_fops,
801da177e4SLinus Torvalds 		device_name,
811da177e4SLinus Torvalds 		"rewinding"
821da177e4SLinus Torvalds 	);
831da177e4SLinus Torvalds 
841da177e4SLinus Torvalds 	return 0;
851da177e4SLinus Torvalds }
861da177e4SLinus Torvalds 
871da177e4SLinus Torvalds void
881da177e4SLinus Torvalds tapechar_cleanup_device(struct tape_device *device)
891da177e4SLinus Torvalds {
9092bf435fSMichael Holzheu 	unregister_tape_dev(&device->cdev->dev, device->rt);
911da177e4SLinus Torvalds 	device->rt = NULL;
9292bf435fSMichael Holzheu 	unregister_tape_dev(&device->cdev->dev, device->nt);
931da177e4SLinus Torvalds 	device->nt = NULL;
941da177e4SLinus Torvalds }
951da177e4SLinus Torvalds 
964d284cacSHeiko Carstens static int
971da177e4SLinus Torvalds tapechar_check_idalbuffer(struct tape_device *device, size_t block_size)
981da177e4SLinus Torvalds {
991da177e4SLinus Torvalds 	struct idal_buffer *new;
1001da177e4SLinus Torvalds 
1011da177e4SLinus Torvalds 	if (device->char_data.idal_buf != NULL &&
1021da177e4SLinus Torvalds 	    device->char_data.idal_buf->size == block_size)
1031da177e4SLinus Torvalds 		return 0;
1041da177e4SLinus Torvalds 
1051da177e4SLinus Torvalds 	if (block_size > MAX_BLOCKSIZE) {
1061da177e4SLinus Torvalds 		DBF_EVENT(3, "Invalid blocksize (%zd > %d)\n",
1071da177e4SLinus Torvalds 			block_size, MAX_BLOCKSIZE);
1081da177e4SLinus Torvalds 		return -EINVAL;
1091da177e4SLinus Torvalds 	}
1101da177e4SLinus Torvalds 
1111da177e4SLinus Torvalds 	/* The current idal buffer is not correct. Allocate a new one. */
1121da177e4SLinus Torvalds 	new = idal_buffer_alloc(block_size, 0);
1130983e568SJulien Brunel 	if (IS_ERR(new))
1141da177e4SLinus Torvalds 		return -ENOMEM;
1151da177e4SLinus Torvalds 
1161da177e4SLinus Torvalds 	if (device->char_data.idal_buf != NULL)
1171da177e4SLinus Torvalds 		idal_buffer_free(device->char_data.idal_buf);
1181da177e4SLinus Torvalds 
1191da177e4SLinus Torvalds 	device->char_data.idal_buf = new;
1201da177e4SLinus Torvalds 
1211da177e4SLinus Torvalds 	return 0;
1221da177e4SLinus Torvalds }
1231da177e4SLinus Torvalds 
1241da177e4SLinus Torvalds /*
1251da177e4SLinus Torvalds  * Tape device read function
1261da177e4SLinus Torvalds  */
1272b67fc46SHeiko Carstens static ssize_t
1281da177e4SLinus Torvalds tapechar_read(struct file *filp, char __user *data, size_t count, loff_t *ppos)
1291da177e4SLinus Torvalds {
1301da177e4SLinus Torvalds 	struct tape_device *device;
1311da177e4SLinus Torvalds 	struct tape_request *request;
1321da177e4SLinus Torvalds 	size_t block_size;
1331da177e4SLinus Torvalds 	int rc;
1341da177e4SLinus Torvalds 
1351da177e4SLinus Torvalds 	DBF_EVENT(6, "TCHAR:read\n");
1361da177e4SLinus Torvalds 	device = (struct tape_device *) filp->private_data;
1371da177e4SLinus Torvalds 
1381da177e4SLinus Torvalds 	/*
1391da177e4SLinus Torvalds 	 * If the tape isn't terminated yet, do it now. And since we then
1401da177e4SLinus Torvalds 	 * are at the end of the tape there wouldn't be anything to read
14125985edcSLucas De Marchi 	 * anyways. So we return immediately.
1421da177e4SLinus Torvalds 	 */
1431da177e4SLinus Torvalds 	if(device->required_tapemarks) {
1441da177e4SLinus Torvalds 		return tape_std_terminate_write(device);
1451da177e4SLinus Torvalds 	}
1461da177e4SLinus Torvalds 
1471da177e4SLinus Torvalds 	/* Find out block size to use */
1481da177e4SLinus Torvalds 	if (device->char_data.block_size != 0) {
1491da177e4SLinus Torvalds 		if (count < device->char_data.block_size) {
1501da177e4SLinus Torvalds 			DBF_EVENT(3, "TCHAR:read smaller than block "
1511da177e4SLinus Torvalds 				  "size was requested\n");
1521da177e4SLinus Torvalds 			return -EINVAL;
1531da177e4SLinus Torvalds 		}
1541da177e4SLinus Torvalds 		block_size = device->char_data.block_size;
1551da177e4SLinus Torvalds 	} else {
1561da177e4SLinus Torvalds 		block_size = count;
1571da177e4SLinus Torvalds 	}
1581da177e4SLinus Torvalds 
1591da177e4SLinus Torvalds 	rc = tapechar_check_idalbuffer(device, block_size);
1601da177e4SLinus Torvalds 	if (rc)
1611da177e4SLinus Torvalds 		return rc;
1621da177e4SLinus Torvalds 
1631da177e4SLinus Torvalds 	DBF_EVENT(6, "TCHAR:nbytes: %lx\n", block_size);
1641da177e4SLinus Torvalds 	/* Let the discipline build the ccw chain. */
1651da177e4SLinus Torvalds 	request = device->discipline->read_block(device, block_size);
1661da177e4SLinus Torvalds 	if (IS_ERR(request))
1671da177e4SLinus Torvalds 		return PTR_ERR(request);
1681da177e4SLinus Torvalds 	/* Execute it. */
1691da177e4SLinus Torvalds 	rc = tape_do_io(device, request);
1701da177e4SLinus Torvalds 	if (rc == 0) {
1711da177e4SLinus Torvalds 		rc = block_size - request->rescnt;
1721da177e4SLinus Torvalds 		DBF_EVENT(6, "TCHAR:rbytes:  %x\n", rc);
1731da177e4SLinus Torvalds 		/* Copy data from idal buffer to user space. */
1741da177e4SLinus Torvalds 		if (idal_buffer_to_user(device->char_data.idal_buf,
1751da177e4SLinus Torvalds 					data, rc) != 0)
1761da177e4SLinus Torvalds 			rc = -EFAULT;
1771da177e4SLinus Torvalds 	}
1781da177e4SLinus Torvalds 	tape_free_request(request);
1791da177e4SLinus Torvalds 	return rc;
1801da177e4SLinus Torvalds }
1811da177e4SLinus Torvalds 
1821da177e4SLinus Torvalds /*
1831da177e4SLinus Torvalds  * Tape device write function
1841da177e4SLinus Torvalds  */
1852b67fc46SHeiko Carstens static ssize_t
1861da177e4SLinus Torvalds tapechar_write(struct file *filp, const char __user *data, size_t count, loff_t *ppos)
1871da177e4SLinus Torvalds {
1881da177e4SLinus Torvalds 	struct tape_device *device;
1891da177e4SLinus Torvalds 	struct tape_request *request;
1901da177e4SLinus Torvalds 	size_t block_size;
1911da177e4SLinus Torvalds 	size_t written;
1921da177e4SLinus Torvalds 	int nblocks;
1931da177e4SLinus Torvalds 	int i, rc;
1941da177e4SLinus Torvalds 
1951da177e4SLinus Torvalds 	DBF_EVENT(6, "TCHAR:write\n");
1961da177e4SLinus Torvalds 	device = (struct tape_device *) filp->private_data;
1971da177e4SLinus Torvalds 	/* Find out block size and number of blocks */
1981da177e4SLinus Torvalds 	if (device->char_data.block_size != 0) {
1991da177e4SLinus Torvalds 		if (count < device->char_data.block_size) {
2001da177e4SLinus Torvalds 			DBF_EVENT(3, "TCHAR:write smaller than block "
2011da177e4SLinus Torvalds 				  "size was requested\n");
2021da177e4SLinus Torvalds 			return -EINVAL;
2031da177e4SLinus Torvalds 		}
2041da177e4SLinus Torvalds 		block_size = device->char_data.block_size;
2051da177e4SLinus Torvalds 		nblocks = count / block_size;
2061da177e4SLinus Torvalds 	} else {
2071da177e4SLinus Torvalds 		block_size = count;
2081da177e4SLinus Torvalds 		nblocks = 1;
2091da177e4SLinus Torvalds 	}
2101da177e4SLinus Torvalds 
2111da177e4SLinus Torvalds 	rc = tapechar_check_idalbuffer(device, block_size);
2121da177e4SLinus Torvalds 	if (rc)
2131da177e4SLinus Torvalds 		return rc;
2141da177e4SLinus Torvalds 
2151da177e4SLinus Torvalds 	DBF_EVENT(6,"TCHAR:nbytes: %lx\n", block_size);
2161da177e4SLinus Torvalds 	DBF_EVENT(6, "TCHAR:nblocks: %x\n", nblocks);
2171da177e4SLinus Torvalds 	/* Let the discipline build the ccw chain. */
2181da177e4SLinus Torvalds 	request = device->discipline->write_block(device, block_size);
2191da177e4SLinus Torvalds 	if (IS_ERR(request))
2201da177e4SLinus Torvalds 		return PTR_ERR(request);
2211da177e4SLinus Torvalds 	rc = 0;
2221da177e4SLinus Torvalds 	written = 0;
2231da177e4SLinus Torvalds 	for (i = 0; i < nblocks; i++) {
2241da177e4SLinus Torvalds 		/* Copy data from user space to idal buffer. */
2251da177e4SLinus Torvalds 		if (idal_buffer_from_user(device->char_data.idal_buf,
2261da177e4SLinus Torvalds 					  data, block_size)) {
2271da177e4SLinus Torvalds 			rc = -EFAULT;
2281da177e4SLinus Torvalds 			break;
2291da177e4SLinus Torvalds 		}
2301da177e4SLinus Torvalds 		rc = tape_do_io(device, request);
2311da177e4SLinus Torvalds 		if (rc)
2321da177e4SLinus Torvalds 			break;
2331da177e4SLinus Torvalds 		DBF_EVENT(6, "TCHAR:wbytes: %lx\n",
2341da177e4SLinus Torvalds 			  block_size - request->rescnt);
2351da177e4SLinus Torvalds 		written += block_size - request->rescnt;
2361da177e4SLinus Torvalds 		if (request->rescnt != 0)
2371da177e4SLinus Torvalds 			break;
2381da177e4SLinus Torvalds 		data += block_size;
2391da177e4SLinus Torvalds 	}
2401da177e4SLinus Torvalds 	tape_free_request(request);
2411da177e4SLinus Torvalds 	if (rc == -ENOSPC) {
2421da177e4SLinus Torvalds 		/*
2431da177e4SLinus Torvalds 		 * Ok, the device has no more space. It has NOT written
2441da177e4SLinus Torvalds 		 * the block.
2451da177e4SLinus Torvalds 		 */
2461da177e4SLinus Torvalds 		if (device->discipline->process_eov)
2471da177e4SLinus Torvalds 			device->discipline->process_eov(device);
2481da177e4SLinus Torvalds 		if (written > 0)
2491da177e4SLinus Torvalds 			rc = 0;
2501da177e4SLinus Torvalds 
2511da177e4SLinus Torvalds 	}
2521da177e4SLinus Torvalds 
2531da177e4SLinus Torvalds 	/*
2541da177e4SLinus Torvalds 	 * After doing a write we always need two tapemarks to correctly
2551da177e4SLinus Torvalds 	 * terminate the tape (one to terminate the file, the second to
2561da177e4SLinus Torvalds 	 * flag the end of recorded data.
2571da177e4SLinus Torvalds 	 * Since process_eov positions the tape in front of the written
2581da177e4SLinus Torvalds 	 * tapemark it doesn't hurt to write two marks again.
2591da177e4SLinus Torvalds 	 */
2601da177e4SLinus Torvalds 	if (!rc)
2611da177e4SLinus Torvalds 		device->required_tapemarks = 2;
2621da177e4SLinus Torvalds 
2631da177e4SLinus Torvalds 	return rc ? rc : written;
2641da177e4SLinus Torvalds }
2651da177e4SLinus Torvalds 
2661da177e4SLinus Torvalds /*
2671da177e4SLinus Torvalds  * Character frontend tape device open function.
2681da177e4SLinus Torvalds  */
2692b67fc46SHeiko Carstens static int
2701da177e4SLinus Torvalds tapechar_open (struct inode *inode, struct file *filp)
2711da177e4SLinus Torvalds {
2721da177e4SLinus Torvalds 	struct tape_device *device;
2731da177e4SLinus Torvalds 	int minor, rc;
2741da177e4SLinus Torvalds 
2751da177e4SLinus Torvalds 	DBF_EVENT(6, "TCHAR:open: %i:%i\n",
276496ad9aaSAl Viro 		imajor(file_inode(filp)),
277496ad9aaSAl Viro 		iminor(file_inode(filp)));
2781da177e4SLinus Torvalds 
279496ad9aaSAl Viro 	if (imajor(file_inode(filp)) != tapechar_major)
2801da177e4SLinus Torvalds 		return -ENODEV;
2811da177e4SLinus Torvalds 
282496ad9aaSAl Viro 	minor = iminor(file_inode(filp));
2838fd138c3SMartin Schwidefsky 	device = tape_find_device(minor / TAPE_MINORS_PER_DEV);
2841da177e4SLinus Torvalds 	if (IS_ERR(device)) {
2858fd138c3SMartin Schwidefsky 		DBF_EVENT(3, "TCHAR:open: tape_find_device() failed\n");
286369a4632SMartin Schwidefsky 		return PTR_ERR(device);
2871da177e4SLinus Torvalds 	}
2881da177e4SLinus Torvalds 
2891da177e4SLinus Torvalds 	rc = tape_open(device);
2901da177e4SLinus Torvalds 	if (rc == 0) {
2911da177e4SLinus Torvalds 		filp->private_data = device;
292c5bf68feSKirill Smelkov 		stream_open(inode, filp);
293369a4632SMartin Schwidefsky 	} else
2941da177e4SLinus Torvalds 		tape_put_device(device);
2951da177e4SLinus Torvalds 
2961da177e4SLinus Torvalds 	return rc;
2971da177e4SLinus Torvalds }
2981da177e4SLinus Torvalds 
2991da177e4SLinus Torvalds /*
3001da177e4SLinus Torvalds  * Character frontend tape device release function.
3011da177e4SLinus Torvalds  */
3021da177e4SLinus Torvalds 
3032b67fc46SHeiko Carstens static int
3041da177e4SLinus Torvalds tapechar_release(struct inode *inode, struct file *filp)
3051da177e4SLinus Torvalds {
3061da177e4SLinus Torvalds 	struct tape_device *device;
3071da177e4SLinus Torvalds 
3081da177e4SLinus Torvalds 	DBF_EVENT(6, "TCHAR:release: %x\n", iminor(inode));
3091da177e4SLinus Torvalds 	device = (struct tape_device *) filp->private_data;
3101da177e4SLinus Torvalds 
3111da177e4SLinus Torvalds 	/*
3121da177e4SLinus Torvalds 	 * If this is the rewinding tape minor then rewind. In that case we
3131da177e4SLinus Torvalds 	 * write all required tapemarks. Otherwise only one to terminate the
3141da177e4SLinus Torvalds 	 * file.
3151da177e4SLinus Torvalds 	 */
3161da177e4SLinus Torvalds 	if ((iminor(inode) & 1) != 0) {
3171da177e4SLinus Torvalds 		if (device->required_tapemarks)
3181da177e4SLinus Torvalds 			tape_std_terminate_write(device);
3191da177e4SLinus Torvalds 		tape_mtop(device, MTREW, 1);
3201da177e4SLinus Torvalds 	} else {
3211da177e4SLinus Torvalds 		if (device->required_tapemarks > 1) {
3221da177e4SLinus Torvalds 			if (tape_mtop(device, MTWEOF, 1) == 0)
3231da177e4SLinus Torvalds 				device->required_tapemarks--;
3241da177e4SLinus Torvalds 		}
3251da177e4SLinus Torvalds 	}
3261da177e4SLinus Torvalds 
3271da177e4SLinus Torvalds 	if (device->char_data.idal_buf != NULL) {
3281da177e4SLinus Torvalds 		idal_buffer_free(device->char_data.idal_buf);
3291da177e4SLinus Torvalds 		device->char_data.idal_buf = NULL;
3301da177e4SLinus Torvalds 	}
3311da177e4SLinus Torvalds 	tape_release(device);
3328fd138c3SMartin Schwidefsky 	filp->private_data = NULL;
3338fd138c3SMartin Schwidefsky 	tape_put_device(device);
3341da177e4SLinus Torvalds 
3351da177e4SLinus Torvalds 	return 0;
3361da177e4SLinus Torvalds }
3371da177e4SLinus Torvalds 
3381da177e4SLinus Torvalds /*
3391da177e4SLinus Torvalds  * Tape device io controls.
3401da177e4SLinus Torvalds  */
3411da177e4SLinus Torvalds static int
342369a4632SMartin Schwidefsky __tapechar_ioctl(struct tape_device *device,
343*1207045dSArnd Bergmann 		 unsigned int no, void __user *data)
3441da177e4SLinus Torvalds {
3451da177e4SLinus Torvalds 	int rc;
3461da177e4SLinus Torvalds 
3471da177e4SLinus Torvalds 	if (no == MTIOCTOP) {
3481da177e4SLinus Torvalds 		struct mtop op;
3491da177e4SLinus Torvalds 
350*1207045dSArnd Bergmann 		if (copy_from_user(&op, data, sizeof(op)) != 0)
3511da177e4SLinus Torvalds 			return -EFAULT;
3521da177e4SLinus Torvalds 		if (op.mt_count < 0)
3531da177e4SLinus Torvalds 			return -EINVAL;
3541da177e4SLinus Torvalds 
3551da177e4SLinus Torvalds 		/*
3561da177e4SLinus Torvalds 		 * Operations that change tape position should write final
3571da177e4SLinus Torvalds 		 * tapemarks.
3581da177e4SLinus Torvalds 		 */
3591da177e4SLinus Torvalds 		switch (op.mt_op) {
3601da177e4SLinus Torvalds 			case MTFSF:
3611da177e4SLinus Torvalds 			case MTBSF:
3621da177e4SLinus Torvalds 			case MTFSR:
3631da177e4SLinus Torvalds 			case MTBSR:
3641da177e4SLinus Torvalds 			case MTREW:
3651da177e4SLinus Torvalds 			case MTOFFL:
3661da177e4SLinus Torvalds 			case MTEOM:
3671da177e4SLinus Torvalds 			case MTRETEN:
3681da177e4SLinus Torvalds 			case MTBSFM:
3691da177e4SLinus Torvalds 			case MTFSFM:
3701da177e4SLinus Torvalds 			case MTSEEK:
3711da177e4SLinus Torvalds 				if (device->required_tapemarks)
3721da177e4SLinus Torvalds 					tape_std_terminate_write(device);
3731da177e4SLinus Torvalds 		}
3741da177e4SLinus Torvalds 		rc = tape_mtop(device, op.mt_op, op.mt_count);
3751da177e4SLinus Torvalds 
3761da177e4SLinus Torvalds 		if (op.mt_op == MTWEOF && rc == 0) {
3771da177e4SLinus Torvalds 			if (op.mt_count > device->required_tapemarks)
3781da177e4SLinus Torvalds 				device->required_tapemarks = 0;
3791da177e4SLinus Torvalds 			else
3801da177e4SLinus Torvalds 				device->required_tapemarks -= op.mt_count;
3811da177e4SLinus Torvalds 		}
3821da177e4SLinus Torvalds 		return rc;
3831da177e4SLinus Torvalds 	}
3841da177e4SLinus Torvalds 	if (no == MTIOCPOS) {
3851da177e4SLinus Torvalds 		/* MTIOCPOS: query the tape position. */
3861da177e4SLinus Torvalds 		struct mtpos pos;
3871da177e4SLinus Torvalds 
3881da177e4SLinus Torvalds 		rc = tape_mtop(device, MTTELL, 1);
3891da177e4SLinus Torvalds 		if (rc < 0)
3901da177e4SLinus Torvalds 			return rc;
3911da177e4SLinus Torvalds 		pos.mt_blkno = rc;
392*1207045dSArnd Bergmann 		return put_user_mtpos(data, &pos);
3931da177e4SLinus Torvalds 	}
3941da177e4SLinus Torvalds 	if (no == MTIOCGET) {
3951da177e4SLinus Torvalds 		/* MTIOCGET: query the tape drive status. */
3961da177e4SLinus Torvalds 		struct mtget get;
3971da177e4SLinus Torvalds 
3981da177e4SLinus Torvalds 		memset(&get, 0, sizeof(get));
3991da177e4SLinus Torvalds 		get.mt_type = MT_ISUNKNOWN;
4001da177e4SLinus Torvalds 		get.mt_resid = 0 /* device->devstat.rescnt */;
4019fc98ad0SStefan Haberland 		get.mt_dsreg =
4029fc98ad0SStefan Haberland 			((device->char_data.block_size << MT_ST_BLKSIZE_SHIFT)
4039fc98ad0SStefan Haberland 			 & MT_ST_BLKSIZE_MASK);
4041da177e4SLinus Torvalds 		/* FIXME: mt_gstat, mt_erreg, mt_fileno */
4051da177e4SLinus Torvalds 		get.mt_gstat = 0;
4061da177e4SLinus Torvalds 		get.mt_erreg = 0;
4071da177e4SLinus Torvalds 		get.mt_fileno = 0;
4081da177e4SLinus Torvalds 		get.mt_gstat  = device->tape_generic_status;
4091da177e4SLinus Torvalds 
4101da177e4SLinus Torvalds 		if (device->medium_state == MS_LOADED) {
4111da177e4SLinus Torvalds 			rc = tape_mtop(device, MTTELL, 1);
4121da177e4SLinus Torvalds 
4131da177e4SLinus Torvalds 			if (rc < 0)
4141da177e4SLinus Torvalds 				return rc;
4151da177e4SLinus Torvalds 
4161da177e4SLinus Torvalds 			if (rc == 0)
4171da177e4SLinus Torvalds 				get.mt_gstat |= GMT_BOT(~0);
4181da177e4SLinus Torvalds 
4191da177e4SLinus Torvalds 			get.mt_blkno = rc;
4201da177e4SLinus Torvalds 		}
4211da177e4SLinus Torvalds 
422*1207045dSArnd Bergmann 		return put_user_mtget(data, &get);
4231da177e4SLinus Torvalds 	}
4241da177e4SLinus Torvalds 	/* Try the discipline ioctl function. */
4251da177e4SLinus Torvalds 	if (device->discipline->ioctl_fn == NULL)
4261da177e4SLinus Torvalds 		return -EINVAL;
427*1207045dSArnd Bergmann 	return device->discipline->ioctl_fn(device, no, (unsigned long)data);
4281da177e4SLinus Torvalds }
4291da177e4SLinus Torvalds 
430f042e0f8SChristoph Hellwig static long
431369a4632SMartin Schwidefsky tapechar_ioctl(struct file *filp, unsigned int no, unsigned long data)
432369a4632SMartin Schwidefsky {
433369a4632SMartin Schwidefsky 	struct tape_device *device;
434369a4632SMartin Schwidefsky 	long rc;
435369a4632SMartin Schwidefsky 
436369a4632SMartin Schwidefsky 	DBF_EVENT(6, "TCHAR:ioct\n");
437369a4632SMartin Schwidefsky 
438369a4632SMartin Schwidefsky 	device = (struct tape_device *) filp->private_data;
439369a4632SMartin Schwidefsky 	mutex_lock(&device->mutex);
440*1207045dSArnd Bergmann 	rc = __tapechar_ioctl(device, no, (void __user *)data);
441369a4632SMartin Schwidefsky 	mutex_unlock(&device->mutex);
442369a4632SMartin Schwidefsky 	return rc;
443369a4632SMartin Schwidefsky }
444369a4632SMartin Schwidefsky 
445c5406079SHeiko Carstens #ifdef CONFIG_COMPAT
446369a4632SMartin Schwidefsky static long
447f042e0f8SChristoph Hellwig tapechar_compat_ioctl(struct file *filp, unsigned int no, unsigned long data)
448f042e0f8SChristoph Hellwig {
449f042e0f8SChristoph Hellwig 	struct tape_device *device = filp->private_data;
450*1207045dSArnd Bergmann 	long rc;
451f042e0f8SChristoph Hellwig 
452*1207045dSArnd Bergmann 	if (no == MTIOCPOS32)
453*1207045dSArnd Bergmann 		no = MTIOCPOS;
454*1207045dSArnd Bergmann 	else if (no == MTIOCGET32)
455*1207045dSArnd Bergmann 		no = MTIOCGET;
456*1207045dSArnd Bergmann 
457369a4632SMartin Schwidefsky 	mutex_lock(&device->mutex);
458*1207045dSArnd Bergmann 	rc = __tapechar_ioctl(device, no, compat_ptr(data));
459369a4632SMartin Schwidefsky 	mutex_unlock(&device->mutex);
460*1207045dSArnd Bergmann 	return rc;
461f042e0f8SChristoph Hellwig }
462c5406079SHeiko Carstens #endif /* CONFIG_COMPAT */
463f042e0f8SChristoph Hellwig 
4641da177e4SLinus Torvalds /*
4651da177e4SLinus Torvalds  * Initialize character device frontend.
4661da177e4SLinus Torvalds  */
4671da177e4SLinus Torvalds int
4681da177e4SLinus Torvalds tapechar_init (void)
4691da177e4SLinus Torvalds {
4701da177e4SLinus Torvalds 	dev_t	dev;
4711da177e4SLinus Torvalds 
4721da177e4SLinus Torvalds 	if (alloc_chrdev_region(&dev, 0, 256, "tape") != 0)
4731da177e4SLinus Torvalds 		return -1;
4741da177e4SLinus Torvalds 
4751da177e4SLinus Torvalds 	tapechar_major = MAJOR(dev);
4761da177e4SLinus Torvalds 
4771da177e4SLinus Torvalds 	return 0;
4781da177e4SLinus Torvalds }
4791da177e4SLinus Torvalds 
4801da177e4SLinus Torvalds /*
4811da177e4SLinus Torvalds  * cleanup
4821da177e4SLinus Torvalds  */
4831da177e4SLinus Torvalds void
4841da177e4SLinus Torvalds tapechar_exit(void)
4851da177e4SLinus Torvalds {
4861da177e4SLinus Torvalds 	unregister_chrdev_region(MKDEV(tapechar_major, 0), 256);
4871da177e4SLinus Torvalds }
488