summaryrefslogtreecommitdiffstats
path: root/drivers/char/ftape/kernel-interface.c
blob: 9bcb35e46560f4fe40fb5ca9dcda72a48882a734 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/*
 *      Copyright (C) 1993-1995 Bas Laarhoven.

 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2, or (at your option)
 any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; see the file COPYING.  If not, write to
 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.

 *
 *      This file contains the code that interfaces the kernel
 *      for the QIC-40/80 floppy-tape driver for Linux.
 */

#include <linux/module.h>
#include <linux/version.h>
#include <linux/errno.h>
#include <linux/fs.h>
#include <asm/uaccess.h>
#include <linux/kernel.h>
#include <linux/signal.h>
#include <linux/major.h>
#include <linux/malloc.h>
#include <linux/init.h>
#include <linux/ftape.h>
#include <asm/dma.h>

#include "tracing.h"
#include "kernel-interface.h"
#include "ftape-read.h"
#include "ftape-write.h"
#include "ftape-io.h"
#include "ftape-ctl.h"
#include "ftape-rw.h"
#include "fdc-io.h"


/*      Global vars.
 */

/* Allocating a 96Kb DMAable buffer in one chunk won't work due to
 * memory fragmentation.  To avoid this, it is broken up into
 * NR_BUFFERS chunks of 32Kbyte. --khp
 */

byte *tape_buffer[NR_BUFFERS] = {NULL};

/*      Local vars.
 */
static int busy_flag = 0;
static int old_sigmask;

static int ftape_open(struct inode *ino, struct file *filep);
static int ftape_close(struct inode *ino, struct file *filep);
static int ftape_ioctl(struct inode *ino, struct file *filep,
		       unsigned int command, unsigned long arg);
static long ftape_read(struct inode *ino, struct file *fp,
		       char *buff, unsigned long req_len);
static long ftape_write(struct inode *ino, struct file *fp,
			const char *buff, unsigned long req_len);

static struct file_operations ftape_cdev =
{
	NULL,			/* lseek */
	ftape_read,		/* read */
	ftape_write,		/* write */
	NULL,			/* readdir */
	NULL,			/* poll */
	ftape_ioctl,		/* ioctl */
	NULL,			/* mmap */
	ftape_open,		/* open */
	ftape_close,		/* release */
	NULL,			/* fsync */
};

/*
 * DMA'able memory allocation stuff.
 */

/* Pure 2^n version of get_order */
static inline int __get_order(unsigned long size)
{
	int order;

	size = (size-1) >> (PAGE_SHIFT-1);
	order = -1;
	do {
		size >>= 1;
		order++;
	} while (size);
	return order;
}

static inline
void *dmaalloc(int order)
{
	return (void *) __get_dma_pages(GFP_KERNEL, order);
}

static inline
void dmafree(void *addr, int order)
{
	free_pages((unsigned long) addr, order);
}

/*
 * Called by modules package when installing the driver
 * or by kernel during the initialization phase
 */

#ifdef MODULE
EXPORT_NO_SYMBOLS;
#define ftape_init init_module
#endif

__initfunc(int ftape_init(void))
{
	int n;
	int order;
	TRACE_FUN(5, "ftape_init");
#ifdef MODULE
	printk(KERN_INFO "ftape-2.08 960314\n"
	       KERN_INFO " (c) 1993-1995 Bas Laarhoven (bas@vimec.nl)\n"
	       KERN_INFO " (c) 1995-1996 Kai Harrekilde-Petersen (khp@dolphinics.no)\n"
	       KERN_INFO " QIC-117 driver for QIC-40/80/3010/3020 tape drives\n"
	       KERN_INFO " Compiled for kernel version " UTS_RELEASE
#ifdef MODVERSIONS
	       " with versioned symbols"
#endif
	       "\n");
#else /* !MODULE */
	/* print a short no-nonsense boot message */
	printk("ftape-2.08 960314 for Linux 1.3.70\n");
#endif /* MODULE */
	TRACE(3, "installing QIC-117 ftape driver...");
	if (register_chrdev(QIC117_TAPE_MAJOR, "ft", &ftape_cdev)) {
		TRACE(1, "register_chrdev failed");
		TRACE_EXIT;
		return -EIO;
	}
	TRACEx1(3, "ftape_init @ 0x%p", ftape_init);
	/*
	 * Allocate the DMA buffers. They are deallocated at cleanup() time.
	 */
	order = __get_order(BUFF_SIZE);
	for (n = 0; n < NR_BUFFERS; n++) {
		tape_buffer[n] = (byte *) dmaalloc(order);
		if (!tape_buffer[n]) {
			TRACE(1, "dmaalloc() failed");
			for (n = 0; n < NR_BUFFERS; n++) {
				if (tape_buffer[n]) {
					dmafree(tape_buffer[n], order);
					tape_buffer[n] = NULL;
				}
			}
			current->blocked = old_sigmask;		/* restore mask */
			if (unregister_chrdev(QIC117_TAPE_MAJOR, "ft") != 0) {
				TRACE(3, "unregister_chrdev failed");
			}
			TRACE_EXIT;
			return -ENOMEM;
		} else {
			TRACEx2(3, "dma-buffer #%d @ %p", n, tape_buffer[n]);
		}
	}
	busy_flag = 0;
	ftape_unit = -1;
	ftape_failure = 1;	/* inhibit any operation but open */
	udelay_calibrate();	/* must be before fdc_wait_calibrate ! */
	fdc_wait_calibrate();
	TRACE_EXIT;

	return 0;
}


#ifdef MODULE
/*      Called by modules package when removing the driver
 */
void cleanup_module(void)
{
	int n;
	int order;
	TRACE_FUN(5, "cleanup_module");

	if (unregister_chrdev(QIC117_TAPE_MAJOR, "ft") != 0) {
		TRACE(3, "failed");
	} else {
		TRACE(3, "successful");
	}
	order = __get_order(BUFF_SIZE);
	for (n = 0; n < NR_BUFFERS; n++) {
		if (tape_buffer[n]) {
			dmafree(tape_buffer[n], order);
			tape_buffer[n] = NULL;
			TRACEx1(3, "removed dma-buffer #%d", n);
		} else {
			TRACEx1(1, "dma-buffer #%d == NULL (bug?)", n);
		}
	}
	TRACE_EXIT;
}
#endif				/* MODULE */

/*      Open ftape device
 */
static int ftape_open(struct inode *ino, struct file *filep)
{
	TRACE_FUN(4, "ftape_open");
	int result;
	MOD_INC_USE_COUNT;	/* lock module in memory */

	TRACEi(5, "called for minor", MINOR(ino->i_rdev));
	if (busy_flag) {
		TRACE(1, "failed: already busy");
		MOD_DEC_USE_COUNT;	/* unlock module in memory */
		TRACE_EXIT;
		return -EBUSY;
	}
	if ((MINOR(ino->i_rdev) & ~FTAPE_NO_REWIND) > 3) {
		TRACE(1, "failed: illegal unit nr");
		MOD_DEC_USE_COUNT;	/* unlock module in memory */
		TRACE_EXIT;
		return -ENXIO;
	}
	if (ftape_unit == -1 || FTAPE_UNIT != (MINOR(ino->i_rdev) & 3)) {
		/*  Other selection than last time
		 */
		ftape_init_driver();
	}
	ftape_unit = MINOR(ino->i_rdev);
	ftape_failure = 0;	/* allow tape operations */
	old_sigmask = current->blocked;
	current->blocked = _BLOCK_ALL;
	fdc_save_drive_specs();	/* save Drive Specification regs on i82078-1's */
	result = _ftape_open();
	if (result < 0) {
		TRACE(1, "_ftape_open failed");
		current->blocked = old_sigmask;		/* restore mask */
		MOD_DEC_USE_COUNT;	/* unlock module in memory */
		TRACE_EXIT;
		return result;
	} else {
		busy_flag = 1;
		/*  Mask signals that will disturb proper operation of the
		 *  program that is calling.
		 */
		current->blocked = old_sigmask | _DO_BLOCK;
		TRACE_EXIT;
		return 0;
	}
}

/*      Close ftape device
 */
static int ftape_close(struct inode *ino, struct file *filep)
{
	TRACE_FUN(4, "ftape_close");
	int result;

	if (!busy_flag || MINOR(ino->i_rdev) != ftape_unit) {
		TRACE(1, "failed: not busy or wrong unit");
		TRACE_EXIT;
		return 0;		/* keep busy_flag !(?) */
	}
	current->blocked = _BLOCK_ALL;
	result = _ftape_close();
	if (result < 0) {
		TRACE(1, "_ftape_close failed");
	}
	fdc_restore_drive_specs();	/* restore original values */
	ftape_failure = 1;	/* inhibit any operation but open */
	busy_flag = 0;
	current->blocked = old_sigmask;		/* restore before open state */
	TRACE_EXIT;
	MOD_DEC_USE_COUNT;	/* unlock module in memory */
	return 0;
}

/*      Ioctl for ftape device
 */
static int ftape_ioctl(struct inode *ino, struct file *filep,
		       unsigned int command, unsigned long arg)
{
	TRACE_FUN(4, "ftape_ioctl");
	int result = -EIO;
	int old_sigmask;

	if (!busy_flag || MINOR(ino->i_rdev) != ftape_unit || ftape_failure) {
		TRACE(1, "failed: not busy, failure or wrong unit");
		TRACE_EXIT;
		return -EIO;
	}
	old_sigmask = current->blocked;		/* save mask */
	current->blocked = _BLOCK_ALL;
	/* This will work as long as sizeof( void*) == sizeof( long)
	 */
	result = _ftape_ioctl(command, (void *) arg);
	current->blocked = old_sigmask;		/* restore mask */
	TRACE_EXIT;
	return result;
}

/*      Read from tape device
 */
static long ftape_read(struct inode *ino, struct file *fp,
	char *buff, unsigned long req_len)
{
	TRACE_FUN(5, "ftape_read");
	int result = -EIO;
	int old_sigmask;

	TRACEi(5, "called with count:", (int) req_len);
	if (!busy_flag || MINOR(ino->i_rdev) != ftape_unit || ftape_failure) {
		TRACE(1, "failed: not busy, failure or wrong unit");
		TRACE_EXIT;
		return -EIO;
	}
	old_sigmask = current->blocked;		/* save mask */
	current->blocked = _BLOCK_ALL;
	result = _ftape_read(buff, req_len);
	TRACEi(7, "return with count:", result);
	current->blocked = old_sigmask;		/* restore mask */
	TRACE_EXIT;
	return result;
}

/*      Write to tape device
 */
static long ftape_write(struct inode *ino, struct file *fp,
	const char *buff, unsigned long req_len)
{
	TRACE_FUN(8, "ftape_write");
	int result = -EIO;
	int old_sigmask;

	TRACEi(5, "called with count:", (int) req_len);
	if (!busy_flag || MINOR(ino->i_rdev) != ftape_unit || ftape_failure) {
		TRACE(1, "failed: not busy, failure or wrong unit");
		TRACE_EXIT;
		return -EIO;
	}
	old_sigmask = current->blocked;		/* save mask */
	current->blocked = _BLOCK_ALL;
	result = _ftape_write(buff, req_len);
	TRACEi(7, "return with count:", result);
	current->blocked = old_sigmask;		/* restore mask */
	TRACE_EXIT;
	return result;
}