Study device driver source files?

北城余情 提交于 2021-02-06 09:34:04

问题


I want to study the source files of some of the device drivers that are installed and loaded on either a raspberry pi(raspian), beaglebone(debian) or a my laptop(ubuntu).

My aim is to learn how to properly implement my own modules by studying the source files of some drivers that actually works.

I am particularly interested in drivers that communicates with actual hardware (USB, I2C, SPI, UART etc).

Can someone tell me how to find these sources? are they available in some particular folder i.e something like /usr/src/**** or do I have to download all of the kernel source files from a particular kernel release?

All advice's, opinions and recommendations are most appreciated.

p.s I have read "Linux Kernel Development 3rd edition" but please tell me if you know any other free/open-source books on the subject.

Best regards Henrik


回答1:


Linux Source directory and description :

  • arch/ - The arch sub-directory contains all of the architecture specific kernel code.

    Example :
    1. 'arch/arm/' will have your board related configuration file.
        like 'arch/arm/mach-omap/' will have omap specific source code.
    2. 'arch/arm/config' Generates a new kernel configuration with the
        default answer being used for all options. The default values
        are taken from a file located in the arch/$ARCH/defconfig
        file,where $ARCH refers to the specific architecture for which
        the kernel is being built.
    3. arch/arm/boot have kernel zImage, dtb image after compilation.
    
  • block/ - This folder holds code for block-device drivers. Block devices are devices that accept and send data in blocks. Data blocks are chunks of data instead of a continual stream.

  • crypto/ - This folder contains the source code for many encryption algorithms.

     example, “sha1_generic.c” is the file that contains the code for
               the sha1 encryption algorithm.
    
  • Documentation/ - It has kernel related information in text format.

  • drivers/ - All of the system's device drivers live in this directory. They are further sub-divided into classes of device driver.

     Example,
     1. drivers/video/backlight/ has blacklight driver source which
        will control display brightness.
     2. drivers/video/display/ has display driver source. 
     3. drivers/input/ has input driver source code. like touch,
        keyboard and mouse driver.
     4. drivers/char/ has charter driver source code.
     5. drivers/i2c/ has i2c subsystem and driver source code.
     6. drivers/pci/ has pci subsytem and driver related source code.
     7. drivers/bluetooth  has Bluetooth driver file.
     8. drivers/power has power and battery driver.
    
  • firmware/ - The firmware folder contains code that allows the computer to read and understand signals from devices. For illustration, a webcam manages its own hardware, but the computer must understand the signals that the webcam is sending the computer.

  • fs/ - All of the file system code. This is further sub-divided into directories, one per supported file system, for example vfat and ext2.

  • kernel/ - The code in this folder controls the kernel itself. For instance, if a debugger needed to trace an issue, the kernel would use code that originated from source files in this folder to inform the debugger of all of the actions that the kernel performs. There is also code here for keeping track of time. In the kernel folder is a directory titled "power". Some code in this folder provide the abilities for the computer to restart, power-off, and suspend.

  • net/ - net The kernel's networking code. lib This directory contains the kernel's library code. The architecture specific library code can be found in arch/*/lib/. scripts This directory contains the scripts (for example awk and tk scripts) that are used when the kernel is configured.

  • lib/ - This directory contains the kernel's library code. The architecture specific library code can be found in arch/*/lib/.

  • scripts/ - This directory contains the scripts (for example awk and tk scripts) that are used when the kernel is configured.

  • mm/ - This directory contains all of the memory management code. The architecture specific memory management code lives down in arch/*/mm/, for example arch/i386/mm/fault.c.

  • ipc/ - This directory contains the kernels inter-process communications code.

  • **init/ -**The init folder has code that deals with the startup of the kernel (INITiation). The main.c file is the core of the kernel. This is the main source code file the connects all of the other files.

  • sound/ - This is where all of the sound card drivers are.

There are few more directory certs, crypto, security, include, virt and usr etc....




回答2:


There are a few different methods that I use for viewing kernel related source, and I'm sure there are a few other good methods out there as well. You will find that the methods are largely the same.

  1. Head on over to kernel.org and download the kernel of your choice. You will find driver related source under /<path to your downloaded kernel>/drivers. For example, I have downloaded and extracted kernel 4.5.5 to /usr/src/linux-4.5.5, and access the source for my drivers via /usr/src/linux-4.5.5/drivers.

  2. Use a linux cross-reference website. Personally, I use the one hosted on free-electrons. These websites are nice for their free-text or identifier searches.

  3. Browse Linus Torvalds' linux repo hosted on github.




回答3:


Never mind, I found the source files under

~/linux/drivers/

example:

nano ~/linux/drivers/spi/spi-bitbang.c

Sorry, for any inconvenience!



来源:https://stackoverflow.com/questions/37680905/study-device-driver-source-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!