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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
/*
* E-UAE - The portable Amiga Emulator
*
* SCSI layer back-end for Linux hosts
*
* Copyright 2006 Jochen Becher
* Copyright 2006 Richard Drummond
*
*/
#ifdef SCSIEMU_LINUX_IOCTL
#include "sysconfig.h"
#include "sysdeps.h"
#include "options.h"
#include "../include/memory.h"
#include "blkdev.h"
#include "scsidev.h"
#include "gui.h"
#include <linux/cdrom.h>
#include <sys/ioctl.h>
#define DEBUG_ME
#ifdef DEBUG_ME
extern int log_scsi;
# define DEBUG_LOG if (log_scsi) write_log
#else
# define DEBUG_LOG(...) do { ; } while (0);
#endif
struct scsidevdata {
char *name;
int fd;
int isatapi;
uae_u8 buf[DEVICE_SCSI_BUFSIZE];
};
#define MAX_SCSI_DRIVES 16
static struct scsidevdata drives[MAX_SCSI_DRIVES];
static int total_drives = 0;
static const uae_u8 *execscsicmd_in_ioctl (int unitnum, const uae_u8 *cmd_data, int cmd_len, int *outlen);
/*
* this little piece of magic from Toni Wilen is needed to detect
* ATAPI devices.
*/
static int is_atapi_drive (int unitnum)
{
static const uae_u8 cmd[6] = {0x12, 0, 0, 0, 36, 0}; /* INQUIRY */
uae_u8 out[36];
int outlen = sizeof (out);
const uae_u8 *p = execscsicmd_in_ioctl (unitnum, cmd, sizeof (cmd), &outlen);
if (!p) {
DEBUG_LOG ("SCSIDEV: Inquiry command failed; unit %d is not ATAPI drive\n", unitnum);
return 0;
}
if (outlen >= 2 && (p[0] & 31) == 5 && (p[2] & 7) == 0) {
DEBUG_LOG ("SCSIDEV: unit %d is ATAPI drive\n", unitnum);
return 1;
}
DEBUG_LOG ("SCSIDEV: unit %d is not ATAPI drive\n", unitnum);
return 0;
}
static int open_scsi_bus_ioctl (int flags)
{
DEBUG_LOG ("SCSIDEV: open_scsi_bus_ioctl\n");
/* TODO: scan everything / bus for drives; not just one drive */
total_drives = 1;
drives[0].name = currprefs.scsi_device;
drives[0].fd = -1;
drives[0].isatapi = 0;
return 1;
}
static void close_scsi_bus_ioctl (void)
{
DEBUG_LOG ("SCSIDEV: close_scsi_bus_ioctl\n");
}
static int open_scsi_device_ioctl (int unitnum)
{
int result = 0;
DEBUG_LOG ("SCSIDEV: unit = %d: open_scsi_device_ioctl\n", unitnum);
if (unitnum < total_drives) {
struct scsidevdata *sdd = &drives[unitnum];
DEBUG_LOG ("SCSIDEV: unit = %d, name = %s, fd = %d\n", unitnum, sdd->name, sdd->fd);
if (sdd->fd == -1) {
if ((sdd->fd = open (sdd->name, O_RDONLY|O_NONBLOCK)) != -1) {
DEBUG_LOG ("SCSIDEV: Successfully opened drive %s\n", sdd->name);
sdd->isatapi = is_atapi_drive (unitnum);
result = 1;
} else {
write_log ("SCSIDEV: Failed to open drive %s\n", sdd->name);
}
} else {
/* already open */
DEBUG_LOG ("SCSIDEV: unit %d is already opened.\n", unitnum);
result = 1;
}
} else {
DEBUG_LOG ("SCSIDEV: bad unit number %d\n", unitnum);
}
return result;
}
static void close_scsi_device_ioctl (int unitnum)
{
struct scsidevdata *sdd;
DEBUG_LOG ("SCSIDEV: unit = %d: close_scsi_device_ioctl\n", unitnum);
if (unitnum >= total_drives) {
DEBUG_LOG ("SCSIDEV: illegal unit %d >= total_drives %d.\n", unitnum, total_drives);
return;
}
sdd = &drives[unitnum];
if (sdd->fd != -1) {
close (sdd->fd);
sdd->fd = -1;
}
}
static int media_check (struct scsidevdata *sdd)
{
if (ioctl (sdd->fd, CDROM_DRIVE_STATUS, CDSL_CURRENT) == CDS_DISC_OK)
return 1;
else
return 0;
}
static const uae_u8 *execscsicmd_out_ioctl (int unitnum, const uae_u8 *cmd_data, int cmd_len)
{
struct scsidevdata *sdd;
struct cdrom_generic_command cmd;
int io_error;
DEBUG_LOG ("SCSIDEV: unit = %d, execscsicmd_out_ioctl\n", unitnum);
if (unitnum >= total_drives) {
DEBUG_LOG ("SCSIDEV: illegal unit %d >= total_drives %d.\n", unitnum, total_drives);
return 0;
}
sdd = &drives[unitnum];
if (cmd_len > CDROM_PACKET_SIZE) {
DEBUG_LOG ("SCSIDEV: cmd_len too large (%d)\n", cmd_len);
return 0;
}
memcpy (cmd.cmd, cmd_data, cmd_len);
cmd.buffer = 0;
cmd.buflen = 0;
cmd.stat = 0;
cmd.sense = 0;
cmd.data_direction = CGC_DATA_WRITE;
cmd.quiet = 0;
cmd.timeout = 80*60;
gui_flicker_led (LED_CD, 0, 1);
io_error = ioctl (sdd->fd, CDROM_SEND_PACKET, &cmd);
DEBUG_LOG ("SCSIDEV: error: %d, stat: %d\n", io_error, cmd.stat);
if (io_error != 0) {
DEBUG_LOG ("SCSIDEV: errno: %d, %s\n", errno, strerror (errno));
return 0;
}
return cmd_data;
}
static const uae_u8 *execscsicmd_in_ioctl (int unitnum, const uae_u8 *cmd_data, int cmd_len, int *outlen)
{
struct scsidevdata *sdd;
struct cdrom_generic_command cmd;
int io_error;
DEBUG_LOG ("SCSIDEV: unit = %d, execscsicmd_in_ioctl\n", unitnum);
if (unitnum >= total_drives) {
DEBUG_LOG ("SCSIDEV: illegal unit %d >= total_drives %d.\n", unitnum, total_drives);
return 0;
}
sdd = &drives[unitnum];
if (cmd_len > CDROM_PACKET_SIZE) {
DEBUG_LOG ("SCSIDEV: cmd_len too large (%d)\n", cmd_len);
return 0;
}
memcpy (cmd.cmd, cmd_data, cmd_len);
cmd.buffer = sdd->buf;
cmd.buflen = sizeof (sdd->buf);
cmd.stat = 0;
cmd.sense = 0;
cmd.data_direction = CGC_DATA_READ;
cmd.quiet = 0;
cmd.timeout = 80*60;
gui_flicker_led (LED_CD, 0, 1);
io_error = ioctl (sdd->fd, CDROM_SEND_PACKET, &cmd);
DEBUG_LOG ("SCSIDEV: error: %d, stat: %d\n", io_error, cmd.stat);
if (io_error != 0) {
DEBUG_LOG ("SCSIDEV: errno: %d, %s\n", errno, strerror (errno));
return 0;
}
if (outlen) {
*outlen = cmd.buflen;
}
return sdd->buf;
}
static int execscsicmd_direct_ioctl (int unitnum, uaecptr acmd)
{
struct scsidevdata *sdd;
struct cdrom_generic_command cmd;
struct request_sense sense;
uaecptr scsi_data = get_long (acmd + 0);
uae_u32 scsi_len = get_long (acmd + 4);
uaecptr scsi_cmd = get_long (acmd + 12);
int scsi_cmd_len = get_word (acmd + 16);
uae_u8 scsi_flags = get_byte (acmd + 20);
uae_u8 scsi_status = get_byte (acmd + 21);
uaecptr scsi_sense = get_long (acmd + 22);
uae_u16 scsi_sense_len = get_word (acmd + 26);
int io_error;
unsigned int senselen;
int parm, i;
addrbank *bank_data = &get_mem_bank (scsi_data);
addrbank *bank_cmd = &get_mem_bank (scsi_cmd);
addrbank *bank_sense = &get_mem_bank (scsi_sense);
uae_u8 *scsi_datap;
uae_u8 *scsi_datap_org;
DEBUG_LOG ("SCSIDEV: unit = %d: execscsicmd_direct_ioctl\n", unitnum);
DEBUG_LOG ("SCSIDEV: scsi_len = %d, scsi_cmd_len = %d, scsi_sense_len = %d, scsi_flags = %x\n",
scsi_len, scsi_cmd_len, scsi_sense_len, scsi_flags);
if (unitnum >= total_drives) {
DEBUG_LOG ("SCSIDEV: illegal unit %d >= total_drives %d.\n", unitnum, total_drives);
return -1; /* TODO: better error code */
}
sdd = &drives[unitnum];
/* do transfer directly to and from Amiga memory */
if (!bank_data || !bank_data->check (scsi_data, scsi_len)) {
DEBUG_LOG ("SCSIDEV: illegal Amiga memory buffer\n");
return -5; /* IOERR_BADADDRESS */
}
if (scsi_cmd_len > CDROM_PACKET_SIZE) {
DEBUG_LOG ("SCSIDEV: scsi_cmd_len too large (%d)\n", scsi_cmd_len);
return -5; /* TODO: better code */
}
scsi_datap = scsi_datap_org = (scsi_len ? bank_data->xlateaddr (scsi_data) : 0);
memcpy (cmd.cmd, bank_cmd->xlateaddr (scsi_cmd), scsi_cmd_len);
cmd.buffer = scsi_datap;
cmd.buflen = scsi_len;
cmd.stat = scsi_status;
if (sdd->isatapi) {
scsi_atapi_fixup_pre (cmd.cmd, &scsi_cmd_len, &scsi_datap,
&scsi_len, &parm);
}
senselen = (scsi_flags & 4) ? 4 : /* SCSIF_OLDAUTOSENSE */
(scsi_flags & 2) ? scsi_sense_len : /* SCSIF_AUTOSENSE */
0;
cmd.sense = senselen > 0 ? &sense : 0;
cmd.data_direction = (scsi_flags & 1) ? CGC_DATA_READ : CGC_DATA_WRITE;
cmd.quiet = 0;
cmd.timeout = 80*60;
gui_flicker_led (LED_CD, 0, 1);
io_error = ioctl (sdd->fd, CDROM_SEND_PACKET, &cmd);
DEBUG_LOG ("SCSIDEV: error: %d, stat: %d\n", io_error, cmd.stat);
if (cmd.stat != 0) {
unsigned int n;
io_error = 45; /* HFERR_BadStatus */
put_byte (acmd + 8, 0);
put_byte (acmd + 18, 0 /*scsi_cmd_len */);
put_byte (acmd + 21, cmd.stat);
DEBUG_LOG ("SCSIDEV: bad status\n");
n = cmd.sense ? cmd.sense->add_sense_len + 7 : 0;
if (senselen > n) {
if (scsi_sense)
memset (bank_sense->xlateaddr (scsi_sense), 0, senselen);
senselen = n;
}
DEBUG_LOG ("SCSIDEV: senselen = %d\n", senselen);
if (scsi_sense && cmd.sense && senselen > 0) {
memcpy (bank_sense->xlateaddr (scsi_sense), cmd.sense, senselen);
}
put_byte (acmd + 28, senselen);
} else {
put_byte (acmd + 28, 0);
if (scsi_sense && senselen > 0) {
memset (bank_sense->xlateaddr (scsi_sense), 0, senselen);
}
if (io_error == 0) {
if (sdd->isatapi) {
scsi_atapi_fixup_post (cmd.cmd, scsi_cmd_len,
scsi_datap_org, scsi_datap,
&scsi_len, parm);
}
put_long (acmd + 8, scsi_len);
put_word (acmd + 18, scsi_cmd_len);
put_byte (acmd + 21, cmd.stat);
io_error = 0;
} else {
DEBUG_LOG ("SCSIDEV: errno: %d, %s\n", errno, strerror (errno));
put_long (acmd + 8, 0);
put_word (acmd + 18, 0);
put_byte (acmd + 21, cmd.stat);
io_error = 20; /* TODO: Map errors */
}
}
if (scsi_datap != scsi_datap_org)
free (scsi_datap);
return io_error;
}
static struct device_info *info_device_ioctl (int unitnum, struct device_info *di)
{
DEBUG_LOG ("SCSIDEV: unit = %d: info_device_ioctl\n", unitnum);
if (unitnum < total_drives) {
struct scsidevdata *sdd = &drives[unitnum];
media_check (sdd);
di->bus = 0;
di->target = unitnum;
di->lun = 0;
di->media_inserted = media_check (sdd);
di->write_protected = 1;
di->bytespersector = 2048;
di->cylinders = 1;
di->type = INQ_ROMD; /* We only support CD/DVD drives for now */
di->id = unitnum + 1;
/* TODO: Create a more informative device label */
sprintf (di->label, "[%s]", sdd->name);
} else {
di = 0;
}
return di;
}
static int pause_ioctl (int unitnum, int paused)
{
DEBUG_LOG ("SCSIDEV: unit = %d, pause_ioctl not implemented\n", unitnum);
return 0;
}
static int stop_ioctl (int unitnum)
{
DEBUG_LOG ("SCSIDEV: unit = %d, stop_ioctl not implemented\n", unitnum);
return 0;
}
static int play_ioctl (int unitnum, uae_u32 startmsf, uae_u32 endmsf, int scan)
{
DEBUG_LOG ("SCSIDEV: unit = %d, play_ioctl not implemented\n", unitnum);
return 0;
}
static const uae_u8 *qcode_ioctl (int unitnum)
{
DEBUG_LOG ("SCSIDEV: unit = %d, qcode_ioctl not implemented\n", unitnum);
return 0;
}
static const uae_u8 *toc_ioctl (int unitnum)
{
DEBUG_LOG ("SCSIDEV: unit = %d, toc_ioctl not implemented\n", unitnum);
return 0;
}
static const uae_u8 *read_ioctl (int unitnum, int offset)
{
DEBUG_LOG ("SCSIDEV: unit = %d, read_ioctl not implemented\n", unitnum);
return 0;
}
static int write_ioctl (int unitnum, int length, uae_u8 *buf)
{
DEBUG_LOG ("SCSIDEV: unit = %d, write_ioctl not implemented\n", unitnum);
return 0;
}
static int check_isatapi_ioctl (int unitnum)
{
DEBUG_LOG ("SCSIDEV: unit = %d, check_isatapi_ioctl\n", unitnum);
if (unitnum >= total_drives) {
DEBUG_LOG ("SCSIDEV: illegal unit %d >= total_drives %d.\n", unitnum, total_drives);
return 0;
}
return drives[unitnum].isatapi;
}
struct device_functions devicefunc_scsi_linux_ioctl = {
open_scsi_bus_ioctl, /* open_bus_func openbus; */
close_scsi_bus_ioctl, /* close_bus_func closebus; */
open_scsi_device_ioctl, /* open_device_func opendev; */
close_scsi_device_ioctl, /* close_device_func closedev; */
info_device_ioctl, /* info_device_func info; */
execscsicmd_out_ioctl, /* execscsicmd_out_func exec_out; */
execscsicmd_in_ioctl, /* execscsicmd_in_func exec_in; */
execscsicmd_direct_ioctl, /* execscsicmd_direct_func exec_direct; */
pause_ioctl, /* pause_func pause; */
stop_ioctl, /* stop_func stop; */
play_ioctl, /* play_func play; */
qcode_ioctl, /* qcode_func qcode; */
toc_ioctl, /* toc_func toc; */
read_ioctl, /* read_func read; */
write_ioctl, /* write_func write; */
check_isatapi_ioctl, /* isatapi_func isatapi; */
0, /* open_device_func opendevthread; */
0 /* close_device_func closedevthread; */
};
#endif