1/* scsi-start / scsi-stop 2 * Copyright (C) 1999 Trent Piepho <xyzzy@speakeasy.org> 3 * 4 * This program is free software; you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation; either version 2 of the License, or 7 * (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with this program; if not, write to the Free Software 16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 17 */ 18 19#include <fcntl.h> 20#include <stdio.h> 21#include <stdlib.h> 22#include <string.h> 23#include <unistd.h> 24#include <sys/ioctl.h> 25#include <sys/stat.h> 26#include <linux/major.h> 27#include <linux/kdev_t.h> 28#include <scsi/scsi_ioctl.h> 29#include <scsi/sg.h> 30 31#ifdef SCSI_DISK0_MAJOR 32#define IS_SCSI_DISK(rdev) SCSI_DISK_MAJOR(MAJOR(rdev)) 33#else 34#define IS_SCSI_DISK(rdev) (MAJOR(rdev)==SCSI_DISK_MAJOR) 35#endif 36 37#ifndef START_STOP 38#define START_STOP 0x1b 39#endif 40 41int main(int argc, char *argv[]) 42{ 43 int fd, mode; 44 struct stat statbuf; 45 46 mode = argv[0][strlen(argv[0])-1]; 47 if(mode=='p' || mode=='P') { 48 mode = 0; /* stoP */ 49 } else if(mode=='t' || mode=='T') { 50 mode = 1; /* starT */ 51 } else { 52 fprintf(stderr, "Try ending the executable name with 'stop' or 'start'\n"); 53 exit(1); 54 } 55 56 if (argc != 2) { 57 fprintf(stderr, "Usage: %s device\n",argv[0]); 58 fprintf(stderr, "%s the device's motor\n", mode?"Starts":"Stops"); 59 exit(1); 60 } 61 if ((stat(argv[1], &statbuf)) < 0) { 62 perror(argv[1]); 63 exit(1); 64 } 65 if (!S_ISBLK(statbuf.st_mode) 66 || !IS_SCSI_DISK(statbuf.st_rdev) ) { 67 fprintf(stderr, "%s is not a SCSI block device\n", argv[1]); 68 exit(1); 69 } 70 if ((fd = open(argv[1], O_RDWR)) < 0) { 71 perror(argv[1]); 72 exit(1); 73 } 74 75#ifdef SCSI_SG 76 char sg_command[] = { START_STOP, 0, 0, 0, 0 /* mode */, 0 }; 77 sg_io_hdr_t io_hdr; 78 79 sg_command[4] = (char)mode; 80 memset(&io_hdr, 0, sizeof(io_hdr)); 81 io_hdr.interface_id = 'S'; 82 io_hdr.dxfer_direction = SG_DXFER_NONE; 83 io_hdr.cmd_len = sizeof(sg_command); 84 io_hdr.cmdp = sg_command; 85 io_hdr.timeout = 60 * 1000; /* 1 min */ 86 87 if (ioctl(fd, SG_IO, &io_hdr) < 0) { 88#else 89 if (ioctl(fd, mode?SCSI_IOCTL_START_UNIT:SCSI_IOCTL_STOP_UNIT) < 0) { 90#endif 91 perror(argv[1]); 92 close(fd); 93 exit(1); 94 } 95 96 close(fd); 97 exit(0); 98} 99