1. 程式人生 > 其它 >基於input子系統的sensor驅動除錯(二)

基於input子系統的sensor驅動除錯(二)

繼上一篇:https://cloud.tencent.com/developer/article/1054078

一、驅動流程解析:

1、模組載入:

 1 static struct of_device_id stk_match_table[] = {
 2     { .compatible = "stk,stk3x1x", },
 3     { },
 4 };
 5 
 6 static struct i2c_driver stk_ps_driver =
 7 {
 8     .driver = {
 9         .name = DEVICE_NAME,
10         .owner = THIS_MODULE,
11         .of_match_table = stk_match_table,
12     },
13     .probe = stk3x1x_probe,
14     .remove = stk3x1x_remove,
15     .id_table = stk_ps_id,
16 };
17 
18 
19 static int __init stk3x1x_init(void)
20 {
21     int ret;
22     ret = i2c_add_driver(&stk_ps_driver);
23     if (ret)
24         return ret;
25 
26     return 0;
27 }
28 
29 static void __exit stk3x1x_exit(void)
30 {
31     i2c_del_driver(&stk_ps_driver);
32 }

of_device_id與DTS中的匹配,這與核心2.6以前的i2c_board_info不一樣;

核心載入驅動模組的時候將呼叫到stk3x1x_init()方法:

初始化了i2c_driver結構體給stk_ps_driver變數,將用於將設備註冊到IIC。關鍵在於結構體中的probe()方法,註冊完成的時候將呼叫;

2、stk3x1x驅動初始化-probe函式:

  1 static int stk3x1x_probe(struct i2c_client *client,
  2                         const struct i2c_device_id *id)
  3 {
  4     int err = -ENODEV;
  5     struct stk3x1x_data *ps_data;
  6     struct stk3x1x_platform_data *plat_data;
  7     printk(KERN_INFO "%s: driver version = %sn", __func__, DRIVER_VERSION);
  8 
  9     if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
 10     {
 11         printk(KERN_ERR "%s: No Support for I2C_FUNC_SMBUS_BYTE_DATAn", __func__);
 12         return -ENODEV;
 13     }
 14     if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_WORD_DATA))
 15     {
 16         printk(KERN_ERR "%s: No Support for I2C_FUNC_SMBUS_WORD_DATAn", __func__);
 17         return -ENODEV;
 18     }
 19 
 20     ps_data = kzalloc(sizeof(struct stk3x1x_data),GFP_KERNEL);
 21     if(!ps_data)
 22     {
 23         printk(KERN_ERR "%s: failed to allocate stk3x1x_datan", __func__);
 24         return -ENOMEM;
 25     }
 26     ps_data->client = client;
 27     i2c_set_clientdata(client,ps_data);
 28     mutex_init(&ps_data->io_lock);
 29     wake_lock_init(&ps_data->ps_wakelock,WAKE_LOCK_SUSPEND, "stk_input_wakelock");
 30 
 31 #ifdef STK_POLL_PS
 32     wake_lock_init(&ps_data->ps_nosuspend_wl,WAKE_LOCK_SUSPEND, "stk_nosuspend_wakelock");
 33 #endif
 34     if (client->dev.of_node) {
 35         plat_data = devm_kzalloc(&client->dev,
 36             sizeof(struct stk3x1x_platform_data), GFP_KERNEL);
 37         if (!plat_data) {
 38             dev_err(&client->dev, "Failed to allocate memoryn");
 39             return -ENOMEM;
 40         }
 41 
 42         err = stk3x1x_parse_dt(&client->dev, plat_data);
 43         dev_err(&client->dev,
 44             "%s: stk3x1x_parse_dt ret=%dn", __func__, err);
 45         if (err)
 46             return err;
 47     } else
 48         plat_data = client->dev.platform_data;
 49 
 50     if (!plat_data) {
 51         dev_err(&client->dev,
 52             "%s: no stk3x1x platform data!n", __func__);
 53         goto err_als_input_allocate;
 54     }
 55     ps_data->als_transmittance = plat_data->transmittance;
 56     ps_data->int_pin = plat_data->int_pin;
 57     ps_data->use_fir = plat_data->use_fir;
 58     ps_data->pdata = plat_data;
 59 
 60     if (ps_data->als_transmittance == 0) {
 61         dev_err(&client->dev,
 62             "%s: Please set als_transmittancen", __func__);
 63         goto err_als_input_allocate;
 64     }
 65 
 66     ps_data->als_input_dev = devm_input_allocate_device(&client->dev);
 67     if (ps_data->als_input_dev==NULL)
 68     {
 69         printk(KERN_ERR "%s: could not allocate als devicen", __func__);
 70         err = -ENOMEM;
 71         goto err_als_input_allocate;
 72     }
 73     ps_data->ps_input_dev = devm_input_allocate_device(&client->dev);
 74     if (ps_data->ps_input_dev==NULL)
 75     {
 76         printk(KERN_ERR "%s: could not allocate ps devicen", __func__);
 77         err = -ENOMEM;
 78         goto err_als_input_allocate;
 79     }
 80     ps_data->als_input_dev->name = ALS_NAME;
 81     ps_data->ps_input_dev->name = PS_NAME;
 82     set_bit(EV_ABS, ps_data->als_input_dev->evbit);
 83     set_bit(EV_ABS, ps_data->ps_input_dev->evbit);
 84     input_set_abs_params(ps_data->als_input_dev, ABS_MISC, 0, stk_alscode2lux(ps_data, (1<<16)-1), 0, 0);
 85     input_set_abs_params(ps_data->ps_input_dev, ABS_DISTANCE, 0,1, 0, 0);
 86     err = input_register_device(ps_data->als_input_dev);
 87     if (err<0)
 88     {
 89         printk(KERN_ERR "%s: can not register als input devicen", __func__);
 90         goto err_als_input_allocate;
 91     }
 92     err = input_register_device(ps_data->ps_input_dev);
 93     if (err<0)
 94     {
 95         printk(KERN_ERR "%s: can not register ps input devicen", __func__);
 96         goto err_als_input_allocate;
 97     }
 98 
 99     err = sysfs_create_group(&ps_data->als_input_dev->dev.kobj, &stk_als_attribute_group);
100     if (err < 0)
101     {
102         printk(KERN_ERR "%s:could not create sysfs group for alsn", __func__);
103         goto err_als_input_allocate;
104     }
105     err = sysfs_create_group(&ps_data->ps_input_dev->dev.kobj, &stk_ps_attribute_group);
106     if (err < 0)
107     {
108         printk(KERN_ERR "%s:could not create sysfs group for psn", __func__);
109         goto err_ps_sysfs_create_group;
110     }
111     input_set_drvdata(ps_data->als_input_dev, ps_data);
112     input_set_drvdata(ps_data->ps_input_dev, ps_data);
113 
114 #ifdef STK_POLL_ALS
115     ps_data->stk_als_wq = create_singlethread_workqueue("stk_als_wq");
116     INIT_WORK(&ps_data->stk_als_work, stk_als_work_func);
117     hrtimer_init(&ps_data->als_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
118     ps_data->als_poll_delay = ns_to_ktime(110 * NSEC_PER_MSEC);
119     ps_data->als_timer.function = stk_als_timer_func;
120 #endif
121 
122     ps_data->stk_ps_wq = create_singlethread_workqueue("stk_ps_wq");
123     INIT_WORK(&ps_data->stk_ps_work, stk_ps_work_func);
124     hrtimer_init(&ps_data->ps_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
125     ps_data->ps_poll_delay = ns_to_ktime(110 * NSEC_PER_MSEC);
126     ps_data->ps_timer.function = stk_ps_timer_func;
127 #if (!defined(STK_POLL_ALS) || !defined(STK_POLL_PS))
128     ps_data->stk_wq = create_singlethread_workqueue("stk_wq");
129     INIT_WORK(&ps_data->stk_work, stk_work_func);
130     err = stk3x1x_setup_irq(client);
131     if(err < 0)
132         goto err_stk3x1x_setup_irq;
133 #endif
134 
135     err = stk3x1x_power_init(ps_data, true);
136     if (err)
137         goto err_power_init;
138 
139     err = stk3x1x_power_ctl(ps_data, true);
140     if (err)
141         goto err_power_on;
142 
143     ps_data->als_enabled = false;
144     ps_data->ps_enabled = false;
145 #ifdef CONFIG_HAS_EARLYSUSPEND
146     ps_data->stk_early_suspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN + 1;
147     ps_data->stk_early_suspend.suspend = stk3x1x_early_suspend;
148     ps_data->stk_early_suspend.resume = stk3x1x_late_resume;
149     register_early_suspend(&ps_data->stk_early_suspend);
150 #endif
151     /* make sure everything is ok before registering the class device */
152     ps_data->als_cdev = sensors_light_cdev;
153     ps_data->als_cdev.sensors_enable = stk_als_enable_set;
154     ps_data->als_cdev.sensors_poll_delay = stk_als_poll_delay_set;
155     err = sensors_classdev_register(&client->dev, &ps_data->als_cdev);
156     if (err)
157         goto err_power_on;
158 
159     ps_data->ps_cdev = sensors_proximity_cdev;
160     ps_data->ps_cdev.sensors_enable = stk_ps_enable_set;
161     err = sensors_classdev_register(&client->dev, &ps_data->ps_cdev);
162     if (err)
163         goto err_class_sysfs;
164 
165     /* enable device power only when it is enabled */
166     err = stk3x1x_power_ctl(ps_data, false);
167     if (err)
168         goto err_init_all_setting;
169 
170     dev_dbg(&client->dev, "%s: probe successfully", __func__);
171 
172     return 0;
173 
174 err_init_all_setting:
175     stk3x1x_power_ctl(ps_data, false);
176     sensors_classdev_unregister(&ps_data->ps_cdev);
177 err_class_sysfs:
178     sensors_classdev_unregister(&ps_data->als_cdev);
179 err_power_on:
180     stk3x1x_power_init(ps_data, false);
181 err_power_init:
182 #ifndef STK_POLL_PS
183     free_irq(ps_data->irq, ps_data);
184     gpio_free(plat_data->int_pin);
185 #endif
186 #if (!defined(STK_POLL_ALS) || !defined(STK_POLL_PS))
187 err_stk3x1x_setup_irq:
188 #endif
189 #ifdef STK_POLL_ALS
190     hrtimer_try_to_cancel(&ps_data->als_timer);
191     destroy_workqueue(ps_data->stk_als_wq);
192 #endif
193     destroy_workqueue(ps_data->stk_ps_wq);
194 #if (!defined(STK_POLL_ALS) || !defined(STK_POLL_PS))
195     destroy_workqueue(ps_data->stk_wq);
196 #endif
197     sysfs_remove_group(&ps_data->ps_input_dev->dev.kobj, &stk_ps_attribute_group);
198 err_ps_sysfs_create_group:
199     sysfs_remove_group(&ps_data->als_input_dev->dev.kobj, &stk_als_attribute_group);
200 err_als_input_allocate:
201 #ifdef STK_POLL_PS
202     wake_lock_destroy(&ps_data->ps_nosuspend_wl);
203 #endif
204     wake_lock_destroy(&ps_data->ps_wakelock);
205     mutex_destroy(&ps_data->io_lock);
206     kfree(ps_data);
207     return err;
208 }

 在stk3x1x_probe函式中主要做了:

1、為驅動私有資料結構體stk3x1x_data分配記憶體空間;

2、 將裝置驅動的私有資料(stk3x1x_data)連線到裝置client(i2c_client)中;(bma255會增加一步:讀取i2c的id);

3、將stk3x1x驅動註冊到linux input子系統;

4、建立工作佇列(主要是對sensor的資料採集);

5、建立sysfs介面;

2.1 建立input子系統:

http://blog.csdn.net/ielife/article/details/7798952

1、 在驅動載入模組中,設定你的input裝置支援的事件型別;

2、 註冊中斷處理函式,例如鍵盤裝置需要編寫按鍵的抬起、放下,觸控式螢幕裝置需要編寫按下、抬起、絕對移動,滑鼠裝置需要編寫單擊、抬起、相對移動,並且需要在必要的時候提交硬體資料(鍵值/座標/狀態等等);

3、將輸入設備註冊到輸入子系統中;

 1   ps_data->als_input_dev = devm_input_allocate_device(&client->dev);    //分配記憶體空間
 2     if (ps_data->als_input_dev==NULL)
 3     {
 4         printk(KERN_ERR "%s: could not allocate als devicen", __func__);
 5         err = -ENOMEM;
 6         goto err_als_input_allocate;
 7     }
 8     ps_data->ps_input_dev = devm_input_allocate_device(&client->dev);
 9     if (ps_data->ps_input_dev==NULL)
10     {
11         printk(KERN_ERR "%s: could not allocate ps devicen", __func__);
12         err = -ENOMEM;
13         goto err_als_input_allocate;
14     }
15     ps_data->als_input_dev->name = ALS_NAME;     
16     ps_data->ps_input_dev->name = PS_NAME;
17     set_bit(EV_ABS, ps_data->als_input_dev->evbit);
18     set_bit(EV_ABS, ps_data->ps_input_dev->evbit);
19     input_set_abs_params(ps_data->als_input_dev, ABS_MISC, 0, stk_alscode2lux(ps_data, (1<<16)-1), 0, 0);    //設定input載入型別;
20     input_set_abs_params(ps_data->ps_input_dev, ABS_DISTANCE, 0,1, 0, 0);
21     err = input_register_device(ps_data->als_input_dev);
22     if (err<0)
23     {
24         printk(KERN_ERR "%s: can not register als input devicen", __func__);
25         goto err_als_input_allocate;
26     }
27     err = input_register_device(ps_data->ps_input_dev);
28     if (err<0)
29     {
30         printk(KERN_ERR "%s: can not register ps input devicen", __func__);
31         goto err_als_input_allocate;
32     }
1     err = stk3x1x_setup_irq(client);        //設定驅動中斷函式
2     if(err < 0)
3         goto err_stk3x1x_setup_irq;

2.2 建立工作佇列:

先提一個問題,為什麼要建立工作佇列?在前面的介紹中我們知道,sensor感測器獲取資料後,將資料傳給controller的暫存器中,供主控去查詢讀取資料。所以這裡建立的工作佇列,就是在一個工作者執行緒,通過IIC不斷的去查詢讀取controller上的資料。

工作佇列的作用就是把工作推後,交由一個核心執行緒去執行,更直接的說就是如果寫了一個函式,而現在不想馬上執行它,想在將來某個時刻去執行它,那用工作佇列準沒錯.大概會想到中斷也是這樣,提供一箇中斷服務函式,在發生中斷的時候去執行,沒錯,和中斷相比,工作佇列最大的好處就是可以排程可以睡眠,靈活性更好。

上面程式碼中我們看到INIT_WORK(&ps_data->stk_ps_work, stk_ps_work_func);,其實是一個巨集的定義,在include/linux/workqueue.h中。stk_ps_work_func()就是我們定義的功能函式,用於查詢讀取Sensor的距離感測器資料的,並上報Input子系統,程式碼如下:

 1 static void stk_ps_work_func(struct work_struct *work)
 2 {
 3     struct stk3x1x_data *ps_data = container_of(work, struct stk3x1x_data, stk_ps_work);
 4     uint32_t reading;
 5     int32_t near_far_state;
 6     uint8_t org_flag_reg;
 7     int32_t ret;
 8     uint8_t disable_flag = 0;
 9     mutex_lock(&ps_data->io_lock);
10 
11     org_flag_reg = stk3x1x_get_flag(ps_data);
12     if(org_flag_reg < 0)
13     {
14         printk(KERN_ERR "%s: get_status_reg fail, ret=%d", __func__, org_flag_reg);
15         goto err_i2c_rw;
16     }
17     near_far_state = (org_flag_reg & STK_FLG_NF_MASK)?1:0;
18     reading = stk3x1x_get_ps_reading(ps_data);
19     if(ps_data->ps_distance_last != near_far_state)
20     {
21         ps_data->ps_distance_last = near_far_state;
22         input_report_abs(ps_data->ps_input_dev, ABS_DISTANCE, near_far_state);    //input上報資料
23         input_sync(ps_data->ps_input_dev);                    //input_sync()在這裡不起關鍵作用。但如果是一個觸控式螢幕,即有x座標和y座標,則需要通過input_sync()函式把x和y座標完整地傳遞給輸入子系統。
24         wake_lock_timeout(&ps_data->ps_wakelock, 3*HZ);
25 #ifdef STK_DEBUG_PRINTF
26         printk(KERN_INFO "%s: ps input event %d cm, ps code = %dn",__func__, near_far_state, reading);
27 #endif
28     }
29     ret = stk3x1x_set_flag(ps_data, org_flag_reg, disable_flag);
30     if(ret < 0)
31     {
32         printk(KERN_ERR "%s:stk3x1x_set_flag fail, ret=%dn", __func__, ret);
33         goto err_i2c_rw;
34     }
35 
36     mutex_unlock(&ps_data->io_lock);
37     return;
38 
39 err_i2c_rw:
40     mutex_unlock(&ps_data->io_lock);
41     msleep(30);
42     return;
43 }

 2.3 建立sysfs介面:

為什麼要建立sysfs介面?在驅動層建立了sysfs介面,HAL層通過這些sysfs介面,對Sensor進行操作,如使能、設定delay等。

DEVICE_ATTR的使用:http://blog.csdn.net/njuitjf/article/details/16849333

函式巨集DEVICE_ATTR內封裝的是__ATTR(_name,_mode,_show,_stroe)方法:

_show:表示的是讀方法,_stroe表示的是寫方法。

1、 呼叫巨集DEVICE_ATTR完成對功能函式的註冊:

 1 static struct device_attribute ps_enable_attribute = __ATTR(enable,0664,stk_ps_enable_show,stk_ps_enable_store);
 2 static struct device_attribute ps_enable_aso_attribute = __ATTR(enableaso,0664,stk_ps_enable_aso_show,stk_ps_enable_aso_store);
 3 static struct device_attribute ps_distance_attribute = __ATTR(distance,0664,stk_ps_distance_show, stk_ps_distance_store);
 4 static struct device_attribute ps_offset_attribute = __ATTR(offset,0664,stk_ps_offset_show, stk_ps_offset_store);
 5 static struct device_attribute ps_code_attribute = __ATTR(code, 0444, stk_ps_code_show, NULL);
 6 static struct device_attribute ps_code_thd_l_attribute = __ATTR(codethdl,0664,stk_ps_code_thd_l_show,stk_ps_code_thd_l_store);
 7 static struct device_attribute ps_code_thd_h_attribute = __ATTR(codethdh,0664,stk_ps_code_thd_h_show,stk_ps_code_thd_h_store);
 8 static struct device_attribute recv_attribute = __ATTR(recv,0664,stk_recv_show,stk_recv_store);
 9 static struct device_attribute send_attribute = __ATTR(send,0664,stk_send_show, stk_send_store);
10 static struct device_attribute all_reg_attribute = __ATTR(allreg, 0444, stk_all_reg_show, NULL);
11 
12 static struct attribute *stk_ps_attrs [] =
13 {
14     &ps_enable_attribute.attr,
15     &ps_enable_aso_attribute.attr,
16     &ps_distance_attribute.attr,
17     &ps_offset_attribute.attr,
18     &ps_code_attribute.attr,
19     &ps_code_thd_l_attribute.attr,
20     &ps_code_thd_h_attribute.attr,
21     &recv_attribute.attr,
22     &send_attribute.attr,
23     &all_reg_attribute.attr,
24     NULL
25 };
26 
27 static struct attribute_group stk_ps_attribute_group = {
28     .attrs = stk_ps_attrs,
29 };

在probe函式中:

 1   err = sysfs_create_group(&ps_data->als_input_dev->dev.kobj, &stk_als_attribute_group);
 2     if (err < 0)
 3     {
 4         printk(KERN_ERR "%s:could not create sysfs group for alsn", __func__);
 5         goto err_als_input_allocate;
 6     }
 7     err = sysfs_create_group(&ps_data->ps_input_dev->dev.kobj, &stk_ps_attribute_group);
 8     if (err < 0)
 9     {
10         printk(KERN_ERR "%s:could not create sysfs group for psn", __func__);
11         goto err_ps_sysfs_create_group;
12     }

到此,完成了sysfs介面的建立,我們可以在根檔案系統中看到/sys/class/input/input1/目錄,在該目錄下我們可以看到多個節點,其中就包含了enable和delay。我們以enable為例子,可以有兩種方法完成對Gsensor的使能工作:

3、讀取上報資料:

在Android的HAL層,通過對/sys/class/input/input3/enable節點的寫操作,使能sensor。呼叫到的方法是stk_ps_enable_store函式:

 1 static struct device_attribute ps_enable_attribute = __ATTR(enable,0664,stk_ps_enable_show,stk_ps_enable_store);
 2 static struct device_attribute ps_enable_aso_attribute = __ATTR(enableaso,0664,stk_ps_enable_aso_show,stk_ps_enable_aso_store);
 3 static struct device_attribute ps_distance_attribute = __ATTR(distance,0664,stk_ps_distance_show, stk_ps_distance_store);
 4 static struct device_attribute ps_offset_attribute = __ATTR(offset,0664,stk_ps_offset_show, stk_ps_offset_store);
 5 static struct device_attribute ps_code_attribute = __ATTR(code, 0444, stk_ps_code_show, NULL);
 6 static struct device_attribute ps_code_thd_l_attribute = __ATTR(codethdl,0664,stk_ps_code_thd_l_show,stk_ps_code_thd_l_store);
 7 static struct device_attribute ps_code_thd_h_attribute = __ATTR(codethdh,0664,stk_ps_code_thd_h_show,stk_ps_code_thd_h_store);
 8 static struct device_attribute recv_attribute = __ATTR(recv,0664,stk_recv_show,stk_recv_store);
 9 static struct device_attribute send_attribute = __ATTR(send,0664,stk_send_show, stk_send_store);
10 static struct device_attribute all_reg_attribute = __ATTR(allreg, 0444, stk_all_reg_show, NULL);

 裡面的show和store函式;

 1 static ssize_t stk_ps_enable_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t size)
 2 {
 3     struct stk3x1x_data *ps_data =  dev_get_drvdata(dev);
 4     uint8_t en;
 5     if (sysfs_streq(buf, "1"))
 6         en = 1;
 7     else if (sysfs_streq(buf, "0"))
 8         en = 0;
 9     else
10     {
11         printk(KERN_ERR "%s, invalid value %dn", __func__, *buf);
12         return -EINVAL;
13     }
14     dev_dbg(dev, "%s: Enable PS : %dn", __func__, en);
15     mutex_lock(&ps_data->io_lock);
16     stk3x1x_enable_ps(ps_data, en);
17     mutex_unlock(&ps_data->io_lock);
18     return size;
19 }
 1 static int32_t stk3x1x_enable_ps(struct stk3x1x_data *ps_data, uint8_t enable)
 2 {
 3     int32_t ret;
 4     uint8_t w_state_reg;
 5     uint8_t curr_ps_enable;
 6     curr_ps_enable = ps_data->ps_enabled?1:0;
 7     if(curr_ps_enable == enable)
 8         return 0;
 9 
10     if (enable) {
11         ret = stk3x1x_device_ctl(ps_data, enable);
12         if (ret)
13             return ret;
14     }
15 
16     ret = i2c_smbus_read_byte_data(ps_data->client, STK_STATE_REG);
17     if (ret < 0)
18     {
19             printk(KERN_ERR "%s: write i2c error, ret=%dn", __func__, ret);
20         return ret;
21     }
22     w_state_reg = ret;
23     w_state_reg &= ~(STK_STATE_EN_PS_MASK | STK_STATE_EN_WAIT_MASK | 0x60);
24     if(enable)
25     {
26         w_state_reg |= STK_STATE_EN_PS_MASK;
27         if(!(ps_data->als_enabled))
28             w_state_reg |= STK_STATE_EN_WAIT_MASK;
29     }
30     ret = i2c_smbus_write_byte_data(ps_data->client, STK_STATE_REG, w_state_reg);
31     if (ret < 0)
32     {
33         printk(KERN_ERR "%s: write i2c error, ret=%dn", __func__, ret);
34         return ret;
35     }
36 
37     if(enable)
38     {
39 #ifdef STK_POLL_PS
40         hrtimer_start(&ps_data->ps_timer, ps_data->ps_poll_delay, HRTIMER_MODE_REL);    //定時一段時間後,開始開啟工作佇列
41         ps_data->ps_distance_last = -1;
42 #endif
43         ps_data->ps_enabled = true;
44 #ifndef STK_POLL_PS
45 #ifndef STK_POLL_ALS
46         if(!(ps_data->als_enabled))
47 #endif    /* #ifndef STK_POLL_ALS    */
48             enable_irq(ps_data->irq);
49         msleep(1);
50         ret = stk3x1x_get_flag(ps_data);
51         if (ret < 0)
52         {
53             printk(KERN_ERR "%s: read i2c error, ret=%dn", __func__, ret);
54             return ret;
55         }
56 
57         near_far_state = ret & STK_FLG_NF_MASK;
58         ps_data->ps_distance_last = near_far_state;
59         input_report_abs(ps_data->ps_input_dev, ABS_DISTANCE, near_far_state);
60         input_sync(ps_data->ps_input_dev);
61         wake_lock_timeout(&ps_data->ps_wakelock, 3*HZ);
62         reading = stk3x1x_get_ps_reading(ps_data);
63         dev_dbg(&ps_data->client->dev,
64             "%s: ps input event=%d, ps code = %dn",
65             __func__, near_far_state, reading);
66 #endif    /* #ifndef STK_POLL_PS */
67     }
68     else
69     {
70 #ifdef STK_POLL_PS
71         hrtimer_cancel(&ps_data->ps_timer);
72 #else
73 #ifndef STK_POLL_ALS
74         if(!(ps_data->als_enabled))
75 #endif
76             disable_irq(ps_data->irq);
77 #endif
78         ps_data->ps_enabled = false;
79     }
80     if (!enable) {
81         ret = stk3x1x_device_ctl(ps_data, enable);
82         if (ret)
83             return ret;
84     }
85 
86     return ret;
87 }
1 static enum hrtimer_restart stk_als_timer_func(struct hrtimer *timer)
2 {
3     struct stk3x1x_data *ps_data = container_of(timer, struct stk3x1x_data, als_timer);
4     queue_work(ps_data->stk_als_wq, &ps_data->stk_als_work);        //開啟工作佇列
5     hrtimer_forward_now(&ps_data->als_timer, ps_data->als_poll_delay);
6     return HRTIMER_RESTART;
7 }

那麼對於HAL層,將通過/dev/input/event1裝置節點讀取到sensor資料。到此,sensor驅動的工作流程完畢。應該很好理解吧!