2 * This file has been modified for the cdrkit suite.
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).
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.
13 /* @(#)scsi-beos.c 1.23 06/02/05 Copyright 1998 J. Schilling */
15 * Interface for the BeOS user-land raw SCSI implementation.
17 * This is a hack, that tries to emulate the functionality
20 * First version done by swetland@be.com
22 * Warning: you may change this source, but if you do that
23 * you need to change the _usal_version and _usal_auth* string below.
24 * You may not return "schily" for an SCG_AUTHOR request anymore.
25 * Choose your name instead of "schily" and make clear that the version
26 * string is related to a modified source.
28 * Copyright (c) 1998 J. Schilling
31 * This program is free software; you can redistribute it and/or modify
32 * it under the terms of the GNU General Public License version 2
33 * as published by the Free Software Foundation.
35 * This program is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 * GNU General Public License for more details.
40 * You should have received a copy of the GNU General Public License along with
41 * this program; see the file COPYING. If not, write to the Free Software
42 * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
47 * Warning: you may change this source, but if you do that
48 * you need to change the _usal_version and _usal_auth* string below.
49 * You may not return "schily" for an SCG_AUTHOR request anymore.
50 * Choose your name instead of "schily" and make clear that the version
51 * string is related to a modified source.
53 static char _usal_trans_version[] = "scsi-beos.c-1.23"; /* The version for this transport*/
56 * There are also defines for:
62 #ifndef B_BEOS_VERSION_5
64 * New BeOS seems to include <be/kernel/OS.h> from device/scsi.h
67 /* nasty hack to avoid broken def of bool in SupportDefs.h */
68 #define _SUPPORT_DEFS_H
71 typedef unsigned long ulong;
72 typedef unsigned int uint;
73 typedef unsigned short ushort;
74 #endif /* _SYS_TYPES_H */
77 #include <sys/types.h>
81 /*-------------------------------------------------------------*/
82 /*----- Shorthand type formats --------------------------------*/
84 typedef signed char int8;
85 typedef unsigned char uint8;
86 typedef volatile signed char vint8;
87 typedef volatile unsigned char vuint8;
90 typedef unsigned short uint16;
91 typedef volatile short vint16;
92 typedef volatile unsigned short vuint16;
95 typedef unsigned long uint32;
96 typedef volatile long vint32;
97 typedef volatile unsigned long vuint32;
99 typedef long long int64;
100 typedef unsigned long long uint64;
101 typedef volatile long long vint64;
102 typedef volatile unsigned long long vuint64;
104 typedef volatile long vlong;
105 typedef volatile int vint;
106 typedef volatile short vshort;
107 typedef volatile char vchar;
109 typedef volatile unsigned long vulong;
110 typedef volatile unsigned int vuint;
111 typedef volatile unsigned short vushort;
112 typedef volatile unsigned char vuchar;
114 typedef unsigned char uchar;
115 typedef unsigned short unichar;
119 /*-------------------------------------------------------------*/
120 /*----- Descriptive formats -----------------------------------*/
121 typedef int32 status_t;
122 typedef int64 bigtime_t;
123 typedef uint32 type_code;
124 typedef uint32 perform_code;
128 #endif /* ! B_BEOS_VERSION_5 */
135 #include <sys/stat.h>
136 #include <usal/usalio.h>
138 /* this is really really dumb (tm) */
141 #include <device/scsi.h>
144 #include <drivers/CAM.h>
147 struct _fdmap_ *next;
155 * Return version information for the low level SCSI transport code.
156 * This has been introduced to make it easier to trace down problems
160 usalo_version(SCSI *usalp, int what)
162 if (usalp != (SCSI *)0) {
166 return (_usal_trans_version);
168 * If you changed this source, you are not allowed to
169 * return "schily" for the SCG_AUTHOR request.
172 return (_usal_auth_cdrkit);
181 usalo_help(SCSI *usalp, FILE *f)
183 __usal_help(f, "CAM", "Generic transport independent SCSI (BeOS CAM variant)",
184 "", "bus,target,lun", "1,2,0", TRUE, FALSE);
189 usalo_open(SCSI *usalp, char *device)
191 int busno = usal_scsibus(usalp);
192 int tgt = usal_target(usalp);
194 int tlun = usal_lun(usalp);
198 if (busno >= MAX_SCG || tgt >= MAX_TGT || tlun >= MAX_LUN) {
201 snprintf(usalp->errstr, SCSI_ERRSTR_SIZE,
202 "Illegal value for busno, target or lun '%d,%d,%d'",
208 if ((device != NULL && *device != '\0') || (busno == -2 && tgt == -2)) {
211 snprintf(usalp->errstr, SCSI_ERRSTR_SIZE,
212 "Open by 'devname' not supported on this OS");
219 usalo_close(SCSI *usalp)
222 struct _fdmap_ *fnext;
224 for (f = (struct _fdmap_ *)usalp->local; f; f = fnext) {
234 usalo_maxdma(SCSI *usalp, long amt)
241 usalo_getbuf(SCSI *usalp, long amt)
243 if (usalp->debug > 0) {
244 fprintf((FILE *)usalp->errfile,
245 "usalo_getbuf: %ld bytes\n", amt);
247 usalp->bufbase = malloc((size_t)(amt));
248 return (usalp->bufbase);
252 usalo_freebuf(SCSI *usalp)
255 free(usalp->bufbase);
256 usalp->bufbase = NULL;
260 usalo_havebus(SCSI *usalp, int busno)
266 snprintf(buf, sizeof (buf), "/dev/bus/scsi/%d", busno);
268 snprintf(buf, sizeof (buf), "/dev/disk/ide/atapi/%d", busno-8);
275 usalo_fileno(SCSI *usalp, int busno, int tgt, int tlun)
281 for (f = (struct _fdmap_ *)usalp->local; f; f = f->next) {
282 if (f->bus == busno && f->targ == tgt && f->lun == tlun)
286 snprintf(buf, sizeof (buf),
287 "/dev/bus/scsi/%d/%d/%d/raw",
290 char *tgtstr = (tgt == 0) ? "master" : (tgt == 1) ? "slave" : "dummy";
291 snprintf(buf, sizeof (buf),
292 "/dev/disk/ide/atapi/%d/%s/%d/raw",
293 busno-8, tgtstr, tlun);
298 f = (struct _fdmap_ *) malloc(sizeof (struct _fdmap_));
303 f->next = (struct _fdmap_ *)usalp->local;
310 usalo_initiator_id(SCSI *usalp)
316 usalo_isatapi(SCSI *usalp)
319 * XXX Should check for ATAPI
325 usalo_reset(SCSI *usalp, int what)
332 usalo_send(SCSI *usalp)
334 struct usal_cmd *sp = usalp->scmd;
338 raw_device_command rdc;
341 sp->error = SCG_FATAL;
345 memcpy(rdc.command, &(sp->cdb), 12);
346 rdc.command_length = sp->cdb_len;
348 rdc.data_length = sp->size;
349 rdc.sense_data_length = sp->sense_len;
350 rdc.sense_data = sp->u_sense.cmd_sense;
351 rdc.flags = sp->flags & SCG_RECV_DATA ? B_RAW_DEVICE_DATA_IN : 0;
353 rdc.flags |= B_RAW_DEVICE_REPORT_RESIDUAL;
354 rdc.timeout = sp->timeout * 1000000;
356 sp->error = SCG_NO_ERROR;
358 sp->u_scb.cmd_scb[0] = 0;
361 if (usalp->debug > 0) {
362 fprintf(stderr, "SEND(%d): cmd %02x, cdblen = %d, datalen = %ld, senselen = %ld\n",
363 usalp->fd, rdc.command[0], rdc.command_length,
364 rdc.data_length, rdc.sense_data_length);
366 e = ioctl(usalp->fd, B_RAW_DEVICE_COMMAND, &rdc, sizeof (rdc));
367 if (usalp->debug > 0) {
368 fprintf(stderr, "SEND(%d): -> %d CAM Status %02X SCSI status %02X\n", e, rdc.cam_status, rdc.scsi_status);
372 fprintf(stderr, "err %d errno %x CAM %X SL %d DL %d/%d FL %X\n",
373 e, geterrno(), rdc.cam_status,
374 rdc.sense_data_length, rdc.data_length, sp->size, rdc.flags);
377 cam_error = rdc.cam_status;
378 scsi_error = rdc.scsi_status;
379 sp->u_scb.cmd_scb[0] = scsi_error;
381 sp->resid = sp->size - rdc.data_length;
383 switch (cam_error & CAM_STATUS_MASK) {
386 sp->error = SCG_NO_ERROR;
389 case CAM_REQ_CMP_ERR:
390 sp->sense_count = sp->sense_len; /* XXX */
391 sp->error = SCG_NO_ERROR;
395 case CAM_CMD_TIMEOUT:
396 sp->error = SCG_TIMEOUT;
399 case CAM_SEL_TIMEOUT:
400 sp->error = SCG_FATAL;
405 sp->error = SCG_RETRYABLE;
409 sp->error = SCG_FATAL;
410 sp->ux_errno = geterrno();
411 sp->resid = sp->size;