107 #define PT_VERSION "1.04"
112 #include <linux/types.h>
125 static int drive0[6] = { 0, 0, 0, -1, -1, -1 };
126 static int drive1[6] = { 0, 0, 0, -1, -1, -1 };
127 static int drive2[6] = { 0, 0, 0, -1, -1, -1 };
128 static int drive3[6] = { 0, 0, 0, -1, -1, -1 };
130 static int (*drives[4])[6] = {&drive0, &drive1, &drive2, &drive3};
139 #define DU (*drives[unit])
143 #include <linux/module.h>
145 #include <linux/fs.h>
147 #include <linux/slab.h>
149 #include <linux/device.h>
150 #include <linux/sched.h>
153 #include <asm/uaccess.h>
165 #define PT_MAX_RETRIES 5
167 #define PT_SPIN_DEL 50
168 #define PT_RESET_TMO 30
169 #define PT_READY_TMO 60
170 #define PT_REWIND_TMO 1200
172 #define PT_SPIN ((1000000/(HZ*PT_SPIN_DEL))*PT_TMO)
174 #define STAT_ERR 0x00001
175 #define STAT_INDEX 0x00002
176 #define STAT_ECC 0x00004
177 #define STAT_DRQ 0x00008
178 #define STAT_SEEK 0x00010
179 #define STAT_WRERR 0x00020
180 #define STAT_READY 0x00040
181 #define STAT_BUSY 0x00080
182 #define STAT_SENSE 0x1f000
184 #define ATAPI_TEST_READY 0x00
185 #define ATAPI_REWIND 0x01
186 #define ATAPI_REQ_SENSE 0x03
187 #define ATAPI_READ_6 0x08
188 #define ATAPI_WRITE_6 0x0a
189 #define ATAPI_WFM 0x10
190 #define ATAPI_IDENTIFY 0x12
191 #define ATAPI_MODE_SENSE 0x1a
192 #define ATAPI_LOG_SENSE 0x4d
196 static long pt_ioctl(
struct file *
file,
unsigned int cmd,
unsigned long arg);
199 size_t count, loff_t * ppos);
200 static ssize_t pt_write(
struct file *filp,
const char __user *
buf,
201 size_t count, loff_t * ppos);
202 static int pt_detect(
void);
207 #define PT_WRITE_OK 2
210 #define PT_READING 16
214 #define PT_BUFSIZE 16384
230 static int pt_identify(
struct pt_unit *tape);
234 static char pt_scratch[512];
242 .unlocked_ioctl = pt_ioctl,
244 .release = pt_release,
249 static struct class *pt_class;
251 static inline int status_reg(
struct pi_adapter *pi)
266 static inline u8 DRIVE(
struct pt_unit *tape)
268 return 0xa0+0x10*tape->
drive;
277 while ((((r = status_reg(pi)) & go) || (stop && (!(r & stop))))
288 printk(
"%s: %s %s: alt=0x%x stat=0x%x err=0x%x"
289 " loop=%d phase=%d\n",
290 tape->
name, fun, msg, r, s, e, j, p);
296 static int pt_command(
struct pt_unit *tape,
char *
cmd,
int dlen,
char *fun)
318 printk(
"%s: %s: command phase error\n", tape->
name, fun);
328 static int pt_completion(
struct pt_unit *tape,
char *
buf,
char *fun)
353 static void pt_req_sense(
struct pt_unit *tape,
int quiet)
355 char rs_cmd[12] = {
ATAPI_REQ_SENSE, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0 };
359 r = pt_command(tape, rs_cmd, 16,
"Request sense");
362 pt_completion(tape, buf,
"Request sense");
367 printk(
"%s: Sense key: %x, ASC: %x, ASQ: %x\n",
368 tape->
name, buf[2] & 0xf, buf[12], buf[13]);
369 tape->
last_sense = (buf[2] & 0xf) | ((buf[12] & 0xff) << 8)
370 | ((buf[13] & 0xff) << 16);
374 static int pt_atapi(
struct pt_unit *tape,
char *cmd,
int dlen,
char *buf,
char *fun)
378 r = pt_command(tape, cmd, dlen, fun);
381 r = pt_completion(tape, buf, fun);
383 pt_req_sense(tape, !fun);
388 static void pt_sleep(
int cs)
393 static int pt_poll_dsc(
struct pt_unit *tape,
int pause,
int tmo,
char *msg)
414 printk(
"%s: %s DSC timeout\n", tape->
name, msg);
416 printk(
"%s: %s stat=0x%x err=0x%x\n", tape->
name, msg, s,
418 pt_req_sense(tape, 0);
424 static void pt_media_access_cmd(
struct pt_unit *tape,
int tmo,
char *cmd,
char *fun)
426 if (pt_command(tape, cmd, 0, fun)) {
427 pt_req_sense(tape, 0);
431 pt_poll_dsc(tape,
HZ, tmo, fun);
434 static void pt_rewind(
struct pt_unit *tape)
436 char rw_cmd[12] = {
ATAPI_REWIND, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
441 static void pt_write_fm(
struct pt_unit *tape)
443 char wm_cmd[12] = {
ATAPI_WFM, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0 };
445 pt_media_access_cmd(tape,
PT_TMO, wm_cmd,
"write filemark");
448 #define DBMSG(msg) ((verbose>1)?(msg):NULL)
450 static int pt_reset(
struct pt_unit *tape)
454 int expect[5] = { 1, 1, 1, 0x14, 0xeb };
460 pt_sleep(20 *
HZ / 1000);
467 for (i = 0; i < 5; i++)
468 flg &= (
read_reg(pi, i + 1) == expect[i]);
471 printk(
"%s: Reset (%d) signature = ", tape->
name, k);
472 for (i = 0; i < 5; i++)
483 static int pt_ready_wait(
struct pt_unit *tape,
int tmo)
485 char tr_cmd[12] = {
ATAPI_TEST_READY, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
491 pt_atapi(tape, tr_cmd, 0,
NULL,
DBMSG(
"test unit ready"));
495 if (!(((p & 0xffff) == 0x0402) || ((p & 0xff) == 6)))
503 static void xs(
char *buf,
char *targ,
int offs,
int len)
509 for (k = 0; k < len; k++)
510 if ((buf[k + offs] != 0x20) || (buf[k + offs] != l))
511 l = targ[j++] = buf[k +
offs];
517 static int xn(
char *buf,
int offs,
int size)
522 for (k = 0; k <
size; k++)
523 v = v * 256 + (buf[k + offs] & 0xff);
527 static int pt_identify(
struct pt_unit *tape)
530 char *
ms[2] = {
"master",
"slave" };
532 char id_cmd[12] = {
ATAPI_IDENTIFY, 0, 0, 0, 36, 0, 0, 0, 0, 0, 0, 0 };
534 {
ATAPI_MODE_SENSE, 0, 0x2a, 0, 36, 0, 0, 0, 0, 0, 0, 0 };
536 {
ATAPI_LOG_SENSE, 0, 0x71, 0, 0, 0, 0, 0, 36, 0, 0, 0 };
539 s = pt_atapi(tape, id_cmd, 36, buf,
"identify");
546 printk(
"%s: Drive %d, unsupported type %d\n",
561 if (!pt_atapi(tape, ms_cmd, 36, buf,
"mode sense")) {
562 if (!(buf[2] & 0x80))
564 tape->
bs = xn(buf, 10, 2);
567 if (!pt_atapi(tape, ls_cmd, 36, buf,
"log sense"))
587 static int pt_probe(
struct pt_unit *tape)
589 if (tape->
drive == -1) {
592 return pt_identify(tape);
595 return pt_identify(tape);
600 static int pt_detect(
void)
603 int specified = 0, found = 0;
609 for (unit = 0; unit <
PT_UNITS; unit++) {
611 tape->
pi = &tape->
pia;
625 if (!pt_probe(tape)) {
632 if (specified == 0) {
634 if (
pi_init(tape->
pi, 1, -1, -1, -1, -1, -1, pt_scratch,
636 if (!pt_probe(tape)) {
647 printk(
"%s: No ATAPI tape drive detected\n",
name);
653 int unit = iminor(inode) & 0x7F;
658 if (unit >= PT_UNITS || (!tape->
present)) {
677 if (!(iminor(inode) & 128))
683 printk(
"%s: buffer allocation failed\n", tape->
name);
697 static long pt_ioctl(
struct file *file,
unsigned int cmd,
unsigned long arg)
735 pt_release(
struct inode *inode,
struct file *file)
757 static ssize_t pt_read(
struct file *filp,
char __user *buf,
size_t count, loff_t * ppos)
761 char rd_cmd[12] = {
ATAPI_READ_6, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
766 if (pt_atapi(tape, rd_cmd, 0,
NULL,
"start read-ahead"))
778 if (!pt_poll_dsc(tape,
HZ / 100,
PT_TMO,
"read"))
784 b = (n - 1 + tape->
bs) / tape->
bs;
789 r = pt_command(tape, rd_cmd, n,
"read");
794 pt_req_sense(tape, 0);
802 DBMSG(
"read DRQ"),
"");
806 pt_req_sense(tape, 0);
822 printk(
"%s: Phase error on read: %d\n", tape->
name,
854 static ssize_t pt_write(
struct file *filp,
const char __user *buf,
size_t count, loff_t * ppos)
858 char wr_cmd[12] = {
ATAPI_WRITE_6, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
867 (tape, wr_cmd, 0,
NULL,
"start buffer-available mode"))
879 if (!pt_poll_dsc(tape,
HZ / 100,
PT_TMO,
"write"))
885 b = (n - 1 + tape->
bs) / tape->
bs;
890 r = pt_command(tape, wr_cmd, n,
"write");
895 pt_req_sense(tape, 0);
905 if (r & STAT_SENSE) {
907 pt_req_sense(tape, 0);
923 printk(
"%s: Phase error on write: %d \n",
954 static int __init pt_init(
void)
969 err = register_chrdev(major,
name, &pt_fops);
971 printk(
"pt_init: unable to get major number %d\n", major);
972 for (unit = 0; unit <
PT_UNITS; unit++)
973 if (pt[unit].present)
979 if (IS_ERR(pt_class)) {
980 err = PTR_ERR(pt_class);
984 for (unit = 0; unit <
PT_UNITS; unit++)
985 if (pt[unit].present) {
989 NULL,
"pt%dn", unit);
994 unregister_chrdev(major,
"pt");
999 static void __exit pt_exit(
void)
1002 for (unit = 0; unit <
PT_UNITS; unit++)
1003 if (pt[unit].present) {
1008 unregister_chrdev(major,
name);
1009 for (unit = 0; unit <
PT_UNITS; unit++)
1010 if (pt[unit].present)