# Examples Directory This directory contains the complete source code for all examples provided throughout the **FreeBSD Device Drivers Book**. These examples are designed to help readers understand and practice the concepts discussed in each chapter. ## Directory Structure The examples are organized to mirror the book's structure: ``` examples/ ├── README.md # This file ├── chapters/ # Examples from main chapters │ ├── part1/ # Part 1: Fundamentals │ ├── part2/ # Part 2: Basic Drivers │ ├── part3/ # Part 3: Advanced Topics │ ├── part4/ # Part 4: Specialized Drivers │ ├── part5/ # Part 5: Performance & Optimization │ ├── part6/ # Part 6: Debugging & Testing │ └── part7/ # Part 7: Real-world Applications └── appendices/ # Examples from appendices ├── appendix-a/ # Appendix A examples ├── appendix-b/ # Appendix B examples ├── appendix-c/ # Appendix C examples ├── appendix-d/ # Appendix D examples └── appendix-e/ # Appendix E examples ``` ## Example Organization Each example includes: - **Source code files** (`.c`, `.h`, `.mk`, etc.) - **Makefile** for building the example - **README** with specific instructions for that example - **Documentation** explaining the example's purpose and usage ## Building Examples ### Prerequisites Before building any examples, ensure you have: 1. **FreeBSD development environment** set up 2. **Kernel source code** available 3. **Required tools** installed: - `gcc` or `clang` - `make` - `kldload`/`kldunload` (for kernel modules) ### Building a Specific Example ```bash # Navigate to the example directory cd examples/chapters/part1/example-01 # Build the example make # Load the kernel module (if applicable) sudo kldload ./example_module.ko # Unload the kernel module (if applicable) sudo kldunload example_module ``` ### Building All Examples ```bash # From the examples directory make all ``` ## Example Types The examples cover various aspects of FreeBSD device driver development: ### Part 1: Foundations: FreeBSD, C, and the Kernel - **Introduction** - Getting started with FreeBSD device driver development - **Setting Up Your Lab** - Development environment setup and configuration - **A Gentle Introduction to UNIX** - UNIX fundamentals for driver development - **A First Look at the C Programming Language** - C programming basics - **Understanding C for Kernel Programming** - Advanced C concepts for kernel development - **The Anatomy of a FreeBSD Driver** - Basic driver structure and components ### Part 2: Basic Driver Development - **Writing Your First Driver** - Creating a simple kernel module - **Working with Device Files** - Device file creation and management - **Reading and Writing to Devices** - Implementing read/write operations - **Handling Input and Output Efficiently** - Optimizing I/O operations ### Part 3: Concurrency and Synchronization - **Concurrency in Drivers** - Managing concurrent operations - **Synchronization Mechanisms** - Implementing proper synchronization - **Timers and Delayed Work** - Time-based operations and delayed execution - **Taskqueues and Deferred Work** - Using taskqueues for deferred processing - **More Synchronization — Conditions, Semaphores, and Coordination** - Advanced synchronization techniques ### Part 4: Hardware and Platform-Level Integration - **Accessing Hardware** - Direct hardware access techniques - **Simulating Hardware** - Creating virtual hardware for testing - **Writing a PCI Driver** - Developing drivers for PCI and PCIe devices - **Handling Interrupts** - Basic interrupt handling implementation - **Advanced Interrupt Handling** - Sophisticated interrupt techniques - **DMA and High-Speed Data Transfer** - Direct Memory Access implementation - **Power Management** - Power management features in drivers ### Part 5: Debugging, Tools, and Real-World Practices - **Debugging and Tracing** - Tools and techniques for debugging drivers - **Integrating with the Kernel** - Best practices for kernel integration - **Advanced Topics and Practical Tips** - Advanced techniques and practical advice ### Part 6: Writing Transport-Specific Drivers - **USB and Serial Drivers** - Developing drivers for USB and serial devices - **Working with Storage Devices and the VFS Layer** - Storage device drivers and VFS integration - **Writing a Network Driver** - Network interface driver development ### Part 7: Mastery Topics: Special Scenarios and Edge Cases - **Portability and Driver Abstraction** - Creating portable drivers across architectures - **Virtualisation and Containerization** - Driver development for virtualized environments - **Security Best Practices** - Implementing security measures in drivers - **Device Tree and Embedded Development** - Driver development for embedded systems - **Performance Tuning and Profiling** - Optimizing driver performance - **Advanced Debugging Techniques** - Sophisticated debugging methods - **Asynchronous I/O and Event Handling** - Asynchronous operations and event-driven architectures - **Creating Drivers Without Documentation (Reverse Engineering)** - Developing drivers without documentation - **Submitting Your Driver to the FreeBSD Project** - Contributing drivers to FreeBSD - **Final Thoughts and Next Steps** - Concluding guidance and continued learning ## Contributing If you find issues with any examples or want to contribute improvements: 1. **Test the example** on your FreeBSD system 2. **Document any issues** you encounter 3. **Submit improvements** via pull request 4. **Follow the coding standards** used in the book ## Version Compatibility These examples are designed for: - **FreeBSD 14.x** and later - **x86_64** and **ARM64** architectures - **GCC** and **Clang** compilers ## Troubleshooting ### Common Issues 1. **Build errors**: Ensure you have the correct kernel headers installed 2. **Module loading failures**: Check kernel messages with `dmesg` 3. **Permission errors**: Use `sudo` for kernel module operations 4. **Architecture mismatches**: Verify you're building for the correct architecture ### Getting Help - Check the specific example's README for detailed instructions - Review the book chapter for theoretical background - Consult FreeBSD documentation and man pages - Check kernel messages: `dmesg | tail` ## License These examples are provided under the same license as the FreeBSD Device Driver Book. See the main project LICENSE file for details. --- **Note**: These examples are for educational purposes. Always test thoroughly before using in production environments.