ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / block / paride / pt.c
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 /* Here are things one can override from the insmod command.
113    Most are autoprobed by paride unless set here.  Verbose is on
114    by default.
115
116 */
117
118 static int verbose = 0;
119 static int major = PT_MAJOR;
120 static char *name = PT_NAME;
121 static int disable = 0;
122
123 static int drive0[6] = { 0, 0, 0, -1, -1, -1 };
124 static int drive1[6] = { 0, 0, 0, -1, -1, -1 };
125 static int drive2[6] = { 0, 0, 0, -1, -1, -1 };
126 static int drive3[6] = { 0, 0, 0, -1, -1, -1 };
127
128 static int (*drives[4])[6] = {&drive0, &drive1, &drive2, &drive3};
129
130 #define D_PRT   0
131 #define D_PRO   1
132 #define D_UNI   2
133 #define D_MOD   3
134 #define D_SLV   4
135 #define D_DLY   5
136
137 #define DU              (*drives[unit])
138
139 /* end of parameters */
140
141 #include <linux/module.h>
142 #include <linux/init.h>
143 #include <linux/fs.h>
144 #include <linux/devfs_fs_kernel.h>
145 #include <linux/delay.h>
146 #include <linux/slab.h>
147 #include <linux/mtio.h>
148
149 #include <asm/uaccess.h>
150
151 #ifndef MODULE
152
153 #include "setup.h"
154
155 static STT pt_stt[5] = {
156         {"drive0", 6, drive0},
157         {"drive1", 6, drive1},
158         {"drive2", 6, drive2},
159         {"drive3", 6, drive3},
160         {"disable", 1, &disable}
161 };
162
163 void
164 pt_setup(char *str, int *ints)
165 {
166         generic_setup(pt_stt, 5, str);
167 }
168
169 #endif
170
171 MODULE_PARM(verbose, "i");
172 MODULE_PARM(major, "i");
173 MODULE_PARM(name, "s");
174 MODULE_PARM(drive0, "1-6i");
175 MODULE_PARM(drive1, "1-6i");
176 MODULE_PARM(drive2, "1-6i");
177 MODULE_PARM(drive3, "1-6i");
178
179 #include "paride.h"
180
181 #define PT_MAX_RETRIES  5
182 #define PT_TMO          3000    /* interrupt timeout in jiffies */
183 #define PT_SPIN_DEL     50      /* spin delay in micro-seconds  */
184 #define PT_RESET_TMO    30      /* 30 seconds */
185 #define PT_READY_TMO    60      /* 60 seconds */
186 #define PT_REWIND_TMO   1200    /* 20 minutes */
187
188 #define PT_SPIN         ((1000000/(HZ*PT_SPIN_DEL))*PT_TMO)
189
190 #define STAT_ERR        0x00001
191 #define STAT_INDEX      0x00002
192 #define STAT_ECC        0x00004
193 #define STAT_DRQ        0x00008
194 #define STAT_SEEK       0x00010
195 #define STAT_WRERR      0x00020
196 #define STAT_READY      0x00040
197 #define STAT_BUSY       0x00080
198 #define STAT_SENSE      0x1f000
199
200 #define ATAPI_TEST_READY        0x00
201 #define ATAPI_REWIND            0x01
202 #define ATAPI_REQ_SENSE         0x03
203 #define ATAPI_READ_6            0x08
204 #define ATAPI_WRITE_6           0x0a
205 #define ATAPI_WFM               0x10
206 #define ATAPI_IDENTIFY          0x12
207 #define ATAPI_MODE_SENSE        0x1a
208 #define ATAPI_LOG_SENSE         0x4d
209
210 static int pt_open(struct inode *inode, struct file *file);
211 static int pt_ioctl(struct inode *inode, struct file *file,
212                     unsigned int cmd, unsigned long arg);
213 static int pt_release(struct inode *inode, struct file *file);
214 static ssize_t pt_read(struct file *filp, char *buf,
215                        size_t count, loff_t * ppos);
216 static ssize_t pt_write(struct file *filp, const char *buf,
217                         size_t count, loff_t * ppos);
218 static int pt_detect(void);
219
220 /* bits in tape->flags */
221
222 #define PT_MEDIA        1
223 #define PT_WRITE_OK     2
224 #define PT_REWIND       4
225 #define PT_WRITING      8
226 #define PT_READING     16
227 #define PT_EOF         32
228
229 #define PT_NAMELEN      8
230 #define PT_BUFSIZE  16384
231
232 struct pt_unit {
233         struct pi_adapter pia;  /* interface to paride layer */
234         struct pi_adapter *pi;
235         int flags;              /* various state flags */
236         int last_sense;         /* result of last request sense */
237         int drive;              /* drive */
238         atomic_t available;     /* 1 if access is available 0 otherwise */
239         int bs;                 /* block size */
240         int capacity;           /* Size of tape in KB */
241         int present;            /* device present ? */
242         char *bufptr;
243         char name[PT_NAMELEN];  /* pf0, pf1, ... */
244 };
245
246 static int pt_identify(struct pt_unit *tape);
247
248 struct pt_unit pt[PT_UNITS];
249
250 static char pt_scratch[512];    /* scratch block buffer */
251
252 /* kernel glue structures */
253
254 static struct file_operations pt_fops = {
255         .owner = THIS_MODULE,
256         .read = pt_read,
257         .write = pt_write,
258         .ioctl = pt_ioctl,
259         .open = pt_open,
260         .release = pt_release,
261 };
262
263 static inline int status_reg(struct pi_adapter *pi)
264 {
265         return pi_read_regr(pi, 1, 6);
266 }
267
268 static inline int read_reg(struct pi_adapter *pi, int reg)
269 {
270         return pi_read_regr(pi, 0, reg);
271 }
272
273 static inline void write_reg(struct pi_adapter *pi, int reg, int val)
274 {
275         pi_write_regr(pi, 0, reg, val);
276 }
277
278 static inline u8 DRIVE(struct pt_unit *tape)
279 {
280         return 0xa0+0x10*tape->drive;
281 }
282
283 static int pt_wait(struct pt_unit *tape, int go, int stop, char *fun, char *msg)
284 {
285         int j, r, e, s, p;
286         struct pi_adapter *pi = tape->pi;
287
288         j = 0;
289         while ((((r = status_reg(pi)) & go) || (stop && (!(r & stop))))
290                && (j++ < PT_SPIN))
291                 udelay(PT_SPIN_DEL);
292
293         if ((r & (STAT_ERR & stop)) || (j >= PT_SPIN)) {
294                 s = read_reg(pi, 7);
295                 e = read_reg(pi, 1);
296                 p = read_reg(pi, 2);
297                 if (j >= PT_SPIN)
298                         e |= 0x100;
299                 if (fun)
300                         printk("%s: %s %s: alt=0x%x stat=0x%x err=0x%x"
301                                " loop=%d phase=%d\n",
302                                tape->name, fun, msg, r, s, e, j, p);
303                 return (e << 8) + s;
304         }
305         return 0;
306 }
307
308 static int pt_command(struct pt_unit *tape, char *cmd, int dlen, char *fun)
309 {
310         struct pi_adapter *pi = tape->pi;
311         pi_connect(pi);
312
313         write_reg(pi, 6, DRIVE(tape));
314
315         if (pt_wait(tape, STAT_BUSY | STAT_DRQ, 0, fun, "before command")) {
316                 pi_disconnect(pi);
317                 return -1;
318         }
319
320         write_reg(pi, 4, dlen % 256);
321         write_reg(pi, 5, dlen / 256);
322         write_reg(pi, 7, 0xa0); /* ATAPI packet command */
323
324         if (pt_wait(tape, STAT_BUSY, STAT_DRQ, fun, "command DRQ")) {
325                 pi_disconnect(pi);
326                 return -1;
327         }
328
329         if (read_reg(pi, 2) != 1) {
330                 printk("%s: %s: command phase error\n", tape->name, fun);
331                 pi_disconnect(pi);
332                 return -1;
333         }
334
335         pi_write_block(pi, cmd, 12);
336
337         return 0;
338 }
339
340 static int pt_completion(struct pt_unit *tape, char *buf, char *fun)
341 {
342         struct pi_adapter *pi = tape->pi;
343         int r, s, n, p;
344
345         r = pt_wait(tape, STAT_BUSY, STAT_DRQ | STAT_READY | STAT_ERR,
346                     fun, "completion");
347
348         if (read_reg(pi, 7) & STAT_DRQ) {
349                 n = (((read_reg(pi, 4) + 256 * read_reg(pi, 5)) +
350                       3) & 0xfffc);
351                 p = read_reg(pi, 2) & 3;
352                 if (p == 0)
353                         pi_write_block(pi, buf, n);
354                 if (p == 2)
355                         pi_read_block(pi, buf, n);
356         }
357
358         s = pt_wait(tape, STAT_BUSY, STAT_READY | STAT_ERR, fun, "data done");
359
360         pi_disconnect(pi);
361
362         return (r ? r : s);
363 }
364
365 static void pt_req_sense(struct pt_unit *tape, int quiet)
366 {
367         char rs_cmd[12] = { ATAPI_REQ_SENSE, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0 };
368         char buf[16];
369         int r;
370
371         r = pt_command(tape, rs_cmd, 16, "Request sense");
372         mdelay(1);
373         if (!r)
374                 pt_completion(tape, buf, "Request sense");
375
376         tape->last_sense = -1;
377         if (!r) {
378                 if (!quiet)
379                         printk("%s: Sense key: %x, ASC: %x, ASQ: %x\n",
380                                tape->name, buf[2] & 0xf, buf[12], buf[13]);
381                 tape->last_sense = (buf[2] & 0xf) | ((buf[12] & 0xff) << 8)
382                     | ((buf[13] & 0xff) << 16);
383         }
384 }
385
386 static int pt_atapi(struct pt_unit *tape, char *cmd, int dlen, char *buf, char *fun)
387 {
388         int r;
389
390         r = pt_command(tape, cmd, dlen, fun);
391         mdelay(1);
392         if (!r)
393                 r = pt_completion(tape, buf, fun);
394         if (r)
395                 pt_req_sense(tape, !fun);
396
397         return r;
398 }
399
400 static void pt_sleep(int cs)
401 {
402         current->state = TASK_INTERRUPTIBLE;
403         schedule_timeout(cs);
404 }
405
406 static int pt_poll_dsc(struct pt_unit *tape, int pause, int tmo, char *msg)
407 {
408         struct pi_adapter *pi = tape->pi;
409         int k, e, s;
410
411         k = 0;
412         e = 0;
413         s = 0;
414         while (k < tmo) {
415                 pt_sleep(pause);
416                 k++;
417                 pi_connect(pi);
418                 write_reg(pi, 6, DRIVE(tape));
419                 s = read_reg(pi, 7);
420                 e = read_reg(pi, 1);
421                 pi_disconnect(pi);
422                 if (s & (STAT_ERR | STAT_SEEK))
423                         break;
424         }
425         if ((k >= tmo) || (s & STAT_ERR)) {
426                 if (k >= tmo)
427                         printk("%s: %s DSC timeout\n", tape->name, msg);
428                 else
429                         printk("%s: %s stat=0x%x err=0x%x\n", tape->name, msg, s,
430                                e);
431                 pt_req_sense(tape, 0);
432                 return 0;
433         }
434         return 1;
435 }
436
437 static void pt_media_access_cmd(struct pt_unit *tape, int tmo, char *cmd, char *fun)
438 {
439         if (pt_command(tape, cmd, 0, fun)) {
440                 pt_req_sense(tape, 0);
441                 return;
442         }
443         pi_disconnect(tape->pi);
444         pt_poll_dsc(tape, HZ, tmo, fun);
445 }
446
447 static void pt_rewind(struct pt_unit *tape)
448 {
449         char rw_cmd[12] = { ATAPI_REWIND, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
450
451         pt_media_access_cmd(tape, PT_REWIND_TMO, rw_cmd, "rewind");
452 }
453
454 static void pt_write_fm(struct pt_unit *tape)
455 {
456         char wm_cmd[12] = { ATAPI_WFM, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0 };
457
458         pt_media_access_cmd(tape, PT_TMO, wm_cmd, "write filemark");
459 }
460
461 #define DBMSG(msg)      ((verbose>1)?(msg):NULL)
462
463 static int pt_reset(struct pt_unit *tape)
464 {
465         struct pi_adapter *pi = tape->pi;
466         int i, k, flg;
467         int expect[5] = { 1, 1, 1, 0x14, 0xeb };
468
469         pi_connect(pi);
470         write_reg(pi, 6, DRIVE(tape));
471         write_reg(pi, 7, 8);
472
473         pt_sleep(20 * HZ / 1000);
474
475         k = 0;
476         while ((k++ < PT_RESET_TMO) && (status_reg(pi) & STAT_BUSY))
477                 pt_sleep(HZ / 10);
478
479         flg = 1;
480         for (i = 0; i < 5; i++)
481                 flg &= (read_reg(pi, i + 1) == expect[i]);
482
483         if (verbose) {
484                 printk("%s: Reset (%d) signature = ", tape->name, k);
485                 for (i = 0; i < 5; i++)
486                         printk("%3x", read_reg(pi, i + 1));
487                 if (!flg)
488                         printk(" (incorrect)");
489                 printk("\n");
490         }
491
492         pi_disconnect(pi);
493         return flg - 1;
494 }
495
496 static int pt_ready_wait(struct pt_unit *tape, int tmo)
497 {
498         char tr_cmd[12] = { ATAPI_TEST_READY, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
499         int k, p;
500
501         k = 0;
502         while (k < tmo) {
503                 tape->last_sense = 0;
504                 pt_atapi(tape, tr_cmd, 0, NULL, DBMSG("test unit ready"));
505                 p = tape->last_sense;
506                 if (!p)
507                         return 0;
508                 if (!(((p & 0xffff) == 0x0402) || ((p & 0xff) == 6)))
509                         return p;
510                 k++;
511                 pt_sleep(HZ);
512         }
513         return 0x000020;        /* timeout */
514 }
515
516 static void xs(char *buf, char *targ, int offs, int len)
517 {
518         int j, k, l;
519
520         j = 0;
521         l = 0;
522         for (k = 0; k < len; k++)
523                 if ((buf[k + offs] != 0x20) || (buf[k + offs] != l))
524                         l = targ[j++] = buf[k + offs];
525         if (l == 0x20)
526                 j--;
527         targ[j] = 0;
528 }
529
530 static int xn(char *buf, int offs, int size)
531 {
532         int v, k;
533
534         v = 0;
535         for (k = 0; k < size; k++)
536                 v = v * 256 + (buf[k + offs] & 0xff);
537         return v;
538 }
539
540 static int pt_identify(struct pt_unit *tape)
541 {
542         int dt, s;
543         char *ms[2] = { "master", "slave" };
544         char mf[10], id[18];
545         char id_cmd[12] = { ATAPI_IDENTIFY, 0, 0, 0, 36, 0, 0, 0, 0, 0, 0, 0 };
546         char ms_cmd[12] =
547             { ATAPI_MODE_SENSE, 0, 0x2a, 0, 36, 0, 0, 0, 0, 0, 0, 0 };
548         char ls_cmd[12] =
549             { ATAPI_LOG_SENSE, 0, 0x71, 0, 0, 0, 0, 0, 36, 0, 0, 0 };
550         char buf[36];
551
552         s = pt_atapi(tape, id_cmd, 36, buf, "identify");
553         if (s)
554                 return -1;
555
556         dt = buf[0] & 0x1f;
557         if (dt != 1) {
558                 if (verbose)
559                         printk("%s: Drive %d, unsupported type %d\n",
560                                tape->name, tape->drive, dt);
561                 return -1;
562         }
563
564         xs(buf, mf, 8, 8);
565         xs(buf, id, 16, 16);
566
567         tape->flags = 0;
568         tape->capacity = 0;
569         tape->bs = 0;
570
571         if (!pt_ready_wait(tape, PT_READY_TMO))
572                 tape->flags |= PT_MEDIA;
573
574         if (!pt_atapi(tape, ms_cmd, 36, buf, "mode sense")) {
575                 if (!(buf[2] & 0x80))
576                         tape->flags |= PT_WRITE_OK;
577                 tape->bs = xn(buf, 10, 2);
578         }
579
580         if (!pt_atapi(tape, ls_cmd, 36, buf, "log sense"))
581                 tape->capacity = xn(buf, 24, 4);
582
583         printk("%s: %s %s, %s", tape->name, mf, id, ms[tape->drive]);
584         if (!(tape->flags & PT_MEDIA))
585                 printk(", no media\n");
586         else {
587                 if (!(tape->flags & PT_WRITE_OK))
588                         printk(", RO");
589                 printk(", blocksize %d, %d MB\n", tape->bs, tape->capacity / 1024);
590         }
591
592         return 0;
593 }
594
595
596 /*
597  * returns  0, with id set if drive is detected
598  *         -1, if drive detection failed
599  */
600 static int pt_probe(struct pt_unit *tape)
601 {
602         if (tape->drive == -1) {
603                 for (tape->drive = 0; tape->drive <= 1; tape->drive++)
604                         if (!pt_reset(tape))
605                                 return pt_identify(tape);
606         } else {
607                 if (!pt_reset(tape))
608                         return pt_identify(tape);
609         }
610         return -1;
611 }
612
613 static int pt_detect(void)
614 {
615         struct pt_unit *tape;
616         int specified = 0, found = 0;
617         int unit;
618
619         printk("%s: %s version %s, major %d\n", name, name, PT_VERSION, major);
620
621         specified = 0;
622         for (unit = 0; unit < PT_UNITS; unit++) {
623                 struct pt_unit *tape = &pt[unit];
624                 tape->pi = &tape->pia;
625                 atomic_set(&tape->available, 1);
626                 tape->flags = 0;
627                 tape->last_sense = 0;
628                 tape->present = 0;
629                 tape->bufptr = NULL;
630                 tape->drive = DU[D_SLV];
631                 snprintf(tape->name, PT_NAMELEN, "%s%d", name, unit);
632                 if (!DU[D_PRT])
633                         continue;
634                 specified++;
635                 if (pi_init(tape->pi, 0, DU[D_PRT], DU[D_MOD], DU[D_UNI],
636                      DU[D_PRO], DU[D_DLY], pt_scratch, PI_PT,
637                      verbose, tape->name)) {
638                         if (!pt_probe(tape)) {
639                                 tape->present = 1;
640                                 found++;
641                         } else
642                                 pi_release(tape->pi);
643                 }
644         }
645         if (specified == 0) {
646                 tape = pt;
647                 if (pi_init(tape->pi, 1, -1, -1, -1, -1, -1, pt_scratch,
648                             PI_PT, verbose, tape->name)) {
649                         if (!pt_probe(tape)) {
650                                 tape->present = 1;
651                                 found++;
652                         } else
653                                 pi_release(tape->pi);
654                 }
655
656         }
657         if (found)
658                 return 0;
659
660         printk("%s: No ATAPI tape drive detected\n", name);
661         return -1;
662 }
663
664 static int pt_open(struct inode *inode, struct file *file)
665 {
666         int unit = iminor(inode) & 0x7F;
667         struct pt_unit *tape = pt + unit;
668         int err;
669
670         if (unit >= PT_UNITS || (!tape->present))
671                 return -ENODEV;
672
673         err = -EBUSY;
674         if (!atomic_dec_and_test(&tape->available))
675                 goto out;
676
677         pt_identify(tape);
678
679         err = -ENODEV;
680         if (!tape->flags & PT_MEDIA)
681                 goto out;
682
683         err = -EROFS;
684         if ((!tape->flags & PT_WRITE_OK) && (file->f_mode & 2))
685                 goto out;
686
687         if (!(iminor(inode) & 128))
688                 tape->flags |= PT_REWIND;
689
690         err = -ENOMEM;
691         tape->bufptr = kmalloc(PT_BUFSIZE, GFP_KERNEL);
692         if (tape->bufptr == NULL) {
693                 printk("%s: buffer allocation failed\n", tape->name);
694                 goto out;
695         }
696
697         file->private_data = tape;
698         return 0;
699
700 out:
701         atomic_inc(&tape->available);
702         return err;
703 }
704
705 static int pt_ioctl(struct inode *inode, struct file *file,
706          unsigned int cmd, unsigned long arg)
707 {
708         struct pt_unit *tape = file->private_data;
709         struct mtop mtop;
710
711         switch (cmd) {
712         case MTIOCTOP:
713                 if (copy_from_user((char *) &mtop, (char *) arg,
714                                    sizeof (struct mtop)))
715                         return -EFAULT;
716
717                 switch (mtop.mt_op) {
718
719                 case MTREW:
720                         pt_rewind(tape);
721                         return 0;
722
723                 case MTWEOF:
724                         pt_write_fm(tape);
725                         return 0;
726
727                 default:
728                         printk("%s: Unimplemented mt_op %d\n", tape->name,
729                                mtop.mt_op);
730                         return -EINVAL;
731                 }
732
733         default:
734                 printk("%s: Unimplemented ioctl 0x%x\n", tape->name, cmd);
735                 return -EINVAL;
736
737         }
738 }
739
740 static int
741 pt_release(struct inode *inode, struct file *file)
742 {
743         struct pt_unit *tape = file->private_data;
744
745         if (atomic_read(&tape->available) > 1)
746                 return -EINVAL;
747
748         if (tape->flags & PT_WRITING)
749                 pt_write_fm(tape);
750
751         if (tape->flags & PT_REWIND)
752                 pt_rewind(tape);
753
754         kfree(tape->bufptr);
755         tape->bufptr = NULL;
756
757         atomic_inc(&tape->available);
758
759         return 0;
760
761 }
762
763 static ssize_t pt_read(struct file *filp, char *buf, size_t count, loff_t * ppos)
764 {
765         struct pt_unit *tape = filp->private_data;
766         struct pi_adapter *pi = tape->pi;
767         char rd_cmd[12] = { ATAPI_READ_6, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
768         int k, n, r, p, s, t, b;
769
770         if (!(tape->flags & (PT_READING | PT_WRITING))) {
771                 tape->flags |= PT_READING;
772                 if (pt_atapi(tape, rd_cmd, 0, NULL, "start read-ahead"))
773                         return -EIO;
774         } else if (tape->flags & PT_WRITING)
775                 return -EIO;
776
777         if (tape->flags & PT_EOF)
778                 return 0;
779
780         t = 0;
781
782         while (count > 0) {
783
784                 if (!pt_poll_dsc(tape, HZ / 100, PT_TMO, "read"))
785                         return -EIO;
786
787                 n = count;
788                 if (n > 32768)
789                         n = 32768;      /* max per command */
790                 b = (n - 1 + tape->bs) / tape->bs;
791                 n = b * tape->bs;       /* rounded up to even block */
792
793                 rd_cmd[4] = b;
794
795                 r = pt_command(tape, rd_cmd, n, "read");
796
797                 mdelay(1);
798
799                 if (r) {
800                         pt_req_sense(tape, 0);
801                         return -EIO;
802                 }
803
804                 while (1) {
805
806                         r = pt_wait(tape, STAT_BUSY,
807                                     STAT_DRQ | STAT_ERR | STAT_READY,
808                                     DBMSG("read DRQ"), "");
809
810                         if (r & STAT_SENSE) {
811                                 pi_disconnect(pi);
812                                 pt_req_sense(tape, 0);
813                                 return -EIO;
814                         }
815
816                         if (r)
817                                 tape->flags |= PT_EOF;
818
819                         s = read_reg(pi, 7);
820
821                         if (!(s & STAT_DRQ))
822                                 break;
823
824                         n = (read_reg(pi, 4) + 256 * read_reg(pi, 5));
825                         p = (read_reg(pi, 2) & 3);
826                         if (p != 2) {
827                                 pi_disconnect(pi);
828                                 printk("%s: Phase error on read: %d\n", tape->name,
829                                        p);
830                                 return -EIO;
831                         }
832
833                         while (n > 0) {
834                                 k = n;
835                                 if (k > PT_BUFSIZE)
836                                         k = PT_BUFSIZE;
837                                 pi_read_block(pi, tape->bufptr, k);
838                                 n -= k;
839                                 b = k;
840                                 if (b > count)
841                                         b = count;
842                                 if (copy_to_user(buf + t, tape->bufptr, b)) {
843                                         pi_disconnect(pi);
844                                         return -EFAULT;
845                                 }
846                                 t += b;
847                                 count -= b;
848                         }
849
850                 }
851                 pi_disconnect(pi);
852                 if (tape->flags & PT_EOF)
853                         break;
854         }
855
856         return t;
857
858 }
859
860 static ssize_t pt_write(struct file *filp, const char *buf, size_t count, loff_t * ppos)
861 {
862         struct pt_unit *tape = filp->private_data;
863         struct pi_adapter *pi = tape->pi;
864         char wr_cmd[12] = { ATAPI_WRITE_6, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
865         int k, n, r, p, s, t, b;
866
867         if (!(tape->flags & PT_WRITE_OK))
868                 return -EROFS;
869
870         if (!(tape->flags & (PT_READING | PT_WRITING))) {
871                 tape->flags |= PT_WRITING;
872                 if (pt_atapi
873                     (tape, wr_cmd, 0, NULL, "start buffer-available mode"))
874                         return -EIO;
875         } else if (tape->flags & PT_READING)
876                 return -EIO;
877
878         if (tape->flags & PT_EOF)
879                 return -ENOSPC;
880
881         t = 0;
882
883         while (count > 0) {
884
885                 if (!pt_poll_dsc(tape, HZ / 100, PT_TMO, "write"))
886                         return -EIO;
887
888                 n = count;
889                 if (n > 32768)
890                         n = 32768;      /* max per command */
891                 b = (n - 1 + tape->bs) / tape->bs;
892                 n = b * tape->bs;       /* rounded up to even block */
893
894                 wr_cmd[4] = b;
895
896                 r = pt_command(tape, wr_cmd, n, "write");
897
898                 mdelay(1);
899
900                 if (r) {        /* error delivering command only */
901                         pt_req_sense(tape, 0);
902                         return -EIO;
903                 }
904
905                 while (1) {
906
907                         r = pt_wait(tape, STAT_BUSY,
908                                     STAT_DRQ | STAT_ERR | STAT_READY,
909                                     DBMSG("write DRQ"), NULL);
910
911                         if (r & STAT_SENSE) {
912                                 pi_disconnect(pi);
913                                 pt_req_sense(tape, 0);
914                                 return -EIO;
915                         }
916
917                         if (r)
918                                 tape->flags |= PT_EOF;
919
920                         s = read_reg(pi, 7);
921
922                         if (!(s & STAT_DRQ))
923                                 break;
924
925                         n = (read_reg(pi, 4) + 256 * read_reg(pi, 5));
926                         p = (read_reg(pi, 2) & 3);
927                         if (p != 0) {
928                                 pi_disconnect(pi);
929                                 printk("%s: Phase error on write: %d \n",
930                                        tape->name, p);
931                                 return -EIO;
932                         }
933
934                         while (n > 0) {
935                                 k = n;
936                                 if (k > PT_BUFSIZE)
937                                         k = PT_BUFSIZE;
938                                 b = k;
939                                 if (b > count)
940                                         b = count;
941                                 if (copy_from_user(tape->bufptr, buf + t, b)) {
942                                         pi_disconnect(pi);
943                                         return -EFAULT;
944                                 }
945                                 pi_write_block(pi, tape->bufptr, k);
946                                 t += b;
947                                 count -= b;
948                                 n -= k;
949                         }
950
951                 }
952                 pi_disconnect(pi);
953                 if (tape->flags & PT_EOF)
954                         break;
955         }
956
957         return t;
958 }
959
960 static int __init pt_init(void)
961 {
962         int unit;
963
964         if (disable)
965                 return -1;
966
967         if (pt_detect())
968                 return -1;
969
970         if (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)
974                                 pi_release(pt[unit].pi);
975                 return -1;
976         }
977
978         devfs_mk_dir("pt");
979         for (unit = 0; unit < PT_UNITS; unit++)
980                 if (pt[unit].present) {
981                         devfs_mk_cdev(MKDEV(major, unit),
982                                       S_IFCHR | S_IRUSR | S_IWUSR,
983                                       "pt/%d", unit);
984                         devfs_mk_cdev(MKDEV(major, unit + 128),
985                                       S_IFCHR | S_IRUSR | S_IWUSR,
986                                       "pt/%dn", unit);
987                 }
988         return 0;
989 }
990
991 static void __exit pt_exit(void)
992 {
993         int unit;
994         for (unit = 0; unit < PT_UNITS; unit++)
995                 if (pt[unit].present) {
996                         devfs_remove("pt/%d", unit);
997                         devfs_remove("pt/%dn", unit);
998                 }
999         devfs_remove("pt");
1000         unregister_chrdev(major, name);
1001         for (unit = 0; unit < PT_UNITS; unit++)
1002                 if (pt[unit].present)
1003                         pi_release(pt[unit].pi);
1004 }
1005
1006 MODULE_LICENSE("GPL");
1007 module_init(pt_init)
1008 module_exit(pt_exit)