ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/Unix/sys_unix.cpp
Revision: 1.18
Committed: 2002-04-28T14:06:17Z (22 years, 6 months ago) by cebix
Branch: MAIN
Changes since 1.17: +1 -3 lines
Log Message:
default CD-ROM drive is /dev/cd0c on NetBSD

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