ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/Unix/sys_unix.cpp
Revision: 1.14
Committed: 2002-02-23T17:54:24Z (22 years, 8 months ago) by cebix
Branch: MAIN
Changes since 1.13: +10 -4 lines
Log Message:
fixed floppy disk change detection problems under Linux

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    
35     #ifdef __NR__llseek
36 cebix 1.12 _syscall5(int, _llseek, unsigned int, fd, unsigned long, hi, unsigned long, lo, loff_t *, res, unsigned int, wh);
37 cebix 1.1 #else
38 cebix 1.12 static int _llseek(unsigned int fd, unsigned long hi, unsigned long lo, loff_t *res, unsigned int wh)
39 cebix 1.1 {
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 cebix 1.13 PrefsAddString("floppy", "/dev/fd0u1440");
124     PrefsAddString("floppy", "/dev/fd1u1440");
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 cebix 1.10 if (len == 0 || line[0] == '#')
150 cebix 1.1 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.8 #if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__)
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 cebix 1.14 close(fh->fd); // Close and reopen so the driver will see the media change
460     fh->fd = open(fh->name, fh->read_only ? O_RDONLY : O_RDWR);
461 cebix 1.1 } else if (fh->is_cdrom) {
462     ioctl(fh->fd, CDROMEJECT);
463     close(fh->fd); // Close and reopen so the driver will see the media change
464     fh->fd = open(fh->name, O_RDONLY | O_NONBLOCK);
465     }
466 cebix 1.3 #elif defined(__FreeBSD__) || defined(__NetBSD__)
467 cebix 1.1 if (fh->is_floppy) {
468     fsync(fh->fd);
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 cebix 1.14 ssize_t actual = read(fh->fd, block, 512);
551     if (actual < 0) {
552     close(fh->fd); // Close and reopen so the driver will see the media change
553     fh->fd = open(fh->name, fh->read_only ? O_RDONLY : O_RDWR);
554     actual = read(fh->fd, block, 512);
555     }
556     return actual == 512;
557 cebix 1.1 } else if (fh->is_cdrom) {
558 cebix 1.8 #ifdef CDROM_MEDIA_CHANGED
559     if (fh->cdrom_cap & CDC_MEDIA_CHANGED) {
560     // If we don't do this, all attempts to read from a disc fail
561 cebix 1.14 // once the tray has been opened (altough the TOC reads fine).
562 cebix 1.8 // Can somebody explain this to me?
563     if (ioctl(fh->fd, CDROM_MEDIA_CHANGED) == 1) {
564     close(fh->fd);
565     fh->fd = open(fh->name, O_RDONLY | O_NONBLOCK);
566     }
567     }
568     #endif
569 cebix 1.1 #ifdef CDROM_DRIVE_STATUS
570     if (fh->cdrom_cap & CDC_DRIVE_STATUS) {
571     return ioctl(fh->fd, CDROM_DRIVE_STATUS, CDSL_CURRENT) == CDS_DISC_OK;
572     }
573     #endif
574     cdrom_tochdr header;
575     return ioctl(fh->fd, CDROMREADTOCHDR, &header) == 0;
576 cebix 1.3 #elif defined(__FreeBSD__) || defined(__NetBSD__)
577 cebix 1.1 } else if (fh->is_floppy) {
578     return false; //!!
579     } else if (fh->is_cdrom) {
580     struct ioc_toc_header header;
581     return ioctl(fh->fd, CDIOREADTOCHEADER, &header) == 0;
582     #endif
583    
584     } else
585     return true;
586     }
587    
588    
589     /*
590     * Prevent medium removal (if applicable)
591     */
592    
593     void SysPreventRemoval(void *arg)
594     {
595     file_handle *fh = (file_handle *)arg;
596     if (!fh)
597     return;
598    
599     #if defined(__linux__) && defined(CDROM_LOCKDOOR)
600     if (fh->is_cdrom)
601     ioctl(fh->fd, CDROM_LOCKDOOR, 1);
602     #endif
603     }
604    
605    
606     /*
607     * Allow medium removal (if applicable)
608     */
609    
610     void SysAllowRemoval(void *arg)
611     {
612     file_handle *fh = (file_handle *)arg;
613     if (!fh)
614     return;
615    
616 cebix 1.2 #if defined(__linux__) && defined(CDROM_LOCKDOOR)
617 cebix 1.1 if (fh->is_cdrom)
618     ioctl(fh->fd, CDROM_LOCKDOOR, 0);
619     #endif
620     }
621    
622    
623     /*
624     * Read CD-ROM TOC (binary MSF format, 804 bytes max.)
625     */
626    
627     bool SysCDReadTOC(void *arg, uint8 *toc)
628     {
629     file_handle *fh = (file_handle *)arg;
630     if (!fh)
631     return false;
632    
633     if (fh->is_cdrom) {
634     #if defined(__linux__)
635     uint8 *p = toc + 2;
636    
637     // Header
638     cdrom_tochdr header;
639     if (ioctl(fh->fd, CDROMREADTOCHDR, &header) < 0)
640     return false;
641     *p++ = header.cdth_trk0;
642     *p++ = header.cdth_trk1;
643    
644     // Tracks
645     cdrom_tocentry entry;
646     for (int i=header.cdth_trk0; i<=header.cdth_trk1; i++) {
647     entry.cdte_track = i;
648     entry.cdte_format = CDROM_MSF;
649     if (ioctl(fh->fd, CDROMREADTOCENTRY, &entry) < 0)
650     return false;
651     *p++ = 0;
652     *p++ = (entry.cdte_adr << 4) | entry.cdte_ctrl;
653     *p++ = entry.cdte_track;
654     *p++ = 0;
655     *p++ = 0;
656     *p++ = entry.cdte_addr.msf.minute;
657     *p++ = entry.cdte_addr.msf.second;
658     *p++ = entry.cdte_addr.msf.frame;
659     }
660    
661     // Leadout track
662     entry.cdte_track = CDROM_LEADOUT;
663     entry.cdte_format = CDROM_MSF;
664     if (ioctl(fh->fd, CDROMREADTOCENTRY, &entry) < 0)
665     return false;
666     *p++ = 0;
667     *p++ = (entry.cdte_adr << 4) | entry.cdte_ctrl;
668     *p++ = entry.cdte_track;
669     *p++ = 0;
670     *p++ = 0;
671     *p++ = entry.cdte_addr.msf.minute;
672     *p++ = entry.cdte_addr.msf.second;
673     *p++ = entry.cdte_addr.msf.frame;
674    
675     // TOC size
676     int toc_size = p - toc;
677     *toc++ = toc_size >> 8;
678     *toc++ = toc_size & 0xff;
679     return true;
680 cebix 1.4 #elif defined(__FreeBSD__)
681 cebix 1.1 uint8 *p = toc + 2;
682    
683     // Header
684     struct ioc_toc_header header;
685     if (ioctl(fh->fd, CDIOREADTOCHEADER, &header) < 0)
686     return false;
687     *p++ = header.starting_track;
688     *p++ = header.ending_track;
689    
690     // Tracks
691     struct ioc_read_toc_single_entry entry;
692     for (int i=header.starting_track; i<=header.ending_track; i++) {
693     entry.track = i;
694     entry.address_format = CD_MSF_FORMAT;
695     if (ioctl(fh->fd, CDIOREADTOCENTRY, &entry) < 0)
696     return false;
697     *p++ = 0;
698     *p++ = (entry.entry.addr_type << 4) | entry.entry.control;
699     *p++ = entry.entry.track;
700     *p++ = 0;
701     *p++ = 0;
702     *p++ = entry.entry.addr.msf.minute;
703     *p++ = entry.entry.addr.msf.second;
704     *p++ = entry.entry.addr.msf.frame;
705     }
706    
707     // Leadout track
708     entry.track = CD_TRACK_INFO;
709     entry.address_format = CD_MSF_FORMAT;
710     if (ioctl(fh->fd, CDIOREADTOCENTRY, &entry) < 0)
711     return false;
712     *p++ = 0;
713     *p++ = (entry.entry.addr_type << 4) | entry.entry.control;
714     *p++ = entry.entry.track;
715     *p++ = 0;
716     *p++ = 0;
717     *p++ = entry.entry.addr.msf.minute;
718     *p++ = entry.entry.addr.msf.second;
719     *p++ = entry.entry.addr.msf.frame;
720 cebix 1.4
721     // TOC size
722     int toc_size = p - toc;
723     *toc++ = toc_size >> 8;
724     *toc++ = toc_size & 0xff;
725     return true;
726     #elif defined(__NetBSD__)
727     uint8 *p = toc + 2;
728    
729     // Header
730     struct ioc_toc_header header;
731     if (ioctl(fh->fd, CDIOREADTOCHEADER, &header) < 0)
732     return false;
733     *p++ = header.starting_track;
734     *p++ = header.ending_track;
735    
736     // Tracks (this is nice... :-)
737     struct ioc_read_toc_entry entries;
738     entries.address_format = CD_MSF_FORMAT;
739     entries.starting_track = 1;
740     entries.data_len = 800;
741     entries.data = (cd_toc_entry *)p;
742     if (ioctl(fh->fd, CDIOREADTOCENTRIES, &entries) < 0)
743     return false;
744 cebix 1.1
745     // TOC size
746     int toc_size = p - toc;
747     *toc++ = toc_size >> 8;
748     *toc++ = toc_size & 0xff;
749     return true;
750     #endif
751     } else
752     return false;
753     }
754    
755    
756     /*
757     * Read CD-ROM position data (Sub-Q Channel, 16 bytes, see SCSI standard)
758     */
759    
760     bool SysCDGetPosition(void *arg, uint8 *pos)
761     {
762     file_handle *fh = (file_handle *)arg;
763     if (!fh)
764     return false;
765    
766     if (fh->is_cdrom) {
767     #if defined(__linux__)
768     cdrom_subchnl chan;
769     chan.cdsc_format = CDROM_MSF;
770     if (ioctl(fh->fd, CDROMSUBCHNL, &chan) < 0)
771     return false;
772     *pos++ = 0;
773     *pos++ = chan.cdsc_audiostatus;
774     *pos++ = 0;
775     *pos++ = 12; // Sub-Q data length
776     *pos++ = 0;
777     *pos++ = (chan.cdsc_adr << 4) | chan.cdsc_ctrl;
778     *pos++ = chan.cdsc_trk;
779     *pos++ = chan.cdsc_ind;
780     *pos++ = 0;
781     *pos++ = chan.cdsc_absaddr.msf.minute;
782     *pos++ = chan.cdsc_absaddr.msf.second;
783     *pos++ = chan.cdsc_absaddr.msf.frame;
784     *pos++ = 0;
785     *pos++ = chan.cdsc_reladdr.msf.minute;
786     *pos++ = chan.cdsc_reladdr.msf.second;
787     *pos++ = chan.cdsc_reladdr.msf.frame;
788     return true;
789 cebix 1.3 #elif defined(__FreeBSD__) || defined(__NetBSD__)
790 cebix 1.1 struct ioc_read_subchannel chan;
791     chan.data_format = CD_MSF_FORMAT;
792     chan.address_format = CD_MSF_FORMAT;
793     chan.track = CD_CURRENT_POSITION;
794     if (ioctl(fh->fd, CDIOCREADSUBCHANNEL, &chan) < 0)
795     return false;
796     *pos++ = 0;
797     *pos++ = chan.data->header.audio_status;
798     *pos++ = 0;
799     *pos++ = 12; // Sub-Q data length
800     *pos++ = 0;
801     *pos++ = (chan.data->what.position.addr_type << 4) | chan.data->what.position.control;
802     *pos++ = chan.data->what.position.track_number;
803     *pos++ = chan.data->what.position.index_number;
804     *pos++ = 0;
805     *pos++ = chan.data->what.position.absaddr.msf.minute;
806     *pos++ = chan.data->what.position.absaddr.msf.second;
807     *pos++ = chan.data->what.position.absaddr.msf.frame;
808     *pos++ = 0;
809     *pos++ = chan.data->what.position.reladdr.msf.minute;
810     *pos++ = chan.data->what.position.reladdr.msf.second;
811     *pos++ = chan.data->what.position.reladdr.msf.frame;
812     return true;
813     #endif
814     } else
815     return false;
816     }
817    
818    
819     /*
820     * Play CD audio
821     */
822    
823     bool SysCDPlay(void *arg, uint8 start_m, uint8 start_s, uint8 start_f, uint8 end_m, uint8 end_s, uint8 end_f)
824     {
825     file_handle *fh = (file_handle *)arg;
826     if (!fh)
827     return false;
828    
829     if (fh->is_cdrom) {
830     #if defined(__linux__)
831     cdrom_msf play;
832     play.cdmsf_min0 = start_m;
833     play.cdmsf_sec0 = start_s;
834     play.cdmsf_frame0 = start_f;
835     play.cdmsf_min1 = end_m;
836     play.cdmsf_sec1 = end_s;
837     play.cdmsf_frame1 = end_f;
838     return ioctl(fh->fd, CDROMPLAYMSF, &play) == 0;
839 cebix 1.3 #elif defined(__FreeBSD__) || defined(__NetBSD__)
840 cebix 1.1 struct ioc_play_msf play;
841     play.start_m = start_m;
842     play.start_s = start_s;
843     play.start_f = start_f;
844     play.end_m = end_m;
845     play.end_s = end_s;
846     play.end_f = end_f;
847     return ioctl(fh->fd, CDIOCPLAYMSF, &play) == 0;
848     #endif
849     } else
850     return false;
851     }
852    
853    
854     /*
855     * Pause CD audio
856     */
857    
858     bool SysCDPause(void *arg)
859     {
860     file_handle *fh = (file_handle *)arg;
861     if (!fh)
862     return false;
863    
864     if (fh->is_cdrom) {
865     #if defined(__linux__)
866     return ioctl(fh->fd, CDROMPAUSE) == 0;
867 cebix 1.3 #elif defined(__FreeBSD__) || defined(__NetBSD__)
868 cebix 1.1 return ioctl(fh->fd, CDIOCPAUSE) == 0;
869     #endif
870     } else
871     return false;
872     }
873    
874    
875     /*
876     * Resume paused CD audio
877     */
878    
879     bool SysCDResume(void *arg)
880     {
881     file_handle *fh = (file_handle *)arg;
882     if (!fh)
883     return false;
884    
885     if (fh->is_cdrom) {
886     #if defined(__linux__)
887     return ioctl(fh->fd, CDROMRESUME) == 0;
888 cebix 1.3 #elif defined(__FreeBSD__) || defined(__NetBSD__)
889 cebix 1.1 return ioctl(fh->fd, CDIOCRESUME) == 0;
890     #endif
891     } else
892     return false;
893     }
894    
895    
896     /*
897     * Stop CD audio
898     */
899    
900     bool SysCDStop(void *arg, uint8 lead_out_m, uint8 lead_out_s, uint8 lead_out_f)
901     {
902     file_handle *fh = (file_handle *)arg;
903     if (!fh)
904     return false;
905    
906     if (fh->is_cdrom) {
907     #if defined(__linux__)
908     return ioctl(fh->fd, CDROMSTOP) == 0;
909 cebix 1.3 #elif defined(__FreeBSD__) || defined(__NetBSD__)
910 cebix 1.1 return ioctl(fh->fd, CDIOCSTOP) == 0;
911     #endif
912     } else
913     return false;
914     }
915    
916    
917     /*
918     * Perform CD audio fast-forward/fast-reverse operation starting from specified address
919     */
920    
921     bool SysCDScan(void *arg, uint8 start_m, uint8 start_s, uint8 start_f, bool reverse)
922     {
923     file_handle *fh = (file_handle *)arg;
924     if (!fh)
925     return false;
926    
927     // Not supported under Linux
928     return false;
929     }
930    
931    
932     /*
933     * Set CD audio volume (0..255 each channel)
934     */
935    
936     void SysCDSetVolume(void *arg, uint8 left, uint8 right)
937     {
938     file_handle *fh = (file_handle *)arg;
939     if (!fh)
940     return;
941    
942     if (fh->is_cdrom) {
943     #if defined(__linux__)
944     cdrom_volctrl vol;
945     vol.channel0 = vol.channel2 = left;
946     vol.channel1 = vol.channel3 = right;
947     ioctl(fh->fd, CDROMVOLCTRL, &vol);
948 cebix 1.3 #elif defined(__FreeBSD__) || defined(__NetBSD__)
949 cebix 1.1 struct ioc_vol vol;
950     vol.vol[0] = vol.vol[2] = left;
951     vol.vol[1] = vol.vol[3] = right;
952     ioctl(fh->fd, CDIOCSETVOL, &vol);
953     #endif
954     }
955     }
956    
957    
958     /*
959     * Get CD audio volume (0..255 each channel)
960     */
961    
962     void SysCDGetVolume(void *arg, uint8 &left, uint8 &right)
963     {
964     file_handle *fh = (file_handle *)arg;
965     if (!fh)
966     return;
967    
968     left = right = 0;
969     if (fh->is_cdrom) {
970     #if defined(__linux__)
971     cdrom_volctrl vol;
972     ioctl(fh->fd, CDROMVOLREAD, &vol);
973     left = vol.channel0;
974     right = vol.channel1;
975 cebix 1.3 #elif defined(__FreeBSD__) || defined(__NetBSD__)
976 cebix 1.1 struct ioc_vol vol;
977     ioctl(fh->fd, CDIOCGETVOL, &vol);
978     left = vol.vol[0];
979     right = vol.vol[1];
980     #endif
981     }
982     }