Import 2.1.118
[davej-history.git] / drivers / char / ftape / zftape / zftape-init.c
blob1b50349391ec22585e893a72dc4ef0b76e4b5d77
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_KMOD
34 #include <linux/kmod.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 NULL, /* flush */
138 zft_close, /* release */
139 NULL, /* fsync */
142 /* Open floppy tape device
144 static int zft_open(struct inode *ino, struct file *filep)
146 int result;
147 TRACE_FUN(ft_t_flow);
149 #if LINUX_VERSION_CODE < KERNEL_VER(2,1,18)
150 if (!MOD_IN_USE) {
151 MOD_INC_USE_COUNT; /* lock module in memory */
153 #else
154 MOD_INC_USE_COUNT; /* sets MOD_VISITED and MOD_USED_ONCE,
155 * locking is done with can_unload()
157 #endif
158 TRACE(ft_t_flow, "called for minor %d", MINOR(ino->i_rdev));
159 if (busy_flag) {
160 TRACE_ABORT(-EBUSY, ft_t_warn, "failed: already busy");
162 busy_flag = 1;
163 if ((MINOR(ino->i_rdev) & ~(ZFT_MINOR_OP_MASK | FTAPE_NO_REWIND))
165 FTAPE_SEL_D) {
166 busy_flag = 0;
167 #if defined(MODULE) && LINUX_VERSION_CODE < KERNEL_VER(2,1,18)
168 if (!zft_dirty()) {
169 MOD_DEC_USE_COUNT; /* unlock module in memory */
171 #endif
172 TRACE_ABORT(-ENXIO, ft_t_err, "failed: illegal unit nr");
174 orig_sigmask = current->blocked;
175 sigfillset(&current->blocked);
176 result = _zft_open(MINOR(ino->i_rdev), filep->f_flags & O_ACCMODE);
177 if (result < 0) {
178 current->blocked = orig_sigmask; /* restore mask */
179 busy_flag = 0;
180 #if defined(MODULE) && LINUX_VERSION_CODE < KERNEL_VER(2,1,18)
181 if (!zft_dirty()) {
182 MOD_DEC_USE_COUNT; /* unlock module in memory */
184 #endif
185 TRACE_ABORT(result, ft_t_err, "_ftape_open failed");
186 } else {
187 /* Mask signals that will disturb proper operation of the
188 * program that is calling.
190 current->blocked = orig_sigmask;
191 sigaddsetmask (&current->blocked, _DO_BLOCK);
192 TRACE_EXIT 0;
196 /* Close floppy tape device
198 static int zft_close(struct inode *ino, struct file *filep)
200 int result;
201 TRACE_FUN(ft_t_flow);
203 if (!busy_flag || MINOR(ino->i_rdev) != zft_unit) {
204 TRACE(ft_t_err, "failed: not busy or wrong unit");
205 #if LINUX_VERSION_CODE >= KERNEL_VER(2,1,31)
206 TRACE_EXIT 0;
207 #else
208 TRACE_EXIT; /* keep busy_flag !(?) */
209 #endif
211 sigfillset(&current->blocked);
212 result = _zft_close();
213 if (result < 0) {
214 TRACE(ft_t_err, "_zft_close failed");
216 current->blocked = orig_sigmask; /* restore before open state */
217 busy_flag = 0;
218 #if defined(MODULE) && LINUX_VERSION_CODE < KERNEL_VER(2,1,18)
219 if (!zft_dirty()) {
220 MOD_DEC_USE_COUNT; /* unlock module in memory */
222 #endif
223 #if LINUX_VERSION_CODE >= KERNEL_VER(2,1,31)
224 TRACE_EXIT 0;
225 #else
226 TRACE_EXIT;
227 #endif
230 /* Ioctl for floppy tape device
232 static int zft_ioctl(struct inode *ino, struct file *filep,
233 unsigned int command, unsigned long arg)
235 int result = -EIO;
236 sigset_t old_sigmask;
237 TRACE_FUN(ft_t_flow);
239 if (!busy_flag || MINOR(ino->i_rdev) != zft_unit || ft_failure) {
240 TRACE_ABORT(-EIO, ft_t_err,
241 "failed: not busy, failure or wrong unit");
243 old_sigmask = current->blocked; /* save mask */
244 sigfillset(&current->blocked);
245 /* This will work as long as sizeof(void *) == sizeof(long) */
246 result = _zft_ioctl(command, (void *) arg);
247 current->blocked = old_sigmask; /* restore mask */
248 TRACE_EXIT result;
251 /* Ioctl for floppy tape device
253 #if LINUX_VERSION_CODE >= KERNEL_VER(2,1,56)
254 static int zft_mmap(struct file *filep, struct vm_area_struct *vma)
255 #else
256 static int zft_mmap(struct inode *ino,
257 struct file *filep,
258 struct vm_area_struct *vma)
259 #endif
261 int result = -EIO;
262 sigset_t old_sigmask;
263 TRACE_FUN(ft_t_flow);
265 if (!busy_flag ||
266 #if LINUX_VERSION_CODE >= KERNEL_VER(2,1,56)
267 MINOR(filep->f_dentry->d_inode->i_rdev) != zft_unit ||
268 #else
269 MINOR(ino->i_rdev) != zft_unit ||
270 #endif
271 ft_failure)
273 TRACE_ABORT(-EIO, ft_t_err,
274 "failed: not busy, failure or wrong unit");
276 old_sigmask = current->blocked; /* save mask */
277 sigfillset(&current->blocked);
278 if ((result = ftape_mmap(vma)) >= 0) {
279 #ifndef MSYNC_BUG_WAS_FIXED
280 static struct vm_operations_struct dummy = { NULL, };
281 vma->vm_ops = &dummy;
282 #endif
283 vma->vm_file = filep;
284 filep->f_count++;
286 current->blocked = old_sigmask; /* restore mask */
287 TRACE_EXIT result;
290 /* Read from floppy tape device
292 #if LINUX_VERSION_CODE >= KERNEL_VER(2,1,60)
293 static ssize_t zft_read(struct file *fp, char *buff,
294 size_t req_len, loff_t *ppos)
295 #elif LINUX_VERSION_CODE >= KERNEL_VER(2,1,0)
296 static long zft_read(struct inode *ino, struct file *fp, char *buff,
297 unsigned long req_len)
298 #else
299 static int zft_read(struct inode *ino, struct file *fp, char *buff,
300 int req_len)
301 #endif
303 int result = -EIO;
304 sigset_t old_sigmask;
305 #if LINUX_VERSION_CODE >= KERNEL_VER(2,1,60)
306 struct inode *ino = fp->f_dentry->d_inode;
307 #endif
308 TRACE_FUN(ft_t_flow);
310 TRACE(ft_t_data_flow, "called with count: %ld", (unsigned long)req_len);
311 if (!busy_flag || MINOR(ino->i_rdev) != zft_unit || ft_failure) {
312 TRACE_ABORT(-EIO, ft_t_err,
313 "failed: not busy, failure or wrong unit");
315 old_sigmask = current->blocked; /* save mask */
316 sigfillset(&current->blocked);
317 result = _zft_read(buff, req_len);
318 current->blocked = old_sigmask; /* restore mask */
319 TRACE(ft_t_data_flow, "return with count: %d", result);
320 TRACE_EXIT result;
323 /* Write to tape device
325 #if LINUX_VERSION_CODE >= KERNEL_VER(2,1,60)
326 static ssize_t zft_write(struct file *fp, const char *buff,
327 size_t req_len, loff_t *ppos)
328 #elif LINUX_VERSION_CODE >= KERNEL_VER(2,1,0)
329 static long zft_write(struct inode *ino, struct file *fp, const char *buff,
330 unsigned long req_len)
331 #elif LINUX_VERSION_CODE >= KERNEL_VER(1,3,0)
332 static int zft_write(struct inode *ino, struct file *fp, const char *buff,
333 int req_len)
334 #else
335 static int zft_write(struct inode *ino, struct file *fp, char *buff,
336 int req_len)
337 #endif
339 int result = -EIO;
340 sigset_t old_sigmask;
341 #if LINUX_VERSION_CODE >= KERNEL_VER(2,1,60)
342 struct inode *ino = fp->f_dentry->d_inode;
343 #endif
344 TRACE_FUN(ft_t_flow);
346 TRACE(ft_t_flow, "called with count: %ld", (unsigned long)req_len);
347 if (!busy_flag || MINOR(ino->i_rdev) != zft_unit || ft_failure) {
348 TRACE_ABORT(-EIO, ft_t_err,
349 "failed: not busy, failure or wrong unit");
351 old_sigmask = current->blocked; /* save mask */
352 sigfillset(&current->blocked);
353 result = _zft_write(buff, req_len);
354 current->blocked = old_sigmask; /* restore mask */
355 TRACE(ft_t_data_flow, "return with count: %d", result);
356 TRACE_EXIT result;
359 /* END OF VFS INTERFACE
361 *****************************************************************************/
363 /* driver/module initialization
366 /* the compression module has to call this function to hook into the zftape
367 * code
369 int zft_cmpr_register(struct zft_cmpr_ops *new_ops)
371 TRACE_FUN(ft_t_flow);
373 if (zft_cmpr_ops != NULL) {
374 TRACE_EXIT -EBUSY;
375 } else {
376 zft_cmpr_ops = new_ops;
377 TRACE_EXIT 0;
381 struct zft_cmpr_ops *zft_cmpr_unregister(void)
383 struct zft_cmpr_ops *old_ops = zft_cmpr_ops;
384 TRACE_FUN(ft_t_flow);
386 zft_cmpr_ops = NULL;
387 TRACE_EXIT old_ops;
390 /* lock the zft-compressor() module.
392 int zft_cmpr_lock(int try_to_load)
394 if (zft_cmpr_ops == NULL) {
395 #ifdef CONFIG_KMOD
396 if (try_to_load) {
397 request_module("zft-compressor");
398 if (zft_cmpr_ops == NULL) {
399 return -ENOSYS;
401 } else {
402 return -ENOSYS;
404 #else
405 return -ENOSYS;
406 #endif
408 (*zft_cmpr_ops->lock)();
409 return 0;
412 #ifdef CONFIG_ZFT_COMPRESSOR
413 extern int zft_compressor_init(void);
414 #endif
416 /* Called by modules package when installing the driver or by kernel
417 * during the initialization phase
419 __initfunc(int zft_init(void))
421 TRACE_FUN(ft_t_flow);
423 #ifdef MODULE
424 printk(KERN_INFO ZFTAPE_VERSION "\n");
425 if (TRACE_LEVEL >= ft_t_info) {
426 printk(
427 KERN_INFO
428 "(c) 1996, 1997 Claus-Justus Heine (claus@momo.math.rwth-aachen.de)\n"
429 KERN_INFO
430 "vfs interface for ftape floppy tape driver.\n"
431 KERN_INFO
432 "Support for QIC-113 compatible volume table, dynamic memory allocation\n"
433 KERN_INFO
434 "and builtin compression (lzrw3 algorithm).\n"
435 KERN_INFO
436 "Compiled for Linux version %s"
437 #ifdef MODVERSIONS
438 " with versioned symbols"
439 #endif
440 "\n", UTS_RELEASE);
442 #else /* !MODULE */
443 /* print a short no-nonsense boot message */
444 printk(KERN_INFO ZFTAPE_VERSION " for Linux " UTS_RELEASE "\n");
445 #endif /* MODULE */
446 TRACE(ft_t_info, "zft_init @ 0x%p", zft_init);
447 TRACE(ft_t_info,
448 "installing zftape VFS interface for ftape driver ...");
449 TRACE_CATCH(register_chrdev(QIC117_TAPE_MAJOR, "zft", &zft_cdev),);
450 #if LINUX_VERSION_CODE >= KERNEL_VER(1,2,0)
451 # if LINUX_VERSION_CODE < KERNEL_VER(2,1,18)
452 register_symtab(&zft_symbol_table); /* add global zftape symbols */
453 # endif
454 #endif
455 #ifdef CONFIG_ZFT_COMPRESSOR
456 (void)zft_compressor_init();
457 #endif
458 zft_status = ftape_get_status(); /* fetch global data of ftape
459 * hardware driver
461 TRACE_EXIT 0;
465 #ifdef MODULE
466 #if LINUX_VERSION_CODE <= KERNEL_VER(1,2,13) && defined(MODULE)
467 char kernel_version[] = UTS_RELEASE;
468 #endif
469 #if LINUX_VERSION_CODE >= KERNEL_VER(2,1,18)
470 /* Called by modules package before trying to unload the module
472 static int can_unload(void)
474 return (zft_dirty() || busy_flag) ? -EBUSY : 0;
476 #endif
477 /* Called by modules package when installing the driver
479 int init_module(void)
481 #if LINUX_VERSION_CODE >= KERNEL_VER(2,1,18)
482 if (!mod_member_present(&__this_module, can_unload)) {
483 return -EBUSY;
485 __this_module.can_unload = can_unload;
486 #endif
487 return zft_init();
490 /* Called by modules package when removing the driver
492 void cleanup_module(void)
494 TRACE_FUN(ft_t_flow);
496 if (unregister_chrdev(QIC117_TAPE_MAJOR, "zft") != 0) {
497 TRACE(ft_t_warn, "failed");
498 } else {
499 TRACE(ft_t_info, "successful");
501 zft_uninit_mem(); /* release remaining memory, if any */
502 printk(KERN_INFO "zftape successfully unloaded.\n");
503 TRACE_EXIT;
506 #endif /* MODULE */