ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/Unix/sys_unix.cpp
Revision: 1.16
Committed: 2002-03-27T20:11:12Z (22 years, 5 months ago) by cebix
Branch: MAIN
Changes since 1.15: +43 -5 lines
Log Message:
support for Linux devfs

File Contents

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