2 * $Id: scsi-chio.c,v 1.14 2006/05/25 01:47:07 johnfranks 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
11 * Patch: Michael Enkelis, michaele@mxim.com)
17 #include <sys/types.h>
18 #include <sys/ioctl.h>
21 /* This include comes with Gerd Knor's SCSI media changer driver.
22 * If you are porting to another system, this is the file that defines
23 * ioctl calls for the changer. You will have to track it down yourself
24 * and possibly change all the ioctl() calls in this program.
27 #if defined(HAVE_LINUX_CHIO_H)
28 # include <linux/chio.h>
30 # if defined(HAVE_CHIO_H)
32 # else /* HAVE_SYS_CHIO_H must be defined */
33 # include <sys/chio.h>
34 # endif /* HAVE_CHIO_H */
35 #endif /* HAVE_LINUX_CHIO_H */
37 char *modname = "@(#)" __FILE__
38 ": SCSI support library for the chio(2) interface @(#)";
41 * cache the general changer information, for faster access elsewhere
43 static struct changer_params changer_info;
44 static int changer_info_init = 0;
45 int GetCurrentSlot(int fd, int drive);
46 int GetDeviceStatus (char *tapedev);
47 int OpenDevice (char *tapedev);
48 int CloseDevice (char *device, int DeviceFD);
49 int Tape_Ready1 ( char *tapedev , int wait);
50 int isempty(int fd, int slot);
51 int find_empty(int fd, int start, int count);
52 int get_clean_state(char *tapedev);
53 int get_slot_count(int fd);
54 int get_drive_count(int fd);
55 int eject_tape(char *tapedev);
56 int drive_loaded(int fd, int drivenum);
57 int unload(int fd, int drive, int slot);
58 int load(int fd, int drive, int slot);
60 static int get_changer_info(int fd)
64 if ( !changer_info_init ) {
65 rc = ioctl(fd, CHIOGPARAMS, &changer_info);
71 /* Get the number of the first free slot
72 * return > 0 number of empty slot
73 * return = 0 no slot free
76 int GetCurrentSlot(int fd, int drive)
78 struct changer_element_status ces;
86 ces.ces_type = CHET_ST;
87 ces.ces_data = malloc(changer_info.cp_nslots);
89 rc = ioctl(fd, CHIOGSTATUS, &ces);
91 dbprintf(_("changer status query failed: 0x%x %s\n"), rc, strerror(errno));
94 for (slot = 0; slot < changer_info.cp_nslots; slot++)
96 i = ces.ces_data[slot] & CESTATUS_FULL;
97 dbprintf(_("\tGetCurrentSlot slot %d = %d\n"), slot, i);
104 int get_clean_state(char *tapedev)
110 #define GMT_CLN(x) ((x) & 0x00008000)
111 rc = ( GMT_CLN(GetDeviceStatus(tapedev)) );
116 int eject_tape(char *tapedev)
117 /* This function ejects the tape from the drive */
122 if ( (mtfd = OpenDevice(tapedev) ) < 0) {
123 dbprintf(_("eject_tape : failed\n"));
127 mt_com.mt_op = MTOFFL;
129 if (ioctl(mtfd, MTIOCTOP, (char *)&mt_com) < 0) {
131 If the drive already ejected the tape due an error, or because it
132 was a cleaning tape, threre can be an error, which we should ignore
138 return(CloseDevice(tapedev, mtfd));
143 * this routine checks a specified slot to see if it is empty
145 int isempty(int fd, int slot)
147 struct changer_element_status ces;
151 get_changer_info(fd);
154 ces.ces_data = malloc(changer_info.cp_nslots);
156 rc = ioctl(fd, CHIOGSTATUS, &ces);
158 dbprintf(_("changer status query failed: 0x%x %s\n"), rc,strerror(errno));
162 i = ces.ces_data[slot] & CESTATUS_FULL;
169 * find the first empty slot
171 int find_empty(int fd, int start, int count)
173 struct changer_element_status ces;
180 get_changer_info(fd);
183 ces.ces_data = malloc(changer_info.cp_nslots);
185 rc = ioctl(fd,CHIOGSTATUS,&ces);
187 dbprintf(_("changer status query failed: 0x%x %s\n"), rc, strerror(errno));
192 while ((i < changer_info.cp_nslots)&&(ces.ces_data[i] & CESTATUS_FULL))
199 * returns one if there is a tape loaded in the drive
201 int drive_loaded(int fd, int drivenum)
203 struct changer_element_status ces;
207 get_changer_info(fd);
210 ces.ces_data = malloc(changer_info.cp_ndrives);
212 rc = ioctl(fd, CHIOGSTATUS, &ces);
214 dbprintf(_("drive status query failed: 0x%x %s\n"), rc, strerror(errno));
218 i = (ces.ces_data[drivenum] & CESTATUS_FULL);
226 * unloads the drive, putting the tape in the specified slot
228 int unload(int fd, int drive, int slot)
230 struct changer_move move;
233 dbprintf(_("unload : fd = %d, drive = %d, slot =%d\n"),fd, drive, slot);
235 move.cm_fromtype = CHET_DT;
236 move.cm_fromunit = drive;
237 move.cm_totype = CHET_ST;
238 move.cm_tounit = slot;
241 rc = ioctl(fd, CHIOMOVE, &move);
243 dbprintf(_("drive unload failed (MOVE): 0x%x %s\n"), rc, strerror(errno));
251 * moves tape from the specified slot into the drive
253 int load(int fd, int drive, int slot)
255 struct changer_move move;
258 dbprintf(_("load : fd = %d, drive = %d, slot =%d\n"),fd, drive, slot);
260 move.cm_fromtype = CHET_ST;
261 move.cm_fromunit = slot;
262 move.cm_totype = CHET_DT;
263 move.cm_tounit = drive;
266 rc = ioctl(fd,CHIOMOVE,&move);
268 dbprintf(_("drive load failed (MOVE): 0x%x %s\n"), rc, strerror(errno));
274 int get_slot_count(int fd)
278 rc = get_changer_info(fd);
280 dbprintf(_("slot count query failed: 0x%x %s\n"), rc, strerror(errno));
284 return changer_info.cp_nslots;
287 int get_drive_count(int fd)
291 rc = get_changer_info(fd);
293 dbprintf(_("drive count query failed: 0x%x %s\n"), rc, strerror(errno));
297 return changer_info.cp_ndrives;
300 /* This function should ask the drive if it is ready */
301 int Tape_Ready1 ( char *tapedev , int wait)
307 while ((cnt < wait) && (NULL==(out=fopen(tapedev,"w+")))){
320 dbprintf(_("Tape_Ready1 : wait for BOT : max %d seconds\n"),wait);
321 /* loop on status BOT */
322 while ((cnt < wait)) {
323 if ( GMT_BOT(GetDeviceStatus(tapedev)) ) {
326 /* dbprintf(("Tape_Ready1 : cnt %d\n",cnt)); */
331 dbprintf(_("Tape_Ready1 : BOT not found : %d seconds\n"),cnt);
334 dbprintf(_("Tape_Ready1 : BOT : %d seconds\n"),cnt);
340 int GetDeviceStatus (char *tapedev)
345 mtfd = OpenDevice(tapedev);
346 ioctl (mtfd, MTIOCGET, (char *)&status);
347 CloseDevice(tapedev, mtfd);
348 rc = status.mt_gstat;
352 int OpenDevice (char *tapedev)
356 DeviceFD = open(tapedev, O_RDWR);
360 int CloseDevice (char *device, int DeviceFD)
364 dbprintf(_("CloseDevice(%s)\n"), device);
365 rc = close(DeviceFD);
372 * indent-tabs-mode: nil
373 * c-default-style: gnu