/*****************************************************************************
** acptlib.h:  Header file for autochanger passthru library functions.  
**             Contains function declarations and constants useful for issuing 
**             passthru commands to autochanger devices.
**
** Jon Hirota 10/94
**
** $Header: acptlib.h,v 1.4 95/10/25 17:27:44 hmgr Exp $
**
**      Unpublished Confidential Information of Hewlett-Packard
**      Company. Do not disclose.  Copyright (c) Hewlett-Packard
**      Company 1995. All rights reserved
** 
** The contents of this software are proprietary and confidential to
** the Hewlett- Packard Company, and are limited in distribution to
** those with a direct need to know.  Individuals having access to
** this software are responsible for maintaining the confidentiality
** of the content and for keeping the software secure when not in use.
** Transfer to any party is strictly forbidden other than as expressly
** permitted in writing by Hewlett-Packard Company.  Unauthorized
** transfer to or possession by any unauthorized party may be a
** criminal offense.
**
**                  RESTRICTED RIGHTS LEGEND
**
**      Use,  duplication,  or disclosure by the U. S. Government is
**      subject to restrictions as set forth in subparagraph (c) (1)
**      (ii) in the Rights in Technical Data and Computer Software
**      clause in DFARS 252.227-7013 or any other successor clause.
**
**                      HEWLETT-PACKARD COMPANY
**                      3000 Hanover St.
**                      Palo Alto, CA  94304 U.S.A.
**
**      Rights for non-DOD U.S. Government Departments and Agencies
**      are as set forth in FAR 52.227-19 (c) (1,2) or any other
**      successor clause.
*****************************************************************************/

#ifndef _ACPTLIB_H_
#define _ACPTLIB_H_

#include <string.h>             /* for memset() declaration */
#include <sys/scsi.h>           /* For struct sctl_io definition */

#ifdef __cplusplus
extern "C" {
#endif

/*
** Function declarations
*/

#ifndef __STDC__

int     acpt_exchange();
int     acpt_init_element_status();
int     acpt_inquiry();
int     acpt_log_sense();
int     acpt_mode_sense();
int     acpt_move();
int     acpt_position();
int     acpt_prevent_mr();
int     acpt_allow_mr();
int     acpt_element_status();
int     acpt_diag_results();
int     acpt_release();
int     acpt_request_sense();
int     acpt_reserve();
int     acpt_rezero_unit();
int     acpt_rotate_mailslot();
int     acpt_send_diagnostic();
int     acpt_test_unit();
int     acpt_write_buffer();

#else           /* For ANSI C and C++:  robust prototypes */

int     acpt_exchange(struct sctl_io *sctl_io,int trans,int src,int dest1,
                      int dest2,int flip1,int flip2);
int     acpt_init_element_status(struct sctl_io *sctl_io);
int     acpt_inquiry(struct sctl_io *sctl_io,char *buffer,int len);
int     acpt_log_sense(struct sctl_io *sctl_io,int pc,char *buffer,int len);
int     acpt_mode_sense(struct sctl_io *sctl_io,int pc,char *buffer,int len);
int     acpt_move(struct sctl_io *sctl_io,int trans,int src,int dest,int flip);
int     acpt_position(struct sctl_io *sctl_io,int trans,int dest,int flip);
int     acpt_prevent_mr(struct sctl_io *sctl_io);
int     acpt_allow_mr(struct sctl_io *sctl_io);
int     acpt_element_status(struct sctl_io *sctl_io,int tc,int elt,int nelt,
                            char *buffer,int len);
int     acpt_diag_results(struct sctl_io *sctl_io,char *buffer,int len);
int     acpt_release(struct sctl_io *sctl_io,int tpty,int tptyid,int elt,
                     int resvid);
int     acpt_request_sense(struct sctl_io *sctl_io,char *buffer,int len);
int     acpt_reserve(struct sctl_io *sctl_io,int elt,int tpty,int tptyid,
                     int all,int resvid,char *buffer);
int     acpt_rezero_unit(struct sctl_io *sctl_io);
int     acpt_rotate_mailslot(struct sctl_io *sctl_io,int open);
int     acpt_send_diagnostic(struct sctl_io *sctl_io,int selftest,int devofl,
                             int unitofl,char *buffer,int len);
int     acpt_test_unit(struct sctl_io *sctl_io);
int     acpt_write_buffer(struct sctl_io *sctl_io,int mode,int bufferid,
                          int b_off,char *buffer,int len);

#endif

/*
** Some useful macros.  Don't remove these, as the functions in acptlib.c
** depend on them.
*/

#define BZERO(ptr,len)          memset((void *) (ptr),0,(len))
#define BYTE0(obj)              ((obj) & 0xFF)
#define BYTE1(obj)              (((obj) >> 8) & 0xFF)
#define BYTE2(obj)              (((obj) >> 16) & 0xFF)
#define BYTE3(obj)              (((obj) >> 24) & 0xFF)

/*
** This macro is useful for setting the Logical Unit Number field in the
** sctl_io struct's cdb (bits 5-7 in byte 1).  Since the acptlib functions are
** designed to work specifically with HP magneto-optical autochangers, which
** use a value of 0 for this field, this macro is provided to facilitate
** use of acptlib with non-HP medium changer devices.
**
** ACPT_SET_LUN(struct sctl_io *sctl_io,int lun)
*/

#define ACPT_SET_LUN(sctl_io,lun)   ((sctl_io)->cdb[1] |= (((lun) << 5) & 0xE0))

/*
** This macro sets the timeout value (in milliseconds).  
**
** ACPT_SET_TIMEOUT(struct sctl_io *sctl_io,int msecs)
*/

#define ACPT_SET_TIMEOUT(sctl_io,msecs)    ((sctl_io)->max_msecs = msecs)

/*
** SCSI command opcode constants.  They are defined here, rather than using
** the definitions in <sys/scsi.h>, because scsi.h does not define all 
** autochanger commands.  Additionally, since this library is intended to
** provide all commands understood by autochangers, some of which overlap
** with other devices (e.g., inquiry, test unit ready), we seek to avoid
** possible name conflicts with other libraries.
*/

#define ACCMDexchange_medium                    0xA6
#define ACCMDinit_element_status                0x07
#define ACCMDinquiry                            0x12
#define ACCMDlog_sense                          0x4D
#define ACCMDmode_sense                         0x1A
#define ACCMDmove_medium                        0xA5
#define ACCMDposition_to_element                0x2B
#define ACCMDprevent_media_removal              0x1E
#define ACCMDallow_media_removal                0x1E
#define ACCMDread_element_status                0xB8
#define ACCMDreceive_diagnostic_results         0x1C
#define ACCMDrelease                            0x17
#define ACCMDrequest_sense                      0x03
#define ACCMDreserve                            0x16
#define ACCMDrezero_unit                        0x01
#define ACCMDrotate_mailslot                    0x0C
#define ACCMDsend_diagnostic                    0x1D
#define ACCMDtest_unit_ready                    0x00
#define ACCMDwrite_buffer                       0x3B

/*
** SCSI command CDB lengths
*/

#define ACCDBexchange_medium                    12
#define ACCDBinit_element_status                6
#define ACCDBinquiry                            6
#define ACCDBlog_sense                          10
#define ACCDBmode_sense                         6
#define ACCDBmove_medium                        12
#define ACCDBposition_to_element                10
#define ACCDBprevent_media_removal              6
#define ACCDBallow_media_removal                6
#define ACCDBread_element_status                12
#define ACCDBreceive_diagnostic_results         6
#define ACCDBrelease                            6
#define ACCDBrequest_sense                      6
#define ACCDBreserve                            6
#define ACCDBrezero_unit                        6
#define ACCDBrotate_mailslot                    6
#define ACCDBsend_diagnostic                    6
#define ACCDBtest_unit_ready                    6
#define ACCDBwrite_buffer                       10

/*
** Default timeout values (in msecs)
*/

#define ACTIMEOUTexchange_medium                600000  /* 10 minutes */
#define ACTIMEOUTinit_element_status            600000  /* 10 minutes */
#define ACTIMEOUTinquiry                        10000   /* 10 seconds */
#define ACTIMEOUTlog_sense                      60000   /* 1 minute */
#define ACTIMEOUTmode_sense                     60000   /* 1 minute */
#define ACTIMEOUTmove_medium                    600000  /* 10 minutes */
#define ACTIMEOUTposition_to_element            600000  /* 10 minutes */
#define ACTIMEOUTprevent_media_removal          10000   /* 10 seconds */
#define ACTIMEOUTallow_media_removal            10000   /* 10 seconds */
#define ACTIMEOUTread_element_status            10000   /* 10 seconds */
#define ACTIMEOUTreceive_diagnostic_results     60000   /* 1 minute */
#define ACTIMEOUTrelease                        10000   /* 10 seconds */
#define ACTIMEOUTrequest_sense                  10000   /* 10 seconds */
#define ACTIMEOUTreserve                        10000   /* 10 seconds */
#define ACTIMEOUTrezero_unit                    60000   /* 1 minute */
#define ACTIMEOUTrotate_mailslot                60000   /* 1 minute */
#define ACTIMEOUTsend_diagnostic                600000  /* 10 minutes */
#define ACTIMEOUTtest_unit_ready                10000   /* 10 seconds */
#define ACTIMEOUTwrite_buffer                   600000  /* 10 minutes */

/*
** Constants for INQUIRY command
*/

#define AC_INQUIRY_LENGTH                       0x3C

/*
** Constants for LOG SENSE command
*/

#define ACLOG_MAXDRIVES                         16      

#define ACLOG_SUPPORTED_PAGES                   0x00
#define ACLOG_ERROR_LOG                         0x30
#define ACLOG_MOVE_SUCCESS_LOG                  0x31
#define ACLOG_FORCE_LOG                         0x32
#define ACLOG_RECOVERY_LOG                      0x33
#define ACLOG_DRIVE_LOG                         0x35
#define ACLOG_VERSION                           0x36
#define ACLOG_ODOMETER_LOG                      0x37
#define ACLOG_RUN_TIME_LOG                      0x38
#define ACLOG_RETRY_LOG                         0x39
#define ACLOG_MOVE_HISTORY_LOG                  0x3A

#define ACLOG_SUPPORTED_PAGES_LENGTH            20
#define ACLOG_ERROR_LOG_LENGTH                  510
#define ACLOG_MOVE_SUCCESS_LOG_LENGTH           72
#define ACLOG_FORCE_LOG_LENGTH                  1024  /* approximate */
#define ACLOG_RECOVERY_LOG_LENGTH               406
#define ACLOG_DRIVE_LOG_LENGTH                  (ACLOG_MAXDRIVES*10)
#define ACLOG_VERSION_LENGTH                    6
#define ACLOG_ODOMETER_LOG_LENGTH               24
#define ACLOG_RUN_TIME_LOG_LENGTH               116
#define ACLOG_RETRY_LOG_LENGTH                  108
#define ACLOG_MOVE_HISTORY_LOG_LENGTH           128   /* approximate */

/*
** Constants for the MODE SENSE command 
*/

#define ACMODE_ELTADDR_PAGE                     0x1D
#define ACMODE_TRANSPORT_ELEMENT_PAGE           0x1E
#define ACMODE_DEVICE_CAPABILITIES_PAGE         0x1F
#define ACMODE_ALL_PAGES                        0x3F

#define ACMODE_ELTADDR_PAGE_LENGTH              24
#define ACMODE_TRANSPORT_ELEMENT_PAGE_LENGTH    8
#define ACMODE_DEVICE_CAPABILITIES_PAGE_LENGTH  24
#define ACMODE_ALL_PAGES_LENGTH                 56

/*
** Constants for the READ ELEMENT STATUS command
*/

#define ELTSTAT_ALL_ELEMENTS                    0x00
#define ELTSTAT_MEDIUM_TRANSPORT_ELEMENT        0x01
#define ELTSTAT_STORAGE_ELEMENT                 0x02
#define ELTSTAT_IMPORT_EXPORT_ELEMENT           0x03
#define ELTSTAT_DATA_TRANSFER_ELEMENT           0x04

#define ELTSTAT_HEADER_LENGTH                   8
#define ELTSTAT_TRANSPORT_PAGE_LENGTH           12
#define ELTSTAT_STORAGE_PAGE_LENGTH             12
#define ELTSTAT_MAILSLOT_PAGE_LENGTH            12
#define ELTSTAT_DATA_TRANSFER_PAGE_LENGTH       20

/*
** This following constant must be defined as the maximum of the page lengths 
** above.                                                                
*/

#define ELTSTAT_MAX_DATA_LENGTH         ELTSTAT_DATA_TRANSFER_PAGE_LENGTH

/*
** Constants for the RECEIVE DIAGNOSTIC RESULTS command
*/

#define AC_DIAG_RESULTS_LENGTH                  14

/*
** Constants for the REQUEST SENSE command
*/

#define AC_REQUEST_SENSE_LENGTH                 78

/*
** Constants for the RESERVE command
*/

#define AC_RESERVE_LIST_LENGTH                  6
#define AC_RESERVE_NUM_ELEMENTS                 0x01    /* one at a time! */

/*
** Constants for the SEND DIAGNOSTIC command
*/

#define AC_SEND_DIAGNOSTIC_LENGTH               10

/*
** Constants for the WRITE BUFFER command
*/

#define AC_DOWNLOAD_MICROCODE                   4
#define AC_CUSTOMER_ID                          1
#define AC_DOWNLOAD_MICROCODE_ID                1
#define AC_CUSTOMER_ID_ID                       15

#ifdef __cplusplus
}
#endif

#endif  /* _ACPTLIB_H_ */
