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 #define KMSG_COMPONENT "tape"
14 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
16 #include <linux/module.h>
17 #include <linux/types.h>
18 #include <linux/proc_fs.h>
19 #include <linux/mtio.h>
20 #include <linux/smp_lock.h>
21 #include <linux/compat.h>
23 #include <asm/uaccess.h>
25 #define TAPE_DBF_AREA tape_core_dbf
29 #include "tape_class.h"
31 #define TAPECHAR_MAJOR 0 /* get dynamic major */
34 * file operation structure for tape character frontend
36 static ssize_t
tapechar_read(struct file
*, char __user
*, size_t, loff_t
*);
37 static ssize_t
tapechar_write(struct file
*, const char __user
*, size_t, loff_t
*);
38 static int tapechar_open(struct inode
*,struct file
*);
39 static int tapechar_release(struct inode
*,struct file
*);
40 static long tapechar_ioctl(struct file
*, unsigned int, unsigned long);
42 static long tapechar_compat_ioctl(struct file
*, unsigned int, unsigned long);
45 static const struct file_operations tape_fops
=
48 .read
= tapechar_read
,
49 .write
= tapechar_write
,
50 .unlocked_ioctl
= tapechar_ioctl
,
52 .compat_ioctl
= tapechar_compat_ioctl
,
54 .open
= tapechar_open
,
55 .release
= tapechar_release
,
59 static int tapechar_major
= TAPECHAR_MAJOR
;
62 * This function is called for every new tapedevice
65 tapechar_setup_device(struct tape_device
* device
)
69 sprintf(device_name
, "ntibm%i", device
->first_minor
/ 2);
70 device
->nt
= register_tape_dev(
72 MKDEV(tapechar_major
, device
->first_minor
),
78 device
->rt
= register_tape_dev(
80 MKDEV(tapechar_major
, device
->first_minor
+ 1),
90 tapechar_cleanup_device(struct tape_device
*device
)
92 unregister_tape_dev(&device
->cdev
->dev
, device
->rt
);
94 unregister_tape_dev(&device
->cdev
->dev
, device
->nt
);
99 tapechar_check_idalbuffer(struct tape_device
*device
, size_t block_size
)
101 struct idal_buffer
*new;
103 if (device
->char_data
.idal_buf
!= NULL
&&
104 device
->char_data
.idal_buf
->size
== block_size
)
107 if (block_size
> MAX_BLOCKSIZE
) {
108 DBF_EVENT(3, "Invalid blocksize (%zd > %d)\n",
109 block_size
, MAX_BLOCKSIZE
);
113 /* The current idal buffer is not correct. Allocate a new one. */
114 new = idal_buffer_alloc(block_size
, 0);
118 if (device
->char_data
.idal_buf
!= NULL
)
119 idal_buffer_free(device
->char_data
.idal_buf
);
121 device
->char_data
.idal_buf
= new;
127 * Tape device read function
130 tapechar_read(struct file
*filp
, char __user
*data
, size_t count
, loff_t
*ppos
)
132 struct tape_device
*device
;
133 struct tape_request
*request
;
137 DBF_EVENT(6, "TCHAR:read\n");
138 device
= (struct tape_device
*) filp
->private_data
;
141 * If the tape isn't terminated yet, do it now. And since we then
142 * are at the end of the tape there wouldn't be anything to read
143 * anyways. So we return immediatly.
145 if(device
->required_tapemarks
) {
146 return tape_std_terminate_write(device
);
149 /* Find out block size to use */
150 if (device
->char_data
.block_size
!= 0) {
151 if (count
< device
->char_data
.block_size
) {
152 DBF_EVENT(3, "TCHAR:read smaller than block "
153 "size was requested\n");
156 block_size
= device
->char_data
.block_size
;
161 rc
= tapechar_check_idalbuffer(device
, block_size
);
165 #ifdef CONFIG_S390_TAPE_BLOCK
166 /* Changes position. */
167 device
->blk_data
.medium_changed
= 1;
170 DBF_EVENT(6, "TCHAR:nbytes: %lx\n", block_size
);
171 /* Let the discipline build the ccw chain. */
172 request
= device
->discipline
->read_block(device
, block_size
);
174 return PTR_ERR(request
);
176 rc
= tape_do_io(device
, request
);
178 rc
= block_size
- request
->rescnt
;
179 DBF_EVENT(6, "TCHAR:rbytes: %x\n", rc
);
180 /* Copy data from idal buffer to user space. */
181 if (idal_buffer_to_user(device
->char_data
.idal_buf
,
185 tape_free_request(request
);
190 * Tape device write function
193 tapechar_write(struct file
*filp
, const char __user
*data
, size_t count
, loff_t
*ppos
)
195 struct tape_device
*device
;
196 struct tape_request
*request
;
202 DBF_EVENT(6, "TCHAR:write\n");
203 device
= (struct tape_device
*) filp
->private_data
;
204 /* Find out block size and number of blocks */
205 if (device
->char_data
.block_size
!= 0) {
206 if (count
< device
->char_data
.block_size
) {
207 DBF_EVENT(3, "TCHAR:write smaller than block "
208 "size was requested\n");
211 block_size
= device
->char_data
.block_size
;
212 nblocks
= count
/ block_size
;
218 rc
= tapechar_check_idalbuffer(device
, block_size
);
222 #ifdef CONFIG_S390_TAPE_BLOCK
223 /* Changes position. */
224 device
->blk_data
.medium_changed
= 1;
227 DBF_EVENT(6,"TCHAR:nbytes: %lx\n", block_size
);
228 DBF_EVENT(6, "TCHAR:nblocks: %x\n", nblocks
);
229 /* Let the discipline build the ccw chain. */
230 request
= device
->discipline
->write_block(device
, block_size
);
232 return PTR_ERR(request
);
235 for (i
= 0; i
< nblocks
; i
++) {
236 /* Copy data from user space to idal buffer. */
237 if (idal_buffer_from_user(device
->char_data
.idal_buf
,
242 rc
= tape_do_io(device
, request
);
245 DBF_EVENT(6, "TCHAR:wbytes: %lx\n",
246 block_size
- request
->rescnt
);
247 written
+= block_size
- request
->rescnt
;
248 if (request
->rescnt
!= 0)
252 tape_free_request(request
);
255 * Ok, the device has no more space. It has NOT written
258 if (device
->discipline
->process_eov
)
259 device
->discipline
->process_eov(device
);
266 * After doing a write we always need two tapemarks to correctly
267 * terminate the tape (one to terminate the file, the second to
268 * flag the end of recorded data.
269 * Since process_eov positions the tape in front of the written
270 * tapemark it doesn't hurt to write two marks again.
273 device
->required_tapemarks
= 2;
275 return rc
? rc
: written
;
279 * Character frontend tape device open function.
282 tapechar_open (struct inode
*inode
, struct file
*filp
)
284 struct tape_device
*device
;
287 DBF_EVENT(6, "TCHAR:open: %i:%i\n",
288 imajor(filp
->f_path
.dentry
->d_inode
),
289 iminor(filp
->f_path
.dentry
->d_inode
));
291 if (imajor(filp
->f_path
.dentry
->d_inode
) != tapechar_major
)
294 minor
= iminor(filp
->f_path
.dentry
->d_inode
);
295 device
= tape_find_device(minor
/ TAPE_MINORS_PER_DEV
);
296 if (IS_ERR(device
)) {
297 DBF_EVENT(3, "TCHAR:open: tape_find_device() failed\n");
298 return PTR_ERR(device
);
301 rc
= tape_open(device
);
303 filp
->private_data
= device
;
304 nonseekable_open(inode
, filp
);
306 tape_put_device(device
);
312 * Character frontend tape device release function.
316 tapechar_release(struct inode
*inode
, struct file
*filp
)
318 struct tape_device
*device
;
320 DBF_EVENT(6, "TCHAR:release: %x\n", iminor(inode
));
321 device
= (struct tape_device
*) filp
->private_data
;
324 * If this is the rewinding tape minor then rewind. In that case we
325 * write all required tapemarks. Otherwise only one to terminate the
328 if ((iminor(inode
) & 1) != 0) {
329 if (device
->required_tapemarks
)
330 tape_std_terminate_write(device
);
331 tape_mtop(device
, MTREW
, 1);
333 if (device
->required_tapemarks
> 1) {
334 if (tape_mtop(device
, MTWEOF
, 1) == 0)
335 device
->required_tapemarks
--;
339 if (device
->char_data
.idal_buf
!= NULL
) {
340 idal_buffer_free(device
->char_data
.idal_buf
);
341 device
->char_data
.idal_buf
= NULL
;
343 tape_release(device
);
344 filp
->private_data
= NULL
;
345 tape_put_device(device
);
351 * Tape device io controls.
354 __tapechar_ioctl(struct tape_device
*device
,
355 unsigned int no
, unsigned long data
)
359 if (no
== MTIOCTOP
) {
362 if (copy_from_user(&op
, (char __user
*) data
, sizeof(op
)) != 0)
368 * Operations that change tape position should write final
383 #ifdef CONFIG_S390_TAPE_BLOCK
384 device
->blk_data
.medium_changed
= 1;
386 if (device
->required_tapemarks
)
387 tape_std_terminate_write(device
);
391 rc
= tape_mtop(device
, op
.mt_op
, op
.mt_count
);
393 if (op
.mt_op
== MTWEOF
&& rc
== 0) {
394 if (op
.mt_count
> device
->required_tapemarks
)
395 device
->required_tapemarks
= 0;
397 device
->required_tapemarks
-= op
.mt_count
;
401 if (no
== MTIOCPOS
) {
402 /* MTIOCPOS: query the tape position. */
405 rc
= tape_mtop(device
, MTTELL
, 1);
409 if (copy_to_user((char __user
*) data
, &pos
, sizeof(pos
)) != 0)
413 if (no
== MTIOCGET
) {
414 /* MTIOCGET: query the tape drive status. */
417 memset(&get
, 0, sizeof(get
));
418 get
.mt_type
= MT_ISUNKNOWN
;
419 get
.mt_resid
= 0 /* device->devstat.rescnt */;
420 get
.mt_dsreg
= device
->tape_state
;
421 /* FIXME: mt_gstat, mt_erreg, mt_fileno */
425 get
.mt_gstat
= device
->tape_generic_status
;
427 if (device
->medium_state
== MS_LOADED
) {
428 rc
= tape_mtop(device
, MTTELL
, 1);
434 get
.mt_gstat
|= GMT_BOT(~0);
439 if (copy_to_user((char __user
*) data
, &get
, sizeof(get
)) != 0)
444 /* Try the discipline ioctl function. */
445 if (device
->discipline
->ioctl_fn
== NULL
)
447 return device
->discipline
->ioctl_fn(device
, no
, data
);
451 tapechar_ioctl(struct file
*filp
, unsigned int no
, unsigned long data
)
453 struct tape_device
*device
;
456 DBF_EVENT(6, "TCHAR:ioct\n");
458 device
= (struct tape_device
*) filp
->private_data
;
459 mutex_lock(&device
->mutex
);
460 rc
= __tapechar_ioctl(device
, no
, data
);
461 mutex_unlock(&device
->mutex
);
467 tapechar_compat_ioctl(struct file
*filp
, unsigned int no
, unsigned long data
)
469 struct tape_device
*device
= filp
->private_data
;
470 int rval
= -ENOIOCTLCMD
;
473 /* The 'arg' argument of any ioctl function may only be used for
474 * pointers because of the compat pointer conversion.
475 * Consider this when adding new ioctls.
477 argp
= (unsigned long) compat_ptr(data
);
478 if (device
->discipline
->ioctl_fn
) {
479 mutex_lock(&device
->mutex
);
480 rval
= device
->discipline
->ioctl_fn(device
, no
, argp
);
481 mutex_unlock(&device
->mutex
);
488 #endif /* CONFIG_COMPAT */
491 * Initialize character device frontend.
498 if (alloc_chrdev_region(&dev
, 0, 256, "tape") != 0)
501 tapechar_major
= MAJOR(dev
);
512 unregister_chrdev_region(MKDEV(tapechar_major
, 0), 256);