diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..d159169 --- /dev/null +++ b/LICENSE @@ -0,0 +1,339 @@ + GNU GENERAL PUBLIC LICENSE + Version 2, June 1991 + + Copyright (C) 1989, 1991 Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +License is intended to guarantee your freedom to share and change free +software--to make sure the software is free for all its users. This +General Public License applies to most of the Free Software +Foundation's software and to any other program whose authors commit to +using it. (Some other Free Software Foundation software is covered by +the GNU Lesser General Public License instead.) You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +this service if you wish), that you receive source code or can get it +if you want it, that you can change the software or use pieces of it +in new free programs; and that you know you can do these things. + + To protect your rights, we need to make restrictions that forbid +anyone to deny you these rights or to ask you to surrender the rights. +These restrictions translate to certain responsibilities for you if you +distribute copies of the software, or if you modify it. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must give the recipients all the rights that +you have. You must make sure that they, too, receive or can get the +source code. And you must show them these terms so they know their +rights. + + We protect your rights with two steps: (1) copyright the software, and +(2) offer you this license which gives you legal permission to copy, +distribute and/or modify the software. + + Also, for each author's protection and ours, we want to make certain +that everyone understands that there is no warranty for this free +software. If the software is modified by someone else and passed on, we +want its recipients to know that what they have is not the original, so +that any problems introduced by others will not reflect on the original +authors' reputations. + + Finally, any free program is threatened constantly by software +patents. We wish to avoid the danger that redistributors of a free +program will individually obtain patent licenses, in effect making the +program proprietary. To prevent this, we have made it clear that any +patent must be licensed for everyone's free use or not licensed at all. + + The precise terms and conditions for copying, distribution and +modification follow. + + GNU GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License applies to any program or other work which contains +a notice placed by the copyright holder saying it may be distributed +under the terms of this General Public License. The "Program", below, +refers to any such program or work, and a "work based on the Program" +means either the Program or any derivative work under copyright law: +that is to say, a work containing the Program or a portion of it, +either verbatim or with modifications and/or translated into another +language. (Hereinafter, translation is included without limitation in +the term "modification".) Each licensee is addressed as "you". + +Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running the Program is not restricted, and the output from the Program +is covered only if its contents constitute a work based on the +Program (independent of having been made by running the Program). +Whether that is true depends on what the Program does. + + 1. You may copy and distribute verbatim copies of the Program's +source code as you receive it, in any medium, provided that you +conspicuously and appropriately publish on each copy an appropriate +copyright notice and disclaimer of warranty; keep intact all the +notices that refer to this License and to the absence of any warranty; +and give any other recipients of the Program a copy of this License +along with the Program. + +You may charge a fee for the physical act of transferring a copy, and +you may at your option offer warranty protection in exchange for a fee. + + 2. You may modify your copy or copies of the Program or any portion +of it, thus forming a work based on the Program, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) You must cause the modified files to carry prominent notices + stating that you changed the files and the date of any change. + + b) You must cause any work that you distribute or publish, that in + whole or in part contains or is derived from the Program or any + part thereof, to be licensed as a whole at no charge to all third + parties under the terms of this License. + + c) If the modified program normally reads commands interactively + when run, you must cause it, when started running for such + interactive use in the most ordinary way, to print or display an + announcement including an appropriate copyright notice and a + notice that there is no warranty (or else, saying that you provide + a warranty) and that users may redistribute the program under + these conditions, and telling the user how to view a copy of this + License. (Exception: if the Program itself is interactive but + does not normally print such an announcement, your work based on + the Program is not required to print an announcement.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Program, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Program, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Program. + +In addition, mere aggregation of another work not based on the Program +with the Program (or with a work based on the Program) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may copy and distribute the Program (or a work based on it, +under Section 2) in object code or executable form under the terms of +Sections 1 and 2 above provided that you also do one of the following: + + a) Accompany it with the complete corresponding machine-readable + source code, which must be distributed under the terms of Sections + 1 and 2 above on a medium customarily used for software interchange; or, + + b) Accompany it with a written offer, valid for at least three + years, to give any third party, for a charge no more than your + cost of physically performing source distribution, a complete + machine-readable copy of the corresponding source code, to be + distributed under the terms of Sections 1 and 2 above on a medium + customarily used for software interchange; or, + + c) Accompany it with the information you received as to the offer + to distribute corresponding source code. (This alternative is + allowed only for noncommercial distribution and only if you + received the program in object code or executable form with such + an offer, in accord with Subsection b above.) + +The source code for a work means the preferred form of the work for +making modifications to it. For an executable work, complete source +code means all the source code for all modules it contains, plus any +associated interface definition files, plus the scripts used to +control compilation and installation of the executable. However, as a +special exception, the source code distributed need not include +anything that is normally distributed (in either source or binary +form) with the major components (compiler, kernel, and so on) of the +operating system on which the executable runs, unless that component +itself accompanies the executable. + +If distribution of executable or object code is made by offering +access to copy from a designated place, then offering equivalent +access to copy the source code from the same place counts as +distribution of the source code, even though third parties are not +compelled to copy the source along with the object code. + + 4. You may not copy, modify, sublicense, or distribute the Program +except as expressly provided under this License. Any attempt +otherwise to copy, modify, sublicense or distribute the Program is +void, and will automatically terminate your rights under this License. +However, parties who have received copies, or rights, from you under +this License will not have their licenses terminated so long as such +parties remain in full compliance. + + 5. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Program or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Program (or any work based on the +Program), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Program or works based on it. + + 6. Each time you redistribute the Program (or any work based on the +Program), the recipient automatically receives a license from the +original licensor to copy, distribute or modify the Program subject to +these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties to +this License. + + 7. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Program at all. For example, if a patent +license would not permit royalty-free redistribution of the Program by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Program. + +If any portion of this section is held invalid or unenforceable under +any particular circumstance, the balance of the section is intended to +apply and the section as a whole is intended to apply in other +circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system, which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 8. If the distribution and/or use of the Program is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Program under this License +may add an explicit geographical distribution limitation excluding +those countries, so that distribution is permitted only in or among +countries not thus excluded. In such case, this License incorporates +the limitation as if written in the body of this License. + + 9. The Free Software Foundation may publish revised and/or new versions +of the General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + +Each version is given a distinguishing version number. If the Program +specifies a version number of this License which applies to it and "any +later version", you have the option of following the terms and conditions +either of that version or of any later version published by the Free +Software Foundation. If the Program does not specify a version number of +this License, you may choose any version ever published by the Free Software +Foundation. + + 10. If you wish to incorporate parts of the Program into other free +programs whose distribution conditions are different, write to the author +to ask for permission. For software which is copyrighted by the Free +Software Foundation, write to the Free Software Foundation; we sometimes +make exceptions for this. Our decision will be guided by the two goals +of preserving the free status of all derivatives of our free software and +of promoting the sharing and reuse of software generally. + + NO WARRANTY + + 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY +FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN +OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES +PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED +OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS +TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE +PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, +REPAIR OR CORRECTION. + + 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR +REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, +INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING +OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED +TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY +YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER +PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE +POSSIBILITY OF SUCH DAMAGES. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +convey the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + 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., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +Also add information on how to contact you by electronic and paper mail. + +If the program is interactive, make it output a short notice like this +when it starts in an interactive mode: + + Gnomovision version 69, Copyright (C) year name of author + Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, the commands you use may +be called something other than `show w' and `show c'; they could even be +mouse-clicks or menu items--whatever suits your program. + +You should also get your employer (if you work as a programmer) or your +school, if any, to sign a "copyright disclaimer" for the program, if +necessary. Here is a sample; alter the names: + + Yoyodyne, Inc., hereby disclaims all copyright interest in the program + `Gnomovision' (which makes passes at compilers) written by James Hacker. + + , 1 April 1989 + Ty Coon, President of Vice + +This General Public License does not permit incorporating your program into +proprietary programs. If your program is a subroutine library, you may +consider it more useful to permit linking proprietary applications with the +library. If this is what you want to do, use the GNU Lesser General +Public License instead of this License. diff --git a/README.md b/README.md new file mode 100644 index 0000000..b466a35 --- /dev/null +++ b/README.md @@ -0,0 +1,31 @@ +##TOSMotionSensor +TOSMotionSensor is an accelerometer driver for Toshiba hackintosh devices. It has been designed to use the built-in accelerometer in any Toshiba device sporting one and should be compatible with any supported device. + +TOSMotionSensor also kinda-sorta emulates the built in accelerometer HDD MacBook and MacBook Pros used to come with by publishing the same IOService and can be queried by user-space applications in the same way. + +tldr: This is compatible with native macOS applications that use SMS (SMCMotionSensor/Sudden Motion Sensor) data. No proprietary software/patches/hacks necessary. + +**NOTE: This driver does NOT offer HDD protection. A lot more testing and research needs to be done in order for that to happen.** I did not include this in the first release as I don't feel this is as important as it used to be. Most of us are using solid state storage these days. Would still be cool to get working though. + +## Usage +I have tested this kext on High Sierra and Mojave in Clover's 'kexts/Other', SLE, and LE. All seem to be working. + +If you do not want to build it yourself you can **[Download the latest release here](https://github.com/jslegendre/TOSMotionSensor)** + +## Development +If you would like to get started developing motion-based applications you can check out [SMSLib](http://suitable.com/smslib.html), a library made especially for grabbing accelerometer data on Apple hardware or check out my own display autorotation app [Displace](https://github.com/jslegendre/Displace) for a real-world example. + +## Future +I am really excited about iOS apps (supposedly) coming to macOS and the possibilities this opens up. Possible uses include: + +- Gaming. Playing motion-based iOS games "natively" on a bigger screen. +- Testing for iOS motion-based app developers. + +## Contributing +I am very open to pull requests. This is my first kext release and I'm sure that I have some areas to improve upon. + +- Development. Check out the source, there are comments, questions, and todos all over the place. +- Testing. If you have a Toshiba device with an accelerometer, please try this out alongside [Displace](https://github.com/jslegendre/Displace) and let me know how it worked for you. +- Financially. Yeeeeessshh, okay, this is where I get uncomfortable. This and other projects take time and something I've come to learn since having a child is that time isn't free. Especially (and ironically) free time. The only thing I have up right now is a [Patreon](https://www.patreon.com/jslegendre) account if you are interested. \*cue Sarah McLachlan* Even $1/month is a huge help and allows me to dedicate more time to Hackintosh projects as well as access to more devices to make drivers for. + +I cannot thank you enough for even reading this far let alone helping out in any way you can. \ No newline at end of file diff --git a/TOSMotionSensor.xcodeproj/project.pbxproj b/TOSMotionSensor.xcodeproj/project.pbxproj new file mode 100644 index 0000000..3d4da41 --- /dev/null +++ b/TOSMotionSensor.xcodeproj/project.pbxproj @@ -0,0 +1,335 @@ +// !$*UTF8*$! +{ + archiveVersion = 1; + classes = { + }; + objectVersion = 50; + objects = { + +/* Begin PBXBuildFile section */ + 090B57D42293B9BE00879B1B /* TOSMotionSensor.hpp in Headers */ = {isa = PBXBuildFile; fileRef = 090B57D32293B9BE00879B1B /* TOSMotionSensor.hpp */; }; + 090B57D62293B9BE00879B1B /* TOSMotionSensor.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 090B57D52293B9BE00879B1B /* TOSMotionSensor.cpp */; }; + 09288B22229656F10061881A /* TOSMotionSensorClient.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 09288B21229656F10061881A /* TOSMotionSensorClient.cpp */; }; + 09288B2522965BAA0061881A /* ToshibaHAPS.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 09288B2322965BAA0061881A /* ToshibaHAPS.cpp */; }; + 09288B2622965BAA0061881A /* ToshibaHAPS.hpp in Headers */ = {isa = PBXBuildFile; fileRef = 09288B2422965BAA0061881A /* ToshibaHAPS.hpp */; }; +/* End PBXBuildFile section */ + +/* Begin PBXFileReference section */ + 090B57D02293B9BE00879B1B /* TOSMotionSensor.kext */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = TOSMotionSensor.kext; sourceTree = BUILT_PRODUCTS_DIR; }; + 090B57D32293B9BE00879B1B /* TOSMotionSensor.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = TOSMotionSensor.hpp; sourceTree = ""; }; + 090B57D52293B9BE00879B1B /* TOSMotionSensor.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = TOSMotionSensor.cpp; sourceTree = ""; }; + 090B57D72293B9BE00879B1B /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; + 09288B21229656F10061881A /* TOSMotionSensorClient.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = TOSMotionSensorClient.cpp; sourceTree = ""; }; + 09288B2322965BAA0061881A /* ToshibaHAPS.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = ToshibaHAPS.cpp; sourceTree = ""; }; + 09288B2422965BAA0061881A /* ToshibaHAPS.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = ToshibaHAPS.hpp; sourceTree = ""; }; +/* End PBXFileReference section */ + +/* Begin PBXFrameworksBuildPhase section */ + 090B57CC2293B9BE00879B1B /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXFrameworksBuildPhase section */ + +/* Begin PBXGroup section */ + 090B57C62293B9BE00879B1B = { + isa = PBXGroup; + children = ( + 090B57D22293B9BE00879B1B /* TOSMotionSensor */, + 090B57D12293B9BE00879B1B /* Products */, + ); + sourceTree = ""; + }; + 090B57D12293B9BE00879B1B /* Products */ = { + isa = PBXGroup; + children = ( + 090B57D02293B9BE00879B1B /* TOSMotionSensor.kext */, + ); + name = Products; + sourceTree = ""; + }; + 090B57D22293B9BE00879B1B /* TOSMotionSensor */ = { + isa = PBXGroup; + children = ( + 09288B2422965BAA0061881A /* ToshibaHAPS.hpp */, + 09288B2322965BAA0061881A /* ToshibaHAPS.cpp */, + 090B57D32293B9BE00879B1B /* TOSMotionSensor.hpp */, + 090B57D52293B9BE00879B1B /* TOSMotionSensor.cpp */, + 09288B21229656F10061881A /* TOSMotionSensorClient.cpp */, + 090B57D72293B9BE00879B1B /* Info.plist */, + ); + path = TOSMotionSensor; + sourceTree = ""; + }; +/* End PBXGroup section */ + +/* Begin PBXHeadersBuildPhase section */ + 090B57CD2293B9BE00879B1B /* Headers */ = { + isa = PBXHeadersBuildPhase; + buildActionMask = 2147483647; + files = ( + 090B57D42293B9BE00879B1B /* TOSMotionSensor.hpp in Headers */, + 09288B2622965BAA0061881A /* ToshibaHAPS.hpp in Headers */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXHeadersBuildPhase section */ + +/* Begin PBXNativeTarget section */ + 090B57CF2293B9BE00879B1B /* TOSMotionSensor */ = { + isa = PBXNativeTarget; + buildConfigurationList = 090B57DA2293B9BE00879B1B /* Build configuration list for PBXNativeTarget "TOSMotionSensor" */; + buildPhases = ( + 090B57CB2293B9BE00879B1B /* Sources */, + 090B57CC2293B9BE00879B1B /* Frameworks */, + 090B57CD2293B9BE00879B1B /* Headers */, + 090B57CE2293B9BE00879B1B /* Resources */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = TOSMotionSensor; + productName = TOSMotionSensor; + productReference = 090B57D02293B9BE00879B1B /* TOSMotionSensor.kext */; + productType = "com.apple.product-type.kernel-extension"; + }; +/* End PBXNativeTarget section */ + +/* Begin PBXProject section */ + 090B57C72293B9BE00879B1B /* Project object */ = { + isa = PBXProject; + attributes = { + LastUpgradeCheck = 0940; + ORGANIZATIONNAME = j; + TargetAttributes = { + 090B57CF2293B9BE00879B1B = { + CreatedOnToolsVersion = 9.4; + }; + }; + }; + buildConfigurationList = 090B57CA2293B9BE00879B1B /* Build configuration list for PBXProject "TOSMotionSensor" */; + compatibilityVersion = "Xcode 9.3"; + developmentRegion = en; + hasScannedForEncodings = 0; + knownRegions = ( + en, + Base, + ); + mainGroup = 090B57C62293B9BE00879B1B; + productRefGroup = 090B57D12293B9BE00879B1B /* Products */; + projectDirPath = ""; + projectRoot = ""; + targets = ( + 090B57CF2293B9BE00879B1B /* TOSMotionSensor */, + ); + }; +/* End PBXProject section */ + +/* Begin PBXResourcesBuildPhase section */ + 090B57CE2293B9BE00879B1B /* Resources */ = { + isa = PBXResourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXResourcesBuildPhase section */ + +/* Begin PBXSourcesBuildPhase section */ + 090B57CB2293B9BE00879B1B /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + 090B57D62293B9BE00879B1B /* TOSMotionSensor.cpp in Sources */, + 09288B2522965BAA0061881A /* ToshibaHAPS.cpp in Sources */, + 09288B22229656F10061881A /* TOSMotionSensorClient.cpp in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXSourcesBuildPhase section */ + +/* Begin XCBuildConfiguration section */ + 090B57D82293B9BE00879B1B /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_ANALYZER_NONNULL = YES; + CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_ENABLE_OBJC_WEAK = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_DOCUMENTATION_COMMENTS = YES; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; + CLANG_WARN_SUSPICIOUS_MOVE = YES; + CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + CODE_SIGN_IDENTITY = "-"; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = dwarf; + ENABLE_STRICT_OBJC_MSGSEND = YES; + ENABLE_TESTABILITY = YES; + GCC_C_LANGUAGE_STANDARD = gnu11; + GCC_DYNAMIC_NO_PIC = NO; + GCC_NO_COMMON_BLOCKS = YES; + GCC_OPTIMIZATION_LEVEL = 0; + GCC_PREPROCESSOR_DEFINITIONS = ( + "DEBUG=1", + "$(inherited)", + ); + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + MACOSX_DEPLOYMENT_TARGET = 10.14; + MTL_ENABLE_DEBUG_INFO = YES; + ONLY_ACTIVE_ARCH = YES; + SDKROOT = macosx; + }; + name = Debug; + }; + 090B57D92293B9BE00879B1B /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_ANALYZER_NONNULL = YES; + CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_ENABLE_OBJC_WEAK = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_DOCUMENTATION_COMMENTS = YES; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; + CLANG_WARN_SUSPICIOUS_MOVE = YES; + CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + CODE_SIGN_IDENTITY = "-"; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; + ENABLE_NS_ASSERTIONS = NO; + ENABLE_STRICT_OBJC_MSGSEND = YES; + GCC_C_LANGUAGE_STANDARD = gnu11; + GCC_NO_COMMON_BLOCKS = YES; + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + MACOSX_DEPLOYMENT_TARGET = 10.14; + MTL_ENABLE_DEBUG_INFO = NO; + SDKROOT = macosx; + }; + name = Release; + }; + 090B57DB2293B9BE00879B1B /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + CODE_SIGN_IDENTITY = ""; + CODE_SIGN_STYLE = Manual; + COMBINE_HIDPI_IMAGES = YES; + CURRENT_PROJECT_VERSION = 1.0.0d1; + DEVELOPMENT_TEAM = ""; + INFOPLIST_FILE = TOSMotionSensor/Info.plist; + MACOSX_DEPLOYMENT_TARGET = 10.14; + MODULE_NAME = com.github.jslegendre.TOSMotionSensor; + MODULE_VERSION = 1.0.0d1; + OTHER_CPLUSPLUSFLAGS = ( + "$(OTHER_CFLAGS)", + "-Wno-inconsistent-missing-override", + ); + PRODUCT_BUNDLE_IDENTIFIER = com.github.jslegendre.TOSMotionSensor; + PRODUCT_NAME = "$(TARGET_NAME)"; + PROVISIONING_PROFILE_SPECIFIER = ""; + WRAPPER_EXTENSION = kext; + }; + name = Debug; + }; + 090B57DC2293B9BE00879B1B /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + CODE_SIGN_IDENTITY = ""; + CODE_SIGN_STYLE = Manual; + COMBINE_HIDPI_IMAGES = YES; + CURRENT_PROJECT_VERSION = 1.0.0d1; + DEVELOPMENT_TEAM = ""; + INFOPLIST_FILE = TOSMotionSensor/Info.plist; + MACOSX_DEPLOYMENT_TARGET = 10.14; + MODULE_NAME = com.github.jslegendre.TOSMotionSensor; + MODULE_VERSION = 1.0.0d1; + OTHER_CPLUSPLUSFLAGS = ( + "$(OTHER_CFLAGS)", + "-Wno-inconsistent-missing-override", + ); + PRODUCT_BUNDLE_IDENTIFIER = com.github.jslegendre.TOSMotionSensor; + PRODUCT_NAME = "$(TARGET_NAME)"; + PROVISIONING_PROFILE_SPECIFIER = ""; + WRAPPER_EXTENSION = kext; + }; + name = Release; + }; +/* End XCBuildConfiguration section */ + +/* Begin XCConfigurationList section */ + 090B57CA2293B9BE00879B1B /* Build configuration list for PBXProject "TOSMotionSensor" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 090B57D82293B9BE00879B1B /* Debug */, + 090B57D92293B9BE00879B1B /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; + 090B57DA2293B9BE00879B1B /* Build configuration list for PBXNativeTarget "TOSMotionSensor" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 090B57DB2293B9BE00879B1B /* Debug */, + 090B57DC2293B9BE00879B1B /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; +/* End XCConfigurationList section */ + }; + rootObject = 090B57C72293B9BE00879B1B /* Project object */; +} diff --git a/TOSMotionSensor.xcodeproj/project.xcworkspace/contents.xcworkspacedata b/TOSMotionSensor.xcodeproj/project.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..0b9aa04 --- /dev/null +++ b/TOSMotionSensor.xcodeproj/project.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/TOSMotionSensor.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist b/TOSMotionSensor.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist new file mode 100644 index 0000000..18d9810 --- /dev/null +++ b/TOSMotionSensor.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist @@ -0,0 +1,8 @@ + + + + + IDEDidComputeMac32BitWarning + + + diff --git a/TOSMotionSensor.xcodeproj/project.xcworkspace/xcuserdata/j.xcuserdatad/UserInterfaceState.xcuserstate b/TOSMotionSensor.xcodeproj/project.xcworkspace/xcuserdata/j.xcuserdatad/UserInterfaceState.xcuserstate new file mode 100644 index 0000000..a50519c Binary files /dev/null and b/TOSMotionSensor.xcodeproj/project.xcworkspace/xcuserdata/j.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/TOSMotionSensor.xcodeproj/project.xcworkspace/xcuserdata/xord.xcuserdatad/UserInterfaceState.xcuserstate b/TOSMotionSensor.xcodeproj/project.xcworkspace/xcuserdata/xord.xcuserdatad/UserInterfaceState.xcuserstate new file mode 100644 index 0000000..1121089 Binary files /dev/null and b/TOSMotionSensor.xcodeproj/project.xcworkspace/xcuserdata/xord.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/TOSMotionSensor.xcodeproj/xcshareddata/xcschemes/TOSMotionSensor.xcscheme b/TOSMotionSensor.xcodeproj/xcshareddata/xcschemes/TOSMotionSensor.xcscheme new file mode 100644 index 0000000..df6e9ee --- /dev/null +++ b/TOSMotionSensor.xcodeproj/xcshareddata/xcschemes/TOSMotionSensor.xcscheme @@ -0,0 +1,80 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/TOSMotionSensor.xcodeproj/xcuserdata/j.xcuserdatad/xcschemes/TOSMotionSensor.xcscheme b/TOSMotionSensor.xcodeproj/xcuserdata/j.xcuserdatad/xcschemes/TOSMotionSensor.xcscheme new file mode 100644 index 0000000..6ffdbaf --- /dev/null +++ b/TOSMotionSensor.xcodeproj/xcuserdata/j.xcuserdatad/xcschemes/TOSMotionSensor.xcscheme @@ -0,0 +1,80 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/TOSMotionSensor.xcodeproj/xcuserdata/j.xcuserdatad/xcschemes/xcschememanagement.plist b/TOSMotionSensor.xcodeproj/xcuserdata/j.xcuserdatad/xcschemes/xcschememanagement.plist new file mode 100644 index 0000000..8aab346 --- /dev/null +++ b/TOSMotionSensor.xcodeproj/xcuserdata/j.xcuserdatad/xcschemes/xcschememanagement.plist @@ -0,0 +1,22 @@ + + + + + SchemeUserState + + TOSMotionSensor.xcscheme + + orderHint + 0 + + + SuppressBuildableAutocreation + + 090B57CF2293B9BE00879B1B + + primary + + + + + diff --git a/TOSMotionSensor.xcodeproj/xcuserdata/xord.xcuserdatad/xcschemes/xcschememanagement.plist b/TOSMotionSensor.xcodeproj/xcuserdata/xord.xcuserdatad/xcschemes/xcschememanagement.plist new file mode 100644 index 0000000..d8b900a --- /dev/null +++ b/TOSMotionSensor.xcodeproj/xcuserdata/xord.xcuserdatad/xcschemes/xcschememanagement.plist @@ -0,0 +1,22 @@ + + + + + SchemeUserState + + TOSMotionSensor.xcscheme_^#shared#^_ + + orderHint + 0 + + + SuppressBuildableAutocreation + + 090B57CF2293B9BE00879B1B + + primary + + + + + diff --git a/TOSMotionSensor/Info.plist b/TOSMotionSensor/Info.plist new file mode 100644 index 0000000..1608caa --- /dev/null +++ b/TOSMotionSensor/Info.plist @@ -0,0 +1,60 @@ + + + + + CFBundleDevelopmentRegion + $(DEVELOPMENT_LANGUAGE) + CFBundleExecutable + $(EXECUTABLE_NAME) + CFBundleIdentifier + $(PRODUCT_BUNDLE_IDENTIFIER) + CFBundleInfoDictionaryVersion + 6.0 + CFBundleName + $(PRODUCT_NAME) + CFBundlePackageType + KEXT + CFBundleShortVersionString + 1.0 + CFBundleVersion + 1 + IOKitPersonalities + + HCI Method Interface + + CFBundleIdentifier + $(PRODUCT_BUNDLE_IDENTIFIER) + IOClass + SMCMotionSensor + IONameMatch + TOS1900 + IOProviderClass + IOACPIPlatformDevice + IOUserClientClass + SMCMotionSensorClient + + HD Protection Scheme + + CFBundleIdentifier + $(PRODUCT_BUNDLE_IDENTIFIER) + IOClass + ToshibaHAPS + IONameMatch + TOS620A + IOProviderClass + IOACPIPlatformDevice + + + NSHumanReadableCopyright + Copyright © 2019 jslegendre. All rights reserved. + OSBundleLibraries + + com.apple.iokit.IOACPIFamily + 1.0d1 + com.apple.kpi.iokit + 9.0.0 + com.apple.kpi.libkern + 9.0.0 + + + diff --git a/TOSMotionSensor/TOSMotionSensor.cpp b/TOSMotionSensor/TOSMotionSensor.cpp new file mode 100644 index 0000000..77f98ed --- /dev/null +++ b/TOSMotionSensor/TOSMotionSensor.cpp @@ -0,0 +1,185 @@ +/* + * Copyright (c) 2019 jslegendre / Xord. All rights reserved. + * + * Released under "The GNU General Public License (GPL-2.0)" + * + * 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 + * + */ + +/* + Credits: + https://github.com/torvalds/linux/blob/master/drivers/platform/x86/toshiba_acpi.c + */ + +#include "TOSMotionSensor.hpp" + +#ifdef DEBUG +#define DPRINT(fmt, ...) \ +IOLog("TOSMotionSensor::%s:%d(): " fmt, __func__, __LINE__, ##__VA_ARGS__); +#else +#define DPRINT(fmt, ...) do {} while (0) +#endif + +#define HCI_GET 0xfe00 +#define HCI_ACCEL 0x6d +#define HCI_ACCEL_SUPPORT 0xa6 +#define HCI_ACCEL_MASK 0x7fff +#define HCI_ACCEL_DIRECTION_MASK 0x8000 +#define HCI_ACCEL_Y_SHIFT 0x10 +#define HCI_UNSUPPORTED 0x8000 +#define HCI_SUCCESSS 0x0 + +UInt32 getUnsignedIntFromArray(OSArray * array, UInt8 index); + +#define super IOService +OSDefineMetaClassAndStructors(SMCMotionSensor, IOService) + +bool TOSMotionSensor::init(OSDictionary *dict) +{ + DPRINT("Initializing\n"); + + bool result = super::init(dict); + TVAP = NULL; + hci_method = NULL; + _axes = { 0, 0, 0 }; + HCI_OUT = NULL; + axes = NULL; + + for(UInt8 i = 0; i < 6; i++) { + HCI_IN[i] = OSNumber::withNumber((unsigned long long)0, 32); + } + + return result; +} + +const OSSymbol * TOSMotionSensor::findHCI() { + if(!TVAP->validateObject("SPFC")) { + DPRINT("Found HCI method SPFC"); + return OSSymbol::withCString("SPFC"); + } + + if(!TVAP->validateObject("GHCI")) { + DPRINT("Found HCI method GHCI"); + return OSSymbol::withCString("GHCI"); + } + + return NULL; +} + +bool TOSMotionSensor::isAccelSupported() { + HCI_IN[0] = OSDynamicCast(OSObject, OSNumber::withNumber(HCI_GET, 32)); + HCI_IN[1] = OSDynamicCast(OSObject, OSNumber::withNumber(HCI_ACCEL_SUPPORT, 32)); + + TVAP->evaluateObject(hci_method, &HCI_OUT, HCI_IN, 6); + + if(HCI_OUT && (OSTypeIDInst(HCI_OUT) == OSTypeID(OSArray))) { + if(getUnsignedIntFromArray(OSDynamicCast(OSArray, HCI_OUT), 0) != HCI_UNSUPPORTED) { + DPRINT("Accelerometer supported"); + return true; + } + } + + return false; +} + +bool TOSMotionSensor::start(IOService *provider) +{ + DPRINT("TOSMotionSensor::start: called\n"); + + TVAP = OSDynamicCast(IOACPIPlatformDevice, provider); + if (NULL == TVAP || !super::start(provider)) + return false; + + /* + * Make sure the accelerometer is activated + * Sidenote: Not sure if SSD only Toshiba machines have this device. If they do + * not, how to make sure devices shipped with HDD have activated the sensor hub + * before continuing? + */ + IOService *haps_service = waitForMatchingService(serviceMatching("ToshibaHAPS")); + if(!haps_service->getProperty("ProtectionLevel")) { + DPRINT("Accelerometer not activated"); + haps_service->release(); + return false; + } + haps_service->release(); + + hci_method = findHCI(); + if(!hci_method) { + DPRINT("Could not find HCI method"); + return false; + } + + if(!isAccelSupported()) { + DPRINT("Accelerometer not supported"); + return false; + } + + //Setup for HCI method to get accelerometer values as default + HCI_IN[1] = OSNumber::withNumber(HCI_ACCEL, 32); + HCI_IN[3] = OSNumber::withNumber(0x01, 32); + + registerService(); + return true; +} + +//Should this be locking? Specifically access to _axes? +IOReturn TOSMotionSensor::getAxes(void *iBuf, void *oBuf) { + DPRINT("getAxes Called"); + TVAP->evaluateObject(hci_method, &HCI_OUT, HCI_IN, 6); + if(HCI_OUT && (OSTypeIDInst(HCI_OUT) == OSTypeID(OSArray))) { + axes = OSDynamicCast(OSArray, HCI_OUT); + if(getUnsignedIntFromArray(axes, 0) != (UInt32)HCI_SUCCESSS){ //One more check + DPRINT("HCI method did not return with HCI_SUCCESS"); + return kIOReturnError; + } + _axes.z = (SInt16)getUnsignedIntFromArray(axes, 4); + _axes.z = _axes.z & HCI_ACCEL_DIRECTION_MASK ? + -(_axes.z & HCI_ACCEL_MASK) : _axes.z & HCI_ACCEL_MASK; + + UInt32 xy = getUnsignedIntFromArray(axes, 2); + _axes.x = xy & HCI_ACCEL_DIRECTION_MASK ? + -(xy & HCI_ACCEL_MASK) : xy & HCI_ACCEL_MASK; + + _axes.y = (xy >> HCI_ACCEL_Y_SHIFT) & HCI_ACCEL_DIRECTION_MASK ? + -((xy >> HCI_ACCEL_Y_SHIFT) & HCI_ACCEL_MASK) : + (xy >> HCI_ACCEL_Y_SHIFT) & HCI_ACCEL_MASK; + } + + if(!HCI_OUT) { + DPRINT("HCI_OUT is NULL"); + return kIOReturnError; + } + + memcpy((char*)oBuf, &_axes, sizeof(struct s_axes)); + return kIOReturnSuccess; +} + +void TOSMotionSensor::stop(IOService *provider) +{ + DPRINT("called\n"); + super::stop(provider); +} + +UInt32 getUnsignedIntFromArray(OSArray * array, UInt8 index) { + OSObject * obj = array->getObject(index); + if(obj && (OSTypeIDInst(obj) == OSTypeID(OSNumber))) { + OSNumber * num = OSDynamicCast(OSNumber, obj); + return num->unsigned32BitValue(); + } + + return -1; +} diff --git a/TOSMotionSensor/TOSMotionSensor.hpp b/TOSMotionSensor/TOSMotionSensor.hpp new file mode 100644 index 0000000..20d63fe --- /dev/null +++ b/TOSMotionSensor/TOSMotionSensor.hpp @@ -0,0 +1,86 @@ +/* + * Copyright (c) 2019 jslegendre / Xord. All rights reserved. + * + * Released under "The GNU General Public License (GPL-2.0)" + * + * 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 + * + */ + +#ifndef TOSMotionSensor_hpp +#define TOSMotionSensor_hpp + +#include +#include +#include + +#define TOSMotionSensor SMCMotionSensor +#define TOSMotionSensorClient SMCMotionSensorClient + +class TOSMotionSensor : public IOService +{ + OSDeclareDefaultStructors(TOSMotionSensor); + friend class TOSMotionSensorClient; + +public: + virtual bool init(OSDictionary *dictionary = 0); + virtual bool start(IOService *provider); + virtual void stop(IOService *provider); + + const OSSymbol * findHCI(); + bool isAccelSupported(); + IOReturn getAxes(void *iBuf, void *oBuf); +protected: + struct s_axes { SInt16 x; SInt16 y; SInt16 z; }; +private: + IOACPIPlatformDevice* TVAP; + + OSObject* HCI_IN[6]; + OSObject* HCI_OUT; + OSArray* axes; + const OSSymbol* hci_method; //HCI: Hardware Configuration Interface + + struct s_axes _axes; +}; + +class TOSMotionSensorClient : public IOUserClient +{ + OSDeclareDefaultStructors(SMCMotionSensorClient); +public: + virtual bool start(IOService* provider); + virtual void stop(IOService* provider); + virtual bool initWithTask(task_t owningTask, void *securityToken, UInt32 type, OSDictionary *properties); + virtual IOReturn clientClose(); + virtual IOExternalMethod * getTargetAndMethodForIndex(IOService **targetP, UInt32 index); +protected: + TOSMotionSensor* p_provider; + task_t p_task; +private: + IOSimpleLock *axes_lock; + +}; + +static const IOExternalMethod external_methods[1] = +{ + { + NULL, + (IOMethod)&TOSMotionSensor::getAxes, + kIOUCStructIStructO, + 40, + 40 + } +}; + +#endif diff --git a/TOSMotionSensor/TOSMotionSensorClient.cpp b/TOSMotionSensor/TOSMotionSensorClient.cpp new file mode 100644 index 0000000..4b597ac --- /dev/null +++ b/TOSMotionSensor/TOSMotionSensorClient.cpp @@ -0,0 +1,79 @@ +/* + * Copyright (c) 2019 jslegendre / Xord. All rights reserved. + * + * Released under "The GNU General Public License (GPL-2.0)" + * + * 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 + * + */ + +#include "TOSMotionSensor.hpp" + +#ifdef DEBUG +#define DPRINT(fmt, ...) \ +IOLog("TOSMotionSensorClient::%s:%d(): " fmt, __func__, __LINE__, ##__VA_ARGS__); +#else +#define DPRINT(fmt, ...) do {} while (0) +#endif + +#define super IOUserClient +OSDefineMetaClassAndStructors(SMCMotionSensorClient, IOUserClient); + +bool TOSMotionSensorClient::initWithTask(task_t owningTask, void *securityToken, UInt32 type, OSDictionary *properties) { + DPRINT("TOS1900 initwithtask"); + + if(!super::initWithTask(owningTask, securityToken, type)) { + DPRINT("TOS1900: could not initWithTask"); + return false; + } + + p_task = owningTask; + return true; +} + +IOReturn TOSMotionSensorClient::clientClose() { + p_task = NULL; + p_provider = NULL; + + if(terminate()) + return kIOReturnSuccess; + + return kIOReturnError; +} + +IOExternalMethod * TOSMotionSensorClient::getTargetAndMethodForIndex(IOService **targetP, UInt32 index) { + DPRINT("called"); + *targetP = p_provider; + + if(index == 5) //Index 5 is the orginal SMCMotionSensor selector to get accelerometer data + index = 0; + + return (IOExternalMethod*)&external_methods[index]; +} + +bool TOSMotionSensorClient::start(IOService* provider) { + DPRINT("called"); + if (!super::start(provider)) + return false; + + p_provider = OSDynamicCast(TOSMotionSensor, provider); + registerService(); + return true; +} + +void TOSMotionSensorClient::stop(IOService* provider) { + DPRINT("called"); + super::stop(provider); +} diff --git a/TOSMotionSensor/ToshibaHAPS.cpp b/TOSMotionSensor/ToshibaHAPS.cpp new file mode 100644 index 0000000..7912d18 --- /dev/null +++ b/TOSMotionSensor/ToshibaHAPS.cpp @@ -0,0 +1,112 @@ +/* + * Copyright (c) 2019 jslegendre / Xord. All rights reserved. + * + * Released under "The GNU General Public License (GPL-2.0)" + * + * 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 + * + */ + +#include "ToshibaHAPS.hpp" + +#ifdef DEBUG +#define DPRINT(fmt, ...) \ + IOLog("ToshibaHAPS::%s:%d(): " fmt, __func__, __LINE__, ##__VA_ARGS__); +#else +#define DPRINT(fmt, ...) do {} while (0) +#endif + +#define super IOService +OSDefineMetaClassAndStructors(ToshibaHAPS, IOService); + +IOReturn ToshibaHAPS::resetHaps() { + UInt32 dummy; //Throwaway - Avoid NULL (de)referencing + + //ACPI method 'RSSS' does not return anything + return haps->evaluateInteger(RSSS, &dummy); +} + +IOReturn ToshibaHAPS::setCurrentPTLV(UInt32 level) { + DPRINT("Setting protection level: %d", level); + IOReturn ret = kIOReturnSuccess; + + /* Some models that ship with built in SSD have a HAPS device + with no methods other than _STA (which unconditionally + returns zero). In this case we set IORegistry protection level + entry to 2 (default) return success, and move on. */ + ret = haps->validateObject(PTLV); + if(ret != kIOReturnSuccess) { + setProperty(ptlv_io_reg_property, OSNumber::withNumber(2, 32)); + return kIOReturnSuccess; + } + + UInt32 dummy; + OSObject* ptlv = OSDynamicCast(OSObject, OSNumber::withNumber(level, 32)); + + //ACPI method 'PTLV' does not return anything + ret = haps->evaluateInteger(PTLV, &dummy, &ptlv, 1); + if(ret == kIOReturnSuccess) { + DPRINT("Setting protection level successful"); + setProperty(ptlv_io_reg_property, ptlv); + } else { + DPRINT("Setting protection level failed with error: %d", ret); + return ret; + } + + ret = resetHaps(); + if (ret != kIOReturnSuccess) { + DPRINT("Could not activate HAPS device"); + } + return ret; +} + +bool ToshibaHAPS::init(OSDictionary* properties) { + DPRINT("ToshibaHAPS init"); + + if (!super::init(properties)) + return false; + + haps = NULL; + ptlv_io_reg_property = "ProtectionLevel"; + + RSSS = OSSymbol::withCStringNoCopy("RSSS"); + PTLV = OSSymbol::withCStringNoCopy("PTLV"); + + setProperty(ptlv_io_reg_property, (UInt32)0); + return true; +} + +void ToshibaHAPS::free() { + super::free(); +} + +bool ToshibaHAPS::start(IOService* provider) { + DPRINT("ToshibaHAPS starting"); + haps = OSDynamicCast(IOACPIPlatformDevice, provider); + + if (!super::start(provider)) + return false; + + //PT_LEVEL_MED: Enables accelerometers, no shock protection + if(setCurrentPTLV(PT_LEVEL_MED) != kIOReturnSuccess) + return false; + + this->registerService(); + return true; +} + +void ToshibaHAPS::stop(IOService* provider) { + super::stop(provider); +} diff --git a/TOSMotionSensor/ToshibaHAPS.hpp b/TOSMotionSensor/ToshibaHAPS.hpp new file mode 100644 index 0000000..19b58e3 --- /dev/null +++ b/TOSMotionSensor/ToshibaHAPS.hpp @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2019 jslegendre / Xord. All rights reserved. + * + * Released under "The GNU General Public License (GPL-2.0)" + * + * 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 + * + */ + +#ifndef ToshibaHAPS_hpp +#define ToshibaHAPS_hpp + +#include +#include + +//Various HD protection levels +enum PT_LEVELS { + PT_LEVEL_NONE, // Off + PT_LEVEL_LOW, + PT_LEVEL_MED, + PT_LEVEL_HIGH // Do not use! Causes KP +}; + +class ToshibaHAPS : public IOService { + OSDeclareDefaultStructors(ToshibaHAPS); +public: + bool init(OSDictionary* properties); + void free(); + bool start(IOService* provider); + void stop(IOService* provider); + + IOReturn resetHaps(); + IOReturn setCurrentPTLV(UInt32 level); + OSObject* getCurrentPTLV(); + +protected: +private: + IOACPIPlatformDevice* haps; + const char* ptlv_io_reg_property; + const OSSymbol* RSSS; // Reset - ACPI Method + const OSSymbol* PTLV; // Protection Level - ACPI Method +}; + + +#endif /* ToshibaHAPS_hpp */ diff --git a/Test/test b/Test/test new file mode 100755 index 0000000..7c7234d Binary files /dev/null and b/Test/test differ diff --git a/Test/test.c b/Test/test.c new file mode 100644 index 0000000..c6c2849 --- /dev/null +++ b/Test/test.c @@ -0,0 +1,72 @@ +/* + * Bare minimum setup to poll SMCMotionSensor service + * Build: + * gcc -framework CoreFoundation -framework IOKit test.c -o test + */ + +#include +#include +#include + +typedef struct { + short x; + short y; + short z; +} axes; + +int main(int argc, const char * argv[]) { + CFMutableDictionaryRef dict = IOServiceMatching("SMCMotionSensor"); + if(!dict) { + printf("Could not find service\n"); + return 1; + } + + kern_return_t result; + io_connect_t connection; + io_service_t device = 0; + + device = IOServiceGetMatchingService(kIOMasterPortDefault, dict); + if(!device) { + printf("No device\n"); + return 1; + } + + + if(IOServiceOpen(device, mach_task_self(), 0, &connection) != KERN_SUCCESS) { + printf("Could not open device\n"); + return 1; + } + + size_t smc_arg_size = 40; + axes *out_struct = NULL; + + void* smc_in = malloc(smc_arg_size); + memset(smc_in, 1, smc_arg_size); + + void* smc_out = malloc(smc_arg_size); + memset(smc_out, 0, smc_arg_size); + + result = IOConnectCallStructMethod(connection, + 5, + smc_in, + 40, + smc_out, + &smc_arg_size); + if(result != KERN_SUCCESS) { + printf("Could not call struct method\n"); + goto end; + } + + out_struct = (axes*)smc_out; + printf("Success\nAccelerometer reading: { x: %d, y: %d, z: %d }\n", out_struct->x, out_struct->y, out_struct->z); + +end: + free(smc_in); + free(smc_out); + + if(result != KERN_SUCCESS) + return 1; + + return 0; +} +