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 #include <linux/module.h>
14 #include <linux/types.h>
15 #include <linux/proc_fs.h>
16 #include <linux/mtio.h>
17 #include <linux/smp_lock.h>
19 #include <asm/uaccess.h>
21 #define TAPE_DBF_AREA tape_core_dbf
25 #include "tape_class.h"
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 __user *, size_t, loff_t *);
33 static ssize_t tapechar_write(struct file *, const char __user *, size_t, loff_t *);
34 static int tapechar_open(struct inode *,struct file *);
35 static int tapechar_release(struct inode *,struct file *);
36 static long tapechar_ioctl(struct file *, unsigned int, unsigned long);
37 static long tapechar_compat_ioctl(struct file *, unsigned int,
40 static const struct file_operations tape_fops =
43 .read = tapechar_read,
44 .write = tapechar_write,
45 .unlocked_ioctl = tapechar_ioctl,
46 .compat_ioctl = tapechar_compat_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->cdev->dev, device->rt);
86 unregister_tape_dev(&device->cdev->dev, device->nt);
91 tapechar_check_idalbuffer(struct tape_device *device, size_t block_size)
93 struct idal_buffer *new;
95 if (device->char_data.idal_buf != NULL &&
96 device->char_data.idal_buf->size == block_size)
99 if (block_size > MAX_BLOCKSIZE) {
100 DBF_EVENT(3, "Invalid blocksize (%zd > %d)\n",
101 block_size, MAX_BLOCKSIZE);
105 /* The current idal buffer is not correct. Allocate a new one. */
106 new = idal_buffer_alloc(block_size, 0);
110 if (device->char_data.idal_buf != NULL)
111 idal_buffer_free(device->char_data.idal_buf);
113 device->char_data.idal_buf = new;
119 * Tape device read function
122 tapechar_read(struct file *filp, char __user *data, size_t count, loff_t *ppos)
124 struct tape_device *device;
125 struct tape_request *request;
129 DBF_EVENT(6, "TCHAR:read\n");
130 device = (struct tape_device *) filp->private_data;
133 * If the tape isn't terminated yet, do it now. And since we then
134 * are at the end of the tape there wouldn't be anything to read
135 * anyways. So we return immediatly.
137 if(device->required_tapemarks) {
138 return tape_std_terminate_write(device);
141 /* Find out block size to use */
142 if (device->char_data.block_size != 0) {
143 if (count < device->char_data.block_size) {
144 DBF_EVENT(3, "TCHAR:read smaller than block "
145 "size was requested\n");
148 block_size = device->char_data.block_size;
153 rc = tapechar_check_idalbuffer(device, block_size);
157 #ifdef CONFIG_S390_TAPE_BLOCK
158 /* Changes position. */
159 device->blk_data.medium_changed = 1;
162 DBF_EVENT(6, "TCHAR:nbytes: %lx\n", block_size);
163 /* Let the discipline build the ccw chain. */
164 request = device->discipline->read_block(device, block_size);
166 return PTR_ERR(request);
168 rc = tape_do_io(device, request);
170 rc = block_size - request->rescnt;
171 DBF_EVENT(6, "TCHAR:rbytes: %x\n", rc);
172 /* Copy data from idal buffer to user space. */
173 if (idal_buffer_to_user(device->char_data.idal_buf,
177 tape_free_request(request);
182 * Tape device write function
185 tapechar_write(struct file *filp, const char __user *data, size_t count, loff_t *ppos)
187 struct tape_device *device;
188 struct tape_request *request;
194 DBF_EVENT(6, "TCHAR:write\n");
195 device = (struct tape_device *) filp->private_data;
196 /* Find out block size and number of blocks */
197 if (device->char_data.block_size != 0) {
198 if (count < device->char_data.block_size) {
199 DBF_EVENT(3, "TCHAR:write smaller than block "
200 "size was requested\n");
203 block_size = device->char_data.block_size;
204 nblocks = count / block_size;
210 rc = tapechar_check_idalbuffer(device, block_size);
214 #ifdef CONFIG_S390_TAPE_BLOCK
215 /* Changes position. */
216 device->blk_data.medium_changed = 1;
219 DBF_EVENT(6,"TCHAR:nbytes: %lx\n", block_size);
220 DBF_EVENT(6, "TCHAR:nblocks: %x\n", nblocks);
221 /* Let the discipline build the ccw chain. */
222 request = device->discipline->write_block(device, block_size);
224 return PTR_ERR(request);
227 for (i = 0; i < nblocks; i++) {
228 /* Copy data from user space to idal buffer. */
229 if (idal_buffer_from_user(device->char_data.idal_buf,
234 rc = tape_do_io(device, request);
237 DBF_EVENT(6, "TCHAR:wbytes: %lx\n",
238 block_size - request->rescnt);
239 written += block_size - request->rescnt;
240 if (request->rescnt != 0)
244 tape_free_request(request);
247 * Ok, the device has no more space. It has NOT written
250 if (device->discipline->process_eov)
251 device->discipline->process_eov(device);
258 * After doing a write we always need two tapemarks to correctly
259 * terminate the tape (one to terminate the file, the second to
260 * flag the end of recorded data.
261 * Since process_eov positions the tape in front of the written
262 * tapemark it doesn't hurt to write two marks again.
265 device->required_tapemarks = 2;
267 return rc ? rc : written;
271 * Character frontend tape device open function.
274 tapechar_open (struct inode *inode, struct file *filp)
276 struct tape_device *device;
279 DBF_EVENT(6, "TCHAR:open: %i:%i\n",
280 imajor(filp->f_path.dentry->d_inode),
281 iminor(filp->f_path.dentry->d_inode));
283 if (imajor(filp->f_path.dentry->d_inode) != tapechar_major)
286 minor = iminor(filp->f_path.dentry->d_inode);
287 device = tape_find_device(minor / TAPE_MINORS_PER_DEV);
288 if (IS_ERR(device)) {
289 DBF_EVENT(3, "TCHAR:open: tape_find_device() failed\n");
290 return PTR_ERR(device);
293 rc = tape_open(device);
295 filp->private_data = device;
296 nonseekable_open(inode, filp);
298 tape_put_device(device);
304 * Character frontend tape device release function.
308 tapechar_release(struct inode *inode, struct file *filp)
310 struct tape_device *device;
312 DBF_EVENT(6, "TCHAR:release: %x\n", iminor(inode));
313 device = (struct tape_device *) filp->private_data;
316 * If this is the rewinding tape minor then rewind. In that case we
317 * write all required tapemarks. Otherwise only one to terminate the
320 if ((iminor(inode) & 1) != 0) {
321 if (device->required_tapemarks)
322 tape_std_terminate_write(device);
323 tape_mtop(device, MTREW, 1);
325 if (device->required_tapemarks > 1) {
326 if (tape_mtop(device, MTWEOF, 1) == 0)
327 device->required_tapemarks--;
331 if (device->char_data.idal_buf != NULL) {
332 idal_buffer_free(device->char_data.idal_buf);
333 device->char_data.idal_buf = NULL;
335 tape_release(device);
336 filp->private_data = NULL;
337 tape_put_device(device);
343 * Tape device io controls.
346 __tapechar_ioctl(struct tape_device *device,
347 unsigned int no, unsigned long data)
351 if (no == MTIOCTOP) {
354 if (copy_from_user(&op, (char __user *) data, sizeof(op)) != 0)
360 * Operations that change tape position should write final
375 #ifdef CONFIG_S390_TAPE_BLOCK
376 device->blk_data.medium_changed = 1;
378 if (device->required_tapemarks)
379 tape_std_terminate_write(device);
383 rc = tape_mtop(device, op.mt_op, op.mt_count);
385 if (op.mt_op == MTWEOF && rc == 0) {
386 if (op.mt_count > device->required_tapemarks)
387 device->required_tapemarks = 0;
389 device->required_tapemarks -= op.mt_count;
393 if (no == MTIOCPOS) {
394 /* MTIOCPOS: query the tape position. */
397 rc = tape_mtop(device, MTTELL, 1);
401 if (copy_to_user((char __user *) data, &pos, sizeof(pos)) != 0)
405 if (no == MTIOCGET) {
406 /* MTIOCGET: query the tape drive status. */
409 memset(&get, 0, sizeof(get));
410 get.mt_type = MT_ISUNKNOWN;
411 get.mt_resid = 0 /* device->devstat.rescnt */;
412 get.mt_dsreg = device->tape_state;
413 /* FIXME: mt_gstat, mt_erreg, mt_fileno */
417 get.mt_gstat = device->tape_generic_status;
419 if (device->medium_state == MS_LOADED) {
420 rc = tape_mtop(device, MTTELL, 1);
426 get.mt_gstat |= GMT_BOT(~0);
431 if (copy_to_user((char __user *) data, &get, sizeof(get)) != 0)
436 /* Try the discipline ioctl function. */
437 if (device->discipline->ioctl_fn == NULL)
439 return device->discipline->ioctl_fn(device, no, data);
443 tapechar_ioctl(struct file *filp, unsigned int no, unsigned long data)
445 struct tape_device *device;
448 DBF_EVENT(6, "TCHAR:ioct\n");
450 device = (struct tape_device *) filp->private_data;
451 mutex_lock(&device->mutex);
452 rc = __tapechar_ioctl(device, no, data);
453 mutex_unlock(&device->mutex);
458 tapechar_compat_ioctl(struct file *filp, unsigned int no, unsigned long data)
460 struct tape_device *device = filp->private_data;
461 int rval = -ENOIOCTLCMD;
463 if (device->discipline->ioctl_fn) {
464 mutex_lock(&device->mutex);
465 rval = device->discipline->ioctl_fn(device, no, data);
466 mutex_unlock(&device->mutex);
475 * Initialize character device frontend.
482 if (alloc_chrdev_region(&dev, 0, 256, "tape") != 0)
485 tapechar_major = MAJOR(dev);
496 unregister_chrdev_region(MKDEV(tapechar_major, 0), 256);