2 * drivers/s390/char/tape_char.c
3 * character device frontend for tape device driver
5 * S390 and zSeries version
6 * Copyright (C) 2001,2002 IBM Deutschland Entwicklung GmbH, IBM Corporation
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 #include <linux/config.h>
14 #include <linux/module.h>
15 #include <linux/types.h>
16 #include <linux/proc_fs.h>
17 #include <linux/mtio.h>
19 #include <asm/uaccess.h>
21 #define TAPE_DBF_AREA tape_core_dbf
25 #include "tape_class.h"
27 #define PRINTK_HEADER "TAPE_CHAR: "
29 #define TAPECHAR_MAJOR 0 /* get dynamic major */
32 * file operation structure for tape character frontend
34 static ssize_t tapechar_read(struct file *, char __user *, size_t, loff_t *);
35 static ssize_t tapechar_write(struct file *, const char __user *, size_t, loff_t *);
36 static int tapechar_open(struct inode *,struct file *);
37 static int tapechar_release(struct inode *,struct file *);
38 static int tapechar_ioctl(struct inode *, struct file *, unsigned int,
40 static long tapechar_compat_ioctl(struct file *, unsigned int,
43 static struct file_operations tape_fops =
46 .read = tapechar_read,
47 .write = tapechar_write,
48 .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->rt);
89 unregister_tape_dev(device->nt);
94 * Terminate write command (we write two TMs and skip backward over last)
95 * This ensures that the tape is always correctly terminated.
96 * When the user writes afterwards a new file, he will overwrite the
97 * second TM and therefore one TM will remain to separate the
98 * two files on the tape...
101 tapechar_terminate_write(struct tape_device *device)
103 if (tape_mtop(device, MTWEOF, 1) == 0 &&
104 tape_mtop(device, MTWEOF, 1) == 0)
105 tape_mtop(device, MTBSR, 1);
109 tapechar_check_idalbuffer(struct tape_device *device, size_t block_size)
111 struct idal_buffer *new;
113 if (device->char_data.idal_buf != NULL &&
114 device->char_data.idal_buf->size == block_size)
117 if (block_size > MAX_BLOCKSIZE) {
118 DBF_EVENT(3, "Invalid blocksize (%zd > %d)\n",
119 block_size, MAX_BLOCKSIZE);
120 PRINT_ERR("Invalid blocksize (%zd> %d)\n",
121 block_size, MAX_BLOCKSIZE);
125 /* The current idal buffer is not correct. Allocate a new one. */
126 new = idal_buffer_alloc(block_size, 0);
130 if (device->char_data.idal_buf != NULL)
131 idal_buffer_free(device->char_data.idal_buf);
133 device->char_data.idal_buf = new;
139 * Tape device read function
142 tapechar_read(struct file *filp, char __user *data, size_t count, loff_t *ppos)
144 struct tape_device *device;
145 struct tape_request *request;
149 DBF_EVENT(6, "TCHAR:read\n");
150 device = (struct tape_device *) filp->private_data;
153 * If the tape isn't terminated yet, do it now. And since we then
154 * are at the end of the tape there wouldn't be anything to read
155 * anyways. So we return immediatly.
157 if(device->required_tapemarks) {
158 return tape_std_terminate_write(device);
161 /* Find out block size to use */
162 if (device->char_data.block_size != 0) {
163 if (count < device->char_data.block_size) {
164 DBF_EVENT(3, "TCHAR:read smaller than block "
165 "size was requested\n");
168 block_size = device->char_data.block_size;
173 rc = tapechar_check_idalbuffer(device, block_size);
177 #ifdef CONFIG_S390_TAPE_BLOCK
178 /* Changes position. */
179 device->blk_data.medium_changed = 1;
182 DBF_EVENT(6, "TCHAR:nbytes: %lx\n", block_size);
183 /* Let the discipline build the ccw chain. */
184 request = device->discipline->read_block(device, block_size);
186 return PTR_ERR(request);
188 rc = tape_do_io(device, request);
190 rc = block_size - request->rescnt;
191 DBF_EVENT(6, "TCHAR:rbytes: %x\n", rc);
193 /* Copy data from idal buffer to user space. */
194 if (idal_buffer_to_user(device->char_data.idal_buf,
198 tape_free_request(request);
203 * Tape device write function
206 tapechar_write(struct file *filp, const char __user *data, size_t count, loff_t *ppos)
208 struct tape_device *device;
209 struct tape_request *request;
215 DBF_EVENT(6, "TCHAR:write\n");
216 device = (struct tape_device *) filp->private_data;
217 /* Find out block size and number of blocks */
218 if (device->char_data.block_size != 0) {
219 if (count < device->char_data.block_size) {
220 DBF_EVENT(3, "TCHAR:write smaller than block "
221 "size was requested\n");
224 block_size = device->char_data.block_size;
225 nblocks = count / block_size;
231 rc = tapechar_check_idalbuffer(device, block_size);
235 #ifdef CONFIG_S390_TAPE_BLOCK
236 /* Changes position. */
237 device->blk_data.medium_changed = 1;
240 DBF_EVENT(6,"TCHAR:nbytes: %lx\n", block_size);
241 DBF_EVENT(6, "TCHAR:nblocks: %x\n", nblocks);
242 /* Let the discipline build the ccw chain. */
243 request = device->discipline->write_block(device, block_size);
245 return PTR_ERR(request);
248 for (i = 0; i < nblocks; i++) {
249 /* Copy data from user space to idal buffer. */
250 if (idal_buffer_from_user(device->char_data.idal_buf,
255 rc = tape_do_io(device, request);
258 DBF_EVENT(6, "TCHAR:wbytes: %lx\n",
259 block_size - request->rescnt);
260 filp->f_pos += block_size - request->rescnt;
261 written += block_size - request->rescnt;
262 if (request->rescnt != 0)
266 tape_free_request(request);
269 * Ok, the device has no more space. It has NOT written
272 if (device->discipline->process_eov)
273 device->discipline->process_eov(device);
280 * After doing a write we always need two tapemarks to correctly
281 * terminate the tape (one to terminate the file, the second to
282 * flag the end of recorded data.
283 * Since process_eov positions the tape in front of the written
284 * tapemark it doesn't hurt to write two marks again.
287 device->required_tapemarks = 2;
289 return rc ? rc : written;
293 * Character frontend tape device open function.
296 tapechar_open (struct inode *inode, struct file *filp)
298 struct tape_device *device;
301 DBF_EVENT(6, "TCHAR:open: %i:%i\n",
302 imajor(filp->f_dentry->d_inode),
303 iminor(filp->f_dentry->d_inode));
305 if (imajor(filp->f_dentry->d_inode) != tapechar_major)
308 minor = iminor(filp->f_dentry->d_inode);
309 device = tape_get_device(minor / TAPE_MINORS_PER_DEV);
310 if (IS_ERR(device)) {
311 DBF_EVENT(3, "TCHAR:open: tape_get_device() failed\n");
312 return PTR_ERR(device);
316 rc = tape_open(device);
318 filp->private_data = device;
319 return nonseekable_open(inode, filp);
321 tape_put_device(device);
327 * Character frontend tape device release function.
331 tapechar_release(struct inode *inode, struct file *filp)
333 struct tape_device *device;
335 DBF_EVENT(6, "TCHAR:release: %x\n", iminor(inode));
336 device = (struct tape_device *) filp->private_data;
339 * If this is the rewinding tape minor then rewind. In that case we
340 * write all required tapemarks. Otherwise only one to terminate the
343 if ((iminor(inode) & 1) != 0) {
344 if (device->required_tapemarks)
345 tape_std_terminate_write(device);
346 tape_mtop(device, MTREW, 1);
348 if (device->required_tapemarks > 1) {
349 if (tape_mtop(device, MTWEOF, 1) == 0)
350 device->required_tapemarks--;
354 if (device->char_data.idal_buf != NULL) {
355 idal_buffer_free(device->char_data.idal_buf);
356 device->char_data.idal_buf = NULL;
358 tape_release(device);
359 filp->private_data = tape_put_device(device);
365 * Tape device io controls.
368 tapechar_ioctl(struct inode *inp, struct file *filp,
369 unsigned int no, unsigned long data)
371 struct tape_device *device;
374 DBF_EVENT(6, "TCHAR:ioct\n");
376 device = (struct tape_device *) filp->private_data;
378 if (no == MTIOCTOP) {
381 if (copy_from_user(&op, (char __user *) data, sizeof(op)) != 0)
387 * Operations that change tape position should write final
402 #ifdef CONFIG_S390_TAPE_BLOCK
403 device->blk_data.medium_changed = 1;
405 if (device->required_tapemarks)
406 tape_std_terminate_write(device);
410 rc = tape_mtop(device, op.mt_op, op.mt_count);
412 if (op.mt_op == MTWEOF && rc == 0) {
413 if (op.mt_count > device->required_tapemarks)
414 device->required_tapemarks = 0;
416 device->required_tapemarks -= op.mt_count;
420 if (no == MTIOCPOS) {
421 /* MTIOCPOS: query the tape position. */
424 rc = tape_mtop(device, MTTELL, 1);
428 if (copy_to_user((char __user *) data, &pos, sizeof(pos)) != 0)
432 if (no == MTIOCGET) {
433 /* MTIOCGET: query the tape drive status. */
436 memset(&get, 0, sizeof(get));
437 get.mt_type = MT_ISUNKNOWN;
438 get.mt_resid = 0 /* device->devstat.rescnt */;
439 get.mt_dsreg = device->tape_state;
440 /* FIXME: mt_gstat, mt_erreg, mt_fileno */
444 get.mt_gstat = device->tape_generic_status;
446 if (device->medium_state == MS_LOADED) {
447 rc = tape_mtop(device, MTTELL, 1);
453 get.mt_gstat |= GMT_BOT(~0);
458 if (copy_to_user((char __user *) data, &get, sizeof(get)) != 0)
463 /* Try the discipline ioctl function. */
464 if (device->discipline->ioctl_fn == NULL)
466 return device->discipline->ioctl_fn(device, no, data);
470 tapechar_compat_ioctl(struct file *filp, unsigned int no, unsigned long data)
472 struct tape_device *device = filp->private_data;
473 int rval = -ENOIOCTLCMD;
475 if (device->discipline->ioctl_fn) {
477 rval = device->discipline->ioctl_fn(device, no, data);
487 * Initialize character device frontend.
494 if (alloc_chrdev_region(&dev, 0, 256, "tape") != 0)
497 tapechar_major = MAJOR(dev);
498 PRINT_INFO("tape gets major %d for character devices\n", MAJOR(dev));
509 PRINT_INFO("tape releases major %d for character devices\n",
511 unregister_chrdev_region(MKDEV(tapechar_major, 0), 256);