usbmodeswitch: Updated to v.1.2.6 from shibby's branch.
[tomato.git] / release / src / router / scsi-idle / scsi-start.c
blobce6e39bfa605e07354b2fae0ffd750b2c2aca7f1
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.
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.h>
29 #include <scsi/scsi_ioctl.h>
30 #include <scsi/sg.h>
32 #ifdef SCSI_DISK0_MAJOR
33 #define IS_SCSI_DISK(rdev) SCSI_DISK_MAJOR(MAJOR(rdev))
34 #else
35 #define IS_SCSI_DISK(rdev) (MAJOR(rdev)==SCSI_DISK_MAJOR)
36 #endif
38 int main(int argc, char *argv[])
40 int fd, mode;
41 struct stat statbuf;
43 mode = argv[0][strlen(argv[0])-1];
44 if(mode=='p' || mode=='P') {
45 mode = 0; /* stoP */
46 } else if(mode=='t' || mode=='T') {
47 mode = 1; /* starT */
48 } else {
49 fprintf(stderr, "Try ending the executable name with 'stop' or 'start'\n");
50 exit(1);
53 if (argc != 2) {
54 fprintf(stderr, "Usage: %s device\n",argv[0]);
55 fprintf(stderr, "%s the device's motor\n", mode?"Starts":"Stops");
56 exit(1);
58 if ((stat(argv[1], &statbuf)) < 0) {
59 perror(argv[1]);
60 exit(1);
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]);
65 exit(1);
67 if ((fd = open(argv[1], O_RDWR)) < 0) {
68 perror(argv[1]);
69 exit(1);
72 #ifdef SCSI_SG
73 char sg_command[] = { START_STOP, 0, 0, 0, 0 /* mode */, 0 };
74 sg_io_hdr_t io_hdr;
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) {
85 #else
86 if (ioctl(fd, mode?SCSI_IOCTL_START_UNIT:SCSI_IOCTL_STOP_UNIT) < 0) {
87 #endif
88 perror(argv[1]);
89 close(fd);
90 exit(1);
93 close(fd);
94 exit(0);