-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblkdev.c
executable file
·354 lines (321 loc) · 9.04 KB
/
blkdev.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/*
* UAE - The Un*x Amiga Emulator
*
* lowlevel device glue
*
*/
#include "sysconfig.h"
#include "sysdeps.h"
#include "options.h"
#include "memory.h"
#include "blkdev.h"
static struct device_functions *device_func[2];
static int have_ioctl;
# ifdef TARGET_AMIGAOS
extern struct device_functions devicefunc_scsi_amiga;
static void install_driver (int flags)
{
device_func[DF_SCSI] = &devicefunc_scsi_amiga;
device_func[DF_IOCTL] = 0;
}
# else
extern struct device_functions devicefunc_scsi_libscg;
static void install_driver (int flags)
{
device_func[DF_SCSI] = &devicefunc_scsi_libscg;
device_func[DF_IOCTL] = 0;
}
# endif
#endif
int sys_command_open (int mode, int unitnum)
{
if (mode == DF_SCSI || !have_ioctl)
return device_func[DF_SCSI]->opendev (unitnum);
else
return device_func[DF_IOCTL]->opendev (unitnum);
}
void sys_command_close (int mode, int unitnum)
{
if (mode == DF_SCSI || !have_ioctl)
device_func[DF_SCSI]->closedev (unitnum);
else
device_func[DF_IOCTL]->closedev (unitnum);
}
int sys_command_open_thread (int mode, int unitnum)
{
int result = 0;
if (mode == DF_SCSI || !have_ioctl) {
if (device_func[DF_SCSI]->opendevthread)
result = device_func[DF_SCSI]->opendevthread (unitnum);
} else {
if (device_func[DF_IOCTL]->opendevthread)
result= device_func[DF_IOCTL]->opendevthread (unitnum);
}
return result;
}
void sys_command_close_thread (int mode, int unitnum)
{
if (mode == DF_SCSI || !have_ioctl) {
if (device_func[DF_SCSI]->closedevthread)
device_func[DF_SCSI]->closedevthread (unitnum);
} else {
if (device_func[DF_IOCTL]->closedevthread)
device_func[DF_IOCTL]->closedevthread (unitnum);
}
}
int device_func_init (int flags)
{
static int initialized;
int support_scsi = 0, support_ioctl = 0;
int oflags = (flags & DEVICE_TYPE_SCSI) ? 0 : (1 << INQ_ROMD);
if (initialized)
return initialized;
install_driver (flags);
if (device_func[DF_IOCTL])
have_ioctl = 1;
else
have_ioctl = 0;
support_scsi = device_func[DF_SCSI]->openbus (oflags) ? 1 : 0;
if (have_ioctl)
support_ioctl = device_func[DF_IOCTL]->openbus (1 << INQ_ROMD) ? 1 : 0;
initialized = 1;
write_log ("support_scsi = %d support_ioctl = %d\n", support_scsi, support_ioctl);
return (support_scsi ? (1 << DF_SCSI) : 0) | (support_ioctl ? (1 << DF_IOCTL) : 0);
}
static int audiostatus (int unitnum)
{
static const uae_u8 cmd[10] = {
0x42, 2, 0x40, 1, 0, 0, 0, DEVICE_SCSI_BUFSIZE >> 8, DEVICE_SCSI_BUFSIZE & 0xff, 0
};
const uae_u8 *p = device_func[DF_SCSI]->exec_in (unitnum, cmd, sizeof (cmd), 0);
if (!p)
return 0;
return p[1];
}
/* pause/unpause CD audio */
void sys_command_pause (int mode, int unitnum, int paused)
{
if (mode == DF_SCSI || !have_ioctl) {
int as = audiostatus (unitnum);
if ((paused && as == 0x11) && (!paused && as == 0x12)) {
uae_u8 cmd[10] = {
0x4b, 0, 0, 0, 0, 0, 0, 0, paused ? 0 :1, 0
};
device_func[DF_SCSI]->exec_out (unitnum, cmd, sizeof (cmd));
}
return;
}
device_func[DF_IOCTL]->pause (unitnum, paused);
}
/* stop CD audio */
void sys_command_stop (int mode, int unitnum)
{
if (mode == DF_SCSI || !have_ioctl) {
int as = audiostatus (unitnum);
if (as == 0x11) {
static const uae_u8 cmd[6] = {
0x4e, 0, 0, 0, 0, 0
};
device_func[DF_SCSI]->exec_out (unitnum, cmd, sizeof (cmd));
}
return;
}
device_func[DF_IOCTL]->stop (unitnum);
}
/* play CD audio */
int sys_command_play (int mode, int unitnum, uae_u32 startmsf, uae_u32 endmsf, int scan)
{
if (mode == DF_SCSI || !have_ioctl) {
uae_u8 cmd[12] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};
#if 0
if (scan) {
cmd[0] = 0xba;
cmd[1] = scan < 0 ? 0x10 : 0x0;
cmd[3] = (uae_u8)(startmsf >> 16);
cmd[4] = (uae_u8)(startmsf >> 8);
cmd[5] = (uae_u8)(startmsf >> 0);
cmd[9] = 0x40;
} else {
#endif
cmd[0] = 0x47;
cmd[3] = (uae_u8)(startmsf >> 16);
cmd[4] = (uae_u8)(startmsf >> 8);
cmd[5] = (uae_u8)(startmsf >> 0);
cmd[6] = (uae_u8)(endmsf >> 16);
cmd[7] = (uae_u8)(endmsf >> 8);
cmd[8] = (uae_u8)(endmsf >> 0);
#if 0
}
#endif
return device_func[DF_SCSI]->exec_out (unitnum, cmd, sizeof (cmd)) == 0 ? 0 : 1;
}
return device_func[DF_IOCTL]->play (unitnum, startmsf, endmsf, scan);
}
/* read qcode */
const uae_u8 *sys_command_qcode (int mode, int unitnum)
{
if (mode == DF_SCSI || !have_ioctl) {
static const uae_u8 cmd[10] = {
0x42, 2, 0x40, 1, 0, 0, 0, DEVICE_SCSI_BUFSIZE >> 8, DEVICE_SCSI_BUFSIZE & 0xff, 0
};
return device_func[DF_SCSI]->exec_in (unitnum, cmd, sizeof (cmd), 0);
}
return device_func[DF_IOCTL]->qcode (unitnum);
};
/* read table of contents */
const uae_u8 *sys_command_toc (int mode, int unitnum)
{
if (mode == DF_SCSI || !have_ioctl) {
static const uae_u8 cmd[10] = {
0x43, 0, 2, 0, 0, 0, 1, DEVICE_SCSI_BUFSIZE >> 8, DEVICE_SCSI_BUFSIZE & 0xFF, 0
};
return device_func[DF_SCSI]->exec_in (unitnum, cmd, sizeof(cmd), 0);
}
return device_func[DF_IOCTL]->toc (unitnum);
}
/* read one sector */
const uae_u8 *sys_command_read (int mode, int unitnum, int offset)
{
if (mode == DF_SCSI || !have_ioctl) {
uae_u8 cmd[12] = {
0xbe, 0, 0, 0, 0, 0, 0, 0, 1, 0x10, 0, 0
};
cmd[3] = (uae_u8)(offset >> 16);
cmd[4] = (uae_u8)(offset >> 8);
cmd[5] = (uae_u8)(offset >> 0);
return device_func[DF_SCSI]->exec_in (unitnum, cmd, sizeof (cmd), 0);
}
return device_func[DF_IOCTL]->read (unitnum, offset);
}
struct device_info *sys_command_info (int mode, int unitnum, struct device_info *di)
{
if (mode == DF_SCSI || !have_ioctl)
return device_func[DF_SCSI]->info (unitnum, di);
else
return device_func[DF_IOCTL]->info (unitnum, di);
}
#define MODE_SELECT_6 0x15
#define MODE_SENSE_6 0x1a
#define MODE_SELECT_10 0x55
#define MODE_SENSE_10 0x5a
void scsi_atapi_fixup_pre (uae_u8 *scsi_cmd, int *len, uae_u8 **datap, unsigned int *datalenp, int *parm)
{
uae_u8 cmd, *p, *data = *datap;
unsigned int l, datalen = *datalenp;
*parm = 0;
cmd = scsi_cmd[0];
if (cmd != MODE_SELECT_6 && cmd != MODE_SENSE_6)
return;
l = scsi_cmd[4];
if (l > 4)
l += 4;
scsi_cmd[7] = l >> 8;
scsi_cmd[8] = l;
if (cmd == MODE_SELECT_6) {
scsi_cmd[0] = MODE_SELECT_10;
scsi_cmd[9] = scsi_cmd[5];
scsi_cmd[2] = scsi_cmd[3] = scsi_cmd[4] = scsi_cmd[5] = scsi_cmd[6] = 0;
*len = 10;
p = xmalloc (8 + datalen + 4);
if (datalen > 4)
memcpy (p + 8, data + 4, datalen - 4);
p[0] = 0;
p[1] = data[0];
p[2] = data[1];
p[3] = data[2];
p[4] = p[5] = p[6] = 0;
p[7] = data[3];
if (l > 8)
datalen += 4;
*parm = MODE_SELECT_10;
*datap = p;
} else {
scsi_cmd[0] = MODE_SENSE_10;
scsi_cmd[9] = scsi_cmd[5];
scsi_cmd[3] = scsi_cmd[4] = scsi_cmd[5] = scsi_cmd[6] = 0;
if (l > 8)
datalen += 4;
*datap = xmalloc (datalen);
*len = 10;
*parm = MODE_SENSE_10;
}
*datalenp = datalen;
}
void scsi_atapi_fixup_post (uae_u8 *scsi_cmd, int len, uae_u8 *olddata, uae_u8 *data, unsigned int *datalenp, int parm)
{
unsigned int datalen = *datalenp;
if (!data || !datalen)
return;
if (parm == MODE_SENSE_10) {
olddata[0] = data[1];
olddata[1] = data[2];
olddata[2] = data[3];
olddata[3] = data[7];
datalen -= 4;
if (datalen > 4)
memcpy (olddata + 4, data + 8, datalen - 4);
*datalenp = datalen;
}
}
static void scsi_atapi_fixup_inquiry (uaecptr req)
{
uaecptr scsi_data = get_long (req + 0);
uae_u32 scsi_len = get_long (req + 4);
uaecptr scsi_cmd = get_long (req + 12);
uae_u8 cmd;
cmd = get_byte (scsi_cmd);
/* CDROM INQUIRY: most Amiga programs expect ANSI version == 2
* (ATAPI normally responds with zero)
*/
if (cmd == 0x12 && scsi_len > 2 && scsi_data) {
uae_u8 per = get_byte (scsi_data + 0);
uae_u8 b = get_byte (scsi_data + 2);
/* CDROM and ANSI version == 0 ? */
if ((per & 31) == 5 && (b & 7) == 0) {
b |= 2;
put_byte (scsi_data + 2, b);
}
}
}
int sys_command_scsi_direct (int unitnum, uaecptr request)
{
int ret = device_func[DF_SCSI]->exec_direct (unitnum, request);
if (!ret && device_func[DF_SCSI]->isatapi(unitnum))
scsi_atapi_fixup_inquiry (request);
return ret;
}
void scsi_log_before (const uae_u8 *cdb, int cdblen, const uae_u8 *data, int datalen)
{
int i;
for (i = 0; i < cdblen; i++) {
write_log("%s%02.2X", i > 0 ? "." : "", cdb[i]);
}
write_log("\n");
if (data) {
write_log ("DATAOUT: %d\n", datalen);
for (i = 0; i < datalen && i < 100; i++)
write_log("%s%02.2X", i > 0 ? "." : "", data[i]);
if (datalen > 0)
write_log("\n");
}
}
void scsi_log_after (const uae_u8 *data, int datalen, const uae_u8 *sense, int senselen)
{
int i;
if (data) {
write_log ("DATAIN: %d\n", datalen);
for (i = 0; i < datalen && i < 100; i++)
write_log("%s%02.2X", i > 0 ? "." : "", data[i]);
if (datalen > 0)
write_log("\n");
}
if (senselen > 0) {
write_log("SENSE: ");
for (i = 0; i < senselen && i < 32; i++) {
write_log("%s%02.2X", i > 0 ? "." : "", sense[i]);
}
write_log("\n");
}
}