2 * character device frontend for tape device driver
4 * S390 and zSeries version
5 * Copyright IBM Corp. 2001, 2006
6 * Author(s): Carsten Otte <cotte@de.ibm.com>
7 * Michael Holzheu <holzheu@de.ibm.com>
8 * Tuan Ngo-Anh <ngoanh@de.ibm.com>
9 * Martin Schwidefsky <schwidefsky@de.ibm.com>
12 #define KMSG_COMPONENT "tape"
13 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
15 #include <linux/module.h>
16 #include <linux/types.h>
17 #include <linux/proc_fs.h>
18 #include <linux/mtio.h>
19 #include <linux/compat.h>
21 #include <asm/uaccess.h>
23 #define TAPE_DBF_AREA tape_core_dbf
27 #include "tape_class.h"
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 long tapechar_ioctl(struct file *, unsigned int, unsigned long);
40 static long tapechar_compat_ioctl(struct file *, unsigned int, unsigned long);
43 static const struct file_operations tape_fops =
46 .read = tapechar_read,
47 .write = tapechar_write,
48 .unlocked_ioctl = tapechar_ioctl,
50 .compat_ioctl = tapechar_compat_ioctl,
52 .open = tapechar_open,
53 .release = tapechar_release,
57 static int tapechar_major = TAPECHAR_MAJOR;
60 * This function is called for every new tapedevice
63 tapechar_setup_device(struct tape_device * device)
67 sprintf(device_name, "ntibm%i", device->first_minor / 2);
68 device->nt = register_tape_dev(
70 MKDEV(tapechar_major, device->first_minor),
76 device->rt = register_tape_dev(
78 MKDEV(tapechar_major, device->first_minor + 1),
88 tapechar_cleanup_device(struct tape_device *device)
90 unregister_tape_dev(&device->cdev->dev, device->rt);
92 unregister_tape_dev(&device->cdev->dev, device->nt);
97 tapechar_check_idalbuffer(struct tape_device *device, size_t block_size)
99 struct idal_buffer *new;
101 if (device->char_data.idal_buf != NULL &&
102 device->char_data.idal_buf->size == block_size)
105 if (block_size > MAX_BLOCKSIZE) {
106 DBF_EVENT(3, "Invalid blocksize (%zd > %d)\n",
107 block_size, MAX_BLOCKSIZE);
111 /* The current idal buffer is not correct. Allocate a new one. */
112 new = idal_buffer_alloc(block_size, 0);
116 if (device->char_data.idal_buf != NULL)
117 idal_buffer_free(device->char_data.idal_buf);
119 device->char_data.idal_buf = new;
125 * Tape device read function
128 tapechar_read(struct file *filp, char __user *data, size_t count, loff_t *ppos)
130 struct tape_device *device;
131 struct tape_request *request;
135 DBF_EVENT(6, "TCHAR:read\n");
136 device = (struct tape_device *) filp->private_data;
139 * If the tape isn't terminated yet, do it now. And since we then
140 * are at the end of the tape there wouldn't be anything to read
141 * anyways. So we return immediately.
143 if(device->required_tapemarks) {
144 return tape_std_terminate_write(device);
147 /* Find out block size to use */
148 if (device->char_data.block_size != 0) {
149 if (count < device->char_data.block_size) {
150 DBF_EVENT(3, "TCHAR:read smaller than block "
151 "size was requested\n");
154 block_size = device->char_data.block_size;
159 rc = tapechar_check_idalbuffer(device, block_size);
163 DBF_EVENT(6, "TCHAR:nbytes: %lx\n", block_size);
164 /* Let the discipline build the ccw chain. */
165 request = device->discipline->read_block(device, block_size);
167 return PTR_ERR(request);
169 rc = tape_do_io(device, request);
171 rc = block_size - request->rescnt;
172 DBF_EVENT(6, "TCHAR:rbytes: %x\n", rc);
173 /* Copy data from idal buffer to user space. */
174 if (idal_buffer_to_user(device->char_data.idal_buf,
178 tape_free_request(request);
183 * Tape device write function
186 tapechar_write(struct file *filp, const char __user *data, size_t count, loff_t *ppos)
188 struct tape_device *device;
189 struct tape_request *request;
195 DBF_EVENT(6, "TCHAR:write\n");
196 device = (struct tape_device *) filp->private_data;
197 /* Find out block size and number of blocks */
198 if (device->char_data.block_size != 0) {
199 if (count < device->char_data.block_size) {
200 DBF_EVENT(3, "TCHAR:write smaller than block "
201 "size was requested\n");
204 block_size = device->char_data.block_size;
205 nblocks = count / block_size;
211 rc = tapechar_check_idalbuffer(device, block_size);
215 DBF_EVENT(6,"TCHAR:nbytes: %lx\n", block_size);
216 DBF_EVENT(6, "TCHAR:nblocks: %x\n", nblocks);
217 /* Let the discipline build the ccw chain. */
218 request = device->discipline->write_block(device, block_size);
220 return PTR_ERR(request);
223 for (i = 0; i < nblocks; i++) {
224 /* Copy data from user space to idal buffer. */
225 if (idal_buffer_from_user(device->char_data.idal_buf,
230 rc = tape_do_io(device, request);
233 DBF_EVENT(6, "TCHAR:wbytes: %lx\n",
234 block_size - request->rescnt);
235 written += block_size - request->rescnt;
236 if (request->rescnt != 0)
240 tape_free_request(request);
243 * Ok, the device has no more space. It has NOT written
246 if (device->discipline->process_eov)
247 device->discipline->process_eov(device);
254 * After doing a write we always need two tapemarks to correctly
255 * terminate the tape (one to terminate the file, the second to
256 * flag the end of recorded data.
257 * Since process_eov positions the tape in front of the written
258 * tapemark it doesn't hurt to write two marks again.
261 device->required_tapemarks = 2;
263 return rc ? rc : written;
267 * Character frontend tape device open function.
270 tapechar_open (struct inode *inode, struct file *filp)
272 struct tape_device *device;
275 DBF_EVENT(6, "TCHAR:open: %i:%i\n",
276 imajor(file_inode(filp)),
277 iminor(file_inode(filp)));
279 if (imajor(file_inode(filp)) != tapechar_major)
282 minor = iminor(file_inode(filp));
283 device = tape_find_device(minor / TAPE_MINORS_PER_DEV);
284 if (IS_ERR(device)) {
285 DBF_EVENT(3, "TCHAR:open: tape_find_device() failed\n");
286 return PTR_ERR(device);
289 rc = tape_open(device);
291 filp->private_data = device;
292 nonseekable_open(inode, filp);
294 tape_put_device(device);
300 * Character frontend tape device release function.
304 tapechar_release(struct inode *inode, struct file *filp)
306 struct tape_device *device;
308 DBF_EVENT(6, "TCHAR:release: %x\n", iminor(inode));
309 device = (struct tape_device *) filp->private_data;
312 * If this is the rewinding tape minor then rewind. In that case we
313 * write all required tapemarks. Otherwise only one to terminate the
316 if ((iminor(inode) & 1) != 0) {
317 if (device->required_tapemarks)
318 tape_std_terminate_write(device);
319 tape_mtop(device, MTREW, 1);
321 if (device->required_tapemarks > 1) {
322 if (tape_mtop(device, MTWEOF, 1) == 0)
323 device->required_tapemarks--;
327 if (device->char_data.idal_buf != NULL) {
328 idal_buffer_free(device->char_data.idal_buf);
329 device->char_data.idal_buf = NULL;
331 tape_release(device);
332 filp->private_data = NULL;
333 tape_put_device(device);
339 * Tape device io controls.
342 __tapechar_ioctl(struct tape_device *device,
343 unsigned int no, unsigned long data)
347 if (no == MTIOCTOP) {
350 if (copy_from_user(&op, (char __user *) data, sizeof(op)) != 0)
356 * Operations that change tape position should write final
371 if (device->required_tapemarks)
372 tape_std_terminate_write(device);
376 rc = tape_mtop(device, op.mt_op, op.mt_count);
378 if (op.mt_op == MTWEOF && rc == 0) {
379 if (op.mt_count > device->required_tapemarks)
380 device->required_tapemarks = 0;
382 device->required_tapemarks -= op.mt_count;
386 if (no == MTIOCPOS) {
387 /* MTIOCPOS: query the tape position. */
390 rc = tape_mtop(device, MTTELL, 1);
394 if (copy_to_user((char __user *) data, &pos, sizeof(pos)) != 0)
398 if (no == MTIOCGET) {
399 /* MTIOCGET: query the tape drive status. */
402 memset(&get, 0, sizeof(get));
403 get.mt_type = MT_ISUNKNOWN;
404 get.mt_resid = 0 /* device->devstat.rescnt */;
405 get.mt_dsreg = device->tape_state;
406 /* FIXME: mt_gstat, mt_erreg, mt_fileno */
410 get.mt_gstat = device->tape_generic_status;
412 if (device->medium_state == MS_LOADED) {
413 rc = tape_mtop(device, MTTELL, 1);
419 get.mt_gstat |= GMT_BOT(~0);
424 if (copy_to_user((char __user *) data, &get, sizeof(get)) != 0)
429 /* Try the discipline ioctl function. */
430 if (device->discipline->ioctl_fn == NULL)
432 return device->discipline->ioctl_fn(device, no, data);
436 tapechar_ioctl(struct file *filp, unsigned int no, unsigned long data)
438 struct tape_device *device;
441 DBF_EVENT(6, "TCHAR:ioct\n");
443 device = (struct tape_device *) filp->private_data;
444 mutex_lock(&device->mutex);
445 rc = __tapechar_ioctl(device, no, data);
446 mutex_unlock(&device->mutex);
452 tapechar_compat_ioctl(struct file *filp, unsigned int no, unsigned long data)
454 struct tape_device *device = filp->private_data;
455 int rval = -ENOIOCTLCMD;
458 /* The 'arg' argument of any ioctl function may only be used for
459 * pointers because of the compat pointer conversion.
460 * Consider this when adding new ioctls.
462 argp = (unsigned long) compat_ptr(data);
463 if (device->discipline->ioctl_fn) {
464 mutex_lock(&device->mutex);
465 rval = device->discipline->ioctl_fn(device, no, argp);
466 mutex_unlock(&device->mutex);
473 #endif /* CONFIG_COMPAT */
476 * Initialize character device frontend.
483 if (alloc_chrdev_region(&dev, 0, 256, "tape") != 0)
486 tapechar_major = MAJOR(dev);
497 unregister_chrdev_region(MKDEV(tapechar_major, 0), 256);