home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / CMDS / mtools_3.6.src.lzh / MTOOLS_3.6 / scsi.c < prev    next >
Text File  |  1997-11-12  |  4KB  |  177 lines

  1. /*
  2.  * scsi.c
  3.  * Iomega Zip/Jaz drive tool
  4.  * change protection mode and eject disk
  5.  */
  6.  
  7. /* scis.c by Markus Gyger <mgyger@itr.ch> */
  8. /* This code is based on ftp://gear.torque.net/pub/ziptool.c */
  9. /* by Grant R. Guenther with the following copyright notice: */
  10.  
  11. /*  (c) 1996   Grant R. Guenther,  based on work of Itai Nahshon  */
  12. /*  http://www.torque.net/ziptool.html  */
  13.  
  14.  
  15. /* A.K. Moved this from mzip.c to a separate file in order to share with
  16.  * plain_io.c */
  17.  
  18. #include "sysincludes.h"
  19. #include "mtools.h"
  20. #include "scsi.h"
  21.  
  22. #if defined hpux
  23. #include <sys/scsi.h>
  24. #endif
  25.  
  26. #ifdef solaris
  27. #include <sys/scsi/scsi.h>
  28. #endif /* solaris */
  29.  
  30. #ifdef sunos
  31. #include <scsi/generic/commands.h>
  32. #include <scsi/impl/uscsi.h>
  33. #endif /* sunos */
  34.  
  35. #ifdef linux
  36. #define SCSI_IOCTL_SEND_COMMAND 1
  37. struct scsi_ioctl_command {
  38.     int  inlen;
  39.     int  outlen;
  40.     char cmd[5008];
  41. };
  42. #endif
  43.  
  44. #ifdef _SCO_DS
  45. #include <sys/scsicmd.h>
  46. #endif
  47.  
  48. int scsi_max_length(void)
  49. {
  50. #ifdef linux
  51.     return 8;
  52. #else
  53.     return 255;
  54. #endif
  55. }
  56.  
  57. int scsi_cmd(int fd, unsigned char *cdb, int cmdlen, scsi_io_mode_t mode,
  58.          void *data, size_t len)
  59. {
  60. #if defined hpux
  61.     struct sctl_io sctl_io;
  62.     
  63.     memset(&sctl_io, 0, sizeof sctl_io);   /* clear reserved fields */
  64.     memcpy(sctl_io.cdb, cdb, cmdlen);      /* copy command */
  65.     sctl_io.cdb_length = cmdlen;           /* command length */
  66.     sctl_io.max_msecs = 2000;              /* allow 2 seconds for cmd */
  67.  
  68.     switch (mode) {
  69.         case SCSI_IO_READ:
  70.             sctl_io.flags = SCTL_READ;
  71.             sctl_io.data_length = len;
  72.             sctl_io.data = data;
  73.             break;
  74.         case SCSI_IO_WRITE: 
  75.             sctl_io.flags = 0;
  76.             sctl_io.data_length = data ? len : 0;
  77.             sctl_io.data = len ? data : 0;
  78.             break;
  79.     }
  80.  
  81.     if (ioctl(fd, SIOC_IO, &sctl_io) == -1) {
  82.         perror("scsi_io");
  83.         return -1;
  84.     }
  85.  
  86.     return sctl_io.cdb_status;
  87.     
  88. #elif defined sunos || defined solaris
  89.     struct uscsi_cmd uscsi_cmd;
  90.     memset(&uscsi_cmd, 0, sizeof uscsi_cmd);
  91.     uscsi_cmd.uscsi_cdb = (char *)cdb;
  92.     uscsi_cmd.uscsi_cdblen = cmdlen;
  93. #ifdef solaris
  94.     uscsi_cmd.uscsi_timeout = 20;  /* msec? */
  95. #endif /* solaris */
  96.     
  97.     uscsi_cmd.uscsi_buflen = (u_int)len;
  98.     uscsi_cmd.uscsi_bufaddr = data;
  99.  
  100.     switch (mode) {
  101.         case SCSI_IO_READ:
  102.             uscsi_cmd.uscsi_flags = USCSI_READ;
  103.             break;
  104.         case SCSI_IO_WRITE:
  105.             uscsi_cmd.uscsi_flags = USCSI_WRITE;
  106.             break;
  107.     }
  108.  
  109.     if (ioctl(fd, USCSICMD, &uscsi_cmd) == -1) {
  110.         perror("scsi_io");
  111.         return -1;
  112.     }
  113.  
  114.     if(uscsi_cmd.uscsi_status) {
  115.         errno = 0;
  116.         fprintf(stderr,"scsi status=%x\n",  
  117.             (unsigned short)uscsi_cmd.uscsi_status);
  118.         return -1;
  119.     }
  120.     
  121.     return 0;
  122.     
  123. #elif defined linux
  124.     struct scsi_ioctl_command scsi_cmd;
  125.  
  126.  
  127.     memcpy(scsi_cmd.cmd, cdb, cmdlen);        /* copy command */
  128.  
  129.     switch (mode) {
  130.         case SCSI_IO_READ:
  131.             scsi_cmd.inlen = 0;
  132.             scsi_cmd.outlen = len;
  133.             break;
  134.         case SCSI_IO_WRITE:
  135.             scsi_cmd.inlen = len;
  136.             scsi_cmd.outlen = 0;
  137.             memcpy(scsi_cmd.cmd + cmdlen,data,len);
  138.             break;
  139.     }
  140.     
  141.     if (ioctl(fd, SCSI_IOCTL_SEND_COMMAND, &scsi_cmd) < 0) {
  142.         perror("scsi_io");
  143.         return -1;
  144.     }
  145.     
  146.     switch (mode) {
  147.         case SCSI_IO_READ:
  148.             memcpy(data, &scsi_cmd.cmd[0], len);
  149.             break;
  150.         case SCSI_IO_WRITE:
  151.             break;
  152.     }
  153.  
  154.     return 0;  /* where to get scsi status? */
  155.  
  156. #elif defined _SCO_DS
  157.     struct scsicmd scsi_cmd;
  158.  
  159.     memset(scsi_cmd.cdb, 0, SCSICMDLEN);    /* ensure zero pad */
  160.     memcpy(scsi_cmd.cdb, cdb, cmdlen);
  161.     scsi_cmd.cdb_len = cmdlen;
  162.     scsi_cmd.data_len = len;
  163.     scsi_cmd.data_ptr = data;
  164.     scsi_cmd.is_write = mode == SCSI_IO_WRITE;
  165.     if (ioctl(fd,SCSIUSERCMD,&scsi_cmd) == -1) {
  166.         perror("scsi_io");
  167.         printf("scsi status: host=%x; target=%x\n",
  168.         (unsigned)scsi_cmd.host_sts,(unsigned)scsi_cmd.target_sts);
  169.         return -1;
  170.     }
  171.     return 0;    
  172. #else
  173.     fprintf(stderr, "scsi_io not implemented\n");
  174.     return -1;
  175. #endif
  176. }
  177.