1 /* scsi-start / scsi-stop
2 * Copyright (C) 1999 Trent Piepho <xyzzy@speakeasy.org>
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.
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.
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.
24 #include <sys/ioctl.h>
26 #include <linux/major.h>
27 #include <linux/kdev_t.h>
28 #include <scsi/scsi.h>
29 #include <scsi/scsi_ioctl.h>
32 #ifdef SCSI_DISK0_MAJOR
33 #define IS_SCSI_DISK(rdev) SCSI_DISK_MAJOR(MAJOR(rdev))
35 #define IS_SCSI_DISK(rdev) (MAJOR(rdev)==SCSI_DISK_MAJOR)
38 int main(int argc
, char *argv
[])
43 mode
= argv
[0][strlen(argv
[0])-1];
44 if(mode
=='p' || mode
=='P') {
46 } else if(mode
=='t' || mode
=='T') {
49 fprintf(stderr
, "Try ending the executable name with 'stop' or 'start'\n");
54 fprintf(stderr
, "Usage: %s device\n",argv
[0]);
55 fprintf(stderr
, "%s the device's motor\n", mode
?"Starts":"Stops");
58 if ((stat(argv
[1], &statbuf
)) < 0) {
62 if (!S_ISBLK(statbuf
.st_mode
)
63 || !IS_SCSI_DISK(statbuf
.st_rdev
) ) {
64 fprintf(stderr
, "%s is not a SCSI block device\n", argv
[1]);
67 if ((fd
= open(argv
[1], O_RDWR
)) < 0) {
73 char sg_command
[] = { START_STOP
, 0, 0, 0, 0 /* mode */, 0 };
76 sg_command
[4] = (char)mode
;
77 memset(&io_hdr
, 0, sizeof(io_hdr
));
78 io_hdr
.interface_id
= 'S';
79 io_hdr
.dxfer_direction
= SG_DXFER_NONE
;
80 io_hdr
.cmd_len
= sizeof(sg_command
);
81 io_hdr
.cmdp
= sg_command
;
82 io_hdr
.timeout
= 60 * 1000; /* 1 min */
84 if (ioctl(fd
, SG_IO
, &io_hdr
) < 0) {
86 if (ioctl(fd
, mode
?SCSI_IOCTL_START_UNIT
:SCSI_IOCTL_STOP_UNIT
) < 0) {