Imported Upstream version 1.1.11
[platform/upstream/cdrkit.git] / libusal / scsihack.c
1 /*
2  * This file has been modified for the cdrkit suite.
3  *
4  * The behaviour and appearence of the program code below can differ to a major
5  * extent from the version distributed by the original author(s).
6  *
7  * For details, see Changelog file distributed with the cdrkit package. If you
8  * received this file from another source then ask the distributing person for
9  * a log of modifications.
10  *
11  */
12
13 /* @(#)scsihack.c       1.44 06/01/30 Copyright 1997,2000,2001 J. Schilling */
14 /*
15  *      Interface for other generic SCSI implementations.
16  *      Emulate the functionality of /dev/usal? with the local
17  *      SCSI user land implementation.
18  *
19  *      To add a new hack, add something like:
20  *
21  *      #ifdef  __FreeBSD__
22  *      #define SCSI_IMPL
23  *      #include some code
24  *      #endif
25  *
26  *      Warning: you may change this source or add new SCSI tranport
27  *      implementations, but if you do that you need to change the
28  *      _usal_version and _usal_auth* string that are returned by the
29  *      SCSI transport code.
30  *      Choose your name instead of "schily" and make clear that the version
31  *      string is related to a modified source.
32  *      If your version has been integrated into the main steam release,
33  *      the return value will be set to "schily".
34  *
35  *      Copyright (c) 1997,2000,2001 J. Schilling
36  */
37 /*
38  * This program is free software; you can redistribute it and/or modify
39  * it under the terms of the GNU General Public License version 2
40  * as published by the Free Software Foundation.
41  *
42  * This program is distributed in the hope that it will be useful,
43  * but WITHOUT ANY WARRANTY; without even the implied warranty of
44  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
45  * GNU General Public License for more details.
46  *
47  * You should have received a copy of the GNU General Public License along with
48  * this program; see the file COPYING.  If not, write to the Free Software
49  * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
50  */
51
52 #include <mconfig.h>
53
54 #ifdef  HAVE_SYS_PARAM_H
55 #include <sys/param.h>  /* Include various defs needed with some OS */
56 #endif
57 #include <stdio.h>
58 #include <standard.h>
59 #include <stdxlib.h>
60 #include <unixstd.h>
61 #include <errno.h>
62 #include <timedefs.h>
63 #include <sys/ioctl.h>
64 #include <fctldefs.h>
65 #include <strdefs.h>
66 #include <schily.h>
67
68 #include <usal/usalcmd.h>
69 #include <usal/scsitransp.h>
70 #include "usaltimes.h"
71
72 #ifndef HAVE_ERRNO_DEF
73 extern  int     errno;
74 #endif
75
76 static  int     usalo_send(SCSI *usalp);
77 static  char *usalo_version(SCSI *usalp, int what);
78 static  int     usalo_help(SCSI *usalp, FILE *f);
79 static  int     usalo_open(SCSI *usalp, char *device);
80 static  int     usalo_close(SCSI *usalp);
81 static  long    usalo_maxdma(SCSI *usalp, long amt);
82 static  void *usalo_getbuf(SCSI *usalp, long amt);
83 static  void    usalo_freebuf(SCSI *usalp);
84
85 static  BOOL    usalo_havebus(SCSI *usalp, int busno);
86 static  int     usalo_fileno(SCSI *usalp, int busno, int tgt, int tlun);
87 static char * usalo_natname(SCSI *usalp, int busno, int tgt, int tlun);
88 static  int     usalo_initiator_id(SCSI *usalp);
89 static  int     usalo_isatapi(SCSI *usalp);
90 static  int     usalo_reset(SCSI *usalp, int what);
91
92 static  char    _usal_auth_cdrkit[]     = "cdrkit-team";        /* The author for this module   */
93
94 usal_ops_t usal_std_ops = {
95         usalo_send,
96         usalo_version,
97         usalo_help,
98         usalo_open,
99         usalo_close,
100         usalo_maxdma,
101         usalo_getbuf,
102         usalo_freebuf,
103         usalo_havebus,
104         usalo_fileno,
105         usalo_initiator_id,
106         usalo_isatapi,
107         usalo_reset,
108         usalo_natname,
109 };
110
111 /*#undef sun*/
112 /*#undef __sun*/
113 /*#undef __sun__*/
114
115 #if defined(sun) || defined(__sun) || defined(__sun__)
116 #define SCSI_IMPL               /* We have a SCSI implementation for Sun */
117
118 #include "scsi-sun.c"
119
120 #endif  /* Sun */
121
122
123 #ifdef  linux
124 #define SCSI_IMPL               /* We have a SCSI implementation for Linux */
125
126 #ifdef  not_needed              /* We now have a local vrersion of pg.h  */
127 #ifndef HAVE_LINUX_PG_H         /* If we are compiling on an old version */
128 #       undef   USE_PG_ONLY     /* there is no 'pg' driver and we cannot */
129 #       undef   USE_PG          /* include <linux/pg.h> which is needed  */
130 #endif                          /* by the pg transport code.             */
131 #endif
132
133 #ifdef  USE_PG_ONLY
134 #include "scsi-linux-pg.c"
135 #else
136 #include "scsi-linux-sg.c"
137 #endif
138
139 #endif  /* linux */
140
141 #if     defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)  || defined(__FreeBSD_kernel__) 
142 #define SCSI_IMPL               /* We have a SCSI implementation for *BSD */
143
144 #include "scsi-bsd.c"
145
146 #endif  /* *BSD */
147
148 #if     defined(__bsdi__)       /* We have a SCSI implementation for BSD/OS 3.x (and later?) */
149 # include <sys/param.h>
150 # if (_BSDI_VERSION >= 199701)
151 #  define       SCSI_IMPL
152
153 #  include "scsi-bsd-os.c"
154
155 # endif /* BSD/OS >= 3.0 */
156 #endif /* BSD/OS */
157
158 #ifdef  __sgi
159 #define SCSI_IMPL               /* We have a SCSI implementation for SGI */
160
161 #include "scsi-sgi.c"
162
163 #endif  /* SGI */
164
165 #ifdef  __hpux
166 #define SCSI_IMPL               /* We have a SCSI implementation for HP-UX */
167
168 #include "scsi-hpux.c"
169
170 #endif  /* HP-UX */
171
172 #if     defined(_IBMR2) || defined(_AIX)
173 #define SCSI_IMPL               /* We have a SCSI implementation for AIX */
174
175 #include "scsi-aix.c"
176
177 #endif  /* AIX */
178
179 #if     defined(__NeXT__) || defined(IS_MACOS_X)
180 #if     defined(HAVE_BSD_DEV_SCSIREG_H)
181 /*
182  *      This is the
183  */
184 #define SCSI_IMPL               /* We found a SCSI implementation for NextStep and Mac OS X */
185
186 #include "scsi-next.c"
187 #else
188
189 #define SCSI_IMPL               /* We found a SCSI implementation for Mac OS X (Darwin-1.4) */
190
191 #include "scsi-mac-iokit.c"
192
193 #endif  /* HAVE_BSD_DEV_SCSIREG_H */
194
195 #endif  /* NEXT / Mac OS X */
196
197 #if     defined(__osf__)
198 #define SCSI_IMPL               /* We have a SCSI implementation for OSF/1 */
199
200 #include "scsi-osf.c"
201
202 #endif  /* OSF/1 */
203
204 #ifdef  VMS
205 #define SCSI_IMPL               /* We have a SCSI implementation for VMS */
206
207 #include "scsi-vms.c"
208
209 #endif  /* VMS */
210
211 #ifdef  OPENSERVER
212 #define SCSI_IMPL               /* We have a SCSI implementation for SCO OpenServer */
213
214 #include "scsi-openserver.c"
215
216 #endif  /* SCO */
217
218 #ifdef  UNIXWARE
219 #define SCSI_IMPL               /* We have a SCSI implementation for SCO UnixWare */
220
221 #include "scsi-unixware.c"
222
223 #endif  /* UNIXWARE */
224
225 #ifdef  __OS2
226 #define SCSI_IMPL               /* We have a SCSI implementation for OS/2 */
227
228 #include "scsi-os2.c"
229
230 #endif  /* OS/2 */
231
232 #ifdef  __BEOS__
233 #define SCSI_IMPL               /* Yep, BeOS does that funky scsi stuff */
234 #include "scsi-beos.c"
235 #endif
236
237 #ifdef  __CYGWIN32__
238 #define SCSI_IMPL               /* Yep, we support WNT and W9? */
239 #include "scsi-wnt.c"
240 #endif
241
242 #ifdef  apollo
243 #define SCSI_IMPL               /* We have a SCSI implementation for Apollo Domain/OS */
244 #include "scsi-apollo.c"
245 #endif
246
247 #ifdef  AMIGA                   /* We have a SCSI implementation for AmigaOS */
248 #define SCSI_IMPL
249 #include "scsi-amigaos.c"
250 #endif
251
252 #if     defined(__QNXNTO__) || defined(__QNX__)
253 #define SCSI_IMPL               /* We have a SCSI implementation for QNX */
254 #include "scsi-qnx.c"
255 #endif  /* QNX */
256
257 #ifdef  __DJGPP__               /* We have a SCSI implementation for MS-DOS/DJGPP */
258 #define SCSI_IMPL
259 #include "scsi-dos.c"
260 #endif
261
262 #ifdef  __NEW_ARCHITECTURE
263 #define SCSI_IMPL               /* We have a SCSI implementation for XXX */
264 /*
265  * Add new hacks here
266  */
267 #include "scsi-new-arch.c"
268 #endif
269
270
271 #ifndef SCSI_IMPL
272 /*
273  * To make scsihack.c compile on all architectures.
274  * This does not mean that you may use it, but you can see
275  * if other problems exist.
276  */
277 #define usalo_dversion          usalo_version
278 #define usalo_dhelp             usalo_help
279 #define usalo_dopen             usalo_open
280 #define usalo_dclose            usalo_close
281 #define usalo_dmaxdma           usalo_maxdma
282 #define usalo_dgetbuf           usalo_getbuf
283 #define usalo_dfreebuf          usalo_freebuf
284 #define usalo_dhavebus          usalo_havebus
285 #define usalo_dfileno           usalo_fileno
286 #define usalo_dinitiator_id     usalo_initiator_id
287 #define usalo_disatapi          usalo_isatapi
288 #define usalo_dreset            usalo_reset
289 #define usalo_dsend             usalo_send
290 #endif  /* SCSI_IMPL */
291
292 static  int     usalo_dsend(SCSI *usalp);
293 static  char *usalo_dversion(SCSI *usalp, int what);
294 static  int     usalo_dhelp(SCSI *usalp, FILE *f);
295 static  int     usalo_nohelp(SCSI *usalp, FILE *f);
296 static  int     usalo_ropen(SCSI *usalp, char *device);
297 static  int     usalo_dopen(SCSI *usalp, char *device);
298 static  int     usalo_dclose(SCSI *usalp);
299 static  long    usalo_dmaxdma(SCSI *usalp, long amt);
300 static  void *usalo_dgetbuf(SCSI *usalp, long amt);
301 static  void    usalo_dfreebuf(SCSI *usalp);
302 static  BOOL    usalo_dhavebus(SCSI *usalp, int busno);
303 static  int     usalo_dfileno(SCSI *usalp, int busno, int tgt, int tlun);
304 static  int     usalo_dinitiator_id(SCSI *usalp);
305 static  int     usalo_disatapi(SCSI *usalp);
306 static  int     usalo_dreset(SCSI *usalp, int what);
307
308 usal_ops_t usal_remote_ops = {
309         usalo_dsend,
310         usalo_dversion,
311         usalo_nohelp,
312         usalo_ropen,
313         usalo_dclose,
314         usalo_dmaxdma,
315         usalo_dgetbuf,
316         usalo_dfreebuf,
317         usalo_dhavebus,
318         usalo_dfileno,
319         usalo_dinitiator_id,
320         usalo_disatapi,
321         usalo_dreset,
322   usalo_natname,
323 };
324
325 usal_ops_t usal_dummy_ops = {
326         usalo_dsend,
327         usalo_dversion,
328         usalo_dhelp,
329         usalo_dopen,
330         usalo_dclose,
331         usalo_dmaxdma,
332         usalo_dgetbuf,
333         usalo_dfreebuf,
334         usalo_dhavebus,
335         usalo_dfileno,
336         usalo_dinitiator_id,
337         usalo_disatapi,
338         usalo_dreset,
339   usalo_natname,
340 };
341
342 /*
343  *      Warning: you may change this source, but if you do that
344  *      you need to change the _usal_version and _usal_auth* string below.
345  *      You may not return "schily" for an SCG_AUTHOR request anymore.
346  *      Choose your name instead of "schily" and make clear that the version
347  *      string is related to a modified source.
348  */
349 static  char    _usal_trans_dversion[] = "scsihack.c-1.44";     /* The version for this transport*/
350
351 /*
352  * Return version information for the low level SCSI transport code.
353  * This has been introduced to make it easier to trace down problems
354  * in applications.
355  */
356 static char *
357 usalo_dversion(SCSI *usalp, int what)
358 {
359         if (usalp != (SCSI *)0) {
360                 switch (what) {
361
362                 case SCG_VERSION:
363                         return (_usal_trans_dversion);
364                 /*
365                  * If you changed this source, you are not allowed to
366                  * return "schily" for the SCG_AUTHOR request.
367                  */
368                 case SCG_AUTHOR:
369                         return (_usal_auth_cdrkit);
370                 case SCG_SCCS_ID:
371                         return (_sccsid);
372                 }
373         }
374         return ((char *)0);
375 }
376
377 static int
378 usalo_dhelp(SCSI *usalp, FILE *f)
379 {
380         printf("None.\n");
381         return (0);
382 }
383
384 static int
385 usalo_nohelp(SCSI *usalp, FILE *f)
386 {
387         return (0);
388 }
389
390 static int
391 usalo_ropen(SCSI *usalp, char *device)
392 {
393         comerrno(EX_BAD, "No remote SCSI transport available.\n");
394         return (-1);    /* Keep lint happy */
395 }
396
397 #ifndef SCSI_IMPL
398 static int
399 usalo_dopen(SCSI *usalp, char *device)
400 {
401         comerrno(EX_BAD, "No local SCSI transport implementation for this architecture.\n");
402         return (-1);    /* Keep lint happy */
403 }
404 #else
405 static int
406 usalo_dopen(SCSI *usalp, char *device)
407 {
408         comerrno(EX_BAD, "SCSI open usage error.\n");
409         return (-1);    /* Keep lint happy */
410 }
411 #endif  /* SCSI_IMPL */
412
413 static int
414 usalo_dclose(SCSI *usalp)
415 {
416         errno = EINVAL;
417         return (-1);
418 }
419
420 static long
421 usalo_dmaxdma(SCSI *usalp, long amt)
422 {
423         errno = EINVAL;
424         return  (0L);
425 }
426
427 static void *
428 usalo_dgetbuf(SCSI *usalp, long amt)
429 {
430         errno = EINVAL;
431         return ((void *)0);
432 }
433
434 static void
435 usalo_dfreebuf(SCSI *usalp)
436 {
437 }
438
439 static BOOL
440 usalo_dhavebus(SCSI *usalp, int busno)
441 {
442         return (FALSE);
443 }
444
445 static int
446 usalo_dfileno(SCSI *usalp, int busno, int tgt, int tlun)
447 {
448         return (-1);
449 }
450
451 #ifndef HAVE_NAT_NAMES /* to be defined in included source if supported */
452 static char * usalo_natname(SCSI *usalp, int busno, int tgt, int tlun) {
453    static char namebuf[81];
454    snprintf(namebuf, 80, "%d,%d,%d", usal_scsibus(usalp), usal_target(usalp), usal_lun(usalp));
455    return namebuf;
456 }
457 #endif
458
459 static int
460 usalo_dinitiator_id(SCSI *usalp)
461 {
462         return (-1);
463 }
464
465 static int
466 usalo_disatapi(SCSI *usalp)
467 {
468         return (FALSE);
469 }
470
471 static int
472 usalo_dreset(SCSI *usalp, int what)
473 {
474         errno = EINVAL;
475         return (-1);
476 }
477
478 static int
479 usalo_dsend(SCSI *usalp)
480 {
481         errno = EINVAL;
482         return (-1);
483 }