工作队列允许内核代码请求某个函数在未来的时间被调用,具有很好的灵活性,其运行在特殊的内核线程,该线程没有对应的用户空间可以访问,因此,工作队列函数不能访问用户空间,但是可以休眠。
TP的私有数据结构:
TP的私有数据结构:
struct msg21xx_ts_data {
int irq;
struct work_struct work;
struct i2c_client *client;
struct workqueue_struct *msg21xx_wq;
};
probe函数中与工作队列有关的部分摘录如下:static int msg21xx_ts_probe(struct i2c_client *client,
const struct i2c_device_id *id)
{
struct msg21xx_ts_data *data;
data = kzalloc(sizeof(struct msg21xx_ts_data), GFP_KERNEL);
data->msg21xx_wq = create_singlethread_workqueue("msg21xx_wq");
INIT_WORK(&data->work, msg21xx_ts_work_func);
request_threaded_irq(client->irq, NULL, msg21xx_irq_handler,
IRQF_TRIGGER_RISING | IRQF_ONESHOT,
client->dev.driver->name, data);
}
当TP发生中断时,msg21xx_irq_handler函数被调用,改函数会调用queue_work像工作队列提交任务:
static irqreturn_t msg21xx_irq_handler(int irq, void *dev_id)
{
struct msg21xx_ts_data *data = (struct msg21xx_ts_data *)dev_id;
disable_irq_nosync(data->irq);
queue_work(data->msg21xx_wq, &data->work);
return IRQ_HANDLED;
}
执行queue_work(data->msg21xx_wq, &data->work);后,msg21xx_ts_work_func函数会被调用,该函数完成触摸数据的读取和上报。