~ubuntu-branches/debian/sid/sg3-utils/sid

« back to all changes in this revision

Viewing changes to lib/sg_pt_linux.c

  • Committer: Bazaar Package Importer
  • Author(s): Michael Biebl
  • Date: 2009-11-05 20:42:13 UTC
  • mfrom: (5.2.2 squeeze)
  • Revision ID: james.westby@ubuntu.com-20091105204213-ug7wrb6m7l9kmvyg
* QA upload.
* Add libsgutils2-2.symbols.kfreebsd. Some of the symbols are Linux
  specific causing FTBFS on kfreebsd.

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
 *
28
28
 */
29
29
 
30
 
/* sg_pt_linux version 1.11 20090308 */
 
30
/* sg_pt_linux version 1.12 20090507 */
31
31
 
32
32
#include <stdio.h>
33
33
#include <stdlib.h>
40
40
#include <sys/types.h>
41
41
#include <sys/stat.h>
42
42
 
 
43
 
43
44
#ifdef HAVE_CONFIG_H
44
45
#include "config.h"
45
46
#endif
101
102
 
102
103
// <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
103
104
#if defined(IGNORE_LINUX_BSG) || ! defined(HAVE_LINUX_BSG_H)
104
 
/* sgv3 via SG_IO ioctl on a sg node or other node that accepts that ioctl */
 
105
/*
 
106
 * sg(v3) via SG_IO ioctl on a sg node or other node that accepts that ioctl.
 
107
 * Decision has been made at compile time because either:
 
108
 *   a) no /usr/include/linux/bsg.h header file was found, or
 
109
 *   b) the builder gave the '--enable-no-linux-bsg' option to ./configure
 
110
 */
105
111
 
106
112
 
107
113
struct sg_pt_linux_scsi {
312
318
    /* io_hdr.timeout is in milliseconds */
313
319
    ptp->io_hdr.timeout = ((time_secs > 0) ? (time_secs * 1000) :
314
320
                                             DEF_TIMEOUT);
315
 
    if (ptp->io_hdr.sbp && (ptp->io_hdr.sb_len_wr > 0))
316
 
        memset(ptp->io_hdr.sbp, 0, ptp->io_hdr.sb_len_wr);
 
321
    if (ptp->io_hdr.sbp && (ptp->io_hdr.mx_sb_len > 0))
 
322
        memset(ptp->io_hdr.sbp, 0, ptp->io_hdr.mx_sb_len);
317
323
    if (ioctl(fd, SG_IO, &ptp->io_hdr) < 0) {
318
324
        ptp->os_err = errno;
319
325
        if (verbose)
452
458
 
453
459
 
454
460
// <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
455
 
#else /* allow for run selection of sg v3 or v4 (via bsg) */
 
461
#else /* allow for runtime selection of sg v3 or v4 (via bsg) */
456
462
// <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
 
463
/*
 
464
 * So bsg is an option. Thus we make a runtime decision. If all the following
 
465
 * are true we use sg v4 which is only currently supported on bsg device
 
466
 * nodes:
 
467
 *   a) there is a bsg entry in the /proc/devices file
 
468
 *   b) the device node given to scsi_pt_open() is a char device
 
469
 *   c) the char major number of the device node given to scsi_pt_open()
 
470
 *      matches the char major number of the bsg entry in /proc/devices
 
471
 * Otherwise the sg v3 interface is used.
 
472
 *
 
473
 * Note that in either case we prepare the data in a sg v4 structure. If
 
474
 * the runtime tests indicate that the v3 interface is needed then
 
475
 * do_scsi_pt_v3() transfers the input data into a v3 structure and
 
476
 * then the output data is transferred back into a sg v4 structure.
 
477
 * That implementation detail could change in the future.
 
478
 */
457
479
 
458
480
 
459
481
#include <linux/types.h>
460
482
#include <linux/bsg.h>
461
483
 
462
 
#define DEF_TIMEOUT 60000       /* 60,000 millisecs (60 seconds) */
 
484
#ifdef HAVE_LINUX_KDEV_T_H
 
485
#include <linux/kdev_t.h>
 
486
#endif
 
487
 
463
488
 
464
489
struct sg_pt_linux_scsi {
465
490
    struct sg_io_v4 io_hdr;     /* use v4 header as it is more general */
849
874
        v3_hdr.dxfer_len = (unsigned int)ptp->io_hdr.dout_xfer_len;
850
875
        v3_hdr.dxfer_direction =  SG_DXFER_TO_DEV;
851
876
    }
852
 
    if (ptp->io_hdr.response && (ptp->io_hdr.response_len > 0)) {
 
877
    if (ptp->io_hdr.response && (ptp->io_hdr.max_response_len > 0)) {
853
878
        v3_hdr.sbp = (void *)(long)ptp->io_hdr.response;
854
 
        v3_hdr.mx_sb_len = (unsigned char)ptp->io_hdr.request_len;
 
879
        v3_hdr.mx_sb_len = (unsigned char)ptp->io_hdr.max_response_len;
855
880
    }
856
881
    v3_hdr.pack_id = (int)ptp->io_hdr.spare_in;
857
882
 
860
885
            fprintf(sg_warnings_strm, "No SCSI command (cdb) given\n");
861
886
        return SCSI_PT_DO_BAD_PARAMS;
862
887
    }
863
 
    /* io_hdr.timeout is in milliseconds */
 
888
    /* io_hdr.timeout is in milliseconds, if greater than zero */
864
889
    v3_hdr.timeout = ((time_secs > 0) ? (time_secs * 1000) : DEF_TIMEOUT);
865
 
    if (v3_hdr.sbp && (v3_hdr.sb_len_wr > 0))
866
 
        memset(v3_hdr.sbp, 0, v3_hdr.sb_len_wr);
 
890
    /* Finally do the v3 SG_IO ioctl */
867
891
    if (ioctl(fd, SG_IO, &v3_hdr) < 0) {
868
892
        ptp->os_err = errno;
869
893
        if (verbose)
874
898
    ptp->io_hdr.device_status = (__u32)v3_hdr.status;
875
899
    ptp->io_hdr.driver_status = (__u32)v3_hdr.driver_status;
876
900
    ptp->io_hdr.transport_status = (__u32)v3_hdr.host_status;
 
901
    ptp->io_hdr.response_len = (__u32)v3_hdr.sb_len_wr;
877
902
    ptp->io_hdr.duration = (__u32)v3_hdr.duration;
878
903
    ptp->io_hdr.din_resid = (__s32)v3_hdr.resid;
 
904
    /* v3_hdr.info not passed back since no mapping defined (yet) */
879
905
    return 0;
880
906
}
881
907
 
913
939
                        "(errno) = %d\n", ptp->os_err);
914
940
            return -ptp->os_err;
915
941
        }
 
942
#ifdef HAVE_LINUX_KDEV_T_H
 
943
        if (! S_ISCHR(a_stat.st_mode) ||
 
944
            (bsg_major != (int)MAJOR(a_stat.st_rdev)))
 
945
            return do_scsi_pt_v3(ptp, fd, time_secs, verbose);
 
946
#else
916
947
        if (! S_ISCHR(a_stat.st_mode) ||
917
948
            (bsg_major != (int)major(a_stat.st_rdev)))
918
949
            return do_scsi_pt_v3(ptp, fd, time_secs, verbose);
 
950
#endif
919
951
    }
920
952
 
921
953
    if (! ptp->io_hdr.request) {