2 * drivers/s390/char/tape_char.c
3 * character device frontend for tape device driver
5 * S390 and zSeries version
6 * Copyright IBM Corp. 2001,2006
7 * Author(s): Carsten Otte <cotte@de.ibm.com>
8 * Michael Holzheu <holzheu@de.ibm.com>
9 * Tuan Ngo-Anh <ngoanh@de.ibm.com>
10 * Martin Schwidefsky <schwidefsky@de.ibm.com>
13 #define KMSG_COMPONENT "tape"
14 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
16 #include <linux/module.h>
17 #include <linux/types.h>
18 #include <linux/proc_fs.h>
19 #include <linux/mtio.h>
20 #include <linux/smp_lock.h>
22 #include <asm/uaccess.h>
24 #define TAPE_DBF_AREA tape_core_dbf
28 #include "tape_class.h"
30 #define TAPECHAR_MAJOR 0 /* get dynamic major */
33 * file operation structure for tape character frontend
35 static ssize_t tapechar_read(struct file *, char __user *, size_t, loff_t *);
36 static ssize_t tapechar_write(struct file *, const char __user *, size_t, loff_t *);
37 static int tapechar_open(struct inode *,struct file *);
38 static int tapechar_release(struct inode *,struct file *);
39 static long tapechar_ioctl(struct file *, unsigned int, unsigned long);
40 static long tapechar_compat_ioctl(struct file *, unsigned int,
43 static const struct file_operations tape_fops =
46 .read = tapechar_read,
47 .write = tapechar_write,
48 .unlocked_ioctl = tapechar_ioctl,
49 .compat_ioctl = tapechar_compat_ioctl,
50 .open = tapechar_open,
51 .release = tapechar_release,
54 static int tapechar_major = TAPECHAR_MAJOR;
57 * This function is called for every new tapedevice
60 tapechar_setup_device(struct tape_device * device)
64 sprintf(device_name, "ntibm%i", device->first_minor / 2);
65 device->nt = register_tape_dev(
67 MKDEV(tapechar_major, device->first_minor),
73 device->rt = register_tape_dev(
75 MKDEV(tapechar_major, device->first_minor + 1),
85 tapechar_cleanup_device(struct tape_device *device)
87 unregister_tape_dev(&device->cdev->dev, device->rt);
89 unregister_tape_dev(&device->cdev->dev, device->nt);
94 tapechar_check_idalbuffer(struct tape_device *device, size_t block_size)
96 struct idal_buffer *new;
98 if (device->char_data.idal_buf != NULL &&
99 device->char_data.idal_buf->size == block_size)
102 if (block_size > MAX_BLOCKSIZE) {
103 DBF_EVENT(3, "Invalid blocksize (%zd > %d)\n",
104 block_size, MAX_BLOCKSIZE);
108 /* The current idal buffer is not correct. Allocate a new one. */
109 new = idal_buffer_alloc(block_size, 0);
113 if (device->char_data.idal_buf != NULL)
114 idal_buffer_free(device->char_data.idal_buf);
116 device->char_data.idal_buf = new;
122 * Tape device read function
125 tapechar_read(struct file *filp, char __user *data, size_t count, loff_t *ppos)
127 struct tape_device *device;
128 struct tape_request *request;
132 DBF_EVENT(6, "TCHAR:read\n");
133 device = (struct tape_device *) filp->private_data;
136 * If the tape isn't terminated yet, do it now. And since we then
137 * are at the end of the tape there wouldn't be anything to read
138 * anyways. So we return immediatly.
140 if(device->required_tapemarks) {
141 return tape_std_terminate_write(device);
144 /* Find out block size to use */
145 if (device->char_data.block_size != 0) {
146 if (count < device->char_data.block_size) {
147 DBF_EVENT(3, "TCHAR:read smaller than block "
148 "size was requested\n");
151 block_size = device->char_data.block_size;
156 rc = tapechar_check_idalbuffer(device, block_size);
160 #ifdef CONFIG_S390_TAPE_BLOCK
161 /* Changes position. */
162 device->blk_data.medium_changed = 1;
165 DBF_EVENT(6, "TCHAR:nbytes: %lx\n", block_size);
166 /* Let the discipline build the ccw chain. */
167 request = device->discipline->read_block(device, block_size);
169 return PTR_ERR(request);
171 rc = tape_do_io(device, request);
173 rc = block_size - request->rescnt;
174 DBF_EVENT(6, "TCHAR:rbytes: %x\n", rc);
175 /* Copy data from idal buffer to user space. */
176 if (idal_buffer_to_user(device->char_data.idal_buf,
180 tape_free_request(request);
185 * Tape device write function
188 tapechar_write(struct file *filp, const char __user *data, size_t count, loff_t *ppos)
190 struct tape_device *device;
191 struct tape_request *request;
197 DBF_EVENT(6, "TCHAR:write\n");
198 device = (struct tape_device *) filp->private_data;
199 /* Find out block size and number of blocks */
200 if (device->char_data.block_size != 0) {
201 if (count < device->char_data.block_size) {
202 DBF_EVENT(3, "TCHAR:write smaller than block "
203 "size was requested\n");
206 block_size = device->char_data.block_size;
207 nblocks = count / block_size;
213 rc = tapechar_check_idalbuffer(device, block_size);
217 #ifdef CONFIG_S390_TAPE_BLOCK
218 /* Changes position. */
219 device->blk_data.medium_changed = 1;
222 DBF_EVENT(6,"TCHAR:nbytes: %lx\n", block_size);
223 DBF_EVENT(6, "TCHAR:nblocks: %x\n", nblocks);
224 /* Let the discipline build the ccw chain. */
225 request = device->discipline->write_block(device, block_size);
227 return PTR_ERR(request);
230 for (i = 0; i < nblocks; i++) {
231 /* Copy data from user space to idal buffer. */
232 if (idal_buffer_from_user(device->char_data.idal_buf,
237 rc = tape_do_io(device, request);
240 DBF_EVENT(6, "TCHAR:wbytes: %lx\n",
241 block_size - request->rescnt);
242 written += block_size - request->rescnt;
243 if (request->rescnt != 0)
247 tape_free_request(request);
250 * Ok, the device has no more space. It has NOT written
253 if (device->discipline->process_eov)
254 device->discipline->process_eov(device);
261 * After doing a write we always need two tapemarks to correctly
262 * terminate the tape (one to terminate the file, the second to
263 * flag the end of recorded data.
264 * Since process_eov positions the tape in front of the written
265 * tapemark it doesn't hurt to write two marks again.
268 device->required_tapemarks = 2;
270 return rc ? rc : written;
274 * Character frontend tape device open function.
277 tapechar_open (struct inode *inode, struct file *filp)
279 struct tape_device *device;
282 DBF_EVENT(6, "TCHAR:open: %i:%i\n",
283 imajor(filp->f_path.dentry->d_inode),
284 iminor(filp->f_path.dentry->d_inode));
286 if (imajor(filp->f_path.dentry->d_inode) != tapechar_major)
289 minor = iminor(filp->f_path.dentry->d_inode);
290 device = tape_find_device(minor / TAPE_MINORS_PER_DEV);
291 if (IS_ERR(device)) {
292 DBF_EVENT(3, "TCHAR:open: tape_find_device() failed\n");
293 return PTR_ERR(device);
296 rc = tape_open(device);
298 filp->private_data = device;
299 nonseekable_open(inode, filp);
301 tape_put_device(device);
307 * Character frontend tape device release function.
311 tapechar_release(struct inode *inode, struct file *filp)
313 struct tape_device *device;
315 DBF_EVENT(6, "TCHAR:release: %x\n", iminor(inode));
316 device = (struct tape_device *) filp->private_data;
319 * If this is the rewinding tape minor then rewind. In that case we
320 * write all required tapemarks. Otherwise only one to terminate the
323 if ((iminor(inode) & 1) != 0) {
324 if (device->required_tapemarks)
325 tape_std_terminate_write(device);
326 tape_mtop(device, MTREW, 1);
328 if (device->required_tapemarks > 1) {
329 if (tape_mtop(device, MTWEOF, 1) == 0)
330 device->required_tapemarks--;
334 if (device->char_data.idal_buf != NULL) {
335 idal_buffer_free(device->char_data.idal_buf);
336 device->char_data.idal_buf = NULL;
338 tape_release(device);
339 filp->private_data = NULL;
340 tape_put_device(device);
346 * Tape device io controls.
349 __tapechar_ioctl(struct tape_device *device,
350 unsigned int no, unsigned long data)
354 if (no == MTIOCTOP) {
357 if (copy_from_user(&op, (char __user *) data, sizeof(op)) != 0)
363 * Operations that change tape position should write final
378 #ifdef CONFIG_S390_TAPE_BLOCK
379 device->blk_data.medium_changed = 1;
381 if (device->required_tapemarks)
382 tape_std_terminate_write(device);
386 rc = tape_mtop(device, op.mt_op, op.mt_count);
388 if (op.mt_op == MTWEOF && rc == 0) {
389 if (op.mt_count > device->required_tapemarks)
390 device->required_tapemarks = 0;
392 device->required_tapemarks -= op.mt_count;
396 if (no == MTIOCPOS) {
397 /* MTIOCPOS: query the tape position. */
400 rc = tape_mtop(device, MTTELL, 1);
404 if (copy_to_user((char __user *) data, &pos, sizeof(pos)) != 0)
408 if (no == MTIOCGET) {
409 /* MTIOCGET: query the tape drive status. */
412 memset(&get, 0, sizeof(get));
413 get.mt_type = MT_ISUNKNOWN;
414 get.mt_resid = 0 /* device->devstat.rescnt */;
415 get.mt_dsreg = device->tape_state;
416 /* FIXME: mt_gstat, mt_erreg, mt_fileno */
420 get.mt_gstat = device->tape_generic_status;
422 if (device->medium_state == MS_LOADED) {
423 rc = tape_mtop(device, MTTELL, 1);
429 get.mt_gstat |= GMT_BOT(~0);
434 if (copy_to_user((char __user *) data, &get, sizeof(get)) != 0)
439 /* Try the discipline ioctl function. */
440 if (device->discipline->ioctl_fn == NULL)
442 return device->discipline->ioctl_fn(device, no, data);
446 tapechar_ioctl(struct file *filp, unsigned int no, unsigned long data)
448 struct tape_device *device;
451 DBF_EVENT(6, "TCHAR:ioct\n");
453 device = (struct tape_device *) filp->private_data;
454 mutex_lock(&device->mutex);
455 rc = __tapechar_ioctl(device, no, data);
456 mutex_unlock(&device->mutex);
461 tapechar_compat_ioctl(struct file *filp, unsigned int no, unsigned long data)
463 struct tape_device *device = filp->private_data;
464 int rval = -ENOIOCTLCMD;
466 if (device->discipline->ioctl_fn) {
467 mutex_lock(&device->mutex);
468 rval = device->discipline->ioctl_fn(device, no, data);
469 mutex_unlock(&device->mutex);
478 * Initialize character device frontend.
485 if (alloc_chrdev_region(&dev, 0, 256, "tape") != 0)
488 tapechar_major = MAJOR(dev);
499 unregister_chrdev_region(MKDEV(tapechar_major, 0), 256);