安卓linux uevent内核上报机制实例

uevent可以实现内核通知上层的一种机制,最常见的电池状态的变化就是kernel uevent通知的,每次百分比或者其他的变化通过power_supply_changed通知上层update;


每个device下面都有kobj,找到device就可以通过kobject_uevent_env 通知android了;


以拔出T卡为例,内核通知上层。这里的T卡是一种sdio设备,普通TF卡是块设备,安卓通过块设备可以知道T卡的移除。

kernel:
Index: kernel/drivers/mmc/host/msm_sdcc.c
===================================================================
--- kernel/drivers/mmc/host/msm_sdcc.c    (revision 10898)
+++ kernel/drivers/mmc/host/msm_sdcc.c    (working copy)
@@ -77,6 +77,8 @@
 #define MSM_MMC_BUS_VOTING_DELAY    200 /* msecs */
 #define INVALID_TUNING_PHASE        -1
 
+struct device *mmc1_dev;

+static int obj_enable =0;
+static int set_kobject_env(const char *val, struct kernel_param *kp)
+{
+    printk("%s called 00 \n",__func__);
+   kobject_uevent_env(&mmc1_dev->kobj, KOBJ_CHANGE, NULL);
+    return 0;
+}
+module_param_call(obj, set_kobject_env, param_get_uint,
+                    &obj_enable, 0644);
+
 static irqreturn_t
 msmsdcc_platform_status_irq(int irq, void *dev_id)
 {
@@ -6186,6 +6205,11 @@
     ret = device_create_file(&pdev->dev, &host->idle_timeout);
     if (ret)
         goto remove_polling_file;
+
+    if(mmc->index ==1)
+        mmc1_dev = &pdev->dev;
+
+   
     return 0;
 

java:
 if (new File("/sys/devices/platform/msm_sdcc.3/uevent").exists()) {
            mInvalidChargerObserver.startObserving(
                    "DEVPATH=/devices/platform/msm_sdcc.3");
        }

    private final UEventObserver mInvalidChargerObserver = new UEventObserver() {
        @Override
        public void onUEvent(UEventObserver.UEvent event) {

        Slog.v(TAG, "Uevent changed ");
            final int invalidCharger = "1".equals(event.get("SWITCH_STATE")) ? 1 : 0;
            synchronized (mLock) {
                if (mInvalidCharger != invalidCharger) {
                    mInvalidCharger = invalidCharger;
                    updateLocked();
                }
            }
        }
    };


你可能感兴趣的:(内核机制)