fix lintian issues
[debian/mtx] / scsi_sun.c
1 /* Copyright 1997, 1998 Leonard Zubkoff <lnz@dandelion.com>
2    Changes copyright 2000 Eric Green <eric@badtux.org>
3    Copyright 2007-2008 by Robert Nelson <robertn@the-nelsons.org>
4
5 $Date: 2008-08-19 03:03:38 -0700 (Tue, 19 Aug 2008) $
6 $Revision: 193 $
7
8   This program is free software; you may redistribute and/or modify it under
9   the terms of the GNU General Public License Version 2 as published by the
10   Free Software Foundation.
11
12   This program is distributed in the hope that it will be useful, but
13   WITHOUT ANY WARRANTY, without even the implied warranty of MERCHANTABILITY
14   or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15   for complete details.
16
17 */
18
19 /* This is the SCSI commands for Sun Solaris. */
20
21 #define LONG_PRINT_REQUEST_SENSE  /* sigh! */
22
23 DEVICE_TYPE SCSI_OpenDevice(char *DeviceName)
24 {
25         int DeviceFD = open(DeviceName, O_RDWR | O_NDELAY);
26         if (DeviceFD < 0)
27                 FatalError("cannot open SCSI device '%s' - %m\n", DeviceName);
28         return (DEVICE_TYPE) DeviceFD;
29 }
30
31
32 void SCSI_CloseDevice(char *DeviceName, DEVICE_TYPE DeviceFD)
33 {
34         if (close(DeviceFD) < 0)
35                 FatalError("cannot close SCSI device '%s' - %m\n", DeviceName);
36 }
37
38
39 #define HAS_SCSI_TIMEOUT
40
41 static int uscsi_timeout=5*60; 
42
43 void SCSI_Set_Timeout(int to)
44 {
45         uscsi_timeout = to;
46 }
47
48 void SCSI_Default_Timeout(void)
49 {
50         uscsi_timeout=5*60; /* the default */
51 }
52
53 #ifdef DEBUG
54 int SCSI_DumpBuffer(int DataBufferLength, unsigned char *DataBuffer)
55 {
56         int i,j;
57         j = 0;
58
59         for (i = 0; i < DataBufferLength; i++)
60         {
61                 if (j == 25)
62                 {
63                         fprintf(stderr,"\n");
64                         j = 0;
65                 }
66
67                 if (j == 0)
68                 {
69                         fprintf(stderr, "%04x:", i);
70                 }
71
72                 if (j > 0)
73                 {
74                         fprintf(stderr," ");
75                 }
76
77                 fprintf(stderr, "%02x", (int)DataBuffer[i]);
78                 j++;
79         }
80         fprintf(stderr, "\n");
81 }
82 #endif
83
84
85 int SCSI_ExecuteCommand(DEVICE_TYPE DeviceFD,
86                                                 Direction_T Direction,
87                                                 CDB_T *CDB,
88                                                 int CDB_Length,
89                                                 void *DataBuffer,
90                                                 int DataBufferLength,
91                                                 RequestSense_T *RequestSense)
92 {
93         int ioctl_result;
94         struct uscsi_cmd Command;
95
96 #ifdef DEBUG_SCSI
97         fprintf(stderr,"------CDB--------\n");
98         SCSI_DumpBuffer(CDB_Length,(char *)CDB);
99 #endif
100
101         memset(&Command, 0, sizeof(struct uscsi_cmd));
102         memset(RequestSense, 0, sizeof(RequestSense_T));
103         switch (Direction)
104         {
105         case Input:
106                 Command.uscsi_flags = USCSI_DIAGNOSE | USCSI_ISOLATE | USCSI_RQENABLE;
107                 if (DataBufferLength > 0)
108                 {
109                         memset(DataBuffer, 0, DataBufferLength);
110                         Command.uscsi_flags |= USCSI_READ;
111                 }
112                 break;
113         case Output:
114                 Command.uscsi_flags = USCSI_DIAGNOSE | USCSI_ISOLATE |
115                                                                 USCSI_WRITE | USCSI_RQENABLE;
116                 break;
117         }
118   /* Set timeout to 5 minutes. */
119 #ifdef DEBUG_TIMEOUT
120         fprintf(stderr,"uscsi_timeout=%d\n",uscsi_timeout);
121         fflush(stderr);
122 #endif
123         Command.uscsi_timeout = uscsi_timeout;
124
125         Command.uscsi_cdb = (caddr_t) CDB;
126         Command.uscsi_cdblen = CDB_Length;
127         Command.uscsi_bufaddr = DataBuffer;
128         Command.uscsi_buflen = DataBufferLength;
129         Command.uscsi_rqbuf = (caddr_t) RequestSense;
130         Command.uscsi_rqlen = sizeof(RequestSense_T);
131         ioctl_result = ioctl(DeviceFD, USCSICMD, &Command);
132
133         SCSI_Default_Timeout(); /* set it back to default, sigh. */
134
135         if (ioctl_result < 0)
136         {
137 #ifdef DEBUG
138                 perror("mtx");
139 #endif
140                 return ioctl_result;
141         }
142
143         if (RequestSense->ErrorCode > 1)
144         {
145                 return -1;
146         }
147
148 #ifdef DEBUG_SCSI
149         if (Direction==Input)
150         {
151                 fprintf(stderr,"--------input data-----------\n");
152                 SCSI_DumpBuffer(DataBufferLength, DataBuffer);
153         }
154 #endif
155         return 0;
156 }