Import 2.3.10pre5
[davej-history.git] / drivers / char / ftape / zftape / zftape-init.c
blob6dbf2c9cd64e9882ee906c02da073fa9dec19a04
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
284 current->blocked = old_sigmask; /* restore mask */
285 TRACE_EXIT result;
288 /* Read from floppy tape device
290 #if LINUX_VERSION_CODE >= KERNEL_VER(2,1,60)
291 static ssize_t zft_read(struct file *fp, char *buff,
292 size_t req_len, loff_t *ppos)
293 #elif LINUX_VERSION_CODE >= KERNEL_VER(2,1,0)
294 static long zft_read(struct inode *ino, struct file *fp, char *buff,
295 unsigned long req_len)
296 #else
297 static int zft_read(struct inode *ino, struct file *fp, char *buff,
298 int req_len)
299 #endif
301 int result = -EIO;
302 sigset_t old_sigmask;
303 #if LINUX_VERSION_CODE >= KERNEL_VER(2,1,60)
304 struct inode *ino = fp->f_dentry->d_inode;
305 #endif
306 TRACE_FUN(ft_t_flow);
308 TRACE(ft_t_data_flow, "called with count: %ld", (unsigned long)req_len);
309 if (!busy_flag || MINOR(ino->i_rdev) != zft_unit || ft_failure) {
310 TRACE_ABORT(-EIO, ft_t_err,
311 "failed: not busy, failure or wrong unit");
313 old_sigmask = current->blocked; /* save mask */
314 sigfillset(&current->blocked);
315 result = _zft_read(buff, req_len);
316 current->blocked = old_sigmask; /* restore mask */
317 TRACE(ft_t_data_flow, "return with count: %d", result);
318 TRACE_EXIT result;
321 /* Write to tape device
323 #if LINUX_VERSION_CODE >= KERNEL_VER(2,1,60)
324 static ssize_t zft_write(struct file *fp, const char *buff,
325 size_t req_len, loff_t *ppos)
326 #elif LINUX_VERSION_CODE >= KERNEL_VER(2,1,0)
327 static long zft_write(struct inode *ino, struct file *fp, const char *buff,
328 unsigned long req_len)
329 #elif LINUX_VERSION_CODE >= KERNEL_VER(1,3,0)
330 static int zft_write(struct inode *ino, struct file *fp, const char *buff,
331 int req_len)
332 #else
333 static int zft_write(struct inode *ino, struct file *fp, char *buff,
334 int req_len)
335 #endif
337 int result = -EIO;
338 sigset_t old_sigmask;
339 #if LINUX_VERSION_CODE >= KERNEL_VER(2,1,60)
340 struct inode *ino = fp->f_dentry->d_inode;
341 #endif
342 TRACE_FUN(ft_t_flow);
344 TRACE(ft_t_flow, "called with count: %ld", (unsigned long)req_len);
345 if (!busy_flag || MINOR(ino->i_rdev) != zft_unit || ft_failure) {
346 TRACE_ABORT(-EIO, ft_t_err,
347 "failed: not busy, failure or wrong unit");
349 old_sigmask = current->blocked; /* save mask */
350 sigfillset(&current->blocked);
351 result = _zft_write(buff, req_len);
352 current->blocked = old_sigmask; /* restore mask */
353 TRACE(ft_t_data_flow, "return with count: %d", result);
354 TRACE_EXIT result;
357 /* END OF VFS INTERFACE
359 *****************************************************************************/
361 /* driver/module initialization
364 /* the compression module has to call this function to hook into the zftape
365 * code
367 int zft_cmpr_register(struct zft_cmpr_ops *new_ops)
369 TRACE_FUN(ft_t_flow);
371 if (zft_cmpr_ops != NULL) {
372 TRACE_EXIT -EBUSY;
373 } else {
374 zft_cmpr_ops = new_ops;
375 TRACE_EXIT 0;
379 struct zft_cmpr_ops *zft_cmpr_unregister(void)
381 struct zft_cmpr_ops *old_ops = zft_cmpr_ops;
382 TRACE_FUN(ft_t_flow);
384 zft_cmpr_ops = NULL;
385 TRACE_EXIT old_ops;
388 /* lock the zft-compressor() module.
390 int zft_cmpr_lock(int try_to_load)
392 if (zft_cmpr_ops == NULL) {
393 #ifdef CONFIG_KMOD
394 if (try_to_load) {
395 request_module("zft-compressor");
396 if (zft_cmpr_ops == NULL) {
397 return -ENOSYS;
399 } else {
400 return -ENOSYS;
402 #else
403 return -ENOSYS;
404 #endif
406 (*zft_cmpr_ops->lock)();
407 return 0;
410 #ifdef CONFIG_ZFT_COMPRESSOR
411 extern int zft_compressor_init(void);
412 #endif
414 /* Called by modules package when installing the driver or by kernel
415 * during the initialization phase
417 int __init zft_init(void)
419 TRACE_FUN(ft_t_flow);
421 #ifdef MODULE
422 printk(KERN_INFO ZFTAPE_VERSION "\n");
423 if (TRACE_LEVEL >= ft_t_info) {
424 printk(
425 KERN_INFO
426 "(c) 1996, 1997 Claus-Justus Heine (claus@momo.math.rwth-aachen.de)\n"
427 KERN_INFO
428 "vfs interface for ftape floppy tape driver.\n"
429 KERN_INFO
430 "Support for QIC-113 compatible volume table, dynamic memory allocation\n"
431 KERN_INFO
432 "and builtin compression (lzrw3 algorithm).\n"
433 KERN_INFO
434 "Compiled for Linux version %s"
435 #ifdef MODVERSIONS
436 " with versioned symbols"
437 #endif
438 "\n", UTS_RELEASE);
440 #else /* !MODULE */
441 /* print a short no-nonsense boot message */
442 printk(KERN_INFO ZFTAPE_VERSION " for Linux " UTS_RELEASE "\n");
443 #endif /* MODULE */
444 TRACE(ft_t_info, "zft_init @ 0x%p", zft_init);
445 TRACE(ft_t_info,
446 "installing zftape VFS interface for ftape driver ...");
447 TRACE_CATCH(register_chrdev(QIC117_TAPE_MAJOR, "zft", &zft_cdev),);
448 #if LINUX_VERSION_CODE >= KERNEL_VER(1,2,0)
449 # if LINUX_VERSION_CODE < KERNEL_VER(2,1,18)
450 register_symtab(&zft_symbol_table); /* add global zftape symbols */
451 # endif
452 #endif
453 #ifdef CONFIG_ZFT_COMPRESSOR
454 (void)zft_compressor_init();
455 #endif
456 zft_status = ftape_get_status(); /* fetch global data of ftape
457 * hardware driver
459 TRACE_EXIT 0;
463 #ifdef MODULE
464 #if LINUX_VERSION_CODE <= KERNEL_VER(1,2,13) && defined(MODULE)
465 char kernel_version[] = UTS_RELEASE;
466 #endif
467 #if LINUX_VERSION_CODE >= KERNEL_VER(2,1,18)
468 /* Called by modules package before trying to unload the module
470 static int can_unload(void)
472 return (zft_dirty() || busy_flag) ? -EBUSY : 0;
474 #endif
475 /* Called by modules package when installing the driver
477 int init_module(void)
479 #if LINUX_VERSION_CODE >= KERNEL_VER(2,1,18)
480 if (!mod_member_present(&__this_module, can_unload)) {
481 return -EBUSY;
483 __this_module.can_unload = can_unload;
484 #endif
485 return zft_init();
488 /* Called by modules package when removing the driver
490 void cleanup_module(void)
492 TRACE_FUN(ft_t_flow);
494 if (unregister_chrdev(QIC117_TAPE_MAJOR, "zft") != 0) {
495 TRACE(ft_t_warn, "failed");
496 } else {
497 TRACE(ft_t_info, "successful");
499 zft_uninit_mem(); /* release remaining memory, if any */
500 printk(KERN_INFO "zftape successfully unloaded.\n");
501 TRACE_EXIT;
504 #endif /* MODULE */