ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/MacOSX/sys_darwin.cpp
Revision: 1.8
Committed: 2005-06-12T23:36:33Z (19 years ago) by gbeauche
Branch: MAIN
CVS Tags: nigel-build-17
Changes since 1.7: +2 -2 lines
Log Message:
Check for CoreFoundation framework (Darwin 8.0.1). Likewise for
IOKit/storage/IOBlockStorageDevice.h which is not available there on x86

File Contents

# User Rev Content
1 nigel 1.1 /*
2 gbeauche 1.8 * $Id: sys_darwin.cpp,v 1.7 2005/01/30 21:42:13 gbeauche Exp $
3 nigel 1.1 *
4     * sys_darwin.cpp - Extra Darwin system dependant routines. Called by:
5     *
6     * sys_unix.cpp - System dependent routines, Unix implementation
7     *
8     * Based on Apple's CDROMSample.c and Evan Jones' cd-discid.c patches
9     *
10 gbeauche 1.7 * Basilisk II (C) 1997-2005 Christian Bauer
11 nigel 1.1 *
12     * This program is free software; you can redistribute it and/or modify
13     * it under the terms of the GNU General Public License as published by
14     * the Free Software Foundation; either version 2 of the License, or
15     * (at your option) any later version.
16     *
17     * This program is distributed in the hope that it will be useful,
18     * but WITHOUT ANY WARRANTY; without even the implied warranty of
19     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20     * GNU General Public License for more details.
21     *
22     * You should have received a copy of the GNU General Public License
23     * along with this program; if not, write to the Free Software
24     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25     */
26    
27     #import <errno.h>
28     #import <sys/param.h>
29     #import <IOKit/IOKitLib.h>
30     #import <IOKit/IOBSD.h>
31 nigel 1.4 #import <IOKit/serial/IOSerialKeys.h>
32 nigel 1.1 #import <IOKit/storage/IOMedia.h>
33     #import <IOKit/storage/IOMediaBSDClient.h>
34 gbeauche 1.8 #ifdef HAVE_IOKIT_STORAGE_IOBLOCKSTORAGEDEVICE_H
35 nigel 1.5 #import <IOKit/storage/IOBlockStorageDevice.h>
36     #endif
37 nigel 1.1 #import <IOKit/storage/IOCDMedia.h>
38     #import <IOKit/storage/IOCDMediaBSDClient.h>
39     #import <CoreFoundation/CoreFoundation.h>
40    
41     #import "sysdeps.h"
42    
43     #import "prefs.h"
44    
45     #define DEBUG 0
46     #import "debug.h"
47    
48    
49    
50     /*
51     * This gets called when no "cdrom" prefs items are found
52     * It scans for available CD-ROM drives and adds appropriate prefs items
53     */
54    
55     void DarwinAddCDROMPrefs(void)
56     {
57     mach_port_t masterPort; // The way to talk to the kernel
58     io_iterator_t allCDs; // List of CD drives on the system
59     CFMutableDictionaryRef classesToMatch;
60     io_object_t nextCD;
61    
62    
63     // Don't scan for drives if nocdrom option given
64     if ( PrefsFindBool("nocdrom") )
65     return;
66    
67    
68     // Let this task talk to the guts of the kernel:
69     if ( IOMasterPort(MACH_PORT_NULL, &masterPort) != KERN_SUCCESS )
70 nigel 1.4 bug("IOMasterPort failed. Won't be able to do anything with CD drives\n");
71 nigel 1.1
72    
73     // CD media are instances of class kIOCDMediaClass
74     classesToMatch = IOServiceMatching(kIOCDMediaClass);
75     if ( classesToMatch )
76     {
77     // Narrow the search a little further. Each IOMedia object
78     // has a property with key kIOMediaEjectable. We limit
79     // the match only to those CDs that are actually ejectable
80     CFDictionarySetValue(classesToMatch,
81 nigel 1.3 CFSTR(kIOMediaEjectableKey), kCFBooleanTrue);
82 nigel 1.1 }
83    
84     if ( IOServiceGetMatchingServices(masterPort,
85     classesToMatch, &allCDs) != KERN_SUCCESS )
86     {
87     D(bug("IOServiceGetMatchingServices failed. No CD media drives found?\n"));
88     return;
89     }
90    
91    
92     // Iterate through each CD drive
93     while ( nextCD = IOIteratorNext(allCDs))
94     {
95     char bsdPath[MAXPATHLEN];
96     CFTypeRef bsdPathAsCFString =
97 nigel 1.3 IORegistryEntryCreateCFProperty(nextCD,
98     CFSTR(kIOBSDNameKey),
99     kCFAllocatorDefault, 0);
100 nigel 1.1 *bsdPath = '\0';
101     if ( bsdPathAsCFString )
102     {
103     size_t devPathLength;
104    
105     strcpy(bsdPath, "/dev/");
106     devPathLength = strlen(bsdPath);
107    
108     if ( CFStringGetCString((const __CFString *)bsdPathAsCFString,
109     bsdPath + devPathLength,
110     MAXPATHLEN - devPathLength,
111     kCFStringEncodingASCII) )
112     {
113     D(bug("CDROM BSD path: %s\n", bsdPath));
114 nigel 1.4 PrefsAddString("cdrom", bsdPath);
115 nigel 1.1 }
116     else
117     D(bug("Could not get BSD device path for CD\n"));
118    
119     CFRelease(bsdPathAsCFString);
120     }
121 nigel 1.4 else
122     D(bug("Cannot determine bsdPath for CD\n"));
123 nigel 1.1 }
124    
125     IOObjectRelease(nextCD);
126     IOObjectRelease(allCDs);
127 nigel 1.4 }
128    
129    
130     void DarwinAddFloppyPrefs(void)
131     {
132     mach_port_t masterPort; // The way to talk to the kernel
133 nigel 1.6 io_iterator_t allFloppies; // List of possible floppys
134 nigel 1.4 CFMutableDictionaryRef classesToMatch;
135     io_object_t nextFloppy;
136    
137    
138     if ( IOMasterPort(MACH_PORT_NULL, &masterPort) != KERN_SUCCESS )
139     bug("IOMasterPort failed. Won't be able to do anything with floppy drives\n");
140    
141    
142 nigel 1.6 // This selects all partitions of all disks
143 nigel 1.4 classesToMatch = IOServiceMatching(kIOMediaClass);
144     if ( classesToMatch )
145     {
146 nigel 1.6 // Skip drivers and partitions
147     CFDictionarySetValue(classesToMatch,
148     CFSTR(kIOMediaWholeKey), kCFBooleanTrue);
149    
150     // Skip fixed drives (hard disks?)
151 nigel 1.4 CFDictionarySetValue(classesToMatch,
152 nigel 1.6 CFSTR(kIOMediaEjectableKey), kCFBooleanTrue);
153 nigel 1.4 }
154    
155     if ( IOServiceGetMatchingServices(masterPort,
156     classesToMatch, &allFloppies) != KERN_SUCCESS )
157     {
158     D(bug("IOServiceGetMatchingServices failed. No removable drives found?\n"));
159     return;
160     }
161    
162    
163     // Iterate through each floppy
164     while ( nextFloppy = IOIteratorNext(allFloppies))
165     {
166     char bsdPath[MAXPATHLEN];
167     CFTypeRef bsdPathAsCFString =
168     IORegistryEntryCreateCFProperty(nextFloppy,
169     CFSTR(kIOBSDNameKey),
170     kCFAllocatorDefault, 0);
171 nigel 1.6 long size;
172     CFTypeRef sizeAsCFNumber =
173     IORegistryEntryCreateCFProperty(nextFloppy,
174     CFSTR(kIOMediaSizeKey),
175     kCFAllocatorDefault, 0);
176    
177     if ( CFNumberGetValue((CFNumberRef)sizeAsCFNumber,
178     kCFNumberSInt32Type, &size) )
179     {
180     D(bug("Got size of %ld\n", size));
181     if ( size < 800 * 1024 || size > 1440 * 1024 )
182     {
183     D(puts("Device does not appear to be 800k or 1440k"));
184     continue;
185     }
186     }
187     else
188     bug("Couldn't get kIOMediaSizeKey of device");
189    
190    
191 nigel 1.4 *bsdPath = '\0';
192     if ( bsdPathAsCFString )
193     {
194     size_t devPathLength;
195    
196     strcpy(bsdPath, "/dev/");
197     devPathLength = strlen(bsdPath);
198    
199     if ( CFStringGetCString((const __CFString *)bsdPathAsCFString,
200     bsdPath + devPathLength,
201     MAXPATHLEN - devPathLength,
202     kCFStringEncodingASCII) )
203     {
204     D(bug("Floppy BSD path: %s\n", bsdPath));
205     PrefsAddString("floppy", bsdPath);
206     }
207     else
208     D(bug("Could not get BSD device path for floppy\n"));
209    
210     CFRelease(bsdPathAsCFString);
211     }
212     else
213     D(bug("Cannot determine bsdPath for floppy\n"));
214     }
215    
216     IOObjectRelease(nextFloppy);
217     IOObjectRelease(allFloppies);
218     }
219    
220    
221     void DarwinAddSerialPrefs(void)
222     {
223     mach_port_t masterPort; // The way to talk to the kernel
224     io_iterator_t allModems; // List of modems on the system
225     CFMutableDictionaryRef classesToMatch;
226     io_object_t nextModem;
227    
228    
229     if ( IOMasterPort(MACH_PORT_NULL, &masterPort) != KERN_SUCCESS )
230     bug("IOMasterPort failed. Won't be able to do anything with modems\n");
231    
232    
233     // Serial devices are instances of class IOSerialBSDClient
234     classesToMatch = IOServiceMatching(kIOSerialBSDServiceValue);
235     if ( classesToMatch )
236     {
237     // Narrow the search a little further. Each serial device object has
238     // a property with key kIOSerialBSDTypeKey and a value that is one of
239     // kIOSerialBSDAllTypes, kIOSerialBSDModemType, or kIOSerialBSDRS232Type.
240    
241     CFDictionarySetValue(classesToMatch,
242     CFSTR(kIOSerialBSDTypeKey),
243     CFSTR(kIOSerialBSDModemType));
244    
245     // This will find built-in and USB modems, but not serial modems.
246     }
247    
248     if ( IOServiceGetMatchingServices(masterPort,
249     classesToMatch, &allModems) != KERN_SUCCESS )
250     {
251     D(bug("IOServiceGetMatchingServices failed. No modems found?\n"));
252     return;
253     }
254    
255     // Iterate through each modem
256     while ( nextModem = IOIteratorNext(allModems))
257     {
258     char bsdPath[MAXPATHLEN];
259     CFTypeRef bsdPathAsCFString =
260     IORegistryEntryCreateCFProperty(nextModem,
261     CFSTR(kIOCalloutDeviceKey),
262     // kIODialinDeviceKey?
263     kCFAllocatorDefault, 0);
264     *bsdPath = '\0';
265     if ( bsdPathAsCFString )
266     {
267     if ( CFStringGetCString((const __CFString *)bsdPathAsCFString,
268     bsdPath, MAXPATHLEN,
269     kCFStringEncodingASCII) )
270     {
271     D(bug("Modem BSD path: %s\n", bsdPath));
272    
273     // Note that if there are multiple modems, we only get the last
274     PrefsAddString("seriala", bsdPath);
275     }
276     else
277     D(bug("Could not get BSD device path for modem\n"));
278    
279     CFRelease(bsdPathAsCFString);
280     }
281     else
282     D(puts("Cannot determine bsdPath for modem\n"));
283     }
284    
285     IOObjectRelease(nextModem);
286     IOObjectRelease(allModems);
287    
288    
289     // Getting a printer device is a bit harder. Creating a fake device
290     // that emulates a simple printer (e.g. a HP DeskJet) is one possibility,
291     // but for now I will just create a fake, safe, device entry:
292    
293     PrefsAddString("serialb", "/dev/null");
294 nigel 1.1 }
295    
296    
297     #ifdef MAC_OS_X_VERSION_10_2
298     /*
299     * Read CD-ROM TOC (binary MSF format, 804 bytes max.)
300     */
301    
302     bool DarwinCDReadTOC(char *name, uint8 *toc)
303     {
304     char *c, *devname;
305     int fd;
306    
307    
308     // The open filehandle is something like /dev/disk5s1
309     // The DKIOCCDREADTOC ioctl needs the original cd file,
310     // so we strip the s1 suffix off it, and open the file just for this ioctl
311    
312     devname = strdup(name);
313     if ( ! devname )
314     return false;
315    
316     for ( c = devname; *c; ++c ) ; // Go to the end of the name,
317     --c, --c; // point to the 's1' on the end,
318     *c = '\0'; // and truncate the string
319    
320     fd = open(devname, O_RDONLY);
321     if ( ! fd )
322     {
323     printf("Failed to open CD device %s for ioctl\n", devname);
324     free(devname);
325     return false;
326     }
327    
328     D(bug("Opened %s for ioctl()\n", devname));
329    
330     dk_cd_read_toc_t TOCrequest;
331    
332     // Setup the ioctl request structure:
333    
334     memset(&TOCrequest, 0, sizeof(TOCrequest));
335     TOCrequest.buffer = toc;
336     TOCrequest.bufferLength = 804;
337     TOCrequest.formatAsTime = kCDTrackInfoAddressTypeTrackNumber;
338    
339     if ( ioctl(fd, DKIOCCDREADTOC, &TOCrequest) < 0 )
340     {
341     printf("ioctl(DKIOCCDREADTOC) failed: %s\n", strerror(errno));
342     close(fd);
343     free(devname);
344     return false;
345     }
346     if ( TOCrequest.bufferLength < sizeof(CDTOC) )
347     {
348     printf("ioctl(DKIOCCDREADTOC): only read %d bytes (a CDTOC is at least %d)\n",
349     TOCrequest.bufferLength, (int)sizeof(CDTOC));
350     close(fd);
351     free(devname);
352     return false;
353     }
354     D(bug("ioctl(DKIOCCDREADTOC) read %d bytes\n", TOCrequest.bufferLength));
355    
356     close(fd);
357     free(devname);
358     return true;
359     }
360     #endif