1 |
/* |
2 |
* $Id: sys_darwin.cpp,v 1.12 2007/01/22 14:58:33 asvitkine Exp $ |
3 |
* |
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 |
* Basilisk II (C) 1997-2008 Christian Bauer |
11 |
* |
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 |
#import <IOKit/serial/IOSerialKeys.h> |
32 |
#import <IOKit/storage/IOMedia.h> |
33 |
#import <IOKit/storage/IOMediaBSDClient.h> |
34 |
#ifdef HAVE_IOKIT_STORAGE_IOBLOCKSTORAGEDEVICE_H |
35 |
#import <IOKit/storage/IOBlockStorageDevice.h> |
36 |
#endif |
37 |
#import <IOKit/storage/IOCDMedia.h> |
38 |
#import <IOKit/storage/IOCDMediaBSDClient.h> |
39 |
#import <CoreFoundation/CoreFoundation.h> |
40 |
|
41 |
#include "sysdeps.h" |
42 |
|
43 |
#include "sys.h" |
44 |
#include "prefs.h" |
45 |
|
46 |
#define DEBUG 0 |
47 |
#import "debug.h" |
48 |
|
49 |
|
50 |
// Global variables |
51 |
static CFRunLoopRef media_poll_loop = NULL; |
52 |
static bool media_thread_active = false; |
53 |
static pthread_t media_thread; |
54 |
|
55 |
// Prototypes |
56 |
static void *media_poll_func(void *); |
57 |
|
58 |
// From sys_unix.cpp |
59 |
extern void SysMediaArrived(const char *path, int type); |
60 |
extern void SysMediaRemoved(const char *path, int type); |
61 |
|
62 |
|
63 |
/* |
64 |
* Initialization |
65 |
*/ |
66 |
|
67 |
void DarwinSysInit(void) |
68 |
{ |
69 |
media_thread_active = (pthread_create(&media_thread, NULL, media_poll_func, NULL) == 0); |
70 |
D(bug("Media poll thread installed (%ld)\n", media_thread)); |
71 |
} |
72 |
|
73 |
|
74 |
/* |
75 |
* Deinitialization |
76 |
*/ |
77 |
|
78 |
void DarwinSysExit(void) |
79 |
{ |
80 |
// Stop media poll thread |
81 |
if (media_poll_loop) |
82 |
CFRunLoopStop(media_poll_loop); |
83 |
if (media_thread_active) |
84 |
pthread_join(media_thread, NULL); |
85 |
} |
86 |
|
87 |
|
88 |
/* |
89 |
* Get the BSD-style path of specified object |
90 |
*/ |
91 |
|
92 |
static kern_return_t get_device_path(io_object_t obj, char *path, size_t maxPathLength) |
93 |
{ |
94 |
kern_return_t kernResult = KERN_FAILURE; |
95 |
CFTypeRef pathAsCFString = IORegistryEntryCreateCFProperty(obj, CFSTR(kIOBSDNameKey), |
96 |
kCFAllocatorDefault, 0); |
97 |
if (pathAsCFString) { |
98 |
strcpy(path, "/dev/"); |
99 |
size_t pathLength = strlen(path); |
100 |
if (CFStringGetCString((const __CFString *)pathAsCFString, |
101 |
path + pathLength, |
102 |
maxPathLength - pathLength, |
103 |
kCFStringEncodingASCII)) |
104 |
kernResult = KERN_SUCCESS; |
105 |
CFRelease(pathAsCFString); |
106 |
} |
107 |
return kernResult; |
108 |
} |
109 |
|
110 |
|
111 |
/* |
112 |
* kIOMatchedNotification handler |
113 |
*/ |
114 |
|
115 |
static void media_arrived(int type, io_iterator_t iterator) |
116 |
{ |
117 |
io_object_t obj; |
118 |
while ((obj = IOIteratorNext(iterator)) != NULL) { |
119 |
char path[MAXPATHLEN]; |
120 |
kern_return_t kernResult = get_device_path(obj, path, sizeof(path)); |
121 |
if (kernResult == KERN_SUCCESS) { |
122 |
D(bug("Media Arrived: %s\n", path)); |
123 |
SysMediaArrived(path, type); |
124 |
} |
125 |
kernResult = IOObjectRelease(obj); |
126 |
if (kernResult != KERN_SUCCESS) { |
127 |
fprintf(stderr, "IOObjectRelease() returned %d\n", kernResult); |
128 |
} |
129 |
} |
130 |
} |
131 |
|
132 |
|
133 |
/* |
134 |
* kIOTerminatedNotification handler |
135 |
*/ |
136 |
|
137 |
static void media_removed(int type, io_iterator_t iterator) |
138 |
{ |
139 |
io_object_t obj; |
140 |
while ((obj = IOIteratorNext(iterator)) != NULL) { |
141 |
char path[MAXPATHLEN]; |
142 |
kern_return_t kernResult = get_device_path(obj, path, sizeof(path)); |
143 |
if (kernResult == KERN_SUCCESS) { |
144 |
D(bug("Media Removed: %s\n", path)); |
145 |
SysMediaRemoved(path, type); |
146 |
} |
147 |
kernResult = IOObjectRelease(obj); |
148 |
if (kernResult != KERN_SUCCESS) { |
149 |
fprintf(stderr, "IOObjectRelease() returned %d\n", kernResult); |
150 |
} |
151 |
} |
152 |
} |
153 |
|
154 |
|
155 |
/* |
156 |
* Media poll function |
157 |
*/ |
158 |
|
159 |
static void *media_poll_func(void *) |
160 |
{ |
161 |
media_poll_loop = CFRunLoopGetCurrent(); |
162 |
|
163 |
mach_port_t masterPort; |
164 |
kern_return_t kernResult = IOMasterPort(bootstrap_port, &masterPort); |
165 |
if (kernResult != KERN_SUCCESS) { |
166 |
fprintf(stderr, "IOMasterPort() returned %d\n", kernResult); |
167 |
return NULL; |
168 |
} |
169 |
|
170 |
CFMutableDictionaryRef matchingDictionary = IOServiceMatching(kIOCDMediaClass); |
171 |
if (matchingDictionary == NULL) { |
172 |
fprintf(stderr, "IOServiceMatching() returned a NULL dictionary\n"); |
173 |
return NULL; |
174 |
} |
175 |
matchingDictionary = (CFMutableDictionaryRef)CFRetain(matchingDictionary); |
176 |
|
177 |
IONotificationPortRef notificationPort = IONotificationPortCreate(kIOMasterPortDefault); |
178 |
CFRunLoopAddSource(media_poll_loop, |
179 |
IONotificationPortGetRunLoopSource(notificationPort), |
180 |
kCFRunLoopDefaultMode); |
181 |
|
182 |
io_iterator_t mediaArrivedIterator; |
183 |
kernResult = IOServiceAddMatchingNotification(notificationPort, |
184 |
kIOMatchedNotification, |
185 |
matchingDictionary, |
186 |
(IOServiceMatchingCallback)media_arrived, |
187 |
(void *)MEDIA_CD, &mediaArrivedIterator); |
188 |
if (kernResult != KERN_SUCCESS) |
189 |
fprintf(stderr, "IOServiceAddMatchingNotification() returned %d\n", kernResult); |
190 |
media_arrived(MEDIA_CD, mediaArrivedIterator); |
191 |
|
192 |
io_iterator_t mediaRemovedIterator; |
193 |
kernResult = IOServiceAddMatchingNotification(notificationPort, |
194 |
kIOTerminatedNotification, |
195 |
matchingDictionary, |
196 |
(IOServiceMatchingCallback)media_removed, |
197 |
(void *)MEDIA_CD, &mediaRemovedIterator); |
198 |
if (kernResult != KERN_SUCCESS) |
199 |
fprintf(stderr, "IOServiceAddMatchingNotification() returned %d\n", kernResult); |
200 |
media_removed(MEDIA_CD, mediaRemovedIterator); |
201 |
|
202 |
CFRunLoopRun(); |
203 |
return NULL; |
204 |
} |
205 |
|
206 |
|
207 |
void DarwinAddFloppyPrefs(void) |
208 |
{ |
209 |
mach_port_t masterPort; // The way to talk to the kernel |
210 |
io_iterator_t allFloppies; // List of possible floppys |
211 |
CFMutableDictionaryRef classesToMatch; |
212 |
io_object_t nextFloppy; |
213 |
|
214 |
|
215 |
if ( IOMasterPort(MACH_PORT_NULL, &masterPort) != KERN_SUCCESS ) |
216 |
bug("IOMasterPort failed. Won't be able to do anything with floppy drives\n"); |
217 |
|
218 |
|
219 |
// This selects all partitions of all disks |
220 |
classesToMatch = IOServiceMatching(kIOMediaClass); |
221 |
if ( classesToMatch ) |
222 |
{ |
223 |
// Skip drivers and partitions |
224 |
CFDictionarySetValue(classesToMatch, |
225 |
CFSTR(kIOMediaWholeKey), kCFBooleanTrue); |
226 |
|
227 |
// Skip fixed drives (hard disks?) |
228 |
CFDictionarySetValue(classesToMatch, |
229 |
CFSTR(kIOMediaEjectableKey), kCFBooleanTrue); |
230 |
} |
231 |
|
232 |
if ( IOServiceGetMatchingServices(masterPort, |
233 |
classesToMatch, &allFloppies) != KERN_SUCCESS ) |
234 |
{ |
235 |
D(bug("IOServiceGetMatchingServices failed. No removable drives found?\n")); |
236 |
return; |
237 |
} |
238 |
|
239 |
// Iterate through each floppy |
240 |
while ( nextFloppy = IOIteratorNext(allFloppies)) |
241 |
{ |
242 |
char bsdPath[MAXPATHLEN]; |
243 |
long size; |
244 |
CFTypeRef sizeAsCFNumber = |
245 |
IORegistryEntryCreateCFProperty(nextFloppy, |
246 |
CFSTR(kIOMediaSizeKey), |
247 |
kCFAllocatorDefault, 0); |
248 |
|
249 |
if ( CFNumberGetValue((CFNumberRef)sizeAsCFNumber, |
250 |
kCFNumberSInt32Type, &size) ) |
251 |
{ |
252 |
D(bug("Got size of %ld\n", size)); |
253 |
if ( size < 800 * 1024 || size > 1440 * 1024 ) |
254 |
{ |
255 |
D(puts("Device does not appear to be 800k or 1440k")); |
256 |
continue; |
257 |
} |
258 |
} |
259 |
else { |
260 |
D(puts("Couldn't get kIOMediaSizeKey of device")); |
261 |
continue; // if kIOMediaSizeKey is unavailable, we shouldn't use it anyway |
262 |
} |
263 |
|
264 |
if (get_device_path(nextFloppy, bsdPath, sizeof(bsdPath)) == KERN_SUCCESS) { |
265 |
PrefsAddString("floppy", bsdPath); |
266 |
} else { |
267 |
D(bug("Could not get BSD device path for floppy\n")); |
268 |
} |
269 |
} |
270 |
|
271 |
IOObjectRelease(nextFloppy); |
272 |
IOObjectRelease(allFloppies); |
273 |
} |
274 |
|
275 |
|
276 |
void DarwinAddSerialPrefs(void) |
277 |
{ |
278 |
mach_port_t masterPort; // The way to talk to the kernel |
279 |
io_iterator_t allModems; // List of modems on the system |
280 |
CFMutableDictionaryRef classesToMatch; |
281 |
io_object_t nextModem; |
282 |
|
283 |
|
284 |
if ( IOMasterPort(MACH_PORT_NULL, &masterPort) != KERN_SUCCESS ) |
285 |
bug("IOMasterPort failed. Won't be able to do anything with modems\n"); |
286 |
|
287 |
|
288 |
// Serial devices are instances of class IOSerialBSDClient |
289 |
classesToMatch = IOServiceMatching(kIOSerialBSDServiceValue); |
290 |
if ( classesToMatch ) |
291 |
{ |
292 |
// Narrow the search a little further. Each serial device object has |
293 |
// a property with key kIOSerialBSDTypeKey and a value that is one of |
294 |
// kIOSerialBSDAllTypes, kIOSerialBSDModemType, or kIOSerialBSDRS232Type. |
295 |
|
296 |
CFDictionarySetValue(classesToMatch, |
297 |
CFSTR(kIOSerialBSDTypeKey), |
298 |
CFSTR(kIOSerialBSDModemType)); |
299 |
|
300 |
// This will find built-in and USB modems, but not serial modems. |
301 |
} |
302 |
|
303 |
if ( IOServiceGetMatchingServices(masterPort, |
304 |
classesToMatch, &allModems) != KERN_SUCCESS ) |
305 |
{ |
306 |
D(bug("IOServiceGetMatchingServices failed. No modems found?\n")); |
307 |
return; |
308 |
} |
309 |
|
310 |
// Iterate through each modem |
311 |
while ( nextModem = IOIteratorNext(allModems)) |
312 |
{ |
313 |
char bsdPath[MAXPATHLEN]; |
314 |
CFTypeRef bsdPathAsCFString = |
315 |
IORegistryEntryCreateCFProperty(nextModem, |
316 |
CFSTR(kIOCalloutDeviceKey), |
317 |
// kIODialinDeviceKey? |
318 |
kCFAllocatorDefault, 0); |
319 |
*bsdPath = '\0'; |
320 |
if ( bsdPathAsCFString ) |
321 |
{ |
322 |
if ( CFStringGetCString((const __CFString *)bsdPathAsCFString, |
323 |
bsdPath, MAXPATHLEN, |
324 |
kCFStringEncodingASCII) ) |
325 |
{ |
326 |
D(bug("Modem BSD path: %s\n", bsdPath)); |
327 |
|
328 |
// Note that if there are multiple modems, we only get the last |
329 |
PrefsAddString("seriala", bsdPath); |
330 |
} |
331 |
else |
332 |
D(bug("Could not get BSD device path for modem\n")); |
333 |
|
334 |
CFRelease(bsdPathAsCFString); |
335 |
} |
336 |
else |
337 |
D(puts("Cannot determine bsdPath for modem\n")); |
338 |
} |
339 |
|
340 |
IOObjectRelease(nextModem); |
341 |
IOObjectRelease(allModems); |
342 |
|
343 |
|
344 |
// Getting a printer device is a bit harder. Creating a fake device |
345 |
// that emulates a simple printer (e.g. a HP DeskJet) is one possibility, |
346 |
// but for now I will just create a fake, safe, device entry: |
347 |
|
348 |
PrefsAddString("serialb", "/dev/null"); |
349 |
} |
350 |
|
351 |
|
352 |
#ifdef MAC_OS_X_VERSION_10_2 |
353 |
/* |
354 |
* Read CD-ROM TOC (binary MSF format, 804 bytes max.) |
355 |
*/ |
356 |
|
357 |
bool DarwinCDReadTOC(char *name, uint8 *toc) |
358 |
{ |
359 |
char *c, *devname; |
360 |
int fd; |
361 |
|
362 |
|
363 |
// The open filehandle is something like /dev/disk5s1 |
364 |
// The DKIOCCDREADTOC ioctl needs the original cd file, |
365 |
// so we strip the s1 suffix off it, and open the file just for this ioctl |
366 |
|
367 |
devname = strdup(name); |
368 |
if ( ! devname ) |
369 |
return false; |
370 |
|
371 |
for ( c = devname; *c; ++c ) ; // Go to the end of the name, |
372 |
--c, --c; // point to the 's1' on the end, |
373 |
*c = '\0'; // and truncate the string |
374 |
|
375 |
fd = open(devname, O_RDONLY); |
376 |
if ( ! fd ) |
377 |
{ |
378 |
printf("Failed to open CD device %s for ioctl\n", devname); |
379 |
free(devname); |
380 |
return false; |
381 |
} |
382 |
|
383 |
D(bug("Opened %s for ioctl()\n", devname)); |
384 |
|
385 |
dk_cd_read_toc_t TOCrequest; |
386 |
|
387 |
// Setup the ioctl request structure: |
388 |
|
389 |
memset(&TOCrequest, 0, sizeof(TOCrequest)); |
390 |
TOCrequest.buffer = toc; |
391 |
TOCrequest.bufferLength = 804; |
392 |
TOCrequest.formatAsTime = kCDTrackInfoAddressTypeTrackNumber; |
393 |
|
394 |
if ( ioctl(fd, DKIOCCDREADTOC, &TOCrequest) < 0 ) |
395 |
{ |
396 |
printf("ioctl(DKIOCCDREADTOC) failed: %s\n", strerror(errno)); |
397 |
close(fd); |
398 |
free(devname); |
399 |
return false; |
400 |
} |
401 |
if ( TOCrequest.bufferLength < sizeof(CDTOC) ) |
402 |
{ |
403 |
printf("ioctl(DKIOCCDREADTOC): only read %d bytes (a CDTOC is at least %d)\n", |
404 |
TOCrequest.bufferLength, (int)sizeof(CDTOC)); |
405 |
close(fd); |
406 |
free(devname); |
407 |
return false; |
408 |
} |
409 |
D(bug("ioctl(DKIOCCDREADTOC) read %d bytes\n", TOCrequest.bufferLength)); |
410 |
|
411 |
close(fd); |
412 |
free(devname); |
413 |
return true; |
414 |
} |
415 |
#endif |