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/module.h>
14 #include <linux/types.h>
15 #include <linux/proc_fs.h>
16 #include <linux/mtio.h>
18 #include <asm/uaccess.h>
20 #define TAPE_DBF_AREA tape_core_dbf
24 #include "tape_class.h"
26 #define PRINTK_HEADER "TAPE_CHAR: "
28 #define TAPECHAR_MAJOR 0 /* get dynamic major */
31 * file operation structure for tape character frontend
33 static ssize_t
tapechar_read(struct file
*, char __user
*, size_t, loff_t
*);
34 static ssize_t
tapechar_write(struct file
*, const char __user
*, size_t, loff_t
*);
35 static int tapechar_open(struct inode
*,struct file
*);
36 static int tapechar_release(struct inode
*,struct file
*);
37 static int tapechar_ioctl(struct inode
*, struct file
*, unsigned int,
39 static long tapechar_compat_ioctl(struct file
*, unsigned int,
42 static struct file_operations tape_fops
=
45 .read
= tapechar_read
,
46 .write
= tapechar_write
,
47 .ioctl
= tapechar_ioctl
,
48 .compat_ioctl
= tapechar_compat_ioctl
,
49 .open
= tapechar_open
,
50 .release
= tapechar_release
,
53 static int tapechar_major
= TAPECHAR_MAJOR
;
56 * This function is called for every new tapedevice
59 tapechar_setup_device(struct tape_device
* device
)
63 sprintf(device_name
, "ntibm%i", device
->first_minor
/ 2);
64 device
->nt
= register_tape_dev(
66 MKDEV(tapechar_major
, device
->first_minor
),
72 device
->rt
= register_tape_dev(
74 MKDEV(tapechar_major
, device
->first_minor
+ 1),
84 tapechar_cleanup_device(struct tape_device
*device
)
86 unregister_tape_dev(device
->rt
);
88 unregister_tape_dev(device
->nt
);
93 * Terminate write command (we write two TMs and skip backward over last)
94 * This ensures that the tape is always correctly terminated.
95 * When the user writes afterwards a new file, he will overwrite the
96 * second TM and therefore one TM will remain to separate the
97 * two files on the tape...
100 tapechar_terminate_write(struct tape_device
*device
)
102 if (tape_mtop(device
, MTWEOF
, 1) == 0 &&
103 tape_mtop(device
, MTWEOF
, 1) == 0)
104 tape_mtop(device
, MTBSR
, 1);
108 tapechar_check_idalbuffer(struct tape_device
*device
, size_t block_size
)
110 struct idal_buffer
*new;
112 if (device
->char_data
.idal_buf
!= NULL
&&
113 device
->char_data
.idal_buf
->size
== block_size
)
116 if (block_size
> MAX_BLOCKSIZE
) {
117 DBF_EVENT(3, "Invalid blocksize (%zd > %d)\n",
118 block_size
, MAX_BLOCKSIZE
);
119 PRINT_ERR("Invalid blocksize (%zd> %d)\n",
120 block_size
, MAX_BLOCKSIZE
);
124 /* The current idal buffer is not correct. Allocate a new one. */
125 new = idal_buffer_alloc(block_size
, 0);
129 if (device
->char_data
.idal_buf
!= NULL
)
130 idal_buffer_free(device
->char_data
.idal_buf
);
132 device
->char_data
.idal_buf
= new;
138 * Tape device read function
141 tapechar_read(struct file
*filp
, char __user
*data
, size_t count
, loff_t
*ppos
)
143 struct tape_device
*device
;
144 struct tape_request
*request
;
148 DBF_EVENT(6, "TCHAR:read\n");
149 device
= (struct tape_device
*) filp
->private_data
;
152 * If the tape isn't terminated yet, do it now. And since we then
153 * are at the end of the tape there wouldn't be anything to read
154 * anyways. So we return immediatly.
156 if(device
->required_tapemarks
) {
157 return tape_std_terminate_write(device
);
160 /* Find out block size to use */
161 if (device
->char_data
.block_size
!= 0) {
162 if (count
< device
->char_data
.block_size
) {
163 DBF_EVENT(3, "TCHAR:read smaller than block "
164 "size was requested\n");
167 block_size
= device
->char_data
.block_size
;
172 rc
= tapechar_check_idalbuffer(device
, block_size
);
176 #ifdef CONFIG_S390_TAPE_BLOCK
177 /* Changes position. */
178 device
->blk_data
.medium_changed
= 1;
181 DBF_EVENT(6, "TCHAR:nbytes: %lx\n", block_size
);
182 /* Let the discipline build the ccw chain. */
183 request
= device
->discipline
->read_block(device
, block_size
);
185 return PTR_ERR(request
);
187 rc
= tape_do_io(device
, request
);
189 rc
= block_size
- request
->rescnt
;
190 DBF_EVENT(6, "TCHAR:rbytes: %x\n", rc
);
192 /* Copy data from idal buffer to user space. */
193 if (idal_buffer_to_user(device
->char_data
.idal_buf
,
197 tape_free_request(request
);
202 * Tape device write function
205 tapechar_write(struct file
*filp
, const char __user
*data
, size_t count
, loff_t
*ppos
)
207 struct tape_device
*device
;
208 struct tape_request
*request
;
214 DBF_EVENT(6, "TCHAR:write\n");
215 device
= (struct tape_device
*) filp
->private_data
;
216 /* Find out block size and number of blocks */
217 if (device
->char_data
.block_size
!= 0) {
218 if (count
< device
->char_data
.block_size
) {
219 DBF_EVENT(3, "TCHAR:write smaller than block "
220 "size was requested\n");
223 block_size
= device
->char_data
.block_size
;
224 nblocks
= count
/ block_size
;
230 rc
= tapechar_check_idalbuffer(device
, block_size
);
234 #ifdef CONFIG_S390_TAPE_BLOCK
235 /* Changes position. */
236 device
->blk_data
.medium_changed
= 1;
239 DBF_EVENT(6,"TCHAR:nbytes: %lx\n", block_size
);
240 DBF_EVENT(6, "TCHAR:nblocks: %x\n", nblocks
);
241 /* Let the discipline build the ccw chain. */
242 request
= device
->discipline
->write_block(device
, block_size
);
244 return PTR_ERR(request
);
247 for (i
= 0; i
< nblocks
; i
++) {
248 /* Copy data from user space to idal buffer. */
249 if (idal_buffer_from_user(device
->char_data
.idal_buf
,
254 rc
= tape_do_io(device
, request
);
257 DBF_EVENT(6, "TCHAR:wbytes: %lx\n",
258 block_size
- request
->rescnt
);
259 filp
->f_pos
+= block_size
- request
->rescnt
;
260 written
+= block_size
- request
->rescnt
;
261 if (request
->rescnt
!= 0)
265 tape_free_request(request
);
268 * Ok, the device has no more space. It has NOT written
271 if (device
->discipline
->process_eov
)
272 device
->discipline
->process_eov(device
);
279 * After doing a write we always need two tapemarks to correctly
280 * terminate the tape (one to terminate the file, the second to
281 * flag the end of recorded data.
282 * Since process_eov positions the tape in front of the written
283 * tapemark it doesn't hurt to write two marks again.
286 device
->required_tapemarks
= 2;
288 return rc
? rc
: written
;
292 * Character frontend tape device open function.
295 tapechar_open (struct inode
*inode
, struct file
*filp
)
297 struct tape_device
*device
;
300 DBF_EVENT(6, "TCHAR:open: %i:%i\n",
301 imajor(filp
->f_dentry
->d_inode
),
302 iminor(filp
->f_dentry
->d_inode
));
304 if (imajor(filp
->f_dentry
->d_inode
) != tapechar_major
)
307 minor
= iminor(filp
->f_dentry
->d_inode
);
308 device
= tape_get_device(minor
/ TAPE_MINORS_PER_DEV
);
309 if (IS_ERR(device
)) {
310 DBF_EVENT(3, "TCHAR:open: tape_get_device() failed\n");
311 return PTR_ERR(device
);
315 rc
= tape_open(device
);
317 filp
->private_data
= device
;
318 return nonseekable_open(inode
, filp
);
320 tape_put_device(device
);
326 * Character frontend tape device release function.
330 tapechar_release(struct inode
*inode
, struct file
*filp
)
332 struct tape_device
*device
;
334 DBF_EVENT(6, "TCHAR:release: %x\n", iminor(inode
));
335 device
= (struct tape_device
*) filp
->private_data
;
338 * If this is the rewinding tape minor then rewind. In that case we
339 * write all required tapemarks. Otherwise only one to terminate the
342 if ((iminor(inode
) & 1) != 0) {
343 if (device
->required_tapemarks
)
344 tape_std_terminate_write(device
);
345 tape_mtop(device
, MTREW
, 1);
347 if (device
->required_tapemarks
> 1) {
348 if (tape_mtop(device
, MTWEOF
, 1) == 0)
349 device
->required_tapemarks
--;
353 if (device
->char_data
.idal_buf
!= NULL
) {
354 idal_buffer_free(device
->char_data
.idal_buf
);
355 device
->char_data
.idal_buf
= NULL
;
357 tape_release(device
);
358 filp
->private_data
= tape_put_device(device
);
364 * Tape device io controls.
367 tapechar_ioctl(struct inode
*inp
, struct file
*filp
,
368 unsigned int no
, unsigned long data
)
370 struct tape_device
*device
;
373 DBF_EVENT(6, "TCHAR:ioct\n");
375 device
= (struct tape_device
*) filp
->private_data
;
377 if (no
== MTIOCTOP
) {
380 if (copy_from_user(&op
, (char __user
*) data
, sizeof(op
)) != 0)
386 * Operations that change tape position should write final
401 #ifdef CONFIG_S390_TAPE_BLOCK
402 device
->blk_data
.medium_changed
= 1;
404 if (device
->required_tapemarks
)
405 tape_std_terminate_write(device
);
409 rc
= tape_mtop(device
, op
.mt_op
, op
.mt_count
);
411 if (op
.mt_op
== MTWEOF
&& rc
== 0) {
412 if (op
.mt_count
> device
->required_tapemarks
)
413 device
->required_tapemarks
= 0;
415 device
->required_tapemarks
-= op
.mt_count
;
419 if (no
== MTIOCPOS
) {
420 /* MTIOCPOS: query the tape position. */
423 rc
= tape_mtop(device
, MTTELL
, 1);
427 if (copy_to_user((char __user
*) data
, &pos
, sizeof(pos
)) != 0)
431 if (no
== MTIOCGET
) {
432 /* MTIOCGET: query the tape drive status. */
435 memset(&get
, 0, sizeof(get
));
436 get
.mt_type
= MT_ISUNKNOWN
;
437 get
.mt_resid
= 0 /* device->devstat.rescnt */;
438 get
.mt_dsreg
= device
->tape_state
;
439 /* FIXME: mt_gstat, mt_erreg, mt_fileno */
443 get
.mt_gstat
= device
->tape_generic_status
;
445 if (device
->medium_state
== MS_LOADED
) {
446 rc
= tape_mtop(device
, MTTELL
, 1);
452 get
.mt_gstat
|= GMT_BOT(~0);
457 if (copy_to_user((char __user
*) data
, &get
, sizeof(get
)) != 0)
462 /* Try the discipline ioctl function. */
463 if (device
->discipline
->ioctl_fn
== NULL
)
465 return device
->discipline
->ioctl_fn(device
, no
, data
);
469 tapechar_compat_ioctl(struct file
*filp
, unsigned int no
, unsigned long data
)
471 struct tape_device
*device
= filp
->private_data
;
472 int rval
= -ENOIOCTLCMD
;
474 if (device
->discipline
->ioctl_fn
) {
476 rval
= device
->discipline
->ioctl_fn(device
, no
, data
);
486 * Initialize character device frontend.
493 if (alloc_chrdev_region(&dev
, 0, 256, "tape") != 0)
496 tapechar_major
= MAJOR(dev
);
497 PRINT_INFO("tape gets major %d for character devices\n", MAJOR(dev
));
508 PRINT_INFO("tape releases major %d for character devices\n",
510 unregister_chrdev_region(MKDEV(tapechar_major
, 0), 256);