Loading...
1/*
2 pt.c (c) 1998 Grant R. Guenther <grant@torque.net>
3 Under the terms of the GNU General Public License.
4
5 This is the high-level driver for parallel port ATAPI tape
6 drives based on chips supported by the paride module.
7
8 The driver implements both rewinding and non-rewinding
9 devices, filemarks, and the rewind ioctl. It allocates
10 a small internal "bounce buffer" for each open device, but
11 otherwise expects buffering and blocking to be done at the
12 user level. As with most block-structured tapes, short
13 writes are padded to full tape blocks, so reading back a file
14 may return more data than was actually written.
15
16 By default, the driver will autoprobe for a single parallel
17 port ATAPI tape drive, but if their individual parameters are
18 specified, the driver can handle up to 4 drives.
19
20 The rewinding devices are named /dev/pt0, /dev/pt1, ...
21 while the non-rewinding devices are /dev/npt0, /dev/npt1, etc.
22
23 The behaviour of the pt driver can be altered by setting
24 some parameters from the insmod command line. The following
25 parameters are adjustable:
26
27 drive0 These four arguments can be arrays of
28 drive1 1-6 integers as follows:
29 drive2
30 drive3 <prt>,<pro>,<uni>,<mod>,<slv>,<dly>
31
32 Where,
33
34 <prt> is the base of the parallel port address for
35 the corresponding drive. (required)
36
37 <pro> is the protocol number for the adapter that
38 supports this drive. These numbers are
39 logged by 'paride' when the protocol modules
40 are initialised. (0 if not given)
41
42 <uni> for those adapters that support chained
43 devices, this is the unit selector for the
44 chain of devices on the given port. It should
45 be zero for devices that don't support chaining.
46 (0 if not given)
47
48 <mod> this can be -1 to choose the best mode, or one
49 of the mode numbers supported by the adapter.
50 (-1 if not given)
51
52 <slv> ATAPI devices can be jumpered to master or slave.
53 Set this to 0 to choose the master drive, 1 to
54 choose the slave, -1 (the default) to choose the
55 first drive found.
56
57 <dly> some parallel ports require the driver to
58 go more slowly. -1 sets a default value that
59 should work with the chosen protocol. Otherwise,
60 set this to a small integer, the larger it is
61 the slower the port i/o. In some cases, setting
62 this to zero will speed up the device. (default -1)
63
64 major You may use this parameter to override the
65 default major number (96) that this driver
66 will use. Be sure to change the device
67 name as well.
68
69 name This parameter is a character string that
70 contains the name the kernel will use for this
71 device (in /proc output, for instance).
72 (default "pt").
73
74 verbose This parameter controls the amount of logging
75 that the driver will do. Set it to 0 for
76 normal operation, 1 to see autoprobe progress
77 messages, or 2 to see additional debugging
78 output. (default 0)
79
80 If this driver is built into the kernel, you can use
81 the following command line parameters, with the same values
82 as the corresponding module parameters listed above:
83
84 pt.drive0
85 pt.drive1
86 pt.drive2
87 pt.drive3
88
89 In addition, you can use the parameter pt.disable to disable
90 the driver entirely.
91
92*/
93
94/* Changes:
95
96 1.01 GRG 1998.05.06 Round up transfer size, fix ready_wait,
97 loosed interpretation of ATAPI standard
98 for clearing error status.
99 Eliminate sti();
100 1.02 GRG 1998.06.16 Eliminate an Ugh.
101 1.03 GRG 1998.08.15 Adjusted PT_TMO, use HZ in loop timing,
102 extra debugging
103 1.04 GRG 1998.09.24 Repair minor coding error, added jumbo support
104
105*/
106
107#define PT_VERSION "1.04"
108#define PT_MAJOR 96
109#define PT_NAME "pt"
110#define PT_UNITS 4
111
112#include <linux/types.h>
113
114/* Here are things one can override from the insmod command.
115 Most are autoprobed by paride unless set here. Verbose is on
116 by default.
117
118*/
119
120static int verbose = 0;
121static int major = PT_MAJOR;
122static char *name = PT_NAME;
123static int disable = 0;
124
125static int drive0[6] = { 0, 0, 0, -1, -1, -1 };
126static int drive1[6] = { 0, 0, 0, -1, -1, -1 };
127static int drive2[6] = { 0, 0, 0, -1, -1, -1 };
128static int drive3[6] = { 0, 0, 0, -1, -1, -1 };
129
130static int (*drives[4])[6] = {&drive0, &drive1, &drive2, &drive3};
131
132#define D_PRT 0
133#define D_PRO 1
134#define D_UNI 2
135#define D_MOD 3
136#define D_SLV 4
137#define D_DLY 5
138
139#define DU (*drives[unit])
140
141/* end of parameters */
142
143#include <linux/module.h>
144#include <linux/init.h>
145#include <linux/fs.h>
146#include <linux/delay.h>
147#include <linux/slab.h>
148#include <linux/mtio.h>
149#include <linux/device.h>
150#include <linux/sched.h> /* current, TASK_*, schedule_timeout() */
151#include <linux/mutex.h>
152
153#include <linux/uaccess.h>
154
155module_param(verbose, int, 0);
156module_param(major, int, 0);
157module_param(name, charp, 0);
158module_param_array(drive0, int, NULL, 0);
159module_param_array(drive1, int, NULL, 0);
160module_param_array(drive2, int, NULL, 0);
161module_param_array(drive3, int, NULL, 0);
162
163#include "paride.h"
164
165#define PT_MAX_RETRIES 5
166#define PT_TMO 3000 /* interrupt timeout in jiffies */
167#define PT_SPIN_DEL 50 /* spin delay in micro-seconds */
168#define PT_RESET_TMO 30 /* 30 seconds */
169#define PT_READY_TMO 60 /* 60 seconds */
170#define PT_REWIND_TMO 1200 /* 20 minutes */
171
172#define PT_SPIN ((1000000/(HZ*PT_SPIN_DEL))*PT_TMO)
173
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
183
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
193
194static DEFINE_MUTEX(pt_mutex);
195static int pt_open(struct inode *inode, struct file *file);
196static long pt_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
197static int pt_release(struct inode *inode, struct file *file);
198static ssize_t pt_read(struct file *filp, char __user *buf,
199 size_t count, loff_t * ppos);
200static ssize_t pt_write(struct file *filp, const char __user *buf,
201 size_t count, loff_t * ppos);
202static int pt_detect(void);
203
204/* bits in tape->flags */
205
206#define PT_MEDIA 1
207#define PT_WRITE_OK 2
208#define PT_REWIND 4
209#define PT_WRITING 8
210#define PT_READING 16
211#define PT_EOF 32
212
213#define PT_NAMELEN 8
214#define PT_BUFSIZE 16384
215
216struct pt_unit {
217 struct pi_adapter pia; /* interface to paride layer */
218 struct pi_adapter *pi;
219 int flags; /* various state flags */
220 int last_sense; /* result of last request sense */
221 int drive; /* drive */
222 atomic_t available; /* 1 if access is available 0 otherwise */
223 int bs; /* block size */
224 int capacity; /* Size of tape in KB */
225 int present; /* device present ? */
226 char *bufptr;
227 char name[PT_NAMELEN]; /* pf0, pf1, ... */
228};
229
230static int pt_identify(struct pt_unit *tape);
231
232static struct pt_unit pt[PT_UNITS];
233
234static char pt_scratch[512]; /* scratch block buffer */
235static void *par_drv; /* reference of parport driver */
236
237/* kernel glue structures */
238
239static const struct file_operations pt_fops = {
240 .owner = THIS_MODULE,
241 .read = pt_read,
242 .write = pt_write,
243 .unlocked_ioctl = pt_ioctl,
244 .open = pt_open,
245 .release = pt_release,
246 .llseek = noop_llseek,
247};
248
249/* sysfs class support */
250static struct class *pt_class;
251
252static inline int status_reg(struct pi_adapter *pi)
253{
254 return pi_read_regr(pi, 1, 6);
255}
256
257static inline int read_reg(struct pi_adapter *pi, int reg)
258{
259 return pi_read_regr(pi, 0, reg);
260}
261
262static inline void write_reg(struct pi_adapter *pi, int reg, int val)
263{
264 pi_write_regr(pi, 0, reg, val);
265}
266
267static inline u8 DRIVE(struct pt_unit *tape)
268{
269 return 0xa0+0x10*tape->drive;
270}
271
272static int pt_wait(struct pt_unit *tape, int go, int stop, char *fun, char *msg)
273{
274 int j, r, e, s, p;
275 struct pi_adapter *pi = tape->pi;
276
277 j = 0;
278 while ((((r = status_reg(pi)) & go) || (stop && (!(r & stop))))
279 && (j++ < PT_SPIN))
280 udelay(PT_SPIN_DEL);
281
282 if ((r & (STAT_ERR & stop)) || (j > PT_SPIN)) {
283 s = read_reg(pi, 7);
284 e = read_reg(pi, 1);
285 p = read_reg(pi, 2);
286 if (j > PT_SPIN)
287 e |= 0x100;
288 if (fun)
289 printk("%s: %s %s: alt=0x%x stat=0x%x err=0x%x"
290 " loop=%d phase=%d\n",
291 tape->name, fun, msg, r, s, e, j, p);
292 return (e << 8) + s;
293 }
294 return 0;
295}
296
297static int pt_command(struct pt_unit *tape, char *cmd, int dlen, char *fun)
298{
299 struct pi_adapter *pi = tape->pi;
300 pi_connect(pi);
301
302 write_reg(pi, 6, DRIVE(tape));
303
304 if (pt_wait(tape, STAT_BUSY | STAT_DRQ, 0, fun, "before command")) {
305 pi_disconnect(pi);
306 return -1;
307 }
308
309 write_reg(pi, 4, dlen % 256);
310 write_reg(pi, 5, dlen / 256);
311 write_reg(pi, 7, 0xa0); /* ATAPI packet command */
312
313 if (pt_wait(tape, STAT_BUSY, STAT_DRQ, fun, "command DRQ")) {
314 pi_disconnect(pi);
315 return -1;
316 }
317
318 if (read_reg(pi, 2) != 1) {
319 printk("%s: %s: command phase error\n", tape->name, fun);
320 pi_disconnect(pi);
321 return -1;
322 }
323
324 pi_write_block(pi, cmd, 12);
325
326 return 0;
327}
328
329static int pt_completion(struct pt_unit *tape, char *buf, char *fun)
330{
331 struct pi_adapter *pi = tape->pi;
332 int r, s, n, p;
333
334 r = pt_wait(tape, STAT_BUSY, STAT_DRQ | STAT_READY | STAT_ERR,
335 fun, "completion");
336
337 if (read_reg(pi, 7) & STAT_DRQ) {
338 n = (((read_reg(pi, 4) + 256 * read_reg(pi, 5)) +
339 3) & 0xfffc);
340 p = read_reg(pi, 2) & 3;
341 if (p == 0)
342 pi_write_block(pi, buf, n);
343 if (p == 2)
344 pi_read_block(pi, buf, n);
345 }
346
347 s = pt_wait(tape, STAT_BUSY, STAT_READY | STAT_ERR, fun, "data done");
348
349 pi_disconnect(pi);
350
351 return (r ? r : s);
352}
353
354static void pt_req_sense(struct pt_unit *tape, int quiet)
355{
356 char rs_cmd[12] = { ATAPI_REQ_SENSE, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0 };
357 char buf[16];
358 int r;
359
360 r = pt_command(tape, rs_cmd, 16, "Request sense");
361 mdelay(1);
362 if (!r)
363 pt_completion(tape, buf, "Request sense");
364
365 tape->last_sense = -1;
366 if (!r) {
367 if (!quiet)
368 printk("%s: Sense key: %x, ASC: %x, ASQ: %x\n",
369 tape->name, buf[2] & 0xf, buf[12], buf[13]);
370 tape->last_sense = (buf[2] & 0xf) | ((buf[12] & 0xff) << 8)
371 | ((buf[13] & 0xff) << 16);
372 }
373}
374
375static int pt_atapi(struct pt_unit *tape, char *cmd, int dlen, char *buf, char *fun)
376{
377 int r;
378
379 r = pt_command(tape, cmd, dlen, fun);
380 mdelay(1);
381 if (!r)
382 r = pt_completion(tape, buf, fun);
383 if (r)
384 pt_req_sense(tape, !fun);
385
386 return r;
387}
388
389static void pt_sleep(int cs)
390{
391 schedule_timeout_interruptible(cs);
392}
393
394static int pt_poll_dsc(struct pt_unit *tape, int pause, int tmo, char *msg)
395{
396 struct pi_adapter *pi = tape->pi;
397 int k, e, s;
398
399 k = 0;
400 e = 0;
401 s = 0;
402 while (k < tmo) {
403 pt_sleep(pause);
404 k++;
405 pi_connect(pi);
406 write_reg(pi, 6, DRIVE(tape));
407 s = read_reg(pi, 7);
408 e = read_reg(pi, 1);
409 pi_disconnect(pi);
410 if (s & (STAT_ERR | STAT_SEEK))
411 break;
412 }
413 if ((k >= tmo) || (s & STAT_ERR)) {
414 if (k >= tmo)
415 printk("%s: %s DSC timeout\n", tape->name, msg);
416 else
417 printk("%s: %s stat=0x%x err=0x%x\n", tape->name, msg, s,
418 e);
419 pt_req_sense(tape, 0);
420 return 0;
421 }
422 return 1;
423}
424
425static void pt_media_access_cmd(struct pt_unit *tape, int tmo, char *cmd, char *fun)
426{
427 if (pt_command(tape, cmd, 0, fun)) {
428 pt_req_sense(tape, 0);
429 return;
430 }
431 pi_disconnect(tape->pi);
432 pt_poll_dsc(tape, HZ, tmo, fun);
433}
434
435static void pt_rewind(struct pt_unit *tape)
436{
437 char rw_cmd[12] = { ATAPI_REWIND, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
438
439 pt_media_access_cmd(tape, PT_REWIND_TMO, rw_cmd, "rewind");
440}
441
442static void pt_write_fm(struct pt_unit *tape)
443{
444 char wm_cmd[12] = { ATAPI_WFM, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0 };
445
446 pt_media_access_cmd(tape, PT_TMO, wm_cmd, "write filemark");
447}
448
449#define DBMSG(msg) ((verbose>1)?(msg):NULL)
450
451static int pt_reset(struct pt_unit *tape)
452{
453 struct pi_adapter *pi = tape->pi;
454 int i, k, flg;
455 int expect[5] = { 1, 1, 1, 0x14, 0xeb };
456
457 pi_connect(pi);
458 write_reg(pi, 6, DRIVE(tape));
459 write_reg(pi, 7, 8);
460
461 pt_sleep(20 * HZ / 1000);
462
463 k = 0;
464 while ((k++ < PT_RESET_TMO) && (status_reg(pi) & STAT_BUSY))
465 pt_sleep(HZ / 10);
466
467 flg = 1;
468 for (i = 0; i < 5; i++)
469 flg &= (read_reg(pi, i + 1) == expect[i]);
470
471 if (verbose) {
472 printk("%s: Reset (%d) signature = ", tape->name, k);
473 for (i = 0; i < 5; i++)
474 printk("%3x", read_reg(pi, i + 1));
475 if (!flg)
476 printk(" (incorrect)");
477 printk("\n");
478 }
479
480 pi_disconnect(pi);
481 return flg - 1;
482}
483
484static int pt_ready_wait(struct pt_unit *tape, int tmo)
485{
486 char tr_cmd[12] = { ATAPI_TEST_READY, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
487 int k, p;
488
489 k = 0;
490 while (k < tmo) {
491 tape->last_sense = 0;
492 pt_atapi(tape, tr_cmd, 0, NULL, DBMSG("test unit ready"));
493 p = tape->last_sense;
494 if (!p)
495 return 0;
496 if (!(((p & 0xffff) == 0x0402) || ((p & 0xff) == 6)))
497 return p;
498 k++;
499 pt_sleep(HZ);
500 }
501 return 0x000020; /* timeout */
502}
503
504static void xs(char *buf, char *targ, int offs, int len)
505{
506 int j, k, l;
507
508 j = 0;
509 l = 0;
510 for (k = 0; k < len; k++)
511 if ((buf[k + offs] != 0x20) || (buf[k + offs] != l))
512 l = targ[j++] = buf[k + offs];
513 if (l == 0x20)
514 j--;
515 targ[j] = 0;
516}
517
518static int xn(char *buf, int offs, int size)
519{
520 int v, k;
521
522 v = 0;
523 for (k = 0; k < size; k++)
524 v = v * 256 + (buf[k + offs] & 0xff);
525 return v;
526}
527
528static int pt_identify(struct pt_unit *tape)
529{
530 int dt, s;
531 char *ms[2] = { "master", "slave" };
532 char mf[10], id[18];
533 char id_cmd[12] = { ATAPI_IDENTIFY, 0, 0, 0, 36, 0, 0, 0, 0, 0, 0, 0 };
534 char ms_cmd[12] =
535 { ATAPI_MODE_SENSE, 0, 0x2a, 0, 36, 0, 0, 0, 0, 0, 0, 0 };
536 char ls_cmd[12] =
537 { ATAPI_LOG_SENSE, 0, 0x71, 0, 0, 0, 0, 0, 36, 0, 0, 0 };
538 char buf[36];
539
540 s = pt_atapi(tape, id_cmd, 36, buf, "identify");
541 if (s)
542 return -1;
543
544 dt = buf[0] & 0x1f;
545 if (dt != 1) {
546 if (verbose)
547 printk("%s: Drive %d, unsupported type %d\n",
548 tape->name, tape->drive, dt);
549 return -1;
550 }
551
552 xs(buf, mf, 8, 8);
553 xs(buf, id, 16, 16);
554
555 tape->flags = 0;
556 tape->capacity = 0;
557 tape->bs = 0;
558
559 if (!pt_ready_wait(tape, PT_READY_TMO))
560 tape->flags |= PT_MEDIA;
561
562 if (!pt_atapi(tape, ms_cmd, 36, buf, "mode sense")) {
563 if (!(buf[2] & 0x80))
564 tape->flags |= PT_WRITE_OK;
565 tape->bs = xn(buf, 10, 2);
566 }
567
568 if (!pt_atapi(tape, ls_cmd, 36, buf, "log sense"))
569 tape->capacity = xn(buf, 24, 4);
570
571 printk("%s: %s %s, %s", tape->name, mf, id, ms[tape->drive]);
572 if (!(tape->flags & PT_MEDIA))
573 printk(", no media\n");
574 else {
575 if (!(tape->flags & PT_WRITE_OK))
576 printk(", RO");
577 printk(", blocksize %d, %d MB\n", tape->bs, tape->capacity / 1024);
578 }
579
580 return 0;
581}
582
583
584/*
585 * returns 0, with id set if drive is detected
586 * -1, if drive detection failed
587 */
588static int pt_probe(struct pt_unit *tape)
589{
590 if (tape->drive == -1) {
591 for (tape->drive = 0; tape->drive <= 1; tape->drive++)
592 if (!pt_reset(tape))
593 return pt_identify(tape);
594 } else {
595 if (!pt_reset(tape))
596 return pt_identify(tape);
597 }
598 return -1;
599}
600
601static int pt_detect(void)
602{
603 struct pt_unit *tape;
604 int specified = 0, found = 0;
605 int unit;
606
607 printk("%s: %s version %s, major %d\n", name, name, PT_VERSION, major);
608
609 par_drv = pi_register_driver(name);
610 if (!par_drv) {
611 pr_err("failed to register %s driver\n", name);
612 return -1;
613 }
614
615 specified = 0;
616 for (unit = 0; unit < PT_UNITS; unit++) {
617 struct pt_unit *tape = &pt[unit];
618 tape->pi = &tape->pia;
619 atomic_set(&tape->available, 1);
620 tape->flags = 0;
621 tape->last_sense = 0;
622 tape->present = 0;
623 tape->bufptr = NULL;
624 tape->drive = DU[D_SLV];
625 snprintf(tape->name, PT_NAMELEN, "%s%d", name, unit);
626 if (!DU[D_PRT])
627 continue;
628 specified++;
629 if (pi_init(tape->pi, 0, DU[D_PRT], DU[D_MOD], DU[D_UNI],
630 DU[D_PRO], DU[D_DLY], pt_scratch, PI_PT,
631 verbose, tape->name)) {
632 if (!pt_probe(tape)) {
633 tape->present = 1;
634 found++;
635 } else
636 pi_release(tape->pi);
637 }
638 }
639 if (specified == 0) {
640 tape = pt;
641 if (pi_init(tape->pi, 1, -1, -1, -1, -1, -1, pt_scratch,
642 PI_PT, verbose, tape->name)) {
643 if (!pt_probe(tape)) {
644 tape->present = 1;
645 found++;
646 } else
647 pi_release(tape->pi);
648 }
649
650 }
651 if (found)
652 return 0;
653
654 pi_unregister_driver(par_drv);
655 printk("%s: No ATAPI tape drive detected\n", name);
656 return -1;
657}
658
659static int pt_open(struct inode *inode, struct file *file)
660{
661 int unit = iminor(inode) & 0x7F;
662 struct pt_unit *tape = pt + unit;
663 int err;
664
665 mutex_lock(&pt_mutex);
666 if (unit >= PT_UNITS || (!tape->present)) {
667 mutex_unlock(&pt_mutex);
668 return -ENODEV;
669 }
670
671 err = -EBUSY;
672 if (!atomic_dec_and_test(&tape->available))
673 goto out;
674
675 pt_identify(tape);
676
677 err = -ENODEV;
678 if (!(tape->flags & PT_MEDIA))
679 goto out;
680
681 err = -EROFS;
682 if ((!(tape->flags & PT_WRITE_OK)) && (file->f_mode & FMODE_WRITE))
683 goto out;
684
685 if (!(iminor(inode) & 128))
686 tape->flags |= PT_REWIND;
687
688 err = -ENOMEM;
689 tape->bufptr = kmalloc(PT_BUFSIZE, GFP_KERNEL);
690 if (tape->bufptr == NULL) {
691 printk("%s: buffer allocation failed\n", tape->name);
692 goto out;
693 }
694
695 file->private_data = tape;
696 mutex_unlock(&pt_mutex);
697 return 0;
698
699out:
700 atomic_inc(&tape->available);
701 mutex_unlock(&pt_mutex);
702 return err;
703}
704
705static long pt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
706{
707 struct pt_unit *tape = file->private_data;
708 struct mtop __user *p = (void __user *)arg;
709 struct mtop mtop;
710
711 switch (cmd) {
712 case MTIOCTOP:
713 if (copy_from_user(&mtop, p, sizeof(struct mtop)))
714 return -EFAULT;
715
716 switch (mtop.mt_op) {
717
718 case MTREW:
719 mutex_lock(&pt_mutex);
720 pt_rewind(tape);
721 mutex_unlock(&pt_mutex);
722 return 0;
723
724 case MTWEOF:
725 mutex_lock(&pt_mutex);
726 pt_write_fm(tape);
727 mutex_unlock(&pt_mutex);
728 return 0;
729
730 default:
731 /* FIXME: rate limit ?? */
732 printk(KERN_DEBUG "%s: Unimplemented mt_op %d\n", tape->name,
733 mtop.mt_op);
734 return -EINVAL;
735 }
736
737 default:
738 return -ENOTTY;
739 }
740}
741
742static int
743pt_release(struct inode *inode, struct file *file)
744{
745 struct pt_unit *tape = file->private_data;
746
747 if (atomic_read(&tape->available) > 1)
748 return -EINVAL;
749
750 if (tape->flags & PT_WRITING)
751 pt_write_fm(tape);
752
753 if (tape->flags & PT_REWIND)
754 pt_rewind(tape);
755
756 kfree(tape->bufptr);
757 tape->bufptr = NULL;
758
759 atomic_inc(&tape->available);
760
761 return 0;
762
763}
764
765static ssize_t pt_read(struct file *filp, char __user *buf, size_t count, loff_t * ppos)
766{
767 struct pt_unit *tape = filp->private_data;
768 struct pi_adapter *pi = tape->pi;
769 char rd_cmd[12] = { ATAPI_READ_6, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
770 int k, n, r, p, s, t, b;
771
772 if (!(tape->flags & (PT_READING | PT_WRITING))) {
773 tape->flags |= PT_READING;
774 if (pt_atapi(tape, rd_cmd, 0, NULL, "start read-ahead"))
775 return -EIO;
776 } else if (tape->flags & PT_WRITING)
777 return -EIO;
778
779 if (tape->flags & PT_EOF)
780 return 0;
781
782 t = 0;
783
784 while (count > 0) {
785
786 if (!pt_poll_dsc(tape, HZ / 100, PT_TMO, "read"))
787 return -EIO;
788
789 n = count;
790 if (n > 32768)
791 n = 32768; /* max per command */
792 b = (n - 1 + tape->bs) / tape->bs;
793 n = b * tape->bs; /* rounded up to even block */
794
795 rd_cmd[4] = b;
796
797 r = pt_command(tape, rd_cmd, n, "read");
798
799 mdelay(1);
800
801 if (r) {
802 pt_req_sense(tape, 0);
803 return -EIO;
804 }
805
806 while (1) {
807
808 r = pt_wait(tape, STAT_BUSY,
809 STAT_DRQ | STAT_ERR | STAT_READY,
810 DBMSG("read DRQ"), "");
811
812 if (r & STAT_SENSE) {
813 pi_disconnect(pi);
814 pt_req_sense(tape, 0);
815 return -EIO;
816 }
817
818 if (r)
819 tape->flags |= PT_EOF;
820
821 s = read_reg(pi, 7);
822
823 if (!(s & STAT_DRQ))
824 break;
825
826 n = (read_reg(pi, 4) + 256 * read_reg(pi, 5));
827 p = (read_reg(pi, 2) & 3);
828 if (p != 2) {
829 pi_disconnect(pi);
830 printk("%s: Phase error on read: %d\n", tape->name,
831 p);
832 return -EIO;
833 }
834
835 while (n > 0) {
836 k = n;
837 if (k > PT_BUFSIZE)
838 k = PT_BUFSIZE;
839 pi_read_block(pi, tape->bufptr, k);
840 n -= k;
841 b = k;
842 if (b > count)
843 b = count;
844 if (copy_to_user(buf + t, tape->bufptr, b)) {
845 pi_disconnect(pi);
846 return -EFAULT;
847 }
848 t += b;
849 count -= b;
850 }
851
852 }
853 pi_disconnect(pi);
854 if (tape->flags & PT_EOF)
855 break;
856 }
857
858 return t;
859
860}
861
862static ssize_t pt_write(struct file *filp, const char __user *buf, size_t count, loff_t * ppos)
863{
864 struct pt_unit *tape = filp->private_data;
865 struct pi_adapter *pi = tape->pi;
866 char wr_cmd[12] = { ATAPI_WRITE_6, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
867 int k, n, r, p, s, t, b;
868
869 if (!(tape->flags & PT_WRITE_OK))
870 return -EROFS;
871
872 if (!(tape->flags & (PT_READING | PT_WRITING))) {
873 tape->flags |= PT_WRITING;
874 if (pt_atapi
875 (tape, wr_cmd, 0, NULL, "start buffer-available mode"))
876 return -EIO;
877 } else if (tape->flags & PT_READING)
878 return -EIO;
879
880 if (tape->flags & PT_EOF)
881 return -ENOSPC;
882
883 t = 0;
884
885 while (count > 0) {
886
887 if (!pt_poll_dsc(tape, HZ / 100, PT_TMO, "write"))
888 return -EIO;
889
890 n = count;
891 if (n > 32768)
892 n = 32768; /* max per command */
893 b = (n - 1 + tape->bs) / tape->bs;
894 n = b * tape->bs; /* rounded up to even block */
895
896 wr_cmd[4] = b;
897
898 r = pt_command(tape, wr_cmd, n, "write");
899
900 mdelay(1);
901
902 if (r) { /* error delivering command only */
903 pt_req_sense(tape, 0);
904 return -EIO;
905 }
906
907 while (1) {
908
909 r = pt_wait(tape, STAT_BUSY,
910 STAT_DRQ | STAT_ERR | STAT_READY,
911 DBMSG("write DRQ"), NULL);
912
913 if (r & STAT_SENSE) {
914 pi_disconnect(pi);
915 pt_req_sense(tape, 0);
916 return -EIO;
917 }
918
919 if (r)
920 tape->flags |= PT_EOF;
921
922 s = read_reg(pi, 7);
923
924 if (!(s & STAT_DRQ))
925 break;
926
927 n = (read_reg(pi, 4) + 256 * read_reg(pi, 5));
928 p = (read_reg(pi, 2) & 3);
929 if (p != 0) {
930 pi_disconnect(pi);
931 printk("%s: Phase error on write: %d \n",
932 tape->name, p);
933 return -EIO;
934 }
935
936 while (n > 0) {
937 k = n;
938 if (k > PT_BUFSIZE)
939 k = PT_BUFSIZE;
940 b = k;
941 if (b > count)
942 b = count;
943 if (copy_from_user(tape->bufptr, buf + t, b)) {
944 pi_disconnect(pi);
945 return -EFAULT;
946 }
947 pi_write_block(pi, tape->bufptr, k);
948 t += b;
949 count -= b;
950 n -= k;
951 }
952
953 }
954 pi_disconnect(pi);
955 if (tape->flags & PT_EOF)
956 break;
957 }
958
959 return t;
960}
961
962static int __init pt_init(void)
963{
964 int unit;
965 int err;
966
967 if (disable) {
968 err = -EINVAL;
969 goto out;
970 }
971
972 if (pt_detect()) {
973 err = -ENODEV;
974 goto out;
975 }
976
977 err = register_chrdev(major, name, &pt_fops);
978 if (err < 0) {
979 printk("pt_init: unable to get major number %d\n", major);
980 for (unit = 0; unit < PT_UNITS; unit++)
981 if (pt[unit].present)
982 pi_release(pt[unit].pi);
983 goto out;
984 }
985 major = err;
986 pt_class = class_create(THIS_MODULE, "pt");
987 if (IS_ERR(pt_class)) {
988 err = PTR_ERR(pt_class);
989 goto out_chrdev;
990 }
991
992 for (unit = 0; unit < PT_UNITS; unit++)
993 if (pt[unit].present) {
994 device_create(pt_class, NULL, MKDEV(major, unit), NULL,
995 "pt%d", unit);
996 device_create(pt_class, NULL, MKDEV(major, unit + 128),
997 NULL, "pt%dn", unit);
998 }
999 goto out;
1000
1001out_chrdev:
1002 unregister_chrdev(major, "pt");
1003out:
1004 return err;
1005}
1006
1007static 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
1022MODULE_LICENSE("GPL");
1023module_init(pt_init)
1024module_exit(pt_exit)
1/*
2 pt.c (c) 1998 Grant R. Guenther <grant@torque.net>
3 Under the terms of the GNU General Public License.
4
5 This is the high-level driver for parallel port ATAPI tape
6 drives based on chips supported by the paride module.
7
8 The driver implements both rewinding and non-rewinding
9 devices, filemarks, and the rewind ioctl. It allocates
10 a small internal "bounce buffer" for each open device, but
11 otherwise expects buffering and blocking to be done at the
12 user level. As with most block-structured tapes, short
13 writes are padded to full tape blocks, so reading back a file
14 may return more data than was actually written.
15
16 By default, the driver will autoprobe for a single parallel
17 port ATAPI tape drive, but if their individual parameters are
18 specified, the driver can handle up to 4 drives.
19
20 The rewinding devices are named /dev/pt0, /dev/pt1, ...
21 while the non-rewinding devices are /dev/npt0, /dev/npt1, etc.
22
23 The behaviour of the pt driver can be altered by setting
24 some parameters from the insmod command line. The following
25 parameters are adjustable:
26
27 drive0 These four arguments can be arrays of
28 drive1 1-6 integers as follows:
29 drive2
30 drive3 <prt>,<pro>,<uni>,<mod>,<slv>,<dly>
31
32 Where,
33
34 <prt> is the base of the parallel port address for
35 the corresponding drive. (required)
36
37 <pro> is the protocol number for the adapter that
38 supports this drive. These numbers are
39 logged by 'paride' when the protocol modules
40 are initialised. (0 if not given)
41
42 <uni> for those adapters that support chained
43 devices, this is the unit selector for the
44 chain of devices on the given port. It should
45 be zero for devices that don't support chaining.
46 (0 if not given)
47
48 <mod> this can be -1 to choose the best mode, or one
49 of the mode numbers supported by the adapter.
50 (-1 if not given)
51
52 <slv> ATAPI devices can be jumpered to master or slave.
53 Set this to 0 to choose the master drive, 1 to
54 choose the slave, -1 (the default) to choose the
55 first drive found.
56
57 <dly> some parallel ports require the driver to
58 go more slowly. -1 sets a default value that
59 should work with the chosen protocol. Otherwise,
60 set this to a small integer, the larger it is
61 the slower the port i/o. In some cases, setting
62 this to zero will speed up the device. (default -1)
63
64 major You may use this parameter to overide the
65 default major number (96) that this driver
66 will use. Be sure to change the device
67 name as well.
68
69 name This parameter is a character string that
70 contains the name the kernel will use for this
71 device (in /proc output, for instance).
72 (default "pt").
73
74 verbose This parameter controls the amount of logging
75 that the driver will do. Set it to 0 for
76 normal operation, 1 to see autoprobe progress
77 messages, or 2 to see additional debugging
78 output. (default 0)
79
80 If this driver is built into the kernel, you can use
81 the following command line parameters, with the same values
82 as the corresponding module parameters listed above:
83
84 pt.drive0
85 pt.drive1
86 pt.drive2
87 pt.drive3
88
89 In addition, you can use the parameter pt.disable to disable
90 the driver entirely.
91
92*/
93
94/* Changes:
95
96 1.01 GRG 1998.05.06 Round up transfer size, fix ready_wait,
97 loosed interpretation of ATAPI standard
98 for clearing error status.
99 Eliminate sti();
100 1.02 GRG 1998.06.16 Eliminate an Ugh.
101 1.03 GRG 1998.08.15 Adjusted PT_TMO, use HZ in loop timing,
102 extra debugging
103 1.04 GRG 1998.09.24 Repair minor coding error, added jumbo support
104
105*/
106
107#define PT_VERSION "1.04"
108#define PT_MAJOR 96
109#define PT_NAME "pt"
110#define PT_UNITS 4
111
112#include <linux/types.h>
113
114/* Here are things one can override from the insmod command.
115 Most are autoprobed by paride unless set here. Verbose is on
116 by default.
117
118*/
119
120static bool verbose = 0;
121static int major = PT_MAJOR;
122static char *name = PT_NAME;
123static int disable = 0;
124
125static int drive0[6] = { 0, 0, 0, -1, -1, -1 };
126static int drive1[6] = { 0, 0, 0, -1, -1, -1 };
127static int drive2[6] = { 0, 0, 0, -1, -1, -1 };
128static int drive3[6] = { 0, 0, 0, -1, -1, -1 };
129
130static int (*drives[4])[6] = {&drive0, &drive1, &drive2, &drive3};
131
132#define D_PRT 0
133#define D_PRO 1
134#define D_UNI 2
135#define D_MOD 3
136#define D_SLV 4
137#define D_DLY 5
138
139#define DU (*drives[unit])
140
141/* end of parameters */
142
143#include <linux/module.h>
144#include <linux/init.h>
145#include <linux/fs.h>
146#include <linux/delay.h>
147#include <linux/slab.h>
148#include <linux/mtio.h>
149#include <linux/device.h>
150#include <linux/sched.h> /* current, TASK_*, schedule_timeout() */
151#include <linux/mutex.h>
152
153#include <asm/uaccess.h>
154
155module_param(verbose, bool, 0);
156module_param(major, int, 0);
157module_param(name, charp, 0);
158module_param_array(drive0, int, NULL, 0);
159module_param_array(drive1, int, NULL, 0);
160module_param_array(drive2, int, NULL, 0);
161module_param_array(drive3, int, NULL, 0);
162
163#include "paride.h"
164
165#define PT_MAX_RETRIES 5
166#define PT_TMO 3000 /* interrupt timeout in jiffies */
167#define PT_SPIN_DEL 50 /* spin delay in micro-seconds */
168#define PT_RESET_TMO 30 /* 30 seconds */
169#define PT_READY_TMO 60 /* 60 seconds */
170#define PT_REWIND_TMO 1200 /* 20 minutes */
171
172#define PT_SPIN ((1000000/(HZ*PT_SPIN_DEL))*PT_TMO)
173
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
183
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
193
194static DEFINE_MUTEX(pt_mutex);
195static int pt_open(struct inode *inode, struct file *file);
196static long pt_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
197static int pt_release(struct inode *inode, struct file *file);
198static ssize_t pt_read(struct file *filp, char __user *buf,
199 size_t count, loff_t * ppos);
200static ssize_t pt_write(struct file *filp, const char __user *buf,
201 size_t count, loff_t * ppos);
202static int pt_detect(void);
203
204/* bits in tape->flags */
205
206#define PT_MEDIA 1
207#define PT_WRITE_OK 2
208#define PT_REWIND 4
209#define PT_WRITING 8
210#define PT_READING 16
211#define PT_EOF 32
212
213#define PT_NAMELEN 8
214#define PT_BUFSIZE 16384
215
216struct pt_unit {
217 struct pi_adapter pia; /* interface to paride layer */
218 struct pi_adapter *pi;
219 int flags; /* various state flags */
220 int last_sense; /* result of last request sense */
221 int drive; /* drive */
222 atomic_t available; /* 1 if access is available 0 otherwise */
223 int bs; /* block size */
224 int capacity; /* Size of tape in KB */
225 int present; /* device present ? */
226 char *bufptr;
227 char name[PT_NAMELEN]; /* pf0, pf1, ... */
228};
229
230static int pt_identify(struct pt_unit *tape);
231
232static struct pt_unit pt[PT_UNITS];
233
234static char pt_scratch[512]; /* scratch block buffer */
235
236/* kernel glue structures */
237
238static const struct file_operations pt_fops = {
239 .owner = THIS_MODULE,
240 .read = pt_read,
241 .write = pt_write,
242 .unlocked_ioctl = pt_ioctl,
243 .open = pt_open,
244 .release = pt_release,
245 .llseek = noop_llseek,
246};
247
248/* sysfs class support */
249static struct class *pt_class;
250
251static inline int status_reg(struct pi_adapter *pi)
252{
253 return pi_read_regr(pi, 1, 6);
254}
255
256static inline int read_reg(struct pi_adapter *pi, int reg)
257{
258 return pi_read_regr(pi, 0, reg);
259}
260
261static inline void write_reg(struct pi_adapter *pi, int reg, int val)
262{
263 pi_write_regr(pi, 0, reg, val);
264}
265
266static inline u8 DRIVE(struct pt_unit *tape)
267{
268 return 0xa0+0x10*tape->drive;
269}
270
271static int pt_wait(struct pt_unit *tape, int go, int stop, char *fun, char *msg)
272{
273 int j, r, e, s, p;
274 struct pi_adapter *pi = tape->pi;
275
276 j = 0;
277 while ((((r = status_reg(pi)) & go) || (stop && (!(r & stop))))
278 && (j++ < PT_SPIN))
279 udelay(PT_SPIN_DEL);
280
281 if ((r & (STAT_ERR & stop)) || (j > PT_SPIN)) {
282 s = read_reg(pi, 7);
283 e = read_reg(pi, 1);
284 p = read_reg(pi, 2);
285 if (j > PT_SPIN)
286 e |= 0x100;
287 if (fun)
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);
291 return (e << 8) + s;
292 }
293 return 0;
294}
295
296static int pt_command(struct pt_unit *tape, char *cmd, int dlen, char *fun)
297{
298 struct pi_adapter *pi = tape->pi;
299 pi_connect(pi);
300
301 write_reg(pi, 6, DRIVE(tape));
302
303 if (pt_wait(tape, STAT_BUSY | STAT_DRQ, 0, fun, "before command")) {
304 pi_disconnect(pi);
305 return -1;
306 }
307
308 write_reg(pi, 4, dlen % 256);
309 write_reg(pi, 5, dlen / 256);
310 write_reg(pi, 7, 0xa0); /* ATAPI packet command */
311
312 if (pt_wait(tape, STAT_BUSY, STAT_DRQ, fun, "command DRQ")) {
313 pi_disconnect(pi);
314 return -1;
315 }
316
317 if (read_reg(pi, 2) != 1) {
318 printk("%s: %s: command phase error\n", tape->name, fun);
319 pi_disconnect(pi);
320 return -1;
321 }
322
323 pi_write_block(pi, cmd, 12);
324
325 return 0;
326}
327
328static int pt_completion(struct pt_unit *tape, char *buf, char *fun)
329{
330 struct pi_adapter *pi = tape->pi;
331 int r, s, n, p;
332
333 r = pt_wait(tape, STAT_BUSY, STAT_DRQ | STAT_READY | STAT_ERR,
334 fun, "completion");
335
336 if (read_reg(pi, 7) & STAT_DRQ) {
337 n = (((read_reg(pi, 4) + 256 * read_reg(pi, 5)) +
338 3) & 0xfffc);
339 p = read_reg(pi, 2) & 3;
340 if (p == 0)
341 pi_write_block(pi, buf, n);
342 if (p == 2)
343 pi_read_block(pi, buf, n);
344 }
345
346 s = pt_wait(tape, STAT_BUSY, STAT_READY | STAT_ERR, fun, "data done");
347
348 pi_disconnect(pi);
349
350 return (r ? r : s);
351}
352
353static void pt_req_sense(struct pt_unit *tape, int quiet)
354{
355 char rs_cmd[12] = { ATAPI_REQ_SENSE, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0 };
356 char buf[16];
357 int r;
358
359 r = pt_command(tape, rs_cmd, 16, "Request sense");
360 mdelay(1);
361 if (!r)
362 pt_completion(tape, buf, "Request sense");
363
364 tape->last_sense = -1;
365 if (!r) {
366 if (!quiet)
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);
371 }
372}
373
374static int pt_atapi(struct pt_unit *tape, char *cmd, int dlen, char *buf, char *fun)
375{
376 int r;
377
378 r = pt_command(tape, cmd, dlen, fun);
379 mdelay(1);
380 if (!r)
381 r = pt_completion(tape, buf, fun);
382 if (r)
383 pt_req_sense(tape, !fun);
384
385 return r;
386}
387
388static void pt_sleep(int cs)
389{
390 schedule_timeout_interruptible(cs);
391}
392
393static int pt_poll_dsc(struct pt_unit *tape, int pause, int tmo, char *msg)
394{
395 struct pi_adapter *pi = tape->pi;
396 int k, e, s;
397
398 k = 0;
399 e = 0;
400 s = 0;
401 while (k < tmo) {
402 pt_sleep(pause);
403 k++;
404 pi_connect(pi);
405 write_reg(pi, 6, DRIVE(tape));
406 s = read_reg(pi, 7);
407 e = read_reg(pi, 1);
408 pi_disconnect(pi);
409 if (s & (STAT_ERR | STAT_SEEK))
410 break;
411 }
412 if ((k >= tmo) || (s & STAT_ERR)) {
413 if (k >= tmo)
414 printk("%s: %s DSC timeout\n", tape->name, msg);
415 else
416 printk("%s: %s stat=0x%x err=0x%x\n", tape->name, msg, s,
417 e);
418 pt_req_sense(tape, 0);
419 return 0;
420 }
421 return 1;
422}
423
424static void pt_media_access_cmd(struct pt_unit *tape, int tmo, char *cmd, char *fun)
425{
426 if (pt_command(tape, cmd, 0, fun)) {
427 pt_req_sense(tape, 0);
428 return;
429 }
430 pi_disconnect(tape->pi);
431 pt_poll_dsc(tape, HZ, tmo, fun);
432}
433
434static void pt_rewind(struct pt_unit *tape)
435{
436 char rw_cmd[12] = { ATAPI_REWIND, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
437
438 pt_media_access_cmd(tape, PT_REWIND_TMO, rw_cmd, "rewind");
439}
440
441static void pt_write_fm(struct pt_unit *tape)
442{
443 char wm_cmd[12] = { ATAPI_WFM, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0 };
444
445 pt_media_access_cmd(tape, PT_TMO, wm_cmd, "write filemark");
446}
447
448#define DBMSG(msg) ((verbose>1)?(msg):NULL)
449
450static int pt_reset(struct pt_unit *tape)
451{
452 struct pi_adapter *pi = tape->pi;
453 int i, k, flg;
454 int expect[5] = { 1, 1, 1, 0x14, 0xeb };
455
456 pi_connect(pi);
457 write_reg(pi, 6, DRIVE(tape));
458 write_reg(pi, 7, 8);
459
460 pt_sleep(20 * HZ / 1000);
461
462 k = 0;
463 while ((k++ < PT_RESET_TMO) && (status_reg(pi) & STAT_BUSY))
464 pt_sleep(HZ / 10);
465
466 flg = 1;
467 for (i = 0; i < 5; i++)
468 flg &= (read_reg(pi, i + 1) == expect[i]);
469
470 if (verbose) {
471 printk("%s: Reset (%d) signature = ", tape->name, k);
472 for (i = 0; i < 5; i++)
473 printk("%3x", read_reg(pi, i + 1));
474 if (!flg)
475 printk(" (incorrect)");
476 printk("\n");
477 }
478
479 pi_disconnect(pi);
480 return flg - 1;
481}
482
483static int pt_ready_wait(struct pt_unit *tape, int tmo)
484{
485 char tr_cmd[12] = { ATAPI_TEST_READY, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
486 int k, p;
487
488 k = 0;
489 while (k < tmo) {
490 tape->last_sense = 0;
491 pt_atapi(tape, tr_cmd, 0, NULL, DBMSG("test unit ready"));
492 p = tape->last_sense;
493 if (!p)
494 return 0;
495 if (!(((p & 0xffff) == 0x0402) || ((p & 0xff) == 6)))
496 return p;
497 k++;
498 pt_sleep(HZ);
499 }
500 return 0x000020; /* timeout */
501}
502
503static void xs(char *buf, char *targ, int offs, int len)
504{
505 int j, k, l;
506
507 j = 0;
508 l = 0;
509 for (k = 0; k < len; k++)
510 if ((buf[k + offs] != 0x20) || (buf[k + offs] != l))
511 l = targ[j++] = buf[k + offs];
512 if (l == 0x20)
513 j--;
514 targ[j] = 0;
515}
516
517static int xn(char *buf, int offs, int size)
518{
519 int v, k;
520
521 v = 0;
522 for (k = 0; k < size; k++)
523 v = v * 256 + (buf[k + offs] & 0xff);
524 return v;
525}
526
527static int pt_identify(struct pt_unit *tape)
528{
529 int dt, s;
530 char *ms[2] = { "master", "slave" };
531 char mf[10], id[18];
532 char id_cmd[12] = { ATAPI_IDENTIFY, 0, 0, 0, 36, 0, 0, 0, 0, 0, 0, 0 };
533 char ms_cmd[12] =
534 { ATAPI_MODE_SENSE, 0, 0x2a, 0, 36, 0, 0, 0, 0, 0, 0, 0 };
535 char ls_cmd[12] =
536 { ATAPI_LOG_SENSE, 0, 0x71, 0, 0, 0, 0, 0, 36, 0, 0, 0 };
537 char buf[36];
538
539 s = pt_atapi(tape, id_cmd, 36, buf, "identify");
540 if (s)
541 return -1;
542
543 dt = buf[0] & 0x1f;
544 if (dt != 1) {
545 if (verbose)
546 printk("%s: Drive %d, unsupported type %d\n",
547 tape->name, tape->drive, dt);
548 return -1;
549 }
550
551 xs(buf, mf, 8, 8);
552 xs(buf, id, 16, 16);
553
554 tape->flags = 0;
555 tape->capacity = 0;
556 tape->bs = 0;
557
558 if (!pt_ready_wait(tape, PT_READY_TMO))
559 tape->flags |= PT_MEDIA;
560
561 if (!pt_atapi(tape, ms_cmd, 36, buf, "mode sense")) {
562 if (!(buf[2] & 0x80))
563 tape->flags |= PT_WRITE_OK;
564 tape->bs = xn(buf, 10, 2);
565 }
566
567 if (!pt_atapi(tape, ls_cmd, 36, buf, "log sense"))
568 tape->capacity = xn(buf, 24, 4);
569
570 printk("%s: %s %s, %s", tape->name, mf, id, ms[tape->drive]);
571 if (!(tape->flags & PT_MEDIA))
572 printk(", no media\n");
573 else {
574 if (!(tape->flags & PT_WRITE_OK))
575 printk(", RO");
576 printk(", blocksize %d, %d MB\n", tape->bs, tape->capacity / 1024);
577 }
578
579 return 0;
580}
581
582
583/*
584 * returns 0, with id set if drive is detected
585 * -1, if drive detection failed
586 */
587static int pt_probe(struct pt_unit *tape)
588{
589 if (tape->drive == -1) {
590 for (tape->drive = 0; tape->drive <= 1; tape->drive++)
591 if (!pt_reset(tape))
592 return pt_identify(tape);
593 } else {
594 if (!pt_reset(tape))
595 return pt_identify(tape);
596 }
597 return -1;
598}
599
600static int pt_detect(void)
601{
602 struct pt_unit *tape;
603 int specified = 0, found = 0;
604 int unit;
605
606 printk("%s: %s version %s, major %d\n", name, name, PT_VERSION, major);
607
608 specified = 0;
609 for (unit = 0; unit < PT_UNITS; unit++) {
610 struct pt_unit *tape = &pt[unit];
611 tape->pi = &tape->pia;
612 atomic_set(&tape->available, 1);
613 tape->flags = 0;
614 tape->last_sense = 0;
615 tape->present = 0;
616 tape->bufptr = NULL;
617 tape->drive = DU[D_SLV];
618 snprintf(tape->name, PT_NAMELEN, "%s%d", name, unit);
619 if (!DU[D_PRT])
620 continue;
621 specified++;
622 if (pi_init(tape->pi, 0, DU[D_PRT], DU[D_MOD], DU[D_UNI],
623 DU[D_PRO], DU[D_DLY], pt_scratch, PI_PT,
624 verbose, tape->name)) {
625 if (!pt_probe(tape)) {
626 tape->present = 1;
627 found++;
628 } else
629 pi_release(tape->pi);
630 }
631 }
632 if (specified == 0) {
633 tape = pt;
634 if (pi_init(tape->pi, 1, -1, -1, -1, -1, -1, pt_scratch,
635 PI_PT, verbose, tape->name)) {
636 if (!pt_probe(tape)) {
637 tape->present = 1;
638 found++;
639 } else
640 pi_release(tape->pi);
641 }
642
643 }
644 if (found)
645 return 0;
646
647 printk("%s: No ATAPI tape drive detected\n", name);
648 return -1;
649}
650
651static int pt_open(struct inode *inode, struct file *file)
652{
653 int unit = iminor(inode) & 0x7F;
654 struct pt_unit *tape = pt + unit;
655 int err;
656
657 mutex_lock(&pt_mutex);
658 if (unit >= PT_UNITS || (!tape->present)) {
659 mutex_unlock(&pt_mutex);
660 return -ENODEV;
661 }
662
663 err = -EBUSY;
664 if (!atomic_dec_and_test(&tape->available))
665 goto out;
666
667 pt_identify(tape);
668
669 err = -ENODEV;
670 if (!(tape->flags & PT_MEDIA))
671 goto out;
672
673 err = -EROFS;
674 if ((!(tape->flags & PT_WRITE_OK)) && (file->f_mode & FMODE_WRITE))
675 goto out;
676
677 if (!(iminor(inode) & 128))
678 tape->flags |= PT_REWIND;
679
680 err = -ENOMEM;
681 tape->bufptr = kmalloc(PT_BUFSIZE, GFP_KERNEL);
682 if (tape->bufptr == NULL) {
683 printk("%s: buffer allocation failed\n", tape->name);
684 goto out;
685 }
686
687 file->private_data = tape;
688 mutex_unlock(&pt_mutex);
689 return 0;
690
691out:
692 atomic_inc(&tape->available);
693 mutex_unlock(&pt_mutex);
694 return err;
695}
696
697static long pt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
698{
699 struct pt_unit *tape = file->private_data;
700 struct mtop __user *p = (void __user *)arg;
701 struct mtop mtop;
702
703 switch (cmd) {
704 case MTIOCTOP:
705 if (copy_from_user(&mtop, p, sizeof(struct mtop)))
706 return -EFAULT;
707
708 switch (mtop.mt_op) {
709
710 case MTREW:
711 mutex_lock(&pt_mutex);
712 pt_rewind(tape);
713 mutex_unlock(&pt_mutex);
714 return 0;
715
716 case MTWEOF:
717 mutex_lock(&pt_mutex);
718 pt_write_fm(tape);
719 mutex_unlock(&pt_mutex);
720 return 0;
721
722 default:
723 /* FIXME: rate limit ?? */
724 printk(KERN_DEBUG "%s: Unimplemented mt_op %d\n", tape->name,
725 mtop.mt_op);
726 return -EINVAL;
727 }
728
729 default:
730 return -ENOTTY;
731 }
732}
733
734static int
735pt_release(struct inode *inode, struct file *file)
736{
737 struct pt_unit *tape = file->private_data;
738
739 if (atomic_read(&tape->available) > 1)
740 return -EINVAL;
741
742 if (tape->flags & PT_WRITING)
743 pt_write_fm(tape);
744
745 if (tape->flags & PT_REWIND)
746 pt_rewind(tape);
747
748 kfree(tape->bufptr);
749 tape->bufptr = NULL;
750
751 atomic_inc(&tape->available);
752
753 return 0;
754
755}
756
757static ssize_t pt_read(struct file *filp, char __user *buf, size_t count, loff_t * ppos)
758{
759 struct pt_unit *tape = filp->private_data;
760 struct pi_adapter *pi = tape->pi;
761 char rd_cmd[12] = { ATAPI_READ_6, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
762 int k, n, r, p, s, t, b;
763
764 if (!(tape->flags & (PT_READING | PT_WRITING))) {
765 tape->flags |= PT_READING;
766 if (pt_atapi(tape, rd_cmd, 0, NULL, "start read-ahead"))
767 return -EIO;
768 } else if (tape->flags & PT_WRITING)
769 return -EIO;
770
771 if (tape->flags & PT_EOF)
772 return 0;
773
774 t = 0;
775
776 while (count > 0) {
777
778 if (!pt_poll_dsc(tape, HZ / 100, PT_TMO, "read"))
779 return -EIO;
780
781 n = count;
782 if (n > 32768)
783 n = 32768; /* max per command */
784 b = (n - 1 + tape->bs) / tape->bs;
785 n = b * tape->bs; /* rounded up to even block */
786
787 rd_cmd[4] = b;
788
789 r = pt_command(tape, rd_cmd, n, "read");
790
791 mdelay(1);
792
793 if (r) {
794 pt_req_sense(tape, 0);
795 return -EIO;
796 }
797
798 while (1) {
799
800 r = pt_wait(tape, STAT_BUSY,
801 STAT_DRQ | STAT_ERR | STAT_READY,
802 DBMSG("read DRQ"), "");
803
804 if (r & STAT_SENSE) {
805 pi_disconnect(pi);
806 pt_req_sense(tape, 0);
807 return -EIO;
808 }
809
810 if (r)
811 tape->flags |= PT_EOF;
812
813 s = read_reg(pi, 7);
814
815 if (!(s & STAT_DRQ))
816 break;
817
818 n = (read_reg(pi, 4) + 256 * read_reg(pi, 5));
819 p = (read_reg(pi, 2) & 3);
820 if (p != 2) {
821 pi_disconnect(pi);
822 printk("%s: Phase error on read: %d\n", tape->name,
823 p);
824 return -EIO;
825 }
826
827 while (n > 0) {
828 k = n;
829 if (k > PT_BUFSIZE)
830 k = PT_BUFSIZE;
831 pi_read_block(pi, tape->bufptr, k);
832 n -= k;
833 b = k;
834 if (b > count)
835 b = count;
836 if (copy_to_user(buf + t, tape->bufptr, b)) {
837 pi_disconnect(pi);
838 return -EFAULT;
839 }
840 t += b;
841 count -= b;
842 }
843
844 }
845 pi_disconnect(pi);
846 if (tape->flags & PT_EOF)
847 break;
848 }
849
850 return t;
851
852}
853
854static ssize_t pt_write(struct file *filp, const char __user *buf, size_t count, loff_t * ppos)
855{
856 struct pt_unit *tape = filp->private_data;
857 struct pi_adapter *pi = tape->pi;
858 char wr_cmd[12] = { ATAPI_WRITE_6, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
859 int k, n, r, p, s, t, b;
860
861 if (!(tape->flags & PT_WRITE_OK))
862 return -EROFS;
863
864 if (!(tape->flags & (PT_READING | PT_WRITING))) {
865 tape->flags |= PT_WRITING;
866 if (pt_atapi
867 (tape, wr_cmd, 0, NULL, "start buffer-available mode"))
868 return -EIO;
869 } else if (tape->flags & PT_READING)
870 return -EIO;
871
872 if (tape->flags & PT_EOF)
873 return -ENOSPC;
874
875 t = 0;
876
877 while (count > 0) {
878
879 if (!pt_poll_dsc(tape, HZ / 100, PT_TMO, "write"))
880 return -EIO;
881
882 n = count;
883 if (n > 32768)
884 n = 32768; /* max per command */
885 b = (n - 1 + tape->bs) / tape->bs;
886 n = b * tape->bs; /* rounded up to even block */
887
888 wr_cmd[4] = b;
889
890 r = pt_command(tape, wr_cmd, n, "write");
891
892 mdelay(1);
893
894 if (r) { /* error delivering command only */
895 pt_req_sense(tape, 0);
896 return -EIO;
897 }
898
899 while (1) {
900
901 r = pt_wait(tape, STAT_BUSY,
902 STAT_DRQ | STAT_ERR | STAT_READY,
903 DBMSG("write DRQ"), NULL);
904
905 if (r & STAT_SENSE) {
906 pi_disconnect(pi);
907 pt_req_sense(tape, 0);
908 return -EIO;
909 }
910
911 if (r)
912 tape->flags |= PT_EOF;
913
914 s = read_reg(pi, 7);
915
916 if (!(s & STAT_DRQ))
917 break;
918
919 n = (read_reg(pi, 4) + 256 * read_reg(pi, 5));
920 p = (read_reg(pi, 2) & 3);
921 if (p != 0) {
922 pi_disconnect(pi);
923 printk("%s: Phase error on write: %d \n",
924 tape->name, p);
925 return -EIO;
926 }
927
928 while (n > 0) {
929 k = n;
930 if (k > PT_BUFSIZE)
931 k = PT_BUFSIZE;
932 b = k;
933 if (b > count)
934 b = count;
935 if (copy_from_user(tape->bufptr, buf + t, b)) {
936 pi_disconnect(pi);
937 return -EFAULT;
938 }
939 pi_write_block(pi, tape->bufptr, k);
940 t += b;
941 count -= b;
942 n -= k;
943 }
944
945 }
946 pi_disconnect(pi);
947 if (tape->flags & PT_EOF)
948 break;
949 }
950
951 return t;
952}
953
954static int __init pt_init(void)
955{
956 int unit;
957 int err;
958
959 if (disable) {
960 err = -EINVAL;
961 goto out;
962 }
963
964 if (pt_detect()) {
965 err = -ENODEV;
966 goto out;
967 }
968
969 err = register_chrdev(major, name, &pt_fops);
970 if (err < 0) {
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)
974 pi_release(pt[unit].pi);
975 goto out;
976 }
977 major = err;
978 pt_class = class_create(THIS_MODULE, "pt");
979 if (IS_ERR(pt_class)) {
980 err = PTR_ERR(pt_class);
981 goto out_chrdev;
982 }
983
984 for (unit = 0; unit < PT_UNITS; unit++)
985 if (pt[unit].present) {
986 device_create(pt_class, NULL, MKDEV(major, unit), NULL,
987 "pt%d", unit);
988 device_create(pt_class, NULL, MKDEV(major, unit + 128),
989 NULL, "pt%dn", unit);
990 }
991 goto out;
992
993out_chrdev:
994 unregister_chrdev(major, "pt");
995out:
996 return err;
997}
998
999static void __exit pt_exit(void)
1000{
1001 int unit;
1002 for (unit = 0; unit < PT_UNITS; unit++)
1003 if (pt[unit].present) {
1004 device_destroy(pt_class, MKDEV(major, unit));
1005 device_destroy(pt_class, MKDEV(major, unit + 128));
1006 }
1007 class_destroy(pt_class);
1008 unregister_chrdev(major, name);
1009 for (unit = 0; unit < PT_UNITS; unit++)
1010 if (pt[unit].present)
1011 pi_release(pt[unit].pi);
1012}
1013
1014MODULE_LICENSE("GPL");
1015module_init(pt_init)
1016module_exit(pt_exit)