tpqic02.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:86k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

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