ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / block / paride / pg.c
1 /* 
2         pg.c    (c) 1998  Grant R. Guenther <grant@torque.net>
3                           Under the terms of the GNU General Public License.
4
5         The pg driver provides a simple character device interface for
6         sending ATAPI commands to a device.  With the exception of the
7         ATAPI reset operation, all operations are performed by a pair
8         of read and write operations to the appropriate /dev/pgN device.
9         A write operation delivers a command and any outbound data in
10         a single buffer.  Normally, the write will succeed unless the
11         device is offline or malfunctioning, or there is already another
12         command pending.  If the write succeeds, it should be followed
13         immediately by a read operation, to obtain any returned data and
14         status information.  A read will fail if there is no operation
15         in progress.
16
17         As a special case, the device can be reset with a write operation,
18         and in this case, no following read is expected, or permitted.
19
20         There are no ioctl() operations.  Any single operation
21         may transfer at most PG_MAX_DATA bytes.  Note that the driver must
22         copy the data through an internal buffer.  In keeping with all
23         current ATAPI devices, command packets are assumed to be exactly
24         12 bytes in length.
25
26         To permit future changes to this interface, the headers in the
27         read and write buffers contain a single character "magic" flag.
28         Currently this flag must be the character "P".
29
30         By default, the driver will autoprobe for a single parallel
31         port ATAPI device, but if their individual parameters are
32         specified, the driver can handle up to 4 devices.
33
34         To use this device, you must have the following device 
35         special files defined:
36
37                 /dev/pg0 c 97 0
38                 /dev/pg1 c 97 1
39                 /dev/pg2 c 97 2
40                 /dev/pg3 c 97 3
41
42         (You'll need to change the 97 to something else if you use
43         the 'major' parameter to install the driver on a different
44         major number.)
45
46         The behaviour of the pg driver can be altered by setting
47         some parameters from the insmod command line.  The following
48         parameters are adjustable:
49
50             drive0      These four arguments can be arrays of       
51             drive1      1-6 integers as follows:
52             drive2
53             drive3      <prt>,<pro>,<uni>,<mod>,<slv>,<dly>
54
55                         Where,
56
57                 <prt>   is the base of the parallel port address for
58                         the corresponding drive.  (required)
59
60                 <pro>   is the protocol number for the adapter that
61                         supports this drive.  These numbers are
62                         logged by 'paride' when the protocol modules
63                         are initialised.  (0 if not given)
64
65                 <uni>   for those adapters that support chained
66                         devices, this is the unit selector for the
67                         chain of devices on the given port.  It should
68                         be zero for devices that don't support chaining.
69                         (0 if not given)
70
71                 <mod>   this can be -1 to choose the best mode, or one
72                         of the mode numbers supported by the adapter.
73                         (-1 if not given)
74
75                 <slv>   ATAPI devices can be jumpered to master or slave.
76                         Set this to 0 to choose the master drive, 1 to
77                         choose the slave, -1 (the default) to choose the
78                         first drive found.
79
80                 <dly>   some parallel ports require the driver to 
81                         go more slowly.  -1 sets a default value that
82                         should work with the chosen protocol.  Otherwise,
83                         set this to a small integer, the larger it is
84                         the slower the port i/o.  In some cases, setting
85                         this to zero will speed up the device. (default -1)
86
87             major       You may use this parameter to overide the
88                         default major number (97) that this driver
89                         will use.  Be sure to change the device
90                         name as well.
91
92             name        This parameter is a character string that
93                         contains the name the kernel will use for this
94                         device (in /proc output, for instance).
95                         (default "pg").
96
97             verbose     This parameter controls the amount of logging
98                         that is done by the driver.  Set it to 0 for 
99                         quiet operation, to 1 to enable progress
100                         messages while the driver probes for devices,
101                         or to 2 for full debug logging.  (default 0)
102
103         If this driver is built into the kernel, you can use 
104         the following command line parameters, with the same values
105         as the corresponding module parameters listed above:
106
107             pg.drive0
108             pg.drive1
109             pg.drive2
110             pg.drive3
111
112         In addition, you can use the parameter pg.disable to disable
113         the driver entirely.
114
115 */
116
117 /* Changes:
118
119         1.01    GRG 1998.06.16  Bug fixes
120         1.02    GRG 1998.09.24  Added jumbo support
121
122 */
123
124 #define PG_VERSION      "1.02"
125 #define PG_MAJOR        97
126 #define PG_NAME         "pg"
127 #define PG_UNITS        4
128
129 #ifndef PI_PG
130 #define PI_PG   4
131 #endif
132
133 /* Here are things one can override from the insmod command.
134    Most are autoprobed by paride unless set here.  Verbose is 0
135    by default.
136
137 */
138
139 static int verbose = 0;
140 static int major = PG_MAJOR;
141 static char *name = PG_NAME;
142 static int disable = 0;
143
144 static int drive0[6] = { 0, 0, 0, -1, -1, -1 };
145 static int drive1[6] = { 0, 0, 0, -1, -1, -1 };
146 static int drive2[6] = { 0, 0, 0, -1, -1, -1 };
147 static int drive3[6] = { 0, 0, 0, -1, -1, -1 };
148
149 static int (*drives[4])[6] = {&drive0, &drive1, &drive2, &drive3};
150 static int pg_drive_count;
151
152 enum {D_PRT, D_PRO, D_UNI, D_MOD, D_SLV, D_DLY};
153
154 /* end of parameters */
155
156 #include <linux/module.h>
157 #include <linux/init.h>
158 #include <linux/fs.h>
159 #include <linux/devfs_fs_kernel.h>
160 #include <linux/delay.h>
161 #include <linux/slab.h>
162 #include <linux/mtio.h>
163 #include <linux/pg.h>
164
165 #include <asm/uaccess.h>
166
167 #ifndef MODULE
168
169 #include "setup.h"
170
171 static STT pg_stt[5] = {
172         {"drive0", 6, drive0},
173         {"drive1", 6, drive1},
174         {"drive2", 6, drive2},
175         {"drive3", 6, drive3},
176         {"disable", 1, &disable}
177 };
178
179 void pg_setup(char *str, int *ints)
180 {
181         generic_setup(pg_stt, 5, str);
182 }
183
184 #endif
185
186 MODULE_PARM(verbose, "i");
187 MODULE_PARM(major, "i");
188 MODULE_PARM(name, "s");
189 MODULE_PARM(drive0, "1-6i");
190 MODULE_PARM(drive1, "1-6i");
191 MODULE_PARM(drive2, "1-6i");
192 MODULE_PARM(drive3, "1-6i");
193
194 #include "paride.h"
195
196 #define PG_SPIN_DEL     50      /* spin delay in micro-seconds  */
197 #define PG_SPIN         200
198 #define PG_TMO          HZ
199 #define PG_RESET_TMO    10*HZ
200
201 #define STAT_ERR        0x01
202 #define STAT_INDEX      0x02
203 #define STAT_ECC        0x04
204 #define STAT_DRQ        0x08
205 #define STAT_SEEK       0x10
206 #define STAT_WRERR      0x20
207 #define STAT_READY      0x40
208 #define STAT_BUSY       0x80
209
210 #define ATAPI_IDENTIFY          0x12
211
212 static int pg_open(struct inode *inode, struct file *file);
213 static int pg_release(struct inode *inode, struct file *file);
214 static ssize_t pg_read(struct file *filp, char *buf,
215                        size_t count, loff_t * ppos);
216 static ssize_t pg_write(struct file *filp, const char *buf,
217                         size_t count, loff_t * ppos);
218 static int pg_detect(void);
219
220 #define PG_NAMELEN      8
221
222 struct pg {
223         struct pi_adapter pia;  /* interface to paride layer */
224         struct pi_adapter *pi;
225         int busy;               /* write done, read expected */
226         int start;              /* jiffies at command start */
227         int dlen;               /* transfer size requested */
228         unsigned long timeout;  /* timeout requested */
229         int status;             /* last sense key */
230         int drive;              /* drive */
231         unsigned long access;   /* count of active opens ... */
232         int present;            /* device present ? */
233         char *bufptr;
234         char name[PG_NAMELEN];  /* pg0, pg1, ... */
235 };
236
237 struct pg devices[PG_UNITS];
238
239 static int pg_identify(struct pg *dev, int log);
240
241 static char pg_scratch[512];    /* scratch block buffer */
242
243 /* kernel glue structures */
244
245 static struct file_operations pg_fops = {
246         .owner = THIS_MODULE,
247         .read = pg_read,
248         .write = pg_write,
249         .open = pg_open,
250         .release = pg_release,
251 };
252
253 static void pg_init_units(void)
254 {
255         int unit;
256
257         pg_drive_count = 0;
258         for (unit = 0; unit < PG_UNITS; unit++) {
259                 int *parm = *drives[unit];
260                 struct pg *dev = &devices[unit];
261                 dev->pi = &dev->pia;
262                 set_bit(0, &dev->access);
263                 dev->busy = 0;
264                 dev->present = 0;
265                 dev->bufptr = NULL;
266                 dev->drive = parm[D_SLV];
267                 snprintf(dev->name, PG_NAMELEN, "%s%c", name, 'a'+unit);
268                 if (parm[D_PRT])
269                         pg_drive_count++;
270         }
271 }
272
273 static inline int status_reg(struct pg *dev)
274 {
275         return pi_read_regr(dev->pi, 1, 6);
276 }
277
278 static inline int read_reg(struct pg *dev, int reg)
279 {
280         return pi_read_regr(dev->pi, 0, reg);
281 }
282
283 static inline void write_reg(struct pg *dev, int reg, int val)
284 {
285         pi_write_regr(dev->pi, 0, reg, val);
286 }
287
288 static inline u8 DRIVE(struct pg *dev)
289 {
290         return 0xa0+0x10*dev->drive;
291 }
292
293 static void pg_sleep(int cs)
294 {
295         current->state = TASK_INTERRUPTIBLE;
296         schedule_timeout(cs);
297 }
298
299 static int pg_wait(struct pg *dev, int go, int stop, unsigned long tmo, char *msg)
300 {
301         int j, r, e, s, p, to;
302
303         dev->status = 0;
304
305         j = 0;
306         while ((((r = status_reg(dev)) & go) || (stop && (!(r & stop))))
307                && time_before(jiffies, tmo)) {
308                 if (j++ < PG_SPIN)
309                         udelay(PG_SPIN_DEL);
310                 else
311                         pg_sleep(1);
312         }
313
314         to = time_after_eq(jiffies, tmo);
315
316         if ((r & (STAT_ERR & stop)) || to) {
317                 s = read_reg(dev, 7);
318                 e = read_reg(dev, 1);
319                 p = read_reg(dev, 2);
320                 if (verbose > 1)
321                         printk("%s: %s: stat=0x%x err=0x%x phase=%d%s\n",
322                                dev->name, msg, s, e, p, to ? " timeout" : "");
323                 if (to)
324                         e |= 0x100;
325                 dev->status = (e >> 4) & 0xff;
326                 return -1;
327         }
328         return 0;
329 }
330
331 static int pg_command(struct pg *dev, char *cmd, int dlen, unsigned long tmo)
332 {
333         int k;
334
335         pi_connect(dev->pi);
336
337         write_reg(dev, 6, DRIVE(dev));
338
339         if (pg_wait(dev, STAT_BUSY | STAT_DRQ, 0, tmo, "before command"))
340                 goto fail;
341
342         write_reg(dev, 4, dlen % 256);
343         write_reg(dev, 5, dlen / 256);
344         write_reg(dev, 7, 0xa0);        /* ATAPI packet command */
345
346         if (pg_wait(dev, STAT_BUSY, STAT_DRQ, tmo, "command DRQ"))
347                 goto fail;
348
349         if (read_reg(dev, 2) != 1) {
350                 printk("%s: command phase error\n", dev->name);
351                 goto fail;
352         }
353
354         pi_write_block(dev->pi, cmd, 12);
355
356         if (verbose > 1) {
357                 printk("%s: Command sent, dlen=%d packet= ", dev->name, dlen);
358                 for (k = 0; k < 12; k++)
359                         printk("%02x ", cmd[k] & 0xff);
360                 printk("\n");
361         }
362         return 0;
363 fail:
364         pi_disconnect(dev->pi);
365         return -1;
366 }
367
368 static int pg_completion(struct pg *dev, char *buf, unsigned long tmo)
369 {
370         int r, d, n, p;
371
372         r = pg_wait(dev, STAT_BUSY, STAT_DRQ | STAT_READY | STAT_ERR,
373                     tmo, "completion");
374
375         dev->dlen = 0;
376
377         while (read_reg(dev, 7) & STAT_DRQ) {
378                 d = (read_reg(dev, 4) + 256 * read_reg(dev, 5));
379                 n = ((d + 3) & 0xfffc);
380                 p = read_reg(dev, 2) & 3;
381                 if (p == 0)
382                         pi_write_block(dev->pi, buf, n);
383                 if (p == 2)
384                         pi_read_block(dev->pi, buf, n);
385                 if (verbose > 1)
386                         printk("%s: %s %d bytes\n", dev->name,
387                                p ? "Read" : "Write", n);
388                 dev->dlen += (1 - p) * d;
389                 buf += d;
390                 r = pg_wait(dev, STAT_BUSY, STAT_DRQ | STAT_READY | STAT_ERR,
391                             tmo, "completion");
392         }
393
394         pi_disconnect(dev->pi);
395
396         return r;
397 }
398
399 static int pg_reset(struct pg *dev)
400 {
401         int i, k, err;
402         int expect[5] = { 1, 1, 1, 0x14, 0xeb };
403         int got[5];
404
405         pi_connect(dev->pi);
406         write_reg(dev, 6, DRIVE(dev));
407         write_reg(dev, 7, 8);
408
409         pg_sleep(20 * HZ / 1000);
410
411         k = 0;
412         while ((k++ < PG_RESET_TMO) && (status_reg(dev) & STAT_BUSY))
413                 pg_sleep(1);
414
415         for (i = 0; i < 5; i++)
416                 got[i] = read_reg(dev, i + 1);
417
418         err = memcmp(expect, got, sizeof(got)) ? -1 : 0;
419
420         if (verbose) {
421                 printk("%s: Reset (%d) signature = ", dev->name, k);
422                 for (i = 0; i < 5; i++)
423                         printk("%3x", got[i]);
424                 if (err)
425                         printk(" (incorrect)");
426                 printk("\n");
427         }
428
429         pi_disconnect(dev->pi);
430         return err;
431 }
432
433 static void xs(char *buf, char *targ, int len)
434 {
435         char l = '\0';
436         int k;
437
438         for (k = 0; k < len; k++) {
439                 char c = *buf++;
440                 if (c != ' ' || c != l)
441                         l = *targ++ = c;
442         }
443         if (l == ' ')
444                 targ--;
445         *targ = '\0';
446 }
447
448 static int pg_identify(struct pg *dev, int log)
449 {
450         int s;
451         char *ms[2] = { "master", "slave" };
452         char mf[10], id[18];
453         char id_cmd[12] = { ATAPI_IDENTIFY, 0, 0, 0, 36, 0, 0, 0, 0, 0, 0, 0 };
454         char buf[36];
455
456         s = pg_command(dev, id_cmd, 36, jiffies + PG_TMO);
457         if (s)
458                 return -1;
459         s = pg_completion(dev, buf, jiffies + PG_TMO);
460         if (s)
461                 return -1;
462
463         if (log) {
464                 xs(buf + 8, mf, 8);
465                 xs(buf + 16, id, 16);
466                 printk("%s: %s %s, %s\n", dev->name, mf, id, ms[dev->drive]);
467         }
468
469         return 0;
470 }
471
472 /*
473  * returns  0, with id set if drive is detected
474  *         -1, if drive detection failed
475  */
476 static int pg_probe(struct pg *dev)
477 {
478         if (dev->drive == -1) {
479                 for (dev->drive = 0; dev->drive <= 1; dev->drive++)
480                         if (!pg_reset(dev))
481                                 return pg_identify(dev, 1);
482         } else {
483                 if (!pg_reset(dev))
484                         return pg_identify(dev, 1);
485         }
486         return -1;
487 }
488
489 static int pg_detect(void)
490 {
491         struct pg *dev = &devices[0];
492         int k, unit;
493
494         printk("%s: %s version %s, major %d\n", name, name, PG_VERSION, major);
495
496         k = 0;
497         if (pg_drive_count == 0) {
498                 if (pi_init(dev->pi, 1, -1, -1, -1, -1, -1, pg_scratch,
499                             PI_PG, verbose, dev->name)) {
500                         if (!pg_probe(dev)) {
501                                 dev->present = 1;
502                                 k++;
503                         } else
504                                 pi_release(dev->pi);
505                 }
506
507         } else
508                 for (unit = 0; unit < PG_UNITS; unit++, dev++) {
509                         int *parm = *drives[unit];
510                         if (!parm[D_PRT])
511                                 continue;
512                         if (pi_init(dev->pi, 0, parm[D_PRT], parm[D_MOD],
513                                     parm[D_UNI], parm[D_PRO], parm[D_DLY],
514                                     pg_scratch, PI_PG, verbose, dev->name)) {
515                                 if (!pg_probe(dev)) {
516                                         dev->present = 1;
517                                         k++;
518                                 } else
519                                         pi_release(dev->pi);
520                         }
521                 }
522
523         if (k)
524                 return 0;
525
526         printk("%s: No ATAPI device detected\n", name);
527         return -1;
528 }
529
530 static int pg_open(struct inode *inode, struct file *file)
531 {
532         int unit = iminor(inode) & 0x7f;
533         struct pg *dev = &devices[unit];
534
535         if ((unit >= PG_UNITS) || (!dev->present))
536                 return -ENODEV;
537
538         if (test_and_set_bit(0, &dev->access))
539                 return -EBUSY;
540
541         if (dev->busy) {
542                 pg_reset(dev);
543                 dev->busy = 0;
544         }
545
546         pg_identify(dev, (verbose > 1));
547
548         dev->bufptr = kmalloc(PG_MAX_DATA, GFP_KERNEL);
549         if (dev->bufptr == NULL) {
550                 clear_bit(0, &dev->access);
551                 printk("%s: buffer allocation failed\n", dev->name);
552                 return -ENOMEM;
553         }
554
555         file->private_data = dev;
556
557         return 0;
558 }
559
560 static int pg_release(struct inode *inode, struct file *file)
561 {
562         struct pg *dev = file->private_data;
563
564         kfree(dev->bufptr);
565         dev->bufptr = NULL;
566         clear_bit(0, &dev->access);
567
568         return 0;
569 }
570
571 static ssize_t pg_write(struct file *filp, const char *buf, size_t count, loff_t *ppos)
572 {
573         struct pg *dev = filp->private_data;
574         struct pg_write_hdr hdr;
575         int hs = sizeof (hdr);
576
577         if (dev->busy)
578                 return -EBUSY;
579         if (count < hs)
580                 return -EINVAL;
581
582         if (copy_from_user((char *) &hdr, buf, hs))
583                 return -EFAULT;
584
585         if (hdr.magic != PG_MAGIC)
586                 return -EINVAL;
587         if (hdr.dlen > PG_MAX_DATA)
588                 return -EINVAL;
589         if ((count - hs) > PG_MAX_DATA)
590                 return -EINVAL;
591
592         if (hdr.func == PG_RESET) {
593                 if (count != hs)
594                         return -EINVAL;
595                 if (pg_reset(dev))
596                         return -EIO;
597                 return count;
598         }
599
600         if (hdr.func != PG_COMMAND)
601                 return -EINVAL;
602
603         dev->start = jiffies;
604         dev->timeout = hdr.timeout * HZ + HZ / 2 + jiffies;
605
606         if (pg_command(dev, hdr.packet, hdr.dlen, jiffies + PG_TMO)) {
607                 if (dev->status & 0x10)
608                         return -ETIME;
609                 return -EIO;
610         }
611
612         dev->busy = 1;
613
614         if (copy_from_user(dev->bufptr, buf + hs, count - hs))
615                 return -EFAULT;
616         return count;
617 }
618
619 static ssize_t pg_read(struct file *filp, char *buf, size_t count, loff_t *ppos)
620 {
621         struct pg *dev = filp->private_data;
622         struct pg_read_hdr hdr;
623         int hs = sizeof (hdr);
624         int copy;
625
626         if (!dev->busy)
627                 return -EINVAL;
628         if (count < hs)
629                 return -EINVAL;
630
631         dev->busy = 0;
632
633         if (pg_completion(dev, dev->bufptr, dev->timeout))
634                 if (dev->status & 0x10)
635                         return -ETIME;
636
637         hdr.magic = PG_MAGIC;
638         hdr.dlen = dev->dlen;
639         copy = 0;
640
641         if (hdr.dlen < 0) {
642                 hdr.dlen = -1 * hdr.dlen;
643                 copy = hdr.dlen;
644                 if (copy > (count - hs))
645                         copy = count - hs;
646         }
647
648         hdr.duration = (jiffies - dev->start + HZ / 2) / HZ;
649         hdr.scsi = dev->status & 0x0f;
650
651         if (copy_to_user(buf, (char *) &hdr, hs))
652                 return -EFAULT;
653         if (copy > 0)
654                 if (copy_to_user(buf + hs, dev->bufptr, copy))
655                         return -EFAULT;
656         return copy + hs;
657 }
658
659 static int __init pg_init(void)
660 {
661         int unit;
662
663         if (disable)
664                 return -1;
665
666         pg_init_units();
667
668         if (pg_detect())
669                 return -1;
670
671         if (register_chrdev(major, name, &pg_fops)) {
672                 printk("pg_init: unable to get major number %d\n", major);
673                 for (unit = 0; unit < PG_UNITS; unit++) {
674                         struct pg *dev = &devices[unit];
675                         if (dev->present)
676                                 pi_release(dev->pi);
677                 }
678                 return -1;
679         }
680         devfs_mk_dir("pg");
681         for (unit = 0; unit < PG_UNITS; unit++) {
682                 struct pg *dev = &devices[unit];
683                 if (dev->present) {
684                         devfs_mk_cdev(MKDEV(major, unit),
685                                       S_IFCHR | S_IRUSR | S_IWUSR, "pg/%u",
686                                       unit);
687                 }
688         }
689         return 0;
690 }
691
692 static void __exit pg_exit(void)
693 {
694         int unit;
695
696         for (unit = 0; unit < PG_UNITS; unit++) {
697                 struct pg *dev = &devices[unit];
698                 if (dev->present)
699                         devfs_remove("pg/%u", unit);
700         }
701
702         devfs_remove("pg");
703         unregister_chrdev(major, name);
704
705         for (unit = 0; unit < PG_UNITS; unit++) {
706                 struct pg *dev = &devices[unit];
707                 if (dev->present)
708                         pi_release(dev->pi);
709         }
710 }
711
712 MODULE_LICENSE("GPL");
713 module_init(pg_init)
714 module_exit(pg_exit)