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,
41 static struct file_operations tape_fops =
44 .read = tapechar_read,
45 .write = tapechar_write,
46 .ioctl = tapechar_ioctl,
47 .open = tapechar_open,
48 .release = tapechar_release,
51 static int tapechar_major = TAPECHAR_MAJOR;
54 * This function is called for every new tapedevice
57 tapechar_setup_device(struct tape_device * device)
61 sprintf(device_name, "ntibm%i", device->first_minor / 2);
62 device->nt = register_tape_dev(
64 MKDEV(tapechar_major, device->first_minor),
70 device->rt = register_tape_dev(
72 MKDEV(tapechar_major, device->first_minor + 1),
82 tapechar_cleanup_device(struct tape_device *device)
84 unregister_tape_dev(device->rt);
86 unregister_tape_dev(device->nt);
91 * Terminate write command (we write two TMs and skip backward over last)
92 * This ensures that the tape is always correctly terminated.
93 * When the user writes afterwards a new file, he will overwrite the
94 * second TM and therefore one TM will remain to separate the
95 * two files on the tape...
98 tapechar_terminate_write(struct tape_device *device)
100 if (tape_mtop(device, MTWEOF, 1) == 0 &&
101 tape_mtop(device, MTWEOF, 1) == 0)
102 tape_mtop(device, MTBSR, 1);
106 tapechar_check_idalbuffer(struct tape_device *device, size_t block_size)
108 struct idal_buffer *new;
110 if (device->char_data.idal_buf != NULL &&
111 device->char_data.idal_buf->size == block_size)
114 if (block_size > MAX_BLOCKSIZE) {
115 DBF_EVENT(3, "Invalid blocksize (%zd > %d)\n",
116 block_size, MAX_BLOCKSIZE);
117 PRINT_ERR("Invalid blocksize (%zd> %d)\n",
118 block_size, MAX_BLOCKSIZE);
122 /* The current idal buffer is not correct. Allocate a new one. */
123 new = idal_buffer_alloc(block_size, 0);
127 if (device->char_data.idal_buf != NULL)
128 idal_buffer_free(device->char_data.idal_buf);
130 device->char_data.idal_buf = new;
136 * Tape device read function
139 tapechar_read(struct file *filp, char __user *data, size_t count, loff_t *ppos)
141 struct tape_device *device;
142 struct tape_request *request;
146 DBF_EVENT(6, "TCHAR:read\n");
147 device = (struct tape_device *) filp->private_data;
150 * If the tape isn't terminated yet, do it now. And since we then
151 * are at the end of the tape there wouldn't be anything to read
152 * anyways. So we return immediatly.
154 if(device->required_tapemarks) {
155 return tape_std_terminate_write(device);
158 /* Find out block size to use */
159 if (device->char_data.block_size != 0) {
160 if (count < device->char_data.block_size) {
161 DBF_EVENT(3, "TCHAR:read smaller than block "
162 "size was requested\n");
165 block_size = device->char_data.block_size;
170 rc = tapechar_check_idalbuffer(device, block_size);
174 #ifdef CONFIG_S390_TAPE_BLOCK
175 /* Changes position. */
176 device->blk_data.medium_changed = 1;
179 DBF_EVENT(6, "TCHAR:nbytes: %lx\n", block_size);
180 /* Let the discipline build the ccw chain. */
181 request = device->discipline->read_block(device, block_size);
183 return PTR_ERR(request);
185 rc = tape_do_io(device, request);
187 rc = block_size - request->rescnt;
188 DBF_EVENT(6, "TCHAR:rbytes: %x\n", rc);
190 /* Copy data from idal buffer to user space. */
191 if (idal_buffer_to_user(device->char_data.idal_buf,
195 tape_free_request(request);
200 * Tape device write function
203 tapechar_write(struct file *filp, const char __user *data, size_t count, loff_t *ppos)
205 struct tape_device *device;
206 struct tape_request *request;
212 DBF_EVENT(6, "TCHAR:write\n");
213 device = (struct tape_device *) filp->private_data;
214 /* Find out block size and number of blocks */
215 if (device->char_data.block_size != 0) {
216 if (count < device->char_data.block_size) {
217 DBF_EVENT(3, "TCHAR:write smaller than block "
218 "size was requested\n");
221 block_size = device->char_data.block_size;
222 nblocks = count / block_size;
228 rc = tapechar_check_idalbuffer(device, block_size);
232 #ifdef CONFIG_S390_TAPE_BLOCK
233 /* Changes position. */
234 device->blk_data.medium_changed = 1;
237 DBF_EVENT(6,"TCHAR:nbytes: %lx\n", block_size);
238 DBF_EVENT(6, "TCHAR:nblocks: %x\n", nblocks);
239 /* Let the discipline build the ccw chain. */
240 request = device->discipline->write_block(device, block_size);
242 return PTR_ERR(request);
245 for (i = 0; i < nblocks; i++) {
246 /* Copy data from user space to idal buffer. */
247 if (idal_buffer_from_user(device->char_data.idal_buf,
252 rc = tape_do_io(device, request);
255 DBF_EVENT(6, "TCHAR:wbytes: %lx\n",
256 block_size - request->rescnt);
257 filp->f_pos += block_size - request->rescnt;
258 written += block_size - request->rescnt;
259 if (request->rescnt != 0)
263 tape_free_request(request);
266 * Ok, the device has no more space. It has NOT written
269 if (device->discipline->process_eov)
270 device->discipline->process_eov(device);
277 * After doing a write we always need two tapemarks to correctly
278 * terminate the tape (one to terminate the file, the second to
279 * flag the end of recorded data.
280 * Since process_eov positions the tape in front of the written
281 * tapemark it doesn't hurt to write two marks again.
284 device->required_tapemarks = 2;
286 return rc ? rc : written;
290 * Character frontend tape device open function.
293 tapechar_open (struct inode *inode, struct file *filp)
295 struct tape_device *device;
298 DBF_EVENT(6, "TCHAR:open: %i:%i\n",
299 imajor(filp->f_dentry->d_inode),
300 iminor(filp->f_dentry->d_inode));
302 if (imajor(filp->f_dentry->d_inode) != tapechar_major)
305 minor = iminor(filp->f_dentry->d_inode);
306 device = tape_get_device(minor / TAPE_MINORS_PER_DEV);
307 if (IS_ERR(device)) {
308 DBF_EVENT(3, "TCHAR:open: tape_get_device() failed\n");
309 return PTR_ERR(device);
313 rc = tape_open(device);
315 filp->private_data = device;
316 return nonseekable_open(inode, filp);
318 tape_put_device(device);
324 * Character frontend tape device release function.
328 tapechar_release(struct inode *inode, struct file *filp)
330 struct tape_device *device;
332 DBF_EVENT(6, "TCHAR:release: %x\n", iminor(inode));
333 device = (struct tape_device *) filp->private_data;
336 * If this is the rewinding tape minor then rewind. In that case we
337 * write all required tapemarks. Otherwise only one to terminate the
340 if ((iminor(inode) & 1) != 0) {
341 if (device->required_tapemarks)
342 tape_std_terminate_write(device);
343 tape_mtop(device, MTREW, 1);
345 if (device->required_tapemarks > 1) {
346 if (tape_mtop(device, MTWEOF, 1) == 0)
347 device->required_tapemarks--;
351 if (device->char_data.idal_buf != NULL) {
352 idal_buffer_free(device->char_data.idal_buf);
353 device->char_data.idal_buf = NULL;
355 tape_release(device);
356 filp->private_data = tape_put_device(device);
362 * Tape device io controls.
365 tapechar_ioctl(struct inode *inp, struct file *filp,
366 unsigned int no, unsigned long data)
368 struct tape_device *device;
371 DBF_EVENT(6, "TCHAR:ioct\n");
373 device = (struct tape_device *) filp->private_data;
375 if (no == MTIOCTOP) {
378 if (copy_from_user(&op, (char __user *) data, sizeof(op)) != 0)
384 * Operations that change tape position should write final
399 #ifdef CONFIG_S390_TAPE_BLOCK
400 device->blk_data.medium_changed = 1;
402 if (device->required_tapemarks)
403 tape_std_terminate_write(device);
407 rc = tape_mtop(device, op.mt_op, op.mt_count);
409 if (op.mt_op == MTWEOF && rc == 0) {
410 if (op.mt_count > device->required_tapemarks)
411 device->required_tapemarks = 0;
413 device->required_tapemarks -= op.mt_count;
417 if (no == MTIOCPOS) {
418 /* MTIOCPOS: query the tape position. */
421 rc = tape_mtop(device, MTTELL, 1);
425 if (copy_to_user((char __user *) data, &pos, sizeof(pos)) != 0)
429 if (no == MTIOCGET) {
430 /* MTIOCGET: query the tape drive status. */
433 memset(&get, 0, sizeof(get));
434 get.mt_type = MT_ISUNKNOWN;
435 get.mt_resid = 0 /* device->devstat.rescnt */;
436 get.mt_dsreg = device->tape_state;
437 /* FIXME: mt_gstat, mt_erreg, mt_fileno */
441 get.mt_gstat = device->tape_generic_status;
443 if (device->medium_state == MS_LOADED) {
444 rc = tape_mtop(device, MTTELL, 1);
450 get.mt_gstat |= GMT_BOT(~0);
455 if (copy_to_user((char __user *) data, &get, sizeof(get)) != 0)
460 /* Try the discipline ioctl function. */
461 if (device->discipline->ioctl_fn == NULL)
463 return device->discipline->ioctl_fn(device, no, data);
467 * Initialize character device frontend.
474 if (alloc_chrdev_region(&dev, 0, 256, "tape") != 0)
477 tapechar_major = MAJOR(dev);
478 PRINT_INFO("tape gets major %d for character devices\n", MAJOR(dev));
489 PRINT_INFO("tape releases major %d for character devices\n",
491 unregister_chrdev_region(MKDEV(tapechar_major, 0), 256);