Linux kernel & device driver programming

Cross-Referenced Linux and Device Driver Code

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