fix lintian issues
[debian/mtx] / scsi_hpux.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   This program is free software; you may redistribute and/or modify it under
6   the terms of the GNU General Public License Version 2 as published by the
7   Free Software Foundation.
8
9   This program is distributed in the hope that it will be useful, but
10   WITHOUT ANY WARRANTY, without even the implied warranty of MERCHANTABILITY
11   or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12   for complete details.
13
14 struct sctl_io
15 {
16         unsigned flags;                 // IN: SCTL_READ
17         unsigned cdb_length;            // IN
18         unsigned char cdb[16];          // IN 
19         void *data;                     // IN 
20         unsigned data_length;           // IN 
21         unsigned max_msecs;             // IN: milli-seconds before abort 
22         unsigned data_xfer;             // OUT 
23         unsigned cdb_status;            // OUT: SCSI status 
24         unsigned char sense[256];       // OUT 
25         unsigned sense_status;          // OUT: SCSI status 
26         unsigned sense_xfer;            // OUT: bytes of sense data received 
27         unsigned reserved[16];          // IN: Must be zero; OUT: undefined 
28 };
29
30 */
31
32
33 /* Hockey Pux may define these. If so, *UN*define them. */
34 #ifdef ILI
35 #undef ILI
36 #endif
37
38 #ifdef EOM
39 #undef EOM
40 #endif
41
42 /* This is the SCSI commands for HPUX. */
43
44 #define LONG_PRINT_REQUEST_SENSE  /* Sigh! */
45
46 DEVICE_TYPE SCSI_OpenDevice(char *DeviceName)
47 {
48         int DeviceFD = open(DeviceName, O_RDWR | O_NDELAY);
49
50         if (DeviceFD < 0)
51                 FatalError("cannot open SCSI device '%s' - %m\n", DeviceName);
52
53         return (DEVICE_TYPE) DeviceFD;
54 }
55
56
57 void SCSI_CloseDevice(char *DeviceName, DEVICE_TYPE DeviceFD)
58 {
59         if (close(DeviceFD) < 0)
60                 FatalError("cannot close SCSI device '%s' - %m\n", DeviceName);
61 }
62
63 #define MTX_HZ 1000
64 #define DEFAULT_HZ (5*60*MTX_HZ)
65
66 static int sctl_io_timeout=DEFAULT_HZ;          /* default timeout is 5 minutes. */
67
68
69 void SCSI_Set_Timeout(int to)
70 {
71         sctl_io_timeout=to*60*MTX_HZ;
72 }
73
74 void SCSI_Default_Timeout(void)
75 {
76         sctl_io_timeout=DEFAULT_HZ;
77 }
78
79
80 int SCSI_ExecuteCommand(DEVICE_TYPE DeviceFD,
81                                                 Direction_T Direction,
82                                                 CDB_T *CDB,
83                                                 int CDB_Length,
84                                                 void *DataBuffer,
85                                                 int DataBufferLength,
86                                                 RequestSense_T *RequestSense)
87 {
88         int ioctl_result;
89         struct sctl_io Command;
90
91         int i;
92
93         memset(&Command, 0, sizeof(struct sctl_io));
94         memset(RequestSense, 0, sizeof(RequestSense_T));
95
96         switch (Direction)
97         {
98         case Input:
99                 if (DataBufferLength > 0)
100                         memset(DataBuffer, 0, DataBufferLength);
101                 Command.flags =  SCTL_READ | SCTL_INIT_SDTR;
102                 break;
103
104         case Output:
105                 Command.flags = SCTL_INIT_WDTR | SCTL_INIT_SDTR;
106                 break;
107         }
108
109         Command.max_msecs = sctl_io_timeout;    /* Set timeout to <n> minutes. */
110         memcpy(Command.cdb, CDB, CDB_Length);
111         Command.cdb_length = CDB_Length;
112         Command.data = DataBuffer;
113         Command.data_length = DataBufferLength;
114         ioctl_result=ioctl(DeviceFD, SIOC_IO, &Command);
115         SCSI_Default_Timeout();                 /* change the default back to 5 minutes */
116
117         if (ioctl_result < 0)
118         {
119                 perror("mtx");
120                 return ioctl_result;
121         }
122
123         if (Command.sense_xfer > sizeof(RequestSense_T))
124         {
125                 Command.sense_xfer=sizeof(RequestSense_T);
126         }
127
128         if (Command.sense_xfer)
129         {
130                 memcpy(RequestSense, Command.sense, Command.sense_xfer);
131         }
132
133         return Command.sense_status;
134 }