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