Import 2.1.81
[davej-history.git] / drivers / char / tpqic02.c
blob461e58165407abe0ad5c637e8345c2ea253465ce
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/module.h>
79 #include <linux/sched.h>
80 #include <linux/timer.h>
81 #include <linux/fs.h>
82 #include <linux/kernel.h>
83 #include <linux/major.h>
84 #include <linux/errno.h>
85 #include <linux/mtio.h>
86 #include <linux/fcntl.h>
87 #include <linux/delay.h>
88 #include <linux/ioport.h>
89 #include <linux/tpqic02.h>
90 #include <linux/mm.h>
91 #include <linux/malloc.h>
92 #include <linux/init.h>
94 #include <asm/dma.h>
95 #include <asm/system.h>
96 #include <asm/io.h>
97 #include <asm/uaccess.h>
99 /* check existence of required configuration parameters */
100 #if !defined(QIC02_CMD_PORT) || !defined(QIC02_TAPE_IRQ) || !defined(QIC02_TAPE_DMA)
101 # error qic02_tape configuration error
102 #endif
105 #define TPQIC02_NAME "tpqic02"
107 /* Linux outb() commands have (value,port) as parameters.
108 * One might expect (port,value) instead, so beware!
111 #ifdef CONFIG_QIC02_DYNCONF
112 /* This holds the dynamic configuration info for the interface
113 * card+drive info if runtime configuration has been selected.
116 static struct mtconfiginfo qic02_tape_dynconf = /* user settable */
117 { 0, 0, BOGUS_IRQ, 0, 0, TPQD_DEFAULT_FLAGS, };
118 static struct qic02_ccb qic02_tape_ccb = { 0, }; /* private stuff */
120 #else
122 unsigned long qic02_tape_debug = TPQD_DEFAULT_FLAGS;
124 # if ((QIC02_TAPE_IFC!=WANGTEK) && (QIC02_TAPE_IFC!=ARCHIVE) && (QIC02_TAPE_IFC!=MOUNTAIN))
125 # error No valid interface card specified
126 # endif
127 #endif /* CONFIG_QIC02_DYNCONF */
129 static volatile int ctlbits = 0; /* control reg bits for tape interface */
131 static struct wait_queue *qic02_tape_transfer = NULL; /* sync rw with interrupts */
133 static volatile struct mtget ioctl_status; /* current generic status */
135 static volatile struct tpstatus tperror; /* last drive status */
137 static char rcs_revision[] = "$Revision: 1.10 $";
138 static char rcs_date[] = "$Date: 1997/01/26 07:13:20 $";
140 /* Flag bits for status and outstanding requests.
141 * (Could all be put in one bit-field-struct.)
142 * Some variables need `volatile' because they may be modified
143 * by an interrupt.
145 static volatile flag status_dead = YES; /* device is legally dead until proven alive */
146 static flag status_zombie = YES; /* it's `zombie' until irq/dma allocated */
148 static volatile flag status_bytes_wr = NO; /* write FM at close or not */
149 static volatile flag status_bytes_rd = NO; /* (rd|wr) used for rewinding */
151 static volatile unsigned long status_cmd_pending = 0; /* cmd in progress */
152 static volatile flag status_expect_int = NO; /* ready for interrupts */
153 static volatile flag status_timer_on = NO; /* using time-out */
154 static volatile int status_error = 0; /* int handler may detect error */
155 static volatile flag status_eof_detected = NO; /* end of file */
156 static volatile flag status_eom_detected = NO; /* end of recorded media */
157 static volatile flag status_eot_detected = NO; /* end of tape */
158 static volatile flag doing_read = NO;
159 static volatile flag doing_write = NO;
161 static volatile unsigned long dma_bytes_todo;
162 static volatile unsigned long dma_bytes_done;
163 static volatile unsigned dma_mode = 0; /* !=0 also means DMA in use */
164 static flag need_rewind = YES;
166 static kdev_t current_tape_dev;
167 static int extra_blocks_left = BLOCKS_BEYOND_EW;
170 /* return_*_eof:
171 * NO: not at EOF,
172 * YES: tell app EOF was reached (return 0).
174 * return_*_eof==YES && reported_*_eof==NO ==>
175 * return current buffer, next time(s) return EOF.
177 * return_*_eof==YES && reported_*_eof==YES ==>
178 * at EOF and application knows it, so we can
179 * move on to the next file.
182 static flag return_read_eof = NO; /* set to signal app EOF was reached */
183 static flag return_write_eof = NO;
184 static flag reported_read_eof = NO; /* set when we've done that */
185 static flag reported_write_eof = NO;
188 /* This is for doing `mt seek <blocknr>' */
189 static char seek_addr_buf[AR_SEEK_BUF_SIZE];
192 /* In write mode, we have to write a File Mark after the last block written,
193 * when the tape device is closed. Tape repositioning and reading in write
194 * mode is allowed as long as no actual writing has been done. After writing
195 * the File Mark, repositioning and reading are allowed again.
197 static int mode_access; /* access mode: READ or WRITE */
199 static int qic02_get_resources(void);
200 static void qic02_release_resources(void);
202 /* This is a pointer to the actual kernel buffer where the interrupt routines
203 * read from/write to. It is needed because the DMA channels 1 and 3 cannot
204 * always access the user buffers. [The kernel buffer must reside in the
205 * lower 16MBytes of system memory because of the DMA controller.] The user
206 * must ensure that a large enough buffer is passed to the kernel, in order
207 * to reduce tape repositioning wear and tear.
209 static unsigned long buffaddr = 0; /* physical address of buffer */
211 /* This translates minor numbers to the corresponding recording format: */
212 static const char *format_names[] = {
213 "not set", /* for dumb drives unable to handle format selection */
214 "11", /* extinct */
215 "24",
216 "120",
217 "150",
218 "300", /* untested. */
219 "600" /* untested. */
223 /* `exception_list' is needed for exception status reporting.
224 * Exceptions 1..14 are defined by QIC-02 rev F.
225 * The drive status is matched sequentially to each entry,
226 * ignoring irrelevant bits, until a match is found. If no
227 * match is found, exception number 0 is used. (That should of
228 * course never happen...) The original table was based on the
229 * "Exception Status Summary" in QIC-02 rev F, but some changes
230 * were required to make it work with real-world drives.
232 * Exception 2 (CNI) is changed to also cover status 0x00e0 (mask USL),
233 * Exception 4 (EOM) is changed to also cover status 0x8288 (mask EOR),
234 * Exception 11 (FIL) is changed to also cover status 0x0089 (mask EOM).
235 * Exception 15 (EOR) is added for seek-to-end-of-data (catch EOR),
236 * Exception 16 (BOM) is added for beginning-of-media (catch BOM).
238 * Had to swap EXC_NDRV and EXC_NCART to ensure that extended EXC_NCART
239 * (because of the incorrect Wangtek status code) doesn't catch the
240 * EXC_NDRV first.
242 static struct exception_list_type {
243 unsigned short mask, code;
244 const char *msg;
245 /* EXC_nr attribute should match with tpqic02.h */
246 } exception_list[] = {
247 {0, 0,
248 "Unknown exception status code", /* extra: 0 */},
249 {~(0), TP_ST0|TP_CNI|TP_USL|TP_WRP,
250 "Drive not online" /* 1 */},
251 /* Drive presence goes before cartridge presence. */
252 {~(TP_WRP|TP_USL), TP_ST0|TP_CNI,
253 /* My Wangtek 5150EQ sometimes reports a status code
254 * of 0x00e0, which is not a valid exception code, but
255 * I think it should be recognized as "NO CARTRIDGE".
257 "Cartridge not in place" /* 2 */},
258 {(unsigned short) ~(TP_ST1|TP_BOM), (TP_ST0|TP_WRP),
259 "Write protected cartridge" /* 3 */},
260 {(unsigned short) ~(TP_ST1|TP_EOR), (TP_ST0|TP_EOM),
261 "End of media" /* 4 */},
262 {~TP_WRP, TP_ST0|TP_UDA| TP_ST1|TP_BOM,
263 "Read or Write abort. Rewind tape." /* 5 */},
264 {~TP_WRP, TP_ST0|TP_UDA,
265 "Read error. Bad block transferred." /* 6 */},
266 {~TP_WRP, TP_ST0|TP_UDA|TP_BNL,
267 "Read error. Filler block transferred." /* 7 */},
268 {~TP_WRP, TP_ST0|TP_UDA|TP_BNL |TP_ST1|TP_NDT,
269 "Read error. No data detected." /* 8 */},
270 {~TP_WRP, TP_ST0|TP_EOM|TP_UDA|TP_BNL |TP_ST1|TP_NDT,
271 "Read error. No data detected. EOM." /* 9 */},
272 {~(TP_WRP|TP_MBD|TP_PAR|TP_EOR), TP_ST0|TP_UDA|TP_BNL |TP_ST1|TP_NDT|TP_BOM,
273 "Read error. No data detected. BOM." /* 10 */},
274 {~(TP_WRP|TP_EOM), TP_ST0|TP_FIL,
275 /* Status 0x0089 (EOM & FM) is viewed as an FM,
276 * because it can only happen during a read.
277 * EOM is checked separately for an FM condition.
279 "File mark detected" /* 11 */},
280 {~(TP_ST0|TP_CNI|TP_USL|TP_WRP|TP_BOM), TP_ST1|TP_ILL,
281 "Illegal command" /* 12 */},
282 {~(TP_ST0|TP_CNI|TP_USL|TP_WRP|TP_BOM), TP_ST1|TP_POR,
283 "Reset occurred" /* 13 */},
284 {~TP_WRP, TP_ST0|TP_FIL|TP_MBD, /* NOTE: ST1 not set! */
285 "Marginal block detected" /* 14 */},
286 {~(TP_ST0|TP_WRP|TP_EOM|TP_UDA|TP_BNL|TP_FIL |TP_NDT), TP_ST1|TP_EOR,
287 /********** Is the extra TP_NDT really needed Eddy? **********/
288 "End of recorded media" /* extra: 15 */},
289 /* 15 is returned when SEEKEOD completes successfully */
290 {~(TP_WRP|TP_ST0), TP_ST1|TP_BOM,
291 "Beginning of media" /* extra: 16 */}
293 #define NR_OF_EXC (sizeof(exception_list)/sizeof(struct exception_list_type))
295 /* Compare expected struct size and actual struct size. This
296 * is useful to catch programs compiled with old #includes.
298 #define CHECK_IOC_SIZE(structure) \
299 if (_IOC_SIZE(iocmd) != sizeof(struct structure)) { \
300 tpqputs(TPQD_ALWAYS, "sizeof(struct " #structure \
301 ") does not match!"); \
302 return -EFAULT; \
305 static void tpqputs(unsigned long flags, const char *s)
307 if ((flags & TPQD_ALWAYS) || (flags & QIC02_TAPE_DEBUG))
308 printk(TPQIC02_NAME ": %s\n", s);
309 } /* tpqputs */
312 /* Perform byte order swapping for a 16-bit word.
314 * [FIXME] This should probably be in include/asm/
315 * ([FIXME] i486 can do this faster)
317 static inline void byte_swap_w(volatile unsigned short * w)
319 int t = *w;
321 *w = (t>>8) | ((t & 0xff)<<8);
326 /* Init control register bits on interface card.
327 * For Archive, interrupts must be enabled explicitly.
328 * Wangtek interface card requires ONLINE to be set, Archive SC402/SC499R
329 * cards keep it active all the time.
331 static void ifc_init(void)
333 if (QIC02_TAPE_IFC == WANGTEK) /* || (QIC02_TAPE_IFC == EVEREX) */
335 ctlbits = WT_CTL_ONLINE; /* online */
336 outb_p(ctlbits, QIC02_CTL_PORT);
338 else if (QIC02_TAPE_IFC == ARCHIVE)
340 ctlbits = 0; /* no interrupts yet */
341 outb_p(ctlbits, QIC02_CTL_PORT);
342 outb_p(0, AR_RESET_DMA_PORT); /* dummy write to reset DMA */
344 else /* MOUNTAIN */
346 ctlbits = MTN_CTL_ONLINE; /* online, and logic enabled */
347 outb_p(ctlbits, QIC02_CTL_PORT);
349 } /* ifc_init */
352 static void report_qic_exception(unsigned n)
354 if (n >= NR_OF_EXC)
356 tpqputs(TPQD_ALWAYS, "Oops -- report_qic_exception");
357 n = 0;
359 if (TPQDBG(SENSE_TEXT) || n==0)
361 printk(TPQIC02_NAME ": sense: %s\n", exception_list[n].msg);
363 } /* report_qic_exception */
366 /* Try to map the drive-exception bits `s' to a predefined "exception number",
367 * by comparing the significant exception bits for each entry in the
368 * exception table (`exception_list[]').
369 * It is assumed that s!=0.
371 static int decode_qic_exception_nr(unsigned s)
373 int i;
375 for (i=1; i<NR_OF_EXC; i++)
377 if ((s & exception_list[i].mask)==exception_list[i].code)
379 return i;
382 printk(TPQIC02_NAME ": decode_qic_exception_nr: exception(%x) not recognized\n", s);
383 return 0;
384 } /* decode_qic_exception_nr */
388 /* Perform appropriate action for certain exceptions.
389 * should return a value to indicate stop/continue (in case of bad blocks)
391 static void handle_qic_exception(int exnr, int exbits)
393 if (exnr==EXC_NCART)
395 /* Cartridge was changed. Redo sense().
396 * EXC_NCART should be handled in open().
397 * It is not permitted to remove the tape while
398 * the tape driver has open files.
400 need_rewind = YES;
401 status_eof_detected = NO;
402 status_eom_detected = NO;
404 else if (exnr==EXC_XFILLER)
406 tpqputs(TPQD_ALWAYS, "[Bad block -- filler data transferred.]");
408 else if (exnr==EXC_XBAD)
410 tpqputs(TPQD_ALWAYS, "[CRC failed!]");
412 else if (exnr==EXC_MARGINAL)
414 /* A marginal block behaves much like a FM.
415 * User may continue reading, if desired.
417 tpqputs(TPQD_ALWAYS, "[Marginal block]");
418 doing_read = NO;
420 else if (exnr==EXC_FM)
422 doing_read = NO;
424 } /* handle_qic_exception */
427 static inline int is_exception(void)
429 return (inb(QIC02_STAT_PORT) & QIC02_STAT_EXCEPTION) == 0;
430 } /* is_exception */
433 /* Reset the tape drive and controller.
434 * When reset fails, it marks the drive as dead and all
435 * requests (except reset) are to be ignored (ENXIO).
437 static int tape_reset(int verbose)
439 ifc_init(); /* reset interface card */
441 /* assert reset */
442 if (QIC02_TAPE_IFC == MOUNTAIN)
444 outb_p(ctlbits & ~MTN_QIC02_CTL_RESET_NOT, QIC02_CTL_PORT);
446 else /* WANGTEK, ARCHIVE */
448 outb_p(ctlbits | QIC02_CTL_RESET, QIC02_CTL_PORT);
451 /* Next, we need to wait >=25 usec. */
452 udelay(30);
454 /* after reset, we will be at BOT (modulo an automatic rewind) */
455 status_eof_detected = NO;
456 status_eom_detected = NO;
457 status_cmd_pending = 0;
458 need_rewind = YES;
459 doing_read = doing_write = NO;
460 ioctl_status.mt_fileno = ioctl_status.mt_blkno = 0;
462 /* de-assert reset */
463 if (QIC02_TAPE_IFC == MOUNTAIN)
465 outb_p(ctlbits | MTN_QIC02_CTL_RESET_NOT, QIC02_CTL_PORT);
467 else
469 outb_p(ctlbits & ~QIC02_CTL_RESET, QIC02_CTL_PORT);
472 /* KLUDGE FOR G++ BUG */
473 { int stat = inb_p(QIC02_STAT_PORT);
474 status_dead = ((stat & QIC02_STAT_RESETMASK) != QIC02_STAT_RESETVAL); }
475 /* if successful, inb(STAT) returned RESETVAL */
476 if (status_dead == YES)
478 printk(TPQIC02_NAME ": reset failed!\n");
480 else if (verbose)
482 printk(TPQIC02_NAME ": reset successful\n");
485 return (status_dead == YES)? TE_DEAD : TE_OK;
486 } /* tape_reset */
490 /* Notify tape drive of a new command. It only waits for the
491 * command to be accepted, not for the actual command to complete.
493 * Before calling this routine, QIC02_CMD_PORT must have been loaded
494 * with the command to be executed.
495 * After this routine, the exception bit must be checked.
496 * This routine is also used by rdstatus(), so in that case, any exception
497 * must be ignored (`ignore_ex' flag).
499 static int notify_cmd(char cmd, short ignore_ex)
501 int i;
503 outb_p(cmd, QIC02_CMD_PORT); /* output the command */
505 /* wait 1 usec before asserting /REQUEST */
506 udelay(1);
508 if ((!ignore_ex) && is_exception()) {
509 tpqputs(TPQD_ALWAYS, "*** exception detected in notify_cmd");
510 /** force a reset here **/
511 if (tape_reset(1)==TE_DEAD)
512 return TE_DEAD;
513 if (is_exception()) {
514 tpqputs(TPQD_ALWAYS, "exception persists after reset.");
515 tpqputs(TPQD_ALWAYS, " ^ exception ignored.");
519 outb_p(ctlbits | QIC02_CTL_REQUEST, QIC02_CTL_PORT); /* set request bit */
520 i = TAPE_NOTIFY_TIMEOUT;
521 /* The specs say this takes about 500 usec, but there is no upper limit!
522 * If the drive were busy retensioning or something like that,
523 * it could be *much* longer!
525 while ((inb_p(QIC02_STAT_PORT) & QIC02_STAT_READY) && (--i>0))
526 /*skip*/; /* wait for ready */
527 if (i==0) {
528 tpqputs(TPQD_ALWAYS, "timed out waiting for ready in notify_cmd");
529 status_dead = YES;
530 return TE_TIM;
533 outb_p(ctlbits & ~QIC02_CTL_REQUEST, QIC02_CTL_PORT); /* reset request bit */
534 i = TAPE_NOTIFY_TIMEOUT;
535 /* according to the specs this one should never time-out */
536 while (((inb_p(QIC02_STAT_PORT) & QIC02_STAT_READY) == 0) && (--i>0))
537 /*skip*/; /* wait for not ready */
538 if (i==0) {
539 tpqputs(TPQD_ALWAYS, "timed out waiting for !ready in notify_cmd");
540 status_dead = YES;
541 return TE_TIM;
543 /* command accepted */
544 return TE_OK;
545 } /* notify_cmd */
549 /* Wait for a command to complete, with timeout */
550 static int wait_for_ready(time_t timeout)
552 int stat;
553 time_t spin_t;
555 /* Wait for ready or exception, without driving the loadavg up too much.
556 * In most cases, the tape drive already has READY asserted,
557 * so optimize for that case.
559 * First, busy wait a few usec:
561 spin_t = 50;
562 while (((stat = inb_p(QIC02_STAT_PORT) & QIC02_STAT_MASK) == QIC02_STAT_MASK) && (--spin_t>0))
563 /*SKIP*/;
564 if ((stat & QIC02_STAT_READY) == 0)
565 return TE_OK; /* covers 99.99% of all calls */
567 /* Then use schedule() a few times */
568 spin_t = 3; /* max 0.03 sec busy waiting */
569 if (spin_t > timeout)
570 spin_t = timeout;
571 timeout -= spin_t;
572 spin_t += jiffies;
574 while (((stat = inb_p(QIC02_STAT_PORT) & QIC02_STAT_MASK) == QIC02_STAT_MASK) && (jiffies<spin_t))
575 schedule(); /* don't waste all the CPU time */
576 if ((stat & QIC02_STAT_READY) == 0)
577 return TE_OK;
579 /* If we reach this point, we probably need to wait much longer, or
580 * an exception occurred. Either case is not very time-critical.
581 * Check the status port only a few times every second.
582 * A interval of less than 0.10 sec will not be noticed by the user,
583 * more than 0.40 sec may give noticeable delays.
585 spin_t += timeout;
586 TPQDEB({printk("wait_for_ready: additional timeout: %d\n", spin_t);})
588 /* not ready and no exception && timeout not expired yet */
589 while (((stat = inb_p(QIC02_STAT_PORT) & QIC02_STAT_MASK) == QIC02_STAT_MASK) && (jiffies<spin_t)) {
590 /* be `nice` to other processes on long operations... */
591 current->timeout = jiffies + 3*HZ/10; /* nap 0.30 sec between checks, */
592 current->state = TASK_INTERRUPTIBLE;
593 schedule(); /* but could be woken up earlier by signals... */
596 /* don't use jiffies for this test because it may have changed by now */
597 if ((stat & QIC02_STAT_MASK) == QIC02_STAT_MASK) {
598 tpqputs(TPQD_ALWAYS, "wait_for_ready() timed out");
599 return TE_TIM;
602 if ((stat & QIC02_STAT_EXCEPTION) == 0) {
603 tpqputs(TPQD_ALWAYS, "exception detected after waiting_for_ready");
604 return TE_EX;
605 } else {
606 return TE_OK;
608 } /* wait_for_ready */
612 /* Send some data to the drive */
613 static int send_qic02_data(char sb[], unsigned size, int ignore_ex)
615 int i, stat;
617 for (i=0; i<size; i++) {
619 stat = wait_for_ready(TIM_S);
620 if (stat != TE_OK)
621 return stat;
623 stat = notify_cmd(sb[i], ignore_ex);
624 if (stat != TE_OK)
625 return stat;
627 return TE_OK;
629 } /* send_qic02_data */
632 /* Send a QIC-02 command (`cmd') to the tape drive, with
633 * a time-out (`timeout').
634 * This one is also used by tp_sense(), so we must have
635 * a flag to disable exception checking (`ignore_ex').
637 * On entry, the controller is supposed to be READY.
639 static int send_qic02_cmd(int cmd, time_t timeout, int ignore_ex)
641 int stat;
643 stat = inb_p(QIC02_STAT_PORT);
644 if ((stat & QIC02_STAT_EXCEPTION) == 0) { /* if exception */
645 tpqputs(TPQD_ALWAYS, "send_qic02_cmd: Exception!");
646 return TE_EX;
648 if (stat & QIC02_STAT_READY) { /* if not ready */
649 tpqputs(TPQD_ALWAYS, "send_qic02_cmd: not Ready!");
650 return TE_ERR;
653 /* assert(ready & !exception) */
655 /* Remember current command for later re-use with dma transfers.
656 * (For reading/writing multiple blocks.)
658 status_cmd_pending = cmd;
660 stat = notify_cmd(cmd, ignore_ex); /* tell drive new command was loaded, */
661 /* inherit exception check. */
662 if (TP_HAVE_SEEK && (cmd == AR_QCMDV_SEEK_BLK)) {
663 /* This one needs to send 3 more bytes, MSB first */
664 stat = send_qic02_data(seek_addr_buf, sizeof(seek_addr_buf), ignore_ex);
667 if (stat != TE_OK) {
668 tpqputs(TPQD_ALWAYS, "send_qic02_cmd failed");
670 return stat;
671 } /* send_qic02_cmd */
675 /* Get drive status. Assume drive is ready or has exception set.
676 * (or will be in <1000 usec.)
677 * Extra parameters added because of 'Read Extended Status 3' command.
679 static int rdstatus(char *stp, unsigned size, char qcmd)
681 int s, n;
682 char *q = stp;
684 /* Try to busy-wait a few (700) usec, after that de-schedule.
686 * The problem is, if we don't de-schedule, performance will
687 * drop to zero when the drive is not responding and if we
688 * de-schedule immediately, we waste a lot of time because a
689 * task switch is much longer than we usually have to wait here.
691 n = 1000; /* 500 is not enough on a 486/33 */
692 while ((n>0) && ((inb_p(QIC02_STAT_PORT) & QIC02_STAT_MASK) == QIC02_STAT_MASK))
693 n--; /* wait for ready or exception or timeout */
694 if (n==0) {
695 /* n (above) should be chosen such that on your machine
696 * you rarely ever see the message below, and it should
697 * be small enough to give reasonable response time.]
699 tpqputs(TPQD_ALWAYS, "waiting looong in rdstatus() -- drive dead?");
700 while ((inb_p(QIC02_STAT_PORT) & QIC02_STAT_MASK) == QIC02_STAT_MASK)
701 schedule();
702 tpqputs(TPQD_ALWAYS, "finished waiting in rdstatus()");
705 (void) notify_cmd(qcmd, 1); /* send read status command */
706 /* ignore return code -- should always be ok, STAT may contain
707 * exception flag from previous exception which we are trying to clear.
710 if (TP_DIAGS(current_tape_dev))
711 printk(TPQIC02_NAME ": reading status bytes: ");
713 for (q=stp; q<stp+size; q++)
715 do s = inb_p(QIC02_STAT_PORT);
716 while ((s & QIC02_STAT_MASK) == QIC02_STAT_MASK); /* wait for ready or exception */
718 if ((s & QIC02_STAT_EXCEPTION) == 0) { /* if exception */
719 tpqputs(TPQD_ALWAYS, "rdstatus: exception error");
720 ioctl_status.mt_erreg = 0; /* dunno... */
721 return TE_NS; /* error, shouldn't happen... */
724 *q = inb_p(QIC02_DATA_PORT); /* read status byte */
726 if (TP_DIAGS(current_tape_dev))
727 printk("[%1d]=0x%x ", q-stp, (unsigned) (*q) & 0xff);
729 outb_p(ctlbits | QIC02_CTL_REQUEST, QIC02_CTL_PORT); /* set request */
731 while ((inb_p(QIC02_STAT_PORT) & QIC02_STAT_READY) == 0); /* wait for not ready */
733 udelay(22); /* delay >20 usec */
735 outb_p(ctlbits & ~QIC02_CTL_REQUEST, QIC02_CTL_PORT); /* un-set request */
739 /* Specs say we should wait for READY here.
740 * My drive doesn't seem to need it here yet, but others do?
742 while (inb_p(QIC02_STAT_PORT) & QIC02_STAT_READY)
743 /*skip*/; /* wait for ready */
745 if (TP_DIAGS(current_tape_dev))
746 printk("\n");
748 return TE_OK;
749 } /* rdstatus */
753 /* Get standard status (6 bytes).
754 * The `.dec' and `.urc' fields are in MSB-first byte-order,
755 * so they have to be swapped first.
757 static int get_status(volatile struct tpstatus *stp)
759 int stat = rdstatus((char *) stp, TPSTATSIZE, QCMD_RD_STAT);
760 #if defined(i386) || defined(i486)
761 byte_swap_w(&(stp->dec));
762 byte_swap_w(&(stp->urc));
763 #else
764 /* should probably swap status bytes #definition */
765 #endif
766 return stat;
767 } /* get_status */
770 #if 0
771 /* This fails for my Wangtek drive */
772 /* get "Extended Status Register 3" (64 bytes)
774 * If the meaning of the returned bytes were known, the MT_TYPE
775 * identifier could be used to decode them, since they are
776 * "vendor unique". :-(
778 static int get_ext_status3(void)
780 char vus[64]; /* vendor unique status */
781 int stat, i;
783 tpqputs(TPQD_ALWAYS, "Attempting to read Extended Status 3...");
784 stat = rdstatus(vus, sizeof(vus), QCMD_RD_STAT_X3);
785 if (stat != TE_OK)
786 return stat;
788 tpqputs(TPQD_ALWAYS, "Returned status bytes:");
789 for (i=0; i<sizeof(vus); i++) {
790 if ( i % 8 == 0 )
791 printk("\n" TPQIC02_NAME ": %2d:");
792 printk(" %2x", vus[i] & 0xff);
794 printk("\n");
796 return TE_OK;
797 } /* get_ext_status3 */
798 #endif
801 /* Read drive status and set generic status too.
802 * NOTE: Once we do a tp_sense(), read/write transfers are killed.
804 static int tp_sense(int ignore)
806 unsigned err = 0, exnr = 0, gs = 0;
807 static void finish_rw(int cmd);
809 if (TPQDBG(SENSE_TEXT))
810 printk(TPQIC02_NAME ": tp_sense(ignore=0x%x) enter\n", ignore);
812 /* sense() is not allowed during a read or write cycle */
813 if (doing_write == YES)
814 tpqputs(TPQD_ALWAYS, "Warning: File Mark inserted because of sense() request");
815 /* The extra test is to avoid calling finish_rw during booting */
816 if ((doing_read!=NO) || (doing_write!=NO))
817 finish_rw(QCMD_RD_STAT);
819 if (get_status(&tperror) != TE_OK) {
820 tpqputs(TPQD_ALWAYS, "tp_sense: could not read tape drive status");
821 return TE_ERR;
824 err = tperror.exs; /* get exception status bits */
825 if (err & (TP_ST0|TP_ST1))
826 printk(TPQIC02_NAME ": tp_sense: status: %x, error count: %d, underruns: %d\n",
827 tperror.exs, tperror.dec, tperror.urc);
828 else if ((tperror.dec!=0) || (tperror.urc!=0) || TPQDBG(SENSE_CNTS))
829 printk(TPQIC02_NAME ": tp_sense: no hard errors, soft error count: %d, underruns: %d\n",
830 tperror.dec, tperror.urc);
832 /* Set generic status. HP-UX defines these, but some extra would
833 * be useful. Problem is to remain compatible. [Do we want to be
834 * compatible??]
836 if (err & TP_ST0) {
837 if (err & TP_CNI) /* no cartridge */
838 gs |= GMT_DR_OPEN(-1);
839 if (status_dead == NO)
840 gs |= GMT_ONLINE(-1); /* always online */
841 if (err & TP_USL) /* not online */
842 gs &= ~GMT_ONLINE(-1);
843 if (err & TP_WRP)
844 gs |= GMT_WR_PROT(-1);
845 if (err & TP_EOM) { /* end of media */
846 gs |= GMT_EOT(-1); /* not sure this is correct for writes */
847 status_eom_detected = YES;
848 /* I don't know whether drive always reports EOF at or before EOM. */
849 status_eof_detected = YES;
851 /** if (err & TP_UDA) "Unrecoverable data error" **/
852 /** if (err & TP_BNL) "Bad block not located" **/
853 if (err & TP_FIL) {
854 gs |= GMT_EOF(-1);
855 status_eof_detected = YES;
858 if (err & TP_ST1) {
859 /** if (err & TP_ILL) "Illegal command" **/
860 /** if (err & TP_NDT) "No data detected" **/
861 /** if (err & TP_MBD) "Marginal block detected" **/
862 if (err & TP_BOM)
863 gs |= GMT_BOT(-1); /* beginning of tape */
865 ioctl_status.mt_gstat = gs;
866 ioctl_status.mt_dsreg = tperror.exs; /* "drive status" */
867 ioctl_status.mt_erreg = tperror.dec; /* "sense key error" */
869 if (err & (TP_ST0|TP_ST1)) {
870 /* My Wangtek occasionally reports `status' 1212 which should be ignored. */
871 exnr = decode_qic_exception_nr(err);
872 handle_qic_exception(exnr, err); /* update driver state wrt drive status */
873 report_qic_exception(exnr);
875 err &= ~ignore; /* mask unwanted errors -- not the correct way, use exception nrs?? */
876 if (((err & TP_ST0) && (err & REPORT_ERR0)) ||
877 ((err & TP_ST1) && (err & REPORT_ERR1)))
878 return TE_ERR;
879 return TE_OK;
880 } /* tp_sense */
884 /* Wait for a wind or rewind operation to finish or
885 * to time-out. (May take very long).
887 static int wait_for_rewind(time_t timeout)
889 int stat;
891 stat = inb(QIC02_STAT_PORT) & QIC02_STAT_MASK;
892 if (TPQDBG(REWIND))
893 printk(TPQIC02_NAME ": Waiting for (re-)wind to finish: stat=0x%x\n", stat);
895 stat = wait_for_ready(timeout);
897 if (stat != TE_OK) {
898 tpqputs(TPQD_ALWAYS, "(re-) winding failed\n");
900 return stat;
901 } /* wait_for_rewind */
905 /* Perform a full QIC02 command, and wait for completion,
906 * check status when done. Complain about exceptions.
908 * This function should return an OS error code when
909 * something goes wrong, 0 otherwise.
911 static int ll_do_qic_cmd(int cmd, time_t timeout)
913 int stat;
915 if (status_dead == YES) {
916 tpqputs(TPQD_ALWAYS, "Drive is dead. Do a `mt reset`.");
917 return -ENXIO; /* User should do an MTRESET. */
920 stat = wait_for_ready(timeout); /* wait for ready or exception */
921 if (stat == TE_EX) {
922 if (tp_sense(TP_WRP|TP_BOM|TP_EOM|TP_FIL)!=TE_OK)
923 return -EIO;
924 /* else nothing to worry about, I hope */
925 stat = TE_OK;
927 if (stat != TE_OK) {
928 printk(TPQIC02_NAME ": ll_do_qic_cmd(%x, %ld) failed\n", cmd, (long) timeout);
929 return -EIO;
933 #if OBSOLETE
934 /* wait for ready since it may not be active immediately after reading status */
935 while ((inb_p(QIC02_STAT_PORT) & QIC02_STAT_READY) != 0);
936 #endif
938 stat = send_qic02_cmd(cmd, timeout, 0); /* (checks for exceptions) */
940 if (cmd==QCMD_RD_FM) {
941 status_eof_detected = NO;
942 ioctl_status.mt_fileno++;
943 /* Should update block count as well, but can't.
944 * Can do a `read address' for some drives, when MTNOP is done.
946 } else if (cmd==QCMD_WRT_FM) {
947 status_eof_detected = NO;
948 ioctl_status.mt_fileno++;
949 } else if ((cmd==QCMD_REWIND) || (cmd==QCMD_ERASE) || (cmd==QCMD_RETEN)) {
950 status_eof_detected = NO;
951 status_eom_detected = NO;
952 status_eot_detected = NO;
953 need_rewind = NO;
954 ioctl_status.mt_fileno = ioctl_status.mt_blkno = 0;
955 extra_blocks_left = BLOCKS_BEYOND_EW;
956 return_write_eof = NO;
957 return_read_eof = NO;
958 reported_read_eof = NO;
959 reported_write_eof = NO;
961 /* sense() will set eof/eom as required */
962 if (stat==TE_EX) {
963 if (tp_sense(TP_WRP|TP_BOM|TP_EOM|TP_FIL)!=TE_OK) {
964 printk(TPQIC02_NAME ": Exception persist in ll_do_qic_cmd[1](%x, %ld)", cmd, (long) timeout);
965 status_dead = YES;
966 return -ENXIO;
967 /* if rdstatus fails too, we're in trouble */
970 else if (stat!=TE_OK) {
971 printk(TPQIC02_NAME ": ll_do_qic_cmd: send_qic02_cmd failed, stat = 0x%x\n", stat);
972 return -EIO; /*** -EIO is probably not always appropriate */
976 if (timeout == TIM_R)
977 stat = wait_for_rewind(timeout);
978 else
979 stat = wait_for_ready(timeout);
981 if (stat==TE_EX) {
982 if (tp_sense((cmd==QCMD_SEEK_EOD ? /*****************************/
983 TP_EOR|TP_NDT|TP_UDA|TP_BNL|TP_WRP|TP_BOM|TP_EOM|TP_FIL :
984 TP_WRP|TP_BOM|TP_EOM|TP_FIL))!=TE_OK) {
985 printk(TPQIC02_NAME ": Exception persist in ll_do_qic_cmd[2](%x, %ld)\n", cmd, (long) timeout);
986 if (cmd!=QCMD_RD_FM)
987 status_dead = YES;
988 return -ENXIO;
989 /* if rdstatus fails too, we're in trouble */
992 else if (stat!=TE_OK) {
993 printk(TPQIC02_NAME ": ll_do_qic_cmd %x: wait failed, stat == 0x%x\n", cmd, stat);
994 return -EIO;
996 return 0;
997 } /* ll_do_qic_cmd */
1001 * Problem: What to do when the user cancels a read/write operation
1002 * in-progress?
1004 * "Deactivating ONLINE during a READ also causes the"
1005 * "tape to be rewound to BOT." Ditto for WRITEs, except
1006 * a FM is written first. "The host may alternatively terminate
1007 * the READ/WRITE command by issuing a RFM/WFM command."
1009 * For READs:
1010 * Neither option will leave the tape positioned where it was.
1011 * Another (better?) solution is to terminate the READ by two
1012 * subsequent sense() operations, the first to stop the current
1013 * READ cycle, the second to clear the `Illegal command' exception,
1014 * because the QIC-02 specs didn't anticipate this. This is
1015 * delayed until actually needed, so a tar listing can be aborted
1016 * by the user and continued later.
1017 * If anybody has a better solution, let me know! [Also, let me
1018 * know if your drive (mine is a Wangtek5150EQ) does not accept
1019 * this sequence for canceling the read-cycle.]
1021 * For WRITEs it's simple: Just do a WRITE_FM, leaving the tape
1022 * positioned after the FM.
1025 static void terminate_read(int cmd)
1027 if (doing_read == YES) {
1028 doing_read = NO;
1029 if (cmd != QCMD_RD_FM) {
1030 /* if the command is a RFM, there is no need to do this
1031 * because a RFM will legally terminate the read-cycle.
1033 tpqputs(TPQD_ALWAYS, "terminating pending read-cycle");
1035 /* I'm not too sure about this part -- hhb */
1036 if (QIC02_TAPE_IFC == MOUNTAIN) {
1037 /* Mountain reference says can terminate by de-asserting online */
1038 ctlbits &= ~MTN_QIC02_CTL_ONLINE;
1041 if (tp_sense(TP_FIL|TP_EOM|TP_WRP) != TE_OK) {
1042 tpqputs(TPQD_ALWAYS, "finish_rw[read1]: ignore the 2 lines above");
1043 if (is_exception()) {
1044 if (tp_sense(TP_ILL|TP_FIL|TP_EOM|TP_WRP) != TE_OK)
1045 tpqputs(TPQD_ALWAYS, "finish_rw[read2]: read cycle error");
1050 } /* terminate_read */
1053 static void terminate_write(int cmd)
1055 int stat;
1057 if (doing_write == YES) {
1058 doing_write = NO;
1059 /* Finish writing by appending a FileMark at the end. */
1060 if (cmd != QCMD_WRT_FM) {
1061 /* finish off write cycle */
1062 stat = ll_do_qic_cmd(QCMD_WRT_FM, TIM_M);
1063 if (stat != TE_OK)
1064 tpqputs(TPQD_ALWAYS, "Couldn't finish write cycle properly");
1065 (void) tp_sense(0);
1067 /* If there is an EOF token waiting to be returned to
1068 * the (writing) application, discard it now.
1069 * We could be at EOT, so don't reset return_write_eof.
1071 reported_write_eof=YES;
1073 } /* terminate_write */
1076 /* terminate read or write cycle because of command `cmd' */
1077 static void finish_rw(int cmd)
1079 if (wait_for_ready(TIM_S) != TE_OK) {
1080 tpqputs(TPQD_ALWAYS, "error: drive not ready in finish_rw() !");
1081 return;
1083 terminate_read(cmd);
1084 terminate_write(cmd);
1085 } /* finish_rw */
1088 /* Perform a QIC command through ll_do_qic_cmd().
1089 * If necessary, rewind the tape first.
1090 * Return an OS error code if something goes wrong, 0 if all is well.
1092 static int do_qic_cmd(int cmd, time_t timeout)
1094 int stat;
1097 finish_rw(cmd);
1099 if (need_rewind) {
1100 tpqputs(TPQD_REWIND, "Rewinding tape...");
1101 stat = ll_do_qic_cmd(QCMD_REWIND, TIM_R);
1102 if (stat != 0) {
1103 printk(TPQIC02_NAME ": rewind failed in do_qic_cmd(). stat=0x%2x", stat);
1104 return stat;
1106 need_rewind = NO;
1107 if (cmd==QCMD_REWIND) /* don't wind beyond BOT ;-) */
1108 return 0;
1111 return ll_do_qic_cmd(cmd, timeout);
1112 } /* do_qic_cmd */
1115 /* Not all ioctls are supported for all drives. Some rely on
1116 * optional QIC-02 commands. Check tpqic02.h for configuration.
1117 * Some of these commands may require ONLINE to be active.
1119 static int do_ioctl_cmd(int cmd)
1121 int stat;
1123 /* It is not permitted to read or wind the tape after bytes have
1124 * been written. It is not permitted to write the tape while in
1125 * read mode.
1126 * We try to be kind and allow reading again after writing a FM...
1129 switch (cmd) {
1130 case MTRESET:
1131 /* reset verbose */
1132 return (tape_reset(1)==TE_OK)? 0 : -EIO;
1134 case MTFSF:
1135 tpqputs(TPQD_IOCTLS, "MTFSF forward searching filemark");
1136 if ((mode_access==WRITE) && status_bytes_wr)
1137 return -EACCES;
1138 return do_qic_cmd(QCMD_RD_FM, TIM_F);
1140 case MTBSF:
1141 if (TP_HAVE_BSF) {
1142 tpqputs(TPQD_IOCTLS, "MTBSF backward searching filemark -- optional command");
1143 if ((mode_access==WRITE) && status_bytes_wr)
1144 return -EACCES;
1145 stat = do_qic_cmd(QCMD_RD_FM_BCK, TIM_F);
1146 } else {
1147 stat = -ENXIO;
1149 status_eom_detected = status_eof_detected = NO;
1150 return stat;
1152 case MTFSR:
1153 if (TP_HAVE_FSR) { /* This is an optional QIC-02 command */
1154 tpqputs(TPQD_IOCTLS, "MTFSR forward space record");
1155 if ((mode_access==WRITE) && status_bytes_wr)
1156 return -EACCES;
1157 stat = do_qic_cmd(QCMD_SPACE_FWD, TIM_F);
1158 } else {
1159 /**** fake it by doing a read data block command? ******/
1160 tpqputs(TPQD_IOCTLS, "MTFSR not supported");
1161 stat = -ENXIO;
1163 return stat;
1165 case MTBSR:
1166 if (TP_HAVE_BSR) { /* This is an optional QIC-02 command */
1167 /* we need this for appending files with GNU tar!! */
1168 tpqputs(TPQD_IOCTLS, "MTFSR backward space record");
1169 if ((mode_access==WRITE) && status_bytes_wr)
1170 return -EACCES;
1171 stat = do_qic_cmd(QCMD_SPACE_BCK, TIM_F);
1172 } else {
1173 tpqputs(TPQD_IOCTLS, "MTBSR not supported");
1174 stat = -ENXIO;
1176 status_eom_detected = status_eof_detected = NO;
1177 return stat;
1179 case MTWEOF:
1180 tpqputs(TPQD_IOCTLS, "MTWEOF write eof mark");
1181 /* Plain GNU mt(1) 2.2 uses read-only mode for writing FM. :-( */
1182 if (mode_access==READ)
1183 return -EACCES;
1185 /* allow tape movement after writing FM */
1186 status_bytes_rd = status_bytes_wr; /* Kludge-O-Matic */
1187 status_bytes_wr = NO;
1188 return do_qic_cmd(QCMD_WRT_FM, TIM_M);
1189 /* not sure what to do with status_bytes when WFM should fail */
1191 case MTREW:
1192 tpqputs(TPQD_IOCTLS, "MTREW rewinding tape");
1193 if ((mode_access==WRITE) && status_bytes_wr)
1194 return -EACCES;
1195 status_eom_detected = status_eof_detected = NO;
1196 return do_qic_cmd(QCMD_REWIND, TIM_R);
1198 case MTOFFL:
1199 tpqputs(TPQD_IOCTLS, "MTOFFL rewinding & going offline");
1200 /* Doing a drive select will clear (unlock) the current drive.
1201 * But that requires support for multiple drives and locking.
1203 if ((mode_access==WRITE) && status_bytes_wr)
1204 return -EACCES;
1205 status_eom_detected = status_eof_detected = NO;
1206 /**** do rewind depending on minor bits??? ***/
1207 stat = do_qic_cmd(QCMD_REWIND, TIM_R);
1208 return stat;
1210 case MTNOP:
1211 tpqputs(TPQD_IOCTLS, "MTNOP setting status only");
1212 /********** should do `read position' for drives that support it **********/
1213 return (tp_sense(-1)==TE_OK)? 0 : -EIO; /**** check return codes ****/
1215 case MTRETEN:
1216 tpqputs(TPQD_IOCTLS, "MTRETEN retension tape");
1217 if ((mode_access==WRITE) && status_bytes_wr)
1218 return -EACCES;
1219 status_eom_detected = status_eof_detected = NO;
1220 return do_qic_cmd(QCMD_RETEN, TIM_R);
1222 case MTBSFM:
1223 /* Think think is like MTBSF, except that
1224 * we shouldn't skip the FM. Tricky.
1225 * Maybe use RD_FM_BCK, then do a SPACE_FWD?
1227 tpqputs(TPQD_IOCTLS, "MTBSFM not supported");
1228 if ((mode_access==WRITE) && status_bytes_wr)
1229 return -EACCES;
1230 return -ENXIO;
1232 case MTFSFM:
1233 /* I think this is like MTFSF, except that
1234 * we shouldn't skip the FM. Tricky.
1235 * Maybe use QCMD_RD_DATA until we get a TP_FIL exception?
1236 * But then the FM will have been skipped...
1237 * Maybe use RD_FM, then RD_FM_BCK, but not all
1238 * drives will support that!
1240 tpqputs(TPQD_IOCTLS, "MTFSFM not supported");
1241 if ((mode_access==WRITE) && status_bytes_wr)
1242 return -EACCES;
1243 return -ENXIO;
1245 case MTEOM:
1246 /* This should leave the tape ready for appending
1247 * another file to the end, such that it would append
1248 * after the last FM on tape.
1250 tpqputs(TPQD_IOCTLS, "MTEOM search for End Of recorded Media");
1251 if ((mode_access==WRITE) && status_bytes_wr)
1252 return -EACCES;
1253 if (TP_HAVE_EOD) {
1254 /* Use faster seeking when possible.
1255 * This requires the absence of data beyond the EOM.
1256 * It seems that my drive does not always perform the
1257 * SEEK_EOD correctly, unless it is preceded by a
1258 * rewind command.
1260 # if 0
1261 status_eom_detected = status_eof_detected = NO;
1262 # endif
1263 stat = do_qic_cmd(QCMD_REWIND, TIM_R);
1264 if (stat)
1265 return stat;
1266 stat = do_qic_cmd(QCMD_SEEK_EOD, TIM_F);
1267 /* After a successful seek, TP_EOR should be returned */
1268 } else {
1269 /* else just seek until the drive returns exception "No Data" */
1270 stat = 0;
1271 while ((stat==0) && (!status_eom_detected)) {
1272 stat = do_qic_cmd(QCMD_RD_FM, TIM_F); /***** should use MTFSFM here???? ******/
1274 if (tperror.exs & TP_NDT)
1275 return 0;
1277 return stat;
1279 case MTERASE:
1280 tpqputs(TPQD_IOCTLS, "MTERASE -- ERASE TAPE !");
1281 if ((tperror.exs & TP_ST0) && (tperror.exs & TP_WRP)) {
1282 tpqputs(TPQD_ALWAYS, "Cartridge is write-protected.");
1283 return -EACCES;
1284 } else {
1285 time_t t = jiffies;
1287 /* Plain GNU mt(1) 2.2 erases a tape in O_RDONLY. :-( */
1288 if (mode_access==READ)
1289 return -EACCES;
1291 /* give user a few seconds to pull out tape */
1292 while (jiffies - t < 4*HZ)
1293 schedule();
1296 /* don't bother writing filemark first */
1297 status_eom_detected = status_eof_detected = NO;
1298 return do_qic_cmd(QCMD_ERASE, TIM_R);
1300 case MTRAS1:
1301 if (TP_HAVE_RAS1) {
1302 tpqputs(TPQD_IOCTLS, "MTRAS1: non-destructive self test");
1303 stat = do_qic_cmd(QCMD_SELF_TST1, TIM_R);
1304 if (stat != 0) {
1305 tpqputs(TPQD_ALWAYS, "RAS1 failed");
1306 return stat;
1308 return (tp_sense(0)==TE_OK)? 0 : -EIO; /* get_ext_status3(); */
1310 tpqputs(TPQD_IOCTLS, "RAS1 not supported");
1311 return -ENXIO;
1313 case MTRAS2:
1314 if (TP_HAVE_RAS2) {
1315 tpqputs(TPQD_IOCTLS, "MTRAS2: destructive self test");
1316 stat = do_qic_cmd(QCMD_SELF_TST2, TIM_R);
1317 if (stat != 0) {
1318 tpqputs(TPQD_ALWAYS, "RAS2 failed");
1319 return stat;
1321 return (tp_sense(0)==TE_OK)? 0 : -EIO; /* get_ext_status3(); */
1323 tpqputs(TPQD_IOCTLS, "RAS2 not supported");
1324 return -ENXIO;
1326 case MTSEEK:
1327 if (TP_HAVE_SEEK && (QIC02_TAPE_IFC==ARCHIVE)) {
1328 tpqputs(TPQD_IOCTLS, "MTSEEK seeking block");
1329 if ((mode_access==WRITE) && status_bytes_wr)
1330 return -EACCES;
1331 /* NOTE: address (24 bits) is in seek_addr_buf[] */
1332 return do_qic_cmd(AR_QCMDV_SEEK_BLK, TIM_F);
1334 else
1335 return -ENOTTY;
1337 default:
1338 return -ENOTTY;
1340 } /* do_ioctl_cmd */
1343 /* dma_transfer(): This routine is called for every 512 bytes to be read
1344 * from/written to the tape controller. Speed is important here!
1345 * (There must be enough time left for the hd controller!)
1346 * When other devices use DMA they must ensure they use un-interruptible
1347 * double byte accesses to the DMA controller. Floppy.c is ok.
1348 * Must have interrupts disabled when this function is invoked,
1349 * otherwise, the double-byte transfers to the DMA controller will not
1350 * be atomic. That could lead to nasty problems when they are interrupted
1351 * by other DMA interrupt-routines.
1353 * This routine merely does the least possible to keep
1354 * the transfers going:
1355 * - set the DMA count register for the next 512 bytes
1356 * - adjust the DMA address and page registers
1357 * - adjust the timeout
1358 * - tell the tape controller to start transferring
1359 * We assume the dma address and mode are, and remain, valid.
1361 static inline void dma_transfer(void)
1364 if (QIC02_TAPE_IFC == WANGTEK) /* or EVEREX */
1365 outb_p(WT_CTL_ONLINE, QIC02_CTL_PORT); /* back to normal */
1366 else if (QIC02_TAPE_IFC == ARCHIVE)
1367 outb_p(0, AR_RESET_DMA_PORT);
1368 else /* QIC02_TAPE_IFC == MOUNTAIN */
1369 outb_p(ctlbits, QIC02_CTL_PORT);
1372 clear_dma_ff(QIC02_TAPE_DMA);
1373 set_dma_mode(QIC02_TAPE_DMA, dma_mode);
1374 set_dma_addr(QIC02_TAPE_DMA, buffaddr+dma_bytes_done); /* full address */
1375 set_dma_count(QIC02_TAPE_DMA, TAPE_BLKSIZE);
1377 /* start tape DMA controller */
1378 if (QIC02_TAPE_IFC == WANGTEK) /* or EVEREX */
1379 outb_p(WT_CTL_DMA | WT_CTL_ONLINE, QIC02_CTL_PORT); /* trigger DMA transfer */
1381 else if (QIC02_TAPE_IFC == ARCHIVE) {
1382 outb_p(AR_CTL_IEN | AR_CTL_DNIEN, QIC02_CTL_PORT); /* enable interrupts again */
1383 outb_p(0, AR_START_DMA_PORT); /* start DMA transfer */
1384 /* In dma_end() AR_RESET_DMA_PORT is written too. */
1386 } else /* QIC02_TAPE_IFC == MOUNTAIN */ {
1387 inb(MTN_R_DESELECT_DMA_PORT);
1388 outb_p(ctlbits | (MTN_CTL_EXC_IEN | MTN_CTL_DNIEN), QIC02_CTL_PORT);
1389 outb_p(0, MTN_W_SELECT_DMA_PORT); /* start DMA transfer */
1390 if (dma_mode == DMA_MODE_WRITE)
1391 outb_p(0, MTN_W_DMA_WRITE_PORT); /* start DMA transfer */
1394 /* start computer DMA controller */
1395 enable_dma(QIC02_TAPE_DMA);
1396 /* block transfer should start now, jumping to the
1397 * interrupt routine when done or an exception was detected.
1399 } /* dma_transfer */
1402 /* start_dma() sets a DMA transfer up between the tape controller and
1403 * the kernel qic02_tape_buf buffer.
1404 * Normally bytes_todo==dma_bytes_done at the end of a DMA transfer. If not,
1405 * a filemark was read, or an attempt to write beyond the End Of Tape
1406 * was made. [Or some other bad thing happened.]
1407 * Must do a sense() before returning error.
1409 static int start_dma(short mode, unsigned long bytes_todo)
1410 /* assume 'bytes_todo'>0 */
1412 int stat;
1414 tpqputs(TPQD_DEBUG, "start_dma() enter");
1415 TPQDEB({printk(TPQIC02_NAME ": doing_read==%d, doing_write==%d\n", doing_read, doing_write);})
1417 dma_bytes_done = 0;
1418 dma_bytes_todo = bytes_todo;
1419 status_error = NO;
1420 /* dma_mode!=0 indicates that the dma controller is in use */
1421 dma_mode = (mode == WRITE)? DMA_MODE_WRITE : DMA_MODE_READ;
1423 /* Only give READ/WRITE DATA command to tape drive if we haven't
1424 * done that already. Otherwise the drive will rewind to the beginning
1425 * of the current file on tape. Any QIC command given other than
1426 * R/W FM will break the read/write transfer cycle.
1427 * do_qic_cmd() will terminate doing_{read,write}
1429 if ((doing_read == NO) && (doing_write == NO)) {
1430 /* First, we have to clear the status -- maybe remove TP_FIL???
1433 #if 0
1434 /* Next dummy get status is to make sure CNI is valid,
1435 since we're only just starting a read/write it doesn't
1436 matter some exceptions are cleared by reading the status;
1437 we're only interested in CNI and WRP. -Eddy */
1438 get_status(&tperror);
1439 #else
1440 /* TP_CNI should now be handled in open(). -Hennus */
1441 #endif
1443 stat = tp_sense(((mode == WRITE)? 0 : TP_WRP) | TP_BOM | TP_FIL);
1444 if (stat != TE_OK)
1445 return stat;
1447 #if OBSOLETE
1448 /************* not needed iff rd_status() would wait for ready!!!!!! **********/
1449 if (wait_for_ready(TIM_S) != TE_OK) { /*** not sure this is needed ***/
1450 tpqputs(TPQD_ALWAYS, "wait_for_ready failed in start_dma");
1451 return -EIO;
1453 #endif
1455 if (QIC02_TAPE_IFC == MOUNTAIN) {
1456 /* Set control bits to select ONLINE during command */
1457 ctlbits |= MTN_QIC02_CTL_ONLINE;
1460 /* Tell the controller the data direction */
1462 /* r/w, timeout medium, check exceptions, sets status_cmd_pending. */
1463 stat = send_qic02_cmd((mode == WRITE)? QCMD_WRT_DATA : QCMD_RD_DATA, TIM_M, 0);
1464 if (stat!=TE_OK) {
1465 printk(TPQIC02_NAME ": start_dma: init %s failed\n",
1466 (mode == WRITE)? "write" : "read");
1467 (void) tp_sense(0);
1468 return stat;
1471 /* Do this last, because sense() will clear the doing_{read,write}
1472 * flags, causing trouble next time around.
1474 if (wait_for_ready(TIM_M) != TE_OK)
1475 return -EIO;
1476 switch (mode) {
1477 case READ:
1478 doing_read = YES;
1479 break;
1480 case WRITE:
1481 doing_write = YES;
1482 break;
1483 default:
1484 printk(TPQIC02_NAME ": requested unknown mode %d\n", mode);
1485 panic(TPQIC02_NAME ": invalid mode in start_dma()");
1488 } else if (is_exception()) {
1489 /* This is for Archive drives, to handle reads with 0 bytes
1490 * left for the last read request.
1492 * ******** this also affects EOF/EOT handling! ************
1494 tpqputs(TPQD_ALWAYS, "detected exception in start_dma() while transfer in progress");
1495 status_error = YES;
1496 return TE_END;
1500 status_expect_int = YES;
1502 /* This assumes tape is already positioned, but these
1503 * semi-'intelligent' drives are unpredictable...
1505 TIMERON(TIM_M*2);
1507 /* initiate first data block read from/write to the tape controller */
1509 cli();
1510 dma_transfer();
1511 sti();
1513 TPQPUTS("start_dma() end");
1514 return TE_OK;
1515 } /* start_dma */
1518 /* This cleans up after the dma transfer has completed
1519 * (or failed). If an exception occurred, a sense()
1520 * must be done. If the exception was caused by a FM,
1521 * sense() will set `status_eof_detected' and
1522 * `status_eom_detected', as required.
1524 static void end_dma(unsigned long * bytes_done)
1526 int stat = TE_OK;
1528 TIMEROFF;
1530 TPQPUTS("end_dma() enter");
1532 disable_dma(QIC02_TAPE_DMA);
1533 clear_dma_ff(QIC02_TAPE_DMA);
1535 if (QIC02_TAPE_IFC == WANGTEK) /* or EVEREX */
1536 outb_p(WT_CTL_ONLINE, QIC02_CTL_PORT); /* back to normal */
1537 else if (QIC02_TAPE_IFC == ARCHIVE)
1538 outb_p(0, AR_RESET_DMA_PORT);
1539 else /* QIC02_TAPE_IFC == MOUNTAIN */ {
1540 /* Clear control bits, de-select ONLINE during tp_sense */
1541 ctlbits &= ~MTN_QIC02_CTL_ONLINE;
1544 stat = wait_for_ready(TIM_M);
1545 if (status_error || (stat!=TE_OK)) {
1546 tpqputs(TPQD_DMAX, "DMA transfer exception");
1547 stat = tp_sense((dma_mode==READ)? TP_WRP : 0);
1548 /* no return here -- got to clean up first! */
1549 } else /* if (QIC02_TAPE_IFC == MOUNTAIN) */ {
1550 outb_p(ctlbits, QIC02_CTL_PORT);
1553 if (QIC02_TAPE_IFC == MOUNTAIN)
1554 inb(MTN_R_DESELECT_DMA_PORT);
1556 /* take the tape controller offline */
1558 /* finish off DMA stuff */
1561 dma_mode = 0;
1562 /* Note: The drive is left on-line, ready for the next
1563 * data transfer.
1564 * If the next command to the drive does not continue
1565 * the pending cycle, it must do 2 sense()s first.
1568 *bytes_done = dma_bytes_done;
1569 status_expect_int = NO;
1570 ioctl_status.mt_blkno += (dma_bytes_done / TAPE_BLKSIZE);
1572 TPQPUTS("end_dma() exit");
1573 /*** could return stat here ***/
1574 } /* end_dma */
1576 /*********** Below are the (public) OS-interface procedures ***********/
1579 /* qic02_tape_times_out() is called when a DMA transfer doesn't complete
1580 * quickly enough. Usually this means there is something seriously wrong
1581 * with the hardware/software, but it could just be that the controller
1582 * has decided to do a long rewind, just when I didn't expect it.
1583 * Just try again.
1585 static void qic02_tape_times_out(void)
1587 printk("time-out in %s driver\n", TPQIC02_NAME);
1588 if ((status_cmd_pending>0) || dma_mode) {
1589 /* takes tooo long, shut it down */
1590 status_dead = YES;
1591 status_cmd_pending = 0;
1592 status_timer_on = NO;
1593 status_expect_int = NO;
1594 status_error = YES;
1595 if (dma_mode) {
1596 dma_mode = 0; /* signal end to read/write routine */
1597 wake_up(&qic02_tape_transfer);
1600 } /* qic02_tape_times_out */
1603 * Interrupt handling:
1605 * 1) Interrupt is generated iff at the end of
1606 * a 512-DMA-block transfer.
1607 * 2) EXCEPTION is not raised unless something
1608 * is wrong or EOT/FM is detected.
1609 * 3) FM EXCEPTION is set *after* the last byte has
1610 * been transferred by DMA. By the time the interrupt
1611 * is handled, the EXCEPTION may already be set.
1613 * So,
1614 * 1) On EXCEPTION, assume data has been transferred, so
1615 * continue as usual, but set a flag to indicate the
1616 * exception was detected.
1617 * Do a sense status when the flag is found set.
1618 * 2) Do not attempt to continue a transfer after an exception.
1619 * [??? What about marginal blocks???????]
1623 /* qic02_tape_interrupt() is called when the tape controller completes
1624 * a DMA transfer.
1625 * We are not allowed to sleep here!
1627 * Check if the transfer was successful, check if we need to transfer
1628 * more. If the buffer contains enough data/is empty enough, signal the
1629 * read/write() thread to copy to/from user space.
1630 * When we are finished, set flags to indicate end, disable timer.
1631 * NOTE: This *must* be fast!
1633 static void qic02_tape_interrupt(int irq, void *dev_id, struct pt_regs *regs)
1635 int stat, r, i;
1637 TIMEROFF;
1639 if (status_expect_int) {
1640 #ifdef WANT_EXTRA_FULL_DEBUGGING
1641 if (TP_DIAGS(current_tape_dev))
1642 printk("@");
1643 #endif
1644 stat = inb(QIC02_STAT_PORT); /* Knock, knock */
1645 if (QIC02_TAPE_IFC == ARCHIVE) { /* "Who's there?" */
1646 if (((stat & (AR_STAT_DMADONE)) == 0) &&
1647 ((stat & (QIC02_STAT_EXCEPTION)) != 0)) {
1648 TIMERCONT;
1649 return; /* "Linux with IRQ sharing" */
1653 if ((stat & QIC02_STAT_EXCEPTION) == 0) { /* exception occurred */
1654 /* Possible causes for an exception during a transfer:
1655 * - during a write-cycle: end of tape (EW) hole detected.
1656 * - during a read-cycle: filemark or EOD detected.
1657 * - something went wrong
1658 * So don't continue with the next block.
1660 tpqputs(TPQD_ALWAYS, "isr: exception on tape controller");
1661 printk(" status %02x\n", stat);
1662 status_error = TE_EX;
1664 dma_bytes_done += TAPE_BLKSIZE;
1666 dma_mode = 0; /* wake up rw() */
1667 status_expect_int = NO;
1668 wake_up(&qic02_tape_transfer);
1669 return;
1671 /* return if tape controller not ready, or
1672 * if dma channel hasn't finished last byte yet.
1674 r = 0;
1676 /* Skip next ready check for Archive controller because
1677 * it may be busy reading ahead. Weird. --hhb
1679 if (QIC02_TAPE_IFC == WANGTEK) /* I think this is a drive-dependency, not IFC -- hhb */
1680 if (stat & QIC02_STAT_READY) { /* not ready */
1681 tpqputs(TPQD_ALWAYS, "isr: ? Tape controller not ready");
1682 r = 1;
1685 if ( (i = get_dma_residue(QIC02_TAPE_DMA)) != 0 ) {
1686 printk(TPQIC02_NAME ": dma_residue == %x !!!\n", i);
1687 r = 1; /* big trouble, but can't do much about it... */
1690 if (r)
1691 return;
1693 /* finish DMA cycle */
1695 /* no errors detected, continue */
1696 dma_bytes_done += TAPE_BLKSIZE;
1697 if (dma_bytes_done >= dma_bytes_todo) {
1698 /* finished! Wakeup rw() */
1699 dma_mode = 0;
1700 status_expect_int = NO;
1701 TPQPUTS("isr: dma_bytes_done");
1702 wake_up(&qic02_tape_transfer);
1703 } else {
1704 /* start next transfer, account for track-switching time */
1705 timer_table[QIC02_TAPE_TIMER].expires = jiffies + 6*HZ;
1706 dma_transfer();
1708 } else {
1709 printk(TPQIC02_NAME ": Unexpected interrupt, stat == %x\n",
1710 inb(QIC02_STAT_PORT));
1712 } /* qic02_tape_interrupt */
1715 static long long qic02_tape_lseek(struct file * file, long long offset, int origin)
1717 return -EINVAL; /* not supported */
1718 } /* qic02_tape_lseek */
1721 /* read/write routines:
1722 * This code copies between a kernel buffer and a user buffer. The
1723 * actual data transfer is done using DMA and interrupts. Time-outs
1724 * are also used.
1726 * When a filemark is read, we return '0 bytes read' and continue with the
1727 * next file after that.
1728 * When EOM is read, we return '0 bytes read' twice.
1729 * When the EOT marker is detected on writes, '0 bytes read' should be
1730 * returned twice. If user program does a MTNOP after that, 2 additional
1731 * blocks may be written. ------- FIXME: Implement this correctly *************************************************
1733 * Only read/writes in multiples of 512 bytes are accepted.
1734 * When no bytes are available, we sleep() until they are. The controller will
1735 * generate an interrupt, and we (should) get a wake_up() call.
1737 * Simple buffering is used. User program should ensure that a large enough
1738 * buffer is used. Usually the drive does some buffering as well (something
1739 * like 4k or so).
1741 * Scott S. Bertilson suggested to continue filling the user buffer, rather
1742 * than waste time on a context switch, when the kernel buffer fills up.
1746 * Problem: tar(1) doesn't always read the entire file. Sometimes the entire file
1747 * has been read, but the EOF token is never returned to tar(1), simply because
1748 * tar(1) knows it has already read all of the data it needs. So we must use
1749 * open/release to reset the `reported_read_eof' flag. If we don't, the next read
1750 * request would return the EOF flag for the previous file.
1753 static ssize_t qic02_tape_read(struct file * filp, char * buf, size_t count, loff_t *ppos)
1755 int err;
1756 kdev_t dev = filp->f_dentry->d_inode->i_rdev;
1757 unsigned short flags = filp->f_flags;
1758 unsigned long bytes_todo, bytes_done, total_bytes_done = 0;
1759 int stat;
1761 if (status_zombie==YES)
1763 tpqputs(TPQD_ALWAYS, "configs not set");
1764 return -ENXIO;
1767 if (TP_DIAGS(current_tape_dev))
1768 /* can't print a ``long long'' (for filp->f_pos), so chop it */
1769 printk(TPQIC02_NAME ": request READ, minor=%x, buf=%p, count=%lx"
1770 ", pos=%lx, flags=%x\n",
1771 MINOR(dev), buf, (long) count,
1772 (unsigned long) filp->f_pos, flags);
1774 if (count % TAPE_BLKSIZE) /* Only allow mod 512 bytes at a time. */
1776 tpqputs(TPQD_BLKSZ, "Wrong block size");
1777 return -EINVAL;
1780 /* Just assume everything is ok. Controller will scream if not. */
1782 if (status_bytes_wr) /* Once written, no more reads, 'till after WFM. */
1784 return -EACCES;
1787 /* This is rather ugly because it has to implement a finite state
1788 * machine in order to handle the EOF situations properly.
1790 while ((signed)count>=0)
1792 bytes_done = 0;
1793 /* see how much fits in the kernel buffer */
1794 bytes_todo = TPQBUF_SIZE;
1795 if (bytes_todo>count)
1797 bytes_todo = count;
1800 /* Must ensure that user program sees exactly one EOF token (==0) */
1801 if (return_read_eof==YES)
1803 if (TPQDBG(DEBUG))
1805 printk("read: return_read_eof==%d, reported_read_eof==%d, total_bytes_done==%lu\n", return_read_eof, reported_read_eof, total_bytes_done);
1808 if (reported_read_eof==NO)
1810 /* have not yet returned EOF to user program */
1811 if (total_bytes_done>0)
1813 return total_bytes_done; /* next time return EOF */
1815 else
1817 reported_read_eof = YES; /* move on next time */
1818 return 0; /* return EOF */
1821 else
1823 /* Application program has already received EOF
1824 * (above), now continue with next file on tape,
1825 * if possible.
1826 * When the FM is reached, EXCEPTION is set,
1827 * causing a sense(). Subsequent read/writes will
1828 * continue after the FM.
1830 /*********** ?????????? this should check for (EOD|NDT), not EOM, 'cause we can read past EW: ************/
1831 if (status_eom_detected)
1833 /* If EOM, nothing left to read, so keep returning EOFs.
1834 *** should probably set some flag to avoid clearing
1835 *** status_eom_detected through ioctls or something
1837 return 0;
1839 else
1841 /* just eof, there may be more files ahead... */
1842 return_read_eof = NO;
1843 reported_read_eof = NO;
1844 status_eof_detected = NO; /* reset this too */
1845 /*fall through*/
1850 /*****************************/
1851 if (bytes_todo==0)
1853 return total_bytes_done;
1856 if (bytes_todo>0)
1858 /* start reading data */
1859 if (is_exception()) /****************************************/
1861 tpqputs(TPQD_DMAX, "is_exception() before start_dma()!");
1864 /******************************************************************
1865 ***** if start_dma() fails because the head is positioned 0 bytes
1866 ***** before the FM, (causing EXCEPTION to be set) return_read_eof should
1867 ***** be set to YES, and we should return total_bytes_done, rather than -ENXIO.
1868 ***** The app should recognize this as an EOF condition.
1869 ***************************************************************************/
1870 stat = start_dma(READ, bytes_todo);
1871 if (stat == TE_OK)
1873 /* Wait for transfer to complete, interrupt should wake us */
1874 while (dma_mode != 0)
1876 sleep_on(&qic02_tape_transfer);
1878 if (status_error)
1880 return_read_eof = YES;
1884 else if (stat != TE_END)
1886 /* should do sense() on error here */
1887 #if 0
1888 return -ENXIO;
1889 #else
1890 printk("Trouble: stat==%02x\n", stat);
1891 return_read_eof = YES;
1892 /*************** check EOF/EOT handling!!!!!! **/
1893 #endif
1895 end_dma(&bytes_done);
1896 if (bytes_done>bytes_todo)
1898 tpqputs(TPQD_ALWAYS, "read: Oops, read more bytes than requested");
1899 return -EIO;
1901 /* copy buffer to user-space in one go */
1902 if (bytes_done>0)
1904 err = copy_to_user( (void *) buf, (void *) bus_to_virt(buffaddr), bytes_done);
1905 if (err)
1907 return -EFAULT;
1910 #if 1
1911 /* Checks Ton's patch below */
1912 if ((return_read_eof == NO) && (status_eof_detected == YES))
1914 printk(TPQIC02_NAME ": read(): return_read_eof=%d, status_eof_detected=YES. return_read_eof:=YES\n", return_read_eof);
1916 #endif
1917 if ((bytes_todo != bytes_done) || (status_eof_detected == YES))
1919 /* EOF or EOM detected. return EOF next time. */
1920 return_read_eof = YES;
1923 } /* else: ignore read request for 0 bytes */
1925 if (bytes_done>0)
1927 status_bytes_rd = YES;
1928 buf += bytes_done;
1929 *ppos += bytes_done;
1930 total_bytes_done += bytes_done;
1931 count -= bytes_done;
1934 tpqputs(TPQD_ALWAYS, "read request for <0 bytes");
1935 return -EINVAL;
1936 } /* qic02_tape_read */
1940 /* The drive detects near-EOT by means of the holes in the tape.
1941 * When the holes are detected, there is some space left. The drive
1942 * reports this as a TP_EOM exception. After clearing the exception,
1943 * the drive should accept two extra blocks.
1945 * It seems there are some archiver programs that would like to use the
1946 * extra space for writing a continuation marker. The driver should return
1947 * end-of-file to the user program on writes, when the holes are detected.
1948 * If the user-program wants to use the extra space, it should use the
1949 * MTNOP ioctl() to get the generic status register and may then continue
1950 * writing (max 1kB). ----------- doesn't work yet...............
1952 * EOF behaviour on writes:
1953 * If there is enough room, write all of the data.
1954 * If there is insufficient room, write as much as will fit and
1955 * return the amount written. If the requested amount differs from the
1956 * written amount, the application program should recognize that as the
1957 * end of file. Subsequent writes will return -ENOSPC.
1958 * Unless the minor bits specify a rewind-on-close, the tape will not
1959 * be rewound when it is full. The user-program should do that, if desired.
1960 * If the driver were to do that automatically, a user-program could be
1961 * confused about the EOT/BOT condition after re-opening the tape device.
1963 * Multiple volume support: Tar closes the tape device before prompting for
1964 * the next tape. The user may then insert a new tape and tar will open the
1965 * tape device again. The driver will detect an exception status in (No Cartridge)
1966 * and force a rewind. After that tar may continue writing.
1968 static ssize_t qic02_tape_write( struct file * filp, const char * buf,
1969 size_t count, loff_t *ppos)
1971 int err;
1972 kdev_t dev = filp->f_dentry->d_inode->i_rdev;
1973 unsigned short flags = filp->f_flags;
1974 unsigned long bytes_todo, bytes_done, total_bytes_done = 0;
1976 if (status_zombie==YES)
1978 tpqputs(TPQD_ALWAYS, "configs not set");
1979 return -ENXIO;
1982 if (TP_DIAGS(current_tape_dev))
1984 /* can't print a ``long long'' (for filp->f_pos), so chop it */
1985 printk(TPQIC02_NAME ": request WRITE, minor=%x, buf=%p"
1986 ", count=%lx, pos=%lx, flags=%x\n",
1987 MINOR(dev), buf,
1988 (long) count, (unsigned long) filp->f_pos, flags);
1991 if (count % TAPE_BLKSIZE) /* only allow mod 512 bytes at a time */
1993 tpqputs(TPQD_BLKSZ, "Wrong block size");
1994 return -EINVAL;
1997 if (mode_access==READ)
1999 tpqputs(TPQD_ALWAYS, "Not in write mode");
2000 return -EACCES;
2003 /* open() does a sense() and we can assume the tape isn't changed
2004 * between open() and release(), so the tperror.exs bits will still
2005 * be valid.
2007 if ((tperror.exs & TP_ST0) && (tperror.exs & TP_WRP))
2009 tpqputs(TPQD_ALWAYS, "Cartridge is write-protected.");
2010 return -EACCES; /* don't even try when write protected */
2013 if (doing_read == YES)
2015 terminate_read(0);
2018 while ((signed)count>=0)
2020 /* see how much fits in the kernel buffer */
2021 bytes_done = 0;
2022 bytes_todo = TPQBUF_SIZE;
2023 if (bytes_todo>count)
2025 bytes_todo = count;
2028 if (return_write_eof == YES)
2030 /* return_write_eof should be reset on reverse tape movements. */
2032 if (reported_write_eof==NO)
2034 if (bytes_todo>0)
2036 tpqputs(TPQD_ALWAYS, "partial write");
2037 /* partial write signals EOF to user program */
2039 reported_write_eof = YES;
2040 return total_bytes_done;
2042 else
2044 return -ENOSPC; /* return error */
2048 /* Quit when done. */
2049 if (bytes_todo==0)
2051 return total_bytes_done;
2054 /* copy from user to DMA buffer and initiate transfer. */
2055 if (bytes_todo>0)
2057 err = copy_from_user( (void *) bus_to_virt(buffaddr), (const void *) buf, bytes_todo);
2058 if (err)
2060 return -EFAULT;
2063 /****************** similar problem with read() at FM could happen here at EOT.
2064 ******************/
2066 /***** if at EOT, 0 bytes can be written. start_dma() will
2067 ***** fail and write() will return ENXIO error
2068 *****/
2069 if (start_dma(WRITE, bytes_todo) != TE_OK)
2071 tpqputs(TPQD_ALWAYS, "write: start_dma() failed");
2072 /* should do sense() on error here */
2073 return -ENXIO; /*********** FIXTHIS **************/
2076 /* Wait for write to complete, interrupt should wake us. */
2077 while ((status_error == 0) && (dma_mode != 0))
2079 sleep_on(&qic02_tape_transfer);
2082 end_dma(&bytes_done);
2083 if (bytes_done>bytes_todo)
2085 tpqputs(TPQD_ALWAYS, "write: Oops, wrote more bytes than requested");
2086 return -EIO;
2088 /* If the dma-transfer was aborted because of an exception,
2089 * status_error will have been set in the interrupt handler.
2090 * Then end_dma() will do a sense().
2091 * If the exception was EXC_EOM, the EW-hole was encountered
2092 * and two more blocks could be written. For the time being we'll
2093 * just consider this to be the EOT.
2094 * Otherwise, something Bad happened, such as the maximum number
2095 * of block-rewrites was exceeded. [e.g. A very bad spot on tape was
2096 * encountered. Normally short dropouts are compensated for by
2097 * rewriting the block in error, up to 16 times. I'm not sure
2098 * QIC-24 drives can do this.]
2100 if (status_error)
2102 if (status_eom_detected == YES)
2104 tpqputs(TPQD_ALWAYS, "write: EW detected");
2105 return_write_eof = YES;
2107 else
2109 /* probably EXC_RWA */
2110 tpqputs(TPQD_ALWAYS, "write: dma: error in writing");
2111 return -EIO;
2114 if (bytes_todo != bytes_done)
2116 /* EOF or EOM detected. return EOT next time. */
2117 return_write_eof = YES;
2120 /* else: ignore write request for 0 bytes. */
2122 if (bytes_done>0)
2124 status_bytes_wr = YES;
2125 buf += bytes_done;
2126 *ppos += bytes_done;
2127 total_bytes_done += bytes_done;
2128 count -= bytes_done;
2132 tpqputs(TPQD_ALWAYS, "write request for <0 bytes");
2133 if (TPQDBG(DEBUG))
2135 printk(TPQIC02_NAME ": status_bytes_wr %x, buf %p"
2136 ", total_bytes_done %lx, count %lx\n",
2137 status_bytes_wr, buf, total_bytes_done, (long) count);
2139 return -EINVAL;
2140 } /* qic02_tape_write */
2144 /* qic02_tape_open()
2145 * We allow the device to be opened, even if it is marked 'dead' because
2146 * we want to be able to reset the tape device without rebooting.
2147 * Only one open tape file at a time, except when minor=255.
2148 * Minor 255 is only allowed for resetting and always returns <0.
2150 * The density command is only allowed when TP_BOM is set. Thus, remember
2151 * the most recently used minor bits. When they are different from the
2152 * remembered values, rewind the tape and set the required density.
2153 * Don't rewind if the minor bits specify density 0.
2156 static int qic02_tape_open(struct inode * inode, struct file * filp)
2158 static int qic02_tape_open_no_use_count(struct inode *, struct file *);
2159 int open_error;
2161 open_error = qic02_tape_open_no_use_count(inode, filp);
2162 if (!open_error)
2164 MOD_INC_USE_COUNT;
2166 return open_error;
2169 static int qic02_tape_open_no_use_count(struct inode * inode, struct file * filp)
2171 kdev_t dev = inode->i_rdev;
2172 unsigned short flags = filp->f_flags;
2173 unsigned short dens = 0;
2174 int s;
2177 if (TP_DIAGS(dev))
2179 printk("qic02_tape_open: dev=%s, flags=%x ",
2180 kdevname(dev), flags);
2183 if (MINOR(dev)==255) /* special case for resetting */
2185 if (suser())
2187 return (tape_reset(1)==TE_OK) ? -EAGAIN : -ENXIO;
2189 else
2191 return -EPERM;
2195 if (status_dead==YES)
2197 /* Allow `mt reset' ioctl() even when already open()ed. */
2198 return 0;
2201 /* Only one at a time from here on... */
2202 if (filp->f_count>1) /* filp->f_count==1 for the first open() */
2204 return -EBUSY;
2207 if (status_zombie==YES)
2209 /* no irq/dma/port stuff allocated yet, no reset done
2210 * yet, so return until MTSETCONFIG has been done.
2212 return 0;
2215 status_bytes_rd = NO;
2216 status_bytes_wr = NO;
2218 return_read_eof = NO; /********????????????????*****/
2219 return_write_eof = (status_eot_detected)? YES : NO;
2221 /* Clear this in case user app close()d before reading EOF token */
2222 status_eof_detected = NO;
2224 reported_read_eof = NO;
2225 reported_write_eof = NO;
2228 switch (flags & O_ACCMODE)
2230 case O_RDONLY:
2231 mode_access = READ;
2232 break;
2233 case O_WRONLY: /* Fallthru... Strictly speaking this is not correct... */
2234 case O_RDWR: /* Reads are allowed as long as nothing is written */
2235 mode_access = WRITE;
2236 break;
2239 /* This is to avoid tape-changed problems (TP_CNI exception).
2241 * Since removing the cartridge will not raise an exception,
2242 * we always do a tp_sense() to make sure we have the proper
2243 * CNI status, the 2150L may need an additional sense.... - Eddy
2245 s = tp_sense(TP_WRP|TP_EOM|TP_BOM|TP_CNI|TP_EOR);
2247 if (s == TE_OK)
2249 /* Try to clear cartridge-changed status for Archive-2150L */
2250 if ((tperror.exs & TP_ST0) && (tperror.exs & TP_CNI))
2252 s = tp_sense(TP_WRP|TP_EOM|TP_BOM|TP_CNI|TP_EOR);
2256 if (s != TE_OK)
2258 tpqputs(TPQD_ALWAYS, "open: sense() failed");
2259 return -EIO;
2262 /* exception bits should be up-to-date now, so check for
2263 * tape presence and exit if absent.
2264 * Even `mt stat' will fail without a tape.
2266 if ((tperror.exs & TP_ST0) && (tperror.exs & TP_CNI))
2268 tpqputs(TPQD_ALWAYS, "No tape present.");
2269 return -EIO;
2272 /* At this point we can assume that a tape is present and
2273 * that it will remain present until release() is called.
2276 /* not allowed to do QCMD_DENS_* unless tape is rewound */
2277 if ((TP_DENS(dev)!=0) && (TP_DENS(current_tape_dev) != TP_DENS(dev)))
2279 /* force rewind if minor bits have changed,
2280 * i.e. user wants to use tape in different format.
2281 * [assuming single drive operation]
2283 if (TP_HAVE_DENS)
2285 tpqputs(TPQD_REWIND, "Density minor bits have changed. Forcing rewind.");
2286 need_rewind = YES;
2289 else
2291 /* density bits still the same, but TP_DIAGS bit
2292 * may have changed.
2294 current_tape_dev = dev;
2297 if (need_rewind == YES) /***************** CHECK THIS!!!!!!!! **********/
2299 s = do_qic_cmd(QCMD_REWIND, TIM_R);
2300 if (s != 0)
2302 tpqputs(TPQD_ALWAYS, "open: rewind failed");
2303 return -EIO;
2308 /* Note: After a reset command, the controller will rewind the tape
2309 * just before performing any tape movement operation! ************ SO SET need_rewind flag!!!!!
2311 if (status_dead==YES)
2313 tpqputs(TPQD_ALWAYS, "open: tape dead, attempting reset");
2314 if (tape_reset(1)!=TE_OK)
2316 return -ENXIO;
2318 else
2320 status_dead = NO;
2321 if (tp_sense(~(TP_ST1|TP_ILL)) != TE_OK)
2323 tpqputs(TPQD_ALWAYS, "open: tp_sense() failed\n");
2324 status_dead = YES; /* try reset next time */
2325 return -EIO;
2330 /* things should be ok, once we get here */
2333 /* set density: only allowed when TP_BOM status bit is set,
2334 * so we must have done a rewind by now. If not, just skip over.
2335 * Only give set density command when minor bits have changed.
2337 if (TP_DENS(current_tape_dev) == TP_DENS(dev) )
2339 return 0;
2342 current_tape_dev = dev;
2343 need_rewind = NO;
2344 if (TP_HAVE_DENS)
2346 dens = TP_DENS(dev);
2349 if (dens < sizeof(format_names)/sizeof(char *))
2351 printk(TPQIC02_NAME ": format: %s%s\n", (dens!=0)? "QIC-" : "", format_names[dens]);
2353 else
2355 tpqputs(TPQD_REWIND, "Wait for retensioning...");
2358 switch (TP_DENS(dev))
2360 case 0: /* Minor 0 is for drives without set-density support */
2361 s = 0;
2362 break;
2363 case 1:
2364 s = do_qic_cmd(QCMD_DENS_11, TIM_S);
2365 break;
2366 case 2:
2367 s = do_qic_cmd(QCMD_DENS_24, TIM_S);
2368 break;
2369 case 3:
2370 s = do_qic_cmd(QCMD_DENS_120, TIM_S);
2371 break;
2372 case 4:
2373 s = do_qic_cmd(QCMD_DENS_150, TIM_S);
2374 break;
2375 case 5:
2376 s = do_qic_cmd(QCMD_DENS_300, TIM_S);
2377 break;
2378 case 6:
2379 s = do_qic_cmd(QCMD_DENS_600, TIM_S);
2380 break;
2381 default: /* otherwise do a retension before anything else */
2382 s = do_qic_cmd(QCMD_RETEN, TIM_R);
2384 if (s != 0)
2386 status_dead = YES; /* force reset */
2387 current_tape_dev = 0; /* earlier 0xff80 */
2388 return -EIO;
2391 return 0;
2392 } /* qic02_tape_open */
2395 static int qic02_tape_release(struct inode * inode, struct file * filp)
2397 kdev_t dev = inode->i_rdev;
2399 if (TP_DIAGS(dev))
2401 printk("qic02_tape_release: dev=%s\n", kdevname(dev));
2404 if (status_zombie==NO) /* don't rewind in zombie mode */
2406 /* Terminate any pending write cycle. Terminating the read-cycle
2407 * is delayed until it is required to do so for a new command.
2409 terminate_write(-1);
2411 if (status_dead==YES)
2413 tpqputs(TPQD_ALWAYS, "release: device dead!?");
2416 /* Rewind only if minor number requires it AND
2417 * read/writes have been done. ************* IS THIS CORRECT??????????
2419 if ((TP_REWCLOSE(dev)) && (status_bytes_rd | status_bytes_wr))
2421 tpqputs(TPQD_REWIND, "release: Doing rewind...");
2422 (void) do_qic_cmd(QCMD_REWIND, TIM_R);
2425 #ifdef MODULE
2426 MOD_DEC_USE_COUNT;
2427 #endif
2428 return 0;
2429 } /* qic02_tape_release */
2432 #ifdef CONFIG_QIC02_DYNCONF
2433 /* Set masks etc. based on the interface card type. */
2434 static int update_ifc_masks(int ifc)
2436 QIC02_TAPE_IFC = ifc;
2438 if ((QIC02_TAPE_IFC == WANGTEK) || (QIC02_TAPE_IFC == EVEREX))
2440 QIC02_STAT_PORT = QIC02_TAPE_PORT;
2441 QIC02_CTL_PORT = QIC02_TAPE_PORT;
2442 QIC02_CMD_PORT = QIC02_TAPE_PORT+1;
2443 QIC02_DATA_PORT = QIC02_TAPE_PORT+1;
2444 QIC02_STAT_READY = WT_QIC02_STAT_READY;
2445 QIC02_STAT_EXCEPTION = WT_QIC02_STAT_EXCEPTION;
2446 QIC02_STAT_MASK = WT_QIC02_STAT_MASK;
2448 QIC02_STAT_RESETMASK = WT_QIC02_STAT_RESETMASK;
2449 QIC02_STAT_RESETVAL = WT_QIC02_STAT_RESETVAL;
2451 QIC02_CTL_RESET = WT_QIC02_CTL_RESET;
2452 QIC02_CTL_REQUEST = WT_QIC02_CTL_REQUEST;
2454 if (QIC02_TAPE_DMA == 3)
2456 WT_CTL_DMA = WT_CTL_DMA3;
2458 else if (QIC02_TAPE_DMA == 1)
2460 WT_CTL_DMA = WT_CTL_DMA1;
2462 else
2464 tpqputs(TPQD_ALWAYS, "Unsupported or incorrect DMA channel");
2465 return -EIO;
2468 if (QIC02_TAPE_IFC == EVEREX)
2470 /* Everex is a special case for Wangtek (actually
2471 * it's the other way 'round, but I saw Wangtek first)
2473 if (QIC02_TAPE_DMA==3)
2475 WT_CTL_DMA = WT_CTL_DMA1;
2478 /* Fixup the kernel copy of the IFC type to that
2479 * we don't have to distinguish between Wangtek and
2480 * and Everex at runtime.
2482 QIC02_TAPE_IFC = WANGTEK;
2485 else if (QIC02_TAPE_IFC == ARCHIVE)
2487 QIC02_STAT_PORT = QIC02_TAPE_PORT+1;
2488 QIC02_CTL_PORT = QIC02_TAPE_PORT+1;
2489 QIC02_CMD_PORT = QIC02_TAPE_PORT;
2490 QIC02_DATA_PORT = QIC02_TAPE_PORT;
2491 QIC02_STAT_READY = AR_QIC02_STAT_READY;
2492 QIC02_STAT_EXCEPTION = AR_QIC02_STAT_EXCEPTION;
2493 QIC02_STAT_MASK = AR_QIC02_STAT_MASK;
2495 QIC02_STAT_RESETMASK = AR_QIC02_STAT_RESETMASK;
2496 QIC02_STAT_RESETVAL = AR_QIC02_STAT_RESETVAL;
2498 QIC02_CTL_RESET = AR_QIC02_CTL_RESET;
2499 QIC02_CTL_REQUEST = AR_QIC02_CTL_REQUEST;
2501 if (QIC02_TAPE_DMA > 3)
2503 tpqputs(TPQD_ALWAYS, "Unsupported or incorrect DMA channel");
2504 return -EIO;
2507 else if (QIC02_TAPE_IFC == MOUNTAIN)
2509 QIC02_STAT_PORT = QIC02_TAPE_PORT+1;
2510 QIC02_CTL_PORT = QIC02_TAPE_PORT+1;
2511 QIC02_CMD_PORT = QIC02_TAPE_PORT;
2512 QIC02_DATA_PORT = QIC02_TAPE_PORT;
2514 QIC02_STAT_READY = MTN_QIC02_STAT_READY;
2515 QIC02_STAT_EXCEPTION = MTN_QIC02_STAT_EXCEPTION;
2516 QIC02_STAT_MASK = MTN_QIC02_STAT_MASK;
2518 QIC02_STAT_RESETMASK = MTN_QIC02_STAT_RESETMASK;
2519 QIC02_STAT_RESETVAL = MTN_QIC02_STAT_RESETVAL;
2521 QIC02_CTL_RESET = MTN_QIC02_CTL_RESET;
2522 QIC02_CTL_REQUEST = MTN_QIC02_CTL_REQUEST;
2524 if (QIC02_TAPE_DMA > 3)
2526 tpqputs(TPQD_ALWAYS, "Unsupported or incorrect DMA channel");
2527 return -EIO;
2530 else
2532 tpqputs(TPQD_ALWAYS, "Invalid interface type");
2533 return -ENXIO;
2535 return qic02_get_resources();
2536 } /* update_ifc_masks */
2537 #endif
2540 /* ioctl allows user programs to rewind the tape and stuff like that */
2541 static int qic02_tape_ioctl(struct inode * inode, struct file * filp,
2542 unsigned int iocmd, unsigned long ioarg)
2544 int error;
2545 int dev_maj = MAJOR(inode->i_rdev);
2546 int c;
2547 struct mtop operation;
2548 unsigned char blk_addr[6];
2549 struct mtpos ioctl_tell;
2552 if (TP_DIAGS(current_tape_dev))
2554 printk(TPQIC02_NAME ": ioctl(%4x, %4x, %4lx)\n", dev_maj, iocmd, ioarg);
2557 if (!inode || !ioarg)
2559 return -EINVAL;
2562 /* check iocmd first */
2564 if (dev_maj != QIC02_TAPE_MAJOR)
2566 printk(TPQIC02_NAME ": Oops! Wrong device?\n");
2567 /* A panic() would be appropriate here */
2568 return -ENODEV;
2571 c = _IOC_NR(iocmd);
2573 #ifdef CONFIG_QIC02_DYNCONF
2574 if (c == _IOC_NR(MTIOCGETCONFIG))
2576 CHECK_IOC_SIZE(mtconfiginfo);
2578 if (copy_to_user((char *) ioarg, (char *) &qic02_tape_dynconf, sizeof(qic02_tape_dynconf)))
2580 return -EFAULT;
2582 return 0;
2585 else if (c == _IOC_NR(MTIOCSETCONFIG))
2587 /* One should always do a MTIOCGETCONFIG first, then update
2588 * user-settings, then write back with MTIOCSETCONFIG.
2589 * The qic02conf program should re-open() the device before actual
2590 * use, to make sure everything is initialized.
2593 CHECK_IOC_SIZE(mtconfiginfo);
2595 if (!suser())
2597 return -EPERM;
2600 if ((doing_read!=NO) || (doing_write!=NO))
2602 return -EBUSY;
2605 if (status_zombie==NO)
2607 qic02_release_resources(); /* and go zombie */
2610 /* copy struct from user space to kernel space */
2611 if (copy_from_user((char *) &qic02_tape_dynconf, (char *) ioarg, sizeof(qic02_tape_dynconf)))
2613 return -EFAULT;
2615 return update_ifc_masks(qic02_tape_dynconf.ifc_type);
2617 if (status_zombie==YES)
2619 tpqputs(TPQD_ALWAYS, "Configs not set");
2620 return -ENXIO;
2622 #endif
2623 if (c == _IOC_NR(MTIOCTOP))
2625 CHECK_IOC_SIZE(mtop);
2627 /* copy mtop struct from user space to kernel space */
2628 if (copy_from_user((char *) &operation, (char *) ioarg, sizeof(operation)))
2630 return -EFAULT;
2633 /* ---note: mt_count is signed, negative seeks must be
2634 * --- translated to seeks in opposite direction!
2635 * (only needed for Sun-programs, I think.)
2637 /* ---note: MTFSF with count 0 should position the
2638 * --- tape at the beginning of the current file.
2641 if (TP_DIAGS(current_tape_dev))
2643 printk("OP op=%4x, count=%4x\n", operation.mt_op, operation.mt_count);
2646 if (operation.mt_count < 0)
2648 tpqputs(TPQD_ALWAYS, "Warning: negative mt_count ignored");
2651 ioctl_status.mt_resid = operation.mt_count;
2652 if (operation.mt_op == MTSEEK)
2654 if (!TP_HAVE_SEEK)
2656 return -ENOTTY;
2659 seek_addr_buf[0] = (operation.mt_count>>16)&0xff;
2660 seek_addr_buf[1] = (operation.mt_count>>8)&0xff;
2661 seek_addr_buf[2] = (operation.mt_count)&0xff;
2662 if (operation.mt_count>>24)
2664 return -EINVAL;
2666 if ((error = do_ioctl_cmd(operation.mt_op)) != 0)
2668 return error;
2671 ioctl_status.mt_resid = 0;
2673 else
2675 while (operation.mt_count > 0)
2677 operation.mt_count--;
2678 if ((error = do_ioctl_cmd(operation.mt_op)) != 0)
2680 return error;
2683 ioctl_status.mt_resid = operation.mt_count;
2686 return 0;
2689 else if (c == _IOC_NR(MTIOCGET))
2691 if (TP_DIAGS(current_tape_dev))
2693 printk("GET ");
2696 CHECK_IOC_SIZE(mtget);
2698 /* It appears (gmt(1)) that it is normal behaviour to
2699 * first set the status with MTNOP, and then to read
2700 * it out with MTIOCGET
2703 /* copy results to user space */
2704 if (copy_to_user((char *) ioarg, (char *) &ioctl_status, sizeof(ioctl_status)))
2706 return -EFAULT;
2708 return 0;
2710 else if (TP_HAVE_TELL && (c == _IOC_NR(MTIOCPOS)))
2712 if (TP_DIAGS(current_tape_dev))
2714 printk("POS ");
2717 CHECK_IOC_SIZE(mtpos);
2719 tpqputs(TPQD_IOCTLS, "MTTELL reading block address");
2720 if ((doing_read==YES) || (doing_write==YES))
2722 finish_rw(AR_QCMDV_TELL_BLK);
2725 c = rdstatus((char *) blk_addr, sizeof(blk_addr), AR_QCMDV_TELL_BLK);
2726 if (c!=TE_OK)
2728 return -EIO;
2731 ioctl_tell.mt_blkno = (blk_addr[3] << 16) | (blk_addr[4] << 8) | blk_addr[5];
2733 /* copy results to user space */
2734 if (copy_to_user((char *) ioarg, (char *) &ioctl_tell, sizeof(ioctl_tell)))
2736 return -EFAULT;
2738 return 0;
2741 else
2743 return -ENOTTY; /* Other cmds not supported. */
2745 } /* qic02_tape_ioctl */
2749 /* These are (most) of the interface functions: */
2750 static struct file_operations qic02_tape_fops = {
2751 qic02_tape_lseek, /* not allowed */
2752 qic02_tape_read, /* read */
2753 qic02_tape_write, /* write */
2754 NULL, /* readdir not allowed */
2755 NULL, /* poll ??? */
2756 qic02_tape_ioctl, /* ioctl */
2757 NULL, /* mmap not allowed */
2758 qic02_tape_open, /* open */
2759 qic02_tape_release, /* release */
2760 NULL, /* fsync */
2761 NULL, /* fasync */
2762 NULL, /* check_media_change */
2763 NULL /* revalidate */
2767 /* Why is this not is one place? */
2768 /* Pure 2^n version of get_order */
2769 static inline int __get_order(unsigned long size)
2771 int order;
2773 size = (size-1) >> (PAGE_SHIFT-1);
2774 order = -1;
2777 size >>= 1;
2778 order++;
2779 } while (size);
2780 return order;
2784 static void qic02_release_resources(void)
2786 free_irq(QIC02_TAPE_IRQ, NULL);
2787 free_dma(QIC02_TAPE_DMA);
2788 release_region(QIC02_TAPE_PORT, QIC02_TAPE_PORT_RANGE);
2789 if (buffaddr)
2791 free_pages(buffaddr, __get_order(TPQBUF_SIZE));
2793 buffaddr = 0; /* Better to cause a panic than overwite someone else */
2794 status_zombie = YES;
2795 } /* qic02_release_resources */
2798 static int qic02_get_resources(void)
2800 /* First perform some checks. If one of them fails,
2801 * the tape driver will not be registered to the system.
2803 if (QIC02_TAPE_IRQ>16)
2805 tpqputs(TPQD_ALWAYS, "Bogus interrupt number.");
2806 return -ENXIO;
2809 /* for DYNCONF, allocating IO, DMA and IRQ should not be done until
2810 * the config parameters have been set using MTSETCONFIG.
2813 if (check_region(QIC02_TAPE_PORT, QIC02_TAPE_PORT_RANGE))
2815 printk(TPQIC02_NAME ": IO space at 0x%x [%d ports] already reserved\n",
2816 QIC02_TAPE_PORT, QIC02_TAPE_PORT_RANGE);
2817 return -ENXIO;
2820 /* get IRQ */
2821 if (request_irq(QIC02_TAPE_IRQ, qic02_tape_interrupt, SA_INTERRUPT, "QIC-02", NULL))
2823 printk(TPQIC02_NAME ": can't allocate IRQ%d for QIC-02 tape\n",
2824 QIC02_TAPE_IRQ);
2825 return -EBUSY;
2828 /* After IRQ, allocate DMA channel */
2829 if (request_dma(QIC02_TAPE_DMA,"QIC-02"))
2831 printk(TPQIC02_NAME ": can't allocate DMA%d for QIC-02 tape\n",
2832 QIC02_TAPE_DMA);
2833 free_irq(QIC02_TAPE_IRQ, NULL);
2834 return -EBUSY;
2837 /* Grab the IO region. We already made sure it's available. */
2838 request_region(QIC02_TAPE_PORT, QIC02_TAPE_PORT_RANGE, TPQIC02_NAME);
2840 /* Setup the page-address for the dma transfer. */
2842 /*** TODO: does _get_dma_pages() really return the physical address?? ****/
2843 buffaddr = __get_dma_pages(GFP_KERNEL,__get_order(TPQBUF_SIZE));
2845 if (!buffaddr)
2847 qic02_release_resources();
2848 return -EBUSY; /* Not ideal, EAGAIN perhaps? */
2851 memset( (void*) buffaddr, 0, TPQBUF_SIZE );
2853 printk(TPQIC02_NAME ": Settings: IRQ %d, DMA %d, IO 0x%x, IFC %s\n",
2854 QIC02_TAPE_IRQ, QIC02_TAPE_DMA,
2855 ((QIC02_TAPE_IFC==ARCHIVE) || (QIC02_TAPE_IFC==MOUNTAIN))?
2856 QIC02_CMD_PORT : QIC02_STAT_PORT,
2857 (QIC02_TAPE_IFC==MOUNTAIN)? "Mountain" :
2858 ((QIC02_TAPE_IFC==ARCHIVE)? "Archive" : "Wangtek"));
2860 if (tape_reset(0)!=TE_OK || tp_sense(TP_WRP|TP_POR|TP_CNI)!=TE_OK)
2862 /* No drive detected, so vanish */
2863 tpqputs(TPQD_ALWAYS, "No drive detected -- releasing IO/IRQ/DMA.");
2864 status_dead = YES;
2865 qic02_release_resources();
2866 return -EIO;
2869 /* All should be ok now */
2870 status_zombie = NO;
2871 return 0;
2872 } /* qic02_get_resources */
2874 __initfunc(int qic02_tape_init(void))
2876 if (TPSTATSIZE != 6)
2878 printk(TPQIC02_NAME ": internal error: tpstatus struct incorrect!\n");
2879 return -ENODEV;
2881 if ((TPQBUF_SIZE<512) || (TPQBUF_SIZE>=0x10000))
2883 printk(TPQIC02_NAME ": internal error: DMA buffer size out of range\n");
2884 return -ENODEV;
2887 current_tape_dev = MKDEV(QIC02_TAPE_MAJOR, 0);
2889 #ifndef CONFIG_QIC02_DYNCONF
2890 printk(TPQIC02_NAME ": IRQ %d, DMA %d, IO 0x%x, IFC %s, %s, %s\n",
2891 QIC02_TAPE_IRQ, QIC02_TAPE_DMA,
2892 # if QIC02_TAPE_IFC == WANGTEK
2893 QIC02_STAT_PORT, "Wangtek",
2894 # elif QIC02_TAPE_IFC == ARCHIVE
2895 QIC02_CMD_PORT, "Archive",
2896 # elif QIC02_TAPE_IFC == MOUNTAIN
2897 QIC02_CMD_PORT, "Mountain",
2898 # else
2899 # error
2900 # endif
2901 rcs_revision, rcs_date);
2902 if (qic02_get_resources())
2904 return -ENODEV;
2906 #else
2907 printk(TPQIC02_NAME ": Runtime config, %s, %s\n",
2908 rcs_revision, rcs_date);
2909 #endif
2910 printk(TPQIC02_NAME ": DMA buffers: %u blocks\n", NR_BLK_BUF);
2911 /* If we got this far, install driver functions */
2912 if (register_chrdev(QIC02_TAPE_MAJOR, TPQIC02_NAME, &qic02_tape_fops))
2914 printk(TPQIC02_NAME ": Unable to get chrdev major %d\n", QIC02_TAPE_MAJOR);
2915 #ifndef CONFIG_QIC02_DYNCONF
2916 qic02_release_resources();
2917 #endif
2918 return -ENODEV;
2921 /* prepare timer */
2922 TIMEROFF;
2923 timer_table[QIC02_TAPE_TIMER].expires = 0;
2924 timer_table[QIC02_TAPE_TIMER].fn = qic02_tape_times_out;
2926 #ifndef CONFIG_QIC02_DYNCONF
2927 if (tape_reset(0)!=TE_OK || tp_sense(TP_WRP|TP_POR|TP_CNI)!=TE_OK)
2929 /* No drive detected, so vanish */
2930 tpqputs(TPQD_ALWAYS, "No drive detected -- driver going on vacation...");
2931 qic02_release_resources();
2932 status_dead = YES;
2933 return -ENODEV;
2935 else
2937 if (is_exception())
2939 tpqputs(TPQD_ALWAYS, "exception detected\n");
2940 (void) tp_sense(TP_WRP|TP_POR|TP_CNI);
2943 #endif
2945 /* initialize generic status for ioctl requests */
2947 ioctl_status.mt_type = QIC02_TAPE_DRIVE; /* MT_IS* id nr */
2949 ioctl_status.mt_resid = 0; /* ---residual count */
2950 ioctl_status.mt_gstat = 0; /* ---generic status */
2951 ioctl_status.mt_erreg = 0; /* not used */
2952 ioctl_status.mt_fileno = 0; /* number of current file on tape */
2953 ioctl_status.mt_blkno = 0; /* number of current (logical) block */
2955 return 0;
2956 } /* qic02_tape_init */
2958 #ifdef MODULE
2960 void cleanup_module(void)
2962 if (status_zombie == NO)
2964 qic02_release_resources();
2966 unregister_chrdev(QIC02_TAPE_MAJOR, TPQIC02_NAME);
2969 int init_module(void)
2971 int retval;
2972 retval=qic02_tape_init();
2973 # ifdef CONFIG_QIC02_DYNCONF
2974 /* This allows the dynamic config program to setup the card
2975 * by presetting qic02_tape_dynconf via insmod
2977 if (!retval && qic02_tape_dynconf.ifc_type)
2979 retval=update_ifc_masks(qic02_tape_dynconf.ifc_type);
2980 if (retval)
2982 cleanup_module();
2985 # endif
2986 return retval;
2988 #endif