Bump to 4.0.43
[platform/upstream/mtools.git] / devices.h
1 #ifdef OS_linux
2
3 /*  Copyright 1996-2002,2009 Alain Knaff.
4  *  This file is part of mtools.
5  *
6  *  Mtools is free software: you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation, either version 3 of the License, or
9  *  (at your option) any later version.
10  *
11  *  Mtools is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with Mtools.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 #include <sys/types.h>
20
21 #ifdef HAVE_SYS_SYSMACROS_H
22
23 #include <sys/sysmacros.h>
24 #ifndef MAJOR
25 #define MAJOR(dev) major(dev)
26 #endif  /* MAJOR not defined */
27 #ifndef MINOR
28 #define MINOR(dev) minor(dev)
29 #endif  /* MINOR not defined */
30
31 #else
32
33 #include <linux/fs.h>        /* get MAJOR/MINOR from Linux kernel */
34 #ifndef major
35 #define major(x) MAJOR(x)
36 #endif
37
38 #endif /* HAVE_SYS_SYSMACROS_H */
39
40 #define MT_READ 1
41 #define MT_WRITE 2
42
43 #include <linux/fd.h>
44 #include <linux/fdreg.h>
45 #include <linux/major.h>
46
47
48 typedef struct floppy_raw_cmd RawRequest_t;
49
50 static inline void RR_INIT(struct floppy_raw_cmd *request)
51 {
52         request->data = 0;
53         request->length = 0;
54         request->cmd_count = 9;
55         request->flags = FD_RAW_INTR | FD_RAW_NEED_SEEK | FD_RAW_NEED_DISK
56 #ifdef FD_RAW_SOFTFAILUE
57                 | FD_RAW_SOFTFAILURE | FD_RAW_STOP_IF_FAILURE
58 #endif
59                 ;
60         request->cmd[1] = 0;
61         request->cmd[6] = 0;
62         request->cmd[7] = 0x1b;
63         request->cmd[8] = 0xff;
64         request->reply_count = 0;
65 }
66
67 static inline void RR_SETRATE(struct floppy_raw_cmd *request, uint8_t rate)
68 {
69         request->rate = rate;
70 }
71
72 static inline void RR_SETDRIVE(struct floppy_raw_cmd *request,int drive)
73 {
74         request->cmd[1] = (request->cmd[1] & ~3) | (drive & 3);
75 }
76
77 static inline void RR_SETTRACK(struct floppy_raw_cmd *request,uint8_t track)
78 {
79         request->cmd[2] = track;
80 }
81
82 static inline void RR_SETPTRACK(struct floppy_raw_cmd *request,
83                                     int track)
84 {
85         request->track = track;
86 }
87
88 static inline void RR_SETHEAD(struct floppy_raw_cmd *request, uint8_t head)
89 {
90         if(head)
91                 request->cmd[1] |= 4;
92         else
93                 request->cmd[1] &= ~4;
94         request->cmd[3] = head;
95 }
96
97 static inline void RR_SETSECTOR(struct floppy_raw_cmd *request,
98                                     uint8_t sector)
99 {
100         request->cmd[4] = sector;
101         request->cmd[6] = sector-1;
102 }
103
104 static inline void RR_SETSIZECODE(struct floppy_raw_cmd *request,
105                                              uint8_t sizecode)
106 {
107         request->cmd[5] = sizecode;
108         request->cmd[6]++;
109         request->length += 128 << sizecode;
110 }
111
112 #if 0
113 static inline void RR_SETEND(struct floppy_raw_cmd *request, int end)
114 {
115         request->cmd[6] = end;
116 }
117 #endif
118
119 static inline void RR_SETDIRECTION(struct floppy_raw_cmd *request,
120                                        int direction)
121 {
122         if(direction == MT_READ) {
123                 request->flags |= FD_RAW_READ;
124                 request->cmd[0] = FD_READ & ~0x80;
125         } else {
126                 request->flags |= FD_RAW_WRITE;
127                 request->cmd[0] = FD_WRITE & ~0x80;
128         }
129 }
130
131
132 static inline void RR_SETDATA(struct floppy_raw_cmd *request,
133                                   caddr_t data)
134 {
135         request->data = data;
136 }
137
138
139 #if 0
140 static inline void RR_SETLENGTH(struct floppy_raw_cmd *request, int length)
141 {
142         request->length += length;
143 }
144 #endif
145
146 static inline void RR_SETCONT(struct floppy_raw_cmd *request)
147 {
148 #ifdef FD_RAW_MORE
149         request->flags |= FD_RAW_MORE;
150 #endif
151 }
152
153
154 static inline int RR_SIZECODE(struct floppy_raw_cmd *request)
155 {
156         return request->cmd[5];
157 }
158
159
160
161 static inline int RR_TRACK(struct floppy_raw_cmd *request)
162 {
163         return request->cmd[2];
164 }
165
166
167 static inline int GET_DRIVE(int fd)
168 {
169         struct MT_STAT statbuf;
170
171         if (MT_FSTAT(fd, &statbuf) < 0 ){
172                 perror("stat");
173                 return -1;
174         }
175
176         if (!S_ISBLK(statbuf.st_mode) ||
177             MAJOR(statbuf.st_rdev) != FLOPPY_MAJOR)
178                 return -1;
179
180         return (int) MINOR( statbuf.st_rdev );
181 }
182
183
184
185 /* void print_message(RawRequest_t *raw_cmd,char *message);*/
186 int send_one_cmd(int fd, RawRequest_t *raw_cmd, const char *message);
187 int analyze_one_reply(RawRequest_t *raw_cmd, int *bytes, int do_print);
188
189 #endif
190
191 int init_geom(int fd, struct device *dev, struct device *orig_dev,
192               struct MT_STAT *statbuf);