[16bdbf]: lib / driver / osx.c  Maximize  Restore  History

Download this file

1842 lines (1505 with data), 50.7 kB

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
/*
$Id: osx.c,v 1.5 2005/10/05 09:48:12 rocky Exp $
Copyright (C) 2003, 2004, 2005 Rocky Bernstein <rocky@panix.com>
from vcdimager code:
Copyright (C) 2001 Herbert Valerio Riedel <hvr@gnu.org>
and VideoLAN code Copyright (C) 1998-2001 VideoLAN
Authors: Johan Bilien <jobi@via.ecp.fr>
Gildas Bazin <gbazin@netcourrier.com>
Jon Lech Johansen <jon-vl@nanocrew.net>
Derk-Jan Hartman <hartman at videolan.org>
Justin F. Hallett <thesin@southofheaven.org>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/* This file contains OSX-specific code and implements low-level
control of the CD drive.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
static const char _rcsid[] = "$Id: osx.c,v 1.5 2005/10/05 09:48:12 rocky Exp $";
#include <cdio/logging.h>
#include <cdio/sector.h>
#include <cdio/util.h>
/* For SCSI TR_* enumerations */
#include <cdio/cdda.h>
#include "cdio_assert.h"
#include "cdio_private.h"
#include <string.h>
#ifdef HAVE_DARWIN_CDROM
#undef VERSION
#include <CoreFoundation/CoreFoundation.h>
#include <IOKit/IOKitLib.h>
#include <IOKit/storage/IOStorageDeviceCharacteristics.h>
#include <mach/mach.h>
#include <Carbon/Carbon.h>
#include <IOKit/scsi-commands/SCSITaskLib.h>
#include <IOKit/IOCFPlugIn.h>
#include <mach/mach_error.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <paths.h>
#include <CoreFoundation/CoreFoundation.h>
#include <IOKit/IOKitLib.h>
#include <IOKit/IOBSD.h>
#include <IOKit/scsi-commands/IOSCSIMultimediaCommandsDevice.h>
#include <IOKit/storage/IOCDTypes.h>
#include <IOKit/storage/IODVDTypes.h>
#include <IOKit/storage/IOMedia.h>
#include <IOKit/storage/IOCDMedia.h>
#include <IOKit/storage/IODVDMedia.h>
#include <IOKit/storage/IOCDMediaBSDClient.h>
#include <IOKit/storage/IODVDMediaBSDClient.h>
#include <IOKit/storage/IOStorageDeviceCharacteristics.h>
/* FIXME */
#define MAX_BIG_BUFF_SIZE 65535
#define kIOCDBlockStorageDeviceClassString "IOCDBlockStorageDevice"
/* Note leadout is normally defined 0xAA, But on OSX 0xA0 is "lead in" while
0xA2 is "lead out". Don't ask me why. */
#define OSX_CDROM_LEADOUT_TRACK 0xA2
#define TOTAL_TRACKS (p_env->i_last_track - p_env->gen.i_first_track + 1)
#define CDROM_CDI_TRACK 0x1
#define CDROM_XA_TRACK 0x2
typedef enum {
_AM_NONE,
_AM_OSX,
} access_mode_t;
#define MAX_SERVICE_NAME 1000
typedef struct {
/* Things common to all drivers like this.
This must be first. */
generic_img_private_t gen;
access_mode_t access_mode;
/* Track information */
CDTOC *pTOC;
int i_descriptors;
track_t i_last_track; /* highest track number */
track_t i_last_session; /* highest session number */
track_t i_first_session; /* first session number */
lsn_t *pp_lba;
io_service_t MediaClass_service;
char psz_MediaClass_service[MAX_SERVICE_NAME];
SCSITaskDeviceInterface **pp_scsiTaskDeviceInterface;
// io_service_t obj;
// SCSITaskDeviceInterface **scsi;
SCSITaskInterface **scsi_task;
MMCDeviceInterface **mmc;
IOCFPlugInInterface **plugin;
SCSI_Sense_Data sense;
SCSITaskStatus status;
UInt64 realized_len;
} _img_private_t;
static bool read_toc_osx (void *p_user_data);
static track_format_t get_track_format_osx(void *p_user_data,
track_t i_track);
/****
* GetRegistryEntryProperties - Gets the registry entry properties for
* an io_service_t.
*****/
static CFMutableDictionaryRef
GetRegistryEntryProperties ( io_service_t service )
{
IOReturn err = kIOReturnSuccess;
CFMutableDictionaryRef dict = 0;
err = IORegistryEntryCreateCFProperties (service, &dict, kCFAllocatorDefault, 0);
if ( err != kIOReturnSuccess )
cdio_warn( "IORegistryEntryCreateCFProperties: 0x%08x", err );
return dict;
}
#ifdef GET_SCSI_FIXED
static bool
get_scsi(_img_private_t *p_env)
{
SInt32 score;
kern_return_t err;
HRESULT herr;
err = IOCreatePlugInInterfaceForService(p_env->MediaClass_service,
kIOMMCDeviceUserClientTypeID,
kIOCFPlugInInterfaceID,
&p_env->plugin,
&score);
if (err != noErr) {
fprintf(stderr, "Error %x accessing MMC plugin.\n", err);
return false;
}
herr = (*p_env->plugin) ->
QueryInterface(p_env->plugin, CFUUIDGetUUIDBytes(kIOMMCDeviceInterfaceID),
(void *)&p_env->mmc);
if (herr != S_OK) {
fprintf(stderr, "Error %x accessing MMC interface.\n", (int) herr);
IODestroyPlugInInterface(p_env->plugin);
return false;
}
p_env->pp_scsiTaskDeviceInterface =
(*p_env->mmc)->GetSCSITaskDeviceInterface(p_env->mmc);
if (!p_env->pp_scsiTaskDeviceInterface) {
fprintf(stderr,
"Could not get SCSITaskkDevice interface from MMC interface.\n");
(*p_env->mmc)->Release(p_env->mmc);
IODestroyPlugInInterface(p_env->plugin);
return false;
}
err = (*p_env->pp_scsiTaskDeviceInterface)->
ObtainExclusiveAccess(p_env->pp_scsiTaskDeviceInterface);
if (err != kIOReturnSuccess) {
fprintf(stderr, "Could not obtain exclusive access to the device (%x).\n",
err);
if (err == kIOReturnBusy)
fprintf(stderr, "The volume is already mounted.\n");
else if (err == kIOReturnExclusiveAccess)
fprintf(stderr, "Another application already has exclusive access "
"to this device.\n");
else
fprintf(stderr, "I don't know why.\n");
(*p_env->pp_scsiTaskDeviceInterface)->
Release(p_env->pp_scsiTaskDeviceInterface);
(*p_env->mmc)->Release(p_env->mmc);
IODestroyPlugInInterface(p_env->plugin);
return false;
}
p_env->scsi_task =
(*p_env->pp_scsiTaskDeviceInterface) ->
CreateSCSITask(p_env->pp_scsiTaskDeviceInterface);
if (!p_env->scsi_task) {
fprintf(stderr, "Could not create a SCSITask interface.\n");
(*p_env->pp_scsiTaskDeviceInterface)->
ReleaseExclusiveAccess(p_env->pp_scsiTaskDeviceInterface);
(*p_env->pp_scsiTaskDeviceInterface)->
Release(p_env->pp_scsiTaskDeviceInterface);
(*p_env->mmc)->Release(p_env->mmc);
IODestroyPlugInInterface(p_env->plugin);
return false;
}
return true;
}
#endif
static bool
init_osx(_img_private_t *p_env) {
mach_port_t port;
char *psz_devname;
kern_return_t ret;
io_iterator_t iterator;
p_env->gen.fd = open( p_env->gen.source_name, O_RDONLY | O_NONBLOCK );
if (-1 == p_env->gen.fd) {
cdio_warn("Failed to open %s: %s", p_env->gen.source_name,
strerror(errno));
return false;
}
/* get the device name */
psz_devname = strrchr( p_env->gen.source_name, '/');
if( NULL != psz_devname )
++psz_devname;
else
psz_devname = p_env->gen.source_name;
/* unraw the device name */
if( *psz_devname == 'r' )
++psz_devname;
/* get port for IOKit communication */
ret = IOMasterPort( MACH_PORT_NULL, &port );
if( ret != KERN_SUCCESS )
{
cdio_warn( "IOMasterPort: 0x%08x", ret );
return false;
}
ret = IOServiceGetMatchingServices( port,
IOBSDNameMatching(port, 0, psz_devname),
&iterator );
/* get service iterator for the device */
if( ret != KERN_SUCCESS )
{
cdio_warn( "IOServiceGetMatchingServices: 0x%08x", ret );
return false;
}
/* first service */
p_env->MediaClass_service = IOIteratorNext( iterator );
IOObjectRelease( iterator );
/* search for kIOCDMediaClass or kIOCDVDMediaClass */
while( p_env->MediaClass_service &&
(!IOObjectConformsTo(p_env->MediaClass_service, kIOCDMediaClass)) &&
(!IOObjectConformsTo(p_env->MediaClass_service, kIODVDMediaClass)) )
{
ret = IORegistryEntryGetParentIterator( p_env->MediaClass_service,
kIOServicePlane,
&iterator );
if( ret != KERN_SUCCESS )
{
cdio_warn( "IORegistryEntryGetParentIterator: 0x%08x", ret );
IOObjectRelease( p_env->MediaClass_service );
return false;
}
IOObjectRelease( p_env->MediaClass_service );
p_env->MediaClass_service = IOIteratorNext( iterator );
IOObjectRelease( iterator );
}
if ( 0 == p_env->MediaClass_service ) {
cdio_warn( "search for kIOCDMediaClass/kIODVDMediaClass came up empty" );
return false;
}
/* Save the name so we can compare against this in case we have to do
another scan. FIXME: this is hoaky and there's got to be a better
variable to test or way to do.
*/
IORegistryEntryGetPath(p_env->MediaClass_service, kIOServicePlane,
p_env->psz_MediaClass_service);
#ifdef GET_SCSI_FIXED
return get_scsi(p_env);
#else
return true;
#endif
}
/*!
Run a SCSI MMC command.
cdio CD structure set by cdio_open().
i_timeout time in milliseconds we will wait for the command
to complete. If this value is -1, use the default
time-out value.
p_buf Buffer for data, both sending and receiving
i_buf Size of buffer
e_direction direction the transfer is to go.
cdb CDB bytes. All values that are needed should be set on
input. We'll figure out what the right CDB length should be.
We return true if command completed successfully and false if not.
*/
#if 1
/* process a complete scsi command. */
// handle_scsi_cmd(cdrom_drive *d,
static int
run_mmc_cmd_osx( void *p_user_data,
unsigned int i_timeout_ms,
unsigned int i_cdb, const mmc_cdb_t *p_cdb,
scsi_mmc_direction_t e_direction,
unsigned int i_buf, /*in/out*/ void *p_buf )
{
_img_private_t *p_env = p_user_data;
uint8_t cmdbuf[16];
UInt8 dir;
IOVirtualRange buf;
IOReturn ret;
if (!p_env->scsi_task) return DRIVER_OP_UNSUPPORTED;
memcpy(cmdbuf, p_cdb, i_cdb);
dir = ( SCSI_MMC_DATA_READ == e_direction)
? kSCSIDataTransfer_FromTargetToInitiator
: kSCSIDataTransfer_FromInitiatorToTarget;
if (!i_buf)
dir = kSCSIDataTransfer_NoDataTransfer;
if (i_buf > MAX_BIG_BUFF_SIZE) {
fprintf(stderr, "Excessive request size: %d bytes\n", i_buf);
return TR_ILLEGAL;
}
buf.address = (IOVirtualAddress)p_buf;
buf.length = i_buf;
ret = (*p_env->scsi_task)->SetCommandDescriptorBlock(p_env->scsi_task,
cmdbuf, i_cdb);
if (ret != kIOReturnSuccess) {
fprintf(stderr, "SetCommandDescriptorBlock: %x\n", ret);
return TR_UNKNOWN;
}
ret = (*p_env->scsi_task)->SetScatterGatherEntries(p_env->scsi_task, &buf, 1,
i_buf, dir);
if (ret != kIOReturnSuccess) {
fprintf(stderr, "SetScatterGatherEntries: %x\n", ret);
return TR_UNKNOWN;
}
ret = (*p_env->scsi_task)->ExecuteTaskSync(p_env->scsi_task, &p_env->sense,
&p_env->status,
&p_env->realized_len);
if (ret != kIOReturnSuccess) {
fprintf(stderr, "ExecuteTaskSync: %x\n", ret);
return TR_UNKNOWN;
}
if (p_env->status != kSCSITaskStatus_GOOD) {
int i;
fprintf(stderr, "SCSI status: %x\n", p_env->status);
fprintf(stderr, "Sense: %x %x %x\n",
p_env->sense.SENSE_KEY,
p_env->sense.ADDITIONAL_SENSE_CODE,
p_env->sense.ADDITIONAL_SENSE_CODE_QUALIFIER);
for (i = 0; i < i_cdb; i++)
fprintf(stderr, "%02x ", cmdbuf[i]);
fprintf(stderr, "\n");
return TR_UNKNOWN;
}
if (p_env->sense.VALID_RESPONSE_CODE) {
char key = p_env->sense.SENSE_KEY & 0xf;
char ASC = p_env->sense.ADDITIONAL_SENSE_CODE;
char ASCQ = p_env->sense.ADDITIONAL_SENSE_CODE_QUALIFIER;
switch (key) {
case 0:
if (errno == 0)
errno = EIO;
return (TR_UNKNOWN);
case 1:
break;
case 2:
if (errno == 0)
errno = EBUSY;
return (TR_BUSY);
case 3:
if (ASC == 0x0C && ASCQ == 0x09) {
/* loss of streaming */
if (errno == 0)
errno = EIO;
return (TR_STREAMING);
} else {
if (errno == 0)
errno = EIO;
return (TR_MEDIUM);
}
case 4:
if (errno == 0)
errno = EIO;
return (TR_FAULT);
case 5:
if (errno == 0)
errno = EINVAL;
return (TR_ILLEGAL);
default:
if (errno == 0)
errno = EIO;
return (TR_UNKNOWN);
}
}
errno = 0;
return (0);
}
#endif
#if 0
/*!
Run a SCSI MMC command.
cdio CD structure set by cdio_open().
i_timeout time in milliseconds we will wait for the command
to complete. If this value is -1, use the default
time-out value.
p_buf Buffer for data, both sending and receiving
i_buf Size of buffer
e_direction direction the transfer is to go.
cdb CDB bytes. All values that are needed should be set on
input. We'll figure out what the right CDB length should be.
We return true if command completed successfully and false if not.
*/
static int
run_mmc_cmd_osx( const void *p_user_data,
unsigned int i_timeout_ms,
unsigned int i_cdb, const mmc_cdb_t *p_cdb,
scsi_mmc_direction_t e_direction,
unsigned int i_buf, /*in/out*/ void *p_buf )
{
#ifndef SCSI_MMC_FIXED
return DRIVER_OP_UNSUPPORTED;
#else
const _img_private_t *p_env = p_user_data;
SCSITaskDeviceInterface **sc;
SCSITaskInterface **cmd = NULL;
IOVirtualRange iov;
SCSI_Sense_Data senseData;
SCSITaskStatus status;
UInt64 bytesTransferred;
IOReturn ioReturnValue;
int ret = 0;
if (NULL == p_user_data) return 2;
/* Make sure pp_scsiTaskDeviceInterface is initialized. FIXME: The code
should probably be reorganized better for this. */
if (!p_env->gen.toc_init) read_toc_osx (p_user_data) ;
sc = p_env->pp_scsiTaskDeviceInterface;
if (NULL == sc) return 3;
cmd = (*sc)->CreateSCSITask(sc);
if (cmd == NULL) {
cdio_warn("Failed to create SCSI task");
return -1;
}
iov.address = (IOVirtualAddress) p_buf;
iov.length = i_buf;
ioReturnValue = (*cmd)->SetCommandDescriptorBlock(cmd, (UInt8 *) p_cdb,
i_cdb);
if (ioReturnValue != kIOReturnSuccess) {
cdio_warn("SetCommandDescriptorBlock failed with status %x",
ioReturnValue);
return -1;
}
ioReturnValue = (*cmd)->SetScatterGatherEntries(cmd, &iov, 1, i_buf,
(SCSI_MMC_DATA_READ == e_direction ) ?
kSCSIDataTransfer_FromTargetToInitiator :
kSCSIDataTransfer_FromInitiatorToTarget);
if (ioReturnValue != kIOReturnSuccess) {
cdio_warn("SetScatterGatherEntries failed with status %x", ioReturnValue);
return -1;
}
ioReturnValue = (*cmd)->SetTimeoutDuration(cmd, i_timeout_ms );
if (ioReturnValue != kIOReturnSuccess) {
cdio_warn("SetTimeoutDuration failed with status %x", ioReturnValue);
return -1;
}
memset(&senseData, 0, sizeof(senseData));
ioReturnValue = (*cmd)->ExecuteTaskSync(cmd,&senseData, &status, &
bytesTransferred);
if (ioReturnValue != kIOReturnSuccess) {
cdio_warn("Command execution failed with status %x", ioReturnValue);
return -1;
}
if (cmd != NULL) {
(*cmd)->Release(cmd);
}
return (ret);
#endif
}
#endif /* 0*/
/***************************************************************************
* GetDeviceIterator - Gets an io_iterator_t for our class type
***************************************************************************/
static io_iterator_t
GetDeviceIterator ( const char * deviceClass )
{
IOReturn err = kIOReturnSuccess;
io_iterator_t iterator = MACH_PORT_NULL;
err = IOServiceGetMatchingServices ( kIOMasterPortDefault,
IOServiceMatching ( deviceClass ),
&iterator );
check ( err == kIOReturnSuccess );
return iterator;
}
/***************************************************************************
* GetFeaturesFlagsForDrive -Gets the bitfield which represents the
* features flags.
***************************************************************************/
static bool
GetFeaturesFlagsForDrive ( CFDictionaryRef dict,
uint32_t *i_cdFlags,
uint32_t *i_dvdFlags )
{
CFDictionaryRef propertiesDict = 0;
CFNumberRef flagsNumberRef = 0;
*i_cdFlags = 0;
*i_dvdFlags= 0;
propertiesDict = ( CFDictionaryRef )
CFDictionaryGetValue ( dict,
CFSTR ( kIOPropertyDeviceCharacteristicsKey ) );
if ( propertiesDict == 0 ) return false;
/* Get the CD features */
flagsNumberRef = ( CFNumberRef )
CFDictionaryGetValue ( propertiesDict,
CFSTR ( kIOPropertySupportedCDFeatures ) );
if ( flagsNumberRef != 0 ) {
CFNumberGetValue ( flagsNumberRef, kCFNumberLongType, i_cdFlags );
}
/* Get the DVD features */
flagsNumberRef = ( CFNumberRef )
CFDictionaryGetValue ( propertiesDict,
CFSTR ( kIOPropertySupportedDVDFeatures ) );
if ( flagsNumberRef != 0 ) {
CFNumberGetValue ( flagsNumberRef, kCFNumberLongType, i_dvdFlags );
}
return true;
}
/*!
Get disc type associated with the cd object.
*/
static discmode_t
get_discmode_osx (void *p_user_data)
{
_img_private_t *p_env = p_user_data;
char str[10];
int32_t i_discmode = CDIO_DISC_MODE_ERROR;
CFDictionaryRef propertiesDict = 0;
CFStringRef data;
propertiesDict = GetRegistryEntryProperties ( p_env->MediaClass_service );
if ( propertiesDict == 0 ) return i_discmode;
data = ( CFStringRef )
CFDictionaryGetValue ( propertiesDict, CFSTR ( kIODVDMediaTypeKey ) );
if( CFStringGetCString( data, str, sizeof(str),
kCFStringEncodingASCII ) ) {
if (0 == strncmp(str, "DVD+R", strlen(str)) )
i_discmode = CDIO_DISC_MODE_DVD_PR;
else if (0 == strncmp(str, "DVD+RW", strlen(str)) )
i_discmode = CDIO_DISC_MODE_DVD_PRW;
else if (0 == strncmp(str, "DVD-R", strlen(str)) )
i_discmode = CDIO_DISC_MODE_DVD_R;
else if (0 == strncmp(str, "DVD-RW", strlen(str)) )
i_discmode = CDIO_DISC_MODE_DVD_RW;
else if (0 == strncmp(str, "DVD-ROM", strlen(str)) )
i_discmode = CDIO_DISC_MODE_DVD_ROM;
else if (0 == strncmp(str, "DVD-RAM", strlen(str)) )
i_discmode = CDIO_DISC_MODE_DVD_RAM;
else if (0 == strncmp(str, "CD-ROM", strlen(str)) )
i_discmode = CDIO_DISC_MODE_CD_DATA;
else if (0 == strncmp(str, "CDR", strlen(str)) )
i_discmode = CDIO_DISC_MODE_CD_DATA;
else if (0 == strncmp(str, "CDRW", strlen(str)) )
i_discmode = CDIO_DISC_MODE_CD_DATA;
//?? Handled by below? CFRelease( data );
}
CFRelease( propertiesDict );
if (CDIO_DISC_MODE_CD_DATA == i_discmode) {
/* Need to do more classification */
return get_discmode_cd_generic(p_user_data);
}
return i_discmode;
}
static io_service_t
get_drive_service_osx(const _img_private_t *p_env)
{
io_service_t service;
io_iterator_t service_iterator;
service_iterator = GetDeviceIterator ( kIOCDBlockStorageDeviceClassString );
if( service_iterator == MACH_PORT_NULL ) return 0;
service = IOIteratorNext( service_iterator );
if( service == 0 ) return 0;
do
{
char psz_service[MAX_SERVICE_NAME];
IORegistryEntryGetPath(service, kIOServicePlane, psz_service);
psz_service[MAX_SERVICE_NAME-1] = '\0';
/* FIXME: This is all hoaky. Here we need info from a parent class,
psz_service of what we opened above. We are relying on the
fact that the name will be a substring of the name we
openned with.
*/
if (0 == strncmp(psz_service, p_env->psz_MediaClass_service,
strlen(psz_service))) {
/* Found our device */
IOObjectRelease( service_iterator );
return service;
}
IOObjectRelease( service );
} while( ( service = IOIteratorNext( service_iterator ) ) != 0 );
IOObjectRelease( service_iterator );
return service;
}
static void
get_drive_cap_osx(const void *p_user_data,
/*out*/ cdio_drive_read_cap_t *p_read_cap,
/*out*/ cdio_drive_write_cap_t *p_write_cap,
/*out*/ cdio_drive_misc_cap_t *p_misc_cap)
{
const _img_private_t *p_env = p_user_data;
uint32_t i_cdFlags;
uint32_t i_dvdFlags;
io_service_t service = get_drive_service_osx(p_env);
if( service == 0 ) goto err_exit;
/* Found our device */
{
CFDictionaryRef properties = GetRegistryEntryProperties ( service );
if (! GetFeaturesFlagsForDrive ( properties, &i_cdFlags,
&i_dvdFlags ) ) {
IOObjectRelease( service );
goto err_exit;
}
/* Reader */
if ( 0 != (i_cdFlags & kCDFeaturesAnalogAudioMask) )
*p_read_cap |= CDIO_DRIVE_CAP_READ_AUDIO;
if ( 0 != (i_cdFlags & kCDFeaturesWriteOnceMask) )
*p_write_cap |= CDIO_DRIVE_CAP_WRITE_CD_R;
if ( 0 != (i_cdFlags & kCDFeaturesCDDAStreamAccurateMask) )
*p_read_cap |= CDIO_DRIVE_CAP_READ_CD_DA;
if ( 0 != (i_dvdFlags & kDVDFeaturesReadStructuresMask) )
*p_read_cap |= CDIO_DRIVE_CAP_READ_DVD_ROM;
if ( 0 != (i_cdFlags & kCDFeaturesReWriteableMask) )
*p_write_cap |= CDIO_DRIVE_CAP_WRITE_CD_RW;
if ( 0 != (i_dvdFlags & kDVDFeaturesWriteOnceMask) )
*p_write_cap |= CDIO_DRIVE_CAP_WRITE_DVD_R;
if ( 0 != (i_dvdFlags & kDVDFeaturesRandomWriteableMask) )
*p_write_cap |= CDIO_DRIVE_CAP_WRITE_DVD_RAM;
if ( 0 != (i_dvdFlags & kDVDFeaturesReWriteableMask) )
*p_write_cap |= CDIO_DRIVE_CAP_WRITE_DVD_RW;
/***
if ( 0 != (i_dvdFlags & kDVDFeaturesPlusRMask) )
*p_write_cap |= CDIO_DRIVE_CAP_WRITE_DVD_PR;
if ( 0 != (i_dvdFlags & kDVDFeaturesPlusRWMask )
*p_write_cap |= CDIO_DRIVE_CAP_WRITE_DVD_PRW;
***/
/* FIXME: fill out. For now assume CD-ROM is relatively modern. */
*p_misc_cap = (
CDIO_DRIVE_CAP_MISC_CLOSE_TRAY
| CDIO_DRIVE_CAP_MISC_EJECT
| CDIO_DRIVE_CAP_MISC_LOCK
| CDIO_DRIVE_CAP_MISC_SELECT_SPEED
| CDIO_DRIVE_CAP_MISC_MULTI_SESSION
| CDIO_DRIVE_CAP_MISC_MEDIA_CHANGED
| CDIO_DRIVE_CAP_MISC_RESET
| CDIO_DRIVE_CAP_READ_MCN
| CDIO_DRIVE_CAP_READ_ISRC
);
IOObjectRelease( service );
}
return;
err_exit:
*p_misc_cap = *p_write_cap = *p_read_cap = CDIO_DRIVE_CAP_UNKNOWN;
return;
}
#if 1
/****************************************************************************
* GetDriveDescription - Gets drive description.
****************************************************************************/
static bool
get_hwinfo_osx ( const CdIo_t *p_cdio, /*out*/ cdio_hwinfo_t *hw_info)
{
_img_private_t *p_env = (_img_private_t *) p_cdio->env;
io_service_t service = get_drive_service_osx(p_env);
if ( service == 0 ) return false;
/* Found our device */
{
CFStringRef vendor = NULL;
CFStringRef product = NULL;
CFStringRef revision = NULL;
CFDictionaryRef properties = GetRegistryEntryProperties ( service );
CFDictionaryRef deviceDict = ( CFDictionaryRef )
CFDictionaryGetValue ( properties,
CFSTR ( kIOPropertyDeviceCharacteristicsKey ) );
if ( deviceDict == 0 ) return false;
vendor = ( CFStringRef )
CFDictionaryGetValue ( deviceDict, CFSTR ( kIOPropertyVendorNameKey ) );
if ( CFStringGetCString( vendor,
(char *) &(hw_info->psz_vendor),
sizeof(hw_info->psz_vendor),
kCFStringEncodingASCII ) )
CFRelease( vendor );
product = ( CFStringRef )
CFDictionaryGetValue ( deviceDict, CFSTR ( kIOPropertyProductNameKey ) );
if ( CFStringGetCString( product,
(char *) &(hw_info->psz_model),
sizeof(hw_info->psz_model),
kCFStringEncodingASCII ) )
CFRelease( product );
revision = ( CFStringRef )
CFDictionaryGetValue ( deviceDict,
CFSTR ( kIOPropertyProductRevisionLevelKey ) );
if ( CFStringGetCString( revision,
(char *) &(hw_info->psz_revision),
sizeof(hw_info->psz_revision),
kCFStringEncodingASCII ) )
CFRelease( revision );
}
return true;
}
#endif
/*!
Return the media catalog number MCN.
Note: string is malloc'd so caller should free() then returned
string when done with it.
*/
static cdtext_t *
get_cdtext_osx (void *p_user_data, track_t i_track)
{
return NULL;
}
static void
_free_osx (void *p_user_data) {
_img_private_t *p_env = p_user_data;
if (NULL == p_env) return;
cdio_generic_free(p_env);
if (NULL != p_env->pp_lba) free((void *) p_env->pp_lba);
if (NULL != p_env->pTOC) free((void *) p_env->pTOC);
IOObjectRelease( p_env->MediaClass_service );
if (p_env->scsi_task)
(*p_env->scsi_task)->Release(p_env->scsi_task);
if (p_env->pp_scsiTaskDeviceInterface)
(*p_env->pp_scsiTaskDeviceInterface) ->
ReleaseExclusiveAccess(p_env->pp_scsiTaskDeviceInterface);
if (p_env->pp_scsiTaskDeviceInterface)
(*p_env->pp_scsiTaskDeviceInterface) ->
Release ( p_env->pp_scsiTaskDeviceInterface );
if (p_env->mmc)
(*p_env->mmc)->Release(p_env->mmc);
if (p_env->plugin)
IODestroyPlugInInterface(p_env->plugin);
IOObjectRelease(p_env->MediaClass_service);
}
/*!
Reads i_blocks of data sectors from cd device into p_data starting
from i_lsn.
Returns DRIVER_OP_SUCCESS if no error.
*/
static driver_return_code_t
read_data_sectors_osx (void *p_user_data, void *p_data, lsn_t i_lsn,
uint16_t i_blocksize, uint32_t i_blocks)
{
_img_private_t *p_env = p_user_data;
if (!p_user_data) return DRIVER_OP_UNINIT;
{
dk_cd_read_t cd_read;
track_t i_track = cdio_get_track(p_env->gen.cdio, i_lsn);
memset( &cd_read, 0, sizeof(cd_read) );
cd_read.sectorArea = kCDSectorAreaUser;
cd_read.buffer = p_data;
/* FIXME: Do I have to put use get_track_green_osx? */
switch(get_track_format_osx(p_user_data, i_track)) {
case TRACK_FORMAT_CDI:
case TRACK_FORMAT_DATA:
cd_read.sectorType = kCDSectorTypeMode1;
cd_read.offset = i_lsn * kCDSectorSizeMode1;
break;
case TRACK_FORMAT_XA:
cd_read.sectorType = kCDSectorTypeMode2;
cd_read.offset = i_lsn * kCDSectorSizeMode2;
break;
default:
return DRIVER_OP_ERROR;
}
cd_read.bufferLength = i_blocksize * i_blocks;
if( ioctl( p_env->gen.fd, DKIOCCDREAD, &cd_read ) == -1 )
{
cdio_info( "could not read block %d, %s", i_lsn, strerror(errno) );
return DRIVER_OP_ERROR;
}
return DRIVER_OP_SUCCESS;
}
}
/*!
Reads i_blocks of mode2 form2 sectors from cd device into data starting
from i_lsn.
Returns 0 if no error.
*/
static driver_return_code_t
read_mode1_sectors_osx (void *p_user_data, void *p_data, lsn_t i_lsn,
bool b_form2, uint32_t i_blocks)
{
_img_private_t *p_env = p_user_data;
dk_cd_read_t cd_read;
memset( &cd_read, 0, sizeof(cd_read) );
cd_read.sectorArea = kCDSectorAreaUser;
cd_read.buffer = p_data;
cd_read.sectorType = kCDSectorTypeMode1;
if (b_form2) {
cd_read.offset = i_lsn * kCDSectorSizeMode2;
cd_read.bufferLength = kCDSectorSizeMode2 * i_blocks;
} else {
cd_read.offset = i_lsn * kCDSectorSizeMode1;
cd_read.bufferLength = kCDSectorSizeMode1 * i_blocks;
}
if( ioctl( p_env->gen.fd, DKIOCCDREAD, &cd_read ) == -1 )
{
cdio_info( "could not read block %d, %s", i_lsn, strerror(errno) );
return DRIVER_OP_ERROR;
}
return DRIVER_OP_SUCCESS;
}
/*!
Reads i_blocks of mode2 form2 sectors from cd device into data starting
from lsn.
Returns DRIVER_OP_SUCCESS if no error.
*/
static driver_return_code_t
read_mode2_sectors_osx (void *p_user_data, void *p_data, lsn_t i_lsn,
bool b_form2, uint32_t i_blocks)
{
_img_private_t *p_env = p_user_data;
dk_cd_read_t cd_read;
memset( &cd_read, 0, sizeof(cd_read) );
cd_read.sectorArea = kCDSectorAreaUser;
cd_read.buffer = p_data;
if (b_form2) {
cd_read.offset = i_lsn * kCDSectorSizeMode2Form2;
cd_read.sectorType = kCDSectorTypeMode2Form2;
cd_read.bufferLength = kCDSectorSizeMode2Form2 * i_blocks;
} else {
cd_read.offset = i_lsn * kCDSectorSizeMode2Form1;
cd_read.sectorType = kCDSectorTypeMode2Form1;
cd_read.bufferLength = kCDSectorSizeMode2Form1 * i_blocks;
}
if( ioctl( p_env->gen.fd, DKIOCCDREAD, &cd_read ) == -1 )
{
cdio_info( "could not read block %d, %s", i_lsn, strerror(errno) );
return DRIVER_OP_ERROR;
}
return DRIVER_OP_SUCCESS;
}
/*!
Reads a single audio sector from CD device into p_data starting from lsn.
Returns 0 if no error.
*/
static int
read_audio_sectors_osx (void *user_data, void *p_data, lsn_t lsn,
unsigned int i_blocks)
{
_img_private_t *env = user_data;
dk_cd_read_t cd_read;
memset( &cd_read, 0, sizeof(cd_read) );
cd_read.offset = lsn * kCDSectorSizeCDDA;
cd_read.sectorArea = kCDSectorAreaUser;
cd_read.sectorType = kCDSectorTypeCDDA;
cd_read.buffer = p_data;
cd_read.bufferLength = kCDSectorSizeCDDA * i_blocks;
if( ioctl( env->gen.fd, DKIOCCDREAD, &cd_read ) == -1 )
{
cdio_info( "could not read block %d\n%s", lsn,
strerror(errno));
return DRIVER_OP_ERROR;
}
return DRIVER_OP_SUCCESS;
}
/*!
Reads a single mode2 sector from cd device into p_data starting
from lsn. Returns 0 if no error.
*/
static driver_return_code_t
read_mode1_sector_osx (void *p_user_data, void *p_data, lsn_t i_lsn,
bool b_form2)
{
return read_mode1_sectors_osx(p_user_data, p_data, i_lsn, b_form2, 1);
}
/*!
Reads a single mode2 sector from cd device into p_data starting
from lsn. Returns 0 if no error.
*/
static driver_return_code_t
read_mode2_sector_osx (void *p_user_data, void *p_data, lsn_t i_lsn,
bool b_form2)
{
return read_mode2_sectors_osx(p_user_data, p_data, i_lsn, b_form2, 1);
}
/*!
Set the key "arg" to "value" in source device.
*/
static driver_return_code_t
_set_arg_osx (void *p_user_data, const char key[], const char value[])
{
_img_private_t *p_env = p_user_data;
if (!strcmp (key, "source"))
{
if (!value) return DRIVER_OP_ERROR;
free (p_env->gen.source_name);
p_env->gen.source_name = strdup (value);
}
else if (!strcmp (key, "access-mode"))
{
if (!strcmp(value, "OSX"))
p_env->access_mode = _AM_OSX;
else
cdio_warn ("unknown access type: %s. ignored.", value);
}
else return DRIVER_OP_ERROR;
return DRIVER_OP_SUCCESS;
}
#if 0
static void
TestDevice(_img_private_t *p_env, io_service_t service)
{
SInt32 score;
HRESULT herr;
kern_return_t err;
IOCFPlugInInterface **plugInInterface = NULL;
MMCDeviceInterface **mmcInterface = NULL;
/* Create the IOCFPlugIn interface so we can query it. */
err = IOCreatePlugInInterfaceForService ( service,
kIOMMCDeviceUserClientTypeID,
kIOCFPlugInInterfaceID,
&plugInInterface,
&score );
if ( err != noErr ) {
printf("IOCreatePlugInInterfaceForService returned %d\n", err);
return;
}
/* Query the interface for the MMCDeviceInterface. */
herr = ( *plugInInterface )->QueryInterface ( plugInInterface,
CFUUIDGetUUIDBytes ( kIOMMCDeviceInterfaceID ),
( LPVOID ) &mmcInterface );
if ( herr != S_OK ) {
printf("QueryInterface returned %ld\n", herr);
return;
}
p_env->pp_scsiTaskDeviceInterface =
( *mmcInterface )->GetSCSITaskDeviceInterface ( mmcInterface );
if ( NULL == p_env->pp_scsiTaskDeviceInterface ) {
printf("GetSCSITaskDeviceInterface returned NULL\n");
return;
}
( *mmcInterface )->Release ( mmcInterface );
IODestroyPlugInInterface ( plugInInterface );
}
#endif
/*!
Read and cache the CD's Track Table of Contents and track info.
Return false if successful or true if an error.
*/
static bool
read_toc_osx (void *p_user_data)
{
_img_private_t *p_env = p_user_data;
CFDictionaryRef propertiesDict = 0;
CFDataRef data;
/* create a CF dictionary containing the TOC */
propertiesDict = GetRegistryEntryProperties( p_env->MediaClass_service );
if ( 0 == propertiesDict ) {
return false;
}
/* get the TOC from the dictionary */
data = (CFDataRef) CFDictionaryGetValue( propertiesDict,
CFSTR(kIOCDMediaTOCKey) );
if ( data != NULL ) {
CFRange range;
CFIndex buf_len;
buf_len = CFDataGetLength( data ) + 1;
range = CFRangeMake( 0, buf_len );
if( ( p_env->pTOC = (CDTOC *)malloc( buf_len ) ) != NULL ) {
CFDataGetBytes( data, range, (u_char *) p_env->pTOC );
} else {
cdio_warn( "Trouble allocating CDROM TOC" );
return false;
}
} else {
cdio_warn( "Trouble reading TOC" );
return false;
}
/* TestDevice(p_env, service); */
CFRelease( propertiesDict );
p_env->i_descriptors = CDTOCGetDescriptorCount ( p_env->pTOC );
/* Read in starting sectors. There may be non-tracks mixed in with
the real tracks. So find the first and last track number by
scanning. Also find the lead-out track position.
*/
{
int i, i_leadout = -1;
CDTOCDescriptor *pTrackDescriptors;
p_env->pp_lba = malloc( p_env->i_descriptors * sizeof(int) );
if( p_env->pp_lba == NULL )
{
cdio_warn("Out of memory in allocating track starting LSNs" );
free( p_env->pTOC );
return false;
}
pTrackDescriptors = p_env->pTOC->descriptors;
p_env->gen.i_first_track = CDIO_CD_MAX_TRACKS+1;
p_env->i_last_track = CDIO_CD_MIN_TRACK_NO;
p_env->i_first_session = CDIO_CD_MAX_TRACKS+1;
p_env->i_last_session = CDIO_CD_MIN_TRACK_NO;
for( i = 0; i <= p_env->i_descriptors; i++ )
{
track_t i_track = pTrackDescriptors[i].point;
session_t i_session = pTrackDescriptors[i].session;
cdio_debug( "point: %d, tno: %d, session: %d, adr: %d, control:%d, "
"address: %d:%d:%d, p: %d:%d:%d",
i_track,
pTrackDescriptors[i].tno, i_session,
pTrackDescriptors[i].adr, pTrackDescriptors[i].control,
pTrackDescriptors[i].address.minute,
pTrackDescriptors[i].address.second,
pTrackDescriptors[i].address.frame,
pTrackDescriptors[i].p.minute,
pTrackDescriptors[i].p.second,
pTrackDescriptors[i].p.frame );
/* track information has adr = 1 */
if ( 0x01 != pTrackDescriptors[i].adr )
continue;
if( i_track == OSX_CDROM_LEADOUT_TRACK )
i_leadout = i;
if( i_track > CDIO_CD_MAX_TRACKS || i_track < CDIO_CD_MIN_TRACK_NO )
continue;
if (p_env->gen.i_first_track > i_track)
p_env->gen.i_first_track = i_track;
if (p_env->i_last_track < i_track)
p_env->i_last_track = i_track;
if (p_env->i_first_session > i_session)
p_env->i_first_session = i_session;
if (p_env->i_last_session < i_session)
p_env->i_last_session = i_session;
}
/* Now that we know what the first track number is, we can make sure
index positions are ordered starting at 0.
*/
for( i = 0; i <= p_env->i_descriptors; i++ )
{
track_t i_track = pTrackDescriptors[i].point;
if( i_track > CDIO_CD_MAX_TRACKS || i_track < CDIO_CD_MIN_TRACK_NO )
continue;
/* Note what OSX calls a LBA we call an LSN. So below re we
really have have MSF -> LSN -> LBA.
*/
p_env->pp_lba[i_track - p_env->gen.i_first_track] =
cdio_lsn_to_lba(CDConvertMSFToLBA( pTrackDescriptors[i].p ));
set_track_flags(&(p_env->gen.track_flags[i_track]),
pTrackDescriptors[i].control);
}
if( i_leadout == -1 )
{
cdio_warn( "CD leadout not found" );
free( p_env->pp_lba );
free( (void *) p_env->pTOC );
return false;
}
/* Set leadout sector.
Note what OSX calls a LBA we call an LSN. So below re we
really have have MSF -> LSN -> LBA.
*/
p_env->pp_lba[TOTAL_TRACKS] =
cdio_lsn_to_lba(CDConvertMSFToLBA( pTrackDescriptors[i_leadout].p ));
p_env->gen.i_tracks = TOTAL_TRACKS;
}
p_env->gen.toc_init = true;
return( true );
}
/*!
Return the starting LSN track number
i_track in obj. Track numbers start at 1.
The "leadout" track is specified either by
using i_track LEADOUT_TRACK or the total tracks+1.
False is returned if there is no track entry.
*/
static lsn_t
get_track_lba_osx(void *p_user_data, track_t i_track)
{
_img_private_t *p_env = p_user_data;
if (!p_env->gen.toc_init) read_toc_osx (p_env) ;
if (!p_env->gen.toc_init) return CDIO_INVALID_LSN;
if (i_track == CDIO_CDROM_LEADOUT_TRACK) i_track = p_env->i_last_track+1;
if (i_track > p_env->i_last_track + 1 || i_track < p_env->gen.i_first_track) {
return CDIO_INVALID_LSN;
} else {
return p_env->pp_lba[i_track - p_env->gen.i_first_track];
}
}
/*!
Eject media . Return DRIVER_OP_SUCCESS if successful.
The only way to cleanly unmount the disc under MacOS X is to use the
'disktool' command line utility. It uses the non-public Disk
Arbitration API, which can not be used by Cocoa or Carbon
applications.
*/
static driver_return_code_t
_eject_media_osx (void *user_data) {
_img_private_t *p_env = user_data;
FILE *p_file;
char *psz_drive;
char sz_cmd[32];
if( ( psz_drive = (char *)strstr( p_env->gen.source_name, "disk" ) ) != NULL &&
strlen( psz_drive ) > 4 )
{
#define EJECT_CMD "/usr/sbin/hdiutil eject %s"
snprintf( sz_cmd, sizeof(sz_cmd), EJECT_CMD, psz_drive );
#undef EJECT_CMD
if( ( p_file = popen( sz_cmd, "r" ) ) != NULL )
{
char psz_result[0x200];
int i_ret = fread( psz_result, 1, sizeof(psz_result) - 1, p_file );
if( i_ret == 0 && ferror( p_file ) != 0 )
{
pclose( p_file );
return DRIVER_OP_ERROR;
}
pclose( p_file );
psz_result[ i_ret ] = 0;
if( strstr( psz_result, "Disk Ejected" ) != NULL )
{
return DRIVER_OP_SUCCESS;
}
}
}
return DRIVER_OP_ERROR;
}
/*!
Return the size of the CD in logical block address (LBA) units.
*/
static lsn_t
get_disc_last_lsn_osx (void *user_data)
{
return get_track_lba_osx(user_data, CDIO_CDROM_LEADOUT_TRACK);
}
/*!
Return the value associated with the key "arg".
*/
static const char *
_get_arg_osx (void *user_data, const char key[])
{
_img_private_t *p_env = user_data;
if (!strcmp (key, "source")) {
return p_env->gen.source_name;
} else if (!strcmp (key, "access-mode")) {
switch (p_env->access_mode) {
case _AM_OSX:
return "OS X";
case _AM_NONE:
return "no access method";
}
}
return NULL;
}
/*!
Return the media catalog number MCN.
*/
static char *
get_mcn_osx (const void *user_data) {
const _img_private_t *p_env = user_data;
dk_cd_read_mcn_t cd_read;
memset( &cd_read, 0, sizeof(cd_read) );
if( ioctl( p_env->gen.fd, DKIOCCDREADMCN, &cd_read ) < 0 )
{
cdio_debug( "could not read MCN, %s", strerror(errno) );
return NULL;
}
return strdup((char*)cd_read.mcn);
}
/*!
Get format of track.
*/
static track_format_t
get_track_format_osx(void *p_user_data, track_t i_track)
{
_img_private_t *p_env = p_user_data;
dk_cd_read_track_info_t cd_read;
CDTrackInfo a_track;
if (!p_env->gen.toc_init) read_toc_osx (p_env) ;
if (i_track > p_env->i_last_track || i_track < p_env->gen.i_first_track)
return TRACK_FORMAT_ERROR;
memset( &cd_read, 0, sizeof(cd_read) );
cd_read.address = i_track;
cd_read.addressType = kCDTrackInfoAddressTypeTrackNumber;
cd_read.buffer = &a_track;
cd_read.bufferLength = sizeof(CDTrackInfo);
if( ioctl( p_env->gen.fd, DKIOCCDREADTRACKINFO, &cd_read ) == -1 )
{
cdio_warn( "could not read trackinfo for track %d:\n%s", i_track,
strerror(errno));
return TRACK_FORMAT_ERROR;
}
cdio_debug( "%d: trackinfo trackMode: %x dataMode: %x", i_track,
a_track.trackMode, a_track.dataMode );
if (a_track.trackMode == CDIO_CDROM_DATA_TRACK) {
if (a_track.dataMode == CDROM_CDI_TRACK) {
return TRACK_FORMAT_CDI;
} else if (a_track.dataMode == CDROM_XA_TRACK) {
return TRACK_FORMAT_XA;
} else {
return TRACK_FORMAT_DATA;
}
} else {
return TRACK_FORMAT_AUDIO;
}
}
/*!
Return true if we have XA data (green, mode2 form1) or
XA data (green, mode2 form2). That is track begins:
sync - header - subheader
12 4 - 8
FIXME: there's gotta be a better design for this and get_track_format?
*/
static bool
get_track_green_osx(void *p_user_data, track_t i_track)
{
_img_private_t *p_env = p_user_data;
CDTrackInfo a_track;
if (!p_env->gen.toc_init) read_toc_osx (p_env) ;
if ( i_track > p_env->i_last_track || i_track < p_env->gen.i_first_track )
return false;
else {
dk_cd_read_track_info_t cd_read;
memset( &cd_read, 0, sizeof(cd_read) );
cd_read.address = i_track;
cd_read.addressType = kCDTrackInfoAddressTypeTrackNumber;
cd_read.buffer = &a_track;
cd_read.bufferLength = sizeof(CDTrackInfo);
if( ioctl( p_env->gen.fd, DKIOCCDREADTRACKINFO, &cd_read ) == -1 ) {
cdio_warn( "could not read trackinfo for track %d:\n%s", i_track,
strerror(errno));
return false;
}
return ((a_track.trackMode & CDIO_CDROM_DATA_TRACK) != 0);
}
}
/* Set CD-ROM drive speed */
static int
set_speed_osx (void *p_user_data, int i_speed)
{
const _img_private_t *p_env = p_user_data;
if (!p_env) return -1;
return ioctl(p_env->gen.fd, DKIOCCDSETSPEED, i_speed);
}
#endif /* HAVE_DARWIN_CDROM */
/*!
Close tray on CD-ROM.
@param psz_drive the CD-ROM drive to be closed.
*/
/* FIXME: We don't use the device name because we don't how
to.
*/
#define CLOSE_TRAY_CMD "/usr/sbin/drutil tray close"
driver_return_code_t
close_tray_osx (const char *psz_drive)
{
#ifdef HAVE_DARWIN_CDROM
FILE *p_file;
char sz_cmd[80];
if ( !psz_drive) return DRIVER_OP_UNINIT;
/* Right now we really aren't making use of snprintf, but
possibly someday we will.
*/
snprintf( sz_cmd, sizeof(sz_cmd), CLOSE_TRAY_CMD );
if( ( p_file = popen( sz_cmd, "r" ) ) != NULL )
{
char psz_result[0x200];
int i_ret = fread( psz_result, 1, sizeof(psz_result) - 1, p_file );
if( i_ret == 0 && ferror( p_file ) != 0 )
{
pclose( p_file );
return DRIVER_OP_ERROR;
}
pclose( p_file );
psz_result[ i_ret ] = 0;
if( 0 == i_ret )
{
return DRIVER_OP_SUCCESS;
}
}
return DRIVER_OP_ERROR;
#else
return DRIVER_OP_NO_DRIVER;
#endif /*HAVE_DARWIN_CDROM*/
}
/*!
Return a string containing the default CD device if none is specified.
*/
char **
cdio_get_devices_osx(void)
{
#ifndef HAVE_DARWIN_CDROM
return NULL;
#else
io_object_t next_media;
mach_port_t master_port;
kern_return_t kern_result;
io_iterator_t media_iterator;
CFMutableDictionaryRef classes_to_match;
char **drives = NULL;
unsigned int num_drives=0;
kern_result = IOMasterPort( MACH_PORT_NULL, &master_port );
if( kern_result != KERN_SUCCESS )
{
return( NULL );
}
classes_to_match = IOServiceMatching( kIOCDMediaClass );
if( classes_to_match == NULL )
{
return( NULL );
}
CFDictionarySetValue( classes_to_match, CFSTR(kIOMediaEjectableKey),
kCFBooleanTrue );
kern_result = IOServiceGetMatchingServices( master_port,
classes_to_match,
&media_iterator );
if( kern_result != KERN_SUCCESS )
{
return( NULL );
}
next_media = IOIteratorNext( media_iterator );
if( next_media != 0 )
{
char psz_buf[0x32];
size_t dev_path_length;
CFTypeRef str_bsd_path;
do
{
str_bsd_path = IORegistryEntryCreateCFProperty( next_media,
CFSTR( kIOBSDNameKey ),
kCFAllocatorDefault,
0 );
if( str_bsd_path == NULL )
{
IOObjectRelease( next_media );
continue;
}
snprintf( psz_buf, sizeof(psz_buf), "%s%c", _PATH_DEV, 'r' );
dev_path_length = strlen( psz_buf );
if( CFStringGetCString( str_bsd_path,
(char*)&psz_buf + dev_path_length,
sizeof(psz_buf) - dev_path_length,
kCFStringEncodingASCII ) )
{
cdio_add_device_list(&drives, strdup(psz_buf), &num_drives);
}
CFRelease( str_bsd_path );
IOObjectRelease( next_media );
} while( ( next_media = IOIteratorNext( media_iterator ) ) != 0 );
}
IOObjectRelease( media_iterator );
cdio_add_device_list(&drives, NULL, &num_drives);
return drives;
#endif /* HAVE_DARWIN_CDROM */
}
/*!
Return a string containing the default CD device if none is specified.
*/
char *
cdio_get_default_device_osx(void)
{
#ifndef HAVE_DARWIN_CDROM
return NULL;
#else
io_object_t next_media;
mach_port_t master_port;
kern_return_t kern_result;
io_iterator_t media_iterator;
CFMutableDictionaryRef classes_to_match;
kern_result = IOMasterPort( MACH_PORT_NULL, &master_port );
if( kern_result != KERN_SUCCESS )
{
return( NULL );
}
classes_to_match = IOServiceMatching( kIOCDMediaClass );
if( classes_to_match == NULL )
{
return( NULL );
}
CFDictionarySetValue( classes_to_match, CFSTR(kIOMediaEjectableKey),
kCFBooleanTrue );
kern_result = IOServiceGetMatchingServices( master_port,
classes_to_match,
&media_iterator );
if( kern_result != KERN_SUCCESS )
{
return( NULL );
}
next_media = IOIteratorNext( media_iterator );
if( next_media != 0 )
{
char psz_buf[0x32];
size_t dev_path_length;
CFTypeRef str_bsd_path;
do
{
str_bsd_path = IORegistryEntryCreateCFProperty( next_media,
CFSTR( kIOBSDNameKey ),
kCFAllocatorDefault,
0 );
if( str_bsd_path == NULL )
{
IOObjectRelease( next_media );
continue;
}
snprintf( psz_buf, sizeof(psz_buf), "%s%c", _PATH_DEV, 'r' );
dev_path_length = strlen( psz_buf );
if( CFStringGetCString( str_bsd_path,
(char*)&psz_buf + dev_path_length,
sizeof(psz_buf) - dev_path_length,
kCFStringEncodingASCII ) )
{
CFRelease( str_bsd_path );
IOObjectRelease( next_media );
IOObjectRelease( media_iterator );
return strdup( psz_buf );
}
CFRelease( str_bsd_path );
IOObjectRelease( next_media );
} while( ( next_media = IOIteratorNext( media_iterator ) ) != 0 );
}
IOObjectRelease( media_iterator );
return NULL;
#endif /* HAVE_DARWIN_CDROM */
}
/*!
Initialization routine. This is the only thing that doesn't
get called via a function pointer. In fact *we* are the
ones to set that up.
*/
CdIo_t *
cdio_open_am_osx (const char *psz_source_name, const char *psz_access_mode)
{
if (psz_access_mode != NULL)
cdio_warn ("there is only one access mode for OS X. Arg %s ignored",
psz_access_mode);
return cdio_open_osx(psz_source_name);
}
/*!
Initialization routine. This is the only thing that doesn't
get called via a function pointer. In fact *we* are the
ones to set that up.
*/
CdIo_t *
cdio_open_osx (const char *psz_orig_source)
{
#ifdef HAVE_DARWIN_CDROM
CdIo_t *ret;
_img_private_t *_data;
char *psz_source;
cdio_funcs_t _funcs = {
.eject_media = _eject_media_osx,
.free = _free_osx,
.get_arg = _get_arg_osx,
.get_cdtext = get_cdtext_osx,
.get_default_device = cdio_get_default_device_osx,
.get_devices = cdio_get_devices_osx,
.get_disc_last_lsn = get_disc_last_lsn_osx,
.get_discmode = get_discmode_osx,
.get_drive_cap = get_drive_cap_osx,
.get_first_track_num = get_first_track_num_generic,
.get_hwinfo = get_hwinfo_osx,
.get_mcn = get_mcn_osx,
.get_num_tracks = get_num_tracks_generic,
.get_track_channels = get_track_channels_generic,
.get_track_copy_permit = get_track_copy_permit_generic,
.get_track_format = get_track_format_osx,
.get_track_green = get_track_green_osx,
.get_track_lba = get_track_lba_osx,
.get_track_msf = NULL,
.get_track_preemphasis = get_track_preemphasis_generic,
.lseek = cdio_generic_lseek,
.read = cdio_generic_read,
.read_audio_sectors = read_audio_sectors_osx,
.read_data_sectors = read_data_sectors_osx,
.read_mode1_sector = read_mode1_sector_osx,
.read_mode1_sectors = read_mode1_sectors_osx,
.read_mode2_sector = read_mode2_sector_osx,
.read_mode2_sectors = read_mode2_sectors_osx,
.read_toc = read_toc_osx,
.run_mmc_cmd = run_mmc_cmd_osx,
.set_arg = _set_arg_osx,
.set_speed = set_speed_osx,
};
_data = calloc (1, sizeof (_img_private_t));
_data->access_mode = _AM_OSX;
_data->MediaClass_service = 0;
_data->gen.init = false;
_data->gen.fd = -1;
_data->gen.toc_init = false;
_data->gen.b_cdtext_init = false;
_data->gen.b_cdtext_error = false;
if (NULL == psz_orig_source) {
psz_source=cdio_get_default_device_osx();
if (NULL == psz_source) return NULL;
_set_arg_osx(_data, "source", psz_source);
free(psz_source);
} else {
if (cdio_is_device_generic(psz_orig_source))
_set_arg_osx(_data, "source", psz_orig_source);
else {
/* The below would be okay if all device drivers worked this way. */
#if 0
cdio_info ("source %s is a not a device", psz_orig_source);
#endif
return NULL;
}
}
ret = cdio_new ((void *)_data, &_funcs);
if (ret == NULL) return NULL;
ret->driver_id = DRIVER_OSX;
if (cdio_generic_init(_data, O_RDONLY) && init_osx(_data))
return ret;
else {
cdio_generic_free (_data);
return NULL;
}
#else
return NULL;
#endif /* HAVE_DARWIN_CDROM */
}
bool
cdio_have_osx (void)
{
#ifdef HAVE_DARWIN_CDROM
return true;
#else
return false;
#endif /* HAVE_DARWIN_CDROM */
}