Import 2.1.81
[davej-history.git] / drivers / char / ftape / zftape / zftape-init.c
blob0cb9f018f9a30ba87fe2e3f6df42920124b1001a
1 /*
2 * Copyright (C) 1996, 1997 Claus-Justus Heine.
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.
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.
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.
19 * This file contains the code that registers the zftape frontend
20 * to the ftape floppy tape driver for Linux
23 #include <linux/config.h>
24 #include <linux/module.h>
25 #include <linux/errno.h>
26 #include <linux/version.h>
27 #include <linux/fs.h>
28 #include <asm/segment.h>
29 #include <linux/kernel.h>
30 #include <linux/signal.h>
31 #include <linux/major.h>
32 #include <linux/malloc.h>
33 #ifdef CONFIG_KERNELD
34 #include <linux/kerneld.h>
35 #endif
36 #include <linux/fcntl.h>
37 #include <linux/wrapper.h>
39 #include <linux/zftape.h>
40 #if LINUX_VERSION_CODE >=KERNEL_VER(2,1,16)
41 #include <linux/init.h>
42 #else
43 #define __initdata
44 #define __initfunc(__arg) __arg
45 #endif
47 #include "../zftape/zftape-init.h"
48 #include "../zftape/zftape-read.h"
49 #include "../zftape/zftape-write.h"
50 #include "../zftape/zftape-ctl.h"
51 #include "../zftape/zftape-buffers.h"
52 #include "../zftape/zftape_syms.h"
54 char zft_src[] __initdata = "$Source: /homes/cvs/ftape-stacked/ftape/zftape/zftape-init.c,v $";
55 char zft_rev[] __initdata = "$Revision: 1.8 $";
56 char zft_dat[] __initdata = "$Date: 1997/11/06 00:48:56 $";
58 #if LINUX_VERSION_CODE >= KERNEL_VER(2,1,18)
59 MODULE_AUTHOR("(c) 1996, 1997 Claus-Justus Heine "
60 "(claus@momo.math.rwth-aachen.de)");
61 MODULE_DESCRIPTION(ZFTAPE_VERSION " - "
62 "VFS interface for the Linux floppy tape driver. "
63 "Support for QIC-113 compatible volume table "
64 "and builtin compression (lzrw3 algorithm)");
65 MODULE_SUPPORTED_DEVICE("char-major-27");
66 #endif
68 /* Global vars.
70 struct zft_cmpr_ops *zft_cmpr_ops = NULL;
71 const ftape_info *zft_status;
73 /* Local vars.
75 static int busy_flag = 0;
76 static sigset_t orig_sigmask;
78 /* the interface to the kernel vfs layer
81 /* Note about llseek():
83 * st.c and tpqic.c update fp->f_pos but don't implment llseek() and
84 * initialize the llseek component of the file_ops struct with NULL.
85 * This means that the user will get the default seek, but the tape
86 * device will not respect the new position, but happily read from the
87 * old position. Think a zftape specific llseek() function would be
88 * better, returning -ESPIPE. TODO.
91 static int zft_open (struct inode *ino, struct file *filep);
92 #if LINUX_VERSION_CODE >= KERNEL_VER(2,1,31)
93 static int zft_close(struct inode *ino, struct file *filep);
94 #else
95 static void zft_close(struct inode *ino, struct file *filep);
96 #endif
97 static int zft_ioctl(struct inode *ino, struct file *filep,
98 unsigned int command, unsigned long arg);
99 #if LINUX_VERSION_CODE >= KERNEL_VER(2,1,56)
100 static int zft_mmap(struct file *filep, struct vm_area_struct *vma);
101 #else
102 static int zft_mmap(struct inode *ino, struct file *filep,
103 struct vm_area_struct *vma);
104 #endif
105 #if LINUX_VERSION_CODE >= KERNEL_VER(2,1,60)
106 static ssize_t zft_read (struct file *fp, char *buff,
107 size_t req_len, loff_t *ppos);
108 static ssize_t zft_write(struct file *fp, const char *buff,
109 size_t req_len, loff_t *ppos);
110 #elif LINUX_VERSION_CODE >= KERNEL_VER(2,1,0)
111 static long zft_read (struct inode *ino, struct file *fp, char *buff,
112 unsigned long req_len);
113 static long zft_write(struct inode *ino, struct file *fp, const char *buff,
114 unsigned long req_len);
115 #else
116 static int zft_read (struct inode *ino, struct file *fp, char *buff,
117 int req_len);
118 #if LINUX_VERSION_CODE >= KERNEL_VER(1,3,0)
119 static int zft_write(struct inode *ino, struct file *fp, const char *buff,
120 int req_len);
121 #else
122 static int zft_write(struct inode *ino, struct file *fp, char *buff,
123 int req_len);
124 #endif
125 #endif
127 static struct file_operations zft_cdev =
129 NULL, /* llseek */
130 zft_read, /* read */
131 zft_write, /* write */
132 NULL, /* readdir */
133 NULL, /* select */
134 zft_ioctl, /* ioctl */
135 zft_mmap, /* mmap */
136 zft_open, /* open */
137 zft_close, /* release */
138 NULL, /* fsync */
141 /* Open floppy tape device
143 static int zft_open(struct inode *ino, struct file *filep)
145 int result;
146 TRACE_FUN(ft_t_flow);
148 #if LINUX_VERSION_CODE < KERNEL_VER(2,1,18)
149 if (!MOD_IN_USE) {
150 MOD_INC_USE_COUNT; /* lock module in memory */
152 #else
153 MOD_INC_USE_COUNT; /* sets MOD_VISITED and MOD_USED_ONCE,
154 * locking is done with can_unload()
156 #endif
157 TRACE(ft_t_flow, "called for minor %d", MINOR(ino->i_rdev));
158 if (busy_flag) {
159 TRACE_ABORT(-EBUSY, ft_t_warn, "failed: already busy");
161 busy_flag = 1;
162 if ((MINOR(ino->i_rdev) & ~(ZFT_MINOR_OP_MASK | FTAPE_NO_REWIND))
164 FTAPE_SEL_D) {
165 busy_flag = 0;
166 #if defined(MODULE) && LINUX_VERSION_CODE < KERNEL_VER(2,1,18)
167 if (!zft_dirty()) {
168 MOD_DEC_USE_COUNT; /* unlock module in memory */
170 #endif
171 TRACE_ABORT(-ENXIO, ft_t_err, "failed: illegal unit nr");
173 orig_sigmask = current->blocked;
174 sigfillset(&current->blocked);
175 result = _zft_open(MINOR(ino->i_rdev), filep->f_flags & O_ACCMODE);
176 if (result < 0) {
177 current->blocked = orig_sigmask; /* restore mask */
178 busy_flag = 0;
179 #if defined(MODULE) && LINUX_VERSION_CODE < KERNEL_VER(2,1,18)
180 if (!zft_dirty()) {
181 MOD_DEC_USE_COUNT; /* unlock module in memory */
183 #endif
184 TRACE_ABORT(result, ft_t_err, "_ftape_open failed");
185 } else {
186 /* Mask signals that will disturb proper operation of the
187 * program that is calling.
189 current->blocked = orig_sigmask;
190 sigaddsetmask (&current->blocked, _DO_BLOCK);
191 TRACE_EXIT 0;
195 /* Close floppy tape device
197 static int zft_close(struct inode *ino, struct file *filep)
199 int result;
200 TRACE_FUN(ft_t_flow);
202 if (!busy_flag || MINOR(ino->i_rdev) != zft_unit) {
203 TRACE(ft_t_err, "failed: not busy or wrong unit");
204 #if LINUX_VERSION_CODE >= KERNEL_VER(2,1,31)
205 TRACE_EXIT 0;
206 #else
207 TRACE_EXIT; /* keep busy_flag !(?) */
208 #endif
210 sigfillset(&current->blocked);
211 result = _zft_close();
212 if (result < 0) {
213 TRACE(ft_t_err, "_zft_close failed");
215 current->blocked = orig_sigmask; /* restore before open state */
216 busy_flag = 0;
217 #if defined(MODULE) && LINUX_VERSION_CODE < KERNEL_VER(2,1,18)
218 if (!zft_dirty()) {
219 MOD_DEC_USE_COUNT; /* unlock module in memory */
221 #endif
222 #if LINUX_VERSION_CODE >= KERNEL_VER(2,1,31)
223 TRACE_EXIT 0;
224 #else
225 TRACE_EXIT;
226 #endif
229 /* Ioctl for floppy tape device
231 static int zft_ioctl(struct inode *ino, struct file *filep,
232 unsigned int command, unsigned long arg)
234 int result = -EIO;
235 sigset_t old_sigmask;
236 TRACE_FUN(ft_t_flow);
238 if (!busy_flag || MINOR(ino->i_rdev) != zft_unit || ft_failure) {
239 TRACE_ABORT(-EIO, ft_t_err,
240 "failed: not busy, failure or wrong unit");
242 old_sigmask = current->blocked; /* save mask */
243 sigfillset(&current->blocked);
244 /* This will work as long as sizeof(void *) == sizeof(long) */
245 result = _zft_ioctl(command, (void *) arg);
246 current->blocked = old_sigmask; /* restore mask */
247 TRACE_EXIT result;
250 /* Ioctl for floppy tape device
252 #if LINUX_VERSION_CODE >= KERNEL_VER(2,1,56)
253 static int zft_mmap(struct file *filep, struct vm_area_struct *vma)
254 #else
255 static int zft_mmap(struct inode *ino,
256 struct file *filep,
257 struct vm_area_struct *vma)
258 #endif
260 int result = -EIO;
261 sigset_t old_sigmask;
262 TRACE_FUN(ft_t_flow);
264 if (!busy_flag ||
265 #if LINUX_VERSION_CODE >= KERNEL_VER(2,1,56)
266 MINOR(filep->f_dentry->d_inode->i_rdev) != zft_unit ||
267 #else
268 MINOR(ino->i_rdev) != zft_unit ||
269 #endif
270 ft_failure)
272 TRACE_ABORT(-EIO, ft_t_err,
273 "failed: not busy, failure or wrong unit");
275 old_sigmask = current->blocked; /* save mask */
276 sigfillset(&current->blocked);
277 if ((result = ftape_mmap(vma)) >= 0) {
278 #ifndef MSYNC_BUG_WAS_FIXED
279 static struct vm_operations_struct dummy = { NULL, };
280 vma->vm_ops = &dummy;
281 #endif
282 #if LINUX_VERSION_CODE >= KERNEL_VER(2,1,45)
283 vma->vm_dentry = dget(filep->f_dentry);
284 #else
285 vma_set_inode (vma, ino);
286 inode_inc_count (ino);
287 #endif
289 current->blocked = old_sigmask; /* restore mask */
290 TRACE_EXIT result;
293 /* Read from floppy tape device
295 #if LINUX_VERSION_CODE >= KERNEL_VER(2,1,60)
296 static ssize_t zft_read(struct file *fp, char *buff,
297 size_t req_len, loff_t *ppos)
298 #elif LINUX_VERSION_CODE >= KERNEL_VER(2,1,0)
299 static long zft_read(struct inode *ino, struct file *fp, char *buff,
300 unsigned long req_len)
301 #else
302 static int zft_read(struct inode *ino, struct file *fp, char *buff,
303 int req_len)
304 #endif
306 int result = -EIO;
307 sigset_t old_sigmask;
308 #if LINUX_VERSION_CODE >= KERNEL_VER(2,1,60)
309 struct inode *ino = fp->f_dentry->d_inode;
310 #endif
311 TRACE_FUN(ft_t_flow);
313 TRACE(ft_t_data_flow, "called with count: %ld", (unsigned long)req_len);
314 if (!busy_flag || MINOR(ino->i_rdev) != zft_unit || ft_failure) {
315 TRACE_ABORT(-EIO, ft_t_err,
316 "failed: not busy, failure or wrong unit");
318 old_sigmask = current->blocked; /* save mask */
319 sigfillset(&current->blocked);
320 result = _zft_read(buff, req_len);
321 current->blocked = old_sigmask; /* restore mask */
322 TRACE(ft_t_data_flow, "return with count: %d", result);
323 TRACE_EXIT result;
326 /* Write to tape device
328 #if LINUX_VERSION_CODE >= KERNEL_VER(2,1,60)
329 static ssize_t zft_write(struct file *fp, const char *buff,
330 size_t req_len, loff_t *ppos)
331 #elif LINUX_VERSION_CODE >= KERNEL_VER(2,1,0)
332 static long zft_write(struct inode *ino, struct file *fp, const char *buff,
333 unsigned long req_len)
334 #elif LINUX_VERSION_CODE >= KERNEL_VER(1,3,0)
335 static int zft_write(struct inode *ino, struct file *fp, const char *buff,
336 int req_len)
337 #else
338 static int zft_write(struct inode *ino, struct file *fp, char *buff,
339 int req_len)
340 #endif
342 int result = -EIO;
343 sigset_t old_sigmask;
344 #if LINUX_VERSION_CODE >= KERNEL_VER(2,1,60)
345 struct inode *ino = fp->f_dentry->d_inode;
346 #endif
347 TRACE_FUN(ft_t_flow);
349 TRACE(ft_t_flow, "called with count: %ld", (unsigned long)req_len);
350 if (!busy_flag || MINOR(ino->i_rdev) != zft_unit || ft_failure) {
351 TRACE_ABORT(-EIO, ft_t_err,
352 "failed: not busy, failure or wrong unit");
354 old_sigmask = current->blocked; /* save mask */
355 sigfillset(&current->blocked);
356 result = _zft_write(buff, req_len);
357 current->blocked = old_sigmask; /* restore mask */
358 TRACE(ft_t_data_flow, "return with count: %d", result);
359 TRACE_EXIT result;
362 /* END OF VFS INTERFACE
364 *****************************************************************************/
366 /* driver/module initialization
369 /* the compression module has to call this function to hook into the zftape
370 * code
372 int zft_cmpr_register(struct zft_cmpr_ops *new_ops)
374 TRACE_FUN(ft_t_flow);
376 if (zft_cmpr_ops != NULL) {
377 TRACE_EXIT -EBUSY;
378 } else {
379 zft_cmpr_ops = new_ops;
380 TRACE_EXIT 0;
384 struct zft_cmpr_ops *zft_cmpr_unregister(void)
386 struct zft_cmpr_ops *old_ops = zft_cmpr_ops;
387 TRACE_FUN(ft_t_flow);
389 zft_cmpr_ops = NULL;
390 TRACE_EXIT old_ops;
393 /* lock the zft-compressor() module.
395 int zft_cmpr_lock(int try_to_load)
397 if (zft_cmpr_ops == NULL) {
398 #ifdef CONFIG_KERNELD
399 if (try_to_load) {
400 request_module("zft-compressor");
401 if (zft_cmpr_ops == NULL) {
402 return -ENOSYS;
404 } else {
405 return -ENOSYS;
407 #else
408 return -ENOSYS;
409 #endif
411 (*zft_cmpr_ops->lock)();
412 return 0;
415 #ifdef CONFIG_ZFT_COMPRESSOR
416 extern int zft_compressor_init(void);
417 #endif
419 /* Called by modules package when installing the driver or by kernel
420 * during the initialization phase
422 __initfunc(int zft_init(void))
424 TRACE_FUN(ft_t_flow);
426 #ifdef MODULE
427 printk(KERN_INFO ZFTAPE_VERSION "\n");
428 if (TRACE_LEVEL >= ft_t_info) {
429 printk(
430 KERN_INFO
431 "(c) 1996, 1997 Claus-Justus Heine (claus@momo.math.rwth-aachen.de)\n"
432 KERN_INFO
433 "vfs interface for ftape floppy tape driver.\n"
434 KERN_INFO
435 "Support for QIC-113 compatible volume table, dynamic memory allocation\n"
436 KERN_INFO
437 "and builtin compression (lzrw3 algorithm).\n"
438 KERN_INFO
439 "Compiled for Linux version %s"
440 #ifdef MODVERSIONS
441 " with versioned symbols"
442 #endif
443 "\n", UTS_RELEASE);
445 #else /* !MODULE */
446 /* print a short no-nonsense boot message */
447 printk(KERN_INFO ZFTAPE_VERSION " for Linux " UTS_RELEASE "\n");
448 #endif /* MODULE */
449 TRACE(ft_t_info, "zft_init @ 0x%p", zft_init);
450 TRACE(ft_t_info,
451 "installing zftape VFS interface for ftape driver ...");
452 TRACE_CATCH(register_chrdev(QIC117_TAPE_MAJOR, "zft", &zft_cdev),);
453 #if LINUX_VERSION_CODE >= KERNEL_VER(1,2,0)
454 # if LINUX_VERSION_CODE < KERNEL_VER(2,1,18)
455 register_symtab(&zft_symbol_table); /* add global zftape symbols */
456 # endif
457 #endif
458 #ifdef CONFIG_ZFT_COMPRESSOR
459 (void)zft_compressor_init();
460 #endif
461 zft_status = ftape_get_status(); /* fetch global data of ftape
462 * hardware driver
464 TRACE_EXIT 0;
468 #ifdef MODULE
469 #if LINUX_VERSION_CODE <= KERNEL_VER(1,2,13) && defined(MODULE)
470 char kernel_version[] = UTS_RELEASE;
471 #endif
472 #if LINUX_VERSION_CODE >= KERNEL_VER(2,1,18)
473 /* Called by modules package before trying to unload the module
475 static int can_unload(void)
477 return (zft_dirty() || busy_flag) ? -EBUSY : 0;
479 #endif
480 /* Called by modules package when installing the driver
482 int init_module(void)
484 #if LINUX_VERSION_CODE >= KERNEL_VER(2,1,18)
485 if (!mod_member_present(&__this_module, can_unload)) {
486 return -EBUSY;
488 __this_module.can_unload = can_unload;
489 #endif
490 return zft_init();
493 /* Called by modules package when removing the driver
495 void cleanup_module(void)
497 TRACE_FUN(ft_t_flow);
499 if (unregister_chrdev(QIC117_TAPE_MAJOR, "zft") != 0) {
500 TRACE(ft_t_warn, "failed");
501 } else {
502 TRACE(ft_t_info, "successful");
504 zft_uninit_mem(); /* release remaining memory, if any */
505 printk(KERN_INFO "zftape successfully unloaded.\n");
506 TRACE_EXIT;
509 #endif /* MODULE */