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 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,
40 static long tapechar_compat_ioctl(struct file *, unsigned int,
43 static const struct file_operations tape_fops =
46 .read = tapechar_read,
47 .write = tapechar_write,
48 .ioctl = tapechar_ioctl,
49 .compat_ioctl = tapechar_compat_ioctl,
50 .open = tapechar_open,
51 .release = tapechar_release,
54 static int tapechar_major = TAPECHAR_MAJOR;
57 * This function is called for every new tapedevice
60 tapechar_setup_device(struct tape_device * device)
64 sprintf(device_name, "ntibm%i", device->first_minor / 2);
65 device->nt = register_tape_dev(
67 MKDEV(tapechar_major, device->first_minor),
73 device->rt = register_tape_dev(
75 MKDEV(tapechar_major, device->first_minor + 1),
85 tapechar_cleanup_device(struct tape_device *device)
87 unregister_tape_dev(&device->cdev->dev, device->rt);
89 unregister_tape_dev(&device->cdev->dev, device->nt);
94 tapechar_check_idalbuffer(struct tape_device *device, size_t block_size)
96 struct idal_buffer *new;
98 if (device->char_data.idal_buf != NULL &&
99 device->char_data.idal_buf->size == block_size)
102 if (block_size > MAX_BLOCKSIZE) {
103 DBF_EVENT(3, "Invalid blocksize (%zd > %d)\n",
104 block_size, MAX_BLOCKSIZE);
105 PRINT_ERR("Invalid blocksize (%zd> %d)\n",
106 block_size, MAX_BLOCKSIZE);
110 /* The current idal buffer is not correct. Allocate a new one. */
111 new = idal_buffer_alloc(block_size, 0);
115 if (device->char_data.idal_buf != NULL)
116 idal_buffer_free(device->char_data.idal_buf);
118 device->char_data.idal_buf = new;
124 * Tape device read function
127 tapechar_read(struct file *filp, char __user *data, size_t count, loff_t *ppos)
129 struct tape_device *device;
130 struct tape_request *request;
134 DBF_EVENT(6, "TCHAR:read\n");
135 device = (struct tape_device *) filp->private_data;
138 * If the tape isn't terminated yet, do it now. And since we then
139 * are at the end of the tape there wouldn't be anything to read
140 * anyways. So we return immediatly.
142 if(device->required_tapemarks) {
143 return tape_std_terminate_write(device);
146 /* Find out block size to use */
147 if (device->char_data.block_size != 0) {
148 if (count < device->char_data.block_size) {
149 DBF_EVENT(3, "TCHAR:read smaller than block "
150 "size was requested\n");
153 block_size = device->char_data.block_size;
158 rc = tapechar_check_idalbuffer(device, block_size);
162 #ifdef CONFIG_S390_TAPE_BLOCK
163 /* Changes position. */
164 device->blk_data.medium_changed = 1;
167 DBF_EVENT(6, "TCHAR:nbytes: %lx\n", block_size);
168 /* Let the discipline build the ccw chain. */
169 request = device->discipline->read_block(device, block_size);
171 return PTR_ERR(request);
173 rc = tape_do_io(device, request);
175 rc = block_size - request->rescnt;
176 DBF_EVENT(6, "TCHAR:rbytes: %x\n", rc);
178 /* Copy data from idal buffer to user space. */
179 if (idal_buffer_to_user(device->char_data.idal_buf,
183 tape_free_request(request);
188 * Tape device write function
191 tapechar_write(struct file *filp, const char __user *data, size_t count, loff_t *ppos)
193 struct tape_device *device;
194 struct tape_request *request;
200 DBF_EVENT(6, "TCHAR:write\n");
201 device = (struct tape_device *) filp->private_data;
202 /* Find out block size and number of blocks */
203 if (device->char_data.block_size != 0) {
204 if (count < device->char_data.block_size) {
205 DBF_EVENT(3, "TCHAR:write smaller than block "
206 "size was requested\n");
209 block_size = device->char_data.block_size;
210 nblocks = count / block_size;
216 rc = tapechar_check_idalbuffer(device, block_size);
220 #ifdef CONFIG_S390_TAPE_BLOCK
221 /* Changes position. */
222 device->blk_data.medium_changed = 1;
225 DBF_EVENT(6,"TCHAR:nbytes: %lx\n", block_size);
226 DBF_EVENT(6, "TCHAR:nblocks: %x\n", nblocks);
227 /* Let the discipline build the ccw chain. */
228 request = device->discipline->write_block(device, block_size);
230 return PTR_ERR(request);
233 for (i = 0; i < nblocks; i++) {
234 /* Copy data from user space to idal buffer. */
235 if (idal_buffer_from_user(device->char_data.idal_buf,
240 rc = tape_do_io(device, request);
243 DBF_EVENT(6, "TCHAR:wbytes: %lx\n",
244 block_size - request->rescnt);
245 filp->f_pos += block_size - request->rescnt;
246 written += block_size - request->rescnt;
247 if (request->rescnt != 0)
251 tape_free_request(request);
254 * Ok, the device has no more space. It has NOT written
257 if (device->discipline->process_eov)
258 device->discipline->process_eov(device);
265 * After doing a write we always need two tapemarks to correctly
266 * terminate the tape (one to terminate the file, the second to
267 * flag the end of recorded data.
268 * Since process_eov positions the tape in front of the written
269 * tapemark it doesn't hurt to write two marks again.
272 device->required_tapemarks = 2;
274 return rc ? rc : written;
278 * Character frontend tape device open function.
281 tapechar_open (struct inode *inode, struct file *filp)
283 struct tape_device *device;
286 DBF_EVENT(6, "TCHAR:open: %i:%i\n",
287 imajor(filp->f_path.dentry->d_inode),
288 iminor(filp->f_path.dentry->d_inode));
290 if (imajor(filp->f_path.dentry->d_inode) != tapechar_major)
294 minor = iminor(filp->f_path.dentry->d_inode);
295 device = tape_get_device(minor / TAPE_MINORS_PER_DEV);
296 if (IS_ERR(device)) {
297 DBF_EVENT(3, "TCHAR:open: tape_get_device() failed\n");
298 rc = PTR_ERR(device);
303 rc = tape_open(device);
305 filp->private_data = device;
306 rc = nonseekable_open(inode, filp);
309 tape_put_device(device);
317 * Character frontend tape device release function.
321 tapechar_release(struct inode *inode, struct file *filp)
323 struct tape_device *device;
325 DBF_EVENT(6, "TCHAR:release: %x\n", iminor(inode));
326 device = (struct tape_device *) filp->private_data;
329 * If this is the rewinding tape minor then rewind. In that case we
330 * write all required tapemarks. Otherwise only one to terminate the
333 if ((iminor(inode) & 1) != 0) {
334 if (device->required_tapemarks)
335 tape_std_terminate_write(device);
336 tape_mtop(device, MTREW, 1);
338 if (device->required_tapemarks > 1) {
339 if (tape_mtop(device, MTWEOF, 1) == 0)
340 device->required_tapemarks--;
344 if (device->char_data.idal_buf != NULL) {
345 idal_buffer_free(device->char_data.idal_buf);
346 device->char_data.idal_buf = NULL;
348 tape_release(device);
349 filp->private_data = tape_put_device(device);
355 * Tape device io controls.
358 tapechar_ioctl(struct inode *inp, struct file *filp,
359 unsigned int no, unsigned long data)
361 struct tape_device *device;
364 DBF_EVENT(6, "TCHAR:ioct\n");
366 device = (struct tape_device *) filp->private_data;
368 if (no == MTIOCTOP) {
371 if (copy_from_user(&op, (char __user *) data, sizeof(op)) != 0)
377 * Operations that change tape position should write final
392 #ifdef CONFIG_S390_TAPE_BLOCK
393 device->blk_data.medium_changed = 1;
395 if (device->required_tapemarks)
396 tape_std_terminate_write(device);
400 rc = tape_mtop(device, op.mt_op, op.mt_count);
402 if (op.mt_op == MTWEOF && rc == 0) {
403 if (op.mt_count > device->required_tapemarks)
404 device->required_tapemarks = 0;
406 device->required_tapemarks -= op.mt_count;
410 if (no == MTIOCPOS) {
411 /* MTIOCPOS: query the tape position. */
414 rc = tape_mtop(device, MTTELL, 1);
418 if (copy_to_user((char __user *) data, &pos, sizeof(pos)) != 0)
422 if (no == MTIOCGET) {
423 /* MTIOCGET: query the tape drive status. */
426 memset(&get, 0, sizeof(get));
427 get.mt_type = MT_ISUNKNOWN;
428 get.mt_resid = 0 /* device->devstat.rescnt */;
429 get.mt_dsreg = device->tape_state;
430 /* FIXME: mt_gstat, mt_erreg, mt_fileno */
434 get.mt_gstat = device->tape_generic_status;
436 if (device->medium_state == MS_LOADED) {
437 rc = tape_mtop(device, MTTELL, 1);
443 get.mt_gstat |= GMT_BOT(~0);
448 if (copy_to_user((char __user *) data, &get, sizeof(get)) != 0)
453 /* Try the discipline ioctl function. */
454 if (device->discipline->ioctl_fn == NULL)
456 return device->discipline->ioctl_fn(device, no, data);
460 tapechar_compat_ioctl(struct file *filp, unsigned int no, unsigned long data)
462 struct tape_device *device = filp->private_data;
463 int rval = -ENOIOCTLCMD;
465 if (device->discipline->ioctl_fn) {
467 rval = device->discipline->ioctl_fn(device, no, data);
477 * Initialize character device frontend.
484 if (alloc_chrdev_region(&dev, 0, 256, "tape") != 0)
487 tapechar_major = MAJOR(dev);
488 PRINT_INFO("tape gets major %d for character devices\n", MAJOR(dev));
499 PRINT_INFO("tape releases major %d for character devices\n",
501 unregister_chrdev_region(MKDEV(tapechar_major, 0), 256);