0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084
0085
0086
0087
0088
0089
0090
0091
0092
0093
0094
0095
0096
0097
0098
0099
0100
0101
0102
0103
0104
0105
0106
0107 #define PT_VERSION "1.04"
0108 #define PT_MAJOR 96
0109 #define PT_NAME "pt"
0110 #define PT_UNITS 4
0111
0112 #include <linux/types.h>
0113
0114
0115
0116
0117
0118
0119
0120 static int verbose = 0;
0121 static int major = PT_MAJOR;
0122 static char *name = PT_NAME;
0123 static int disable = 0;
0124
0125 static int drive0[6] = { 0, 0, 0, -1, -1, -1 };
0126 static int drive1[6] = { 0, 0, 0, -1, -1, -1 };
0127 static int drive2[6] = { 0, 0, 0, -1, -1, -1 };
0128 static int drive3[6] = { 0, 0, 0, -1, -1, -1 };
0129
0130 static int (*drives[4])[6] = {&drive0, &drive1, &drive2, &drive3};
0131
0132 #define D_PRT 0
0133 #define D_PRO 1
0134 #define D_UNI 2
0135 #define D_MOD 3
0136 #define D_SLV 4
0137 #define D_DLY 5
0138
0139 #define DU (*drives[unit])
0140
0141
0142
0143 #include <linux/module.h>
0144 #include <linux/init.h>
0145 #include <linux/fs.h>
0146 #include <linux/delay.h>
0147 #include <linux/slab.h>
0148 #include <linux/mtio.h>
0149 #include <linux/device.h>
0150 #include <linux/sched.h> /* current, TASK_*, schedule_timeout() */
0151 #include <linux/mutex.h>
0152
0153 #include <linux/uaccess.h>
0154
0155 module_param(verbose, int, 0);
0156 module_param(major, int, 0);
0157 module_param(name, charp, 0);
0158 module_param_array(drive0, int, NULL, 0);
0159 module_param_array(drive1, int, NULL, 0);
0160 module_param_array(drive2, int, NULL, 0);
0161 module_param_array(drive3, int, NULL, 0);
0162
0163 #include "paride.h"
0164
0165 #define PT_MAX_RETRIES 5
0166 #define PT_TMO 3000
0167 #define PT_SPIN_DEL 50
0168 #define PT_RESET_TMO 30
0169 #define PT_READY_TMO 60
0170 #define PT_REWIND_TMO 1200
0171
0172 #define PT_SPIN ((1000000/(HZ*PT_SPIN_DEL))*PT_TMO)
0173
0174 #define STAT_ERR 0x00001
0175 #define STAT_INDEX 0x00002
0176 #define STAT_ECC 0x00004
0177 #define STAT_DRQ 0x00008
0178 #define STAT_SEEK 0x00010
0179 #define STAT_WRERR 0x00020
0180 #define STAT_READY 0x00040
0181 #define STAT_BUSY 0x00080
0182 #define STAT_SENSE 0x1f000
0183
0184 #define ATAPI_TEST_READY 0x00
0185 #define ATAPI_REWIND 0x01
0186 #define ATAPI_REQ_SENSE 0x03
0187 #define ATAPI_READ_6 0x08
0188 #define ATAPI_WRITE_6 0x0a
0189 #define ATAPI_WFM 0x10
0190 #define ATAPI_IDENTIFY 0x12
0191 #define ATAPI_MODE_SENSE 0x1a
0192 #define ATAPI_LOG_SENSE 0x4d
0193
0194 static DEFINE_MUTEX(pt_mutex);
0195 static int pt_open(struct inode *inode, struct file *file);
0196 static long pt_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
0197 static int pt_release(struct inode *inode, struct file *file);
0198 static ssize_t pt_read(struct file *filp, char __user *buf,
0199 size_t count, loff_t * ppos);
0200 static ssize_t pt_write(struct file *filp, const char __user *buf,
0201 size_t count, loff_t * ppos);
0202 static int pt_detect(void);
0203
0204
0205
0206 #define PT_MEDIA 1
0207 #define PT_WRITE_OK 2
0208 #define PT_REWIND 4
0209 #define PT_WRITING 8
0210 #define PT_READING 16
0211 #define PT_EOF 32
0212
0213 #define PT_NAMELEN 8
0214 #define PT_BUFSIZE 16384
0215
0216 struct pt_unit {
0217 struct pi_adapter pia;
0218 struct pi_adapter *pi;
0219 int flags;
0220 int last_sense;
0221 int drive;
0222 atomic_t available;
0223 int bs;
0224 int capacity;
0225 int present;
0226 char *bufptr;
0227 char name[PT_NAMELEN];
0228 };
0229
0230 static int pt_identify(struct pt_unit *tape);
0231
0232 static struct pt_unit pt[PT_UNITS];
0233
0234 static char pt_scratch[512];
0235 static void *par_drv;
0236
0237
0238
0239 static const struct file_operations pt_fops = {
0240 .owner = THIS_MODULE,
0241 .read = pt_read,
0242 .write = pt_write,
0243 .unlocked_ioctl = pt_ioctl,
0244 .open = pt_open,
0245 .release = pt_release,
0246 .llseek = noop_llseek,
0247 };
0248
0249
0250 static struct class *pt_class;
0251
0252 static inline int status_reg(struct pi_adapter *pi)
0253 {
0254 return pi_read_regr(pi, 1, 6);
0255 }
0256
0257 static inline int read_reg(struct pi_adapter *pi, int reg)
0258 {
0259 return pi_read_regr(pi, 0, reg);
0260 }
0261
0262 static inline void write_reg(struct pi_adapter *pi, int reg, int val)
0263 {
0264 pi_write_regr(pi, 0, reg, val);
0265 }
0266
0267 static inline u8 DRIVE(struct pt_unit *tape)
0268 {
0269 return 0xa0+0x10*tape->drive;
0270 }
0271
0272 static int pt_wait(struct pt_unit *tape, int go, int stop, char *fun, char *msg)
0273 {
0274 int j, r, e, s, p;
0275 struct pi_adapter *pi = tape->pi;
0276
0277 j = 0;
0278 while ((((r = status_reg(pi)) & go) || (stop && (!(r & stop))))
0279 && (j++ < PT_SPIN))
0280 udelay(PT_SPIN_DEL);
0281
0282 if ((r & (STAT_ERR & stop)) || (j > PT_SPIN)) {
0283 s = read_reg(pi, 7);
0284 e = read_reg(pi, 1);
0285 p = read_reg(pi, 2);
0286 if (j > PT_SPIN)
0287 e |= 0x100;
0288 if (fun)
0289 printk("%s: %s %s: alt=0x%x stat=0x%x err=0x%x"
0290 " loop=%d phase=%d\n",
0291 tape->name, fun, msg, r, s, e, j, p);
0292 return (e << 8) + s;
0293 }
0294 return 0;
0295 }
0296
0297 static int pt_command(struct pt_unit *tape, char *cmd, int dlen, char *fun)
0298 {
0299 struct pi_adapter *pi = tape->pi;
0300 pi_connect(pi);
0301
0302 write_reg(pi, 6, DRIVE(tape));
0303
0304 if (pt_wait(tape, STAT_BUSY | STAT_DRQ, 0, fun, "before command")) {
0305 pi_disconnect(pi);
0306 return -1;
0307 }
0308
0309 write_reg(pi, 4, dlen % 256);
0310 write_reg(pi, 5, dlen / 256);
0311 write_reg(pi, 7, 0xa0);
0312
0313 if (pt_wait(tape, STAT_BUSY, STAT_DRQ, fun, "command DRQ")) {
0314 pi_disconnect(pi);
0315 return -1;
0316 }
0317
0318 if (read_reg(pi, 2) != 1) {
0319 printk("%s: %s: command phase error\n", tape->name, fun);
0320 pi_disconnect(pi);
0321 return -1;
0322 }
0323
0324 pi_write_block(pi, cmd, 12);
0325
0326 return 0;
0327 }
0328
0329 static int pt_completion(struct pt_unit *tape, char *buf, char *fun)
0330 {
0331 struct pi_adapter *pi = tape->pi;
0332 int r, s, n, p;
0333
0334 r = pt_wait(tape, STAT_BUSY, STAT_DRQ | STAT_READY | STAT_ERR,
0335 fun, "completion");
0336
0337 if (read_reg(pi, 7) & STAT_DRQ) {
0338 n = (((read_reg(pi, 4) + 256 * read_reg(pi, 5)) +
0339 3) & 0xfffc);
0340 p = read_reg(pi, 2) & 3;
0341 if (p == 0)
0342 pi_write_block(pi, buf, n);
0343 if (p == 2)
0344 pi_read_block(pi, buf, n);
0345 }
0346
0347 s = pt_wait(tape, STAT_BUSY, STAT_READY | STAT_ERR, fun, "data done");
0348
0349 pi_disconnect(pi);
0350
0351 return (r ? r : s);
0352 }
0353
0354 static void pt_req_sense(struct pt_unit *tape, int quiet)
0355 {
0356 char rs_cmd[12] = { ATAPI_REQ_SENSE, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0 };
0357 char buf[16];
0358 int r;
0359
0360 r = pt_command(tape, rs_cmd, 16, "Request sense");
0361 mdelay(1);
0362 if (!r)
0363 pt_completion(tape, buf, "Request sense");
0364
0365 tape->last_sense = -1;
0366 if (!r) {
0367 if (!quiet)
0368 printk("%s: Sense key: %x, ASC: %x, ASQ: %x\n",
0369 tape->name, buf[2] & 0xf, buf[12], buf[13]);
0370 tape->last_sense = (buf[2] & 0xf) | ((buf[12] & 0xff) << 8)
0371 | ((buf[13] & 0xff) << 16);
0372 }
0373 }
0374
0375 static int pt_atapi(struct pt_unit *tape, char *cmd, int dlen, char *buf, char *fun)
0376 {
0377 int r;
0378
0379 r = pt_command(tape, cmd, dlen, fun);
0380 mdelay(1);
0381 if (!r)
0382 r = pt_completion(tape, buf, fun);
0383 if (r)
0384 pt_req_sense(tape, !fun);
0385
0386 return r;
0387 }
0388
0389 static void pt_sleep(int cs)
0390 {
0391 schedule_timeout_interruptible(cs);
0392 }
0393
0394 static int pt_poll_dsc(struct pt_unit *tape, int pause, int tmo, char *msg)
0395 {
0396 struct pi_adapter *pi = tape->pi;
0397 int k, e, s;
0398
0399 k = 0;
0400 e = 0;
0401 s = 0;
0402 while (k < tmo) {
0403 pt_sleep(pause);
0404 k++;
0405 pi_connect(pi);
0406 write_reg(pi, 6, DRIVE(tape));
0407 s = read_reg(pi, 7);
0408 e = read_reg(pi, 1);
0409 pi_disconnect(pi);
0410 if (s & (STAT_ERR | STAT_SEEK))
0411 break;
0412 }
0413 if ((k >= tmo) || (s & STAT_ERR)) {
0414 if (k >= tmo)
0415 printk("%s: %s DSC timeout\n", tape->name, msg);
0416 else
0417 printk("%s: %s stat=0x%x err=0x%x\n", tape->name, msg, s,
0418 e);
0419 pt_req_sense(tape, 0);
0420 return 0;
0421 }
0422 return 1;
0423 }
0424
0425 static void pt_media_access_cmd(struct pt_unit *tape, int tmo, char *cmd, char *fun)
0426 {
0427 if (pt_command(tape, cmd, 0, fun)) {
0428 pt_req_sense(tape, 0);
0429 return;
0430 }
0431 pi_disconnect(tape->pi);
0432 pt_poll_dsc(tape, HZ, tmo, fun);
0433 }
0434
0435 static void pt_rewind(struct pt_unit *tape)
0436 {
0437 char rw_cmd[12] = { ATAPI_REWIND, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
0438
0439 pt_media_access_cmd(tape, PT_REWIND_TMO, rw_cmd, "rewind");
0440 }
0441
0442 static void pt_write_fm(struct pt_unit *tape)
0443 {
0444 char wm_cmd[12] = { ATAPI_WFM, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0 };
0445
0446 pt_media_access_cmd(tape, PT_TMO, wm_cmd, "write filemark");
0447 }
0448
0449 #define DBMSG(msg) ((verbose>1)?(msg):NULL)
0450
0451 static int pt_reset(struct pt_unit *tape)
0452 {
0453 struct pi_adapter *pi = tape->pi;
0454 int i, k, flg;
0455 int expect[5] = { 1, 1, 1, 0x14, 0xeb };
0456
0457 pi_connect(pi);
0458 write_reg(pi, 6, DRIVE(tape));
0459 write_reg(pi, 7, 8);
0460
0461 pt_sleep(20 * HZ / 1000);
0462
0463 k = 0;
0464 while ((k++ < PT_RESET_TMO) && (status_reg(pi) & STAT_BUSY))
0465 pt_sleep(HZ / 10);
0466
0467 flg = 1;
0468 for (i = 0; i < 5; i++)
0469 flg &= (read_reg(pi, i + 1) == expect[i]);
0470
0471 if (verbose) {
0472 printk("%s: Reset (%d) signature = ", tape->name, k);
0473 for (i = 0; i < 5; i++)
0474 printk("%3x", read_reg(pi, i + 1));
0475 if (!flg)
0476 printk(" (incorrect)");
0477 printk("\n");
0478 }
0479
0480 pi_disconnect(pi);
0481 return flg - 1;
0482 }
0483
0484 static int pt_ready_wait(struct pt_unit *tape, int tmo)
0485 {
0486 char tr_cmd[12] = { ATAPI_TEST_READY, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
0487 int k, p;
0488
0489 k = 0;
0490 while (k < tmo) {
0491 tape->last_sense = 0;
0492 pt_atapi(tape, tr_cmd, 0, NULL, DBMSG("test unit ready"));
0493 p = tape->last_sense;
0494 if (!p)
0495 return 0;
0496 if (!(((p & 0xffff) == 0x0402) || ((p & 0xff) == 6)))
0497 return p;
0498 k++;
0499 pt_sleep(HZ);
0500 }
0501 return 0x000020;
0502 }
0503
0504 static void xs(char *buf, char *targ, int offs, int len)
0505 {
0506 int j, k, l;
0507
0508 j = 0;
0509 l = 0;
0510 for (k = 0; k < len; k++)
0511 if ((buf[k + offs] != 0x20) || (buf[k + offs] != l))
0512 l = targ[j++] = buf[k + offs];
0513 if (l == 0x20)
0514 j--;
0515 targ[j] = 0;
0516 }
0517
0518 static int xn(char *buf, int offs, int size)
0519 {
0520 int v, k;
0521
0522 v = 0;
0523 for (k = 0; k < size; k++)
0524 v = v * 256 + (buf[k + offs] & 0xff);
0525 return v;
0526 }
0527
0528 static int pt_identify(struct pt_unit *tape)
0529 {
0530 int dt, s;
0531 char *ms[2] = { "master", "slave" };
0532 char mf[10], id[18];
0533 char id_cmd[12] = { ATAPI_IDENTIFY, 0, 0, 0, 36, 0, 0, 0, 0, 0, 0, 0 };
0534 char ms_cmd[12] =
0535 { ATAPI_MODE_SENSE, 0, 0x2a, 0, 36, 0, 0, 0, 0, 0, 0, 0 };
0536 char ls_cmd[12] =
0537 { ATAPI_LOG_SENSE, 0, 0x71, 0, 0, 0, 0, 0, 36, 0, 0, 0 };
0538 char buf[36];
0539
0540 s = pt_atapi(tape, id_cmd, 36, buf, "identify");
0541 if (s)
0542 return -1;
0543
0544 dt = buf[0] & 0x1f;
0545 if (dt != 1) {
0546 if (verbose)
0547 printk("%s: Drive %d, unsupported type %d\n",
0548 tape->name, tape->drive, dt);
0549 return -1;
0550 }
0551
0552 xs(buf, mf, 8, 8);
0553 xs(buf, id, 16, 16);
0554
0555 tape->flags = 0;
0556 tape->capacity = 0;
0557 tape->bs = 0;
0558
0559 if (!pt_ready_wait(tape, PT_READY_TMO))
0560 tape->flags |= PT_MEDIA;
0561
0562 if (!pt_atapi(tape, ms_cmd, 36, buf, "mode sense")) {
0563 if (!(buf[2] & 0x80))
0564 tape->flags |= PT_WRITE_OK;
0565 tape->bs = xn(buf, 10, 2);
0566 }
0567
0568 if (!pt_atapi(tape, ls_cmd, 36, buf, "log sense"))
0569 tape->capacity = xn(buf, 24, 4);
0570
0571 printk("%s: %s %s, %s", tape->name, mf, id, ms[tape->drive]);
0572 if (!(tape->flags & PT_MEDIA))
0573 printk(", no media\n");
0574 else {
0575 if (!(tape->flags & PT_WRITE_OK))
0576 printk(", RO");
0577 printk(", blocksize %d, %d MB\n", tape->bs, tape->capacity / 1024);
0578 }
0579
0580 return 0;
0581 }
0582
0583
0584
0585
0586
0587
0588 static int pt_probe(struct pt_unit *tape)
0589 {
0590 if (tape->drive == -1) {
0591 for (tape->drive = 0; tape->drive <= 1; tape->drive++)
0592 if (!pt_reset(tape))
0593 return pt_identify(tape);
0594 } else {
0595 if (!pt_reset(tape))
0596 return pt_identify(tape);
0597 }
0598 return -1;
0599 }
0600
0601 static int pt_detect(void)
0602 {
0603 struct pt_unit *tape;
0604 int specified = 0, found = 0;
0605 int unit;
0606
0607 printk("%s: %s version %s, major %d\n", name, name, PT_VERSION, major);
0608
0609 par_drv = pi_register_driver(name);
0610 if (!par_drv) {
0611 pr_err("failed to register %s driver\n", name);
0612 return -1;
0613 }
0614
0615 specified = 0;
0616 for (unit = 0; unit < PT_UNITS; unit++) {
0617 struct pt_unit *tape = &pt[unit];
0618 tape->pi = &tape->pia;
0619 atomic_set(&tape->available, 1);
0620 tape->flags = 0;
0621 tape->last_sense = 0;
0622 tape->present = 0;
0623 tape->bufptr = NULL;
0624 tape->drive = DU[D_SLV];
0625 snprintf(tape->name, PT_NAMELEN, "%s%d", name, unit);
0626 if (!DU[D_PRT])
0627 continue;
0628 specified++;
0629 if (pi_init(tape->pi, 0, DU[D_PRT], DU[D_MOD], DU[D_UNI],
0630 DU[D_PRO], DU[D_DLY], pt_scratch, PI_PT,
0631 verbose, tape->name)) {
0632 if (!pt_probe(tape)) {
0633 tape->present = 1;
0634 found++;
0635 } else
0636 pi_release(tape->pi);
0637 }
0638 }
0639 if (specified == 0) {
0640 tape = pt;
0641 if (pi_init(tape->pi, 1, -1, -1, -1, -1, -1, pt_scratch,
0642 PI_PT, verbose, tape->name)) {
0643 if (!pt_probe(tape)) {
0644 tape->present = 1;
0645 found++;
0646 } else
0647 pi_release(tape->pi);
0648 }
0649
0650 }
0651 if (found)
0652 return 0;
0653
0654 pi_unregister_driver(par_drv);
0655 printk("%s: No ATAPI tape drive detected\n", name);
0656 return -1;
0657 }
0658
0659 static int pt_open(struct inode *inode, struct file *file)
0660 {
0661 int unit = iminor(inode) & 0x7F;
0662 struct pt_unit *tape = pt + unit;
0663 int err;
0664
0665 mutex_lock(&pt_mutex);
0666 if (unit >= PT_UNITS || (!tape->present)) {
0667 mutex_unlock(&pt_mutex);
0668 return -ENODEV;
0669 }
0670
0671 err = -EBUSY;
0672 if (!atomic_dec_and_test(&tape->available))
0673 goto out;
0674
0675 pt_identify(tape);
0676
0677 err = -ENODEV;
0678 if (!(tape->flags & PT_MEDIA))
0679 goto out;
0680
0681 err = -EROFS;
0682 if ((!(tape->flags & PT_WRITE_OK)) && (file->f_mode & FMODE_WRITE))
0683 goto out;
0684
0685 if (!(iminor(inode) & 128))
0686 tape->flags |= PT_REWIND;
0687
0688 err = -ENOMEM;
0689 tape->bufptr = kmalloc(PT_BUFSIZE, GFP_KERNEL);
0690 if (tape->bufptr == NULL) {
0691 printk("%s: buffer allocation failed\n", tape->name);
0692 goto out;
0693 }
0694
0695 file->private_data = tape;
0696 mutex_unlock(&pt_mutex);
0697 return 0;
0698
0699 out:
0700 atomic_inc(&tape->available);
0701 mutex_unlock(&pt_mutex);
0702 return err;
0703 }
0704
0705 static long pt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
0706 {
0707 struct pt_unit *tape = file->private_data;
0708 struct mtop __user *p = (void __user *)arg;
0709 struct mtop mtop;
0710
0711 switch (cmd) {
0712 case MTIOCTOP:
0713 if (copy_from_user(&mtop, p, sizeof(struct mtop)))
0714 return -EFAULT;
0715
0716 switch (mtop.mt_op) {
0717
0718 case MTREW:
0719 mutex_lock(&pt_mutex);
0720 pt_rewind(tape);
0721 mutex_unlock(&pt_mutex);
0722 return 0;
0723
0724 case MTWEOF:
0725 mutex_lock(&pt_mutex);
0726 pt_write_fm(tape);
0727 mutex_unlock(&pt_mutex);
0728 return 0;
0729
0730 default:
0731
0732 printk(KERN_DEBUG "%s: Unimplemented mt_op %d\n", tape->name,
0733 mtop.mt_op);
0734 return -EINVAL;
0735 }
0736
0737 default:
0738 return -ENOTTY;
0739 }
0740 }
0741
0742 static int
0743 pt_release(struct inode *inode, struct file *file)
0744 {
0745 struct pt_unit *tape = file->private_data;
0746
0747 if (atomic_read(&tape->available) > 1)
0748 return -EINVAL;
0749
0750 if (tape->flags & PT_WRITING)
0751 pt_write_fm(tape);
0752
0753 if (tape->flags & PT_REWIND)
0754 pt_rewind(tape);
0755
0756 kfree(tape->bufptr);
0757 tape->bufptr = NULL;
0758
0759 atomic_inc(&tape->available);
0760
0761 return 0;
0762
0763 }
0764
0765 static ssize_t pt_read(struct file *filp, char __user *buf, size_t count, loff_t * ppos)
0766 {
0767 struct pt_unit *tape = filp->private_data;
0768 struct pi_adapter *pi = tape->pi;
0769 char rd_cmd[12] = { ATAPI_READ_6, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
0770 int k, n, r, p, s, t, b;
0771
0772 if (!(tape->flags & (PT_READING | PT_WRITING))) {
0773 tape->flags |= PT_READING;
0774 if (pt_atapi(tape, rd_cmd, 0, NULL, "start read-ahead"))
0775 return -EIO;
0776 } else if (tape->flags & PT_WRITING)
0777 return -EIO;
0778
0779 if (tape->flags & PT_EOF)
0780 return 0;
0781
0782 t = 0;
0783
0784 while (count > 0) {
0785
0786 if (!pt_poll_dsc(tape, HZ / 100, PT_TMO, "read"))
0787 return -EIO;
0788
0789 n = count;
0790 if (n > 32768)
0791 n = 32768;
0792 b = (n - 1 + tape->bs) / tape->bs;
0793 n = b * tape->bs;
0794
0795 rd_cmd[4] = b;
0796
0797 r = pt_command(tape, rd_cmd, n, "read");
0798
0799 mdelay(1);
0800
0801 if (r) {
0802 pt_req_sense(tape, 0);
0803 return -EIO;
0804 }
0805
0806 while (1) {
0807
0808 r = pt_wait(tape, STAT_BUSY,
0809 STAT_DRQ | STAT_ERR | STAT_READY,
0810 DBMSG("read DRQ"), "");
0811
0812 if (r & STAT_SENSE) {
0813 pi_disconnect(pi);
0814 pt_req_sense(tape, 0);
0815 return -EIO;
0816 }
0817
0818 if (r)
0819 tape->flags |= PT_EOF;
0820
0821 s = read_reg(pi, 7);
0822
0823 if (!(s & STAT_DRQ))
0824 break;
0825
0826 n = (read_reg(pi, 4) + 256 * read_reg(pi, 5));
0827 p = (read_reg(pi, 2) & 3);
0828 if (p != 2) {
0829 pi_disconnect(pi);
0830 printk("%s: Phase error on read: %d\n", tape->name,
0831 p);
0832 return -EIO;
0833 }
0834
0835 while (n > 0) {
0836 k = n;
0837 if (k > PT_BUFSIZE)
0838 k = PT_BUFSIZE;
0839 pi_read_block(pi, tape->bufptr, k);
0840 n -= k;
0841 b = k;
0842 if (b > count)
0843 b = count;
0844 if (copy_to_user(buf + t, tape->bufptr, b)) {
0845 pi_disconnect(pi);
0846 return -EFAULT;
0847 }
0848 t += b;
0849 count -= b;
0850 }
0851
0852 }
0853 pi_disconnect(pi);
0854 if (tape->flags & PT_EOF)
0855 break;
0856 }
0857
0858 return t;
0859
0860 }
0861
0862 static ssize_t pt_write(struct file *filp, const char __user *buf, size_t count, loff_t * ppos)
0863 {
0864 struct pt_unit *tape = filp->private_data;
0865 struct pi_adapter *pi = tape->pi;
0866 char wr_cmd[12] = { ATAPI_WRITE_6, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
0867 int k, n, r, p, s, t, b;
0868
0869 if (!(tape->flags & PT_WRITE_OK))
0870 return -EROFS;
0871
0872 if (!(tape->flags & (PT_READING | PT_WRITING))) {
0873 tape->flags |= PT_WRITING;
0874 if (pt_atapi
0875 (tape, wr_cmd, 0, NULL, "start buffer-available mode"))
0876 return -EIO;
0877 } else if (tape->flags & PT_READING)
0878 return -EIO;
0879
0880 if (tape->flags & PT_EOF)
0881 return -ENOSPC;
0882
0883 t = 0;
0884
0885 while (count > 0) {
0886
0887 if (!pt_poll_dsc(tape, HZ / 100, PT_TMO, "write"))
0888 return -EIO;
0889
0890 n = count;
0891 if (n > 32768)
0892 n = 32768;
0893 b = (n - 1 + tape->bs) / tape->bs;
0894 n = b * tape->bs;
0895
0896 wr_cmd[4] = b;
0897
0898 r = pt_command(tape, wr_cmd, n, "write");
0899
0900 mdelay(1);
0901
0902 if (r) {
0903 pt_req_sense(tape, 0);
0904 return -EIO;
0905 }
0906
0907 while (1) {
0908
0909 r = pt_wait(tape, STAT_BUSY,
0910 STAT_DRQ | STAT_ERR | STAT_READY,
0911 DBMSG("write DRQ"), NULL);
0912
0913 if (r & STAT_SENSE) {
0914 pi_disconnect(pi);
0915 pt_req_sense(tape, 0);
0916 return -EIO;
0917 }
0918
0919 if (r)
0920 tape->flags |= PT_EOF;
0921
0922 s = read_reg(pi, 7);
0923
0924 if (!(s & STAT_DRQ))
0925 break;
0926
0927 n = (read_reg(pi, 4) + 256 * read_reg(pi, 5));
0928 p = (read_reg(pi, 2) & 3);
0929 if (p != 0) {
0930 pi_disconnect(pi);
0931 printk("%s: Phase error on write: %d \n",
0932 tape->name, p);
0933 return -EIO;
0934 }
0935
0936 while (n > 0) {
0937 k = n;
0938 if (k > PT_BUFSIZE)
0939 k = PT_BUFSIZE;
0940 b = k;
0941 if (b > count)
0942 b = count;
0943 if (copy_from_user(tape->bufptr, buf + t, b)) {
0944 pi_disconnect(pi);
0945 return -EFAULT;
0946 }
0947 pi_write_block(pi, tape->bufptr, k);
0948 t += b;
0949 count -= b;
0950 n -= k;
0951 }
0952
0953 }
0954 pi_disconnect(pi);
0955 if (tape->flags & PT_EOF)
0956 break;
0957 }
0958
0959 return t;
0960 }
0961
0962 static int __init pt_init(void)
0963 {
0964 int unit;
0965 int err;
0966
0967 if (disable) {
0968 err = -EINVAL;
0969 goto out;
0970 }
0971
0972 if (pt_detect()) {
0973 err = -ENODEV;
0974 goto out;
0975 }
0976
0977 err = register_chrdev(major, name, &pt_fops);
0978 if (err < 0) {
0979 printk("pt_init: unable to get major number %d\n", major);
0980 for (unit = 0; unit < PT_UNITS; unit++)
0981 if (pt[unit].present)
0982 pi_release(pt[unit].pi);
0983 goto out;
0984 }
0985 major = err;
0986 pt_class = class_create(THIS_MODULE, "pt");
0987 if (IS_ERR(pt_class)) {
0988 err = PTR_ERR(pt_class);
0989 goto out_chrdev;
0990 }
0991
0992 for (unit = 0; unit < PT_UNITS; unit++)
0993 if (pt[unit].present) {
0994 device_create(pt_class, NULL, MKDEV(major, unit), NULL,
0995 "pt%d", unit);
0996 device_create(pt_class, NULL, MKDEV(major, unit + 128),
0997 NULL, "pt%dn", unit);
0998 }
0999 goto out;
1000
1001 out_chrdev:
1002 unregister_chrdev(major, "pt");
1003 out:
1004 return err;
1005 }
1006
1007 static void __exit pt_exit(void)
1008 {
1009 int unit;
1010 for (unit = 0; unit < PT_UNITS; unit++)
1011 if (pt[unit].present) {
1012 device_destroy(pt_class, MKDEV(major, unit));
1013 device_destroy(pt_class, MKDEV(major, unit + 128));
1014 }
1015 class_destroy(pt_class);
1016 unregister_chrdev(major, name);
1017 for (unit = 0; unit < PT_UNITS; unit++)
1018 if (pt[unit].present)
1019 pi_release(pt[unit].pi);
1020 }
1021
1022 MODULE_LICENSE("GPL");
1023 module_init(pt_init)
1024 module_exit(pt_exit)