2 * $Id: scsi-chio.c,v 1.13 2000/06/25 18:48:11 ant Exp $
4 * scsi-chio.c -- library routines to handle the changer
5 * support for chio based systems
7 * Author: Eric Schnoebelen, eric@cirr.com
8 * based on work by: Larry Pyeatt, pyeatt@cs.colostate.edu
9 * Copyright: 1997, 1998 Eric Schnoebelen
16 #if (defined(HAVE_CHIO_H) || defined(HAVE_SYS_CHIO_H)) \
17 && !defined(HAVE_CAMLIB_H)
19 #include <sys/types.h>
20 #include <sys/ioctl.h>
23 /* This include comes with Gerd Knor's SCSI media changer driver.
24 * If you are porting to another system, this is the file that defines
25 * ioctl calls for the changer. You will have to track it down yourself
26 * and possibly change all the ioctl() calls in this program.
29 #if defined(HAVE_CHIO_H)
31 #else /* HAVE_SYS_CHIO_H must be defined */
32 # include <sys/chio.h>
35 char *modname = "@(#)" __FILE__
36 ": SCSI support library for the chio(2) interface @(#)";
39 * cache the general changer information, for faster access elsewhere
41 static struct changer_params changer_info;
42 static int changer_info_init = 0;
44 static int get_changer_info(fd)
48 if ( !changer_info_init ) {
49 rc = ioctl(fd, CHIOGPARAMS, &changer_info);
55 /* Get the number of the first free slot
56 * return > 0 number of empty slot
57 * return = 0 no slot free
60 int GetCurrentSlot(int fd, int drive)
62 struct changer_element_status ces;
68 ces.ces_type = CHET_ST;
69 ces.ces_data = malloc(changer_info.cp_nslots);
71 rc = ioctl(fd, CHIOGSTATUS, &ces);
73 dbprintf(("%s: changer status query failed: 0x%x %s\n",
74 get_pname(), rc,strerror(errno)));
77 for (slot = 0; slot < changer_info.cp_nslots; slot++)
79 i = ces.ces_data[slot] & CESTATUS_FULL;
80 dbprintf(("\tGetCurrentSlot slot %d = %d\n", slot, i));
88 int get_clean_state(int changerfd, char *changerdev, char *dev)
93 void eject_tape(char *tape)
94 /* This function ejects the tape from the drive */
99 if ((mtfd = open(tape, O_RDWR)) < 0) {
100 dbprintf(("eject_tape : failed\n"));
104 mt_com.mt_op = MTOFFL;
106 if (ioctl(mtfd, MTIOCTOP, (char *)&mt_com) < 0) {
108 If the drive already ejected the tape due an error, or because it
109 was a cleaning tape, threre can be an error, which we should ignore
120 * this routine checks a specified slot to see if it is empty
122 int isempty(int fd, int slot)
124 struct changer_element_status ces;
128 get_changer_info(fd);
131 ces.ces_data = malloc(changer_info.cp_nslots);
133 rc = ioctl(fd, CHIOGSTATUS, &ces);
135 dbprintf(("%s: changer status query failed: 0x%x %s\n",
136 get_pname(), rc,strerror(errno)));
140 i = ces.ces_data[slot] & CESTATUS_FULL;
147 * find the first empty slot
149 int find_empty(int fd, int start, int count)
151 struct changer_element_status ces;
155 get_changer_info(fd);
158 ces.ces_data = malloc(changer_info.cp_nslots);
160 rc = ioctl(fd,CHIOGSTATUS,&ces);
162 dbprintf(("%s: changer status query failed: 0x%x %s\n",
163 get_pname(), rc, strerror(errno)));
168 while ((i < changer_info.cp_nslots)&&(ces.ces_data[i] & CESTATUS_FULL))
175 * returns one if there is a tape loaded in the drive
177 int drive_loaded(int fd, int drivenum)
179 struct changer_element_status ces;
183 get_changer_info(fd);
186 ces.ces_data = malloc(changer_info.cp_ndrives);
188 rc = ioctl(fd, CHIOGSTATUS, &ces);
190 dbprintf(("%s: drive status query failed: 0x%x %s\n",
191 get_pname(), rc, strerror(errno)));
195 i = (ces.ces_data[drivenum] & CESTATUS_FULL);
203 * unloads the drive, putting the tape in the specified slot
205 int unload(int fd, int drive, int slot)
207 struct changer_move move;
210 dbprintf(("unload : fd = %d, drive = %d, slot =%d\n",fd, drive, slot));
212 move.cm_fromtype = CHET_DT;
213 move.cm_fromunit = drive;
214 move.cm_totype = CHET_ST;
215 move.cm_tounit = slot;
218 rc = ioctl(fd, CHIOMOVE, &move);
220 dbprintf(("%s: drive unload failed (MOVE): 0x%x %s\n",
221 get_pname(), rc, strerror(errno)));
229 * moves tape from the specified slot into the drive
231 int load(int fd, int drive, int slot)
233 struct changer_move move;
236 dbprintf(("load : fd = %d, drive = %d, slot =%d\n",fd, drive, slot));
238 move.cm_fromtype = CHET_ST;
239 move.cm_fromunit = slot;
240 move.cm_totype = CHET_DT;
241 move.cm_tounit = drive;
244 rc = ioctl(fd,CHIOMOVE,&move);
246 dbprintf(("%s: drive load failed (MOVE): 0x%x %s\n",
247 get_pname(), rc, strerror(errno)));
253 int get_slot_count(int fd)
257 rc = get_changer_info(fd);
259 dbprintf(("%s: slot count query failed: 0x%x %s\n",
260 get_pname(), rc, strerror(errno)));
264 return changer_info.cp_nslots;
267 int get_drive_count(int fd)
271 rc = get_changer_info(fd);
273 dbprintf(("%s: drive count query failed: 0x%x %s\n",
274 get_pname(), rc, strerror(errno)));
278 return changer_info.cp_ndrives;
281 /* This function should ask the drive if it is ready */
282 int Tape_Ready ( char *tapedev , int wait)
287 while ((cnt < wait) && (NULL==(out=fopen(tapedev,"w+")))){
296 int OpenDevice (char *tapedev)
300 DeviceFD = open(tapedev, O_RDWR);
304 int CloseDevice (char *device, int DeviceFD)
308 ret = close(DeviceFD);
316 * indent-tabs-mode: nil
317 * c-default-style: gnu