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