ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/Unix/sys_unix.cpp
Revision: 1.5
Committed: 1999-10-31T23:18:37Z (25 years, 1 month ago) by cebix
Branch: MAIN
CVS Tags: snapshot-22121999, release-0_8-1, snapshot-02111999
Changes since 1.4: +6 -3 lines
Log Message:
- removed MemoryDispatch() replacement; routine from ROM is now used if
  possible
- rom_patches.cpp: check for double PACK 4 resources; if only one is found,
  assume that the ROM requires an FPU and issue a warning if FPU emulation
  is turned off
- UAE CPU opcode routines no longer return the cycle count
- main_unix.cpp: pressing Ctrl-C dumps the UAE CPU state before entering mon
- sys_unix.cpp: under Linux, partition sizes are read with BLKGETSIZE instead
  of llseek()

File Contents

# User Rev Content
1 cebix 1.1 /*
2     * sys_unix.cpp - System dependent routines, Unix implementation
3     *
4     * Basilisk II (C) 1997-1999 Christian Bauer
5     *
6     * This program 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 2 of the License, or
9     * (at your option) any later version.
10     *
11     * This program 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 this program; if not, write to the Free Software
18     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19     */
20    
21     #include "sysdeps.h"
22    
23     #include <sys/ioctl.h>
24     #include <sys/stat.h>
25     #include <errno.h>
26    
27     #ifdef __linux__
28     #include <linux/cdrom.h>
29     #include <linux/fd.h>
30 cebix 1.5 #include <linux/fs.h>
31 cebix 1.1 #include <linux/major.h>
32     #include <linux/kdev_t.h>
33     #include <linux/unistd.h>
34    
35     #ifdef __NR__llseek
36     _syscall5(int, _llseek, uint, fd, ulong, hi, ulong, lo, loff_t *, res, uint, wh);
37     #else
38     static int _llseek(uint fd, ulong hi, ulong lo, loff_t *res, uint wh)
39     {
40     if (hi)
41     return -1;
42     *res = lseek(fd, lo, wh);
43     if (*res == -1)
44     return -1;
45     return 0;
46     }
47     #endif
48     #endif
49    
50 cebix 1.3 #if defined(__FreeBSD__) || defined(__NetBSD__)
51 cebix 1.1 #include <sys/cdio.h>
52     #endif
53    
54     #include "main.h"
55     #include "macos_util.h"
56     #include "prefs.h"
57     #include "user_strings.h"
58     #include "sys.h"
59    
60     #define DEBUG 0
61     #include "debug.h"
62    
63    
64     // File handles are pointers to these structures
65     struct file_handle {
66     char *name; // Copy of device/file name
67     int fd;
68     bool is_file; // Flag: plain file or /dev/something?
69     bool is_floppy; // Flag: floppy device
70     bool is_cdrom; // Flag: CD-ROM device
71     bool read_only; // Copy of Sys_open() flag
72     loff_t start_byte; // Size of file header (if any)
73     loff_t file_size; // Size of file data (only valid if is_file is true)
74    
75     #if defined(__linux__)
76     int cdrom_cap; // CD-ROM capability flags (only valid if is_cdrom is true)
77     #elif defined(__FreeBSD__)
78     struct ioc_capability cdrom_cap;
79     #endif
80     };
81    
82     // File handle of first floppy drive (for SysMountFirstFloppy())
83     static file_handle *first_floppy = NULL;
84    
85    
86     /*
87     * Initialization
88     */
89    
90     void SysInit(void)
91     {
92     }
93    
94    
95     /*
96     * Deinitialization
97     */
98    
99     void SysExit(void)
100     {
101     }
102    
103    
104     /*
105     * Mount first floppy disk
106     */
107    
108     void SysMountFirstFloppy(void)
109     {
110     if (first_floppy)
111     MountVolume(first_floppy);
112     }
113    
114    
115     /*
116     * This gets called when no "floppy" prefs items are found
117     * It scans for available floppy drives and adds appropriate prefs items
118     */
119    
120     void SysAddFloppyPrefs(void)
121     {
122     #if defined(__linux__)
123     PrefsAddString("floppy", "/dev/fd0H1440");
124     PrefsAddString("floppy", "/dev/fd1H1440");
125 cebix 1.3 #elif defined(__NetBSD__)
126     PrefsAddString("floppy", "/dev/fd0a");
127     PrefsAddString("floppy", "/dev/fd1a");
128 cebix 1.1 #else
129     PrefsAddString("floppy", "/dev/fd0");
130     PrefsAddString("floppy", "/dev/fd1");
131     #endif
132     }
133    
134    
135     /*
136     * This gets called when no "disk" prefs items are found
137     * It scans for available HFS volumes and adds appropriate prefs items
138     */
139    
140     void SysAddDiskPrefs(void)
141     {
142     #ifdef __linux__
143     FILE *f = fopen("/etc/fstab", "r");
144     if (f) {
145     char line[256];
146     while(fgets(line, 255, f)) {
147     // Read line
148     int len = strlen(line);
149     if (len == 0)
150     continue;
151     line[len-1] = 0;
152    
153     // Parse line
154     char *dev, *mnt_point, *fstype;
155 cebix 1.3 if (sscanf(line, "%as %as %as", &dev, &mnt_point, &fstype) == 3) {
156 cebix 1.1 if (strcmp(fstype, "hfs") == 0)
157     PrefsAddString("disk", dev);
158     }
159 cebix 1.3 free(dev); free(mnt_point); free(fstype);
160 cebix 1.1 }
161     fclose(f);
162     }
163     #endif
164     }
165    
166    
167     /*
168     * This gets called when no "cdrom" prefs items are found
169     * It scans for available CD-ROM drives and adds appropriate prefs items
170     */
171    
172     void SysAddCDROMPrefs(void)
173     {
174     // Don't scan for drives if nocdrom option given
175     if (PrefsFindBool("nocdrom"))
176     return;
177    
178     #if defined(__linux__)
179     PrefsAddString("cdrom", "/dev/cdrom");
180     #elif defined(__FreeBSD__)
181     PrefsAddString("cdrom", "/dev/cd0c");
182 cebix 1.3 #elif defined(__NetBSD__)
183     PrefsAddString("cdrom", "/dev/cd0d");
184 cebix 1.1 #endif
185     }
186    
187    
188     /*
189     * Add default serial prefs (must be added, even if no ports present)
190     */
191    
192     void SysAddSerialPrefs(void)
193     {
194     #if defined(__linux__)
195     PrefsAddString("seriala", "/dev/ttyS0");
196     PrefsAddString("serialb", "/dev/ttyS1");
197     #elif defined(__FreeBSD__)
198     PrefsAddString("seriala", "/dev/cuaa0");
199     PrefsAddString("serialb", "/dev/cuaa1");
200 cebix 1.3 #elif defined(__NetBSD__)
201     PrefsAddString("seriala", "/dev/tty00");
202     PrefsAddString("serialb", "/dev/tty01");
203 cebix 1.1 #endif
204     }
205    
206    
207     /*
208     * Check if device is a mounted HFS volume, get mount name
209     */
210    
211     static bool is_drive_mounted(const char *dev_name, char *mount_name)
212     {
213     #ifdef __linux__
214     FILE *f = fopen("/proc/mounts", "r");
215     if (f) {
216     char line[256];
217     while(fgets(line, 255, f)) {
218     // Read line
219     int len = strlen(line);
220     if (len == 0)
221     continue;
222     line[len-1] = 0;
223    
224     // Parse line
225     if (strncmp(line, dev_name, strlen(dev_name)) == 0) {
226     mount_name[0] = 0;
227 cebix 1.3 char *dummy;
228     sscanf(line, "%as %s", &dummy, mount_name);
229     free(dummy);
230 cebix 1.1 fclose(f);
231     return true;
232     }
233     }
234     fclose(f);
235     }
236     #endif
237     return false;
238     }
239    
240    
241     /*
242     * Open file/device, create new file handle (returns NULL on error)
243     */
244    
245     void *Sys_open(const char *name, bool read_only)
246     {
247     bool is_file = strncmp(name, "/dev/", 5) != 0;
248 cebix 1.3 #if defined(__FreeBSD__)
249 cebix 1.1 // SCSI IDE
250     bool is_cdrom = strncmp(name, "/dev/cd", 7) == 0 || strncmp(name, "/dev/acd", 8) == 0;
251     #else
252     bool is_cdrom = strncmp(name, "/dev/cd", 7) == 0;
253     #endif
254    
255     D(bug("Sys_open(%s, %s)\n", name, read_only ? "read-only" : "read/write"));
256    
257     // Check if write access is allowed, set read-only flag if not
258     if (!read_only && access(name, W_OK))
259     read_only = true;
260    
261     // Print warning message and eventually unmount drive when this is an HFS volume mounted under Linux (double mounting will corrupt the volume)
262     char mount_name[256];
263     if (!is_file && !read_only && is_drive_mounted(name, mount_name)) {
264     char str[512];
265     sprintf(str, GetString(STR_VOLUME_IS_MOUNTED_WARN), mount_name);
266     WarningAlert(str);
267     sprintf(str, "umount %s", mount_name);
268     if (system(str)) {
269     sprintf(str, GetString(STR_CANNOT_UNMOUNT_WARN), mount_name, strerror(errno));
270     WarningAlert(str);
271     return NULL;
272     }
273     }
274    
275     // Open file/device
276 cebix 1.3 #if defined(__linux__)
277 cebix 1.1 int fd = open(name, (read_only ? O_RDONLY : O_RDWR) | (is_cdrom ? O_NONBLOCK : 0));
278     #else
279     int fd = open(name, read_only ? O_RDONLY : O_RDWR);
280     #endif
281     if (fd < 0 && !read_only) {
282     // Read-write failed, try read-only
283     read_only = true;
284     fd = open(name, O_RDONLY);
285     }
286     if (fd >= 0) {
287     file_handle *fh = new file_handle;
288     fh->name = strdup(name);
289     fh->fd = fd;
290     fh->is_file = is_file;
291     fh->read_only = read_only;
292     fh->start_byte = 0;
293     fh->is_floppy = false;
294     fh->is_cdrom = false;
295     if (fh->is_file) {
296     // Detect disk image file layout
297     loff_t size = 0;
298 cebix 1.3 #if defined(__linux__)
299 cebix 1.1 _llseek(fh->fd, 0, 0, &size, SEEK_END);
300     #else
301     size = lseek(fd, 0, SEEK_END);
302     #endif
303     uint8 data[256];
304     lseek(fd, 0, SEEK_SET);
305     read(fd, data, 256);
306     FileDiskLayout(size, data, fh->start_byte, fh->file_size);
307     } else {
308     struct stat st;
309     if (fstat(fd, &st) == 0) {
310     if (S_ISBLK(st.st_mode)) {
311     fh->is_cdrom = is_cdrom;
312     #if defined(__linux__)
313     fh->is_floppy = (MAJOR(st.st_rdev) == FLOPPY_MAJOR);
314     #ifdef CDROM_GET_CAPABILITY
315     if (is_cdrom) {
316     fh->cdrom_cap = ioctl(fh->fd, CDROM_GET_CAPABILITY);
317     if (fh->cdrom_cap < 0)
318     fh->cdrom_cap = 0;
319     }
320     #else
321     fh->cdrom_cap = 0;
322     #endif
323 cebix 1.4 #elif defined(__FreeBSD__)
324 cebix 1.1 fh->is_floppy = ((st.st_rdev >> 16) == 2);
325     #ifdef CDIOCCAPABILITY
326     if (is_cdrom) {
327     if (ioctl(fh->fd, CDIOCCAPABILITY, &fh->cdrom_cap) < 0)
328     memset(&fh->cdrom_cap, 0, sizeof(fh->cdrom_cap));
329     }
330     #else
331     fh->cdrom_cap = 0;
332     #endif
333 cebix 1.4 #elif defined(__NetBSD__)
334     fh->is_floppy = ((st.st_rdev >> 16) == 2);
335 cebix 1.1 #endif
336     }
337     }
338     }
339     if (fh->is_floppy && first_floppy == NULL)
340     first_floppy = fh;
341     return fh;
342     } else {
343     printf("WARNING: Cannot open %s (%s)\n", name, strerror(errno));
344     return NULL;
345     }
346     }
347    
348    
349     /*
350     * Close file/device, delete file handle
351     */
352    
353     void Sys_close(void *arg)
354     {
355     file_handle *fh = (file_handle *)arg;
356     if (!fh)
357     return;
358    
359     close(fh->fd);
360     if (fh->name)
361     free(fh->name);
362     delete fh;
363     }
364    
365    
366     /*
367     * Read "length" bytes from file/device, starting at "offset", to "buffer",
368     * returns number of bytes read (or 0)
369     */
370    
371     size_t Sys_read(void *arg, void *buffer, loff_t offset, size_t length)
372     {
373     file_handle *fh = (file_handle *)arg;
374     if (!fh)
375     return 0;
376    
377     // Seek to position
378 cebix 1.3 #if defined(__linux__)
379 cebix 1.1 loff_t pos = offset + fh->start_byte, res;
380     if (_llseek(fh->fd, pos >> 32, pos, &res, SEEK_SET) < 0)
381     return 0;
382     #else
383     if (lseek(fh->fd, offset + fh->start_byte, SEEK_SET) < 0)
384     return 0;
385     #endif
386    
387     // Read data
388     return read(fh->fd, buffer, length);
389     }
390    
391    
392     /*
393     * Write "length" bytes from "buffer" to file/device, starting at "offset",
394     * returns number of bytes written (or 0)
395     */
396    
397     size_t Sys_write(void *arg, void *buffer, loff_t offset, size_t length)
398     {
399     file_handle *fh = (file_handle *)arg;
400     if (!fh)
401     return 0;
402    
403     // Seek to position
404 cebix 1.3 #if defined(__linux__)
405 cebix 1.1 loff_t pos = offset + fh->start_byte, res;
406     if (_llseek(fh->fd, pos >> 32, pos, &res, SEEK_SET) < 0)
407     return 0;
408     #else
409     if (lseek(fh->fd, offset + fh->start_byte, SEEK_SET) < 0)
410     return 0;
411     #endif
412    
413     // Write data
414     return write(fh->fd, buffer, length);
415     }
416    
417    
418     /*
419     * Return size of file/device (minus header)
420     */
421    
422     loff_t SysGetFileSize(void *arg)
423     {
424     file_handle *fh = (file_handle *)arg;
425     if (!fh)
426     return true;
427    
428     if (fh->is_file)
429     return fh->file_size;
430     else {
431 cebix 1.3 #if defined(__linux__)
432 cebix 1.5 long blocks;
433     if (ioctl(fh->fd, BLKGETSIZE, &blocks) < 0)
434     return 0;
435     D(bug(" BLKGETSIZE returns %d blocks\n", blocks));
436     return (loff_t)blocks * 512;
437 cebix 1.1 #else
438     return lseek(fh->fd, 0, SEEK_END) - fh->start_byte;
439     #endif
440     }
441     }
442    
443    
444     /*
445     * Eject volume (if applicable)
446     */
447    
448     void SysEject(void *arg)
449     {
450     file_handle *fh = (file_handle *)arg;
451     if (!fh)
452     return;
453    
454     #if defined(__linux__)
455     if (fh->is_floppy) {
456     fsync(fh->fd);
457     ioctl(fh->fd, FDFLUSH);
458     ioctl(fh->fd, FDEJECT);
459     } else if (fh->is_cdrom) {
460     ioctl(fh->fd, CDROMEJECT);
461     close(fh->fd); // Close and reopen so the driver will see the media change
462     fh->fd = open(fh->name, O_RDONLY | O_NONBLOCK);
463     }
464 cebix 1.3 #elif defined(__FreeBSD__) || defined(__NetBSD__)
465 cebix 1.1 if (fh->is_floppy) {
466     fsync(fh->fd);
467     //ioctl(fh->fd, FDFLUSH);
468     //ioctl(fh->fd, FDEJECT);
469     } else if (fh->is_cdrom) {
470     ioctl(fh->fd, CDIOCEJECT);
471     close(fh->fd); // Close and reopen so the driver will see the media change
472     fh->fd = open(fh->name, O_RDONLY | O_NONBLOCK);
473     }
474     #endif
475     }
476    
477    
478     /*
479     * Format volume (if applicable)
480     */
481    
482     bool SysFormat(void *arg)
483     {
484     file_handle *fh = (file_handle *)arg;
485     if (!fh)
486     return false;
487    
488     //!!
489     return true;
490     }
491    
492    
493     /*
494     * Check if file/device is read-only (this includes the read-only flag on Sys_open())
495     */
496    
497     bool SysIsReadOnly(void *arg)
498     {
499     file_handle *fh = (file_handle *)arg;
500     if (!fh)
501     return true;
502    
503 cebix 1.3 #if defined(__linux__)
504 cebix 1.1 if (fh->is_floppy) {
505     struct floppy_drive_struct stat;
506     ioctl(fh->fd, FDGETDRVSTAT, &stat);
507     return !(stat.flags & FD_DISK_WRITABLE);
508     } else
509     #endif
510     return fh->read_only;
511     }
512    
513    
514     /*
515     * Check if the given file handle refers to a fixed or a removable disk
516     */
517    
518     bool SysIsFixedDisk(void *arg)
519     {
520     file_handle *fh = (file_handle *)arg;
521     if (!fh)
522     return true;
523    
524     if (fh->is_file)
525     return true;
526     else if (fh->is_floppy || fh->is_cdrom)
527     return false;
528     else
529     return true;
530     }
531    
532    
533     /*
534     * Check if a disk is inserted in the drive (always true for files)
535     */
536    
537     bool SysIsDiskInserted(void *arg)
538     {
539     file_handle *fh = (file_handle *)arg;
540     if (!fh)
541     return false;
542    
543     if (fh->is_file) {
544     return true;
545    
546     #if defined(__linux__)
547     } else if (fh->is_floppy) {
548     char block[512];
549     lseek(fh->fd, 0, SEEK_SET);
550     return read(fh->fd, block, 512) == 512;
551     } else if (fh->is_cdrom) {
552     #ifdef CDROM_DRIVE_STATUS
553     if (fh->cdrom_cap & CDC_DRIVE_STATUS) {
554     return ioctl(fh->fd, CDROM_DRIVE_STATUS, CDSL_CURRENT) == CDS_DISC_OK;
555     }
556     #endif
557     cdrom_tochdr header;
558     return ioctl(fh->fd, CDROMREADTOCHDR, &header) == 0;
559 cebix 1.3 #elif defined(__FreeBSD__) || defined(__NetBSD__)
560 cebix 1.1 } else if (fh->is_floppy) {
561     return false; //!!
562     } else if (fh->is_cdrom) {
563     struct ioc_toc_header header;
564     return ioctl(fh->fd, CDIOREADTOCHEADER, &header) == 0;
565     #endif
566    
567     } else
568     return true;
569     }
570    
571    
572     /*
573     * Prevent medium removal (if applicable)
574     */
575    
576     void SysPreventRemoval(void *arg)
577     {
578     file_handle *fh = (file_handle *)arg;
579     if (!fh)
580     return;
581    
582     #if defined(__linux__) && defined(CDROM_LOCKDOOR)
583     if (fh->is_cdrom)
584     ioctl(fh->fd, CDROM_LOCKDOOR, 1);
585     #endif
586     }
587    
588    
589     /*
590     * Allow medium removal (if applicable)
591     */
592    
593     void SysAllowRemoval(void *arg)
594     {
595     file_handle *fh = (file_handle *)arg;
596     if (!fh)
597     return;
598    
599 cebix 1.2 #if defined(__linux__) && defined(CDROM_LOCKDOOR)
600 cebix 1.1 if (fh->is_cdrom)
601     ioctl(fh->fd, CDROM_LOCKDOOR, 0);
602     #endif
603     }
604    
605    
606     /*
607     * Read CD-ROM TOC (binary MSF format, 804 bytes max.)
608     */
609    
610     bool SysCDReadTOC(void *arg, uint8 *toc)
611     {
612     file_handle *fh = (file_handle *)arg;
613     if (!fh)
614     return false;
615    
616     if (fh->is_cdrom) {
617     #if defined(__linux__)
618     uint8 *p = toc + 2;
619    
620     // Header
621     cdrom_tochdr header;
622     if (ioctl(fh->fd, CDROMREADTOCHDR, &header) < 0)
623     return false;
624     *p++ = header.cdth_trk0;
625     *p++ = header.cdth_trk1;
626    
627     // Tracks
628     cdrom_tocentry entry;
629     for (int i=header.cdth_trk0; i<=header.cdth_trk1; i++) {
630     entry.cdte_track = i;
631     entry.cdte_format = CDROM_MSF;
632     if (ioctl(fh->fd, CDROMREADTOCENTRY, &entry) < 0)
633     return false;
634     *p++ = 0;
635     *p++ = (entry.cdte_adr << 4) | entry.cdte_ctrl;
636     *p++ = entry.cdte_track;
637     *p++ = 0;
638     *p++ = 0;
639     *p++ = entry.cdte_addr.msf.minute;
640     *p++ = entry.cdte_addr.msf.second;
641     *p++ = entry.cdte_addr.msf.frame;
642     }
643    
644     // Leadout track
645     entry.cdte_track = CDROM_LEADOUT;
646     entry.cdte_format = CDROM_MSF;
647     if (ioctl(fh->fd, CDROMREADTOCENTRY, &entry) < 0)
648     return false;
649     *p++ = 0;
650     *p++ = (entry.cdte_adr << 4) | entry.cdte_ctrl;
651     *p++ = entry.cdte_track;
652     *p++ = 0;
653     *p++ = 0;
654     *p++ = entry.cdte_addr.msf.minute;
655     *p++ = entry.cdte_addr.msf.second;
656     *p++ = entry.cdte_addr.msf.frame;
657    
658     // TOC size
659     int toc_size = p - toc;
660     *toc++ = toc_size >> 8;
661     *toc++ = toc_size & 0xff;
662     return true;
663 cebix 1.4 #elif defined(__FreeBSD__)
664 cebix 1.1 uint8 *p = toc + 2;
665    
666     // Header
667     struct ioc_toc_header header;
668     if (ioctl(fh->fd, CDIOREADTOCHEADER, &header) < 0)
669     return false;
670     *p++ = header.starting_track;
671     *p++ = header.ending_track;
672    
673     // Tracks
674     struct ioc_read_toc_single_entry entry;
675     for (int i=header.starting_track; i<=header.ending_track; i++) {
676     entry.track = i;
677     entry.address_format = CD_MSF_FORMAT;
678     if (ioctl(fh->fd, CDIOREADTOCENTRY, &entry) < 0)
679     return false;
680     *p++ = 0;
681     *p++ = (entry.entry.addr_type << 4) | entry.entry.control;
682     *p++ = entry.entry.track;
683     *p++ = 0;
684     *p++ = 0;
685     *p++ = entry.entry.addr.msf.minute;
686     *p++ = entry.entry.addr.msf.second;
687     *p++ = entry.entry.addr.msf.frame;
688     }
689    
690     // Leadout track
691     entry.track = CD_TRACK_INFO;
692     entry.address_format = CD_MSF_FORMAT;
693     if (ioctl(fh->fd, CDIOREADTOCENTRY, &entry) < 0)
694     return false;
695     *p++ = 0;
696     *p++ = (entry.entry.addr_type << 4) | entry.entry.control;
697     *p++ = entry.entry.track;
698     *p++ = 0;
699     *p++ = 0;
700     *p++ = entry.entry.addr.msf.minute;
701     *p++ = entry.entry.addr.msf.second;
702     *p++ = entry.entry.addr.msf.frame;
703 cebix 1.4
704     // TOC size
705     int toc_size = p - toc;
706     *toc++ = toc_size >> 8;
707     *toc++ = toc_size & 0xff;
708     return true;
709     #elif defined(__NetBSD__)
710     uint8 *p = toc + 2;
711    
712     // Header
713     struct ioc_toc_header header;
714     if (ioctl(fh->fd, CDIOREADTOCHEADER, &header) < 0)
715     return false;
716     *p++ = header.starting_track;
717     *p++ = header.ending_track;
718    
719     // Tracks (this is nice... :-)
720     struct ioc_read_toc_entry entries;
721     entries.address_format = CD_MSF_FORMAT;
722     entries.starting_track = 1;
723     entries.data_len = 800;
724     entries.data = (cd_toc_entry *)p;
725     if (ioctl(fh->fd, CDIOREADTOCENTRIES, &entries) < 0)
726     return false;
727 cebix 1.1
728     // TOC size
729     int toc_size = p - toc;
730     *toc++ = toc_size >> 8;
731     *toc++ = toc_size & 0xff;
732     return true;
733     #endif
734     } else
735     return false;
736     }
737    
738    
739     /*
740     * Read CD-ROM position data (Sub-Q Channel, 16 bytes, see SCSI standard)
741     */
742    
743     bool SysCDGetPosition(void *arg, uint8 *pos)
744     {
745     file_handle *fh = (file_handle *)arg;
746     if (!fh)
747     return false;
748    
749     if (fh->is_cdrom) {
750     #if defined(__linux__)
751     cdrom_subchnl chan;
752     chan.cdsc_format = CDROM_MSF;
753     if (ioctl(fh->fd, CDROMSUBCHNL, &chan) < 0)
754     return false;
755     *pos++ = 0;
756     *pos++ = chan.cdsc_audiostatus;
757     *pos++ = 0;
758     *pos++ = 12; // Sub-Q data length
759     *pos++ = 0;
760     *pos++ = (chan.cdsc_adr << 4) | chan.cdsc_ctrl;
761     *pos++ = chan.cdsc_trk;
762     *pos++ = chan.cdsc_ind;
763     *pos++ = 0;
764     *pos++ = chan.cdsc_absaddr.msf.minute;
765     *pos++ = chan.cdsc_absaddr.msf.second;
766     *pos++ = chan.cdsc_absaddr.msf.frame;
767     *pos++ = 0;
768     *pos++ = chan.cdsc_reladdr.msf.minute;
769     *pos++ = chan.cdsc_reladdr.msf.second;
770     *pos++ = chan.cdsc_reladdr.msf.frame;
771     return true;
772 cebix 1.3 #elif defined(__FreeBSD__) || defined(__NetBSD__)
773 cebix 1.1 struct ioc_read_subchannel chan;
774     chan.data_format = CD_MSF_FORMAT;
775     chan.address_format = CD_MSF_FORMAT;
776     chan.track = CD_CURRENT_POSITION;
777     if (ioctl(fh->fd, CDIOCREADSUBCHANNEL, &chan) < 0)
778     return false;
779     *pos++ = 0;
780     *pos++ = chan.data->header.audio_status;
781     *pos++ = 0;
782     *pos++ = 12; // Sub-Q data length
783     *pos++ = 0;
784     *pos++ = (chan.data->what.position.addr_type << 4) | chan.data->what.position.control;
785     *pos++ = chan.data->what.position.track_number;
786     *pos++ = chan.data->what.position.index_number;
787     *pos++ = 0;
788     *pos++ = chan.data->what.position.absaddr.msf.minute;
789     *pos++ = chan.data->what.position.absaddr.msf.second;
790     *pos++ = chan.data->what.position.absaddr.msf.frame;
791     *pos++ = 0;
792     *pos++ = chan.data->what.position.reladdr.msf.minute;
793     *pos++ = chan.data->what.position.reladdr.msf.second;
794     *pos++ = chan.data->what.position.reladdr.msf.frame;
795     return true;
796     #endif
797     } else
798     return false;
799     }
800    
801    
802     /*
803     * Play CD audio
804     */
805    
806     bool SysCDPlay(void *arg, uint8 start_m, uint8 start_s, uint8 start_f, uint8 end_m, uint8 end_s, uint8 end_f)
807     {
808     file_handle *fh = (file_handle *)arg;
809     if (!fh)
810     return false;
811    
812     if (fh->is_cdrom) {
813     #if defined(__linux__)
814     cdrom_msf play;
815     play.cdmsf_min0 = start_m;
816     play.cdmsf_sec0 = start_s;
817     play.cdmsf_frame0 = start_f;
818     play.cdmsf_min1 = end_m;
819     play.cdmsf_sec1 = end_s;
820     play.cdmsf_frame1 = end_f;
821     return ioctl(fh->fd, CDROMPLAYMSF, &play) == 0;
822 cebix 1.3 #elif defined(__FreeBSD__) || defined(__NetBSD__)
823 cebix 1.1 struct ioc_play_msf play;
824     play.start_m = start_m;
825     play.start_s = start_s;
826     play.start_f = start_f;
827     play.end_m = end_m;
828     play.end_s = end_s;
829     play.end_f = end_f;
830     return ioctl(fh->fd, CDIOCPLAYMSF, &play) == 0;
831     #endif
832     } else
833     return false;
834     }
835    
836    
837     /*
838     * Pause CD audio
839     */
840    
841     bool SysCDPause(void *arg)
842     {
843     file_handle *fh = (file_handle *)arg;
844     if (!fh)
845     return false;
846    
847     if (fh->is_cdrom) {
848     #if defined(__linux__)
849     return ioctl(fh->fd, CDROMPAUSE) == 0;
850 cebix 1.3 #elif defined(__FreeBSD__) || defined(__NetBSD__)
851 cebix 1.1 return ioctl(fh->fd, CDIOCPAUSE) == 0;
852     #endif
853     } else
854     return false;
855     }
856    
857    
858     /*
859     * Resume paused CD audio
860     */
861    
862     bool SysCDResume(void *arg)
863     {
864     file_handle *fh = (file_handle *)arg;
865     if (!fh)
866     return false;
867    
868     if (fh->is_cdrom) {
869     #if defined(__linux__)
870     return ioctl(fh->fd, CDROMRESUME) == 0;
871 cebix 1.3 #elif defined(__FreeBSD__) || defined(__NetBSD__)
872 cebix 1.1 return ioctl(fh->fd, CDIOCRESUME) == 0;
873     #endif
874     } else
875     return false;
876     }
877    
878    
879     /*
880     * Stop CD audio
881     */
882    
883     bool SysCDStop(void *arg, uint8 lead_out_m, uint8 lead_out_s, uint8 lead_out_f)
884     {
885     file_handle *fh = (file_handle *)arg;
886     if (!fh)
887     return false;
888    
889     if (fh->is_cdrom) {
890     #if defined(__linux__)
891     return ioctl(fh->fd, CDROMSTOP) == 0;
892 cebix 1.3 #elif defined(__FreeBSD__) || defined(__NetBSD__)
893 cebix 1.1 return ioctl(fh->fd, CDIOCSTOP) == 0;
894     #endif
895     } else
896     return false;
897     }
898    
899    
900     /*
901     * Perform CD audio fast-forward/fast-reverse operation starting from specified address
902     */
903    
904     bool SysCDScan(void *arg, uint8 start_m, uint8 start_s, uint8 start_f, bool reverse)
905     {
906     file_handle *fh = (file_handle *)arg;
907     if (!fh)
908     return false;
909    
910     // Not supported under Linux
911     return false;
912     }
913    
914    
915     /*
916     * Set CD audio volume (0..255 each channel)
917     */
918    
919     void SysCDSetVolume(void *arg, uint8 left, uint8 right)
920     {
921     file_handle *fh = (file_handle *)arg;
922     if (!fh)
923     return;
924    
925     if (fh->is_cdrom) {
926     #if defined(__linux__)
927     cdrom_volctrl vol;
928     vol.channel0 = vol.channel2 = left;
929     vol.channel1 = vol.channel3 = right;
930     ioctl(fh->fd, CDROMVOLCTRL, &vol);
931 cebix 1.3 #elif defined(__FreeBSD__) || defined(__NetBSD__)
932 cebix 1.1 struct ioc_vol vol;
933     vol.vol[0] = vol.vol[2] = left;
934     vol.vol[1] = vol.vol[3] = right;
935     ioctl(fh->fd, CDIOCSETVOL, &vol);
936     #endif
937     }
938     }
939    
940    
941     /*
942     * Get CD audio volume (0..255 each channel)
943     */
944    
945     void SysCDGetVolume(void *arg, uint8 &left, uint8 &right)
946     {
947     file_handle *fh = (file_handle *)arg;
948     if (!fh)
949     return;
950    
951     left = right = 0;
952     if (fh->is_cdrom) {
953     #if defined(__linux__)
954     cdrom_volctrl vol;
955     ioctl(fh->fd, CDROMVOLREAD, &vol);
956     left = vol.channel0;
957     right = vol.channel1;
958 cebix 1.3 #elif defined(__FreeBSD__) || defined(__NetBSD__)
959 cebix 1.1 struct ioc_vol vol;
960     ioctl(fh->fd, CDIOCGETVOL, &vol);
961     left = vol.vol[0];
962     right = vol.vol[1];
963     #endif
964     }
965     }