/usr/share/doc/HOWTO/ja-html/SCSI-Programming-HOWTO-24.html is in doc-linux-ja-html 2006.05.25-1.1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<HTML>
<HEAD>
<META NAME="GENERATOR" CONTENT="LinuxDoc-Tools 0.9.21">
<TITLE>The Linux SCSI programming HOWTO: $B%W%m%0%i%`Nc(B</TITLE>
<LINK HREF="SCSI-Programming-HOWTO-23.html" REL=previous>
<LINK HREF="SCSI-Programming-HOWTO.html#toc24" REL=contents>
</HEAD>
<BODY>
$B<!$N%Z!<%8(B
<A HREF="SCSI-Programming-HOWTO-23.html">$BA0$N%Z!<%8(B</A>
<A HREF="SCSI-Programming-HOWTO.html#toc24">$BL\<!$X(B</A>
<HR>
<H2><A NAME="s24">24.</A> <A HREF="SCSI-Programming-HOWTO.html#toc24">$B%W%m%0%i%`Nc(B</A></H2>
<P>$B$3$l$,(B C $B8@8l$K$h$k%W%m%0%i%`Nc$G$9!#$3$l$O@=B$<T(B/$B%b%G%k$rMW5a$7!"(B
$B%a%G%#%"$,AuCV$KEk:\$5$l$F$$$k$+$I$&$+$rJs9p$7$^$9!#(B</P>
<P>
<BLOCKQUOTE><CODE>
<PRE>
#define DEVICE "/dev/sgc"
/* $BHFMQ(B SCSI $B%$%s%?!<%U%'!<%9$r<B:]$KF0$+$7$F$_$k8+K\%W%m%0%i%`(B */
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <scsi/sg.h>
#define SCSI_OFF sizeof(struct sg_header)
static unsigned char cmd[SCSI_OFF + 18]; /* SCSI $B%3%^%s%I(B $B%P%C%U%!(B */
int fd; /* SCSI $B%G%P%$%9(B/$B%U%!%$%k(B $B%G%#%9%/%j%W%?(B */
/* $B407k$7$?(B SCSI $B%3%^%s%I$r=hM}!#(B $BHFMQ(B SCSI $B%$%s%?!<%U%'!<%9$r;HMQ(B */
static int handle_scsi_cmd(unsigned cmd_len, /* $B%3%^%s%ID9(B */
unsigned in_size, /* $BF~NO%G!<%?%5%$%:(B */
unsigned char *i_buff, /* $BF~NO%P%C%U%!(B */
unsigned out_size, /* $B=PNO%G!<%?%5%$%:(B */
unsigned char *o_buff /* $B=PNO%P%C%U%!(B */
)
{
int status = 0;
struct sg_header *sg_hd;
/* $B0BA4@-8!::(B */
if (!cmd_len) return -1; /* cmd_len != 0 $B$,I,MW(B */
if (!i_buff) return -1; /* $BF~NO%P%C%U%!$,(B NULL $B$G$J$$$3$H(B */
#ifdef SG_BIG_BUFF
if (SCSI_OFF + cmd_len + in_size > SG_BIG_BUFF) return -1;
if (SCSI_OFF + out_size > SG_BIG_BUFF) return -1;
#else
if (SCSI_OFF + cmd_len + in_size > 4096) return -1;
if (SCSI_OFF + out_size > 4096) return -1;
#endif
if (!o_buff) out_size = 0;
/* $BHFMQ(B SCSI $B%G%P%$%9%X%C%@$N9=C[(B */
sg_hd = (struct sg_header *) i_buff;
sg_hd->reply_len = SCSI_OFF + out_size;
sg_hd->twelve_byte = cmd_len == 12;
sg_hd->result = 0;
#if 0
sg_hd->pack_len = SCSI_OFF + cmd_len + in_size; /* $BITMW(B */
sg_hd->pack_id; /* $BL$;HMQ(B */
sg_hd->other_flags; /* $BL$;HMQ(B */
#endif
/* $B%3%^%s%IAw=P(B */
status = write( fd, i_buff, SCSI_OFF + cmd_len + in_size );
if ( status < 0 || status != SCSI_OFF + cmd_len + in_size ||
sg_hd->result ) {
/* $B$J$s$i$+$N%(%i!<$,H/@8(B */
fprintf( stderr, "write(generic) result = 0x%x cmd = 0x%x\n",
sg_hd->result, i_buff[SCSI_OFF] );
perror("");
return status;
}
if (!o_buff) o_buff = i_buff; /* $B%P%C%U%!$N%]%$%s%?$r%A%'%C%/(B */
/* $B7k2L$r2s<}(B */
status = read( fd, o_buff, SCSI_OFF + out_size);
if ( status < 0 || status != SCSI_OFF + out_size || sg_hd->result ) {
/* $B$J$s$i$+$N%(%i!<$,H/@8(B */
fprintf( stderr, "read(generic) result = 0x%x cmd = 0x%x\n",
sg_hd->result, o_buff[SCSI_OFF] );
fprintf( stderr, "read(generic) sense "
"%x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x\n",
sg_hd->sense_buffer[0], sg_hd->sense_buffer[1],
sg_hd->sense_buffer[2], sg_hd->sense_buffer[3],
sg_hd->sense_buffer[4], sg_hd->sense_buffer[5],
sg_hd->sense_buffer[6], sg_hd->sense_buffer[7],
sg_hd->sense_buffer[8], sg_hd->sense_buffer[9],
sg_hd->sense_buffer[10], sg_hd->sense_buffer[11],
sg_hd->sense_buffer[12], sg_hd->sense_buffer[13],
sg_hd->sense_buffer[14], sg_hd->sense_buffer[15]);
if (status < 0)
perror("");
}
/* $B<u$1<h$k$Y$-$b$N$r<u$1<h$C$?$+$I$&$+$r$_$k(B */
if (status == SCSI_OFF + out_size) status = 0; /* $BA4It$b$i$C$?(B */
return status; /* 0 $B$O%(%i!<$J$7$r0UL#(B */
}
#define INQUIRY_CMD 0x12
#define INQUIRY_CMDLEN 6
#define INQUIRY_REPLY_LEN 96
#define INQUIRY_VENDOR 8 /* $BJVEz%G!<%?Fb$N%Y%s%@L>$N%*%U%;%C%H(B */
/* $B%Y%s%@$N%V%i%s%I$H%b%G%k$rMW5a(B */
static unsigned char *Inquiry ( void )
{
static unsigned char Inqbuffer[ SCSI_OFF + INQUIRY_REPLY_LEN ];
unsigned char cmdblk [ INQUIRY_CMDLEN ] =
{ INQUIRY_CMD, /* command */
0, /* lun/reserved */
0, /* page code */
0, /* reserved */
INQUIRY_REPLY_LEN, /* allocation length */
0 };/* reserved/flag/link */
memcpy( cmd + SCSI_OFF, cmdblk, sizeof(cmdblk) );
/*
* +------------------+
* | struct sg_header | <- cmd
* +------------------+
* | copy of cmdblk | <- cmd + SCSI_OFF
* +------------------+
*/
if (handle_scsi_cmd(sizeof(cmdblk), 0, cmd,
sizeof(Inqbuffer) - SCSI_OFF, Inqbuffer )) {
fprintf( stderr, "Inquiry failed\n" );
exit(2);
}
return (Inqbuffer + SCSI_OFF);
}
#define TESTUNITREADY_CMD 0
#define TESTUNITREADY_CMDLEN 6
#define ADD_SENSECODE 12
#define ADD_SC_QUALIFIER 13
#define NO_MEDIA_SC 0x3a
#define NO_MEDIA_SCQ 0x00
int TestForMedium ( void )
{
/* READY $B>uBV$rMW5a(B */
static unsigned char cmdblk [TESTUNITREADY_CMDLEN] = {
TESTUNITREADY_CMD, /* command */
0, /* lun/reserved */
0, /* reserved */
0, /* reserved */
0, /* reserved */
0};/* reserved */
memcpy( cmd + SCSI_OFF, cmdblk, sizeof(cmdblk) );
/*
* +------------------+
* | struct sg_header | <- cmd
* +------------------+
* | copy of cmdblk | <- cmd + SCSI_OFF
* +------------------+
*/
if (handle_scsi_cmd(sizeof(cmdblk), 0, cmd,
0, NULL)) {
fprintf (stderr, "Test unit ready failed\n");
exit(2);
}
return
*(((struct sg_header*)cmd)->sense_buffer +ADD_SENSECODE) !=
NO_MEDIA_SC ||
*(((struct sg_header*)cmd)->sense_buffer +ADD_SC_QUALIFIER) !=
NO_MEDIA_SCQ;
}
void main( void )
{
fd = open(DEVICE, O_RDWR);
if (fd < 0) {
fprintf( stderr, "Need read/write permissions for "DEVICE".\n" );
exit(1);
}
/* Inquiry $B$N7k2L$N0lIt$N%U%#!<%k%I$rI=<((B */
printf( "%s\n", Inquiry() + INQUIRY_VENDOR );
/* $B%a%G%#%"$,Ek:\$5$l$F$$$k$+$I$&$+$r$_$k(B */
if (!TestForMedium()) {
printf("device is unloaded\n");
} else {
printf("device is loaded\n");
}
}
</PRE>
</CODE></BLOCKQUOTE>
</P>
<HR>
$B<!$N%Z!<%8(B
<A HREF="SCSI-Programming-HOWTO-23.html">$BA0$N%Z!<%8(B</A>
<A HREF="SCSI-Programming-HOWTO.html#toc24">$BL\<!$X(B</A>
</BODY>
</HTML>
|