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>
23 #include "tape_class.h"
25 #define PRINTK_HEADER "TAPE_CHAR: "
27 #define TAPECHAR_MAJOR 0 /* get dynamic major */
30 * file operation structure for tape character frontend
32 static ssize_t tapechar_read(struct file *, char *, size_t, loff_t *);
33 static ssize_t tapechar_write(struct file *, const char *, size_t, loff_t *);
34 static int tapechar_open(struct inode *,struct file *);
35 static int tapechar_release(struct inode *,struct file *);
36 static int tapechar_ioctl(struct inode *, struct file *, unsigned int,
39 static struct file_operations tape_fops =
42 .read = tapechar_read,
43 .write = tapechar_write,
44 .ioctl = tapechar_ioctl,
45 .open = tapechar_open,
46 .release = tapechar_release,
49 static int tapechar_major = TAPECHAR_MAJOR;
52 * This function is called for every new tapedevice
55 tapechar_setup_device(struct tape_device * device)
59 sprintf(device_name, "ntibm%i", device->first_minor / 2);
60 device->nt = register_tape_dev(
62 MKDEV(tapechar_major, device->first_minor),
68 device->rt = register_tape_dev(
70 MKDEV(tapechar_major, device->first_minor + 1),
80 tapechar_cleanup_device(struct tape_device *device)
82 unregister_tape_dev(device->rt);
84 unregister_tape_dev(device->nt);
89 * Terminate write command (we write two TMs and skip backward over last)
90 * This ensures that the tape is always correctly terminated.
91 * When the user writes afterwards a new file, he will overwrite the
92 * second TM and therefore one TM will remain to separate the
93 * two files on the tape...
96 tapechar_terminate_write(struct tape_device *device)
98 if (tape_mtop(device, MTWEOF, 1) == 0 &&
99 tape_mtop(device, MTWEOF, 1) == 0)
100 tape_mtop(device, MTBSR, 1);
104 tapechar_check_idalbuffer(struct tape_device *device, size_t block_size)
106 struct idal_buffer *new;
108 if (device->char_data.idal_buf != NULL &&
109 device->char_data.idal_buf->size == block_size)
112 if (block_size > MAX_BLOCKSIZE) {
113 DBF_EVENT(3, "Invalid blocksize (%zd > %d)\n",
114 block_size, MAX_BLOCKSIZE);
115 PRINT_ERR("Invalid blocksize (%zd> %d)\n",
116 block_size, MAX_BLOCKSIZE);
120 /* The current idal buffer is not correct. Allocate a new one. */
121 new = idal_buffer_alloc(block_size, 0);
125 if (device->char_data.idal_buf != NULL)
126 idal_buffer_free(device->char_data.idal_buf);
128 device->char_data.idal_buf = new;
134 * Tape device read function
137 tapechar_read (struct file *filp, char *data, size_t count, loff_t *ppos)
139 struct tape_device *device;
140 struct tape_request *request;
144 DBF_EVENT(6, "TCHAR:read\n");
145 device = (struct tape_device *) filp->private_data;
146 /* Check position. */
147 if (ppos != &filp->f_pos) {
149 * "A request was outside the capabilities of the device."
150 * This check uses internal knowledge about how pread and
153 DBF_EVENT(6, "TCHAR:ppos wrong\n");
158 * If the tape isn't terminated yet, do it now. And since we then
159 * are at the end of the tape there wouldn't be anything to read
160 * anyways. So we return immediatly.
162 if(device->required_tapemarks) {
163 return tape_std_terminate_write(device);
166 /* Find out block size to use */
167 if (device->char_data.block_size != 0) {
168 if (count < device->char_data.block_size) {
169 DBF_EVENT(3, "TCHAR:read smaller than block "
170 "size was requested\n");
173 block_size = device->char_data.block_size;
178 rc = tapechar_check_idalbuffer(device, block_size);
182 #ifdef CONFIG_S390_TAPE_BLOCK
183 /* Changes position. */
184 device->blk_data.medium_changed = 1;
187 DBF_EVENT(6, "TCHAR:nbytes: %lx\n", block_size);
188 /* Let the discipline build the ccw chain. */
189 request = device->discipline->read_block(device, block_size);
191 return PTR_ERR(request);
193 rc = tape_do_io(device, request);
195 rc = block_size - request->rescnt;
196 DBF_EVENT(6, "TCHAR:rbytes: %x\n", rc);
198 /* Copy data from idal buffer to user space. */
199 if (idal_buffer_to_user(device->char_data.idal_buf,
203 tape_free_request(request);
208 * Tape device write function
211 tapechar_write(struct file *filp, const char *data, size_t count, loff_t *ppos)
213 struct tape_device *device;
214 struct tape_request *request;
220 DBF_EVENT(6, "TCHAR:write\n");
221 device = (struct tape_device *) filp->private_data;
223 if (ppos != &filp->f_pos) {
224 /* "A request was outside the capabilities of the device." */
225 DBF_EVENT(6, "TCHAR:ppos wrong\n");
228 /* Find out block size and number of blocks */
229 if (device->char_data.block_size != 0) {
230 if (count < device->char_data.block_size) {
231 DBF_EVENT(3, "TCHAR:write smaller than block "
232 "size was requested\n");
235 block_size = device->char_data.block_size;
236 nblocks = count / block_size;
242 rc = tapechar_check_idalbuffer(device, block_size);
246 #ifdef CONFIG_S390_TAPE_BLOCK
247 /* Changes position. */
248 device->blk_data.medium_changed = 1;
251 DBF_EVENT(6,"TCHAR:nbytes: %lx\n", block_size);
252 DBF_EVENT(6, "TCHAR:nblocks: %x\n", nblocks);
253 /* Let the discipline build the ccw chain. */
254 request = device->discipline->write_block(device, block_size);
256 return PTR_ERR(request);
259 for (i = 0; i < nblocks; i++) {
260 /* Copy data from user space to idal buffer. */
261 if (idal_buffer_from_user(device->char_data.idal_buf,
266 rc = tape_do_io(device, request);
269 DBF_EVENT(6, "TCHAR:wbytes: %lx\n",
270 block_size - request->rescnt);
271 filp->f_pos += block_size - request->rescnt;
272 written += block_size - request->rescnt;
273 if (request->rescnt != 0)
277 tape_free_request(request);
280 * Ok, the device has no more space. It has NOT written
283 if (device->discipline->process_eov)
284 device->discipline->process_eov(device);
291 * After doing a write we always need two tapemarks to correctly
292 * terminate the tape (one to terminate the file, the second to
293 * flag the end of recorded data.
294 * Since process_eov positions the tape in front of the written
295 * tapemark it doesn't hurt to write two marks again.
298 device->required_tapemarks = 2;
300 return rc ? rc : written;
304 * Character frontend tape device open function.
307 tapechar_open (struct inode *inode, struct file *filp)
309 struct tape_device *device;
312 DBF_EVENT(6, "TCHAR:open: %i:%i\n",
313 imajor(filp->f_dentry->d_inode),
314 iminor(filp->f_dentry->d_inode));
316 if (imajor(filp->f_dentry->d_inode) != tapechar_major)
319 minor = iminor(filp->f_dentry->d_inode);
320 device = tape_get_device(minor / TAPE_MINORS_PER_DEV);
321 if (IS_ERR(device)) {
322 DBF_EVENT(3, "TCHAR:open: tape_get_device() failed\n");
323 return PTR_ERR(device);
327 rc = tape_open(device);
329 filp->private_data = device;
332 tape_put_device(device);
338 * Character frontend tape device release function.
342 tapechar_release(struct inode *inode, struct file *filp)
344 struct tape_device *device;
346 DBF_EVENT(6, "TCHAR:release: %x\n", iminor(inode));
347 device = (struct tape_device *) filp->private_data;
350 * If this is the rewinding tape minor then rewind. In that case we
351 * write all required tapemarks. Otherwise only one to terminate the
354 if ((iminor(inode) & 1) != 0) {
355 if (device->required_tapemarks)
356 tape_std_terminate_write(device);
357 tape_mtop(device, MTREW, 1);
359 if (device->required_tapemarks > 1) {
360 if (tape_mtop(device, MTWEOF, 1) == 0)
361 device->required_tapemarks--;
365 if (device->char_data.idal_buf != NULL) {
366 idal_buffer_free(device->char_data.idal_buf);
367 device->char_data.idal_buf = NULL;
369 tape_release(device);
370 filp->private_data = tape_put_device(device);
376 * Tape device io controls.
379 tapechar_ioctl(struct inode *inp, struct file *filp,
380 unsigned int no, unsigned long data)
382 struct tape_device *device;
385 DBF_EVENT(6, "TCHAR:ioct\n");
387 device = (struct tape_device *) filp->private_data;
389 if (no == MTIOCTOP) {
392 if (copy_from_user(&op, (char *) data, sizeof(op)) != 0)
398 * Operations that change tape position should write final
413 #ifdef CONFIG_S390_TAPE_BLOCK
414 device->blk_data.medium_changed = 1;
416 if (device->required_tapemarks)
417 tape_std_terminate_write(device);
421 rc = tape_mtop(device, op.mt_op, op.mt_count);
423 if (op.mt_op == MTWEOF && rc == 0) {
424 if (op.mt_count > device->required_tapemarks)
425 device->required_tapemarks = 0;
427 device->required_tapemarks -= op.mt_count;
431 if (no == MTIOCPOS) {
432 /* MTIOCPOS: query the tape position. */
435 rc = tape_mtop(device, MTTELL, 1);
439 if (copy_to_user((char *) data, &pos, sizeof(pos)) != 0)
443 if (no == MTIOCGET) {
444 /* MTIOCGET: query the tape drive status. */
447 memset(&get, 0, sizeof(get));
448 get.mt_type = MT_ISUNKNOWN;
449 get.mt_resid = 0 /* device->devstat.rescnt */;
450 get.mt_dsreg = device->tape_state;
451 /* FIXME: mt_gstat, mt_erreg, mt_fileno */
455 get.mt_gstat = device->tape_generic_status;
457 if (device->medium_state == MS_LOADED) {
458 rc = tape_mtop(device, MTTELL, 1);
464 get.mt_gstat |= GMT_BOT(~0);
469 if (copy_to_user((char *) data, &get, sizeof(get)) != 0)
474 /* Try the discipline ioctl function. */
475 if (device->discipline->ioctl_fn == NULL)
477 return device->discipline->ioctl_fn(device, no, data);
481 * Initialize character device frontend.
488 if (alloc_chrdev_region(&dev, 0, 256, "tape") != 0)
491 tapechar_major = MAJOR(dev);
492 PRINT_INFO("tape gets major %d for character devices\n", MAJOR(dev));
503 PRINT_INFO("tape releases major %d for character devices\n",
505 unregister_chrdev_region(MKDEV(tapechar_major, 0), 256);