Session 5: Character Device Drivers


Download and unzip the tasks archive sesiune-05 on the virtual machine.

  1. Simple character device driver
    • Use the code in the archive to implement a simple device that has a text buffer that can be read and written.
      • The module will use major 42 and minor 0. Make use of this information when creating device node file
      • sudo mknod /dev/mydev c 42 0
      • All the code is implemented by commented out. Take each TODO and analyse the commented code and then uncomment it step by step.
      • After implementing TODO 1a and TODO 1b, compile, load the module and view the contents of /proc/devices to see that the kernel module registered the major number.
    • After implementing TODO 5, compile, load the module and do a cat /dev/mydev and and echo something>/dev/mydev and see the dmesg messages.
    • After implementing all TODOs, write something into /dev/mydev and read from it.
  2. Double the work, same effort
    • Modify the code such as the device driver could handle more devices (NUM_MINORS devices)
      • These devices will have the same Major number, but different minor numbers.
      • Most code can be reused (such as the file operations), but each device needs it's own instance of the struct my_device_data and each cdev structure needs to be initiated.
      • Use a static array of my_device_data[NUM_MINORS]. Iterate from 0 to NUM_MINORS and initiate all needed structures.
  3. Unique access
    • Create a new character device that prints out the string “42” forever (sort of like /dev/zero).
    • Implement only the open, read, and release file operation (make mocks for the rest).
    • Try to access the device driver with two cat processes (in two terminals, for example).
    • Attach an atomic variable to the char_device structure that keeps track of how many processes access the device
    • In the open handler, set the variable to 1 if is set to 0; if it's already set to 1, return -EBUSY
    • Try to access the device driver again with two cat processes.
sesiuni/kernel/day-5.txt · Last modified: 2013/07/05 08:12 by alexj