1、定义等待队列头部
wait_queue_head_t read_queue; //定义读等待队列头部
bool have_data ; /*表明设备有足够数据可供读*/
2、在设备初始化过程,初始化等待队列头部
a)为等待队列申请内存空间
b)init_waitqueue_head(&ts_usb_info->read_queue);//初始化等待队列
3、在open函数设置have_data的初始值
have_data = false
4、在read函数编写睡眠等待代码
while (!ts_usb_info->have_data)
{
if(filp->f_flags & O_NONBLOCK) //判断该文件时以阻塞方式还是非阻塞方式打开
return -EAGAIN;
wait_event_interruptible(ts_usb_info->read_queue,ts_usb_info->have_data);//have_data=true时,才会被完全唤醒
}
.........
have_data = false;
5、在数据产生后唤醒等待队列
wake_up(&ts_usb_info->read_queue);//唤醒读队列
have_data = true;//表示写入数据