|Linux| How to create a device node?

How to create a device in /dev automatically

Ans: device_create

- Include the header file linux/device.h and linux/kdev_t.h

    static struct class c_dev;
    static struct dev_t dev;

- Add the below API 's inside __init fuction of the driver

    cl = class_create(THIS_MODULE ,"x");

    where x - Name to be displayed inside /sys/class/ when driver is loaded.

 - Use device_create () kernel api with device_create(cl, NULL, dev, NULL, "d");

    where d - device file to be created under /dev.

 - Use cdev_init to initialize

 - Use cdev_add to add the device to the /dev list


Advance:
What does “cat sys/bus/msm_subsys/devices/subsys2/state” mean ?
 It means that asking the type of bus.
 The Basic Device Driver-Model Structures


Ref.
udev-強大的device node管理系統
Dynamic Kernel Device Management with udev
How to create an event# device for a virtual input device (/dev/input/js3) in linux kernel module?

Next:
Linux Driver Tutorial: How to Write a Simple Linux Device Driver
HelloDriverSource

|Network| How does the data packet receive in kernel driver’s view ?

I surf the internet and see a good article, and I back up a part of the article.
How does network device driver works

  1. Driver is loaded and initialized.
  2. Packet arrives at the NIC from the network.
  3. Packet is copied (via DMA) to a ring buffer in kernel memory.
  4. Hardware interrupt is generated to let the system know a packet is in memory.
  5. Driver calls into NAPI to start a poll loop if one was not running already.
  6. ksoftirqd processes run on each CPU on the system. They are registered at boot time. The ksoftirqd processes pull packets off the ring buffer by calling the NAPI poll function that the device driver registered during initialization.
  7. Memory regions in the ring buffer that have had network data written to them are unmapped.
  8. Data that was DMA’d into memory is passed up the networking layer as an ‘skb’ for more processing.
  9. Packet steering happens to distribute packet processing load to multiple CPUs (in leu of a NIC with multiple receive queues), if enabled.
  10. Packets are handed to the protocol layers from the queues.
  11. Protocol layers add them to receive buffers attached to sockets.

|Coding| C++ smart pointers: unique_ptr, shared_ptr, strong pointer(sp, Android)

In C++, smart pointers, unique_ptr and shared_ptr, are very common.
If you develop on Android, you may have seen strong pointer(sp), it is also a smart pointer.
Some notes about those pointers.

strong pointer in Android, can be saw as shared_ptr.
shared_ptr has a counter to record who is using this pointer in order to prevent dangerous pointer managements.