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,
556038f373SArnd Bergmann .llseek = no_llseek,
561da177e4SLinus Torvalds };
571da177e4SLinus Torvalds
581da177e4SLinus Torvalds static int tapechar_major = TAPECHAR_MAJOR;
591da177e4SLinus Torvalds
601da177e4SLinus Torvalds /*
611da177e4SLinus Torvalds * This function is called for every new tapedevice
621da177e4SLinus Torvalds */
631da177e4SLinus Torvalds int
tapechar_setup_device(struct tape_device * device)641da177e4SLinus Torvalds tapechar_setup_device(struct tape_device * device)
651da177e4SLinus Torvalds {
661da177e4SLinus Torvalds char device_name[20];
671da177e4SLinus Torvalds
681da177e4SLinus Torvalds sprintf(device_name, "ntibm%i", device->first_minor / 2);
691da177e4SLinus Torvalds device->nt = register_tape_dev(
701da177e4SLinus Torvalds &device->cdev->dev,
711da177e4SLinus Torvalds MKDEV(tapechar_major, device->first_minor),
721da177e4SLinus Torvalds &tape_fops,
731da177e4SLinus Torvalds device_name,
741da177e4SLinus Torvalds "non-rewinding"
751da177e4SLinus Torvalds );
761da177e4SLinus Torvalds device_name[0] = 'r';
771da177e4SLinus Torvalds device->rt = register_tape_dev(
781da177e4SLinus Torvalds &device->cdev->dev,
791da177e4SLinus Torvalds MKDEV(tapechar_major, device->first_minor + 1),
801da177e4SLinus Torvalds &tape_fops,
811da177e4SLinus Torvalds device_name,
821da177e4SLinus Torvalds "rewinding"
831da177e4SLinus Torvalds );
841da177e4SLinus Torvalds
851da177e4SLinus Torvalds return 0;
861da177e4SLinus Torvalds }
871da177e4SLinus Torvalds
881da177e4SLinus Torvalds void
tapechar_cleanup_device(struct tape_device * device)891da177e4SLinus Torvalds tapechar_cleanup_device(struct tape_device *device)
901da177e4SLinus Torvalds {
9192bf435fSMichael Holzheu unregister_tape_dev(&device->cdev->dev, device->rt);
921da177e4SLinus Torvalds device->rt = NULL;
9392bf435fSMichael Holzheu unregister_tape_dev(&device->cdev->dev, device->nt);
941da177e4SLinus Torvalds device->nt = NULL;
951da177e4SLinus Torvalds }
961da177e4SLinus Torvalds
974d284cacSHeiko Carstens static int
tapechar_check_idalbuffer(struct tape_device * device,size_t block_size)981da177e4SLinus Torvalds tapechar_check_idalbuffer(struct tape_device *device, size_t block_size)
991da177e4SLinus Torvalds {
1001da177e4SLinus Torvalds struct idal_buffer *new;
1011da177e4SLinus Torvalds
1021da177e4SLinus Torvalds if (device->char_data.idal_buf != NULL &&
1031da177e4SLinus Torvalds device->char_data.idal_buf->size == block_size)
1041da177e4SLinus Torvalds return 0;
1051da177e4SLinus Torvalds
1061da177e4SLinus Torvalds if (block_size > MAX_BLOCKSIZE) {
1071da177e4SLinus Torvalds DBF_EVENT(3, "Invalid blocksize (%zd > %d)\n",
1081da177e4SLinus Torvalds block_size, MAX_BLOCKSIZE);
1091da177e4SLinus Torvalds return -EINVAL;
1101da177e4SLinus Torvalds }
1111da177e4SLinus Torvalds
1121da177e4SLinus Torvalds /* The current idal buffer is not correct. Allocate a new one. */
1131da177e4SLinus Torvalds new = idal_buffer_alloc(block_size, 0);
1140983e568SJulien Brunel if (IS_ERR(new))
1151da177e4SLinus Torvalds return -ENOMEM;
1161da177e4SLinus Torvalds
1171da177e4SLinus Torvalds if (device->char_data.idal_buf != NULL)
1181da177e4SLinus Torvalds idal_buffer_free(device->char_data.idal_buf);
1191da177e4SLinus Torvalds
1201da177e4SLinus Torvalds device->char_data.idal_buf = new;
1211da177e4SLinus Torvalds
1221da177e4SLinus Torvalds return 0;
1231da177e4SLinus Torvalds }
1241da177e4SLinus Torvalds
1251da177e4SLinus Torvalds /*
1261da177e4SLinus Torvalds * Tape device read function
1271da177e4SLinus Torvalds */
1282b67fc46SHeiko Carstens static ssize_t
tapechar_read(struct file * filp,char __user * data,size_t count,loff_t * ppos)1291da177e4SLinus Torvalds tapechar_read(struct file *filp, char __user *data, size_t count, loff_t *ppos)
1301da177e4SLinus Torvalds {
1311da177e4SLinus Torvalds struct tape_device *device;
1321da177e4SLinus Torvalds struct tape_request *request;
1331da177e4SLinus Torvalds size_t block_size;
1341da177e4SLinus Torvalds int rc;
1351da177e4SLinus Torvalds
1361da177e4SLinus Torvalds DBF_EVENT(6, "TCHAR:read\n");
1371da177e4SLinus Torvalds device = (struct tape_device *) filp->private_data;
1381da177e4SLinus Torvalds
1391da177e4SLinus Torvalds /*
1401da177e4SLinus Torvalds * If the tape isn't terminated yet, do it now. And since we then
1411da177e4SLinus Torvalds * are at the end of the tape there wouldn't be anything to read
14225985edcSLucas De Marchi * anyways. So we return immediately.
1431da177e4SLinus Torvalds */
1441da177e4SLinus Torvalds if(device->required_tapemarks) {
1451da177e4SLinus Torvalds return tape_std_terminate_write(device);
1461da177e4SLinus Torvalds }
1471da177e4SLinus Torvalds
1481da177e4SLinus Torvalds /* Find out block size to use */
1491da177e4SLinus Torvalds if (device->char_data.block_size != 0) {
1501da177e4SLinus Torvalds if (count < device->char_data.block_size) {
1511da177e4SLinus Torvalds DBF_EVENT(3, "TCHAR:read smaller than block "
1521da177e4SLinus Torvalds "size was requested\n");
1531da177e4SLinus Torvalds return -EINVAL;
1541da177e4SLinus Torvalds }
1551da177e4SLinus Torvalds block_size = device->char_data.block_size;
1561da177e4SLinus Torvalds } else {
1571da177e4SLinus Torvalds block_size = count;
1581da177e4SLinus Torvalds }
1591da177e4SLinus Torvalds
1601da177e4SLinus Torvalds rc = tapechar_check_idalbuffer(device, block_size);
1611da177e4SLinus Torvalds if (rc)
1621da177e4SLinus Torvalds return rc;
1631da177e4SLinus Torvalds
1641da177e4SLinus Torvalds DBF_EVENT(6, "TCHAR:nbytes: %lx\n", block_size);
1651da177e4SLinus Torvalds /* Let the discipline build the ccw chain. */
1661da177e4SLinus Torvalds request = device->discipline->read_block(device, block_size);
1671da177e4SLinus Torvalds if (IS_ERR(request))
1681da177e4SLinus Torvalds return PTR_ERR(request);
1691da177e4SLinus Torvalds /* Execute it. */
1701da177e4SLinus Torvalds rc = tape_do_io(device, request);
1711da177e4SLinus Torvalds if (rc == 0) {
1721da177e4SLinus Torvalds rc = block_size - request->rescnt;
1731da177e4SLinus Torvalds DBF_EVENT(6, "TCHAR:rbytes: %x\n", rc);
1741da177e4SLinus Torvalds /* Copy data from idal buffer to user space. */
1751da177e4SLinus Torvalds if (idal_buffer_to_user(device->char_data.idal_buf,
1761da177e4SLinus Torvalds data, rc) != 0)
1771da177e4SLinus Torvalds rc = -EFAULT;
1781da177e4SLinus Torvalds }
1791da177e4SLinus Torvalds tape_free_request(request);
1801da177e4SLinus Torvalds return rc;
1811da177e4SLinus Torvalds }
1821da177e4SLinus Torvalds
1831da177e4SLinus Torvalds /*
1841da177e4SLinus Torvalds * Tape device write function
1851da177e4SLinus Torvalds */
1862b67fc46SHeiko Carstens static ssize_t
tapechar_write(struct file * filp,const char __user * data,size_t count,loff_t * ppos)1871da177e4SLinus Torvalds tapechar_write(struct file *filp, const char __user *data, size_t count, loff_t *ppos)
1881da177e4SLinus Torvalds {
1891da177e4SLinus Torvalds struct tape_device *device;
1901da177e4SLinus Torvalds struct tape_request *request;
1911da177e4SLinus Torvalds size_t block_size;
1921da177e4SLinus Torvalds size_t written;
1931da177e4SLinus Torvalds int nblocks;
1941da177e4SLinus Torvalds int i, rc;
1951da177e4SLinus Torvalds
1961da177e4SLinus Torvalds DBF_EVENT(6, "TCHAR:write\n");
1971da177e4SLinus Torvalds device = (struct tape_device *) filp->private_data;
1981da177e4SLinus Torvalds /* Find out block size and number of blocks */
1991da177e4SLinus Torvalds if (device->char_data.block_size != 0) {
2001da177e4SLinus Torvalds if (count < device->char_data.block_size) {
2011da177e4SLinus Torvalds DBF_EVENT(3, "TCHAR:write smaller than block "
2021da177e4SLinus Torvalds "size was requested\n");
2031da177e4SLinus Torvalds return -EINVAL;
2041da177e4SLinus Torvalds }
2051da177e4SLinus Torvalds block_size = device->char_data.block_size;
2061da177e4SLinus Torvalds nblocks = count / block_size;
2071da177e4SLinus Torvalds } else {
2081da177e4SLinus Torvalds block_size = count;
2091da177e4SLinus Torvalds nblocks = 1;
2101da177e4SLinus Torvalds }
2111da177e4SLinus Torvalds
2121da177e4SLinus Torvalds rc = tapechar_check_idalbuffer(device, block_size);
2131da177e4SLinus Torvalds if (rc)
2141da177e4SLinus Torvalds return rc;
2151da177e4SLinus Torvalds
2161da177e4SLinus Torvalds DBF_EVENT(6,"TCHAR:nbytes: %lx\n", block_size);
2171da177e4SLinus Torvalds DBF_EVENT(6, "TCHAR:nblocks: %x\n", nblocks);
2181da177e4SLinus Torvalds /* Let the discipline build the ccw chain. */
2191da177e4SLinus Torvalds request = device->discipline->write_block(device, block_size);
2201da177e4SLinus Torvalds if (IS_ERR(request))
2211da177e4SLinus Torvalds return PTR_ERR(request);
2221da177e4SLinus Torvalds rc = 0;
2231da177e4SLinus Torvalds written = 0;
2241da177e4SLinus Torvalds for (i = 0; i < nblocks; i++) {
2251da177e4SLinus Torvalds /* Copy data from user space to idal buffer. */
2261da177e4SLinus Torvalds if (idal_buffer_from_user(device->char_data.idal_buf,
2271da177e4SLinus Torvalds data, block_size)) {
2281da177e4SLinus Torvalds rc = -EFAULT;
2291da177e4SLinus Torvalds break;
2301da177e4SLinus Torvalds }
2311da177e4SLinus Torvalds rc = tape_do_io(device, request);
2321da177e4SLinus Torvalds if (rc)
2331da177e4SLinus Torvalds break;
2341da177e4SLinus Torvalds DBF_EVENT(6, "TCHAR:wbytes: %lx\n",
2351da177e4SLinus Torvalds block_size - request->rescnt);
2361da177e4SLinus Torvalds written += block_size - request->rescnt;
2371da177e4SLinus Torvalds if (request->rescnt != 0)
2381da177e4SLinus Torvalds break;
2391da177e4SLinus Torvalds data += block_size;
2401da177e4SLinus Torvalds }
2411da177e4SLinus Torvalds tape_free_request(request);
2421da177e4SLinus Torvalds if (rc == -ENOSPC) {
2431da177e4SLinus Torvalds /*
2441da177e4SLinus Torvalds * Ok, the device has no more space. It has NOT written
2451da177e4SLinus Torvalds * the block.
2461da177e4SLinus Torvalds */
2471da177e4SLinus Torvalds if (device->discipline->process_eov)
2481da177e4SLinus Torvalds device->discipline->process_eov(device);
2491da177e4SLinus Torvalds if (written > 0)
2501da177e4SLinus Torvalds rc = 0;
2511da177e4SLinus Torvalds
2521da177e4SLinus Torvalds }
2531da177e4SLinus Torvalds
2541da177e4SLinus Torvalds /*
2551da177e4SLinus Torvalds * After doing a write we always need two tapemarks to correctly
2561da177e4SLinus Torvalds * terminate the tape (one to terminate the file, the second to
2571da177e4SLinus Torvalds * flag the end of recorded data.
2581da177e4SLinus Torvalds * Since process_eov positions the tape in front of the written
2591da177e4SLinus Torvalds * tapemark it doesn't hurt to write two marks again.
2601da177e4SLinus Torvalds */
2611da177e4SLinus Torvalds if (!rc)
2621da177e4SLinus Torvalds device->required_tapemarks = 2;
2631da177e4SLinus Torvalds
2641da177e4SLinus Torvalds return rc ? rc : written;
2651da177e4SLinus Torvalds }
2661da177e4SLinus Torvalds
2671da177e4SLinus Torvalds /*
2681da177e4SLinus Torvalds * Character frontend tape device open function.
2691da177e4SLinus Torvalds */
2702b67fc46SHeiko Carstens static int
tapechar_open(struct inode * inode,struct file * filp)2711da177e4SLinus Torvalds tapechar_open (struct inode *inode, struct file *filp)
2721da177e4SLinus Torvalds {
2731da177e4SLinus Torvalds struct tape_device *device;
2741da177e4SLinus Torvalds int minor, rc;
2751da177e4SLinus Torvalds
2761da177e4SLinus Torvalds DBF_EVENT(6, "TCHAR:open: %i:%i\n",
277496ad9aaSAl Viro imajor(file_inode(filp)),
278496ad9aaSAl Viro iminor(file_inode(filp)));
2791da177e4SLinus Torvalds
280496ad9aaSAl Viro if (imajor(file_inode(filp)) != tapechar_major)
2811da177e4SLinus Torvalds return -ENODEV;
2821da177e4SLinus Torvalds
283496ad9aaSAl Viro minor = iminor(file_inode(filp));
2848fd138c3SMartin Schwidefsky device = tape_find_device(minor / TAPE_MINORS_PER_DEV);
2851da177e4SLinus Torvalds if (IS_ERR(device)) {
2868fd138c3SMartin Schwidefsky DBF_EVENT(3, "TCHAR:open: tape_find_device() failed\n");
287369a4632SMartin Schwidefsky return PTR_ERR(device);
2881da177e4SLinus Torvalds }
2891da177e4SLinus Torvalds
2901da177e4SLinus Torvalds rc = tape_open(device);
2911da177e4SLinus Torvalds if (rc == 0) {
2921da177e4SLinus Torvalds filp->private_data = device;
293c5bf68feSKirill Smelkov stream_open(inode, filp);
294369a4632SMartin Schwidefsky } else
2951da177e4SLinus Torvalds tape_put_device(device);
2961da177e4SLinus Torvalds
2971da177e4SLinus Torvalds return rc;
2981da177e4SLinus Torvalds }
2991da177e4SLinus Torvalds
3001da177e4SLinus Torvalds /*
3011da177e4SLinus Torvalds * Character frontend tape device release function.
3021da177e4SLinus Torvalds */
3031da177e4SLinus Torvalds
3042b67fc46SHeiko Carstens static int
tapechar_release(struct inode * inode,struct file * filp)3051da177e4SLinus Torvalds tapechar_release(struct inode *inode, struct file *filp)
3061da177e4SLinus Torvalds {
3071da177e4SLinus Torvalds struct tape_device *device;
3081da177e4SLinus Torvalds
3091da177e4SLinus Torvalds DBF_EVENT(6, "TCHAR:release: %x\n", iminor(inode));
3101da177e4SLinus Torvalds device = (struct tape_device *) filp->private_data;
3111da177e4SLinus Torvalds
3121da177e4SLinus Torvalds /*
3131da177e4SLinus Torvalds * If this is the rewinding tape minor then rewind. In that case we
3141da177e4SLinus Torvalds * write all required tapemarks. Otherwise only one to terminate the
3151da177e4SLinus Torvalds * file.
3161da177e4SLinus Torvalds */
3171da177e4SLinus Torvalds if ((iminor(inode) & 1) != 0) {
3181da177e4SLinus Torvalds if (device->required_tapemarks)
3191da177e4SLinus Torvalds tape_std_terminate_write(device);
3201da177e4SLinus Torvalds tape_mtop(device, MTREW, 1);
3211da177e4SLinus Torvalds } else {
3221da177e4SLinus Torvalds if (device->required_tapemarks > 1) {
3231da177e4SLinus Torvalds if (tape_mtop(device, MTWEOF, 1) == 0)
3241da177e4SLinus Torvalds device->required_tapemarks--;
3251da177e4SLinus Torvalds }
3261da177e4SLinus Torvalds }
3271da177e4SLinus Torvalds
3281da177e4SLinus Torvalds if (device->char_data.idal_buf != NULL) {
3291da177e4SLinus Torvalds idal_buffer_free(device->char_data.idal_buf);
3301da177e4SLinus Torvalds device->char_data.idal_buf = NULL;
3311da177e4SLinus Torvalds }
3321da177e4SLinus Torvalds tape_release(device);
3338fd138c3SMartin Schwidefsky filp->private_data = NULL;
3348fd138c3SMartin Schwidefsky tape_put_device(device);
3351da177e4SLinus Torvalds
3361da177e4SLinus Torvalds return 0;
3371da177e4SLinus Torvalds }
3381da177e4SLinus Torvalds
3391da177e4SLinus Torvalds /*
3401da177e4SLinus Torvalds * Tape device io controls.
3411da177e4SLinus Torvalds */
3421da177e4SLinus Torvalds static int
__tapechar_ioctl(struct tape_device * device,unsigned int no,void __user * data)343369a4632SMartin Schwidefsky __tapechar_ioctl(struct tape_device *device,
344*1207045dSArnd Bergmann unsigned int no, void __user *data)
3451da177e4SLinus Torvalds {
3461da177e4SLinus Torvalds int rc;
3471da177e4SLinus Torvalds
3481da177e4SLinus Torvalds if (no == MTIOCTOP) {
3491da177e4SLinus Torvalds struct mtop op;
3501da177e4SLinus Torvalds
351*1207045dSArnd Bergmann if (copy_from_user(&op, data, sizeof(op)) != 0)
3521da177e4SLinus Torvalds return -EFAULT;
3531da177e4SLinus Torvalds if (op.mt_count < 0)
3541da177e4SLinus Torvalds return -EINVAL;
3551da177e4SLinus Torvalds
3561da177e4SLinus Torvalds /*
3571da177e4SLinus Torvalds * Operations that change tape position should write final
3581da177e4SLinus Torvalds * tapemarks.
3591da177e4SLinus Torvalds */
3601da177e4SLinus Torvalds switch (op.mt_op) {
3611da177e4SLinus Torvalds case MTFSF:
3621da177e4SLinus Torvalds case MTBSF:
3631da177e4SLinus Torvalds case MTFSR:
3641da177e4SLinus Torvalds case MTBSR:
3651da177e4SLinus Torvalds case MTREW:
3661da177e4SLinus Torvalds case MTOFFL:
3671da177e4SLinus Torvalds case MTEOM:
3681da177e4SLinus Torvalds case MTRETEN:
3691da177e4SLinus Torvalds case MTBSFM:
3701da177e4SLinus Torvalds case MTFSFM:
3711da177e4SLinus Torvalds case MTSEEK:
3721da177e4SLinus Torvalds if (device->required_tapemarks)
3731da177e4SLinus Torvalds tape_std_terminate_write(device);
3741da177e4SLinus Torvalds }
3751da177e4SLinus Torvalds rc = tape_mtop(device, op.mt_op, op.mt_count);
3761da177e4SLinus Torvalds
3771da177e4SLinus Torvalds if (op.mt_op == MTWEOF && rc == 0) {
3781da177e4SLinus Torvalds if (op.mt_count > device->required_tapemarks)
3791da177e4SLinus Torvalds device->required_tapemarks = 0;
3801da177e4SLinus Torvalds else
3811da177e4SLinus Torvalds device->required_tapemarks -= op.mt_count;
3821da177e4SLinus Torvalds }
3831da177e4SLinus Torvalds return rc;
3841da177e4SLinus Torvalds }
3851da177e4SLinus Torvalds if (no == MTIOCPOS) {
3861da177e4SLinus Torvalds /* MTIOCPOS: query the tape position. */
3871da177e4SLinus Torvalds struct mtpos pos;
3881da177e4SLinus Torvalds
3891da177e4SLinus Torvalds rc = tape_mtop(device, MTTELL, 1);
3901da177e4SLinus Torvalds if (rc < 0)
3911da177e4SLinus Torvalds return rc;
3921da177e4SLinus Torvalds pos.mt_blkno = rc;
393*1207045dSArnd Bergmann return put_user_mtpos(data, &pos);
3941da177e4SLinus Torvalds }
3951da177e4SLinus Torvalds if (no == MTIOCGET) {
3961da177e4SLinus Torvalds /* MTIOCGET: query the tape drive status. */
3971da177e4SLinus Torvalds struct mtget get;
3981da177e4SLinus Torvalds
3991da177e4SLinus Torvalds memset(&get, 0, sizeof(get));
4001da177e4SLinus Torvalds get.mt_type = MT_ISUNKNOWN;
4011da177e4SLinus Torvalds get.mt_resid = 0 /* device->devstat.rescnt */;
4029fc98ad0SStefan Haberland get.mt_dsreg =
4039fc98ad0SStefan Haberland ((device->char_data.block_size << MT_ST_BLKSIZE_SHIFT)
4049fc98ad0SStefan Haberland & MT_ST_BLKSIZE_MASK);
4051da177e4SLinus Torvalds /* FIXME: mt_gstat, mt_erreg, mt_fileno */
4061da177e4SLinus Torvalds get.mt_gstat = 0;
4071da177e4SLinus Torvalds get.mt_erreg = 0;
4081da177e4SLinus Torvalds get.mt_fileno = 0;
4091da177e4SLinus Torvalds get.mt_gstat = device->tape_generic_status;
4101da177e4SLinus Torvalds
4111da177e4SLinus Torvalds if (device->medium_state == MS_LOADED) {
4121da177e4SLinus Torvalds rc = tape_mtop(device, MTTELL, 1);
4131da177e4SLinus Torvalds
4141da177e4SLinus Torvalds if (rc < 0)
4151da177e4SLinus Torvalds return rc;
4161da177e4SLinus Torvalds
4171da177e4SLinus Torvalds if (rc == 0)
4181da177e4SLinus Torvalds get.mt_gstat |= GMT_BOT(~0);
4191da177e4SLinus Torvalds
4201da177e4SLinus Torvalds get.mt_blkno = rc;
4211da177e4SLinus Torvalds }
4221da177e4SLinus Torvalds
423*1207045dSArnd Bergmann return put_user_mtget(data, &get);
4241da177e4SLinus Torvalds }
4251da177e4SLinus Torvalds /* Try the discipline ioctl function. */
4261da177e4SLinus Torvalds if (device->discipline->ioctl_fn == NULL)
4271da177e4SLinus Torvalds return -EINVAL;
428*1207045dSArnd Bergmann return device->discipline->ioctl_fn(device, no, (unsigned long)data);
4291da177e4SLinus Torvalds }
4301da177e4SLinus Torvalds
431f042e0f8SChristoph Hellwig static long
tapechar_ioctl(struct file * filp,unsigned int no,unsigned long data)432369a4632SMartin Schwidefsky tapechar_ioctl(struct file *filp, unsigned int no, unsigned long data)
433369a4632SMartin Schwidefsky {
434369a4632SMartin Schwidefsky struct tape_device *device;
435369a4632SMartin Schwidefsky long rc;
436369a4632SMartin Schwidefsky
437369a4632SMartin Schwidefsky DBF_EVENT(6, "TCHAR:ioct\n");
438369a4632SMartin Schwidefsky
439369a4632SMartin Schwidefsky device = (struct tape_device *) filp->private_data;
440369a4632SMartin Schwidefsky mutex_lock(&device->mutex);
441*1207045dSArnd Bergmann rc = __tapechar_ioctl(device, no, (void __user *)data);
442369a4632SMartin Schwidefsky mutex_unlock(&device->mutex);
443369a4632SMartin Schwidefsky return rc;
444369a4632SMartin Schwidefsky }
445369a4632SMartin Schwidefsky
446c5406079SHeiko Carstens #ifdef CONFIG_COMPAT
447369a4632SMartin Schwidefsky static long
tapechar_compat_ioctl(struct file * filp,unsigned int no,unsigned long data)448f042e0f8SChristoph Hellwig tapechar_compat_ioctl(struct file *filp, unsigned int no, unsigned long data)
449f042e0f8SChristoph Hellwig {
450f042e0f8SChristoph Hellwig struct tape_device *device = filp->private_data;
451*1207045dSArnd Bergmann long rc;
452f042e0f8SChristoph Hellwig
453*1207045dSArnd Bergmann if (no == MTIOCPOS32)
454*1207045dSArnd Bergmann no = MTIOCPOS;
455*1207045dSArnd Bergmann else if (no == MTIOCGET32)
456*1207045dSArnd Bergmann no = MTIOCGET;
457*1207045dSArnd Bergmann
458369a4632SMartin Schwidefsky mutex_lock(&device->mutex);
459*1207045dSArnd Bergmann rc = __tapechar_ioctl(device, no, compat_ptr(data));
460369a4632SMartin Schwidefsky mutex_unlock(&device->mutex);
461*1207045dSArnd Bergmann return rc;
462f042e0f8SChristoph Hellwig }
463c5406079SHeiko Carstens #endif /* CONFIG_COMPAT */
464f042e0f8SChristoph Hellwig
4651da177e4SLinus Torvalds /*
4661da177e4SLinus Torvalds * Initialize character device frontend.
4671da177e4SLinus Torvalds */
4681da177e4SLinus Torvalds int
tapechar_init(void)4691da177e4SLinus Torvalds tapechar_init (void)
4701da177e4SLinus Torvalds {
4711da177e4SLinus Torvalds dev_t dev;
4721da177e4SLinus Torvalds
4731da177e4SLinus Torvalds if (alloc_chrdev_region(&dev, 0, 256, "tape") != 0)
4741da177e4SLinus Torvalds return -1;
4751da177e4SLinus Torvalds
4761da177e4SLinus Torvalds tapechar_major = MAJOR(dev);
4771da177e4SLinus Torvalds
4781da177e4SLinus Torvalds return 0;
4791da177e4SLinus Torvalds }
4801da177e4SLinus Torvalds
4811da177e4SLinus Torvalds /*
4821da177e4SLinus Torvalds * cleanup
4831da177e4SLinus Torvalds */
4841da177e4SLinus Torvalds void
tapechar_exit(void)4851da177e4SLinus Torvalds tapechar_exit(void)
4861da177e4SLinus Torvalds {
4871da177e4SLinus Torvalds unregister_chrdev_region(MKDEV(tapechar_major, 0), 256);
4881da177e4SLinus Torvalds }
489