OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [linux-2.4/] [drivers/] [char/] [tpqic02.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1275 phoenix
/* $Id: tpqic02.c,v 1.1.1.1 2004-04-15 01:58:24 phoenix Exp $
2
 *
3
 * Driver for tape drive support for Linux-i386
4
 *
5
 * Copyright (c) 1992--1996 by H. H. Bergman. All rights reserved.
6
 * Current e-mail address: hennus@cybercomm.nl
7
 *
8
 * Distribution of this program in executable form is only allowed if
9
 * all of the corresponding source files are made available through the same
10
 * medium at no extra cost.
11
 *
12
 * I will not accept any responsibility for damage caused directly or
13
 * indirectly by this program, or code derived from this program.
14
 *
15
 * Use this code at your own risk. Don't blame me if it destroys your data!
16
 * Make sure you have a backup before you try this code.
17
 *
18
 * If you make changes to my code and redistribute it in source or binary
19
 * form you must make it clear to even casual users of your code that you
20
 * have modified my code, clearly point out what the changes exactly are
21
 * (preferably in the form of a context diff file), how to undo your changes,
22
 * where the original can be obtained, and that complaints/requests about the
23
 * modified code should be directed to you instead of me.
24
 *
25
 * This driver was partially inspired by the 'wt' driver in the 386BSD
26
 * source distribution, which carries the following copyright notice:
27
 *
28
 *  Copyright (c) 1991 The Regents of the University of California.
29
 *  All rights reserved.
30
 *
31
 * You are not allowed to change this line nor the text above.
32
 *
33
 * 2001/02/26   Minor s/suser/capable/
34
 *
35
 * 1996/10/10   Emerald changes
36
 *
37
 * 1996/05/21   Misc changes+merges+cleanups + I/O reservations
38
 *
39
 * 1996/05/20   Module support patches submitted by Brian McCauley.
40
 *
41
 * 1994/05/03   Initial attempt at Mountain support for the Mountain 7150.
42
 * Based on patches provided by Erik Jacobson. Still incomplete, I suppose.
43
 *
44
 * 1994/02/07   Archive changes & some cleanups by Eddy Olk.
45
 *
46
 * 1994/01/19   Speed measuring stuff moved from aperf.h to delay.h.
47
 *              BogoMips (tm) introduced by Linus.
48
 *
49
 * 1993/01/25   Kernel udelay. Eof fixups.
50
 *
51
 * 1992/09/19   Some changes based on patches by Eddy Olk to support
52
 *              Archive SC402/SC499R controller cards.
53
 *
54
 * 1992/05/27   First release.
55
 *
56
 * 1992/05/26   Initial version. Copyright H. H. Bergman 1992
57
 */
58
 
59
/* After the legalese, now the important bits:
60
 *
61
 * This is a driver for the Wangtek 5150 tape drive with
62
 * a QIC-02 controller for ISA-PC type computers.
63
 * Hopefully it will work with other QIC-02 tape drives as well.
64
 *
65
 * Make sure your setup matches the configuration parameters.
66
 * Also, be careful to avoid IO conflicts with other devices!
67
 */
68
 
69
 
70
/*
71
#define TDEBUG
72
*/
73
 
74
#define REALLY_SLOW_IO          /* it sure is ... */
75
 
76
#include <linux/module.h>
77
 
78
#include <linux/config.h>
79
 
80
#include <linux/sched.h>
81
#include <linux/timer.h>
82
#include <linux/fs.h>
83
#include <linux/kernel.h>
84
#include <linux/major.h>
85
#include <linux/errno.h>
86
#include <linux/mtio.h>
87
#include <linux/fcntl.h>
88
#include <linux/delay.h>
89
#include <linux/ioport.h>
90
#include <linux/tpqic02.h>
91
#include <linux/mm.h>
92
#include <linux/slab.h>
93
#include <linux/init.h>
94
#include <linux/smp_lock.h>
95
#include <linux/devfs_fs_kernel.h>
96
 
97
#include <asm/dma.h>
98
#include <asm/system.h>
99
#include <asm/io.h>
100
#include <asm/uaccess.h>
101
 
102
/* check existence of required configuration parameters */
103
#if !defined(QIC02_CMD_PORT) || !defined(QIC02_TAPE_IRQ) || !defined(QIC02_TAPE_DMA)
104
# error qic02_tape configuration error
105
#endif
106
 
107
 
108
#define TPQIC02_NAME    "tpqic02"
109
 
110
/* Linux outb() commands have (value,port) as parameters.
111
 * One might expect (port,value) instead, so beware!
112
 */
113
 
114
#ifdef CONFIG_QIC02_DYNCONF
115
/* This holds the dynamic configuration info for the interface
116
 * card+drive info if runtime configuration has been selected.
117
 */
118
 
119
static struct mtconfiginfo qic02_tape_dynconf = /* user settable */
120
{ 0, 0, BOGUS_IRQ, 0, 0, TPQD_DEFAULT_FLAGS, };
121
static struct qic02_ccb qic02_tape_ccb; /* private stuff */
122
 
123
#else
124
 
125
unsigned long qic02_tape_debug = TPQD_DEFAULT_FLAGS;
126
 
127
# if ((QIC02_TAPE_IFC!=WANGTEK) && (QIC02_TAPE_IFC!=ARCHIVE) && (QIC02_TAPE_IFC!=MOUNTAIN))
128
#  error No valid interface card specified
129
# endif
130
#endif                          /* CONFIG_QIC02_DYNCONF */
131
 
132
static volatile int ctlbits;    /* control reg bits for tape interface */
133
 
134
static wait_queue_head_t qic02_tape_transfer;   /* sync rw with interrupts */
135
 
136
static volatile struct mtget ioctl_status;      /* current generic status */
137
 
138
static volatile struct tpstatus tperror;        /* last drive status */
139
 
140
static char rcs_revision[] = "$Revision: 1.1.1.1 $";
141
static char rcs_date[] = "$Date: 2004-04-15 01:58:24 $";
142
 
143
/* Flag bits for status and outstanding requests.
144
 * (Could all be put in one bit-field-struct.)
145
 * Some variables need `volatile' because they may be modified
146
 * by an interrupt.
147
 */
148
static volatile flag status_dead = YES; /* device is legally dead until proven alive */
149
static flag status_zombie = YES;        /* it's `zombie' until irq/dma allocated */
150
 
151
static volatile flag status_bytes_wr = NO;      /* write FM at close or not */
152
static volatile flag status_bytes_rd = NO;      /* (rd|wr) used for rewinding */
153
 
154
static volatile unsigned long status_cmd_pending;       /* cmd in progress */
155
static volatile flag status_expect_int = NO;    /* ready for interrupts */
156
static volatile flag status_timer_on = NO;      /* using time-out */
157
static volatile int status_error;       /* int handler may detect error */
158
static volatile flag status_eof_detected = NO;  /* end of file */
159
static volatile flag status_eom_detected = NO;  /* end of recorded media */
160
static volatile flag status_eot_detected = NO;  /* end of tape */
161
static volatile flag doing_read = NO;
162
static volatile flag doing_write = NO;
163
 
164
static volatile unsigned long dma_bytes_todo;
165
static volatile unsigned long dma_bytes_done;
166
static volatile unsigned dma_mode;      /* !=0 also means DMA in use */
167
static flag need_rewind = YES;
168
 
169
static kdev_t current_tape_dev;
170
static int extra_blocks_left = BLOCKS_BEYOND_EW;
171
 
172
static struct timer_list tp_timer;
173
 
174
/* return_*_eof:
175
 *      NO:     not at EOF,
176
 *      YES:    tell app EOF was reached (return 0).
177
 *
178
 * return_*_eof==YES && reported_*_eof==NO  ==>
179
 *      return current buffer, next time(s) return EOF.
180
 *
181
 * return_*_eof==YES && reported_*_eof==YES  ==>
182
 *      at EOF and application knows it, so we can
183
 *      move on to the next file.
184
 *
185
 */
186
static flag return_read_eof = NO;       /* set to signal app EOF was reached */
187
static flag return_write_eof = NO;
188
static flag reported_read_eof = NO;     /* set when we've done that */
189
static flag reported_write_eof = NO;
190
 
191
 
192
/* This is for doing `mt seek <blocknr>' */
193
static char seek_addr_buf[AR_SEEK_BUF_SIZE];
194
 
195
 
196
/* In write mode, we have to write a File Mark after the last block written,
197
 * when the tape device is closed. Tape repositioning and reading in write
198
 * mode is allowed as long as no actual writing has been done. After writing
199
 * the File Mark, repositioning and reading are allowed again.
200
 */
201
static int mode_access;         /* access mode: READ or WRITE */
202
 
203
static int qic02_get_resources(void);
204
static void qic02_release_resources(void);
205
 
206
/* This is a pointer to the actual kernel buffer where the interrupt routines
207
 * read from/write to. It is needed because the DMA channels 1 and 3 cannot
208
 * always access the user buffers. [The kernel buffer must reside in the
209
 * lower 16MBytes of system memory because of the DMA controller.] The user
210
 * must ensure that a large enough buffer is passed to the kernel, in order
211
 * to reduce tape repositioning wear and tear.
212
 */
213
static void *buffaddr;          /* virtual address of buffer */
214
 
215
/* This translates minor numbers to the corresponding recording format: */
216
static const char *format_names[] = {
217
        "not set",              /* for dumb drives unable to handle format selection */
218
        "11",                   /* extinct */
219
        "24",
220
        "120",
221
        "150",
222
        "300",                  /* untested. */
223
        "600"                   /* untested. */
224
};
225
 
226
 
227
/* `exception_list' is needed for exception status reporting.
228
 * Exceptions 1..14 are defined by QIC-02 rev F.
229
 * The drive status is matched sequentially to each entry,
230
 * ignoring irrelevant bits, until a match is found. If no
231
 * match is found, exception number 0 is used. (That should of
232
 * course never happen...) The original table was based on the
233
 * "Exception Status Summary" in QIC-02 rev F, but some changes
234
 * were required to make it work with real-world drives.
235
 *
236
 * Exception 2 (CNI) is changed to also cover status 0x00e0 (mask USL),
237
 * Exception 4 (EOM) is changed to also cover status 0x8288 (mask EOR),
238
 * Exception 11 (FIL) is changed to also cover status 0x0089 (mask EOM).
239
 * Exception 15 (EOR) is added for seek-to-end-of-data (catch EOR),
240
 * Exception 16 (BOM) is added for beginning-of-media (catch BOM).
241
 *
242
 * Had to swap EXC_NDRV and EXC_NCART to ensure that extended EXC_NCART
243
 * (because of the incorrect Wangtek status code) doesn't catch the
244
 * EXC_NDRV first.
245
 */
246
static struct exception_list_type {
247
        unsigned short mask, code;
248
        const char *msg;
249
        /* EXC_nr attribute should match with tpqic02.h */
250
} exception_list[] = {
251
        {
252
        0, 0, "Unknown exception status code", /* extra: 0 */ },
253
        {
254
        ~(0), TP_ST0 | TP_CNI | TP_USL | TP_WRP,
255
                    "Drive not online" /* 1 */ },
256
            /* Drive presence goes before cartridge presence. */
257
        {
258
                ~(TP_WRP | TP_USL), TP_ST0 | TP_CNI,
259
                    /* My Wangtek 5150EQ sometimes reports a status code
260
                     * of 0x00e0, which is not a valid exception code, but
261
                     * I think it should be recognized as "NO CARTRIDGE".
262
                     */
263
        "Cartridge not in place" /* 2 */ },
264
        {
265
        (unsigned short) ~(TP_ST1 | TP_BOM), (TP_ST0 | TP_WRP),
266
                    "Write protected cartridge" /* 3 */ },
267
        {
268
        (unsigned short) ~(TP_ST1 | TP_EOR), (TP_ST0 | TP_EOM),
269
                    "End of media" /* 4 */ },
270
        {
271
        ~TP_WRP, TP_ST0 | TP_UDA | TP_ST1 | TP_BOM,
272
                    "Read or Write abort. Rewind tape." /* 5 */ },
273
        {
274
        ~TP_WRP, TP_ST0 | TP_UDA,
275
                    "Read error. Bad block transferred." /* 6 */ },
276
        {
277
        ~TP_WRP, TP_ST0 | TP_UDA | TP_BNL,
278
                    "Read error. Filler block transferred." /* 7 */ },
279
        {
280
        ~TP_WRP, TP_ST0 | TP_UDA | TP_BNL | TP_ST1 | TP_NDT,
281
                    "Read error. No data detected." /* 8 */ },
282
        {
283
        ~TP_WRP,
284
                    TP_ST0 | TP_EOM | TP_UDA | TP_BNL | TP_ST1 |
285
                    TP_NDT, "Read error. No data detected. EOM." /* 9 */ },
286
        {
287
        ~(TP_WRP | TP_MBD | TP_PAR | TP_EOR),
288
                    TP_ST0 | TP_UDA | TP_BNL | TP_ST1 | TP_NDT |
289
                    TP_BOM,
290
                    "Read error. No data detected. BOM." /* 10 */ },
291
        {
292
                ~(TP_WRP | TP_EOM), TP_ST0 | TP_FIL,
293
                    /* Status 0x0089 (EOM & FM) is viewed as an FM,
294
                     * because it can only happen during a read.
295
                     * EOM is checked separately for an FM condition.
296
                     */
297
        "File mark detected" /* 11 */ },
298
        {
299
        ~(TP_ST0 | TP_CNI | TP_USL | TP_WRP | TP_BOM),
300
                    TP_ST1 | TP_ILL, "Illegal command" /* 12 */ },
301
        {
302
        ~(TP_ST0 | TP_CNI | TP_USL | TP_WRP | TP_BOM),
303
                    TP_ST1 | TP_POR, "Reset occurred" /* 13 */ },
304
        {
305
                ~TP_WRP, TP_ST0 | TP_FIL | TP_MBD,      /* NOTE: ST1 not set! */
306
        "Marginal block detected" /* 14 */ },
307
        {
308
                ~(TP_ST0 | TP_WRP | TP_EOM | TP_UDA | TP_BNL | TP_FIL |
309
                  TP_NDT), TP_ST1 | TP_EOR,
310
                /********** Is the extra TP_NDT really needed Eddy? **********/
311
        "End of recorded media" /* extra: 15 */ },
312
            /* 15 is returned when SEEKEOD completes successfully */
313
        {
314
        ~(TP_WRP | TP_ST0), TP_ST1 | TP_BOM, "Beginning of media" /* extra: 16 */ }
315
};
316
 
317
#define NR_OF_EXC       (sizeof(exception_list)/sizeof(struct exception_list_type))
318
 
319
/* Compare expected struct size and actual struct size. This
320
 * is useful to catch programs compiled with old #includes.
321
 */
322
#define CHECK_IOC_SIZE(structure) \
323
        if (_IOC_SIZE(iocmd) != sizeof(struct structure)) { \
324
                tpqputs(TPQD_ALWAYS, "sizeof(struct " #structure \
325
                        ") does not match!"); \
326
                return -EFAULT; \
327
        } \
328
 
329
static void tpqputs(unsigned long flags, const char *s)
330
{
331
        if ((flags & TPQD_ALWAYS) || (flags & QIC02_TAPE_DEBUG))
332
                printk(TPQIC02_NAME ": %s\n", s);
333
}                               /* tpqputs */
334
 
335
 
336
/* Perform byte order swapping for a 16-bit word.
337
 *
338
 * [FIXME] This should probably be in include/asm/
339
 * ([FIXME] i486 can do this faster)
340
 */
341
static inline void byte_swap_w(volatile unsigned short *w)
342
{
343
        int t = *w;
344
        *w = (t >> 8) | ((t & 0xff) << 8);
345
}
346
 
347
 
348
 
349
/* Init control register bits on interface card.
350
 * For Archive, interrupts must be enabled explicitly.
351
 * Wangtek interface card requires ONLINE to be set, Archive SC402/SC499R
352
 * cards keep it active all the time.
353
 */
354
static void ifc_init(void)
355
{
356
        if (QIC02_TAPE_IFC == WANGTEK) {        /* || (QIC02_TAPE_IFC == EVEREX) */
357
                ctlbits = WT_CTL_ONLINE;        /* online */
358
                outb_p(ctlbits, QIC02_CTL_PORT);
359
        } else if (QIC02_TAPE_IFC == ARCHIVE) {
360
                ctlbits = 0;     /* no interrupts yet */
361
                outb_p(ctlbits, QIC02_CTL_PORT);
362
                outb_p(0, AR_RESET_DMA_PORT);    /* dummy write to reset DMA */
363
        } else {                /* MOUNTAIN */
364
 
365
                ctlbits = MTN_CTL_ONLINE;       /* online, and logic enabled */
366
                outb_p(ctlbits, QIC02_CTL_PORT);
367
        }
368
}                               /* ifc_init */
369
 
370
 
371
static void report_qic_exception(unsigned n)
372
{
373
        if (n >= NR_OF_EXC) {
374
                tpqputs(TPQD_ALWAYS, "Oops -- report_qic_exception");
375
                n = 0;
376
        }
377
        if (TPQDBG(SENSE_TEXT) || n == 0) {
378
                printk(TPQIC02_NAME ": sense: %s\n",
379
                       exception_list[n].msg);
380
        }
381
}                               /* report_qic_exception */
382
 
383
 
384
/* Try to map the drive-exception bits `s' to a predefined "exception number",
385
 * by comparing the significant exception bits for each entry in the
386
 * exception table (`exception_list[]').
387
 * It is assumed that s!=0.
388
 */
389
static int decode_qic_exception_nr(unsigned s)
390
{
391
        int i;
392
 
393
        for (i = 1; i < NR_OF_EXC; i++) {
394
                if ((s & exception_list[i].mask) == exception_list[i].code) {
395
                        return i;
396
                }
397
        }
398
        printk(TPQIC02_NAME
399
               ": decode_qic_exception_nr: exception(%x) not recognized\n",
400
               s);
401
        return 0;
402
}                               /* decode_qic_exception_nr */
403
 
404
 
405
 
406
/* Perform appropriate action for certain exceptions.
407
 * should return a value to indicate stop/continue (in case of bad blocks)
408
 */
409
static void handle_qic_exception(int exnr, int exbits)
410
{
411
        if (exnr == EXC_NCART) {
412
                /* Cartridge was changed. Redo sense().
413
                 * EXC_NCART should be handled in open().
414
                 * It is not permitted to remove the tape while
415
                 * the tape driver has open files.
416
                 */
417
                need_rewind = YES;
418
                status_eof_detected = NO;
419
                status_eom_detected = NO;
420
        } else if (exnr == EXC_XFILLER) {
421
                tpqputs(TPQD_ALWAYS,
422
                        "[Bad block -- filler data transferred.]");
423
        } else if (exnr == EXC_XBAD) {
424
                tpqputs(TPQD_ALWAYS, "[CRC failed!]");
425
        } else if (exnr == EXC_MARGINAL) {
426
                /* A marginal block behaves much like a FM.
427
                 * User may continue reading, if desired.
428
                 */
429
                tpqputs(TPQD_ALWAYS, "[Marginal block]");
430
                doing_read = NO;
431
        } else if (exnr == EXC_FM) {
432
                doing_read = NO;
433
        }
434
}                               /* handle_qic_exception */
435
 
436
 
437
static inline int is_exception(void)
438
{
439
        return (inb(QIC02_STAT_PORT) & QIC02_STAT_EXCEPTION) == 0;
440
}                               /* is_exception */
441
 
442
 
443
/* Reset the tape drive and controller.
444
 * When reset fails, it marks  the drive as dead and all
445
 * requests (except reset) are to be ignored (ENXIO).
446
 */
447
static int tape_reset(int verbose)
448
{
449
        ifc_init();             /* reset interface card */
450
 
451
        /* assert reset */
452
        if (QIC02_TAPE_IFC == MOUNTAIN) {
453
                outb_p(ctlbits & ~MTN_QIC02_CTL_RESET_NOT, QIC02_CTL_PORT);
454
        } else {                /* WANGTEK, ARCHIVE */
455
 
456
                outb_p(ctlbits | QIC02_CTL_RESET, QIC02_CTL_PORT);
457
        }
458
 
459
        /* Next, we need to wait >=25 usec. */
460
        udelay(30);
461
 
462
        /* after reset, we will be at BOT (modulo an automatic rewind) */
463
        status_eof_detected = NO;
464
        status_eom_detected = NO;
465
        status_cmd_pending = 0;
466
        need_rewind = YES;
467
        doing_read = doing_write = NO;
468
        ioctl_status.mt_fileno = ioctl_status.mt_blkno = 0;
469
 
470
        /* de-assert reset */
471
        if (QIC02_TAPE_IFC == MOUNTAIN) {
472
                outb_p(ctlbits | MTN_QIC02_CTL_RESET_NOT, QIC02_CTL_PORT);
473
        } else {
474
                outb_p(ctlbits & ~QIC02_CTL_RESET, QIC02_CTL_PORT);
475
        }
476
 
477
        /* KLUDGE FOR G++ BUG */
478
        {
479
                int stat = inb_p(QIC02_STAT_PORT);
480
                status_dead =
481
                    ((stat & QIC02_STAT_RESETMASK) != QIC02_STAT_RESETVAL);
482
        }
483
        /* if successful, inb(STAT) returned RESETVAL */
484
        if (status_dead == YES) {
485
                printk(TPQIC02_NAME ": reset failed!\n");
486
        } else if (verbose) {
487
                printk(TPQIC02_NAME ": reset successful\n");
488
        }
489
 
490
        return (status_dead == YES) ? TE_DEAD : TE_OK;
491
}                               /* tape_reset */
492
 
493
 
494
 
495
/* Notify tape drive of a new command. It only waits for the
496
 * command to be accepted, not for the actual command to complete.
497
 *
498
 * Before calling this routine, QIC02_CMD_PORT must have been loaded
499
 * with the command to be executed.
500
 * After this routine, the exception bit must be checked.
501
 * This routine is also used by rdstatus(), so in that case, any exception
502
 * must be ignored (`ignore_ex' flag).
503
 */
504
static int notify_cmd(char cmd, short ignore_ex)
505
{
506
        int i;
507
 
508
        outb_p(cmd, QIC02_CMD_PORT);    /* output the command */
509
 
510
        /* wait 1 usec before asserting /REQUEST */
511
        udelay(1);
512
 
513
        if ((!ignore_ex) && is_exception()) {
514
                tpqputs(TPQD_ALWAYS, "*** exception detected in notify_cmd");
515
                /** force a reset here **/
516
                if (tape_reset(1) == TE_DEAD)
517
                        return TE_DEAD;
518
                if (is_exception()) {
519
                        tpqputs(TPQD_ALWAYS, "exception persists after reset.");
520
                        tpqputs(TPQD_ALWAYS, " ^ exception ignored.");
521
                }
522
        }
523
 
524
        outb_p(ctlbits | QIC02_CTL_REQUEST, QIC02_CTL_PORT);    /* set request bit */
525
        i = TAPE_NOTIFY_TIMEOUT;
526
        /* The specs say this takes about 500 usec, but there is no upper limit!
527
         * If the drive were busy retensioning or something like that,
528
         * it could be *much* longer!
529
         */
530
        while ((inb_p(QIC02_STAT_PORT) & QIC02_STAT_READY) && (--i > 0))
531
                /*skip */ ;
532
        /* wait for ready */
533
        if (i == 0) {
534
                tpqputs(TPQD_ALWAYS,
535
                        "timed out waiting for ready in notify_cmd");
536
                status_dead = YES;
537
                return TE_TIM;
538
        }
539
 
540
        outb_p(ctlbits & ~QIC02_CTL_REQUEST, QIC02_CTL_PORT);   /* reset request bit */
541
        i = TAPE_NOTIFY_TIMEOUT;
542
        /* according to the specs this one should never time-out */
543
        while (((inb_p(QIC02_STAT_PORT) & QIC02_STAT_READY) == 0) && (--i > 0))
544
                /*skip */ ;
545
        /* wait for not ready */
546
        if (i == 0) {
547
                tpqputs(TPQD_ALWAYS, "timed out waiting for !ready in notify_cmd");
548
                status_dead = YES;
549
                return TE_TIM;
550
        }
551
        /* command accepted */
552
        return TE_OK;
553
}                               /* notify_cmd */
554
 
555
 
556
 
557
/* Wait for a command to complete, with timeout */
558
static int wait_for_ready(time_t timeout)
559
{
560
        int stat;
561
        time_t spin_t;
562
 
563
        /* Wait for ready or exception, without driving the loadavg up too much.
564
         * In most cases, the tape drive already has READY asserted,
565
         * so optimize for that case.
566
         *
567
         * First, busy wait a few usec:
568
         */
569
        spin_t = 50;
570
        while (((stat = inb_p(QIC02_STAT_PORT) & QIC02_STAT_MASK) == QIC02_STAT_MASK) && (--spin_t > 0))
571
                /*SKIP*/;
572
        if ((stat & QIC02_STAT_READY) == 0)
573
                return TE_OK;   /* covers 99.99% of all calls */
574
 
575
        /* Then use schedule() a few times */
576
        spin_t = 3;             /* max 0.03 sec busy waiting */
577
        if (spin_t > timeout)
578
                spin_t = timeout;
579
        timeout -= spin_t;
580
        spin_t += jiffies;
581
 
582
        /* FIXME...*/
583
        while (((stat = inb_p(QIC02_STAT_PORT) & QIC02_STAT_MASK) == QIC02_STAT_MASK)
584
                && time_before(jiffies, spin_t))
585
                schedule();     /* don't waste all the CPU time */
586
        if ((stat & QIC02_STAT_READY) == 0)
587
                return TE_OK;
588
 
589
        /* If we reach this point, we probably need to wait much longer, or
590
         * an exception occurred. Either case is not very time-critical.
591
         * Check the status port only a few times every second.
592
         * A interval of less than 0.10 sec will not be noticed by the user,
593
         * more than 0.40 sec may give noticeable delays.
594
         */
595
        spin_t += timeout;
596
        TPQDEB({printk("wait_for_ready: additional timeout: %d\n", spin_t);})
597
 
598
            /* not ready and no exception && timeout not expired yet */
599
        while (((stat = inb_p(QIC02_STAT_PORT) & QIC02_STAT_MASK) == QIC02_STAT_MASK) && time_before(jiffies, spin_t)) {
600
                /* be `nice` to other processes on long operations... */
601
                current->state = TASK_INTERRUPTIBLE;
602
                /* nap 0.30 sec between checks, */
603
                /* but could be woken up earlier by signals... */
604
                schedule_timeout(3 * HZ / 10);
605
        }
606
 
607
        /* don't use jiffies for this test because it may have changed by now */
608
        if ((stat & QIC02_STAT_MASK) == QIC02_STAT_MASK) {
609
                tpqputs(TPQD_ALWAYS, "wait_for_ready() timed out");
610
                return TE_TIM;
611
        }
612
 
613
        if ((stat & QIC02_STAT_EXCEPTION) == 0) {
614
                tpqputs(TPQD_ALWAYS,
615
                        "exception detected after waiting_for_ready");
616
                return TE_EX;
617
        } else {
618
                return TE_OK;
619
        }
620
}                               /* wait_for_ready */
621
 
622
 
623
 
624
/* Send some data to the drive */
625
static int send_qic02_data(char sb[], unsigned size, int ignore_ex)
626
{
627
        int i, stat;
628
 
629
        for (i = 0; i < size; i++) {
630
 
631
                stat = wait_for_ready(TIM_S);
632
                if (stat != TE_OK)
633
                        return stat;
634
 
635
                stat = notify_cmd(sb[i], ignore_ex);
636
                if (stat != TE_OK)
637
                        return stat;
638
        }
639
        return TE_OK;
640
 
641
}                               /* send_qic02_data */
642
 
643
 
644
/* Send a QIC-02 command (`cmd') to the tape drive, with
645
 * a time-out (`timeout').
646
 * This one is also used by tp_sense(), so we must have
647
 * a flag to disable exception checking (`ignore_ex').
648
 *
649
 * On entry, the controller is supposed to be READY.
650
 */
651
static int send_qic02_cmd(int cmd, time_t timeout, int ignore_ex)
652
{
653
        int stat;
654
 
655
        stat = inb_p(QIC02_STAT_PORT);
656
        if ((stat & QIC02_STAT_EXCEPTION) == 0) {        /* if exception */
657
                tpqputs(TPQD_ALWAYS, "send_qic02_cmd: Exception!");
658
                return TE_EX;
659
        }
660
        if (stat & QIC02_STAT_READY) {  /* if not ready */
661
                tpqputs(TPQD_ALWAYS, "send_qic02_cmd: not Ready!");
662
                return TE_ERR;
663
        }
664
 
665
        /* assert(ready & !exception) */
666
 
667
        /* Remember current command for later re-use with dma transfers.
668
         * (For reading/writing multiple blocks.)
669
         */
670
        status_cmd_pending = cmd;
671
 
672
        stat = notify_cmd(cmd, ignore_ex);      /* tell drive new command was loaded, */
673
        /* inherit exception check. */
674
        if (TP_HAVE_SEEK && (cmd == AR_QCMDV_SEEK_BLK)) {
675
                /* This one needs to send 3 more bytes, MSB first */
676
                stat = send_qic02_data(seek_addr_buf, sizeof(seek_addr_buf), ignore_ex);
677
        }
678
 
679
        if (stat != TE_OK) {
680
                tpqputs(TPQD_ALWAYS, "send_qic02_cmd failed");
681
        }
682
        return stat;
683
}                               /* send_qic02_cmd */
684
 
685
 
686
 
687
/* Get drive status. Assume drive is ready or has exception set.
688
 * (or will be in <1000 usec.)
689
 * Extra parameters added because of 'Read Extended Status 3' command.
690
 */
691
static int rdstatus(char *stp, unsigned size, char qcmd)
692
{
693
        int s, n;
694
        char *q = stp;
695
 
696
        /* Try to busy-wait a few (700) usec, after that de-schedule.
697
         *
698
         * The problem is, if we don't de-schedule, performance will
699
         * drop to zero when the drive is not responding and if we
700
         * de-schedule immediately, we waste a lot of time because a
701
         * task switch is much longer than we usually have to wait here.
702
         */
703
        n = 1000;               /* 500 is not enough on a 486/33 */
704
        while ((n > 0) && ((inb_p(QIC02_STAT_PORT) & QIC02_STAT_MASK) == QIC02_STAT_MASK))
705
                n--;            /* wait for ready or exception or timeout */
706
        if (n == 0) {
707
                /* n (above) should be chosen such that on your machine
708
                 * you rarely ever see the message below, and it should
709
                 * be small enough to give reasonable response time.]
710
                 */
711
                /* FIXME */
712
                tpqputs(TPQD_ALWAYS, "waiting looong in rdstatus() -- drive dead?");
713
                while ((inb_p(QIC02_STAT_PORT) & QIC02_STAT_MASK) == QIC02_STAT_MASK)
714
                        schedule();
715
                tpqputs(TPQD_ALWAYS, "finished waiting in rdstatus()");
716
        }
717
 
718
        (void) notify_cmd(qcmd, 1);     /* send read status command */
719
        /* ignore return code -- should always be ok, STAT may contain
720
         * exception flag from previous exception which we are trying to clear.
721
         */
722
 
723
        if (TP_DIAGS(current_tape_dev))
724
                printk(TPQIC02_NAME ": reading status bytes: ");
725
 
726
        for (q = stp; q < stp + size; q++) {
727
                do
728
                        s = inb_p(QIC02_STAT_PORT);
729
                while ((s & QIC02_STAT_MASK) == QIC02_STAT_MASK);       /* wait for ready or exception */
730
 
731
                if ((s & QIC02_STAT_EXCEPTION) == 0) {   /* if exception */
732
                        tpqputs(TPQD_ALWAYS, "rdstatus: exception error");
733
                        ioctl_status.mt_erreg = 0;       /* dunno... */
734
                        return TE_NS;   /* error, shouldn't happen... */
735
                }
736
 
737
                *q = inb_p(QIC02_DATA_PORT);    /* read status byte */
738
 
739
                if (TP_DIAGS(current_tape_dev))
740
                        printk("[%1d]=0x%x  ", q - stp,
741
                               (unsigned) (*q) & 0xff);
742
 
743
                outb_p(ctlbits | QIC02_CTL_REQUEST, QIC02_CTL_PORT);    /* set request */
744
 
745
                while ((inb_p(QIC02_STAT_PORT) & QIC02_STAT_READY) == 0);        /* wait for not ready */
746
 
747
                udelay(22);     /* delay >20 usec */
748
 
749
                outb_p(ctlbits & ~QIC02_CTL_REQUEST, QIC02_CTL_PORT);   /* un-set request */
750
 
751
        }
752
 
753
        /* Specs say we should wait for READY here.
754
         * My drive doesn't seem to need it here yet, but others do?
755
         */
756
        while (inb_p(QIC02_STAT_PORT) & QIC02_STAT_READY)
757
                /*skip */ ;
758
        /* wait for ready */
759
 
760
        if (TP_DIAGS(current_tape_dev))
761
                printk("\n");
762
 
763
        return TE_OK;
764
}                               /* rdstatus */
765
 
766
 
767
 
768
/* Get standard status (6 bytes).
769
 * The `.dec' and `.urc' fields are in MSB-first byte-order,
770
 * so they have to be swapped first.
771
 */
772
static int get_status(volatile struct tpstatus *stp)
773
{
774
        int stat = rdstatus((char *) stp, TPSTATSIZE, QCMD_RD_STAT);
775
#if defined(__i386__) || defined (__x86_64__)
776
        byte_swap_w(&(stp->dec));
777
        byte_swap_w(&(stp->urc));
778
#else
779
#warning Undefined architecture
780
        /* should probably swap status bytes #definition */
781
#endif
782
        return stat;
783
}                               /* get_status */
784
 
785
 
786
#if 0
787
/* This fails for my Wangtek drive */
788
/* get "Extended Status Register 3" (64 bytes)
789
 *
790
 * If the meaning of the returned bytes were known, the MT_TYPE
791
 * identifier could be used to decode them, since they are
792
 * "vendor unique". :-(
793
 */
794
static int get_ext_status3(void)
795
{
796
        char vus[64];           /* vendor unique status */
797
        int stat, i;
798
 
799
        tpqputs(TPQD_ALWAYS, "Attempting to read Extended Status 3...");
800
        stat = rdstatus(vus, sizeof(vus), QCMD_RD_STAT_X3);
801
        if (stat != TE_OK)
802
                return stat;
803
 
804
        tpqputs(TPQD_ALWAYS, "Returned status bytes:");
805
        for (i = 0; i < sizeof(vus); i++) {
806
                if (i % 8 == 0)
807
                        printk("\n" TPQIC02_NAME ": %2d:");
808
                printk(" %2x", vus[i] & 0xff);
809
        }
810
        printk("\n");
811
 
812
        return TE_OK;
813
}                               /* get_ext_status3 */
814
#endif
815
 
816
 
817
/* Read drive status and set generic status too.
818
 * NOTE: Once we do a tp_sense(), read/write transfers are killed.
819
 */
820
static int tp_sense(int ignore)
821
{
822
        unsigned err = 0, exnr = 0, gs = 0;
823
        static void finish_rw(int cmd);
824
 
825
        if (TPQDBG(SENSE_TEXT))
826
                printk(TPQIC02_NAME ": tp_sense(ignore=0x%x) enter\n",
827
                       ignore);
828
 
829
        /* sense() is not allowed during a read or write cycle */
830
        if (doing_write == YES)
831
                tpqputs(TPQD_ALWAYS, "Warning: File Mark inserted because of sense() request");
832
        /* The extra test is to avoid calling finish_rw during booting */
833
        if ((doing_read != NO) || (doing_write != NO))
834
                finish_rw(QCMD_RD_STAT);
835
 
836
        if (get_status(&tperror) != TE_OK) {
837
                tpqputs(TPQD_ALWAYS, "tp_sense: could not read tape drive status");
838
                return TE_ERR;
839
        }
840
 
841
        err = tperror.exs;      /* get exception status bits */
842
        if (err & (TP_ST0 | TP_ST1))
843
                printk(TPQIC02_NAME ": tp_sense: status: %x, error count: %d, underruns: %d\n",
844
                       tperror.exs, tperror.dec, tperror.urc);
845
        else if ((tperror.dec != 0) || (tperror.urc != 0)
846
                 || TPQDBG(SENSE_CNTS))
847
                printk(TPQIC02_NAME
848
                       ": tp_sense: no hard errors, soft error count: %d, underruns: %d\n",
849
                       tperror.dec, tperror.urc);
850
 
851
        /* Set generic status. HP-UX defines these, but some extra would
852
         * be useful. Problem is to remain compatible. [Do we want to be
853
         * compatible??]
854
         */
855
        if (err & TP_ST0) {
856
                if (err & TP_CNI)       /* no cartridge */
857
                        gs |= GMT_DR_OPEN(-1);
858
                if (status_dead == NO)
859
                        gs |= GMT_ONLINE(-1);   /* always online */
860
                if (err & TP_USL)       /* not online */
861
                        gs &= ~GMT_ONLINE(-1);
862
                if (err & TP_WRP)
863
                        gs |= GMT_WR_PROT(-1);
864
                if (err & TP_EOM) {     /* end of media */
865
                        gs |= GMT_EOT(-1);      /* not sure this is correct for writes */
866
                        status_eom_detected = YES;
867
                        /* I don't know whether drive always reports EOF at or before EOM. */
868
                        status_eof_detected = YES;
869
                }
870
                /** if (err & TP_UDA) "Unrecoverable data error" **/
871
                /** if (err & TP_BNL) "Bad block not located" **/
872
                if (err & TP_FIL) {
873
                        gs |= GMT_EOF(-1);
874
                        status_eof_detected = YES;
875
                }
876
        }
877
        if (err & TP_ST1) {
878
                /** if (err & TP_ILL) "Illegal command" **/
879
                /** if (err & TP_NDT) "No data detected" **/
880
                /** if (err & TP_MBD) "Marginal block detected" **/
881
                if (err & TP_BOM)
882
                        gs |= GMT_BOT(-1);      /* beginning of tape */
883
        }
884
        ioctl_status.mt_gstat = gs;
885
        ioctl_status.mt_dsreg = tperror.exs;    /* "drive status" */
886
        ioctl_status.mt_erreg = tperror.dec;    /* "sense key error" */
887
 
888
        if (err & (TP_ST0 | TP_ST1)) {
889
                /* My Wangtek occasionally reports `status' 1212 which should be ignored. */
890
                exnr = decode_qic_exception_nr(err);
891
                handle_qic_exception(exnr, err);        /* update driver state wrt drive status */
892
                report_qic_exception(exnr);
893
        }
894
        err &= ~ignore;         /* mask unwanted errors -- not the correct way, use exception nrs?? */
895
        if (((err & TP_ST0) && (err & REPORT_ERR0)) ||
896
            ((err & TP_ST1) && (err & REPORT_ERR1)))
897
                return TE_ERR;
898
        return TE_OK;
899
}                               /* tp_sense */
900
 
901
 
902
 
903
/* Wait for a wind or rewind operation to finish or
904
 * to time-out. (May take very long).
905
 */
906
static int wait_for_rewind(time_t timeout)
907
{
908
        int stat;
909
 
910
        stat = inb(QIC02_STAT_PORT) & QIC02_STAT_MASK;
911
        if (TPQDBG(REWIND))
912
                printk(TPQIC02_NAME
913
                       ": Waiting for (re-)wind to finish: stat=0x%x\n",
914
                       stat);
915
 
916
        stat = wait_for_ready(timeout);
917
 
918
        if (stat != TE_OK) {
919
                tpqputs(TPQD_ALWAYS, "(re-) winding failed\n");
920
        }
921
        return stat;
922
}                               /* wait_for_rewind */
923
 
924
 
925
 
926
/* Perform a full QIC02 command, and wait for completion,
927
 * check status when done. Complain about exceptions.
928
 *
929
 * This function should return an OS error code when
930
 * something goes wrong, 0 otherwise.
931
 */
932
static int ll_do_qic_cmd(int cmd, time_t timeout)
933
{
934
        int stat;
935
 
936
        if (status_dead == YES) {
937
                tpqputs(TPQD_ALWAYS, "Drive is dead. Do a `mt reset`.");
938
                return -ENXIO;  /* User should do an MTRESET. */
939
        }
940
 
941
        stat = wait_for_ready(timeout); /* wait for ready or exception */
942
        if (stat == TE_EX) {
943
                if (tp_sense(TP_WRP | TP_BOM | TP_EOM | TP_FIL) != TE_OK)
944
                        return -EIO;
945
                /* else nothing to worry about, I hope */
946
                stat = TE_OK;
947
        }
948
        if (stat != TE_OK) {
949
                printk(TPQIC02_NAME ": ll_do_qic_cmd(%x, %ld) failed\n",
950
                       cmd, (long) timeout);
951
                return -EIO;
952
        }
953
#if OBSOLETE
954
        /* wait for ready since it may not be active immediately after reading status */
955
        while ((inb_p(QIC02_STAT_PORT) & QIC02_STAT_READY) != 0);
956
#endif
957
 
958
        stat = send_qic02_cmd(cmd, timeout, 0);  /* (checks for exceptions) */
959
 
960
        if (cmd == QCMD_RD_FM) {
961
                status_eof_detected = NO;
962
                ioctl_status.mt_fileno++;
963
                /* Should update block count as well, but can't.
964
                 * Can do a `read address' for some drives, when MTNOP is done.
965
                 */
966
        } else if (cmd == QCMD_WRT_FM) {
967
                status_eof_detected = NO;
968
                ioctl_status.mt_fileno++;
969
        } else if ((cmd == QCMD_REWIND) || (cmd == QCMD_ERASE)
970
                   || (cmd == QCMD_RETEN)) {
971
                status_eof_detected = NO;
972
                status_eom_detected = NO;
973
                status_eot_detected = NO;
974
                need_rewind = NO;
975
                ioctl_status.mt_fileno = ioctl_status.mt_blkno = 0;
976
                extra_blocks_left = BLOCKS_BEYOND_EW;
977
                return_write_eof = NO;
978
                return_read_eof = NO;
979
                reported_read_eof = NO;
980
                reported_write_eof = NO;
981
        }
982
        /* sense() will set eof/eom as required */
983
        if (stat == TE_EX) {
984
                if (tp_sense(TP_WRP | TP_BOM | TP_EOM | TP_FIL) != TE_OK) {
985
                        printk(TPQIC02_NAME
986
                               ": Exception persist in ll_do_qic_cmd[1](%x, %ld)",
987
                               cmd, (long) timeout);
988
                        status_dead = YES;
989
                        return -ENXIO;
990
                        /* if rdstatus fails too, we're in trouble */
991
                }
992
        } else if (stat != TE_OK) {
993
                printk(TPQIC02_NAME
994
                       ": ll_do_qic_cmd: send_qic02_cmd failed, stat = 0x%x\n",
995
                       stat);
996
                return -EIO;    /*** -EIO is probably not always appropriate */
997
        }
998
 
999
 
1000
        if (timeout == TIM_R)
1001
                stat = wait_for_rewind(timeout);
1002
        else
1003
                stat = wait_for_ready(timeout);
1004
 
1005
        if (stat == TE_EX) {
1006
                if (tp_sense((cmd == QCMD_SEEK_EOD ?            /*****************************/
1007
                              TP_EOR | TP_NDT | TP_UDA | TP_BNL | TP_WRP |
1008
                              TP_BOM | TP_EOM | TP_FIL : TP_WRP | TP_BOM |
1009
                              TP_EOM | TP_FIL)) != TE_OK) {
1010
                        printk(TPQIC02_NAME
1011
                               ": Exception persist in ll_do_qic_cmd[2](%x, %ld)\n",
1012
                               cmd, (long) timeout);
1013
                        if (cmd != QCMD_RD_FM)
1014
                                status_dead = YES;
1015
                        return -ENXIO;
1016
                        /* if rdstatus fails too, we're in trouble */
1017
                }
1018
        } else if (stat != TE_OK) {
1019
                printk(TPQIC02_NAME
1020
                       ": ll_do_qic_cmd %x: wait failed, stat == 0x%x\n",
1021
                       cmd, stat);
1022
                return -EIO;
1023
        }
1024
        return 0;
1025
}                               /* ll_do_qic_cmd */
1026
 
1027
 
1028
/*
1029
 * Problem: What to do when the user cancels a read/write operation
1030
 * in-progress?
1031
 *
1032
 * "Deactivating ONLINE during a READ also causes the"
1033
 * "tape to be rewound to BOT." Ditto for WRITEs, except
1034
 * a FM is written first. "The host may alternatively terminate
1035
 * the READ/WRITE command by issuing a RFM/WFM command."
1036
 *
1037
 * For READs:
1038
 * Neither option will leave the tape positioned where it was.
1039
 * Another (better?) solution is to terminate the READ by two
1040
 * subsequent sense() operations, the first to stop the current
1041
 * READ cycle, the second to clear the `Illegal command' exception,
1042
 * because the QIC-02 specs didn't anticipate this. This is
1043
 * delayed until actually needed, so a tar listing can be aborted
1044
 * by the user and continued later.
1045
 * If anybody has a better solution, let me know! [Also, let me
1046
 * know if your drive (mine is a Wangtek5150EQ) does not accept
1047
 * this sequence for canceling the read-cycle.]
1048
 *
1049
 * For WRITEs it's simple: Just do a WRITE_FM, leaving the tape
1050
 * positioned after the FM.
1051
 */
1052
 
1053
static void terminate_read(int cmd)
1054
{
1055
        if (doing_read == YES) {
1056
                doing_read = NO;
1057
                if (cmd != QCMD_RD_FM) {
1058
                        /* if the command is a RFM, there is no need to do this
1059
                         * because a RFM will legally terminate the read-cycle.
1060
                         */
1061
                        tpqputs(TPQD_ALWAYS, "terminating pending read-cycle");
1062
 
1063
                        /* I'm not too sure about this part  -- hhb */
1064
                        if (QIC02_TAPE_IFC == MOUNTAIN) {
1065
                                /* Mountain reference says can terminate by de-asserting online */
1066
                                ctlbits &= ~MTN_QIC02_CTL_ONLINE;
1067
                        }
1068
 
1069
                        if (tp_sense(TP_FIL | TP_EOM | TP_WRP) != TE_OK) {
1070
                                tpqputs(TPQD_ALWAYS,
1071
                                        "finish_rw[read1]: ignore the 2 lines above");
1072
                                if (is_exception()) {
1073
                                        if (tp_sense
1074
                                            (TP_ILL | TP_FIL | TP_EOM |
1075
                                             TP_WRP) != TE_OK)
1076
                                                tpqputs(TPQD_ALWAYS,"finish_rw[read2]: read cycle error");
1077
                                }
1078
                        }
1079
                }
1080
        }
1081
}                               /* terminate_read */
1082
 
1083
 
1084
static void terminate_write(int cmd)
1085
{
1086
        int stat;
1087
 
1088
        if (doing_write == YES) {
1089
                doing_write = NO;
1090
                /* Finish writing by appending a FileMark at the end. */
1091
                if (cmd != QCMD_WRT_FM) {
1092
                        /* finish off write cycle */
1093
                        stat = ll_do_qic_cmd(QCMD_WRT_FM, TIM_M);
1094
                        if (stat != TE_OK)
1095
                                tpqputs(TPQD_ALWAYS,
1096
                                        "Couldn't finish write cycle properly");
1097
                        (void) tp_sense(0);
1098
                }
1099
                /* If there is an EOF token waiting to be returned to
1100
                 * the (writing) application, discard it now.
1101
                 * We could be at EOT, so don't reset return_write_eof.
1102
                 */
1103
                reported_write_eof = YES;
1104
        }
1105
}                               /* terminate_write */
1106
 
1107
 
1108
/* terminate read or write cycle because of command `cmd' */
1109
static void finish_rw(int cmd)
1110
{
1111
        if (wait_for_ready(TIM_S) != TE_OK) {
1112
                tpqputs(TPQD_ALWAYS,
1113
                        "error: drive not ready in finish_rw() !");
1114
                return;
1115
        }
1116
        terminate_read(cmd);
1117
        terminate_write(cmd);
1118
}                               /* finish_rw */
1119
 
1120
 
1121
/* Perform a QIC command through ll_do_qic_cmd().
1122
 * If necessary, rewind the tape first.
1123
 * Return an OS error code if something goes wrong, 0 if all is well.
1124
 */
1125
static int do_qic_cmd(int cmd, time_t timeout)
1126
{
1127
        int stat;
1128
 
1129
 
1130
        finish_rw(cmd);
1131
 
1132
        if (need_rewind) {
1133
                tpqputs(TPQD_REWIND, "Rewinding tape...");
1134
                stat = ll_do_qic_cmd(QCMD_REWIND, TIM_R);
1135
                if (stat != 0) {
1136
                        printk(TPQIC02_NAME ": rewind failed in do_qic_cmd(). stat=0x%2x", stat);
1137
                        return stat;
1138
                }
1139
                need_rewind = NO;
1140
                if (cmd == QCMD_REWIND) /* don't wind beyond BOT ;-) */
1141
                        return 0;
1142
        }
1143
 
1144
        return ll_do_qic_cmd(cmd, timeout);
1145
}                               /* do_qic_cmd */
1146
 
1147
 
1148
/* Not all ioctls are supported for all drives. Some rely on
1149
 * optional QIC-02 commands. Check tpqic02.h for configuration.
1150
 * Some of these commands may require ONLINE to be active.
1151
 */
1152
static int do_ioctl_cmd(int cmd)
1153
{
1154
        int stat;
1155
 
1156
        /* It is not permitted to read or wind the tape after bytes have
1157
         * been written. It is not permitted to write the tape while in
1158
         * read mode.
1159
         * We try to be kind and allow reading again after writing a FM...
1160
         */
1161
 
1162
        switch (cmd) {
1163
        case MTRESET:
1164
                /* reset verbose */
1165
                return (tape_reset(1) == TE_OK) ? 0 : -EIO;
1166
 
1167
        case MTFSF:
1168
                tpqputs(TPQD_IOCTLS, "MTFSF forward searching filemark");
1169
                if ((mode_access == WRITE) && status_bytes_wr)
1170
                        return -EACCES;
1171
                return do_qic_cmd(QCMD_RD_FM, TIM_F);
1172
 
1173
        case MTBSF:
1174
                if (TP_HAVE_BSF) {
1175
                        tpqputs(TPQD_IOCTLS,
1176
                                "MTBSF backward searching filemark -- optional command");
1177
                        if ((mode_access == WRITE) && status_bytes_wr)
1178
                                return -EACCES;
1179
                        stat = do_qic_cmd(QCMD_RD_FM_BCK, TIM_F);
1180
                } else {
1181
                        stat = -ENXIO;
1182
                }
1183
                status_eom_detected = status_eof_detected = NO;
1184
                return stat;
1185
 
1186
        case MTFSR:
1187
                if (TP_HAVE_FSR) {      /* This is an optional QIC-02 command */
1188
                        tpqputs(TPQD_IOCTLS, "MTFSR forward space record");
1189
                        if ((mode_access == WRITE) && status_bytes_wr)
1190
                                return -EACCES;
1191
                        stat = do_qic_cmd(QCMD_SPACE_FWD, TIM_F);
1192
                } else {
1193
                                /**** fake it by doing a read data block command? ******/
1194
                        tpqputs(TPQD_IOCTLS, "MTFSR not supported");
1195
                        stat = -ENXIO;
1196
                }
1197
                return stat;
1198
 
1199
        case MTBSR:
1200
                if (TP_HAVE_BSR) {      /* This is an optional QIC-02 command */
1201
                        /* we need this for appending files with GNU tar!! */
1202
                        tpqputs(TPQD_IOCTLS, "MTFSR backward space record");
1203
                        if ((mode_access == WRITE) && status_bytes_wr)
1204
                                return -EACCES;
1205
                        stat = do_qic_cmd(QCMD_SPACE_BCK, TIM_F);
1206
                } else {
1207
                        tpqputs(TPQD_IOCTLS, "MTBSR not supported");
1208
                        stat = -ENXIO;
1209
                }
1210
                status_eom_detected = status_eof_detected = NO;
1211
                return stat;
1212
 
1213
        case MTWEOF:
1214
                tpqputs(TPQD_IOCTLS, "MTWEOF write eof mark");
1215
                /* Plain GNU mt(1) 2.2 uses read-only mode for writing FM. :-( */
1216
                if (mode_access == READ)
1217
                        return -EACCES;
1218
 
1219
                /* allow tape movement after writing FM */
1220
                status_bytes_rd = status_bytes_wr;      /* Kludge-O-Matic */
1221
                status_bytes_wr = NO;
1222
                return do_qic_cmd(QCMD_WRT_FM, TIM_M);
1223
                /* not sure what to do with status_bytes when WFM should fail */
1224
 
1225
        case MTREW:
1226
                tpqputs(TPQD_IOCTLS, "MTREW rewinding tape");
1227
                if ((mode_access == WRITE) && status_bytes_wr)
1228
                        return -EACCES;
1229
                status_eom_detected = status_eof_detected = NO;
1230
                return do_qic_cmd(QCMD_REWIND, TIM_R);
1231
 
1232
        case MTOFFL:
1233
                tpqputs(TPQD_IOCTLS, "MTOFFL rewinding & going offline");
1234
                /* Doing a drive select will clear (unlock) the current drive.
1235
                 * But that requires support for multiple drives and locking.
1236
                 */
1237
                if ((mode_access == WRITE) && status_bytes_wr)
1238
                        return -EACCES;
1239
                status_eom_detected = status_eof_detected = NO;
1240
                        /**** do rewind depending on minor bits??? ***/
1241
                stat = do_qic_cmd(QCMD_REWIND, TIM_R);
1242
                return stat;
1243
 
1244
        case MTNOP:
1245
                tpqputs(TPQD_IOCTLS, "MTNOP setting status only");
1246
                        /********** should do `read position' for drives that support it **********/
1247
                return (tp_sense(-1) == TE_OK) ? 0 : -EIO;       /**** check return codes ****/
1248
 
1249
        case MTRETEN:
1250
                tpqputs(TPQD_IOCTLS, "MTRETEN retension tape");
1251
                if ((mode_access == WRITE) && status_bytes_wr)
1252
                        return -EACCES;
1253
                status_eom_detected = status_eof_detected = NO;
1254
                return do_qic_cmd(QCMD_RETEN, TIM_R);
1255
 
1256
        case MTBSFM:
1257
                /* Think think is like MTBSF, except that
1258
                 * we shouldn't skip the FM. Tricky.
1259
                 * Maybe use RD_FM_BCK, then do a SPACE_FWD?
1260
                 */
1261
                tpqputs(TPQD_IOCTLS, "MTBSFM not supported");
1262
                if ((mode_access == WRITE) && status_bytes_wr)
1263
                        return -EACCES;
1264
                return -ENXIO;
1265
 
1266
        case MTFSFM:
1267
                /* I think this is like MTFSF, except that
1268
                 * we shouldn't skip the FM. Tricky.
1269
                 * Maybe use QCMD_RD_DATA until we get a TP_FIL exception?
1270
                 * But then the FM will have been skipped...
1271
                 * Maybe use RD_FM, then RD_FM_BCK, but not all
1272
                 * drives will support that!
1273
                 */
1274
                tpqputs(TPQD_IOCTLS, "MTFSFM not supported");
1275
                if ((mode_access == WRITE) && status_bytes_wr)
1276
                        return -EACCES;
1277
                return -ENXIO;
1278
 
1279
        case MTEOM:
1280
                /* This should leave the tape ready for appending
1281
                 * another file to the end, such that it would append
1282
                 * after the last FM on tape.
1283
                 */
1284
                tpqputs(TPQD_IOCTLS, "MTEOM search for End Of recorded Media");
1285
                if ((mode_access == WRITE) && status_bytes_wr)
1286
                        return -EACCES;
1287
                if (TP_HAVE_EOD) {
1288
                        /* Use faster seeking when possible.
1289
                         * This requires the absence of data beyond the EOM.
1290
                         * It seems that my drive does not always perform the
1291
                         * SEEK_EOD correctly, unless it is preceded by a
1292
                         * rewind command.
1293
                         */
1294
# if 0
1295
                        status_eom_detected = status_eof_detected = NO;
1296
# endif
1297
                        stat = do_qic_cmd(QCMD_REWIND, TIM_R);
1298
                        if (stat)
1299
                                return stat;
1300
                        stat = do_qic_cmd(QCMD_SEEK_EOD, TIM_F);
1301
                        /* After a successful seek, TP_EOR should be returned */
1302
                } else {
1303
                        /* else just seek until the drive returns exception "No Data" */
1304
                        stat = 0;
1305
                        while ((stat == 0) && (!status_eom_detected)) {
1306
                                stat = do_qic_cmd(QCMD_RD_FM, TIM_F);         /***** should use MTFSFM here???? ******/
1307
                        }
1308
                        if (tperror.exs & TP_NDT)
1309
                                return 0;
1310
                }
1311
                return stat;
1312
 
1313
        case MTERASE:
1314
                tpqputs(TPQD_IOCTLS, "MTERASE -- ERASE TAPE !");
1315
                if ((tperror.exs & TP_ST0) && (tperror.exs & TP_WRP)) {
1316
                        tpqputs(TPQD_ALWAYS, "Cartridge is write-protected.");
1317
                        return -EACCES;
1318
                } else {
1319
                        time_t t = jiffies;
1320
 
1321
                        /* Plain GNU mt(1) 2.2 erases a tape in O_RDONLY. :-( */
1322
                        if (mode_access == READ)
1323
                                return -EACCES;
1324
 
1325
                        /* FIXME */
1326
                        /* give user a few seconds to pull out tape */
1327
                        while (jiffies - t < 4 * HZ)
1328
                                schedule();
1329
                }
1330
 
1331
                /* don't bother writing filemark first */
1332
                status_eom_detected = status_eof_detected = NO;
1333
                return do_qic_cmd(QCMD_ERASE, TIM_R);
1334
 
1335
        case MTRAS1:
1336
                if (TP_HAVE_RAS1) {
1337
                        tpqputs(TPQD_IOCTLS, "MTRAS1: non-destructive self test");
1338
                        stat = do_qic_cmd(QCMD_SELF_TST1, TIM_R);
1339
                        if (stat != 0) {
1340
                                tpqputs(TPQD_ALWAYS, "RAS1 failed");
1341
                                return stat;
1342
                        }
1343
                        return (tp_sense(0) == TE_OK) ? 0 : -EIO; /* get_ext_status3(); */
1344
                }
1345
                tpqputs(TPQD_IOCTLS, "RAS1 not supported");
1346
                return -ENXIO;
1347
 
1348
        case MTRAS2:
1349
                if (TP_HAVE_RAS2) {
1350
                        tpqputs(TPQD_IOCTLS, "MTRAS2: destructive self test");
1351
                        stat = do_qic_cmd(QCMD_SELF_TST2, TIM_R);
1352
                        if (stat != 0) {
1353
                                tpqputs(TPQD_ALWAYS, "RAS2 failed");
1354
                                return stat;
1355
                        }
1356
                        return (tp_sense(0) == TE_OK) ? 0 : -EIO; /* get_ext_status3(); */
1357
                }
1358
                tpqputs(TPQD_IOCTLS, "RAS2 not supported");
1359
                return -ENXIO;
1360
 
1361
        case MTSEEK:
1362
                if (TP_HAVE_SEEK && (QIC02_TAPE_IFC == ARCHIVE)) {
1363
                        tpqputs(TPQD_IOCTLS, "MTSEEK seeking block");
1364
                        if ((mode_access == WRITE) && status_bytes_wr)
1365
                                return -EACCES;
1366
                        /* NOTE: address (24 bits) is in seek_addr_buf[] */
1367
                        return do_qic_cmd(AR_QCMDV_SEEK_BLK, TIM_F);
1368
                } else
1369
                        return -ENOTTY;
1370
 
1371
        default:
1372
                return -ENOTTY;
1373
        }
1374
}                               /* do_ioctl_cmd */
1375
 
1376
 
1377
/* dma_transfer(): This routine is called for every 512 bytes to be read
1378
 * from/written to the tape controller. Speed is important here!
1379
 * (There must be enough time left for the hd controller!)
1380
 * When other devices use DMA they must ensure they use un-interruptible
1381
 * double byte accesses to the DMA controller. Floppy.c is ok.
1382
 * Must have interrupts disabled when this function is invoked,
1383
 * otherwise, the double-byte transfers to the DMA controller will not
1384
 * be atomic. That could lead to nasty problems when they are interrupted
1385
 * by other DMA interrupt-routines.
1386
 *
1387
 * This routine merely does the least possible to keep
1388
 * the transfers going:
1389
 *      - set the DMA count register for the next 512 bytes
1390
 *      - adjust the DMA address and page registers
1391
 *      - adjust the timeout
1392
 *      - tell the tape controller to start transferring
1393
 * We assume the dma address and mode are, and remain, valid.
1394
 */
1395
static inline void dma_transfer(void)
1396
{
1397
        unsigned long flags;
1398
 
1399
        if (QIC02_TAPE_IFC == WANGTEK)  /* or EVEREX */
1400
                outb_p(WT_CTL_ONLINE, QIC02_CTL_PORT);  /* back to normal */
1401
        else if (QIC02_TAPE_IFC == ARCHIVE)
1402
                outb_p(0, AR_RESET_DMA_PORT);
1403
        else                    /* QIC02_TAPE_IFC == MOUNTAIN */
1404
                outb_p(ctlbits, QIC02_CTL_PORT);
1405
 
1406
 
1407
        flags = claim_dma_lock();
1408
        clear_dma_ff(QIC02_TAPE_DMA);
1409
        set_dma_mode(QIC02_TAPE_DMA, dma_mode);
1410
        set_dma_addr(QIC02_TAPE_DMA,
1411
                     virt_to_bus(buffaddr) + dma_bytes_done);
1412
        set_dma_count(QIC02_TAPE_DMA, TAPE_BLKSIZE);
1413
 
1414
        /* start tape DMA controller */
1415
        if (QIC02_TAPE_IFC == WANGTEK)  /* or EVEREX */
1416
                outb_p(WT_CTL_DMA | WT_CTL_ONLINE, QIC02_CTL_PORT);     /* trigger DMA transfer */
1417
 
1418
        else if (QIC02_TAPE_IFC == ARCHIVE) {
1419
                outb_p(AR_CTL_IEN | AR_CTL_DNIEN, QIC02_CTL_PORT);      /* enable interrupts again */
1420
                outb_p(0, AR_START_DMA_PORT);    /* start DMA transfer */
1421
                /* In dma_end() AR_RESET_DMA_PORT is written too. */
1422
 
1423
        } else {                /* QIC02_TAPE_IFC == MOUNTAIN */
1424
 
1425
                inb(MTN_R_DESELECT_DMA_PORT);
1426
                outb_p(ctlbits | (MTN_CTL_EXC_IEN | MTN_CTL_DNIEN),
1427
                       QIC02_CTL_PORT);
1428
                outb_p(0, MTN_W_SELECT_DMA_PORT);        /* start DMA transfer */
1429
                if (dma_mode == DMA_MODE_WRITE)
1430
                        outb_p(0, MTN_W_DMA_WRITE_PORT); /* start DMA transfer */
1431
        }
1432
 
1433
        /* start computer DMA controller */
1434
        enable_dma(QIC02_TAPE_DMA);
1435
 
1436
        release_dma_lock(flags);
1437
 
1438
        /* block transfer should start now, jumping to the
1439
         * interrupt routine when done or an exception was detected.
1440
         */
1441
}                               /* dma_transfer */
1442
 
1443
 
1444
/* start_dma() sets a DMA transfer up between the tape controller and
1445
 * the kernel qic02_tape_buf buffer.
1446
 * Normally bytes_todo==dma_bytes_done at the end of a DMA transfer. If not,
1447
 * a filemark was read, or an attempt to write beyond the End Of Tape
1448
 * was made. [Or some other bad thing happened.]
1449
 * Must do a sense() before returning error.
1450
 */
1451
static int start_dma(short mode, unsigned long bytes_todo)
1452
/* assume 'bytes_todo'>0 */
1453
{
1454
        int stat;
1455
        unsigned long flags;
1456
 
1457
        tpqputs(TPQD_DEBUG, "start_dma() enter");
1458
        TPQDEB( {printk(TPQIC02_NAME ": doing_read==%d, doing_write==%d\n",
1459
                      doing_read, doing_write);})
1460
 
1461
            dma_bytes_done = 0;
1462
        dma_bytes_todo = bytes_todo;
1463
        status_error = NO;
1464
        /* dma_mode!=0 indicates that the dma controller is in use */
1465
        dma_mode = (mode == WRITE) ? DMA_MODE_WRITE : DMA_MODE_READ;
1466
 
1467
        /* Only give READ/WRITE DATA command to tape drive if we haven't
1468
         * done that already. Otherwise the drive will rewind to the beginning
1469
         * of the current file on tape. Any QIC command given other than
1470
         * R/W FM will break the read/write transfer cycle.
1471
         * do_qic_cmd() will terminate doing_{read,write}
1472
         */
1473
        if ((doing_read == NO) && (doing_write == NO)) {
1474
                /* First, we have to clear the status -- maybe remove TP_FIL???
1475
                 */
1476
 
1477
#if 0
1478
                /* Next dummy get status is to make sure CNI is valid,
1479
                   since we're only just starting a read/write it doesn't
1480
                   matter some exceptions are cleared by reading the status;
1481
                   we're only interested in CNI and WRP. -Eddy */
1482
                get_status(&tperror);
1483
#else
1484
                /* TP_CNI should now be handled in open(). -Hennus */
1485
#endif
1486
 
1487
                stat =
1488
                    tp_sense(((mode ==
1489
                               WRITE) ? 0 : TP_WRP) | TP_BOM | TP_FIL);
1490
                if (stat != TE_OK)
1491
                        return stat;
1492
 
1493
#if OBSOLETE
1494
                /************* not needed iff rd_status() would wait for ready!!!!!! **********/
1495
                if (wait_for_ready(TIM_S) != TE_OK) {   /*** not sure this is needed ***/
1496
                        tpqputs(TPQD_ALWAYS,
1497
                                "wait_for_ready failed in start_dma");
1498
                        return -EIO;
1499
                }
1500
#endif
1501
 
1502
                if (QIC02_TAPE_IFC == MOUNTAIN) {
1503
                        /* Set control bits to select ONLINE during command */
1504
                        ctlbits |= MTN_QIC02_CTL_ONLINE;
1505
                }
1506
 
1507
                /* Tell the controller the data direction */
1508
 
1509
                /* r/w, timeout medium, check exceptions, sets status_cmd_pending. */
1510
                stat = send_qic02_cmd((mode == WRITE)
1511
                                        ? QCMD_WRT_DATA : QCMD_RD_DATA, TIM_M, 0);
1512
                if (stat != TE_OK) {
1513
                        printk(TPQIC02_NAME ": start_dma: init %s failed\n",
1514
                               (mode == WRITE) ? "write" : "read");
1515
                        (void) tp_sense(0);
1516
                        return stat;
1517
                }
1518
 
1519
                /* Do this last, because sense() will clear the doing_{read,write}
1520
                 * flags, causing trouble next time around.
1521
                 */
1522
                if (wait_for_ready(TIM_M) != TE_OK)
1523
                        return -EIO;
1524
                switch (mode) {
1525
                case READ:
1526
                        doing_read = YES;
1527
                        break;
1528
                case WRITE:
1529
                        doing_write = YES;
1530
                        break;
1531
                default:
1532
                        printk(TPQIC02_NAME
1533
                               ": requested unknown mode %d\n", mode);
1534
                        panic(TPQIC02_NAME
1535
                              ": invalid mode in start_dma()");
1536
                }
1537
 
1538
        } else if (is_exception()) {
1539
                /* This is for Archive drives, to handle reads with 0 bytes
1540
                 * left for the last read request.
1541
                 *
1542
                 * ******** this also affects EOF/EOT handling! ************
1543
                 */
1544
                tpqputs(TPQD_ALWAYS,
1545
                        "detected exception in start_dma() while transfer in progress");
1546
                status_error = YES;
1547
                return TE_END;
1548
        }
1549
 
1550
 
1551
        status_expect_int = YES;
1552
 
1553
        /* This assumes tape is already positioned, but these
1554
         * semi-'intelligent' drives are unpredictable...
1555
         */
1556
        TIMERON(TIM_M * 2);
1557
 
1558
        /* initiate first data block read from/write to the tape controller */
1559
 
1560
        save_flags(flags);
1561
        cli();
1562
        dma_transfer();
1563
        restore_flags(flags);
1564
 
1565
        TPQPUTS("start_dma() end");
1566
        return TE_OK;
1567
}                               /* start_dma */
1568
 
1569
 
1570
/* This cleans up after the dma transfer has completed
1571
 * (or failed). If an exception occurred, a sense()
1572
 * must be done. If the exception was caused by a FM,
1573
 * sense() will set `status_eof_detected' and
1574
 * `status_eom_detected', as required.
1575
 */
1576
static void end_dma(unsigned long *bytes_done)
1577
{
1578
        int stat = TE_OK;
1579
        unsigned long flags;
1580
 
1581
        TIMEROFF;
1582
 
1583
        TPQPUTS("end_dma() enter");
1584
 
1585
        flags = claim_dma_lock();
1586
 
1587
        disable_dma(QIC02_TAPE_DMA);
1588
        clear_dma_ff(QIC02_TAPE_DMA);
1589
 
1590
        release_dma_lock(flags);
1591
 
1592
        if (QIC02_TAPE_IFC == WANGTEK)  /* or EVEREX */
1593
                outb_p(WT_CTL_ONLINE, QIC02_CTL_PORT);  /* back to normal */
1594
        else if (QIC02_TAPE_IFC == ARCHIVE)
1595
                outb_p(0, AR_RESET_DMA_PORT);
1596
        else {                  /* QIC02_TAPE_IFC == MOUNTAIN */
1597
 
1598
                /* Clear control bits, de-select ONLINE during tp_sense */
1599
                ctlbits &= ~MTN_QIC02_CTL_ONLINE;
1600
        }
1601
 
1602
        stat = wait_for_ready(TIM_M);
1603
        if (status_error || (stat != TE_OK)) {
1604
                tpqputs(TPQD_DMAX, "DMA transfer exception");
1605
                stat = tp_sense((dma_mode == READ) ? TP_WRP : 0);
1606
                /* no return here -- got to clean up first! */
1607
        } else {                /* if (QIC02_TAPE_IFC == MOUNTAIN) */
1608
 
1609
                outb_p(ctlbits, QIC02_CTL_PORT);
1610
        }
1611
 
1612
        if (QIC02_TAPE_IFC == MOUNTAIN)
1613
                inb(MTN_R_DESELECT_DMA_PORT);
1614
 
1615
        /* take the tape controller offline */
1616
 
1617
        /* finish off DMA stuff */
1618
 
1619
 
1620
        dma_mode = 0;
1621
        /* Note: The drive is left on-line, ready for the next
1622
         * data transfer.
1623
         * If the next command to the drive does not continue
1624
         * the pending cycle, it must do 2 sense()s first.
1625
         */
1626
 
1627
        *bytes_done = dma_bytes_done;
1628
        status_expect_int = NO;
1629
        ioctl_status.mt_blkno += (dma_bytes_done / TAPE_BLKSIZE);
1630
 
1631
        TPQPUTS("end_dma() exit");
1632
        /*** could return stat here ***/
1633
}                               /* end_dma */
1634
 
1635
/*********** Below are the (public) OS-interface procedures ***********/
1636
 
1637
 
1638
/* qic02_tape_times_out() is called when a DMA transfer doesn't complete
1639
 * quickly enough. Usually this means there is something seriously wrong
1640
 * with the hardware/software, but it could just be that the controller
1641
 * has decided to do a long rewind, just when I didn't expect it.
1642
 * Just try again.
1643
 */
1644
static void qic02_tape_times_out(unsigned long dummy)
1645
{
1646
        printk("time-out in %s driver\n", TPQIC02_NAME);
1647
        if ((status_cmd_pending > 0) || dma_mode) {
1648
                /* takes tooo long, shut it down */
1649
                status_dead = YES;
1650
                status_cmd_pending = 0;
1651
                status_timer_on = NO;
1652
                status_expect_int = NO;
1653
                status_error = YES;
1654
                if (dma_mode) {
1655
                        dma_mode = 0;    /* signal end to read/write routine */
1656
                        wake_up(&qic02_tape_transfer);
1657
                }
1658
        }
1659
}                               /* qic02_tape_times_out */
1660
 
1661
/*
1662
 * Interrupt handling:
1663
 *
1664
 * 1) Interrupt is generated iff at the end of
1665
 *    a 512-DMA-block transfer.
1666
 * 2) EXCEPTION is not raised unless something
1667
 *    is wrong or EOT/FM is detected.
1668
 * 3) FM EXCEPTION is set *after* the last byte has
1669
 *    been transferred by DMA. By the time the interrupt
1670
 *    is handled, the EXCEPTION may already be set.
1671
 *
1672
 * So,
1673
 * 1) On EXCEPTION, assume data has been transferred, so
1674
 *    continue as usual, but set a flag to indicate the
1675
 *    exception was detected.
1676
 *    Do a sense status when the flag is found set.
1677
 * 2) Do not attempt to continue a transfer after an exception.
1678
 *    [??? What about marginal blocks???????]
1679
 */
1680
 
1681
 
1682
/* qic02_tape_interrupt() is called when the tape controller completes
1683
 * a DMA transfer.
1684
 * We are not allowed to sleep here!
1685
 *
1686
 * Check if the transfer was successful, check if we need to transfer
1687
 * more. If the buffer contains enough data/is empty enough, signal the
1688
 * read/write() thread to copy to/from user space.
1689
 * When we are finished, set flags to indicate end, disable timer.
1690
 * NOTE: This *must* be fast!
1691
 */
1692
static void qic02_tape_interrupt(int irq, void *dev_id,
1693
                                 struct pt_regs *regs)
1694
{
1695
        int stat, r, i;
1696
        unsigned long flags;
1697
 
1698
        TIMEROFF;
1699
 
1700
        if (status_expect_int) {
1701
#ifdef WANT_EXTRA_FULL_DEBUGGING
1702
                if (TP_DIAGS(current_tape_dev))
1703
                        printk("@");
1704
#endif
1705
                stat = inb(QIC02_STAT_PORT);    /* Knock, knock */
1706
                if (QIC02_TAPE_IFC == ARCHIVE) {        /* "Who's there?" */
1707
                        if (((stat & (AR_STAT_DMADONE)) == 0) &&
1708
                            ((stat & (QIC02_STAT_EXCEPTION)) != 0)) {
1709
                                TIMERCONT;
1710
                                return; /* "Linux with IRQ sharing" */
1711
                        }
1712
                }
1713
 
1714
                if ((stat & QIC02_STAT_EXCEPTION) == 0) {        /* exception occurred */
1715
                        /* Possible causes for an exception during a transfer:
1716
                         *      - during a write-cycle: end of tape (EW) hole detected.
1717
                         *      - during a read-cycle: filemark or EOD detected.
1718
                         *      - something went wrong
1719
                         * So don't continue with the next block.
1720
                         */
1721
                        tpqputs(TPQD_ALWAYS,
1722
                                "isr: exception on tape controller");
1723
                        printk("      status %02x\n", stat);
1724
                        status_error = TE_EX;
1725
 
1726
                        dma_bytes_done += TAPE_BLKSIZE;
1727
 
1728
                        dma_mode = 0;    /* wake up rw() */
1729
                        status_expect_int = NO;
1730
                        wake_up(&qic02_tape_transfer);
1731
                        return;
1732
                }
1733
                /* return if tape controller not ready, or
1734
                 * if dma channel hasn't finished last byte yet.
1735
                 */
1736
                r = 0;
1737
 
1738
                /* Skip next ready check for Archive controller because
1739
                 * it may be busy reading ahead. Weird. --hhb
1740
                 */
1741
                if (QIC02_TAPE_IFC == WANGTEK)  /* I think this is a drive-dependency, not IFC -- hhb */
1742
                        if (stat & QIC02_STAT_READY) {  /* not ready */
1743
                                tpqputs(TPQD_ALWAYS,
1744
                                        "isr: ? Tape controller not ready");
1745
                                r = 1;
1746
                        }
1747
 
1748
                flags = claim_dma_lock();
1749
 
1750
                if ((i = get_dma_residue(QIC02_TAPE_DMA)) != 0) {
1751
                        printk(TPQIC02_NAME ": dma_residue == %x !!!\n",
1752
                               i);
1753
                        r = 1;  /* big trouble, but can't do much about it... */
1754
                }
1755
 
1756
                release_dma_lock(flags);
1757
 
1758
                if (r)
1759
                        return;
1760
 
1761
                /* finish DMA cycle */
1762
 
1763
                /* no errors detected, continue */
1764
                dma_bytes_done += TAPE_BLKSIZE;
1765
                if (dma_bytes_done >= dma_bytes_todo) {
1766
                        /* finished! Wakeup rw() */
1767
                        dma_mode = 0;
1768
                        status_expect_int = NO;
1769
                        TPQPUTS("isr: dma_bytes_done");
1770
                        wake_up(&qic02_tape_transfer);
1771
                } else {
1772
                        /* start next transfer, account for track-switching time */
1773
                        mod_timer(&tp_timer, jiffies + 6 * HZ);
1774
                        dma_transfer();
1775
                }
1776
        } else {
1777
                printk(TPQIC02_NAME ": Unexpected interrupt, stat == %x\n",
1778
                       inb(QIC02_STAT_PORT));
1779
        }
1780
}                               /* qic02_tape_interrupt */
1781
 
1782
 
1783
/* read/write routines:
1784
 * This code copies between a kernel buffer and a user buffer. The
1785
 * actual data transfer is done using DMA and interrupts. Time-outs
1786
 * are also used.
1787
 *
1788
 * When a filemark is read, we return '0 bytes read' and continue with the
1789
 * next file after that.
1790
 * When EOM is read, we return '0 bytes read' twice.
1791
 * When the EOT marker is detected on writes, '0 bytes read' should be
1792
 * returned twice. If user program does a MTNOP after that, 2 additional
1793
 * blocks may be written.       ------- FIXME: Implement this correctly  *************************************************
1794
 *
1795
 * Only read/writes in multiples of 512 bytes are accepted.
1796
 * When no bytes are available, we sleep() until they are. The controller will
1797
 * generate an interrupt, and we (should) get a wake_up() call.
1798
 *
1799
 * Simple buffering is used. User program should ensure that a large enough
1800
 * buffer is used. Usually the drive does some buffering as well (something
1801
 * like 4k or so).
1802
 *
1803
 * Scott S. Bertilson suggested to continue filling the user buffer, rather
1804
 * than waste time on a context switch, when the kernel buffer fills up.
1805
 */
1806
 
1807
/*
1808
 * Problem: tar(1) doesn't always read the entire file. Sometimes the entire file
1809
 * has been read, but the EOF token is never returned to tar(1), simply because
1810
 * tar(1) knows it has already read all of the data it needs. So we must use
1811
 * open/release to reset the `reported_read_eof' flag. If we don't, the next read
1812
 * request would return the EOF flag for the previous file.
1813
 */
1814
 
1815
static ssize_t qic02_tape_read(struct file *filp, char *buf, size_t count,
1816
                               loff_t * ppos)
1817
{
1818
        kdev_t dev = filp->f_dentry->d_inode->i_rdev;
1819
        unsigned short flags = filp->f_flags;
1820
        unsigned long bytes_todo, bytes_done, total_bytes_done = 0;
1821
        int stat;
1822
 
1823
        if (status_zombie == YES) {
1824
                tpqputs(TPQD_ALWAYS, "configs not set");
1825
                return -ENXIO;
1826
        }
1827
 
1828
        if (TP_DIAGS(current_tape_dev))
1829
                /* can't print a ``long long'' (for filp->f_pos), so chop it */
1830
                printk(TPQIC02_NAME
1831
                       ": request READ, minor=%x, buf=%p, count=%lx"
1832
                       ", pos=%lx, flags=%x\n", MINOR(dev), buf,
1833
                       (long) count, (unsigned long) filp->f_pos, flags);
1834
 
1835
        if (count % TAPE_BLKSIZE) {     /* Only allow mod 512 bytes at a time. */
1836
                tpqputs(TPQD_BLKSZ, "Wrong block size");
1837
                return -EINVAL;
1838
        }
1839
 
1840
        /* Just assume everything is ok. Controller will scream if not. */
1841
 
1842
        if (status_bytes_wr) {  /* Once written, no more reads, 'till after WFM. */
1843
                return -EACCES;
1844
        }
1845
 
1846
        /* This is rather ugly because it has to implement a finite state
1847
         * machine in order to handle the EOF situations properly.
1848
         */
1849
        while ((signed) count >= 0) {
1850
                bytes_done = 0;
1851
                /* see how much fits in the kernel buffer */
1852
                bytes_todo = TPQBUF_SIZE;
1853
                if (bytes_todo > count) {
1854
                        bytes_todo = count;
1855
                }
1856
 
1857
                /* Must ensure that user program sees exactly one EOF token (==0) */
1858
                if (return_read_eof == YES) {
1859
                        if (TPQDBG(DEBUG)) {
1860
                                printk
1861
                                    ("read: return_read_eof==%d, reported_read_eof==%d, total_bytes_done==%lu\n",
1862
                                     return_read_eof, reported_read_eof,
1863
                                     total_bytes_done);
1864
                        }
1865
 
1866
                        if (reported_read_eof == NO) {
1867
                                /* have not yet returned EOF to user program */
1868
                                if (total_bytes_done > 0) {
1869
                                        return total_bytes_done;        /* next time return EOF */
1870
                                } else {
1871
                                        reported_read_eof = YES;        /* move on next time */
1872
                                        return 0;        /* return EOF */
1873
                                }
1874
                        } else {
1875
                                /* Application program has already received EOF
1876
                                 * (above), now continue with next file on tape,
1877
                                 * if possible.
1878
                                 * When the FM is reached, EXCEPTION is set,
1879
                                 * causing a sense(). Subsequent read/writes will
1880
                                 * continue after the FM.
1881
                                 */
1882
                /*********** ?????????? this should check for (EOD|NDT), not EOM, 'cause we can read past EW: ************/
1883
                                if (status_eom_detected) {
1884
                                        /* If EOM, nothing left to read, so keep returning EOFs.
1885
                                         *** should probably set some flag to avoid clearing
1886
                                         *** status_eom_detected through ioctls or something
1887
                                         */
1888
                                        return 0;
1889
                                } else {
1890
                                        /* just eof, there may be more files ahead... */
1891
                                        return_read_eof = NO;
1892
                                        reported_read_eof = NO;
1893
                                        status_eof_detected = NO;       /* reset this too */
1894
                                        /*fall through */
1895
                                }
1896
                        }
1897
                }
1898
 
1899
        /*****************************/
1900
                if (bytes_todo == 0) {
1901
                        return total_bytes_done;
1902
                }
1903
 
1904
                if (bytes_todo > 0) {
1905
                        /* start reading data */
1906
                        if (is_exception()) {
1907
/****************************************/
1908
                                tpqputs(TPQD_DMAX,
1909
                                        "is_exception() before start_dma()!");
1910
                        }
1911
 
1912
/******************************************************************
1913
 ***** if start_dma() fails because the head is positioned 0 bytes
1914
 ***** before the FM, (causing EXCEPTION to be set) return_read_eof should
1915
 ***** be set to YES, and we should return total_bytes_done, rather than -ENXIO.
1916
 ***** The app should recognize this as an EOF condition.
1917
 ***************************************************************************/
1918
                        stat = start_dma(READ, bytes_todo);
1919
                        if (stat == TE_OK) {
1920
                                /* Wait for transfer to complete, interrupt should wake us */
1921
                                while (dma_mode != 0) {
1922
                                        sleep_on(&qic02_tape_transfer);
1923
                                }
1924
                                if (status_error) {
1925
                                        return_read_eof = YES;
1926
                                }
1927
 
1928
                        } else if (stat != TE_END) {
1929
                                /* should do sense() on error here */
1930
#if 0
1931
                                return -ENXIO;
1932
#else
1933
                                printk("Trouble: stat==%02x\n", stat);
1934
                                return_read_eof = YES;
1935
                /*************** check EOF/EOT handling!!!!!! **/
1936
#endif
1937
                        }
1938
                        end_dma(&bytes_done);
1939
                        if (bytes_done > bytes_todo) {
1940
                                tpqputs(TPQD_ALWAYS,
1941
                                        "read: Oops, read more bytes than requested");
1942
                                return -EIO;
1943
                        }
1944
                        /* copy buffer to user-space in one go */
1945
                        if (bytes_done > 0) {
1946
                                if (copy_to_user(buf, buffaddr, bytes_done))
1947
                                        return -EFAULT;
1948
                        }
1949
#if 1
1950
                        /* Checks Ton's patch below */
1951
                        if ((return_read_eof == NO)
1952
                            && (status_eof_detected == YES)) {
1953
                                printk(TPQIC02_NAME
1954
                                       ": read(): return_read_eof=%d, status_eof_detected=YES. return_read_eof:=YES\n",
1955
                                       return_read_eof);
1956
                        }
1957
#endif
1958
                        if ((bytes_todo != bytes_done)
1959
                            || (status_eof_detected == YES)) {
1960
                                /* EOF or EOM detected. return EOF next time. */
1961
                                return_read_eof = YES;
1962
                        }
1963
 
1964
                }
1965
                /* else: ignore read request for 0 bytes */
1966
                if (bytes_done > 0) {
1967
                        status_bytes_rd = YES;
1968
                        buf += bytes_done;
1969
                        *ppos += bytes_done;
1970
                        total_bytes_done += bytes_done;
1971
                        count -= bytes_done;
1972
                }
1973
        }
1974
        tpqputs(TPQD_ALWAYS, "read request for <0 bytes");
1975
        return -EINVAL;
1976
}                               /* qic02_tape_read */
1977
 
1978
 
1979
 
1980
/* The drive detects near-EOT by means of the holes in the tape.
1981
 * When the holes are detected, there is some space left. The drive
1982
 * reports this as a TP_EOM exception. After clearing the exception,
1983
 * the drive should accept two extra blocks.
1984
 *
1985
 * It seems there are some archiver programs that would like to use the
1986
 * extra space for writing a continuation marker. The driver should return
1987
 * end-of-file to the user program on writes, when the holes are detected.
1988
 * If the user-program wants to use the extra space, it should use the
1989
 * MTNOP ioctl() to get the generic status register and may then continue
1990
 * writing (max 1kB).   ----------- doesn't work yet...............
1991
 *
1992
 * EOF behaviour on writes:
1993
 * If there is enough room, write all of the data.
1994
 * If there is insufficient room, write as much as will fit and
1995
 * return the amount written. If the requested amount differs from the
1996
 * written amount, the application program should recognize that as the
1997
 * end of file. Subsequent writes will return -ENOSPC.
1998
 * Unless the minor bits specify a rewind-on-close, the tape will not
1999
 * be rewound when it is full. The user-program should do that, if desired.
2000
 * If the driver were to do that automatically, a user-program could be
2001
 * confused about the EOT/BOT condition after re-opening the tape device.
2002
 *
2003
 * Multiple volume support: Tar closes the tape device before prompting for
2004
 * the next tape. The user may then insert a new tape and tar will open the
2005
 * tape device again. The driver will detect an exception status in (No Cartridge)
2006
 * and force a rewind. After that tar may continue writing.
2007
 */
2008
static ssize_t qic02_tape_write(struct file *filp, const char *buf,
2009
                                size_t count, loff_t * ppos)
2010
{
2011
        kdev_t dev = filp->f_dentry->d_inode->i_rdev;
2012
        unsigned short flags = filp->f_flags;
2013
        unsigned long bytes_todo, bytes_done, total_bytes_done = 0;
2014
 
2015
        if (status_zombie == YES) {
2016
                tpqputs(TPQD_ALWAYS, "configs not set");
2017
                return -ENXIO;
2018
        }
2019
 
2020
        if (TP_DIAGS(current_tape_dev)) {
2021
                /* can't print a ``long long'' (for filp->f_pos), so chop it */
2022
                printk(TPQIC02_NAME ": request WRITE, minor=%x, buf=%p"
2023
                       ", count=%lx, pos=%lx, flags=%x\n",
2024
                       MINOR(dev), buf,
2025
                       (long) count, (unsigned long) filp->f_pos, flags);
2026
        }
2027
 
2028
        if (count % TAPE_BLKSIZE) {     /* only allow mod 512 bytes at a time */
2029
                tpqputs(TPQD_BLKSZ, "Wrong block size");
2030
                return -EINVAL;
2031
        }
2032
 
2033
        if (mode_access == READ) {
2034
                tpqputs(TPQD_ALWAYS, "Not in write mode");
2035
                return -EACCES;
2036
        }
2037
 
2038
        /* open() does a sense() and we can assume the tape isn't changed
2039
         * between open() and release(), so the tperror.exs bits will still
2040
         * be valid.
2041
         */
2042
        if ((tperror.exs & TP_ST0) && (tperror.exs & TP_WRP)) {
2043
                tpqputs(TPQD_ALWAYS, "Cartridge is write-protected.");
2044
                return -EACCES; /* don't even try when write protected */
2045
        }
2046
 
2047
        if (doing_read == YES) {
2048
                terminate_read(0);
2049
        }
2050
 
2051
        while ((signed) count >= 0) {
2052
                /* see how much fits in the kernel buffer */
2053
                bytes_done = 0;
2054
                bytes_todo = TPQBUF_SIZE;
2055
                if (bytes_todo > count) {
2056
                        bytes_todo = count;
2057
                }
2058
 
2059
                if (return_write_eof == YES) {
2060
                        /* return_write_eof should be reset on reverse tape movements. */
2061
 
2062
                        if (reported_write_eof == NO) {
2063
                                if (bytes_todo > 0) {
2064
                                        tpqputs(TPQD_ALWAYS,
2065
                                                "partial write");
2066
                                        /* partial write signals EOF to user program */
2067
                                }
2068
                                reported_write_eof = YES;
2069
                                return total_bytes_done;
2070
                        } else {
2071
                                return -ENOSPC; /* return error */
2072
                        }
2073
                }
2074
 
2075
                /* Quit when done. */
2076
                if (bytes_todo == 0) {
2077
                        return total_bytes_done;
2078
                }
2079
 
2080
                /* copy from user to DMA buffer and initiate transfer. */
2081
                if (bytes_todo > 0) {
2082
                        if (copy_from_user(buffaddr, buf, bytes_todo))
2083
                                return -EFAULT;
2084
 
2085
/****************** similar problem with read() at FM could happen here at EOT.
2086
 ******************/
2087
 
2088
/***** if at EOT, 0 bytes can be written. start_dma() will
2089
 ***** fail and write() will return ENXIO error
2090
 *****/
2091
                        if (start_dma(WRITE, bytes_todo) != TE_OK) {
2092
                                tpqputs(TPQD_ALWAYS,
2093
                                        "write: start_dma() failed");
2094
                                /* should do sense() on error here */
2095
                                return -ENXIO;
2096
                                /*********** FIXTHIS **************/
2097
                        }
2098
 
2099
                        /* Wait for write to complete, interrupt should wake us. */
2100
                        while ((status_error == 0) && (dma_mode != 0)) {
2101
                                sleep_on(&qic02_tape_transfer);
2102
                        }
2103
 
2104
                        end_dma(&bytes_done);
2105
                        if (bytes_done > bytes_todo) {
2106
                                tpqputs(TPQD_ALWAYS,
2107
                                        "write: Oops, wrote more bytes than requested");
2108
                                return -EIO;
2109
                        }
2110
                        /* If the dma-transfer was aborted because of an exception,
2111
                         * status_error will have been set in the interrupt handler.
2112
                         * Then end_dma() will do a sense().
2113
                         * If the exception was EXC_EOM, the EW-hole was encountered
2114
                         * and two more blocks could be written. For the time being we'll
2115
                         * just consider this to be the EOT.
2116
                         * Otherwise, something Bad happened, such as the maximum number
2117
                         * of block-rewrites was exceeded. [e.g. A very bad spot on tape was
2118
                         * encountered. Normally short dropouts are compensated for by
2119
                         * rewriting the block in error, up to 16 times. I'm not sure
2120
                         * QIC-24 drives can do this.]
2121
                         */
2122
                        if (status_error) {
2123
                                if (status_eom_detected == YES) {
2124
                                        tpqputs(TPQD_ALWAYS,
2125
                                                "write: EW detected");
2126
                                        return_write_eof = YES;
2127
                                } else {
2128
                                        /* probably EXC_RWA */
2129
                                        tpqputs(TPQD_ALWAYS,
2130
                                                "write: dma: error in writing");
2131
                                        return -EIO;
2132
                                }
2133
                        }
2134
                        if (bytes_todo != bytes_done) {
2135
                                /* EOF or EOM detected. return EOT next time. */
2136
                                return_write_eof = YES;
2137
                        }
2138
                }
2139
                /* else: ignore write request for 0 bytes. */
2140
 
2141
                if (bytes_done > 0) {
2142
                        status_bytes_wr = YES;
2143
                        buf += bytes_done;
2144
                        *ppos += bytes_done;
2145
                        total_bytes_done += bytes_done;
2146
                        count -= bytes_done;
2147
                }
2148
        }
2149
 
2150
        tpqputs(TPQD_ALWAYS, "write request for <0 bytes");
2151
        if (TPQDBG(DEBUG)) {
2152
                printk(TPQIC02_NAME ": status_bytes_wr %x, buf %p"
2153
                       ", total_bytes_done %lx, count %lx\n",
2154
                       status_bytes_wr, buf, total_bytes_done,
2155
                       (long) count);
2156
        }
2157
        return -EINVAL;
2158
}                               /* qic02_tape_write */
2159
 
2160
 
2161
 
2162
/* qic02_tape_open()
2163
 * We allow the device to be opened, even if it is marked 'dead' because
2164
 * we want to be able to reset the tape device without rebooting.
2165
 * Only one open tape file at a time, except when minor=255.
2166
 * Minor 255 is only allowed for resetting and always returns <0.
2167
 *
2168
 * The density command is only allowed when TP_BOM is set. Thus, remember
2169
 * the most recently used minor bits. When they are different from the
2170
 * remembered values, rewind the tape and set the required density.
2171
 * Don't rewind if the minor bits specify density 0.
2172
 */
2173
 
2174
static int qic02_tape_open(struct inode *inode, struct file *filp)
2175
{
2176
        static int qic02_tape_open_no_use_count(struct inode *,
2177
                                                struct file *);
2178
        int open_error;
2179
 
2180
        open_error = qic02_tape_open_no_use_count(inode, filp);
2181
        return open_error;
2182
}
2183
 
2184
static int qic02_tape_open_no_use_count(struct inode *inode,
2185
                                        struct file *filp)
2186
{
2187
        kdev_t dev = inode->i_rdev;
2188
        unsigned short flags = filp->f_flags;
2189
        unsigned short dens = 0;
2190
        int s;
2191
 
2192
 
2193
        if (TP_DIAGS(dev)) {
2194
                printk("qic02_tape_open: dev=%s, flags=%x     ",
2195
                       kdevname(dev), flags);
2196
        }
2197
 
2198
        if (MINOR(dev) == 255) {        /* special case for resetting */
2199
                if (capable(CAP_SYS_ADMIN)) {
2200
                        return (tape_reset(1) == TE_OK) ? -EAGAIN : -ENXIO;
2201
                } else {
2202
                        return -EPERM;
2203
                }
2204
        }
2205
 
2206
        if (status_dead == YES) {
2207
                /* Allow `mt reset' ioctl() even when already open()ed. */
2208
                return 0;
2209
        }
2210
 
2211
        /* Only one at a time from here on... */
2212
        if (file_count(filp) > 1) {     /* filp->f_count==1 for the first open() */
2213
                return -EBUSY;
2214
        }
2215
 
2216
        if (status_zombie == YES) {
2217
                /* no irq/dma/port stuff allocated yet, no reset done
2218
                 * yet, so return until MTSETCONFIG has been done.
2219
                 */
2220
                return 0;
2221
        }
2222
 
2223
        status_bytes_rd = NO;
2224
        status_bytes_wr = NO;
2225
 
2226
        return_read_eof = NO;   /********????????????????*****/
2227
        return_write_eof = (status_eot_detected) ? YES : NO;
2228
 
2229
        /* Clear this in case user app close()d before reading EOF token */
2230
        status_eof_detected = NO;
2231
 
2232
        reported_read_eof = NO;
2233
        reported_write_eof = NO;
2234
 
2235
 
2236
        switch (flags & O_ACCMODE) {
2237
        case O_RDONLY:
2238
                mode_access = READ;
2239
                break;
2240
        case O_WRONLY:          /* Fallthru... Strictly speaking this is not correct... */
2241
        case O_RDWR:            /* Reads are allowed as long as nothing is written */
2242
                mode_access = WRITE;
2243
                break;
2244
        }
2245
 
2246
        /* This is to avoid tape-changed problems (TP_CNI exception).
2247
         *
2248
         * Since removing the cartridge will not raise an exception,
2249
         * we always do a tp_sense() to make sure we have the proper
2250
         * CNI status, the 2150L may need an additional sense.... - Eddy
2251
         */
2252
        s = tp_sense(TP_WRP | TP_EOM | TP_BOM | TP_CNI | TP_EOR);
2253
 
2254
        if (s == TE_OK) {
2255
                /* Try to clear cartridge-changed status for Archive-2150L */
2256
                if ((tperror.exs & TP_ST0) && (tperror.exs & TP_CNI)) {
2257
                        s = tp_sense(TP_WRP | TP_EOM | TP_BOM | TP_CNI |
2258
                                     TP_EOR);
2259
                }
2260
        }
2261
 
2262
        if (s != TE_OK) {
2263
                tpqputs(TPQD_ALWAYS, "open: sense() failed");
2264
                return -EIO;
2265
        }
2266
 
2267
        /* exception bits should be up-to-date now, so check for
2268
         * tape presence and exit if absent.
2269
         * Even `mt stat' will fail without a tape.
2270
         */
2271
        if ((tperror.exs & TP_ST0) && (tperror.exs & TP_CNI)) {
2272
                tpqputs(TPQD_ALWAYS, "No tape present.");
2273
                return -EIO;
2274
        }
2275
 
2276
        /* At this point we can assume that a tape is present and
2277
         * that it will remain present until release() is called.
2278
         */
2279
 
2280
        /* not allowed to do QCMD_DENS_* unless tape is rewound */
2281
        if ((TP_DENS(dev) != 0)
2282
            && (TP_DENS(current_tape_dev) != TP_DENS(dev))) {
2283
                /* force rewind if minor bits have changed,
2284
                 * i.e. user wants to use tape in different format.
2285
                 * [assuming single drive operation]
2286
                 */
2287
                if (TP_HAVE_DENS) {
2288
                        tpqputs(TPQD_REWIND,
2289
                                "Density minor bits have changed. Forcing rewind.");
2290
                        need_rewind = YES;
2291
                }
2292
        } else {
2293
                /* density bits still the same, but TP_DIAGS bit
2294
                 * may have changed.
2295
                 */
2296
                current_tape_dev = dev;
2297
        }
2298
 
2299
        if (need_rewind == YES) {
2300
/***************** CHECK THIS!!!!!!!! **********/
2301
                s = do_qic_cmd(QCMD_REWIND, TIM_R);
2302
                if (s != 0) {
2303
                        tpqputs(TPQD_ALWAYS, "open: rewind failed");
2304
                        return -EIO;
2305
                }
2306
        }
2307
 
2308
 
2309
/* Note: After a reset command, the controller will rewind the tape
2310
 *       just before performing any tape movement operation! ************ SO SET need_rewind flag!!!!!
2311
 */
2312
        if (status_dead == YES) {
2313
                tpqputs(TPQD_ALWAYS, "open: tape dead, attempting reset");
2314
                if (tape_reset(1) != TE_OK) {
2315
                        return -ENXIO;
2316
                } else {
2317
                        status_dead = NO;
2318
                        if (tp_sense(~(TP_ST1 | TP_ILL)) != TE_OK) {
2319
                                tpqputs(TPQD_ALWAYS,
2320
                                        "open: tp_sense() failed\n");
2321
                                status_dead = YES;      /* try reset next time */
2322
                                return -EIO;
2323
                        }
2324
                }
2325
        }
2326
 
2327
        /* things should be ok, once we get here */
2328
 
2329
 
2330
        /* set density: only allowed when TP_BOM status bit is set,
2331
         * so we must have done a rewind by now. If not, just skip over.
2332
         * Only give set density command when minor bits have changed.
2333
         */
2334
        if (TP_DENS(current_tape_dev) == TP_DENS(dev)) {
2335
                return 0;
2336
        }
2337
 
2338
        current_tape_dev = dev;
2339
        need_rewind = NO;
2340
        if (TP_HAVE_DENS) {
2341
                dens = TP_DENS(dev);
2342
        }
2343
 
2344
        if (dens < sizeof(format_names) / sizeof(char *)) {
2345
                printk(TPQIC02_NAME ": format: %s%s\n",
2346
                       (dens != 0) ? "QIC-" : "", format_names[dens]);
2347
        } else {
2348
                tpqputs(TPQD_REWIND, "Wait for retensioning...");
2349
        }
2350
 
2351
        switch (TP_DENS(dev)) {
2352
        case 0:          /* Minor 0 is for drives without set-density support */
2353
                s = 0;
2354
                break;
2355
        case 1:
2356
                s = do_qic_cmd(QCMD_DENS_11, TIM_S);
2357
                break;
2358
        case 2:
2359
                s = do_qic_cmd(QCMD_DENS_24, TIM_S);
2360
                break;
2361
        case 3:
2362
                s = do_qic_cmd(QCMD_DENS_120, TIM_S);
2363
                break;
2364
        case 4:
2365
                s = do_qic_cmd(QCMD_DENS_150, TIM_S);
2366
                break;
2367
        case 5:
2368
                s = do_qic_cmd(QCMD_DENS_300, TIM_S);
2369
                break;
2370
        case 6:
2371
                s = do_qic_cmd(QCMD_DENS_600, TIM_S);
2372
                break;
2373
        default:                /* otherwise do a retension before anything else */
2374
                s = do_qic_cmd(QCMD_RETEN, TIM_R);
2375
        }
2376
        if (s != 0) {
2377
                status_dead = YES;      /* force reset */
2378
                current_tape_dev = 0;    /* earlier 0xff80 */
2379
                return -EIO;
2380
        }
2381
 
2382
        return 0;
2383
}                               /* qic02_tape_open */
2384
 
2385
 
2386
static int qic02_tape_release(struct inode *inode, struct file *filp)
2387
{
2388
        kdev_t dev = inode->i_rdev;
2389
 
2390
        lock_kernel();
2391
        if (TP_DIAGS(dev)) {
2392
                printk("qic02_tape_release: dev=%s\n", kdevname(dev));
2393
        }
2394
 
2395
        if (status_zombie == NO) {      /* don't rewind in zombie mode */
2396
                /* Terminate any pending write cycle. Terminating the read-cycle
2397
                 * is delayed until it is required to do so for a new command.
2398
                 */
2399
                terminate_write(-1);
2400
 
2401
                if (status_dead == YES) {
2402
                        tpqputs(TPQD_ALWAYS, "release: device dead!?");
2403
                }
2404
 
2405
                /* Rewind only if minor number requires it AND
2406
                 * read/writes have been done. ************* IS THIS CORRECT??????????
2407
                 */
2408
                if ((TP_REWCLOSE(dev))
2409
                    && (status_bytes_rd | status_bytes_wr)) {
2410
                        tpqputs(TPQD_REWIND, "release: Doing rewind...");
2411
                        (void) do_qic_cmd(QCMD_REWIND, TIM_R);
2412
                }
2413
        }
2414
        unlock_kernel();
2415
        return 0;
2416
}                               /* qic02_tape_release */
2417
 
2418
 
2419
#ifdef CONFIG_QIC02_DYNCONF
2420
/* Set masks etc. based on the interface card type. */
2421
static int update_ifc_masks(int ifc)
2422
{
2423
        QIC02_TAPE_IFC = ifc;
2424
 
2425
        if ((QIC02_TAPE_IFC == WANGTEK) || (QIC02_TAPE_IFC == EVEREX)) {
2426
                QIC02_STAT_PORT = QIC02_TAPE_PORT;
2427
                QIC02_CTL_PORT = QIC02_TAPE_PORT;
2428
                QIC02_CMD_PORT = QIC02_TAPE_PORT + 1;
2429
                QIC02_DATA_PORT = QIC02_TAPE_PORT + 1;
2430
                QIC02_STAT_READY = WT_QIC02_STAT_READY;
2431
                QIC02_STAT_EXCEPTION = WT_QIC02_STAT_EXCEPTION;
2432
                QIC02_STAT_MASK = WT_QIC02_STAT_MASK;
2433
 
2434
                QIC02_STAT_RESETMASK = WT_QIC02_STAT_RESETMASK;
2435
                QIC02_STAT_RESETVAL = WT_QIC02_STAT_RESETVAL;
2436
 
2437
                QIC02_CTL_RESET = WT_QIC02_CTL_RESET;
2438
                QIC02_CTL_REQUEST = WT_QIC02_CTL_REQUEST;
2439
 
2440
                if (QIC02_TAPE_DMA == 3) {
2441
                        WT_CTL_DMA = WT_CTL_DMA3;
2442
                } else if (QIC02_TAPE_DMA == 1) {
2443
                        WT_CTL_DMA = WT_CTL_DMA1;
2444
                } else {
2445
                        tpqputs(TPQD_ALWAYS,
2446
                                "Unsupported or incorrect DMA channel");
2447
                        return -EIO;
2448
                }
2449
 
2450
                if (QIC02_TAPE_IFC == EVEREX) {
2451
                        /* Everex is a special case for Wangtek (actually
2452
                         * it's the other way 'round, but I saw Wangtek first)
2453
                         */
2454
                        if (QIC02_TAPE_DMA == 3) {
2455
                                WT_CTL_DMA = WT_CTL_DMA1;
2456
                        }
2457
 
2458
                        /* Fixup the kernel copy of the IFC type to that
2459
                         * we don't have to distinguish between Wangtek and
2460
                         * and Everex at runtime.
2461
                         */
2462
                        QIC02_TAPE_IFC = WANGTEK;
2463
                }
2464
        } else if (QIC02_TAPE_IFC == ARCHIVE) {
2465
                QIC02_STAT_PORT = QIC02_TAPE_PORT + 1;
2466
                QIC02_CTL_PORT = QIC02_TAPE_PORT + 1;
2467
                QIC02_CMD_PORT = QIC02_TAPE_PORT;
2468
                QIC02_DATA_PORT = QIC02_TAPE_PORT;
2469
                QIC02_STAT_READY = AR_QIC02_STAT_READY;
2470
                QIC02_STAT_EXCEPTION = AR_QIC02_STAT_EXCEPTION;
2471
                QIC02_STAT_MASK = AR_QIC02_STAT_MASK;
2472
 
2473
                QIC02_STAT_RESETMASK = AR_QIC02_STAT_RESETMASK;
2474
                QIC02_STAT_RESETVAL = AR_QIC02_STAT_RESETVAL;
2475
 
2476
                QIC02_CTL_RESET = AR_QIC02_CTL_RESET;
2477
                QIC02_CTL_REQUEST = AR_QIC02_CTL_REQUEST;
2478
 
2479
                if (QIC02_TAPE_DMA > 3) {
2480
                        tpqputs(TPQD_ALWAYS,
2481
                                "Unsupported or incorrect DMA channel");
2482
                        return -EIO;
2483
                }
2484
        } else if (QIC02_TAPE_IFC == MOUNTAIN) {
2485
                QIC02_STAT_PORT = QIC02_TAPE_PORT + 1;
2486
                QIC02_CTL_PORT = QIC02_TAPE_PORT + 1;
2487
                QIC02_CMD_PORT = QIC02_TAPE_PORT;
2488
                QIC02_DATA_PORT = QIC02_TAPE_PORT;
2489
 
2490
                QIC02_STAT_READY = MTN_QIC02_STAT_READY;
2491
                QIC02_STAT_EXCEPTION = MTN_QIC02_STAT_EXCEPTION;
2492
                QIC02_STAT_MASK = MTN_QIC02_STAT_MASK;
2493
 
2494
                QIC02_STAT_RESETMASK = MTN_QIC02_STAT_RESETMASK;
2495
                QIC02_STAT_RESETVAL = MTN_QIC02_STAT_RESETVAL;
2496
 
2497
                QIC02_CTL_RESET = MTN_QIC02_CTL_RESET;
2498
                QIC02_CTL_REQUEST = MTN_QIC02_CTL_REQUEST;
2499
 
2500
                if (QIC02_TAPE_DMA > 3) {
2501
                        tpqputs(TPQD_ALWAYS,
2502
                                "Unsupported or incorrect DMA channel");
2503
                        return -EIO;
2504
                }
2505
        } else {
2506
                tpqputs(TPQD_ALWAYS, "Invalid interface type");
2507
                return -ENXIO;
2508
        }
2509
        return qic02_get_resources();
2510
}                               /* update_ifc_masks */
2511
#endif
2512
 
2513
 
2514
/* ioctl allows user programs to rewind the tape and stuff like that */
2515
static int qic02_tape_ioctl(struct inode *inode, struct file *filp,
2516
                            unsigned int iocmd, unsigned long ioarg)
2517
{
2518
        int error;
2519
        int dev_maj = MAJOR(inode->i_rdev);
2520
        int c;
2521
        struct mtop operation;
2522
        unsigned char blk_addr[6];
2523
        struct mtpos ioctl_tell;
2524
 
2525
 
2526
        if (TP_DIAGS(current_tape_dev)) {
2527
                printk(TPQIC02_NAME ": ioctl(%4x, %4x, %4lx)\n", dev_maj,
2528
                       iocmd, ioarg);
2529
        }
2530
 
2531
        if (!inode || !ioarg) {
2532
                return -EINVAL;
2533
        }
2534
 
2535
        /* check iocmd first */
2536
 
2537
        if (dev_maj != QIC02_TAPE_MAJOR) {
2538
                printk(TPQIC02_NAME ": Oops! Wrong device?\n");
2539
                /* A panic() would be appropriate here */
2540
                return -ENODEV;
2541
        }
2542
 
2543
        c = _IOC_NR(iocmd);
2544
 
2545
#ifdef CONFIG_QIC02_DYNCONF
2546
        if (c == _IOC_NR(MTIOCGETCONFIG)) {
2547
                CHECK_IOC_SIZE(mtconfiginfo);
2548
 
2549
                if (copy_to_user
2550
                    ((char *) ioarg, (char *) &qic02_tape_dynconf,
2551
                     sizeof(qic02_tape_dynconf))) {
2552
                        return -EFAULT;
2553
                }
2554
                return 0;
2555
 
2556
        } else if (c == _IOC_NR(MTIOCSETCONFIG)) {
2557
                /* One should always do a MTIOCGETCONFIG first, then update
2558
                 * user-settings, then write back with MTIOCSETCONFIG.
2559
                 * The qic02conf program should re-open() the device before actual
2560
                 * use, to make sure everything is initialized.
2561
                 */
2562
 
2563
                CHECK_IOC_SIZE(mtconfiginfo);
2564
 
2565
                if (!capable(CAP_SYS_ADMIN)) {
2566
                        return -EPERM;
2567
                }
2568
 
2569
                if ((doing_read != NO) || (doing_write != NO)) {
2570
                        return -EBUSY;
2571
                }
2572
 
2573
                if (status_zombie == NO) {
2574
                        qic02_release_resources();      /* and go zombie */
2575
                }
2576
 
2577
                /* copy struct from user space to kernel space */
2578
                if (copy_from_user
2579
                    ((char *) &qic02_tape_dynconf, (char *) ioarg,
2580
                     sizeof(qic02_tape_dynconf))) {
2581
                        return -EFAULT;
2582
                }
2583
                return update_ifc_masks(qic02_tape_dynconf.ifc_type);
2584
        }
2585
        if (status_zombie == YES) {
2586
                tpqputs(TPQD_ALWAYS, "Configs not set");
2587
                return -ENXIO;
2588
        }
2589
#endif
2590
        if (c == _IOC_NR(MTIOCTOP)) {
2591
                CHECK_IOC_SIZE(mtop);
2592
 
2593
                /* copy mtop struct from user space to kernel space */
2594
                if (copy_from_user
2595
                    ((char *) &operation, (char *) ioarg,
2596
                     sizeof(operation))) {
2597
                        return -EFAULT;
2598
                }
2599
 
2600
                /* ---note: mt_count is signed, negative seeks must be
2601
                 * ---      translated to seeks in opposite direction!
2602
                 * (only needed for Sun-programs, I think.)
2603
                 */
2604
                /* ---note: MTFSF with count 0 should position the
2605
                 * ---      tape at the beginning of the current file.
2606
                 */
2607
 
2608
                if (TP_DIAGS(current_tape_dev)) {
2609
                        printk("OP op=%4x, count=%4x\n", operation.mt_op,
2610
                               operation.mt_count);
2611
                }
2612
 
2613
                if (operation.mt_count < 0) {
2614
                        tpqputs(TPQD_ALWAYS,
2615
                                "Warning: negative mt_count ignored");
2616
                }
2617
 
2618
                ioctl_status.mt_resid = operation.mt_count;
2619
                if (operation.mt_op == MTSEEK) {
2620
                        if (!TP_HAVE_SEEK) {
2621
                                return -ENOTTY;
2622
                        }
2623
 
2624
                        seek_addr_buf[0] =
2625
                            (operation.mt_count >> 16) & 0xff;
2626
                        seek_addr_buf[1] =
2627
                            (operation.mt_count >> 8) & 0xff;
2628
                        seek_addr_buf[2] = (operation.mt_count) & 0xff;
2629
                        if (operation.mt_count >> 24) {
2630
                                return -EINVAL;
2631
                        }
2632
                        if ((error = do_ioctl_cmd(operation.mt_op)) != 0) {
2633
                                return error;
2634
                        }
2635
 
2636
                        ioctl_status.mt_resid = 0;
2637
                } else {
2638
                        while (operation.mt_count > 0) {
2639
                                operation.mt_count--;
2640
                                if ((error =
2641
                                     do_ioctl_cmd(operation.mt_op)) != 0) {
2642
                                        return error;
2643
                                }
2644
 
2645
                                ioctl_status.mt_resid = operation.mt_count;
2646
                        }
2647
                }
2648
                return 0;
2649
 
2650
        } else if (c == _IOC_NR(MTIOCGET)) {
2651
                if (TP_DIAGS(current_tape_dev)) {
2652
                        printk("GET ");
2653
                }
2654
 
2655
                CHECK_IOC_SIZE(mtget);
2656
 
2657
                /* It appears (gmt(1)) that it is normal behaviour to
2658
                 * first set the status with MTNOP, and then to read
2659
                 * it out with MTIOCGET
2660
                 */
2661
 
2662
                /* copy results to user space */
2663
                if (copy_to_user
2664
                    ((char *) ioarg, (char *) &ioctl_status,
2665
                     sizeof(ioctl_status))) {
2666
                        return -EFAULT;
2667
                }
2668
                return 0;
2669
        } else if (TP_HAVE_TELL && (c == _IOC_NR(MTIOCPOS))) {
2670
                if (TP_DIAGS(current_tape_dev)) {
2671
                        printk("POS ");
2672
                }
2673
 
2674
                CHECK_IOC_SIZE(mtpos);
2675
 
2676
                tpqputs(TPQD_IOCTLS, "MTTELL reading block address");
2677
                if ((doing_read == YES) || (doing_write == YES)) {
2678
                        finish_rw(AR_QCMDV_TELL_BLK);
2679
                }
2680
 
2681
                c = rdstatus((char *) blk_addr, sizeof(blk_addr),
2682
                             AR_QCMDV_TELL_BLK);
2683
                if (c != TE_OK) {
2684
                        return -EIO;
2685
                }
2686
 
2687
                ioctl_tell.mt_blkno =
2688
                    (blk_addr[3] << 16) | (blk_addr[4] << 8) | blk_addr[5];
2689
 
2690
                /* copy results to user space */
2691
                if (copy_to_user
2692
                    ((char *) ioarg, (char *) &ioctl_tell,
2693
                     sizeof(ioctl_tell))) {
2694
                        return -EFAULT;
2695
                }
2696
                return 0;
2697
 
2698
        } else {
2699
                return -ENOTTY; /* Other cmds not supported. */
2700
        }
2701
}                               /* qic02_tape_ioctl */
2702
 
2703
 
2704
 
2705
/* These are (most) of the interface functions: */
2706
static struct file_operations qic02_tape_fops = {
2707
        owner:THIS_MODULE,
2708
        llseek:no_llseek,
2709
        read:qic02_tape_read,
2710
        write:qic02_tape_write,
2711
        ioctl:qic02_tape_ioctl,
2712
        open:qic02_tape_open,
2713
        release:qic02_tape_release,
2714
};
2715
 
2716
 
2717
static void qic02_release_resources(void)
2718
{
2719
        free_irq(QIC02_TAPE_IRQ, NULL);
2720
        free_dma(QIC02_TAPE_DMA);
2721
        release_region(QIC02_TAPE_PORT, QIC02_TAPE_PORT_RANGE);
2722
        if (buffaddr) {
2723
                free_pages((unsigned long) buffaddr,
2724
                           get_order(TPQBUF_SIZE));
2725
        }
2726
        buffaddr = 0;            /* Better to cause a panic than overwite someone else */
2727
        status_zombie = YES;
2728
}                               /* qic02_release_resources */
2729
 
2730
 
2731
static int qic02_get_resources(void)
2732
{
2733
        /* First perform some checks. If one of them fails,
2734
         * the tape driver will not be registered to the system.
2735
         */
2736
        if (QIC02_TAPE_IRQ > 16) {
2737
                tpqputs(TPQD_ALWAYS, "Bogus interrupt number.");
2738
                return -ENXIO;
2739
        }
2740
 
2741
        /* for DYNCONF, allocating IO, DMA and IRQ should not be done until
2742
         * the config parameters have been set using MTSETCONFIG.
2743
         */
2744
 
2745
        /* Grab the IO region. */
2746
        if (!request_region(QIC02_TAPE_PORT, QIC02_TAPE_PORT_RANGE,
2747
                           TPQIC02_NAME)) {
2748
                printk(TPQIC02_NAME
2749
                       ": IO space at 0x%x [%d ports] already reserved\n",
2750
                       QIC02_TAPE_PORT, QIC02_TAPE_PORT_RANGE);
2751
                return -ENXIO;
2752
        }
2753
 
2754
        /* get IRQ */
2755
        if (request_irq
2756
            (QIC02_TAPE_IRQ, qic02_tape_interrupt, SA_INTERRUPT, "QIC-02",
2757
             NULL)) {
2758
                printk(TPQIC02_NAME
2759
                       ": can't allocate IRQ%d for QIC-02 tape\n",
2760
                       QIC02_TAPE_IRQ);
2761
                release_region(QIC02_TAPE_PORT, QIC02_TAPE_PORT_RANGE);
2762
                return -EBUSY;
2763
        }
2764
 
2765
        /* After IRQ, allocate DMA channel */
2766
        if (request_dma(QIC02_TAPE_DMA, "QIC-02")) {
2767
                printk(TPQIC02_NAME
2768
                       ": can't allocate DMA%d for QIC-02 tape\n",
2769
                       QIC02_TAPE_DMA);
2770
                free_irq(QIC02_TAPE_IRQ, NULL);
2771
                release_region(QIC02_TAPE_PORT, QIC02_TAPE_PORT_RANGE);
2772
                return -EBUSY;
2773
        }
2774
 
2775
        /* Setup the page-address for the dma transfer. */
2776
        buffaddr =
2777
            (void *) __get_dma_pages(GFP_KERNEL, get_order(TPQBUF_SIZE));
2778
 
2779
        if (!buffaddr) {
2780
                qic02_release_resources();
2781
                return -EBUSY;  /* Not ideal, EAGAIN perhaps? */
2782
        }
2783
 
2784
        memset(buffaddr, 0, TPQBUF_SIZE);
2785
 
2786
        printk(TPQIC02_NAME
2787
               ": Settings: IRQ %d, DMA %d, IO 0x%x, IFC %s\n",
2788
               QIC02_TAPE_IRQ, QIC02_TAPE_DMA, ((QIC02_TAPE_IFC == ARCHIVE)
2789
                                                || (QIC02_TAPE_IFC ==
2790
                                                    MOUNTAIN)) ?
2791
               QIC02_CMD_PORT : QIC02_STAT_PORT,
2792
               (QIC02_TAPE_IFC ==
2793
                MOUNTAIN) ? "Mountain" : ((QIC02_TAPE_IFC ==
2794
                                           ARCHIVE) ? "Archive" :
2795
                                          "Wangtek"));
2796
 
2797
        if (tape_reset(0) != TE_OK
2798
            || tp_sense(TP_WRP | TP_POR | TP_CNI) != TE_OK) {
2799
                /* No drive detected, so vanish */
2800
                tpqputs(TPQD_ALWAYS,
2801
                        "No drive detected -- releasing IO/IRQ/DMA.");
2802
                status_dead = YES;
2803
                qic02_release_resources();
2804
                return -EIO;
2805
        }
2806
 
2807
        /* All should be ok now */
2808
        status_zombie = NO;
2809
        return 0;
2810
}                               /* qic02_get_resources */
2811
 
2812
int __init qic02_tape_init(void)
2813
{
2814
        if (TPSTATSIZE != 6) {
2815
                printk(TPQIC02_NAME
2816
                       ": internal error: tpstatus struct incorrect!\n");
2817
                return -ENODEV;
2818
        }
2819
        if ((TPQBUF_SIZE < 512) || (TPQBUF_SIZE >= 0x10000)) {
2820
                printk(TPQIC02_NAME
2821
                       ": internal error: DMA buffer size out of range\n");
2822
                return -ENODEV;
2823
        }
2824
 
2825
        current_tape_dev = MKDEV(QIC02_TAPE_MAJOR, 0);
2826
 
2827
#ifndef CONFIG_QIC02_DYNCONF
2828
        printk(TPQIC02_NAME ": IRQ %d, DMA %d, IO 0x%x, IFC %s, %s, %s\n",
2829
               QIC02_TAPE_IRQ, QIC02_TAPE_DMA,
2830
# if QIC02_TAPE_IFC == WANGTEK
2831
               QIC02_STAT_PORT, "Wangtek",
2832
# elif QIC02_TAPE_IFC == ARCHIVE
2833
               QIC02_CMD_PORT, "Archive",
2834
# elif QIC02_TAPE_IFC == MOUNTAIN
2835
               QIC02_CMD_PORT, "Mountain",
2836
# else
2837
#  error
2838
# endif
2839
               rcs_revision, rcs_date);
2840
        if (qic02_get_resources()) {
2841
                return -ENODEV;
2842
        }
2843
#else
2844
        printk(TPQIC02_NAME ": Runtime config, %s, %s\n",
2845
               rcs_revision, rcs_date);
2846
#endif
2847
        printk(TPQIC02_NAME ": DMA buffers: %u blocks\n", NR_BLK_BUF);
2848
        /* If we got this far, install driver functions */
2849
        if (devfs_register_chrdev
2850
            (QIC02_TAPE_MAJOR, TPQIC02_NAME, &qic02_tape_fops)) {
2851
                printk(TPQIC02_NAME ": Unable to get chrdev major %d\n",
2852
                       QIC02_TAPE_MAJOR);
2853
#ifndef CONFIG_QIC02_DYNCONF
2854
                qic02_release_resources();
2855
#endif
2856
                return -ENODEV;
2857
        }
2858
        devfs_register(NULL, "ntpqic11", DEVFS_FL_DEFAULT,
2859
                       QIC02_TAPE_MAJOR, 2,
2860
                       S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP,
2861
                       &qic02_tape_fops, NULL);
2862
        devfs_register(NULL, "tpqic11", DEVFS_FL_DEFAULT,
2863
                       QIC02_TAPE_MAJOR, 3,
2864
                       S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP,
2865
                       &qic02_tape_fops, NULL);
2866
        devfs_register(NULL, "ntpqic24", DEVFS_FL_DEFAULT,
2867
                       QIC02_TAPE_MAJOR, 4,
2868
                       S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP,
2869
                       &qic02_tape_fops, NULL);
2870
        devfs_register(NULL, "tpqic24", DEVFS_FL_DEFAULT,
2871
                       QIC02_TAPE_MAJOR, 5,
2872
                       S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP,
2873
                       &qic02_tape_fops, NULL);
2874
        devfs_register(NULL, "ntpqic120", DEVFS_FL_DEFAULT,
2875
                       QIC02_TAPE_MAJOR, 6,
2876
                       S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP,
2877
                       &qic02_tape_fops, NULL);
2878
        devfs_register(NULL, "tpqic120", DEVFS_FL_DEFAULT,
2879
                       QIC02_TAPE_MAJOR, 7,
2880
                       S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP,
2881
                       &qic02_tape_fops, NULL);
2882
        devfs_register(NULL, "ntpqic150", DEVFS_FL_DEFAULT,
2883
                       QIC02_TAPE_MAJOR, 8,
2884
                       S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP,
2885
                       &qic02_tape_fops, NULL);
2886
        devfs_register(NULL, "tpqic150", DEVFS_FL_DEFAULT,
2887
                       QIC02_TAPE_MAJOR, 9,
2888
                       S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP,
2889
                       &qic02_tape_fops, NULL);
2890
        init_waitqueue_head(&qic02_tape_transfer);
2891
        /* prepare timer */
2892
        TIMEROFF;
2893
        init_timer(&tp_timer);
2894
        tp_timer.function = qic02_tape_times_out;
2895
 
2896
#ifndef CONFIG_QIC02_DYNCONF
2897
        if (tape_reset(0) != TE_OK
2898
            || tp_sense(TP_WRP | TP_POR | TP_CNI) != TE_OK) {
2899
                /* No drive detected, so vanish */
2900
                tpqputs(TPQD_ALWAYS,
2901
                        "No drive detected -- driver going on vacation...");
2902
                qic02_release_resources();
2903
                status_dead = YES;
2904
                return -ENODEV;
2905
        } else {
2906
                if (is_exception()) {
2907
                        tpqputs(TPQD_ALWAYS, "exception detected\n");
2908
                        (void) tp_sense(TP_WRP | TP_POR | TP_CNI);
2909
                }
2910
        }
2911
#endif
2912
 
2913
        /* initialize generic status for ioctl requests */
2914
 
2915
        ioctl_status.mt_type = QIC02_TAPE_DRIVE;        /* MT_IS* id nr */
2916
 
2917
        ioctl_status.mt_resid = 0;       /* ---residual count */
2918
        ioctl_status.mt_gstat = 0;       /* ---generic status */
2919
        ioctl_status.mt_erreg = 0;       /* not used */
2920
        ioctl_status.mt_fileno = 0;      /* number of current file on tape */
2921
        ioctl_status.mt_blkno = 0;       /* number of current (logical) block */
2922
 
2923
        return 0;
2924
}                               /* qic02_tape_init */
2925
 
2926
#ifdef MODULE
2927
 
2928
void cleanup_module(void)
2929
{
2930
        if (status_zombie == NO) {
2931
                qic02_release_resources();
2932
        }
2933
        devfs_unregister_chrdev(QIC02_TAPE_MAJOR, TPQIC02_NAME);
2934
        devfs_unregister(devfs_find_handle
2935
                         (NULL, "ntpqic11", QIC02_TAPE_MAJOR, 2,
2936
                          DEVFS_SPECIAL_CHR, 0));
2937
        devfs_unregister(devfs_find_handle
2938
                         (NULL, "tpqic11", QIC02_TAPE_MAJOR, 3,
2939
                          DEVFS_SPECIAL_CHR, 0));
2940
        devfs_unregister(devfs_find_handle
2941
                         (NULL, "ntpqic24", QIC02_TAPE_MAJOR, 4,
2942
                          DEVFS_SPECIAL_CHR, 0));
2943
        devfs_unregister(devfs_find_handle
2944
                         (NULL, "tpqic24", QIC02_TAPE_MAJOR, 5,
2945
                          DEVFS_SPECIAL_CHR, 0));
2946
        devfs_unregister(devfs_find_handle
2947
                         (NULL, "ntpqic120", QIC02_TAPE_MAJOR, 6,
2948
                          DEVFS_SPECIAL_CHR, 0));
2949
        devfs_unregister(devfs_find_handle
2950
                         (NULL, "tpqic120", QIC02_TAPE_MAJOR, 7,
2951
                          DEVFS_SPECIAL_CHR, 0));
2952
        devfs_unregister(devfs_find_handle
2953
                         (NULL, "ntpqic150", QIC02_TAPE_MAJOR, 8,
2954
                          DEVFS_SPECIAL_CHR, 0));
2955
        devfs_unregister(devfs_find_handle
2956
                         (NULL, "tpqic150", QIC02_TAPE_MAJOR, 9,
2957
                          DEVFS_SPECIAL_CHR, 0));
2958
}
2959
 
2960
int init_module(void)
2961
{
2962
        int retval;
2963
        retval = qic02_tape_init();
2964
# ifdef CONFIG_QIC02_DYNCONF
2965
        /* This allows the dynamic config program to setup the card
2966
         * by presetting qic02_tape_dynconf via insmod
2967
         */
2968
        if (!retval && qic02_tape_dynconf.ifc_type) {
2969
                retval = update_ifc_masks(qic02_tape_dynconf.ifc_type);
2970
                if (retval) {
2971
                        cleanup_module();
2972
                }
2973
        }
2974
# endif
2975
        return retval;
2976
}
2977
#endif
2978
 
2979
MODULE_LICENSE("GPL");
2980
EXPORT_NO_SYMBOLS;

powered by: WebSVN 2.1.0

© copyright 1999-2026 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.