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;
148 /* Check position. */
149 if (ppos != &filp->f_pos) {
151 * "A request was outside the capabilities of the device."
152 * This check uses internal knowledge about how pread and
155 DBF_EVENT(6, "TCHAR:ppos wrong\n");
160 * If the tape isn't terminated yet, do it now. And since we then
161 * are at the end of the tape there wouldn't be anything to read
162 * anyways. So we return immediatly.
164 if(device->required_tapemarks) {
165 return tape_std_terminate_write(device);
168 /* Find out block size to use */
169 if (device->char_data.block_size != 0) {
170 if (count < device->char_data.block_size) {
171 DBF_EVENT(3, "TCHAR:read smaller than block "
172 "size was requested\n");
175 block_size = device->char_data.block_size;
180 rc = tapechar_check_idalbuffer(device, block_size);
184 #ifdef CONFIG_S390_TAPE_BLOCK
185 /* Changes position. */
186 device->blk_data.medium_changed = 1;
189 DBF_EVENT(6, "TCHAR:nbytes: %lx\n", block_size);
190 /* Let the discipline build the ccw chain. */
191 request = device->discipline->read_block(device, block_size);
193 return PTR_ERR(request);
195 rc = tape_do_io(device, request);
197 rc = block_size - request->rescnt;
198 DBF_EVENT(6, "TCHAR:rbytes: %x\n", rc);
200 /* Copy data from idal buffer to user space. */
201 if (idal_buffer_to_user(device->char_data.idal_buf,
205 tape_free_request(request);
210 * Tape device write function
213 tapechar_write(struct file *filp, const char __user *data, size_t count, loff_t *ppos)
215 struct tape_device *device;
216 struct tape_request *request;
222 DBF_EVENT(6, "TCHAR:write\n");
223 device = (struct tape_device *) filp->private_data;
225 if (ppos != &filp->f_pos) {
226 /* "A request was outside the capabilities of the device." */
227 DBF_EVENT(6, "TCHAR:ppos wrong\n");
230 /* Find out block size and number of blocks */
231 if (device->char_data.block_size != 0) {
232 if (count < device->char_data.block_size) {
233 DBF_EVENT(3, "TCHAR:write smaller than block "
234 "size was requested\n");
237 block_size = device->char_data.block_size;
238 nblocks = count / block_size;
244 rc = tapechar_check_idalbuffer(device, block_size);
248 #ifdef CONFIG_S390_TAPE_BLOCK
249 /* Changes position. */
250 device->blk_data.medium_changed = 1;
253 DBF_EVENT(6,"TCHAR:nbytes: %lx\n", block_size);
254 DBF_EVENT(6, "TCHAR:nblocks: %x\n", nblocks);
255 /* Let the discipline build the ccw chain. */
256 request = device->discipline->write_block(device, block_size);
258 return PTR_ERR(request);
261 for (i = 0; i < nblocks; i++) {
262 /* Copy data from user space to idal buffer. */
263 if (idal_buffer_from_user(device->char_data.idal_buf,
268 rc = tape_do_io(device, request);
271 DBF_EVENT(6, "TCHAR:wbytes: %lx\n",
272 block_size - request->rescnt);
273 filp->f_pos += block_size - request->rescnt;
274 written += block_size - request->rescnt;
275 if (request->rescnt != 0)
279 tape_free_request(request);
282 * Ok, the device has no more space. It has NOT written
285 if (device->discipline->process_eov)
286 device->discipline->process_eov(device);
293 * After doing a write we always need two tapemarks to correctly
294 * terminate the tape (one to terminate the file, the second to
295 * flag the end of recorded data.
296 * Since process_eov positions the tape in front of the written
297 * tapemark it doesn't hurt to write two marks again.
300 device->required_tapemarks = 2;
302 return rc ? rc : written;
306 * Character frontend tape device open function.
309 tapechar_open (struct inode *inode, struct file *filp)
311 struct tape_device *device;
314 DBF_EVENT(6, "TCHAR:open: %i:%i\n",
315 imajor(filp->f_dentry->d_inode),
316 iminor(filp->f_dentry->d_inode));
318 if (imajor(filp->f_dentry->d_inode) != tapechar_major)
321 minor = iminor(filp->f_dentry->d_inode);
322 device = tape_get_device(minor / TAPE_MINORS_PER_DEV);
323 if (IS_ERR(device)) {
324 DBF_EVENT(3, "TCHAR:open: tape_get_device() failed\n");
325 return PTR_ERR(device);
329 rc = tape_open(device);
331 filp->private_data = device;
334 tape_put_device(device);
340 * Character frontend tape device release function.
344 tapechar_release(struct inode *inode, struct file *filp)
346 struct tape_device *device;
348 DBF_EVENT(6, "TCHAR:release: %x\n", iminor(inode));
349 device = (struct tape_device *) filp->private_data;
352 * If this is the rewinding tape minor then rewind. In that case we
353 * write all required tapemarks. Otherwise only one to terminate the
356 if ((iminor(inode) & 1) != 0) {
357 if (device->required_tapemarks)
358 tape_std_terminate_write(device);
359 tape_mtop(device, MTREW, 1);
361 if (device->required_tapemarks > 1) {
362 if (tape_mtop(device, MTWEOF, 1) == 0)
363 device->required_tapemarks--;
367 if (device->char_data.idal_buf != NULL) {
368 idal_buffer_free(device->char_data.idal_buf);
369 device->char_data.idal_buf = NULL;
371 tape_release(device);
372 filp->private_data = tape_put_device(device);
378 * Tape device io controls.
381 tapechar_ioctl(struct inode *inp, struct file *filp,
382 unsigned int no, unsigned long data)
384 struct tape_device *device;
387 DBF_EVENT(6, "TCHAR:ioct\n");
389 device = (struct tape_device *) filp->private_data;
391 if (no == MTIOCTOP) {
394 if (copy_from_user(&op, (char __user *) data, sizeof(op)) != 0)
400 * Operations that change tape position should write final
415 #ifdef CONFIG_S390_TAPE_BLOCK
416 device->blk_data.medium_changed = 1;
418 if (device->required_tapemarks)
419 tape_std_terminate_write(device);
423 rc = tape_mtop(device, op.mt_op, op.mt_count);
425 if (op.mt_op == MTWEOF && rc == 0) {
426 if (op.mt_count > device->required_tapemarks)
427 device->required_tapemarks = 0;
429 device->required_tapemarks -= op.mt_count;
433 if (no == MTIOCPOS) {
434 /* MTIOCPOS: query the tape position. */
437 rc = tape_mtop(device, MTTELL, 1);
441 if (copy_to_user((char __user *) data, &pos, sizeof(pos)) != 0)
445 if (no == MTIOCGET) {
446 /* MTIOCGET: query the tape drive status. */
449 memset(&get, 0, sizeof(get));
450 get.mt_type = MT_ISUNKNOWN;
451 get.mt_resid = 0 /* device->devstat.rescnt */;
452 get.mt_dsreg = device->tape_state;
453 /* FIXME: mt_gstat, mt_erreg, mt_fileno */
457 get.mt_gstat = device->tape_generic_status;
459 if (device->medium_state == MS_LOADED) {
460 rc = tape_mtop(device, MTTELL, 1);
466 get.mt_gstat |= GMT_BOT(~0);
471 if (copy_to_user((char __user *) data, &get, sizeof(get)) != 0)
476 /* Try the discipline ioctl function. */
477 if (device->discipline->ioctl_fn == NULL)
479 return device->discipline->ioctl_fn(device, no, data);
483 * Initialize character device frontend.
490 if (alloc_chrdev_region(&dev, 0, 256, "tape") != 0)
493 tapechar_major = MAJOR(dev);
494 PRINT_INFO("tape gets major %d for character devices\n", MAJOR(dev));
505 PRINT_INFO("tape releases major %d for character devices\n",
507 unregister_chrdev_region(MKDEV(tapechar_major, 0), 256);