blob: dbac7e54e8e069911517c99f153b9c8650475b06 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (C) 1996, 1997 Claus-Justus Heine.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; see the file COPYING. If not, write to
16 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 *
19 * This file contains the code that registers the zftape frontend
20 * to the ftape floppy tape driver for Linux
21 */
22
23#include <linux/config.h>
24#include <linux/module.h>
25#include <linux/errno.h>
26#include <linux/fs.h>
27#include <linux/kernel.h>
28#include <linux/signal.h>
29#include <linux/major.h>
30#include <linux/slab.h>
31#ifdef CONFIG_KMOD
32#include <linux/kmod.h>
33#endif
34#include <linux/fcntl.h>
35#include <linux/smp_lock.h>
36#include <linux/devfs_fs_kernel.h>
37
38#include <linux/zftape.h>
39#include <linux/init.h>
40#include <linux/device.h>
41
42#include "../zftape/zftape-init.h"
43#include "../zftape/zftape-read.h"
44#include "../zftape/zftape-write.h"
45#include "../zftape/zftape-ctl.h"
46#include "../zftape/zftape-buffers.h"
47
48MODULE_AUTHOR("(c) 1996, 1997 Claus-Justus Heine "
49 "(claus@momo.math.rwth-aachen.de)");
50MODULE_DESCRIPTION(ZFTAPE_VERSION " - "
51 "VFS interface for the Linux floppy tape driver. "
52 "Support for QIC-113 compatible volume table "
53 "and builtin compression (lzrw3 algorithm)");
54MODULE_SUPPORTED_DEVICE("char-major-27");
55MODULE_LICENSE("GPL");
56
57/* Global vars.
58 */
59struct zft_cmpr_ops *zft_cmpr_ops = NULL;
60const ftape_info *zft_status;
61
62/* Local vars.
63 */
64static unsigned long busy_flag;
65
66static sigset_t orig_sigmask;
67
68/* the interface to the kernel vfs layer
69 */
70
71/* Note about llseek():
72 *
73 * st.c and tpqic.c update fp->f_pos but don't implment llseek() and
74 * initialize the llseek component of the file_ops struct with NULL.
75 * This means that the user will get the default seek, but the tape
76 * device will not respect the new position, but happily read from the
77 * old position. Think a zftape specific llseek() function would be
78 * better, returning -ESPIPE. TODO.
79 */
80
81static int zft_open (struct inode *ino, struct file *filep);
82static int zft_close(struct inode *ino, struct file *filep);
83static int zft_ioctl(struct inode *ino, struct file *filep,
84 unsigned int command, unsigned long arg);
85static int zft_mmap(struct file *filep, struct vm_area_struct *vma);
86static ssize_t zft_read (struct file *fp, char __user *buff,
87 size_t req_len, loff_t *ppos);
88static ssize_t zft_write(struct file *fp, const char __user *buff,
89 size_t req_len, loff_t *ppos);
90
91static struct file_operations zft_cdev =
92{
93 .owner = THIS_MODULE,
94 .read = zft_read,
95 .write = zft_write,
96 .ioctl = zft_ioctl,
97 .mmap = zft_mmap,
98 .open = zft_open,
99 .release = zft_close,
100};
101
102static struct class_simple *zft_class;
103
104/* Open floppy tape device
105 */
106static int zft_open(struct inode *ino, struct file *filep)
107{
108 int result;
109 TRACE_FUN(ft_t_flow);
110
111 nonseekable_open(ino, filep);
112 TRACE(ft_t_flow, "called for minor %d", iminor(ino));
113 if ( test_and_set_bit(0,&busy_flag) ) {
114 TRACE_ABORT(-EBUSY, ft_t_warn, "failed: already busy");
115 }
116 if ((iminor(ino) & ~(ZFT_MINOR_OP_MASK | FTAPE_NO_REWIND))
117 >
118 FTAPE_SEL_D) {
119 clear_bit(0,&busy_flag);
120 TRACE_ABORT(-ENXIO, ft_t_err, "failed: invalid unit nr");
121 }
122 orig_sigmask = current->blocked;
123 sigfillset(&current->blocked);
124 result = _zft_open(iminor(ino), filep->f_flags & O_ACCMODE);
125 if (result < 0) {
126 current->blocked = orig_sigmask; /* restore mask */
127 clear_bit(0,&busy_flag);
128 TRACE_ABORT(result, ft_t_err, "_ftape_open failed");
129 } else {
130 /* Mask signals that will disturb proper operation of the
131 * program that is calling.
132 */
133 current->blocked = orig_sigmask;
134 sigaddsetmask (&current->blocked, _DO_BLOCK);
135 TRACE_EXIT 0;
136 }
137}
138
139/* Close floppy tape device
140 */
141static int zft_close(struct inode *ino, struct file *filep)
142{
143 int result;
144 TRACE_FUN(ft_t_flow);
145
146 if ( !test_bit(0,&busy_flag) || iminor(ino) != zft_unit) {
147 TRACE(ft_t_err, "failed: not busy or wrong unit");
148 TRACE_EXIT 0;
149 }
150 sigfillset(&current->blocked);
151 result = _zft_close();
152 if (result < 0) {
153 TRACE(ft_t_err, "_zft_close failed");
154 }
155 current->blocked = orig_sigmask; /* restore before open state */
156 clear_bit(0,&busy_flag);
157 TRACE_EXIT 0;
158}
159
160/* Ioctl for floppy tape device
161 */
162static int zft_ioctl(struct inode *ino, struct file *filep,
163 unsigned int command, unsigned long arg)
164{
165 int result = -EIO;
166 sigset_t old_sigmask;
167 TRACE_FUN(ft_t_flow);
168
169 if ( !test_bit(0,&busy_flag) || iminor(ino) != zft_unit || ft_failure) {
170 TRACE_ABORT(-EIO, ft_t_err,
171 "failed: not busy, failure or wrong unit");
172 }
173 old_sigmask = current->blocked; /* save mask */
174 sigfillset(&current->blocked);
175 /* This will work as long as sizeof(void *) == sizeof(long) */
176 result = _zft_ioctl(command, (void __user *) arg);
177 current->blocked = old_sigmask; /* restore mask */
178 TRACE_EXIT result;
179}
180
181/* Ioctl for floppy tape device
182 */
183static int zft_mmap(struct file *filep, struct vm_area_struct *vma)
184{
185 int result = -EIO;
186 sigset_t old_sigmask;
187 TRACE_FUN(ft_t_flow);
188
189 if ( !test_bit(0,&busy_flag) ||
190 iminor(filep->f_dentry->d_inode) != zft_unit ||
191 ft_failure)
192 {
193 TRACE_ABORT(-EIO, ft_t_err,
194 "failed: not busy, failure or wrong unit");
195 }
196 old_sigmask = current->blocked; /* save mask */
197 sigfillset(&current->blocked);
198 if ((result = ftape_mmap(vma)) >= 0) {
199#ifndef MSYNC_BUG_WAS_FIXED
200 static struct vm_operations_struct dummy = { NULL, };
201 vma->vm_ops = &dummy;
202#endif
203 }
204 current->blocked = old_sigmask; /* restore mask */
205 TRACE_EXIT result;
206}
207
208/* Read from floppy tape device
209 */
210static ssize_t zft_read(struct file *fp, char __user *buff,
211 size_t req_len, loff_t *ppos)
212{
213 int result = -EIO;
214 sigset_t old_sigmask;
215 struct inode *ino = fp->f_dentry->d_inode;
216 TRACE_FUN(ft_t_flow);
217
218 TRACE(ft_t_data_flow, "called with count: %ld", (unsigned long)req_len);
219 if (!test_bit(0,&busy_flag) || iminor(ino) != zft_unit || ft_failure) {
220 TRACE_ABORT(-EIO, ft_t_err,
221 "failed: not busy, failure or wrong unit");
222 }
223 old_sigmask = current->blocked; /* save mask */
224 sigfillset(&current->blocked);
225 result = _zft_read(buff, req_len);
226 current->blocked = old_sigmask; /* restore mask */
227 TRACE(ft_t_data_flow, "return with count: %d", result);
228 TRACE_EXIT result;
229}
230
231/* Write to tape device
232 */
233static ssize_t zft_write(struct file *fp, const char __user *buff,
234 size_t req_len, loff_t *ppos)
235{
236 int result = -EIO;
237 sigset_t old_sigmask;
238 struct inode *ino = fp->f_dentry->d_inode;
239 TRACE_FUN(ft_t_flow);
240
241 TRACE(ft_t_flow, "called with count: %ld", (unsigned long)req_len);
242 if (!test_bit(0,&busy_flag) || iminor(ino) != zft_unit || ft_failure) {
243 TRACE_ABORT(-EIO, ft_t_err,
244 "failed: not busy, failure or wrong unit");
245 }
246 old_sigmask = current->blocked; /* save mask */
247 sigfillset(&current->blocked);
248 result = _zft_write(buff, req_len);
249 current->blocked = old_sigmask; /* restore mask */
250 TRACE(ft_t_data_flow, "return with count: %d", result);
251 TRACE_EXIT result;
252}
253
254/* END OF VFS INTERFACE
255 *
256 *****************************************************************************/
257
258/* driver/module initialization
259 */
260
261/* the compression module has to call this function to hook into the zftape
262 * code
263 */
264int zft_cmpr_register(struct zft_cmpr_ops *new_ops)
265{
266 TRACE_FUN(ft_t_flow);
267
268 if (zft_cmpr_ops != NULL) {
269 TRACE_EXIT -EBUSY;
270 } else {
271 zft_cmpr_ops = new_ops;
272 TRACE_EXIT 0;
273 }
274}
275
276/* lock the zft-compressor() module.
277 */
278int zft_cmpr_lock(int try_to_load)
279{
280 if (zft_cmpr_ops == NULL) {
281#ifdef CONFIG_KMOD
282 if (try_to_load) {
283 request_module("zft-compressor");
284 if (zft_cmpr_ops == NULL) {
285 return -ENOSYS;
286 }
287 } else {
288 return -ENOSYS;
289 }
290#else
291 return -ENOSYS;
292#endif
293 }
294 (*zft_cmpr_ops->lock)();
295 return 0;
296}
297
298#ifdef CONFIG_ZFT_COMPRESSOR
299extern int zft_compressor_init(void);
300#endif
301
302/* Called by modules package when installing the driver or by kernel
303 * during the initialization phase
304 */
305int __init zft_init(void)
306{
307 int i;
308 TRACE_FUN(ft_t_flow);
309
310#ifdef MODULE
311 printk(KERN_INFO ZFTAPE_VERSION "\n");
312 if (TRACE_LEVEL >= ft_t_info) {
313 printk(
314KERN_INFO
315"(c) 1996, 1997 Claus-Justus Heine (claus@momo.math.rwth-aachen.de)\n"
316KERN_INFO
317"vfs interface for ftape floppy tape driver.\n"
318KERN_INFO
319"Support for QIC-113 compatible volume table, dynamic memory allocation\n"
320KERN_INFO
321"and builtin compression (lzrw3 algorithm).\n");
322 }
323#else /* !MODULE */
324 /* print a short no-nonsense boot message */
325 printk(KERN_INFO ZFTAPE_VERSION "\n");
326#endif /* MODULE */
327 TRACE(ft_t_info, "zft_init @ 0x%p", zft_init);
328 TRACE(ft_t_info,
329 "installing zftape VFS interface for ftape driver ...");
330 TRACE_CATCH(register_chrdev(QIC117_TAPE_MAJOR, "zft", &zft_cdev),);
331
332 zft_class = class_simple_create(THIS_MODULE, "zft");
333 for (i = 0; i < 4; i++) {
334 class_simple_device_add(zft_class, MKDEV(QIC117_TAPE_MAJOR, i), NULL, "qft%i", i);
335 devfs_mk_cdev(MKDEV(QIC117_TAPE_MAJOR, i),
336 S_IFCHR | S_IRUSR | S_IWUSR,
337 "qft%i", i);
338 class_simple_device_add(zft_class, MKDEV(QIC117_TAPE_MAJOR, i + 4), NULL, "nqft%i", i);
339 devfs_mk_cdev(MKDEV(QIC117_TAPE_MAJOR, i + 4),
340 S_IFCHR | S_IRUSR | S_IWUSR,
341 "nqft%i", i);
342 class_simple_device_add(zft_class, MKDEV(QIC117_TAPE_MAJOR, i + 16), NULL, "zqft%i", i);
343 devfs_mk_cdev(MKDEV(QIC117_TAPE_MAJOR, i + 16),
344 S_IFCHR | S_IRUSR | S_IWUSR,
345 "zqft%i", i);
346 class_simple_device_add(zft_class, MKDEV(QIC117_TAPE_MAJOR, i + 20), NULL, "nzqft%i", i);
347 devfs_mk_cdev(MKDEV(QIC117_TAPE_MAJOR, i + 20),
348 S_IFCHR | S_IRUSR | S_IWUSR,
349 "nzqft%i", i);
350 class_simple_device_add(zft_class, MKDEV(QIC117_TAPE_MAJOR, i + 32), NULL, "rawqft%i", i);
351 devfs_mk_cdev(MKDEV(QIC117_TAPE_MAJOR, i + 32),
352 S_IFCHR | S_IRUSR | S_IWUSR,
353 "rawqft%i", i);
354 class_simple_device_add(zft_class, MKDEV(QIC117_TAPE_MAJOR, i + 36), NULL, "nrawrawqft%i", i);
355 devfs_mk_cdev(MKDEV(QIC117_TAPE_MAJOR, i + 36),
356 S_IFCHR | S_IRUSR | S_IWUSR,
357 "nrawqft%i", i);
358 }
359
360#ifdef CONFIG_ZFT_COMPRESSOR
361 (void)zft_compressor_init();
362#endif
363 zft_status = ftape_get_status(); /* fetch global data of ftape
364 * hardware driver
365 */
366 TRACE_EXIT 0;
367}
368
369
370/* Called by modules package when removing the driver
371 */
372static void zft_exit(void)
373{
374 int i;
375 TRACE_FUN(ft_t_flow);
376
377 if (unregister_chrdev(QIC117_TAPE_MAJOR, "zft") != 0) {
378 TRACE(ft_t_warn, "failed");
379 } else {
380 TRACE(ft_t_info, "successful");
381 }
382 for (i = 0; i < 4; i++) {
383 devfs_remove("qft%i", i);
384 class_simple_device_remove(MKDEV(QIC117_TAPE_MAJOR, i));
385 devfs_remove("nqft%i", i);
386 class_simple_device_remove(MKDEV(QIC117_TAPE_MAJOR, i + 4));
387 devfs_remove("zqft%i", i);
388 class_simple_device_remove(MKDEV(QIC117_TAPE_MAJOR, i + 16));
389 devfs_remove("nzqft%i", i);
390 class_simple_device_remove(MKDEV(QIC117_TAPE_MAJOR, i + 20));
391 devfs_remove("rawqft%i", i);
392 class_simple_device_remove(MKDEV(QIC117_TAPE_MAJOR, i + 32));
393 devfs_remove("nrawqft%i", i);
394 class_simple_device_remove(MKDEV(QIC117_TAPE_MAJOR, i + 36));
395 }
396 class_simple_destroy(zft_class);
397 zft_uninit_mem(); /* release remaining memory, if any */
398 printk(KERN_INFO "zftape successfully unloaded.\n");
399 TRACE_EXIT;
400}
401
402module_init(zft_init);
403module_exit(zft_exit);