- Kai Germaschewski: ymfpci cleanups and resource leak fixes
[davej-history.git] / drivers / char / tpqic02.c
blob536942f413984833023d7bcb66a9d98d506535f4
1 /* $Id: tpqic02.c,v 1.10 1997/01/26 07:13:20 davem Exp $
3 * Driver for tape drive support for Linux-i386
5 * Copyright (c) 1992--1996 by H. H. Bergman. All rights reserved.
6 * Current e-mail address: hennus@cybercomm.nl
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.
12 * I will not accept any responsibility for damage caused directly or
13 * indirectly by this program, or code derived from this program.
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.
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.
25 * This driver was partially inspired by the 'wt' driver in the 386BSD
26 * source distribution, which carries the following copyright notice:
28 * Copyright (c) 1991 The Regents of the University of California.
29 * All rights reserved.
31 * You are not allowed to change this line nor the text above.
33 * 1996/10/10 Emerald changes
35 * 1996/05/21 Misc changes+merges+cleanups + I/O reservations
37 * 1996/05/20 Module support patches submitted by Brian McCauley.
39 * 1994/05/03 Initial attempt at Mountain support for the Mountain 7150.
40 * Based on patches provided by Erik Jacobson. Still incomplete, I suppose.
42 * 1994/02/07 Archive changes & some cleanups by Eddy Olk.
44 * 1994/01/19 Speed measuring stuff moved from aperf.h to delay.h.
45 * BogoMips (tm) introduced by Linus.
47 * 1993/01/25 Kernel udelay. Eof fixups.
49 * 1992/09/19 Some changes based on patches by Eddy Olk to support
50 * Archive SC402/SC499R controller cards.
52 * 1992/05/27 First release.
54 * 1992/05/26 Initial version. Copyright H. H. Bergman 1992
57 /* After the legalese, now the important bits:
59 * This is a driver for the Wangtek 5150 tape drive with
60 * a QIC-02 controller for ISA-PC type computers.
61 * Hopefully it will work with other QIC-02 tape drives as well.
63 * Make sure your setup matches the configuration parameters.
64 * Also, be careful to avoid IO conflicts with other devices!
69 #define TDEBUG
72 #define REALLY_SLOW_IO /* it sure is ... */
74 #include <linux/module.h>
76 #include <linux/config.h>
78 #include <linux/sched.h>
79 #include <linux/timer.h>
80 #include <linux/fs.h>
81 #include <linux/kernel.h>
82 #include <linux/major.h>
83 #include <linux/errno.h>
84 #include <linux/mtio.h>
85 #include <linux/fcntl.h>
86 #include <linux/delay.h>
87 #include <linux/ioport.h>
88 #include <linux/tpqic02.h>
89 #include <linux/mm.h>
90 #include <linux/malloc.h>
91 #include <linux/init.h>
92 #include <linux/smp_lock.h>
93 #include <linux/devfs_fs_kernel.h>
95 #include <asm/dma.h>
96 #include <asm/system.h>
97 #include <asm/io.h>
98 #include <asm/uaccess.h>
100 /* check existence of required configuration parameters */
101 #if !defined(QIC02_CMD_PORT) || !defined(QIC02_TAPE_IRQ) || !defined(QIC02_TAPE_DMA)
102 # error qic02_tape configuration error
103 #endif
106 #define TPQIC02_NAME "tpqic02"
108 /* Linux outb() commands have (value,port) as parameters.
109 * One might expect (port,value) instead, so beware!
112 #ifdef CONFIG_QIC02_DYNCONF
113 /* This holds the dynamic configuration info for the interface
114 * card+drive info if runtime configuration has been selected.
117 static struct mtconfiginfo qic02_tape_dynconf = /* user settable */
118 { 0, 0, BOGUS_IRQ, 0, 0, TPQD_DEFAULT_FLAGS, };
119 static struct qic02_ccb qic02_tape_ccb; /* private stuff */
121 #else
123 unsigned long qic02_tape_debug = TPQD_DEFAULT_FLAGS;
125 # if ((QIC02_TAPE_IFC!=WANGTEK) && (QIC02_TAPE_IFC!=ARCHIVE) && (QIC02_TAPE_IFC!=MOUNTAIN))
126 # error No valid interface card specified
127 # endif
128 #endif /* CONFIG_QIC02_DYNCONF */
130 static volatile int ctlbits; /* control reg bits for tape interface */
132 static wait_queue_head_t qic02_tape_transfer; /* sync rw with interrupts */
134 static volatile struct mtget ioctl_status; /* current generic status */
136 static volatile struct tpstatus tperror; /* last drive status */
138 static char rcs_revision[] = "$Revision: 1.10 $";
139 static char rcs_date[] = "$Date: 1997/01/26 07:13:20 $";
141 /* Flag bits for status and outstanding requests.
142 * (Could all be put in one bit-field-struct.)
143 * Some variables need `volatile' because they may be modified
144 * by an interrupt.
146 static volatile flag status_dead = YES; /* device is legally dead until proven alive */
147 static flag status_zombie = YES; /* it's `zombie' until irq/dma allocated */
149 static volatile flag status_bytes_wr = NO; /* write FM at close or not */
150 static volatile flag status_bytes_rd = NO; /* (rd|wr) used for rewinding */
152 static volatile unsigned long status_cmd_pending; /* cmd in progress */
153 static volatile flag status_expect_int = NO; /* ready for interrupts */
154 static volatile flag status_timer_on = NO; /* using time-out */
155 static volatile int status_error; /* int handler may detect error */
156 static volatile flag status_eof_detected = NO; /* end of file */
157 static volatile flag status_eom_detected = NO; /* end of recorded media */
158 static volatile flag status_eot_detected = NO; /* end of tape */
159 static volatile flag doing_read = NO;
160 static volatile flag doing_write = NO;
162 static volatile unsigned long dma_bytes_todo;
163 static volatile unsigned long dma_bytes_done;
164 static volatile unsigned dma_mode; /* !=0 also means DMA in use */
165 static flag need_rewind = YES;
167 static kdev_t current_tape_dev;
168 static int extra_blocks_left = BLOCKS_BEYOND_EW;
170 static struct timer_list tp_timer;
172 /* return_*_eof:
173 * NO: not at EOF,
174 * YES: tell app EOF was reached (return 0).
176 * return_*_eof==YES && reported_*_eof==NO ==>
177 * return current buffer, next time(s) return EOF.
179 * return_*_eof==YES && reported_*_eof==YES ==>
180 * at EOF and application knows it, so we can
181 * move on to the next file.
184 static flag return_read_eof = NO; /* set to signal app EOF was reached */
185 static flag return_write_eof = NO;
186 static flag reported_read_eof = NO; /* set when we've done that */
187 static flag reported_write_eof = NO;
190 /* This is for doing `mt seek <blocknr>' */
191 static char seek_addr_buf[AR_SEEK_BUF_SIZE];
194 /* In write mode, we have to write a File Mark after the last block written,
195 * when the tape device is closed. Tape repositioning and reading in write
196 * mode is allowed as long as no actual writing has been done. After writing
197 * the File Mark, repositioning and reading are allowed again.
199 static int mode_access; /* access mode: READ or WRITE */
201 static int qic02_get_resources(void);
202 static void qic02_release_resources(void);
204 /* This is a pointer to the actual kernel buffer where the interrupt routines
205 * read from/write to. It is needed because the DMA channels 1 and 3 cannot
206 * always access the user buffers. [The kernel buffer must reside in the
207 * lower 16MBytes of system memory because of the DMA controller.] The user
208 * must ensure that a large enough buffer is passed to the kernel, in order
209 * to reduce tape repositioning wear and tear.
211 static unsigned long buffaddr; /* physical address of buffer */
213 /* This translates minor numbers to the corresponding recording format: */
214 static const char *format_names[] = {
215 "not set", /* for dumb drives unable to handle format selection */
216 "11", /* extinct */
217 "24",
218 "120",
219 "150",
220 "300", /* untested. */
221 "600" /* untested. */
225 /* `exception_list' is needed for exception status reporting.
226 * Exceptions 1..14 are defined by QIC-02 rev F.
227 * The drive status is matched sequentially to each entry,
228 * ignoring irrelevant bits, until a match is found. If no
229 * match is found, exception number 0 is used. (That should of
230 * course never happen...) The original table was based on the
231 * "Exception Status Summary" in QIC-02 rev F, but some changes
232 * were required to make it work with real-world drives.
234 * Exception 2 (CNI) is changed to also cover status 0x00e0 (mask USL),
235 * Exception 4 (EOM) is changed to also cover status 0x8288 (mask EOR),
236 * Exception 11 (FIL) is changed to also cover status 0x0089 (mask EOM).
237 * Exception 15 (EOR) is added for seek-to-end-of-data (catch EOR),
238 * Exception 16 (BOM) is added for beginning-of-media (catch BOM).
240 * Had to swap EXC_NDRV and EXC_NCART to ensure that extended EXC_NCART
241 * (because of the incorrect Wangtek status code) doesn't catch the
242 * EXC_NDRV first.
244 static struct exception_list_type {
245 unsigned short mask, code;
246 const char *msg;
247 /* EXC_nr attribute should match with tpqic02.h */
248 } exception_list[] = {
249 {0, 0,
250 "Unknown exception status code", /* extra: 0 */},
251 {~(0), TP_ST0|TP_CNI|TP_USL|TP_WRP,
252 "Drive not online" /* 1 */},
253 /* Drive presence goes before cartridge presence. */
254 {~(TP_WRP|TP_USL), TP_ST0|TP_CNI,
255 /* My Wangtek 5150EQ sometimes reports a status code
256 * of 0x00e0, which is not a valid exception code, but
257 * I think it should be recognized as "NO CARTRIDGE".
259 "Cartridge not in place" /* 2 */},
260 {(unsigned short) ~(TP_ST1|TP_BOM), (TP_ST0|TP_WRP),
261 "Write protected cartridge" /* 3 */},
262 {(unsigned short) ~(TP_ST1|TP_EOR), (TP_ST0|TP_EOM),
263 "End of media" /* 4 */},
264 {~TP_WRP, TP_ST0|TP_UDA| TP_ST1|TP_BOM,
265 "Read or Write abort. Rewind tape." /* 5 */},
266 {~TP_WRP, TP_ST0|TP_UDA,
267 "Read error. Bad block transferred." /* 6 */},
268 {~TP_WRP, TP_ST0|TP_UDA|TP_BNL,
269 "Read error. Filler block transferred." /* 7 */},
270 {~TP_WRP, TP_ST0|TP_UDA|TP_BNL |TP_ST1|TP_NDT,
271 "Read error. No data detected." /* 8 */},
272 {~TP_WRP, TP_ST0|TP_EOM|TP_UDA|TP_BNL |TP_ST1|TP_NDT,
273 "Read error. No data detected. EOM." /* 9 */},
274 {~(TP_WRP|TP_MBD|TP_PAR|TP_EOR), TP_ST0|TP_UDA|TP_BNL |TP_ST1|TP_NDT|TP_BOM,
275 "Read error. No data detected. BOM." /* 10 */},
276 {~(TP_WRP|TP_EOM), TP_ST0|TP_FIL,
277 /* Status 0x0089 (EOM & FM) is viewed as an FM,
278 * because it can only happen during a read.
279 * EOM is checked separately for an FM condition.
281 "File mark detected" /* 11 */},
282 {~(TP_ST0|TP_CNI|TP_USL|TP_WRP|TP_BOM), TP_ST1|TP_ILL,
283 "Illegal command" /* 12 */},
284 {~(TP_ST0|TP_CNI|TP_USL|TP_WRP|TP_BOM), TP_ST1|TP_POR,
285 "Reset occurred" /* 13 */},
286 {~TP_WRP, TP_ST0|TP_FIL|TP_MBD, /* NOTE: ST1 not set! */
287 "Marginal block detected" /* 14 */},
288 {~(TP_ST0|TP_WRP|TP_EOM|TP_UDA|TP_BNL|TP_FIL |TP_NDT), TP_ST1|TP_EOR,
289 /********** Is the extra TP_NDT really needed Eddy? **********/
290 "End of recorded media" /* extra: 15 */},
291 /* 15 is returned when SEEKEOD completes successfully */
292 {~(TP_WRP|TP_ST0), TP_ST1|TP_BOM,
293 "Beginning of media" /* extra: 16 */}
295 #define NR_OF_EXC (sizeof(exception_list)/sizeof(struct exception_list_type))
297 /* Compare expected struct size and actual struct size. This
298 * is useful to catch programs compiled with old #includes.
300 #define CHECK_IOC_SIZE(structure) \
301 if (_IOC_SIZE(iocmd) != sizeof(struct structure)) { \
302 tpqputs(TPQD_ALWAYS, "sizeof(struct " #structure \
303 ") does not match!"); \
304 return -EFAULT; \
307 static void tpqputs(unsigned long flags, const char *s)
309 if ((flags & TPQD_ALWAYS) || (flags & QIC02_TAPE_DEBUG))
310 printk(TPQIC02_NAME ": %s\n", s);
311 } /* tpqputs */
314 /* Perform byte order swapping for a 16-bit word.
316 * [FIXME] This should probably be in include/asm/
317 * ([FIXME] i486 can do this faster)
319 static inline void byte_swap_w(volatile unsigned short * w)
321 int t = *w;
323 *w = (t>>8) | ((t & 0xff)<<8);
328 /* Init control register bits on interface card.
329 * For Archive, interrupts must be enabled explicitly.
330 * Wangtek interface card requires ONLINE to be set, Archive SC402/SC499R
331 * cards keep it active all the time.
333 static void ifc_init(void)
335 if (QIC02_TAPE_IFC == WANGTEK) /* || (QIC02_TAPE_IFC == EVEREX) */
337 ctlbits = WT_CTL_ONLINE; /* online */
338 outb_p(ctlbits, QIC02_CTL_PORT);
340 else if (QIC02_TAPE_IFC == ARCHIVE)
342 ctlbits = 0; /* no interrupts yet */
343 outb_p(ctlbits, QIC02_CTL_PORT);
344 outb_p(0, AR_RESET_DMA_PORT); /* dummy write to reset DMA */
346 else /* MOUNTAIN */
348 ctlbits = MTN_CTL_ONLINE; /* online, and logic enabled */
349 outb_p(ctlbits, QIC02_CTL_PORT);
351 } /* ifc_init */
354 static void report_qic_exception(unsigned n)
356 if (n >= NR_OF_EXC)
358 tpqputs(TPQD_ALWAYS, "Oops -- report_qic_exception");
359 n = 0;
361 if (TPQDBG(SENSE_TEXT) || n==0)
363 printk(TPQIC02_NAME ": sense: %s\n", exception_list[n].msg);
365 } /* report_qic_exception */
368 /* Try to map the drive-exception bits `s' to a predefined "exception number",
369 * by comparing the significant exception bits for each entry in the
370 * exception table (`exception_list[]').
371 * It is assumed that s!=0.
373 static int decode_qic_exception_nr(unsigned s)
375 int i;
377 for (i=1; i<NR_OF_EXC; i++)
379 if ((s & exception_list[i].mask)==exception_list[i].code)
381 return i;
384 printk(TPQIC02_NAME ": decode_qic_exception_nr: exception(%x) not recognized\n", s);
385 return 0;
386 } /* decode_qic_exception_nr */
390 /* Perform appropriate action for certain exceptions.
391 * should return a value to indicate stop/continue (in case of bad blocks)
393 static void handle_qic_exception(int exnr, int exbits)
395 if (exnr==EXC_NCART)
397 /* Cartridge was changed. Redo sense().
398 * EXC_NCART should be handled in open().
399 * It is not permitted to remove the tape while
400 * the tape driver has open files.
402 need_rewind = YES;
403 status_eof_detected = NO;
404 status_eom_detected = NO;
406 else if (exnr==EXC_XFILLER)
408 tpqputs(TPQD_ALWAYS, "[Bad block -- filler data transferred.]");
410 else if (exnr==EXC_XBAD)
412 tpqputs(TPQD_ALWAYS, "[CRC failed!]");
414 else if (exnr==EXC_MARGINAL)
416 /* A marginal block behaves much like a FM.
417 * User may continue reading, if desired.
419 tpqputs(TPQD_ALWAYS, "[Marginal block]");
420 doing_read = NO;
422 else if (exnr==EXC_FM)
424 doing_read = NO;
426 } /* handle_qic_exception */
429 static inline int is_exception(void)
431 return (inb(QIC02_STAT_PORT) & QIC02_STAT_EXCEPTION) == 0;
432 } /* is_exception */
435 /* Reset the tape drive and controller.
436 * When reset fails, it marks the drive as dead and all
437 * requests (except reset) are to be ignored (ENXIO).
439 static int tape_reset(int verbose)
441 ifc_init(); /* reset interface card */
443 /* assert reset */
444 if (QIC02_TAPE_IFC == MOUNTAIN)
446 outb_p(ctlbits & ~MTN_QIC02_CTL_RESET_NOT, QIC02_CTL_PORT);
448 else /* WANGTEK, ARCHIVE */
450 outb_p(ctlbits | QIC02_CTL_RESET, QIC02_CTL_PORT);
453 /* Next, we need to wait >=25 usec. */
454 udelay(30);
456 /* after reset, we will be at BOT (modulo an automatic rewind) */
457 status_eof_detected = NO;
458 status_eom_detected = NO;
459 status_cmd_pending = 0;
460 need_rewind = YES;
461 doing_read = doing_write = NO;
462 ioctl_status.mt_fileno = ioctl_status.mt_blkno = 0;
464 /* de-assert reset */
465 if (QIC02_TAPE_IFC == MOUNTAIN)
467 outb_p(ctlbits | MTN_QIC02_CTL_RESET_NOT, QIC02_CTL_PORT);
469 else
471 outb_p(ctlbits & ~QIC02_CTL_RESET, QIC02_CTL_PORT);
474 /* KLUDGE FOR G++ BUG */
475 { int stat = inb_p(QIC02_STAT_PORT);
476 status_dead = ((stat & QIC02_STAT_RESETMASK) != QIC02_STAT_RESETVAL); }
477 /* if successful, inb(STAT) returned RESETVAL */
478 if (status_dead == YES)
480 printk(TPQIC02_NAME ": reset failed!\n");
482 else if (verbose)
484 printk(TPQIC02_NAME ": reset successful\n");
487 return (status_dead == YES)? TE_DEAD : TE_OK;
488 } /* tape_reset */
492 /* Notify tape drive of a new command. It only waits for the
493 * command to be accepted, not for the actual command to complete.
495 * Before calling this routine, QIC02_CMD_PORT must have been loaded
496 * with the command to be executed.
497 * After this routine, the exception bit must be checked.
498 * This routine is also used by rdstatus(), so in that case, any exception
499 * must be ignored (`ignore_ex' flag).
501 static int notify_cmd(char cmd, short ignore_ex)
503 int i;
505 outb_p(cmd, QIC02_CMD_PORT); /* output the command */
507 /* wait 1 usec before asserting /REQUEST */
508 udelay(1);
510 if ((!ignore_ex) && is_exception()) {
511 tpqputs(TPQD_ALWAYS, "*** exception detected in notify_cmd");
512 /** force a reset here **/
513 if (tape_reset(1)==TE_DEAD)
514 return TE_DEAD;
515 if (is_exception()) {
516 tpqputs(TPQD_ALWAYS, "exception persists after reset.");
517 tpqputs(TPQD_ALWAYS, " ^ exception ignored.");
521 outb_p(ctlbits | QIC02_CTL_REQUEST, QIC02_CTL_PORT); /* set request bit */
522 i = TAPE_NOTIFY_TIMEOUT;
523 /* The specs say this takes about 500 usec, but there is no upper limit!
524 * If the drive were busy retensioning or something like that,
525 * it could be *much* longer!
527 while ((inb_p(QIC02_STAT_PORT) & QIC02_STAT_READY) && (--i>0))
528 /*skip*/; /* wait for ready */
529 if (i==0) {
530 tpqputs(TPQD_ALWAYS, "timed out waiting for ready in notify_cmd");
531 status_dead = YES;
532 return TE_TIM;
535 outb_p(ctlbits & ~QIC02_CTL_REQUEST, QIC02_CTL_PORT); /* reset request bit */
536 i = TAPE_NOTIFY_TIMEOUT;
537 /* according to the specs this one should never time-out */
538 while (((inb_p(QIC02_STAT_PORT) & QIC02_STAT_READY) == 0) && (--i>0))
539 /*skip*/; /* wait for not ready */
540 if (i==0) {
541 tpqputs(TPQD_ALWAYS, "timed out waiting for !ready in notify_cmd");
542 status_dead = YES;
543 return TE_TIM;
545 /* command accepted */
546 return TE_OK;
547 } /* notify_cmd */
551 /* Wait for a command to complete, with timeout */
552 static int wait_for_ready(time_t timeout)
554 int stat;
555 time_t spin_t;
557 /* Wait for ready or exception, without driving the loadavg up too much.
558 * In most cases, the tape drive already has READY asserted,
559 * so optimize for that case.
561 * First, busy wait a few usec:
563 spin_t = 50;
564 while (((stat = inb_p(QIC02_STAT_PORT) & QIC02_STAT_MASK) == QIC02_STAT_MASK) && (--spin_t>0))
565 /*SKIP*/;
566 if ((stat & QIC02_STAT_READY) == 0)
567 return TE_OK; /* covers 99.99% of all calls */
569 /* Then use schedule() a few times */
570 spin_t = 3; /* max 0.03 sec busy waiting */
571 if (spin_t > timeout)
572 spin_t = timeout;
573 timeout -= spin_t;
574 spin_t += jiffies;
576 while (((stat = inb_p(QIC02_STAT_PORT) & QIC02_STAT_MASK) == QIC02_STAT_MASK) && time_before(jiffies, spin_t))
577 schedule(); /* don't waste all the CPU time */
578 if ((stat & QIC02_STAT_READY) == 0)
579 return TE_OK;
581 /* If we reach this point, we probably need to wait much longer, or
582 * an exception occurred. Either case is not very time-critical.
583 * Check the status port only a few times every second.
584 * A interval of less than 0.10 sec will not be noticed by the user,
585 * more than 0.40 sec may give noticeable delays.
587 spin_t += timeout;
588 TPQDEB({printk("wait_for_ready: additional timeout: %d\n", spin_t);})
590 /* not ready and no exception && timeout not expired yet */
591 while (((stat = inb_p(QIC02_STAT_PORT) & QIC02_STAT_MASK) == QIC02_STAT_MASK) && time_before(jiffies, spin_t)) {
592 /* be `nice` to other processes on long operations... */
593 current->state = TASK_INTERRUPTIBLE;
594 /* nap 0.30 sec between checks, */
595 /* but could be woken up earlier by signals... */
596 schedule_timeout(3*HZ/10);
599 /* don't use jiffies for this test because it may have changed by now */
600 if ((stat & QIC02_STAT_MASK) == QIC02_STAT_MASK) {
601 tpqputs(TPQD_ALWAYS, "wait_for_ready() timed out");
602 return TE_TIM;
605 if ((stat & QIC02_STAT_EXCEPTION) == 0) {
606 tpqputs(TPQD_ALWAYS, "exception detected after waiting_for_ready");
607 return TE_EX;
608 } else {
609 return TE_OK;
611 } /* wait_for_ready */
615 /* Send some data to the drive */
616 static int send_qic02_data(char sb[], unsigned size, int ignore_ex)
618 int i, stat;
620 for (i=0; i<size; i++) {
622 stat = wait_for_ready(TIM_S);
623 if (stat != TE_OK)
624 return stat;
626 stat = notify_cmd(sb[i], ignore_ex);
627 if (stat != TE_OK)
628 return stat;
630 return TE_OK;
632 } /* send_qic02_data */
635 /* Send a QIC-02 command (`cmd') to the tape drive, with
636 * a time-out (`timeout').
637 * This one is also used by tp_sense(), so we must have
638 * a flag to disable exception checking (`ignore_ex').
640 * On entry, the controller is supposed to be READY.
642 static int send_qic02_cmd(int cmd, time_t timeout, int ignore_ex)
644 int stat;
646 stat = inb_p(QIC02_STAT_PORT);
647 if ((stat & QIC02_STAT_EXCEPTION) == 0) { /* if exception */
648 tpqputs(TPQD_ALWAYS, "send_qic02_cmd: Exception!");
649 return TE_EX;
651 if (stat & QIC02_STAT_READY) { /* if not ready */
652 tpqputs(TPQD_ALWAYS, "send_qic02_cmd: not Ready!");
653 return TE_ERR;
656 /* assert(ready & !exception) */
658 /* Remember current command for later re-use with dma transfers.
659 * (For reading/writing multiple blocks.)
661 status_cmd_pending = cmd;
663 stat = notify_cmd(cmd, ignore_ex); /* tell drive new command was loaded, */
664 /* inherit exception check. */
665 if (TP_HAVE_SEEK && (cmd == AR_QCMDV_SEEK_BLK)) {
666 /* This one needs to send 3 more bytes, MSB first */
667 stat = send_qic02_data(seek_addr_buf, sizeof(seek_addr_buf), ignore_ex);
670 if (stat != TE_OK) {
671 tpqputs(TPQD_ALWAYS, "send_qic02_cmd failed");
673 return stat;
674 } /* send_qic02_cmd */
678 /* Get drive status. Assume drive is ready or has exception set.
679 * (or will be in <1000 usec.)
680 * Extra parameters added because of 'Read Extended Status 3' command.
682 static int rdstatus(char *stp, unsigned size, char qcmd)
684 int s, n;
685 char *q = stp;
687 /* Try to busy-wait a few (700) usec, after that de-schedule.
689 * The problem is, if we don't de-schedule, performance will
690 * drop to zero when the drive is not responding and if we
691 * de-schedule immediately, we waste a lot of time because a
692 * task switch is much longer than we usually have to wait here.
694 n = 1000; /* 500 is not enough on a 486/33 */
695 while ((n>0) && ((inb_p(QIC02_STAT_PORT) & QIC02_STAT_MASK) == QIC02_STAT_MASK))
696 n--; /* wait for ready or exception or timeout */
697 if (n==0) {
698 /* n (above) should be chosen such that on your machine
699 * you rarely ever see the message below, and it should
700 * be small enough to give reasonable response time.]
702 tpqputs(TPQD_ALWAYS, "waiting looong in rdstatus() -- drive dead?");
703 while ((inb_p(QIC02_STAT_PORT) & QIC02_STAT_MASK) == QIC02_STAT_MASK)
704 schedule();
705 tpqputs(TPQD_ALWAYS, "finished waiting in rdstatus()");
708 (void) notify_cmd(qcmd, 1); /* send read status command */
709 /* ignore return code -- should always be ok, STAT may contain
710 * exception flag from previous exception which we are trying to clear.
713 if (TP_DIAGS(current_tape_dev))
714 printk(TPQIC02_NAME ": reading status bytes: ");
716 for (q=stp; q<stp+size; q++)
718 do s = inb_p(QIC02_STAT_PORT);
719 while ((s & QIC02_STAT_MASK) == QIC02_STAT_MASK); /* wait for ready or exception */
721 if ((s & QIC02_STAT_EXCEPTION) == 0) { /* if exception */
722 tpqputs(TPQD_ALWAYS, "rdstatus: exception error");
723 ioctl_status.mt_erreg = 0; /* dunno... */
724 return TE_NS; /* error, shouldn't happen... */
727 *q = inb_p(QIC02_DATA_PORT); /* read status byte */
729 if (TP_DIAGS(current_tape_dev))
730 printk("[%1d]=0x%x ", q-stp, (unsigned) (*q) & 0xff);
732 outb_p(ctlbits | QIC02_CTL_REQUEST, QIC02_CTL_PORT); /* set request */
734 while ((inb_p(QIC02_STAT_PORT) & QIC02_STAT_READY) == 0); /* wait for not ready */
736 udelay(22); /* delay >20 usec */
738 outb_p(ctlbits & ~QIC02_CTL_REQUEST, QIC02_CTL_PORT); /* un-set request */
742 /* Specs say we should wait for READY here.
743 * My drive doesn't seem to need it here yet, but others do?
745 while (inb_p(QIC02_STAT_PORT) & QIC02_STAT_READY)
746 /*skip*/; /* wait for ready */
748 if (TP_DIAGS(current_tape_dev))
749 printk("\n");
751 return TE_OK;
752 } /* rdstatus */
756 /* Get standard status (6 bytes).
757 * The `.dec' and `.urc' fields are in MSB-first byte-order,
758 * so they have to be swapped first.
760 static int get_status(volatile struct tpstatus *stp)
762 int stat = rdstatus((char *) stp, TPSTATSIZE, QCMD_RD_STAT);
763 #ifdef __i386__
764 byte_swap_w(&(stp->dec));
765 byte_swap_w(&(stp->urc));
766 #else
767 /* should probably swap status bytes #definition */
768 #endif
769 return stat;
770 } /* get_status */
773 #if 0
774 /* This fails for my Wangtek drive */
775 /* get "Extended Status Register 3" (64 bytes)
777 * If the meaning of the returned bytes were known, the MT_TYPE
778 * identifier could be used to decode them, since they are
779 * "vendor unique". :-(
781 static int get_ext_status3(void)
783 char vus[64]; /* vendor unique status */
784 int stat, i;
786 tpqputs(TPQD_ALWAYS, "Attempting to read Extended Status 3...");
787 stat = rdstatus(vus, sizeof(vus), QCMD_RD_STAT_X3);
788 if (stat != TE_OK)
789 return stat;
791 tpqputs(TPQD_ALWAYS, "Returned status bytes:");
792 for (i=0; i<sizeof(vus); i++) {
793 if ( i % 8 == 0 )
794 printk("\n" TPQIC02_NAME ": %2d:");
795 printk(" %2x", vus[i] & 0xff);
797 printk("\n");
799 return TE_OK;
800 } /* get_ext_status3 */
801 #endif
804 /* Read drive status and set generic status too.
805 * NOTE: Once we do a tp_sense(), read/write transfers are killed.
807 static int tp_sense(int ignore)
809 unsigned err = 0, exnr = 0, gs = 0;
810 static void finish_rw(int cmd);
812 if (TPQDBG(SENSE_TEXT))
813 printk(TPQIC02_NAME ": tp_sense(ignore=0x%x) enter\n", ignore);
815 /* sense() is not allowed during a read or write cycle */
816 if (doing_write == YES)
817 tpqputs(TPQD_ALWAYS, "Warning: File Mark inserted because of sense() request");
818 /* The extra test is to avoid calling finish_rw during booting */
819 if ((doing_read!=NO) || (doing_write!=NO))
820 finish_rw(QCMD_RD_STAT);
822 if (get_status(&tperror) != TE_OK) {
823 tpqputs(TPQD_ALWAYS, "tp_sense: could not read tape drive status");
824 return TE_ERR;
827 err = tperror.exs; /* get exception status bits */
828 if (err & (TP_ST0|TP_ST1))
829 printk(TPQIC02_NAME ": tp_sense: status: %x, error count: %d, underruns: %d\n",
830 tperror.exs, tperror.dec, tperror.urc);
831 else if ((tperror.dec!=0) || (tperror.urc!=0) || TPQDBG(SENSE_CNTS))
832 printk(TPQIC02_NAME ": tp_sense: no hard errors, soft error count: %d, underruns: %d\n",
833 tperror.dec, tperror.urc);
835 /* Set generic status. HP-UX defines these, but some extra would
836 * be useful. Problem is to remain compatible. [Do we want to be
837 * compatible??]
839 if (err & TP_ST0) {
840 if (err & TP_CNI) /* no cartridge */
841 gs |= GMT_DR_OPEN(-1);
842 if (status_dead == NO)
843 gs |= GMT_ONLINE(-1); /* always online */
844 if (err & TP_USL) /* not online */
845 gs &= ~GMT_ONLINE(-1);
846 if (err & TP_WRP)
847 gs |= GMT_WR_PROT(-1);
848 if (err & TP_EOM) { /* end of media */
849 gs |= GMT_EOT(-1); /* not sure this is correct for writes */
850 status_eom_detected = YES;
851 /* I don't know whether drive always reports EOF at or before EOM. */
852 status_eof_detected = YES;
854 /** if (err & TP_UDA) "Unrecoverable data error" **/
855 /** if (err & TP_BNL) "Bad block not located" **/
856 if (err & TP_FIL) {
857 gs |= GMT_EOF(-1);
858 status_eof_detected = YES;
861 if (err & TP_ST1) {
862 /** if (err & TP_ILL) "Illegal command" **/
863 /** if (err & TP_NDT) "No data detected" **/
864 /** if (err & TP_MBD) "Marginal block detected" **/
865 if (err & TP_BOM)
866 gs |= GMT_BOT(-1); /* beginning of tape */
868 ioctl_status.mt_gstat = gs;
869 ioctl_status.mt_dsreg = tperror.exs; /* "drive status" */
870 ioctl_status.mt_erreg = tperror.dec; /* "sense key error" */
872 if (err & (TP_ST0|TP_ST1)) {
873 /* My Wangtek occasionally reports `status' 1212 which should be ignored. */
874 exnr = decode_qic_exception_nr(err);
875 handle_qic_exception(exnr, err); /* update driver state wrt drive status */
876 report_qic_exception(exnr);
878 err &= ~ignore; /* mask unwanted errors -- not the correct way, use exception nrs?? */
879 if (((err & TP_ST0) && (err & REPORT_ERR0)) ||
880 ((err & TP_ST1) && (err & REPORT_ERR1)))
881 return TE_ERR;
882 return TE_OK;
883 } /* tp_sense */
887 /* Wait for a wind or rewind operation to finish or
888 * to time-out. (May take very long).
890 static int wait_for_rewind(time_t timeout)
892 int stat;
894 stat = inb(QIC02_STAT_PORT) & QIC02_STAT_MASK;
895 if (TPQDBG(REWIND))
896 printk(TPQIC02_NAME ": Waiting for (re-)wind to finish: stat=0x%x\n", stat);
898 stat = wait_for_ready(timeout);
900 if (stat != TE_OK) {
901 tpqputs(TPQD_ALWAYS, "(re-) winding failed\n");
903 return stat;
904 } /* wait_for_rewind */
908 /* Perform a full QIC02 command, and wait for completion,
909 * check status when done. Complain about exceptions.
911 * This function should return an OS error code when
912 * something goes wrong, 0 otherwise.
914 static int ll_do_qic_cmd(int cmd, time_t timeout)
916 int stat;
918 if (status_dead == YES) {
919 tpqputs(TPQD_ALWAYS, "Drive is dead. Do a `mt reset`.");
920 return -ENXIO; /* User should do an MTRESET. */
923 stat = wait_for_ready(timeout); /* wait for ready or exception */
924 if (stat == TE_EX) {
925 if (tp_sense(TP_WRP|TP_BOM|TP_EOM|TP_FIL)!=TE_OK)
926 return -EIO;
927 /* else nothing to worry about, I hope */
928 stat = TE_OK;
930 if (stat != TE_OK) {
931 printk(TPQIC02_NAME ": ll_do_qic_cmd(%x, %ld) failed\n", cmd, (long) timeout);
932 return -EIO;
936 #if OBSOLETE
937 /* wait for ready since it may not be active immediately after reading status */
938 while ((inb_p(QIC02_STAT_PORT) & QIC02_STAT_READY) != 0);
939 #endif
941 stat = send_qic02_cmd(cmd, timeout, 0); /* (checks for exceptions) */
943 if (cmd==QCMD_RD_FM) {
944 status_eof_detected = NO;
945 ioctl_status.mt_fileno++;
946 /* Should update block count as well, but can't.
947 * Can do a `read address' for some drives, when MTNOP is done.
949 } else if (cmd==QCMD_WRT_FM) {
950 status_eof_detected = NO;
951 ioctl_status.mt_fileno++;
952 } else if ((cmd==QCMD_REWIND) || (cmd==QCMD_ERASE) || (cmd==QCMD_RETEN)) {
953 status_eof_detected = NO;
954 status_eom_detected = NO;
955 status_eot_detected = NO;
956 need_rewind = NO;
957 ioctl_status.mt_fileno = ioctl_status.mt_blkno = 0;
958 extra_blocks_left = BLOCKS_BEYOND_EW;
959 return_write_eof = NO;
960 return_read_eof = NO;
961 reported_read_eof = NO;
962 reported_write_eof = NO;
964 /* sense() will set eof/eom as required */
965 if (stat==TE_EX) {
966 if (tp_sense(TP_WRP|TP_BOM|TP_EOM|TP_FIL)!=TE_OK) {
967 printk(TPQIC02_NAME ": Exception persist in ll_do_qic_cmd[1](%x, %ld)", cmd, (long) timeout);
968 status_dead = YES;
969 return -ENXIO;
970 /* if rdstatus fails too, we're in trouble */
973 else if (stat!=TE_OK) {
974 printk(TPQIC02_NAME ": ll_do_qic_cmd: send_qic02_cmd failed, stat = 0x%x\n", stat);
975 return -EIO; /*** -EIO is probably not always appropriate */
979 if (timeout == TIM_R)
980 stat = wait_for_rewind(timeout);
981 else
982 stat = wait_for_ready(timeout);
984 if (stat==TE_EX) {
985 if (tp_sense((cmd==QCMD_SEEK_EOD ? /*****************************/
986 TP_EOR|TP_NDT|TP_UDA|TP_BNL|TP_WRP|TP_BOM|TP_EOM|TP_FIL :
987 TP_WRP|TP_BOM|TP_EOM|TP_FIL))!=TE_OK) {
988 printk(TPQIC02_NAME ": Exception persist in ll_do_qic_cmd[2](%x, %ld)\n", cmd, (long) timeout);
989 if (cmd!=QCMD_RD_FM)
990 status_dead = YES;
991 return -ENXIO;
992 /* if rdstatus fails too, we're in trouble */
995 else if (stat!=TE_OK) {
996 printk(TPQIC02_NAME ": ll_do_qic_cmd %x: wait failed, stat == 0x%x\n", cmd, stat);
997 return -EIO;
999 return 0;
1000 } /* ll_do_qic_cmd */
1004 * Problem: What to do when the user cancels a read/write operation
1005 * in-progress?
1007 * "Deactivating ONLINE during a READ also causes the"
1008 * "tape to be rewound to BOT." Ditto for WRITEs, except
1009 * a FM is written first. "The host may alternatively terminate
1010 * the READ/WRITE command by issuing a RFM/WFM command."
1012 * For READs:
1013 * Neither option will leave the tape positioned where it was.
1014 * Another (better?) solution is to terminate the READ by two
1015 * subsequent sense() operations, the first to stop the current
1016 * READ cycle, the second to clear the `Illegal command' exception,
1017 * because the QIC-02 specs didn't anticipate this. This is
1018 * delayed until actually needed, so a tar listing can be aborted
1019 * by the user and continued later.
1020 * If anybody has a better solution, let me know! [Also, let me
1021 * know if your drive (mine is a Wangtek5150EQ) does not accept
1022 * this sequence for canceling the read-cycle.]
1024 * For WRITEs it's simple: Just do a WRITE_FM, leaving the tape
1025 * positioned after the FM.
1028 static void terminate_read(int cmd)
1030 if (doing_read == YES) {
1031 doing_read = NO;
1032 if (cmd != QCMD_RD_FM) {
1033 /* if the command is a RFM, there is no need to do this
1034 * because a RFM will legally terminate the read-cycle.
1036 tpqputs(TPQD_ALWAYS, "terminating pending read-cycle");
1038 /* I'm not too sure about this part -- hhb */
1039 if (QIC02_TAPE_IFC == MOUNTAIN) {
1040 /* Mountain reference says can terminate by de-asserting online */
1041 ctlbits &= ~MTN_QIC02_CTL_ONLINE;
1044 if (tp_sense(TP_FIL|TP_EOM|TP_WRP) != TE_OK) {
1045 tpqputs(TPQD_ALWAYS, "finish_rw[read1]: ignore the 2 lines above");
1046 if (is_exception()) {
1047 if (tp_sense(TP_ILL|TP_FIL|TP_EOM|TP_WRP) != TE_OK)
1048 tpqputs(TPQD_ALWAYS, "finish_rw[read2]: read cycle error");
1053 } /* terminate_read */
1056 static void terminate_write(int cmd)
1058 int stat;
1060 if (doing_write == YES) {
1061 doing_write = NO;
1062 /* Finish writing by appending a FileMark at the end. */
1063 if (cmd != QCMD_WRT_FM) {
1064 /* finish off write cycle */
1065 stat = ll_do_qic_cmd(QCMD_WRT_FM, TIM_M);
1066 if (stat != TE_OK)
1067 tpqputs(TPQD_ALWAYS, "Couldn't finish write cycle properly");
1068 (void) tp_sense(0);
1070 /* If there is an EOF token waiting to be returned to
1071 * the (writing) application, discard it now.
1072 * We could be at EOT, so don't reset return_write_eof.
1074 reported_write_eof=YES;
1076 } /* terminate_write */
1079 /* terminate read or write cycle because of command `cmd' */
1080 static void finish_rw(int cmd)
1082 if (wait_for_ready(TIM_S) != TE_OK) {
1083 tpqputs(TPQD_ALWAYS, "error: drive not ready in finish_rw() !");
1084 return;
1086 terminate_read(cmd);
1087 terminate_write(cmd);
1088 } /* finish_rw */
1091 /* Perform a QIC command through ll_do_qic_cmd().
1092 * If necessary, rewind the tape first.
1093 * Return an OS error code if something goes wrong, 0 if all is well.
1095 static int do_qic_cmd(int cmd, time_t timeout)
1097 int stat;
1100 finish_rw(cmd);
1102 if (need_rewind) {
1103 tpqputs(TPQD_REWIND, "Rewinding tape...");
1104 stat = ll_do_qic_cmd(QCMD_REWIND, TIM_R);
1105 if (stat != 0) {
1106 printk(TPQIC02_NAME ": rewind failed in do_qic_cmd(). stat=0x%2x", stat);
1107 return stat;
1109 need_rewind = NO;
1110 if (cmd==QCMD_REWIND) /* don't wind beyond BOT ;-) */
1111 return 0;
1114 return ll_do_qic_cmd(cmd, timeout);
1115 } /* do_qic_cmd */
1118 /* Not all ioctls are supported for all drives. Some rely on
1119 * optional QIC-02 commands. Check tpqic02.h for configuration.
1120 * Some of these commands may require ONLINE to be active.
1122 static int do_ioctl_cmd(int cmd)
1124 int stat;
1126 /* It is not permitted to read or wind the tape after bytes have
1127 * been written. It is not permitted to write the tape while in
1128 * read mode.
1129 * We try to be kind and allow reading again after writing a FM...
1132 switch (cmd) {
1133 case MTRESET:
1134 /* reset verbose */
1135 return (tape_reset(1)==TE_OK)? 0 : -EIO;
1137 case MTFSF:
1138 tpqputs(TPQD_IOCTLS, "MTFSF forward searching filemark");
1139 if ((mode_access==WRITE) && status_bytes_wr)
1140 return -EACCES;
1141 return do_qic_cmd(QCMD_RD_FM, TIM_F);
1143 case MTBSF:
1144 if (TP_HAVE_BSF) {
1145 tpqputs(TPQD_IOCTLS, "MTBSF backward searching filemark -- optional command");
1146 if ((mode_access==WRITE) && status_bytes_wr)
1147 return -EACCES;
1148 stat = do_qic_cmd(QCMD_RD_FM_BCK, TIM_F);
1149 } else {
1150 stat = -ENXIO;
1152 status_eom_detected = status_eof_detected = NO;
1153 return stat;
1155 case MTFSR:
1156 if (TP_HAVE_FSR) { /* This is an optional QIC-02 command */
1157 tpqputs(TPQD_IOCTLS, "MTFSR forward space record");
1158 if ((mode_access==WRITE) && status_bytes_wr)
1159 return -EACCES;
1160 stat = do_qic_cmd(QCMD_SPACE_FWD, TIM_F);
1161 } else {
1162 /**** fake it by doing a read data block command? ******/
1163 tpqputs(TPQD_IOCTLS, "MTFSR not supported");
1164 stat = -ENXIO;
1166 return stat;
1168 case MTBSR:
1169 if (TP_HAVE_BSR) { /* This is an optional QIC-02 command */
1170 /* we need this for appending files with GNU tar!! */
1171 tpqputs(TPQD_IOCTLS, "MTFSR backward space record");
1172 if ((mode_access==WRITE) && status_bytes_wr)
1173 return -EACCES;
1174 stat = do_qic_cmd(QCMD_SPACE_BCK, TIM_F);
1175 } else {
1176 tpqputs(TPQD_IOCTLS, "MTBSR not supported");
1177 stat = -ENXIO;
1179 status_eom_detected = status_eof_detected = NO;
1180 return stat;
1182 case MTWEOF:
1183 tpqputs(TPQD_IOCTLS, "MTWEOF write eof mark");
1184 /* Plain GNU mt(1) 2.2 uses read-only mode for writing FM. :-( */
1185 if (mode_access==READ)
1186 return -EACCES;
1188 /* allow tape movement after writing FM */
1189 status_bytes_rd = status_bytes_wr; /* Kludge-O-Matic */
1190 status_bytes_wr = NO;
1191 return do_qic_cmd(QCMD_WRT_FM, TIM_M);
1192 /* not sure what to do with status_bytes when WFM should fail */
1194 case MTREW:
1195 tpqputs(TPQD_IOCTLS, "MTREW rewinding tape");
1196 if ((mode_access==WRITE) && status_bytes_wr)
1197 return -EACCES;
1198 status_eom_detected = status_eof_detected = NO;
1199 return do_qic_cmd(QCMD_REWIND, TIM_R);
1201 case MTOFFL:
1202 tpqputs(TPQD_IOCTLS, "MTOFFL rewinding & going offline");
1203 /* Doing a drive select will clear (unlock) the current drive.
1204 * But that requires support for multiple drives and locking.
1206 if ((mode_access==WRITE) && status_bytes_wr)
1207 return -EACCES;
1208 status_eom_detected = status_eof_detected = NO;
1209 /**** do rewind depending on minor bits??? ***/
1210 stat = do_qic_cmd(QCMD_REWIND, TIM_R);
1211 return stat;
1213 case MTNOP:
1214 tpqputs(TPQD_IOCTLS, "MTNOP setting status only");
1215 /********** should do `read position' for drives that support it **********/
1216 return (tp_sense(-1)==TE_OK)? 0 : -EIO; /**** check return codes ****/
1218 case MTRETEN:
1219 tpqputs(TPQD_IOCTLS, "MTRETEN retension tape");
1220 if ((mode_access==WRITE) && status_bytes_wr)
1221 return -EACCES;
1222 status_eom_detected = status_eof_detected = NO;
1223 return do_qic_cmd(QCMD_RETEN, TIM_R);
1225 case MTBSFM:
1226 /* Think think is like MTBSF, except that
1227 * we shouldn't skip the FM. Tricky.
1228 * Maybe use RD_FM_BCK, then do a SPACE_FWD?
1230 tpqputs(TPQD_IOCTLS, "MTBSFM not supported");
1231 if ((mode_access==WRITE) && status_bytes_wr)
1232 return -EACCES;
1233 return -ENXIO;
1235 case MTFSFM:
1236 /* I think this is like MTFSF, except that
1237 * we shouldn't skip the FM. Tricky.
1238 * Maybe use QCMD_RD_DATA until we get a TP_FIL exception?
1239 * But then the FM will have been skipped...
1240 * Maybe use RD_FM, then RD_FM_BCK, but not all
1241 * drives will support that!
1243 tpqputs(TPQD_IOCTLS, "MTFSFM not supported");
1244 if ((mode_access==WRITE) && status_bytes_wr)
1245 return -EACCES;
1246 return -ENXIO;
1248 case MTEOM:
1249 /* This should leave the tape ready for appending
1250 * another file to the end, such that it would append
1251 * after the last FM on tape.
1253 tpqputs(TPQD_IOCTLS, "MTEOM search for End Of recorded Media");
1254 if ((mode_access==WRITE) && status_bytes_wr)
1255 return -EACCES;
1256 if (TP_HAVE_EOD) {
1257 /* Use faster seeking when possible.
1258 * This requires the absence of data beyond the EOM.
1259 * It seems that my drive does not always perform the
1260 * SEEK_EOD correctly, unless it is preceded by a
1261 * rewind command.
1263 # if 0
1264 status_eom_detected = status_eof_detected = NO;
1265 # endif
1266 stat = do_qic_cmd(QCMD_REWIND, TIM_R);
1267 if (stat)
1268 return stat;
1269 stat = do_qic_cmd(QCMD_SEEK_EOD, TIM_F);
1270 /* After a successful seek, TP_EOR should be returned */
1271 } else {
1272 /* else just seek until the drive returns exception "No Data" */
1273 stat = 0;
1274 while ((stat==0) && (!status_eom_detected)) {
1275 stat = do_qic_cmd(QCMD_RD_FM, TIM_F); /***** should use MTFSFM here???? ******/
1277 if (tperror.exs & TP_NDT)
1278 return 0;
1280 return stat;
1282 case MTERASE:
1283 tpqputs(TPQD_IOCTLS, "MTERASE -- ERASE TAPE !");
1284 if ((tperror.exs & TP_ST0) && (tperror.exs & TP_WRP)) {
1285 tpqputs(TPQD_ALWAYS, "Cartridge is write-protected.");
1286 return -EACCES;
1287 } else {
1288 time_t t = jiffies;
1290 /* Plain GNU mt(1) 2.2 erases a tape in O_RDONLY. :-( */
1291 if (mode_access==READ)
1292 return -EACCES;
1294 /* give user a few seconds to pull out tape */
1295 while (jiffies - t < 4*HZ)
1296 schedule();
1299 /* don't bother writing filemark first */
1300 status_eom_detected = status_eof_detected = NO;
1301 return do_qic_cmd(QCMD_ERASE, TIM_R);
1303 case MTRAS1:
1304 if (TP_HAVE_RAS1) {
1305 tpqputs(TPQD_IOCTLS, "MTRAS1: non-destructive self test");
1306 stat = do_qic_cmd(QCMD_SELF_TST1, TIM_R);
1307 if (stat != 0) {
1308 tpqputs(TPQD_ALWAYS, "RAS1 failed");
1309 return stat;
1311 return (tp_sense(0)==TE_OK)? 0 : -EIO; /* get_ext_status3(); */
1313 tpqputs(TPQD_IOCTLS, "RAS1 not supported");
1314 return -ENXIO;
1316 case MTRAS2:
1317 if (TP_HAVE_RAS2) {
1318 tpqputs(TPQD_IOCTLS, "MTRAS2: destructive self test");
1319 stat = do_qic_cmd(QCMD_SELF_TST2, TIM_R);
1320 if (stat != 0) {
1321 tpqputs(TPQD_ALWAYS, "RAS2 failed");
1322 return stat;
1324 return (tp_sense(0)==TE_OK)? 0 : -EIO; /* get_ext_status3(); */
1326 tpqputs(TPQD_IOCTLS, "RAS2 not supported");
1327 return -ENXIO;
1329 case MTSEEK:
1330 if (TP_HAVE_SEEK && (QIC02_TAPE_IFC==ARCHIVE)) {
1331 tpqputs(TPQD_IOCTLS, "MTSEEK seeking block");
1332 if ((mode_access==WRITE) && status_bytes_wr)
1333 return -EACCES;
1334 /* NOTE: address (24 bits) is in seek_addr_buf[] */
1335 return do_qic_cmd(AR_QCMDV_SEEK_BLK, TIM_F);
1337 else
1338 return -ENOTTY;
1340 default:
1341 return -ENOTTY;
1343 } /* do_ioctl_cmd */
1346 /* dma_transfer(): This routine is called for every 512 bytes to be read
1347 * from/written to the tape controller. Speed is important here!
1348 * (There must be enough time left for the hd controller!)
1349 * When other devices use DMA they must ensure they use un-interruptible
1350 * double byte accesses to the DMA controller. Floppy.c is ok.
1351 * Must have interrupts disabled when this function is invoked,
1352 * otherwise, the double-byte transfers to the DMA controller will not
1353 * be atomic. That could lead to nasty problems when they are interrupted
1354 * by other DMA interrupt-routines.
1356 * This routine merely does the least possible to keep
1357 * the transfers going:
1358 * - set the DMA count register for the next 512 bytes
1359 * - adjust the DMA address and page registers
1360 * - adjust the timeout
1361 * - tell the tape controller to start transferring
1362 * We assume the dma address and mode are, and remain, valid.
1364 static inline void dma_transfer(void)
1366 unsigned long flags;
1368 if (QIC02_TAPE_IFC == WANGTEK) /* or EVEREX */
1369 outb_p(WT_CTL_ONLINE, QIC02_CTL_PORT); /* back to normal */
1370 else if (QIC02_TAPE_IFC == ARCHIVE)
1371 outb_p(0, AR_RESET_DMA_PORT);
1372 else /* QIC02_TAPE_IFC == MOUNTAIN */
1373 outb_p(ctlbits, QIC02_CTL_PORT);
1376 flags=claim_dma_lock();
1377 clear_dma_ff(QIC02_TAPE_DMA);
1378 set_dma_mode(QIC02_TAPE_DMA, dma_mode);
1379 set_dma_addr(QIC02_TAPE_DMA, buffaddr+dma_bytes_done); /* full address */
1380 set_dma_count(QIC02_TAPE_DMA, TAPE_BLKSIZE);
1382 /* start tape DMA controller */
1383 if (QIC02_TAPE_IFC == WANGTEK) /* or EVEREX */
1384 outb_p(WT_CTL_DMA | WT_CTL_ONLINE, QIC02_CTL_PORT); /* trigger DMA transfer */
1386 else if (QIC02_TAPE_IFC == ARCHIVE) {
1387 outb_p(AR_CTL_IEN | AR_CTL_DNIEN, QIC02_CTL_PORT); /* enable interrupts again */
1388 outb_p(0, AR_START_DMA_PORT); /* start DMA transfer */
1389 /* In dma_end() AR_RESET_DMA_PORT is written too. */
1391 } else /* QIC02_TAPE_IFC == MOUNTAIN */ {
1392 inb(MTN_R_DESELECT_DMA_PORT);
1393 outb_p(ctlbits | (MTN_CTL_EXC_IEN | MTN_CTL_DNIEN), QIC02_CTL_PORT);
1394 outb_p(0, MTN_W_SELECT_DMA_PORT); /* start DMA transfer */
1395 if (dma_mode == DMA_MODE_WRITE)
1396 outb_p(0, MTN_W_DMA_WRITE_PORT); /* start DMA transfer */
1399 /* start computer DMA controller */
1400 enable_dma(QIC02_TAPE_DMA);
1402 release_dma_lock(flags);
1404 /* block transfer should start now, jumping to the
1405 * interrupt routine when done or an exception was detected.
1407 } /* dma_transfer */
1410 /* start_dma() sets a DMA transfer up between the tape controller and
1411 * the kernel qic02_tape_buf buffer.
1412 * Normally bytes_todo==dma_bytes_done at the end of a DMA transfer. If not,
1413 * a filemark was read, or an attempt to write beyond the End Of Tape
1414 * was made. [Or some other bad thing happened.]
1415 * Must do a sense() before returning error.
1417 static int start_dma(short mode, unsigned long bytes_todo)
1418 /* assume 'bytes_todo'>0 */
1420 int stat;
1421 unsigned long flags;
1423 tpqputs(TPQD_DEBUG, "start_dma() enter");
1424 TPQDEB({printk(TPQIC02_NAME ": doing_read==%d, doing_write==%d\n", doing_read, doing_write);})
1426 dma_bytes_done = 0;
1427 dma_bytes_todo = bytes_todo;
1428 status_error = NO;
1429 /* dma_mode!=0 indicates that the dma controller is in use */
1430 dma_mode = (mode == WRITE)? DMA_MODE_WRITE : DMA_MODE_READ;
1432 /* Only give READ/WRITE DATA command to tape drive if we haven't
1433 * done that already. Otherwise the drive will rewind to the beginning
1434 * of the current file on tape. Any QIC command given other than
1435 * R/W FM will break the read/write transfer cycle.
1436 * do_qic_cmd() will terminate doing_{read,write}
1438 if ((doing_read == NO) && (doing_write == NO)) {
1439 /* First, we have to clear the status -- maybe remove TP_FIL???
1442 #if 0
1443 /* Next dummy get status is to make sure CNI is valid,
1444 since we're only just starting a read/write it doesn't
1445 matter some exceptions are cleared by reading the status;
1446 we're only interested in CNI and WRP. -Eddy */
1447 get_status(&tperror);
1448 #else
1449 /* TP_CNI should now be handled in open(). -Hennus */
1450 #endif
1452 stat = tp_sense(((mode == WRITE)? 0 : TP_WRP) | TP_BOM | TP_FIL);
1453 if (stat != TE_OK)
1454 return stat;
1456 #if OBSOLETE
1457 /************* not needed iff rd_status() would wait for ready!!!!!! **********/
1458 if (wait_for_ready(TIM_S) != TE_OK) { /*** not sure this is needed ***/
1459 tpqputs(TPQD_ALWAYS, "wait_for_ready failed in start_dma");
1460 return -EIO;
1462 #endif
1464 if (QIC02_TAPE_IFC == MOUNTAIN) {
1465 /* Set control bits to select ONLINE during command */
1466 ctlbits |= MTN_QIC02_CTL_ONLINE;
1469 /* Tell the controller the data direction */
1471 /* r/w, timeout medium, check exceptions, sets status_cmd_pending. */
1472 stat = send_qic02_cmd((mode == WRITE)? QCMD_WRT_DATA : QCMD_RD_DATA, TIM_M, 0);
1473 if (stat!=TE_OK) {
1474 printk(TPQIC02_NAME ": start_dma: init %s failed\n",
1475 (mode == WRITE)? "write" : "read");
1476 (void) tp_sense(0);
1477 return stat;
1480 /* Do this last, because sense() will clear the doing_{read,write}
1481 * flags, causing trouble next time around.
1483 if (wait_for_ready(TIM_M) != TE_OK)
1484 return -EIO;
1485 switch (mode) {
1486 case READ:
1487 doing_read = YES;
1488 break;
1489 case WRITE:
1490 doing_write = YES;
1491 break;
1492 default:
1493 printk(TPQIC02_NAME ": requested unknown mode %d\n", mode);
1494 panic(TPQIC02_NAME ": invalid mode in start_dma()");
1497 } else if (is_exception()) {
1498 /* This is for Archive drives, to handle reads with 0 bytes
1499 * left for the last read request.
1501 * ******** this also affects EOF/EOT handling! ************
1503 tpqputs(TPQD_ALWAYS, "detected exception in start_dma() while transfer in progress");
1504 status_error = YES;
1505 return TE_END;
1509 status_expect_int = YES;
1511 /* This assumes tape is already positioned, but these
1512 * semi-'intelligent' drives are unpredictable...
1514 TIMERON(TIM_M*2);
1516 /* initiate first data block read from/write to the tape controller */
1518 save_flags(flags);
1519 cli();
1520 dma_transfer();
1521 restore_flags(flags);
1523 TPQPUTS("start_dma() end");
1524 return TE_OK;
1525 } /* start_dma */
1528 /* This cleans up after the dma transfer has completed
1529 * (or failed). If an exception occurred, a sense()
1530 * must be done. If the exception was caused by a FM,
1531 * sense() will set `status_eof_detected' and
1532 * `status_eom_detected', as required.
1534 static void end_dma(unsigned long * bytes_done)
1536 int stat = TE_OK;
1537 unsigned long flags;
1539 TIMEROFF;
1541 TPQPUTS("end_dma() enter");
1543 flags=claim_dma_lock();
1545 disable_dma(QIC02_TAPE_DMA);
1546 clear_dma_ff(QIC02_TAPE_DMA);
1548 release_dma_lock(flags);
1550 if (QIC02_TAPE_IFC == WANGTEK) /* or EVEREX */
1551 outb_p(WT_CTL_ONLINE, QIC02_CTL_PORT); /* back to normal */
1552 else if (QIC02_TAPE_IFC == ARCHIVE)
1553 outb_p(0, AR_RESET_DMA_PORT);
1554 else /* QIC02_TAPE_IFC == MOUNTAIN */ {
1555 /* Clear control bits, de-select ONLINE during tp_sense */
1556 ctlbits &= ~MTN_QIC02_CTL_ONLINE;
1559 stat = wait_for_ready(TIM_M);
1560 if (status_error || (stat!=TE_OK)) {
1561 tpqputs(TPQD_DMAX, "DMA transfer exception");
1562 stat = tp_sense((dma_mode==READ)? TP_WRP : 0);
1563 /* no return here -- got to clean up first! */
1564 } else /* if (QIC02_TAPE_IFC == MOUNTAIN) */ {
1565 outb_p(ctlbits, QIC02_CTL_PORT);
1568 if (QIC02_TAPE_IFC == MOUNTAIN)
1569 inb(MTN_R_DESELECT_DMA_PORT);
1571 /* take the tape controller offline */
1573 /* finish off DMA stuff */
1576 dma_mode = 0;
1577 /* Note: The drive is left on-line, ready for the next
1578 * data transfer.
1579 * If the next command to the drive does not continue
1580 * the pending cycle, it must do 2 sense()s first.
1583 *bytes_done = dma_bytes_done;
1584 status_expect_int = NO;
1585 ioctl_status.mt_blkno += (dma_bytes_done / TAPE_BLKSIZE);
1587 TPQPUTS("end_dma() exit");
1588 /*** could return stat here ***/
1589 } /* end_dma */
1591 /*********** Below are the (public) OS-interface procedures ***********/
1594 /* qic02_tape_times_out() is called when a DMA transfer doesn't complete
1595 * quickly enough. Usually this means there is something seriously wrong
1596 * with the hardware/software, but it could just be that the controller
1597 * has decided to do a long rewind, just when I didn't expect it.
1598 * Just try again.
1600 static void qic02_tape_times_out(unsigned long dummy)
1602 printk("time-out in %s driver\n", TPQIC02_NAME);
1603 if ((status_cmd_pending>0) || dma_mode) {
1604 /* takes tooo long, shut it down */
1605 status_dead = YES;
1606 status_cmd_pending = 0;
1607 status_timer_on = NO;
1608 status_expect_int = NO;
1609 status_error = YES;
1610 if (dma_mode) {
1611 dma_mode = 0; /* signal end to read/write routine */
1612 wake_up(&qic02_tape_transfer);
1615 } /* qic02_tape_times_out */
1618 * Interrupt handling:
1620 * 1) Interrupt is generated iff at the end of
1621 * a 512-DMA-block transfer.
1622 * 2) EXCEPTION is not raised unless something
1623 * is wrong or EOT/FM is detected.
1624 * 3) FM EXCEPTION is set *after* the last byte has
1625 * been transferred by DMA. By the time the interrupt
1626 * is handled, the EXCEPTION may already be set.
1628 * So,
1629 * 1) On EXCEPTION, assume data has been transferred, so
1630 * continue as usual, but set a flag to indicate the
1631 * exception was detected.
1632 * Do a sense status when the flag is found set.
1633 * 2) Do not attempt to continue a transfer after an exception.
1634 * [??? What about marginal blocks???????]
1638 /* qic02_tape_interrupt() is called when the tape controller completes
1639 * a DMA transfer.
1640 * We are not allowed to sleep here!
1642 * Check if the transfer was successful, check if we need to transfer
1643 * more. If the buffer contains enough data/is empty enough, signal the
1644 * read/write() thread to copy to/from user space.
1645 * When we are finished, set flags to indicate end, disable timer.
1646 * NOTE: This *must* be fast!
1648 static void qic02_tape_interrupt(int irq, void *dev_id, struct pt_regs *regs)
1650 int stat, r, i;
1651 unsigned long flags;
1653 TIMEROFF;
1655 if (status_expect_int) {
1656 #ifdef WANT_EXTRA_FULL_DEBUGGING
1657 if (TP_DIAGS(current_tape_dev))
1658 printk("@");
1659 #endif
1660 stat = inb(QIC02_STAT_PORT); /* Knock, knock */
1661 if (QIC02_TAPE_IFC == ARCHIVE) { /* "Who's there?" */
1662 if (((stat & (AR_STAT_DMADONE)) == 0) &&
1663 ((stat & (QIC02_STAT_EXCEPTION)) != 0)) {
1664 TIMERCONT;
1665 return; /* "Linux with IRQ sharing" */
1669 if ((stat & QIC02_STAT_EXCEPTION) == 0) { /* exception occurred */
1670 /* Possible causes for an exception during a transfer:
1671 * - during a write-cycle: end of tape (EW) hole detected.
1672 * - during a read-cycle: filemark or EOD detected.
1673 * - something went wrong
1674 * So don't continue with the next block.
1676 tpqputs(TPQD_ALWAYS, "isr: exception on tape controller");
1677 printk(" status %02x\n", stat);
1678 status_error = TE_EX;
1680 dma_bytes_done += TAPE_BLKSIZE;
1682 dma_mode = 0; /* wake up rw() */
1683 status_expect_int = NO;
1684 wake_up(&qic02_tape_transfer);
1685 return;
1687 /* return if tape controller not ready, or
1688 * if dma channel hasn't finished last byte yet.
1690 r = 0;
1692 /* Skip next ready check for Archive controller because
1693 * it may be busy reading ahead. Weird. --hhb
1695 if (QIC02_TAPE_IFC == WANGTEK) /* I think this is a drive-dependency, not IFC -- hhb */
1696 if (stat & QIC02_STAT_READY) { /* not ready */
1697 tpqputs(TPQD_ALWAYS, "isr: ? Tape controller not ready");
1698 r = 1;
1701 flags=claim_dma_lock();
1703 if ( (i = get_dma_residue(QIC02_TAPE_DMA)) != 0 ) {
1704 printk(TPQIC02_NAME ": dma_residue == %x !!!\n", i);
1705 r = 1; /* big trouble, but can't do much about it... */
1708 release_dma_lock(flags);
1710 if (r)
1711 return;
1713 /* finish DMA cycle */
1715 /* no errors detected, continue */
1716 dma_bytes_done += TAPE_BLKSIZE;
1717 if (dma_bytes_done >= dma_bytes_todo) {
1718 /* finished! Wakeup rw() */
1719 dma_mode = 0;
1720 status_expect_int = NO;
1721 TPQPUTS("isr: dma_bytes_done");
1722 wake_up(&qic02_tape_transfer);
1723 } else {
1724 /* start next transfer, account for track-switching time */
1725 mod_timer(&tp_timer, jiffies + 6*HZ);
1726 dma_transfer();
1728 } else {
1729 printk(TPQIC02_NAME ": Unexpected interrupt, stat == %x\n",
1730 inb(QIC02_STAT_PORT));
1732 } /* qic02_tape_interrupt */
1735 static long long qic02_tape_lseek(struct file * file, long long offset, int origin)
1737 return -EINVAL; /* not supported */
1738 } /* qic02_tape_lseek */
1741 /* read/write routines:
1742 * This code copies between a kernel buffer and a user buffer. The
1743 * actual data transfer is done using DMA and interrupts. Time-outs
1744 * are also used.
1746 * When a filemark is read, we return '0 bytes read' and continue with the
1747 * next file after that.
1748 * When EOM is read, we return '0 bytes read' twice.
1749 * When the EOT marker is detected on writes, '0 bytes read' should be
1750 * returned twice. If user program does a MTNOP after that, 2 additional
1751 * blocks may be written. ------- FIXME: Implement this correctly *************************************************
1753 * Only read/writes in multiples of 512 bytes are accepted.
1754 * When no bytes are available, we sleep() until they are. The controller will
1755 * generate an interrupt, and we (should) get a wake_up() call.
1757 * Simple buffering is used. User program should ensure that a large enough
1758 * buffer is used. Usually the drive does some buffering as well (something
1759 * like 4k or so).
1761 * Scott S. Bertilson suggested to continue filling the user buffer, rather
1762 * than waste time on a context switch, when the kernel buffer fills up.
1766 * Problem: tar(1) doesn't always read the entire file. Sometimes the entire file
1767 * has been read, but the EOF token is never returned to tar(1), simply because
1768 * tar(1) knows it has already read all of the data it needs. So we must use
1769 * open/release to reset the `reported_read_eof' flag. If we don't, the next read
1770 * request would return the EOF flag for the previous file.
1773 static ssize_t qic02_tape_read(struct file * filp, char * buf, size_t count, loff_t *ppos)
1775 int err;
1776 kdev_t dev = filp->f_dentry->d_inode->i_rdev;
1777 unsigned short flags = filp->f_flags;
1778 unsigned long bytes_todo, bytes_done, total_bytes_done = 0;
1779 int stat;
1781 if (status_zombie==YES)
1783 tpqputs(TPQD_ALWAYS, "configs not set");
1784 return -ENXIO;
1787 if (TP_DIAGS(current_tape_dev))
1788 /* can't print a ``long long'' (for filp->f_pos), so chop it */
1789 printk(TPQIC02_NAME ": request READ, minor=%x, buf=%p, count=%lx"
1790 ", pos=%lx, flags=%x\n",
1791 MINOR(dev), buf, (long) count,
1792 (unsigned long) filp->f_pos, flags);
1794 if (count % TAPE_BLKSIZE) /* Only allow mod 512 bytes at a time. */
1796 tpqputs(TPQD_BLKSZ, "Wrong block size");
1797 return -EINVAL;
1800 /* Just assume everything is ok. Controller will scream if not. */
1802 if (status_bytes_wr) /* Once written, no more reads, 'till after WFM. */
1804 return -EACCES;
1807 /* This is rather ugly because it has to implement a finite state
1808 * machine in order to handle the EOF situations properly.
1810 while ((signed)count>=0)
1812 bytes_done = 0;
1813 /* see how much fits in the kernel buffer */
1814 bytes_todo = TPQBUF_SIZE;
1815 if (bytes_todo>count)
1817 bytes_todo = count;
1820 /* Must ensure that user program sees exactly one EOF token (==0) */
1821 if (return_read_eof==YES)
1823 if (TPQDBG(DEBUG))
1825 printk("read: return_read_eof==%d, reported_read_eof==%d, total_bytes_done==%lu\n", return_read_eof, reported_read_eof, total_bytes_done);
1828 if (reported_read_eof==NO)
1830 /* have not yet returned EOF to user program */
1831 if (total_bytes_done>0)
1833 return total_bytes_done; /* next time return EOF */
1835 else
1837 reported_read_eof = YES; /* move on next time */
1838 return 0; /* return EOF */
1841 else
1843 /* Application program has already received EOF
1844 * (above), now continue with next file on tape,
1845 * if possible.
1846 * When the FM is reached, EXCEPTION is set,
1847 * causing a sense(). Subsequent read/writes will
1848 * continue after the FM.
1850 /*********** ?????????? this should check for (EOD|NDT), not EOM, 'cause we can read past EW: ************/
1851 if (status_eom_detected)
1853 /* If EOM, nothing left to read, so keep returning EOFs.
1854 *** should probably set some flag to avoid clearing
1855 *** status_eom_detected through ioctls or something
1857 return 0;
1859 else
1861 /* just eof, there may be more files ahead... */
1862 return_read_eof = NO;
1863 reported_read_eof = NO;
1864 status_eof_detected = NO; /* reset this too */
1865 /*fall through*/
1870 /*****************************/
1871 if (bytes_todo==0)
1873 return total_bytes_done;
1876 if (bytes_todo>0)
1878 /* start reading data */
1879 if (is_exception()) /****************************************/
1881 tpqputs(TPQD_DMAX, "is_exception() before start_dma()!");
1884 /******************************************************************
1885 ***** if start_dma() fails because the head is positioned 0 bytes
1886 ***** before the FM, (causing EXCEPTION to be set) return_read_eof should
1887 ***** be set to YES, and we should return total_bytes_done, rather than -ENXIO.
1888 ***** The app should recognize this as an EOF condition.
1889 ***************************************************************************/
1890 stat = start_dma(READ, bytes_todo);
1891 if (stat == TE_OK)
1893 /* Wait for transfer to complete, interrupt should wake us */
1894 while (dma_mode != 0)
1896 sleep_on(&qic02_tape_transfer);
1898 if (status_error)
1900 return_read_eof = YES;
1904 else if (stat != TE_END)
1906 /* should do sense() on error here */
1907 #if 0
1908 return -ENXIO;
1909 #else
1910 printk("Trouble: stat==%02x\n", stat);
1911 return_read_eof = YES;
1912 /*************** check EOF/EOT handling!!!!!! **/
1913 #endif
1915 end_dma(&bytes_done);
1916 if (bytes_done>bytes_todo)
1918 tpqputs(TPQD_ALWAYS, "read: Oops, read more bytes than requested");
1919 return -EIO;
1921 /* copy buffer to user-space in one go */
1922 if (bytes_done>0)
1924 err = copy_to_user( (void *) buf, (void *) bus_to_virt(buffaddr), bytes_done);
1925 if (err)
1927 return -EFAULT;
1930 #if 1
1931 /* Checks Ton's patch below */
1932 if ((return_read_eof == NO) && (status_eof_detected == YES))
1934 printk(TPQIC02_NAME ": read(): return_read_eof=%d, status_eof_detected=YES. return_read_eof:=YES\n", return_read_eof);
1936 #endif
1937 if ((bytes_todo != bytes_done) || (status_eof_detected == YES))
1939 /* EOF or EOM detected. return EOF next time. */
1940 return_read_eof = YES;
1943 } /* else: ignore read request for 0 bytes */
1945 if (bytes_done>0)
1947 status_bytes_rd = YES;
1948 buf += bytes_done;
1949 *ppos += bytes_done;
1950 total_bytes_done += bytes_done;
1951 count -= bytes_done;
1954 tpqputs(TPQD_ALWAYS, "read request for <0 bytes");
1955 return -EINVAL;
1956 } /* qic02_tape_read */
1960 /* The drive detects near-EOT by means of the holes in the tape.
1961 * When the holes are detected, there is some space left. The drive
1962 * reports this as a TP_EOM exception. After clearing the exception,
1963 * the drive should accept two extra blocks.
1965 * It seems there are some archiver programs that would like to use the
1966 * extra space for writing a continuation marker. The driver should return
1967 * end-of-file to the user program on writes, when the holes are detected.
1968 * If the user-program wants to use the extra space, it should use the
1969 * MTNOP ioctl() to get the generic status register and may then continue
1970 * writing (max 1kB). ----------- doesn't work yet...............
1972 * EOF behaviour on writes:
1973 * If there is enough room, write all of the data.
1974 * If there is insufficient room, write as much as will fit and
1975 * return the amount written. If the requested amount differs from the
1976 * written amount, the application program should recognize that as the
1977 * end of file. Subsequent writes will return -ENOSPC.
1978 * Unless the minor bits specify a rewind-on-close, the tape will not
1979 * be rewound when it is full. The user-program should do that, if desired.
1980 * If the driver were to do that automatically, a user-program could be
1981 * confused about the EOT/BOT condition after re-opening the tape device.
1983 * Multiple volume support: Tar closes the tape device before prompting for
1984 * the next tape. The user may then insert a new tape and tar will open the
1985 * tape device again. The driver will detect an exception status in (No Cartridge)
1986 * and force a rewind. After that tar may continue writing.
1988 static ssize_t qic02_tape_write( struct file * filp, const char * buf,
1989 size_t count, loff_t *ppos)
1991 int err;
1992 kdev_t dev = filp->f_dentry->d_inode->i_rdev;
1993 unsigned short flags = filp->f_flags;
1994 unsigned long bytes_todo, bytes_done, total_bytes_done = 0;
1996 if (status_zombie==YES)
1998 tpqputs(TPQD_ALWAYS, "configs not set");
1999 return -ENXIO;
2002 if (TP_DIAGS(current_tape_dev))
2004 /* can't print a ``long long'' (for filp->f_pos), so chop it */
2005 printk(TPQIC02_NAME ": request WRITE, minor=%x, buf=%p"
2006 ", count=%lx, pos=%lx, flags=%x\n",
2007 MINOR(dev), buf,
2008 (long) count, (unsigned long) filp->f_pos, flags);
2011 if (count % TAPE_BLKSIZE) /* only allow mod 512 bytes at a time */
2013 tpqputs(TPQD_BLKSZ, "Wrong block size");
2014 return -EINVAL;
2017 if (mode_access==READ)
2019 tpqputs(TPQD_ALWAYS, "Not in write mode");
2020 return -EACCES;
2023 /* open() does a sense() and we can assume the tape isn't changed
2024 * between open() and release(), so the tperror.exs bits will still
2025 * be valid.
2027 if ((tperror.exs & TP_ST0) && (tperror.exs & TP_WRP))
2029 tpqputs(TPQD_ALWAYS, "Cartridge is write-protected.");
2030 return -EACCES; /* don't even try when write protected */
2033 if (doing_read == YES)
2035 terminate_read(0);
2038 while ((signed)count>=0)
2040 /* see how much fits in the kernel buffer */
2041 bytes_done = 0;
2042 bytes_todo = TPQBUF_SIZE;
2043 if (bytes_todo>count)
2045 bytes_todo = count;
2048 if (return_write_eof == YES)
2050 /* return_write_eof should be reset on reverse tape movements. */
2052 if (reported_write_eof==NO)
2054 if (bytes_todo>0)
2056 tpqputs(TPQD_ALWAYS, "partial write");
2057 /* partial write signals EOF to user program */
2059 reported_write_eof = YES;
2060 return total_bytes_done;
2062 else
2064 return -ENOSPC; /* return error */
2068 /* Quit when done. */
2069 if (bytes_todo==0)
2071 return total_bytes_done;
2074 /* copy from user to DMA buffer and initiate transfer. */
2075 if (bytes_todo>0)
2077 err = copy_from_user( (void *) bus_to_virt(buffaddr), (const void *) buf, bytes_todo);
2078 if (err)
2080 return -EFAULT;
2083 /****************** similar problem with read() at FM could happen here at EOT.
2084 ******************/
2086 /***** if at EOT, 0 bytes can be written. start_dma() will
2087 ***** fail and write() will return ENXIO error
2088 *****/
2089 if (start_dma(WRITE, bytes_todo) != TE_OK)
2091 tpqputs(TPQD_ALWAYS, "write: start_dma() failed");
2092 /* should do sense() on error here */
2093 return -ENXIO; /*********** FIXTHIS **************/
2096 /* Wait for write to complete, interrupt should wake us. */
2097 while ((status_error == 0) && (dma_mode != 0))
2099 sleep_on(&qic02_tape_transfer);
2102 end_dma(&bytes_done);
2103 if (bytes_done>bytes_todo)
2105 tpqputs(TPQD_ALWAYS, "write: Oops, wrote more bytes than requested");
2106 return -EIO;
2108 /* If the dma-transfer was aborted because of an exception,
2109 * status_error will have been set in the interrupt handler.
2110 * Then end_dma() will do a sense().
2111 * If the exception was EXC_EOM, the EW-hole was encountered
2112 * and two more blocks could be written. For the time being we'll
2113 * just consider this to be the EOT.
2114 * Otherwise, something Bad happened, such as the maximum number
2115 * of block-rewrites was exceeded. [e.g. A very bad spot on tape was
2116 * encountered. Normally short dropouts are compensated for by
2117 * rewriting the block in error, up to 16 times. I'm not sure
2118 * QIC-24 drives can do this.]
2120 if (status_error)
2122 if (status_eom_detected == YES)
2124 tpqputs(TPQD_ALWAYS, "write: EW detected");
2125 return_write_eof = YES;
2127 else
2129 /* probably EXC_RWA */
2130 tpqputs(TPQD_ALWAYS, "write: dma: error in writing");
2131 return -EIO;
2134 if (bytes_todo != bytes_done)
2136 /* EOF or EOM detected. return EOT next time. */
2137 return_write_eof = YES;
2140 /* else: ignore write request for 0 bytes. */
2142 if (bytes_done>0)
2144 status_bytes_wr = YES;
2145 buf += bytes_done;
2146 *ppos += bytes_done;
2147 total_bytes_done += bytes_done;
2148 count -= bytes_done;
2152 tpqputs(TPQD_ALWAYS, "write request for <0 bytes");
2153 if (TPQDBG(DEBUG))
2155 printk(TPQIC02_NAME ": status_bytes_wr %x, buf %p"
2156 ", total_bytes_done %lx, count %lx\n",
2157 status_bytes_wr, buf, total_bytes_done, (long) count);
2159 return -EINVAL;
2160 } /* qic02_tape_write */
2164 /* qic02_tape_open()
2165 * We allow the device to be opened, even if it is marked 'dead' because
2166 * we want to be able to reset the tape device without rebooting.
2167 * Only one open tape file at a time, except when minor=255.
2168 * Minor 255 is only allowed for resetting and always returns <0.
2170 * The density command is only allowed when TP_BOM is set. Thus, remember
2171 * the most recently used minor bits. When they are different from the
2172 * remembered values, rewind the tape and set the required density.
2173 * Don't rewind if the minor bits specify density 0.
2176 static int qic02_tape_open(struct inode * inode, struct file * filp)
2178 static int qic02_tape_open_no_use_count(struct inode *, struct file *);
2179 int open_error;
2181 open_error = qic02_tape_open_no_use_count(inode, filp);
2182 return open_error;
2185 static int qic02_tape_open_no_use_count(struct inode * inode, struct file * filp)
2187 kdev_t dev = inode->i_rdev;
2188 unsigned short flags = filp->f_flags;
2189 unsigned short dens = 0;
2190 int s;
2193 if (TP_DIAGS(dev))
2195 printk("qic02_tape_open: dev=%s, flags=%x ",
2196 kdevname(dev), flags);
2199 if (MINOR(dev)==255) /* special case for resetting */
2201 if (suser())
2203 return (tape_reset(1)==TE_OK) ? -EAGAIN : -ENXIO;
2205 else
2207 return -EPERM;
2211 if (status_dead==YES)
2213 /* Allow `mt reset' ioctl() even when already open()ed. */
2214 return 0;
2217 /* Only one at a time from here on... */
2218 if (file_count(filp)>1) /* filp->f_count==1 for the first open() */
2220 return -EBUSY;
2223 if (status_zombie==YES)
2225 /* no irq/dma/port stuff allocated yet, no reset done
2226 * yet, so return until MTSETCONFIG has been done.
2228 return 0;
2231 status_bytes_rd = NO;
2232 status_bytes_wr = NO;
2234 return_read_eof = NO; /********????????????????*****/
2235 return_write_eof = (status_eot_detected)? YES : NO;
2237 /* Clear this in case user app close()d before reading EOF token */
2238 status_eof_detected = NO;
2240 reported_read_eof = NO;
2241 reported_write_eof = NO;
2244 switch (flags & O_ACCMODE)
2246 case O_RDONLY:
2247 mode_access = READ;
2248 break;
2249 case O_WRONLY: /* Fallthru... Strictly speaking this is not correct... */
2250 case O_RDWR: /* Reads are allowed as long as nothing is written */
2251 mode_access = WRITE;
2252 break;
2255 /* This is to avoid tape-changed problems (TP_CNI exception).
2257 * Since removing the cartridge will not raise an exception,
2258 * we always do a tp_sense() to make sure we have the proper
2259 * CNI status, the 2150L may need an additional sense.... - Eddy
2261 s = tp_sense(TP_WRP|TP_EOM|TP_BOM|TP_CNI|TP_EOR);
2263 if (s == TE_OK)
2265 /* Try to clear cartridge-changed status for Archive-2150L */
2266 if ((tperror.exs & TP_ST0) && (tperror.exs & TP_CNI))
2268 s = tp_sense(TP_WRP|TP_EOM|TP_BOM|TP_CNI|TP_EOR);
2272 if (s != TE_OK)
2274 tpqputs(TPQD_ALWAYS, "open: sense() failed");
2275 return -EIO;
2278 /* exception bits should be up-to-date now, so check for
2279 * tape presence and exit if absent.
2280 * Even `mt stat' will fail without a tape.
2282 if ((tperror.exs & TP_ST0) && (tperror.exs & TP_CNI))
2284 tpqputs(TPQD_ALWAYS, "No tape present.");
2285 return -EIO;
2288 /* At this point we can assume that a tape is present and
2289 * that it will remain present until release() is called.
2292 /* not allowed to do QCMD_DENS_* unless tape is rewound */
2293 if ((TP_DENS(dev)!=0) && (TP_DENS(current_tape_dev) != TP_DENS(dev)))
2295 /* force rewind if minor bits have changed,
2296 * i.e. user wants to use tape in different format.
2297 * [assuming single drive operation]
2299 if (TP_HAVE_DENS)
2301 tpqputs(TPQD_REWIND, "Density minor bits have changed. Forcing rewind.");
2302 need_rewind = YES;
2305 else
2307 /* density bits still the same, but TP_DIAGS bit
2308 * may have changed.
2310 current_tape_dev = dev;
2313 if (need_rewind == YES) /***************** CHECK THIS!!!!!!!! **********/
2315 s = do_qic_cmd(QCMD_REWIND, TIM_R);
2316 if (s != 0)
2318 tpqputs(TPQD_ALWAYS, "open: rewind failed");
2319 return -EIO;
2324 /* Note: After a reset command, the controller will rewind the tape
2325 * just before performing any tape movement operation! ************ SO SET need_rewind flag!!!!!
2327 if (status_dead==YES)
2329 tpqputs(TPQD_ALWAYS, "open: tape dead, attempting reset");
2330 if (tape_reset(1)!=TE_OK)
2332 return -ENXIO;
2334 else
2336 status_dead = NO;
2337 if (tp_sense(~(TP_ST1|TP_ILL)) != TE_OK)
2339 tpqputs(TPQD_ALWAYS, "open: tp_sense() failed\n");
2340 status_dead = YES; /* try reset next time */
2341 return -EIO;
2346 /* things should be ok, once we get here */
2349 /* set density: only allowed when TP_BOM status bit is set,
2350 * so we must have done a rewind by now. If not, just skip over.
2351 * Only give set density command when minor bits have changed.
2353 if (TP_DENS(current_tape_dev) == TP_DENS(dev) )
2355 return 0;
2358 current_tape_dev = dev;
2359 need_rewind = NO;
2360 if (TP_HAVE_DENS)
2362 dens = TP_DENS(dev);
2365 if (dens < sizeof(format_names)/sizeof(char *))
2367 printk(TPQIC02_NAME ": format: %s%s\n", (dens!=0)? "QIC-" : "", format_names[dens]);
2369 else
2371 tpqputs(TPQD_REWIND, "Wait for retensioning...");
2374 switch (TP_DENS(dev))
2376 case 0: /* Minor 0 is for drives without set-density support */
2377 s = 0;
2378 break;
2379 case 1:
2380 s = do_qic_cmd(QCMD_DENS_11, TIM_S);
2381 break;
2382 case 2:
2383 s = do_qic_cmd(QCMD_DENS_24, TIM_S);
2384 break;
2385 case 3:
2386 s = do_qic_cmd(QCMD_DENS_120, TIM_S);
2387 break;
2388 case 4:
2389 s = do_qic_cmd(QCMD_DENS_150, TIM_S);
2390 break;
2391 case 5:
2392 s = do_qic_cmd(QCMD_DENS_300, TIM_S);
2393 break;
2394 case 6:
2395 s = do_qic_cmd(QCMD_DENS_600, TIM_S);
2396 break;
2397 default: /* otherwise do a retension before anything else */
2398 s = do_qic_cmd(QCMD_RETEN, TIM_R);
2400 if (s != 0)
2402 status_dead = YES; /* force reset */
2403 current_tape_dev = 0; /* earlier 0xff80 */
2404 return -EIO;
2407 return 0;
2408 } /* qic02_tape_open */
2411 static int qic02_tape_release(struct inode * inode, struct file * filp)
2413 kdev_t dev = inode->i_rdev;
2415 lock_kernel();
2416 if (TP_DIAGS(dev))
2418 printk("qic02_tape_release: dev=%s\n", kdevname(dev));
2421 if (status_zombie==NO) /* don't rewind in zombie mode */
2423 /* Terminate any pending write cycle. Terminating the read-cycle
2424 * is delayed until it is required to do so for a new command.
2426 terminate_write(-1);
2428 if (status_dead==YES)
2430 tpqputs(TPQD_ALWAYS, "release: device dead!?");
2433 /* Rewind only if minor number requires it AND
2434 * read/writes have been done. ************* IS THIS CORRECT??????????
2436 if ((TP_REWCLOSE(dev)) && (status_bytes_rd | status_bytes_wr))
2438 tpqputs(TPQD_REWIND, "release: Doing rewind...");
2439 (void) do_qic_cmd(QCMD_REWIND, TIM_R);
2442 unlock_kernel();
2443 return 0;
2444 } /* qic02_tape_release */
2447 #ifdef CONFIG_QIC02_DYNCONF
2448 /* Set masks etc. based on the interface card type. */
2449 static int update_ifc_masks(int ifc)
2451 QIC02_TAPE_IFC = ifc;
2453 if ((QIC02_TAPE_IFC == WANGTEK) || (QIC02_TAPE_IFC == EVEREX))
2455 QIC02_STAT_PORT = QIC02_TAPE_PORT;
2456 QIC02_CTL_PORT = QIC02_TAPE_PORT;
2457 QIC02_CMD_PORT = QIC02_TAPE_PORT+1;
2458 QIC02_DATA_PORT = QIC02_TAPE_PORT+1;
2459 QIC02_STAT_READY = WT_QIC02_STAT_READY;
2460 QIC02_STAT_EXCEPTION = WT_QIC02_STAT_EXCEPTION;
2461 QIC02_STAT_MASK = WT_QIC02_STAT_MASK;
2463 QIC02_STAT_RESETMASK = WT_QIC02_STAT_RESETMASK;
2464 QIC02_STAT_RESETVAL = WT_QIC02_STAT_RESETVAL;
2466 QIC02_CTL_RESET = WT_QIC02_CTL_RESET;
2467 QIC02_CTL_REQUEST = WT_QIC02_CTL_REQUEST;
2469 if (QIC02_TAPE_DMA == 3)
2471 WT_CTL_DMA = WT_CTL_DMA3;
2473 else if (QIC02_TAPE_DMA == 1)
2475 WT_CTL_DMA = WT_CTL_DMA1;
2477 else
2479 tpqputs(TPQD_ALWAYS, "Unsupported or incorrect DMA channel");
2480 return -EIO;
2483 if (QIC02_TAPE_IFC == EVEREX)
2485 /* Everex is a special case for Wangtek (actually
2486 * it's the other way 'round, but I saw Wangtek first)
2488 if (QIC02_TAPE_DMA==3)
2490 WT_CTL_DMA = WT_CTL_DMA1;
2493 /* Fixup the kernel copy of the IFC type to that
2494 * we don't have to distinguish between Wangtek and
2495 * and Everex at runtime.
2497 QIC02_TAPE_IFC = WANGTEK;
2500 else if (QIC02_TAPE_IFC == ARCHIVE)
2502 QIC02_STAT_PORT = QIC02_TAPE_PORT+1;
2503 QIC02_CTL_PORT = QIC02_TAPE_PORT+1;
2504 QIC02_CMD_PORT = QIC02_TAPE_PORT;
2505 QIC02_DATA_PORT = QIC02_TAPE_PORT;
2506 QIC02_STAT_READY = AR_QIC02_STAT_READY;
2507 QIC02_STAT_EXCEPTION = AR_QIC02_STAT_EXCEPTION;
2508 QIC02_STAT_MASK = AR_QIC02_STAT_MASK;
2510 QIC02_STAT_RESETMASK = AR_QIC02_STAT_RESETMASK;
2511 QIC02_STAT_RESETVAL = AR_QIC02_STAT_RESETVAL;
2513 QIC02_CTL_RESET = AR_QIC02_CTL_RESET;
2514 QIC02_CTL_REQUEST = AR_QIC02_CTL_REQUEST;
2516 if (QIC02_TAPE_DMA > 3)
2518 tpqputs(TPQD_ALWAYS, "Unsupported or incorrect DMA channel");
2519 return -EIO;
2522 else if (QIC02_TAPE_IFC == MOUNTAIN)
2524 QIC02_STAT_PORT = QIC02_TAPE_PORT+1;
2525 QIC02_CTL_PORT = QIC02_TAPE_PORT+1;
2526 QIC02_CMD_PORT = QIC02_TAPE_PORT;
2527 QIC02_DATA_PORT = QIC02_TAPE_PORT;
2529 QIC02_STAT_READY = MTN_QIC02_STAT_READY;
2530 QIC02_STAT_EXCEPTION = MTN_QIC02_STAT_EXCEPTION;
2531 QIC02_STAT_MASK = MTN_QIC02_STAT_MASK;
2533 QIC02_STAT_RESETMASK = MTN_QIC02_STAT_RESETMASK;
2534 QIC02_STAT_RESETVAL = MTN_QIC02_STAT_RESETVAL;
2536 QIC02_CTL_RESET = MTN_QIC02_CTL_RESET;
2537 QIC02_CTL_REQUEST = MTN_QIC02_CTL_REQUEST;
2539 if (QIC02_TAPE_DMA > 3)
2541 tpqputs(TPQD_ALWAYS, "Unsupported or incorrect DMA channel");
2542 return -EIO;
2545 else
2547 tpqputs(TPQD_ALWAYS, "Invalid interface type");
2548 return -ENXIO;
2550 return qic02_get_resources();
2551 } /* update_ifc_masks */
2552 #endif
2555 /* ioctl allows user programs to rewind the tape and stuff like that */
2556 static int qic02_tape_ioctl(struct inode * inode, struct file * filp,
2557 unsigned int iocmd, unsigned long ioarg)
2559 int error;
2560 int dev_maj = MAJOR(inode->i_rdev);
2561 int c;
2562 struct mtop operation;
2563 unsigned char blk_addr[6];
2564 struct mtpos ioctl_tell;
2567 if (TP_DIAGS(current_tape_dev))
2569 printk(TPQIC02_NAME ": ioctl(%4x, %4x, %4lx)\n", dev_maj, iocmd, ioarg);
2572 if (!inode || !ioarg)
2574 return -EINVAL;
2577 /* check iocmd first */
2579 if (dev_maj != QIC02_TAPE_MAJOR)
2581 printk(TPQIC02_NAME ": Oops! Wrong device?\n");
2582 /* A panic() would be appropriate here */
2583 return -ENODEV;
2586 c = _IOC_NR(iocmd);
2588 #ifdef CONFIG_QIC02_DYNCONF
2589 if (c == _IOC_NR(MTIOCGETCONFIG))
2591 CHECK_IOC_SIZE(mtconfiginfo);
2593 if (copy_to_user((char *) ioarg, (char *) &qic02_tape_dynconf, sizeof(qic02_tape_dynconf)))
2595 return -EFAULT;
2597 return 0;
2600 else if (c == _IOC_NR(MTIOCSETCONFIG))
2602 /* One should always do a MTIOCGETCONFIG first, then update
2603 * user-settings, then write back with MTIOCSETCONFIG.
2604 * The qic02conf program should re-open() the device before actual
2605 * use, to make sure everything is initialized.
2608 CHECK_IOC_SIZE(mtconfiginfo);
2610 if (!suser())
2612 return -EPERM;
2615 if ((doing_read!=NO) || (doing_write!=NO))
2617 return -EBUSY;
2620 if (status_zombie==NO)
2622 qic02_release_resources(); /* and go zombie */
2625 /* copy struct from user space to kernel space */
2626 if (copy_from_user((char *) &qic02_tape_dynconf, (char *) ioarg, sizeof(qic02_tape_dynconf)))
2628 return -EFAULT;
2630 return update_ifc_masks(qic02_tape_dynconf.ifc_type);
2632 if (status_zombie==YES)
2634 tpqputs(TPQD_ALWAYS, "Configs not set");
2635 return -ENXIO;
2637 #endif
2638 if (c == _IOC_NR(MTIOCTOP))
2640 CHECK_IOC_SIZE(mtop);
2642 /* copy mtop struct from user space to kernel space */
2643 if (copy_from_user((char *) &operation, (char *) ioarg, sizeof(operation)))
2645 return -EFAULT;
2648 /* ---note: mt_count is signed, negative seeks must be
2649 * --- translated to seeks in opposite direction!
2650 * (only needed for Sun-programs, I think.)
2652 /* ---note: MTFSF with count 0 should position the
2653 * --- tape at the beginning of the current file.
2656 if (TP_DIAGS(current_tape_dev))
2658 printk("OP op=%4x, count=%4x\n", operation.mt_op, operation.mt_count);
2661 if (operation.mt_count < 0)
2663 tpqputs(TPQD_ALWAYS, "Warning: negative mt_count ignored");
2666 ioctl_status.mt_resid = operation.mt_count;
2667 if (operation.mt_op == MTSEEK)
2669 if (!TP_HAVE_SEEK)
2671 return -ENOTTY;
2674 seek_addr_buf[0] = (operation.mt_count>>16)&0xff;
2675 seek_addr_buf[1] = (operation.mt_count>>8)&0xff;
2676 seek_addr_buf[2] = (operation.mt_count)&0xff;
2677 if (operation.mt_count>>24)
2679 return -EINVAL;
2681 if ((error = do_ioctl_cmd(operation.mt_op)) != 0)
2683 return error;
2686 ioctl_status.mt_resid = 0;
2688 else
2690 while (operation.mt_count > 0)
2692 operation.mt_count--;
2693 if ((error = do_ioctl_cmd(operation.mt_op)) != 0)
2695 return error;
2698 ioctl_status.mt_resid = operation.mt_count;
2701 return 0;
2704 else if (c == _IOC_NR(MTIOCGET))
2706 if (TP_DIAGS(current_tape_dev))
2708 printk("GET ");
2711 CHECK_IOC_SIZE(mtget);
2713 /* It appears (gmt(1)) that it is normal behaviour to
2714 * first set the status with MTNOP, and then to read
2715 * it out with MTIOCGET
2718 /* copy results to user space */
2719 if (copy_to_user((char *) ioarg, (char *) &ioctl_status, sizeof(ioctl_status)))
2721 return -EFAULT;
2723 return 0;
2725 else if (TP_HAVE_TELL && (c == _IOC_NR(MTIOCPOS)))
2727 if (TP_DIAGS(current_tape_dev))
2729 printk("POS ");
2732 CHECK_IOC_SIZE(mtpos);
2734 tpqputs(TPQD_IOCTLS, "MTTELL reading block address");
2735 if ((doing_read==YES) || (doing_write==YES))
2737 finish_rw(AR_QCMDV_TELL_BLK);
2740 c = rdstatus((char *) blk_addr, sizeof(blk_addr), AR_QCMDV_TELL_BLK);
2741 if (c!=TE_OK)
2743 return -EIO;
2746 ioctl_tell.mt_blkno = (blk_addr[3] << 16) | (blk_addr[4] << 8) | blk_addr[5];
2748 /* copy results to user space */
2749 if (copy_to_user((char *) ioarg, (char *) &ioctl_tell, sizeof(ioctl_tell)))
2751 return -EFAULT;
2753 return 0;
2756 else
2758 return -ENOTTY; /* Other cmds not supported. */
2760 } /* qic02_tape_ioctl */
2764 /* These are (most) of the interface functions: */
2765 static struct file_operations qic02_tape_fops = {
2766 owner: THIS_MODULE,
2767 llseek: qic02_tape_lseek, /* not allowed */
2768 read: qic02_tape_read,
2769 write: qic02_tape_write,
2770 ioctl: qic02_tape_ioctl,
2771 open: qic02_tape_open,
2772 release: qic02_tape_release,
2776 static void qic02_release_resources(void)
2778 free_irq(QIC02_TAPE_IRQ, NULL);
2779 free_dma(QIC02_TAPE_DMA);
2780 release_region(QIC02_TAPE_PORT, QIC02_TAPE_PORT_RANGE);
2781 if (buffaddr)
2783 free_pages(buffaddr, get_order(TPQBUF_SIZE));
2785 buffaddr = 0; /* Better to cause a panic than overwite someone else */
2786 status_zombie = YES;
2787 } /* qic02_release_resources */
2790 static int qic02_get_resources(void)
2792 /* First perform some checks. If one of them fails,
2793 * the tape driver will not be registered to the system.
2795 if (QIC02_TAPE_IRQ>16)
2797 tpqputs(TPQD_ALWAYS, "Bogus interrupt number.");
2798 return -ENXIO;
2801 /* for DYNCONF, allocating IO, DMA and IRQ should not be done until
2802 * the config parameters have been set using MTSETCONFIG.
2805 if (check_region(QIC02_TAPE_PORT, QIC02_TAPE_PORT_RANGE))
2807 printk(TPQIC02_NAME ": IO space at 0x%x [%d ports] already reserved\n",
2808 QIC02_TAPE_PORT, QIC02_TAPE_PORT_RANGE);
2809 return -ENXIO;
2812 /* get IRQ */
2813 if (request_irq(QIC02_TAPE_IRQ, qic02_tape_interrupt, SA_INTERRUPT, "QIC-02", NULL))
2815 printk(TPQIC02_NAME ": can't allocate IRQ%d for QIC-02 tape\n",
2816 QIC02_TAPE_IRQ);
2817 return -EBUSY;
2820 /* After IRQ, allocate DMA channel */
2821 if (request_dma(QIC02_TAPE_DMA,"QIC-02"))
2823 printk(TPQIC02_NAME ": can't allocate DMA%d for QIC-02 tape\n",
2824 QIC02_TAPE_DMA);
2825 free_irq(QIC02_TAPE_IRQ, NULL);
2826 return -EBUSY;
2829 /* Grab the IO region. We already made sure it's available. */
2830 request_region(QIC02_TAPE_PORT, QIC02_TAPE_PORT_RANGE, TPQIC02_NAME);
2832 /* Setup the page-address for the dma transfer. */
2834 /*** TODO: does _get_dma_pages() really return the physical address?? ****/
2835 buffaddr = __get_dma_pages(GFP_KERNEL,get_order(TPQBUF_SIZE));
2837 if (!buffaddr)
2839 qic02_release_resources();
2840 return -EBUSY; /* Not ideal, EAGAIN perhaps? */
2843 memset( (void*) buffaddr, 0, TPQBUF_SIZE );
2845 printk(TPQIC02_NAME ": Settings: IRQ %d, DMA %d, IO 0x%x, IFC %s\n",
2846 QIC02_TAPE_IRQ, QIC02_TAPE_DMA,
2847 ((QIC02_TAPE_IFC==ARCHIVE) || (QIC02_TAPE_IFC==MOUNTAIN))?
2848 QIC02_CMD_PORT : QIC02_STAT_PORT,
2849 (QIC02_TAPE_IFC==MOUNTAIN)? "Mountain" :
2850 ((QIC02_TAPE_IFC==ARCHIVE)? "Archive" : "Wangtek"));
2852 if (tape_reset(0)!=TE_OK || tp_sense(TP_WRP|TP_POR|TP_CNI)!=TE_OK)
2854 /* No drive detected, so vanish */
2855 tpqputs(TPQD_ALWAYS, "No drive detected -- releasing IO/IRQ/DMA.");
2856 status_dead = YES;
2857 qic02_release_resources();
2858 return -EIO;
2861 /* All should be ok now */
2862 status_zombie = NO;
2863 return 0;
2864 } /* qic02_get_resources */
2866 int __init qic02_tape_init(void)
2868 if (TPSTATSIZE != 6)
2870 printk(TPQIC02_NAME ": internal error: tpstatus struct incorrect!\n");
2871 return -ENODEV;
2873 if ((TPQBUF_SIZE<512) || (TPQBUF_SIZE>=0x10000))
2875 printk(TPQIC02_NAME ": internal error: DMA buffer size out of range\n");
2876 return -ENODEV;
2879 current_tape_dev = MKDEV(QIC02_TAPE_MAJOR, 0);
2881 #ifndef CONFIG_QIC02_DYNCONF
2882 printk(TPQIC02_NAME ": IRQ %d, DMA %d, IO 0x%x, IFC %s, %s, %s\n",
2883 QIC02_TAPE_IRQ, QIC02_TAPE_DMA,
2884 # if QIC02_TAPE_IFC == WANGTEK
2885 QIC02_STAT_PORT, "Wangtek",
2886 # elif QIC02_TAPE_IFC == ARCHIVE
2887 QIC02_CMD_PORT, "Archive",
2888 # elif QIC02_TAPE_IFC == MOUNTAIN
2889 QIC02_CMD_PORT, "Mountain",
2890 # else
2891 # error
2892 # endif
2893 rcs_revision, rcs_date);
2894 if (qic02_get_resources())
2896 return -ENODEV;
2898 #else
2899 printk(TPQIC02_NAME ": Runtime config, %s, %s\n",
2900 rcs_revision, rcs_date);
2901 #endif
2902 printk(TPQIC02_NAME ": DMA buffers: %u blocks\n", NR_BLK_BUF);
2903 /* If we got this far, install driver functions */
2904 if (devfs_register_chrdev(QIC02_TAPE_MAJOR, TPQIC02_NAME, &qic02_tape_fops))
2906 printk(TPQIC02_NAME ": Unable to get chrdev major %d\n", QIC02_TAPE_MAJOR);
2907 #ifndef CONFIG_QIC02_DYNCONF
2908 qic02_release_resources();
2909 #endif
2910 return -ENODEV;
2912 devfs_register (NULL, "ntpqic11", DEVFS_FL_DEFAULT,
2913 QIC02_TAPE_MAJOR, 2,
2914 S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP,
2915 &qic02_tape_fops, NULL);
2916 devfs_register (NULL, "tpqic11", DEVFS_FL_DEFAULT,
2917 QIC02_TAPE_MAJOR, 3,
2918 S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP,
2919 &qic02_tape_fops, NULL);
2920 devfs_register (NULL, "ntpqic24", DEVFS_FL_DEFAULT,
2921 QIC02_TAPE_MAJOR, 4,
2922 S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP,
2923 &qic02_tape_fops, NULL);
2924 devfs_register (NULL, "tpqic24", DEVFS_FL_DEFAULT,
2925 QIC02_TAPE_MAJOR, 5,
2926 S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP,
2927 &qic02_tape_fops, NULL);
2928 devfs_register (NULL, "ntpqic120", DEVFS_FL_DEFAULT,
2929 QIC02_TAPE_MAJOR, 6,
2930 S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP,
2931 &qic02_tape_fops, NULL);
2932 devfs_register (NULL, "tpqic120", DEVFS_FL_DEFAULT,
2933 QIC02_TAPE_MAJOR, 7,
2934 S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP,
2935 &qic02_tape_fops, NULL);
2936 devfs_register (NULL, "ntpqic150", DEVFS_FL_DEFAULT,
2937 QIC02_TAPE_MAJOR, 8,
2938 S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP,
2939 &qic02_tape_fops, NULL);
2940 devfs_register (NULL, "tpqic150", DEVFS_FL_DEFAULT,
2941 QIC02_TAPE_MAJOR, 9,
2942 S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP,
2943 &qic02_tape_fops, NULL);
2944 init_waitqueue_head(&qic02_tape_transfer);
2945 /* prepare timer */
2946 TIMEROFF;
2947 init_timer(&tp_timer);
2948 tp_timer.function = qic02_tape_times_out;
2950 #ifndef CONFIG_QIC02_DYNCONF
2951 if (tape_reset(0)!=TE_OK || tp_sense(TP_WRP|TP_POR|TP_CNI)!=TE_OK)
2953 /* No drive detected, so vanish */
2954 tpqputs(TPQD_ALWAYS, "No drive detected -- driver going on vacation...");
2955 qic02_release_resources();
2956 status_dead = YES;
2957 return -ENODEV;
2959 else
2961 if (is_exception())
2963 tpqputs(TPQD_ALWAYS, "exception detected\n");
2964 (void) tp_sense(TP_WRP|TP_POR|TP_CNI);
2967 #endif
2969 /* initialize generic status for ioctl requests */
2971 ioctl_status.mt_type = QIC02_TAPE_DRIVE; /* MT_IS* id nr */
2973 ioctl_status.mt_resid = 0; /* ---residual count */
2974 ioctl_status.mt_gstat = 0; /* ---generic status */
2975 ioctl_status.mt_erreg = 0; /* not used */
2976 ioctl_status.mt_fileno = 0; /* number of current file on tape */
2977 ioctl_status.mt_blkno = 0; /* number of current (logical) block */
2979 return 0;
2980 } /* qic02_tape_init */
2982 #ifdef MODULE
2984 void cleanup_module(void)
2986 if (status_zombie == NO)
2988 qic02_release_resources();
2990 devfs_unregister_chrdev(QIC02_TAPE_MAJOR, TPQIC02_NAME);
2991 devfs_unregister(devfs_find_handle(NULL, "ntpqic11", QIC02_TAPE_MAJOR, 2, DEVFS_SPECIAL_CHR, 0));
2992 devfs_unregister(devfs_find_handle(NULL, "tpqic11", QIC02_TAPE_MAJOR, 3, DEVFS_SPECIAL_CHR, 0));
2993 devfs_unregister(devfs_find_handle(NULL, "ntpqic24", QIC02_TAPE_MAJOR, 4, DEVFS_SPECIAL_CHR, 0));
2994 devfs_unregister(devfs_find_handle(NULL, "tpqic24", QIC02_TAPE_MAJOR, 5, DEVFS_SPECIAL_CHR, 0));
2995 devfs_unregister(devfs_find_handle(NULL, "ntpqic120", QIC02_TAPE_MAJOR, 6, DEVFS_SPECIAL_CHR, 0));
2996 devfs_unregister(devfs_find_handle(NULL, "tpqic120", QIC02_TAPE_MAJOR, 7, DEVFS_SPECIAL_CHR, 0));
2997 devfs_unregister(devfs_find_handle(NULL, "ntpqic150", QIC02_TAPE_MAJOR, 8, DEVFS_SPECIAL_CHR, 0));
2998 devfs_unregister(devfs_find_handle(NULL, "tpqic150", QIC02_TAPE_MAJOR, 9, DEVFS_SPECIAL_CHR, 0));
3001 int init_module(void)
3003 int retval;
3004 retval=qic02_tape_init();
3005 # ifdef CONFIG_QIC02_DYNCONF
3006 /* This allows the dynamic config program to setup the card
3007 * by presetting qic02_tape_dynconf via insmod
3009 if (!retval && qic02_tape_dynconf.ifc_type)
3011 retval=update_ifc_masks(qic02_tape_dynconf.ifc_type);
3012 if (retval)
3014 cleanup_module();
3017 # endif
3018 return retval;
3020 #endif