> 시스템 튜토리얼 > 리눅스 > Linux 장치 드라이버 모델

Linux 장치 드라이버 모델

WBOY
풀어 주다: 2024-07-22 18:54:35
원래의
607명이 탐색했습니다.

Linux 장치 드라이버 모델

목차

리눅스 환경변수, 디바이스 드라이버 모델 1. 리눅스 디바이스 드라이버 모델의 유래

먼저 장치 드라이버 컴파일의 일반적인 프로세스를 검토합니다

【1】입력 함수 module_init()와 모듈 언로드 함수 module_exit()를 구현합니다.

【2】디바이스 번호,register_chrdev() 신청;------>(커널 관련)

【3】udev/mdev 메커니즘을 사용하여 장치 파일 노드 생성 class_create(), device_create();------>(커널 관련)

【4】하드웨어 초기화: 1.io 리소스 매핑 ioremap(), 커널은 gpio 라이브러리 기능을 제공합니다. 2. 인터럽트를 등록합니다. ------->(하드웨어 관련)

【5】file_option 구조 구축---------> (커널 관련)

[6] 하드웨어 운영 방법 xxx_open(), xxx_write()...

linux驱动编程_编程驱动聚水潭_编程驱动光谱仪

추론: 전체 프로세스의 네 번째 부분은 하드웨어와 관련될 뿐만 아니라 나머지도 유사한 작업입니다. 장치 드라이버를 편리하게(바퀴를 다시 만들 필요 없이) 컴파일하고 인력을 절약하기 위해 장치 드라이버 모델이 제안됩니다. 장치를 단순화하세요. 저작 과정을 촉진하세요.

2. 장치 드라이버 모델 프레임워크

sysfs 가상 파일 시스템을 통해 버스 개체를 볼 수 있습니다(USB를 예로 사용)

在这里插入图片描述

2Linux 드라이버 프로그래밍, BUS 버스 모델 프로그래밍

콘셉트맵

1. 버스 객체

【1】structbus_type: 버스 객체 Linux 교육 기관, 버스 설명, 장치 및 드라이버 관리Linux 드라이버 프로그래밍, 완벽하게 일치합니다.

 <span class="token keyword">struct</span> bus_type <span class="token punctuation">{</span><span class="token comment">//只列举重要成员</span>
	<span class="token keyword">const</span> <span class="token keyword">char</span>		<span class="token operator">*</span>name<span class="token punctuation">;</span> <span class="token comment">//总线名字</span>
	<span class="token keyword">int</span> <span class="token punctuation">(</span><span class="token operator">*</span>match<span class="token punctuation">)</span><span class="token punctuation">(</span><span class="token keyword">struct</span> device <span class="token operator">*</span>dev<span class="token punctuation">,</span> <span class="token keyword">struct</span> device_driver <span class="token operator">*</span>drv<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//匹配方法</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
로그인 후 복사

【2】등록 및 로그아웃 버스

<span class="token keyword">int</span> <span class="token function">bus_register</span><span class="token punctuation">(</span><span class="token keyword">struct</span> bus_type <span class="token operator">*</span>bus<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">void</span> <span class="token function">bus_unregister</span><span class="token punctuation">(</span><span class="token keyword">struct</span> bus_type <span class="token operator">*</span>bus<span class="token punctuation">)</span><span class="token punctuation">;</span>
로그인 후 복사

【3】버스 만들기

 <span class="token macro property">#<span class="token directive keyword">include</span> <span class="token string"></span></span>
<span class="token macro property">#<span class="token directive keyword">include</span> <span class="token string"></span></span>
<span class="token macro property">#<span class="token directive keyword">include</span> <span class="token string"></span></span>
<span class="token comment">//实例化一个bus对象</span>
<span class="token keyword">struct</span> bus_type mybus <span class="token operator">=</span> <span class="token punctuation">{</span>
	<span class="token punctuation">.</span>name <span class="token operator">=</span> <span class="token string">"mybus"</span><span class="token punctuation">,</span>
	<span class="token punctuation">.</span>match <span class="token operator">=</span> mybus_match<span class="token punctuation">,</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token function">EXPORT_SYMBOL</span><span class="token punctuation">(</span>mybus<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//导出总线对象,让device对象和driver对象使用</span>
	
<span class="token keyword">static</span> <span class="token keyword">int</span> __init <span class="token function">mybus_init</span><span class="token punctuation">(</span><span class="token keyword">void</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
	<span class="token function">printk</span><span class="token punctuation">(</span><span class="token string">"------%s---------n"</span><span class="token punctuation">,</span>__FUNCTION__<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">int</span> ret<span class="token punctuation">;</span>
	
	<span class="token comment">//构建一个总线结果:/sys/bus/mybus</span>
	ret <span class="token operator">=</span> <span class="token function">bus_register</span><span class="token punctuation">(</span><span class="token operator">&</span>mybus<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">if</span><span class="token punctuation">(</span>ret <span class="token operator">!=</span> <span class="token number">0</span><span class="token punctuation">)</span>
	<span class="token punctuation">{</span>
		<span class="token function">printk</span><span class="token punctuation">(</span><span class="token string">"bus_register errorn"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token keyword">return</span> ret<span class="token punctuation">;</span>
	<span class="token punctuation">}</span>
	<span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword">static</span> <span class="token keyword">void</span> __exit <span class="token function">mybus_exit</span><span class="token punctuation">(</span><span class="token keyword">void</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
	<span class="token function">printk</span><span class="token punctuation">(</span><span class="token string">"------%s---------n"</span><span class="token punctuation">,</span>__FUNCTION__<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token function">bus_unregister</span><span class="token punctuation">(</span><span class="token operator">&</span>mybus<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token function">module_init</span><span class="token punctuation">(</span>mybus_init<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">module_exit</span><span class="token punctuation">(</span>mybus_exit<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">MODULE_LICENSE</span><span class="token punctuation">(</span><span class="token string">"GPL"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
로그인 후 복사

2.장치 객체

【1】장치 객체: 주소, 인터럽트 및 기타 데이터를 포함한 장치 정보를 설명하는 장치 객체

<span class="token keyword">struct</span> device <span class="token punctuation">{</span><span class="token comment">//只列举重要成员</span>
	<span class="token keyword">struct</span> kobject kobj<span class="token punctuation">;</span> <span class="token comment">//所有对象的父类</span>
	<span class="token keyword">const</span> <span class="token keyword">char</span> <span class="token operator">*</span>init_name<span class="token punctuation">;</span><span class="token comment">//在/sys/bus/mybus/device的名字,用于在总线中匹配</span>
	<span class="token keyword">struct</span> bus_type	<span class="token operator">*</span>bus<span class="token punctuation">;</span>		 <span class="token comment">//指向该device对象依附的总线对象</span>
	<span class="token keyword">void</span> <span class="token operator">*</span>platform_data<span class="token punctuation">;</span>	 <span class="token comment">//自定义的数据,指向任何类型的数据</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
로그인 후 복사

【2】등록 및 로그아웃 버스

<span class="token keyword">int</span> <span class="token function">device_register</span><span class="token punctuation">(</span><span class="token keyword">struct</span> device <span class="token operator">*</span>dev<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">void</span> <span class="token function">device_unregister</span><span class="token punctuation">(</span><span class="token keyword">struct</span> device <span class="token operator">*</span>dev<span class="token punctuation">)</span><span class="token punctuation">;</span>
로그인 후 복사

【3】디바이스 객체 쓰기

linux驱动编程_编程驱动聚水潭_编程驱动光谱仪

定义一个描述设备的信息的结构体,匹配成功以后让driver对象在总线中领到device对象的信息,实现分离。

<span class="token macro property">#<span class="token directive keyword">include</span> <span class="token string"></span></span>
<span class="token macro property">#<span class="token directive keyword">include</span> <span class="token string"></span></span>
<span class="token macro property">#<span class="token directive keyword">include</span> <span class="token string"></span></span>
<span class="token keyword">extern</span> <span class="token keyword">struct</span> bus_type mybus<span class="token punctuation">;</span>
<span class="token keyword">struct</span> mydev_desc<span class="token punctuation">{</span>
	<span class="token keyword">char</span><span class="token operator">*</span> name<span class="token punctuation">;</span>
	<span class="token keyword">int</span> irqno<span class="token punctuation">;</span>
	<span class="token keyword">unsigned</span> <span class="token keyword">long</span> addr<span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token keyword">struct</span> mydev_desc deviofo <span class="token operator">=</span> <span class="token punctuation">{</span>
	<span class="token punctuation">.</span>name <span class="token operator">=</span> <span class="token string">"hqs"</span><span class="token punctuation">,</span>
	<span class="token punctuation">.</span>irqno <span class="token operator">=</span> <span class="token number">8877</span><span class="token punctuation">,</span>
	<span class="token punctuation">.</span>addr <span class="token operator">=</span> <span class="token number">0x12345678</span><span class="token punctuation">,</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token keyword">void</span> <span class="token function">mydev_release</span><span class="token punctuation">(</span><span class="token keyword">struct</span> device<span class="token operator">*</span> dev<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
	<span class="token function">printk</span><span class="token punctuation">(</span><span class="token string">"------%s---------n"</span><span class="token punctuation">,</span>__FUNCTION__<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token comment">//构建一个device对象</span>
<span class="token keyword">struct</span> device mydev <span class="token operator">=</span> <span class="token punctuation">{</span>
	<span class="token punctuation">.</span>init_name <span class="token operator">=</span> <span class="token string">"fsdev_drv"</span><span class="token punctuation">,</span>
	<span class="token punctuation">.</span>bus <span class="token operator">=</span> <span class="token operator">&</span>mybus<span class="token punctuation">,</span>
	<span class="token punctuation">.</span>release <span class="token operator">=</span> mydev_release<span class="token punctuation">,</span>
	<span class="token punctuation">.</span>platform_data <span class="token operator">=</span> <span class="token operator">&</span>deviofo<span class="token punctuation">,</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token keyword">static</span> <span class="token keyword">int</span> __init <span class="token function">mydev_init</span><span class="token punctuation">(</span><span class="token keyword">void</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
	<span class="token function">printk</span><span class="token punctuation">(</span><span class="token string">"------%s---------n"</span><span class="token punctuation">,</span>__FUNCTION__<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">int</span> ret<span class="token punctuation">;</span>
	<span class="token comment">//将device注册到总线中</span>
	ret <span class="token operator">=</span> <span class="token function">device_register</span><span class="token punctuation">(</span><span class="token operator">&</span>mydev<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">if</span><span class="token punctuation">(</span>ret <span class="token operator"><</span> <span class="token number">0</span><span class="token punctuation">)</span>
	<span class="token punctuation">{</span>
		<span class="token function">printk</span><span class="token punctuation">(</span><span class="token string">"device_register errorn"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token keyword">return</span> ret<span class="token punctuation">;</span>
	<span class="token punctuation">}</span>
	<span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword">static</span> <span class="token keyword">void</span> __exit <span class="token function">mydev_exit</span><span class="token punctuation">(</span><span class="token keyword">void</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
	<span class="token function">printk</span><span class="token punctuation">(</span><span class="token string">"------%s---------n"</span><span class="token punctuation">,</span>__FUNCTION__<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token function">device_unregister</span><span class="token punctuation">(</span><span class="token operator">&</span>mydev<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token function">module_init</span><span class="token punctuation">(</span>mydev_init<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">module_exit</span><span class="token punctuation">(</span>mydev_exit<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">MODULE_LICENSE</span><span class="token punctuation">(</span><span class="token string">"GPL"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
로그인 후 복사

3.driver对象

【1】driver对象:描述设备驱动发的方式

<span class="token keyword">struct</span> device_driver <span class="token punctuation">{</span><span class="token comment">//只列举重要成员</span>
	<span class="token keyword">const</span> <span class="token keyword">char</span>		<span class="token operator">*</span>name<span class="token punctuation">;</span><span class="token comment">//在/sys/bus/mybus/driver的名字,用于在总线中匹配</span>
	<span class="token keyword">struct</span> bus_type		<span class="token operator">*</span>bus<span class="token punctuation">;</span><span class="token comment">//指向该device对象依附的总线对象</span>
	<span class="token keyword">int</span> <span class="token punctuation">(</span><span class="token operator">*</span>probe<span class="token punctuation">)</span> <span class="token punctuation">(</span><span class="token keyword">struct</span> device <span class="token operator">*</span>dev<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//device和driver匹配之后要做的事情</span>
	<span class="token keyword">int</span> <span class="token punctuation">(</span><span class="token operator">*</span>remove<span class="token punctuation">)</span> <span class="token punctuation">(</span><span class="token keyword">struct</span> device <span class="token operator">*</span>dev<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//device和driver分离之后要做的事情</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
로그인 후 복사

【2】注册和注销总线

<span class="token keyword">int</span> <span class="token function">driver_register</span><span class="token punctuation">(</span><span class="token keyword">struct</span> device_driver <span class="token operator">*</span>drv<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">void</span> <span class="token function">driver_unregister</span><span class="token punctuation">(</span><span class="token keyword">struct</span> device_driver <span class="token operator">*</span>drv<span class="token punctuation">)</span><span class="token punctuation">;</span>
로그인 후 복사

【3】编写driver对象

匹配成功以后可以在总线中领到device对象的数据,具体实现在probe函数里。

<span class="token macro property">#<span class="token directive keyword">include</span> <span class="token string"></span></span>
<span class="token macro property">#<span class="token directive keyword">include</span> <span class="token string"></span></span>
<span class="token macro property">#<span class="token directive keyword">include</span> <span class="token string"></span></span>
<span class="token macro property">#<span class="token directive keyword">include</span> <span class="token string"></span></span>
<span class="token keyword">extern</span> <span class="token keyword">struct</span> bus_type mybus<span class="token punctuation">;</span>
<span class="token keyword">struct</span> mydev_desc<span class="token punctuation">{</span>
	<span class="token keyword">char</span><span class="token operator">*</span> name<span class="token punctuation">;</span>
	<span class="token keyword">int</span> irqno<span class="token punctuation">;</span>
	<span class="token keyword">unsigned</span> <span class="token keyword">long</span> addr<span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token keyword">struct</span> mydev_desc<span class="token operator">*</span> pdesc<span class="token punctuation">;</span>
<span class="token keyword">int</span> <span class="token function">mydrv_probe</span><span class="token punctuation">(</span><span class="token keyword">struct</span> device <span class="token operator">*</span>dev<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
	<span class="token function">printk</span><span class="token punctuation">(</span><span class="token string">"------%s---------n"</span><span class="token punctuation">,</span>__FUNCTION__<span class="token punctuation">)</span><span class="token punctuation">;</span>
	pdesc <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token keyword">struct</span> mydev_desc<span class="token operator">*</span><span class="token punctuation">)</span>dev<span class="token operator">-></span>platform_data<span class="token punctuation">;</span>
	<span class="token function">printk</span><span class="token punctuation">(</span><span class="token string">"name =%sn"</span><span class="token punctuation">,</span>pdesc<span class="token operator">-></span>name<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token function">printk</span><span class="token punctuation">(</span><span class="token string">"irqno = %dn"</span><span class="token punctuation">,</span>pdesc<span class="token operator">-></span>irqno<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">unsigned</span> <span class="token keyword">long</span> <span class="token operator">*</span> paddr <span class="token operator">=</span> <span class="token function">ioremap</span><span class="token punctuation">(</span>pdesc<span class="token operator">-></span>addr<span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword">int</span> mydrv_remove <span class="token punctuation">(</span><span class="token keyword">struct</span> device <span class="token operator">*</span>dev<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
	<span class="token function">printk</span><span class="token punctuation">(</span><span class="token string">"------%s---------n"</span><span class="token punctuation">,</span>__FUNCTION__<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token comment">//构建一个driver对象</span>
<span class="token keyword">struct</span> device_driver mydrv <span class="token operator">=</span> <span class="token punctuation">{</span>
	<span class="token punctuation">.</span>name <span class="token operator">=</span> <span class="token string">"fsdev_drv"</span><span class="token punctuation">,</span>
	<span class="token punctuation">.</span>bus <span class="token operator">=</span> <span class="token operator">&</span>mybus<span class="token punctuation">,</span>
	<span class="token punctuation">.</span>probe <span class="token operator">=</span> mydrv_probe<span class="token punctuation">,</span>
	<span class="token punctuation">.</span>remove <span class="token operator">=</span> mydrv_remove<span class="token punctuation">,</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token keyword">static</span> <span class="token keyword">int</span> __init <span class="token function">mydrv_init</span><span class="token punctuation">(</span><span class="token keyword">void</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
	<span class="token function">printk</span><span class="token punctuation">(</span><span class="token string">"------%s---------n"</span><span class="token punctuation">,</span>__FUNCTION__<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">int</span> ret<span class="token punctuation">;</span>
	<span class="token comment">//将device注册到总线中</span>
	ret <span class="token operator">=</span> <span class="token function">driver_register</span><span class="token punctuation">(</span><span class="token operator">&</span>mydrv<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">if</span><span class="token punctuation">(</span>ret <span class="token operator"><</span> <span class="token number">0</span><span class="token punctuation">)</span>
	<span class="token punctuation">{</span>
		<span class="token function">printk</span><span class="token punctuation">(</span><span class="token string">"driver_register errorn"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token keyword">return</span> ret<span class="token punctuation">;</span>
	<span class="token punctuation">}</span>
	<span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword">static</span> <span class="token keyword">void</span> __exit <span class="token function">mydrv_exit</span><span class="token punctuation">(</span><span class="token keyword">void</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
	<span class="token function">printk</span><span class="token punctuation">(</span><span class="token string">"------%s---------n"</span><span class="token punctuation">,</span>__FUNCTION__<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token function">driver_unregister</span><span class="token punctuation">(</span><span class="token operator">&</span>mydrv<span class="token punctuation">)</span><span class="token punctuation">;</span>	
<span class="token punctuation">}</span>
<span class="token function">module_init</span><span class="token punctuation">(</span>mydrv_init<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">module_exit</span><span class="token punctuation">(</span>mydrv_exit<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">MODULE_LICENSE</span><span class="token punctuation">(</span><span class="token string">"GPL"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
로그인 후 복사

4.device对象和driver对象匹配

【1】实现BUS对象中的match方式(按device对象的名子和driver对象中的名子相匹配)

要注意的是不能直接用device对象中的init_name,而要用device对象中继承的structkobjectkobj;上面的成员name不然会报错

<span class="token keyword">int</span> <span class="token function">mybus_match</span><span class="token punctuation">(</span><span class="token keyword">struct</span> device <span class="token operator">*</span>dev<span class="token punctuation">,</span> <span class="token keyword">struct</span> device_driver <span class="token operator">*</span>drv<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
	<span class="token comment">//如果匹配成功,match方法一定要返回一个1,失败返回0</span>
	<span class="token keyword">if</span><span class="token punctuation">(</span><span class="token operator">!</span> <span class="token function">strncmp</span><span class="token punctuation">(</span>drv<span class="token operator">-></span>name<span class="token punctuation">,</span>dev<span class="token operator">-></span>kobj<span class="token punctuation">.</span>name<span class="token punctuation">,</span><span class="token function">strlen</span><span class="token punctuation">(</span>drv<span class="token operator">-></span>name<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
	<span class="token punctuation">{</span>
		<span class="token function">printk</span><span class="token punctuation">(</span><span class="token string">"match okn"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token keyword">return</span> <span class="token number">1</span><span class="token punctuation">;</span>
	<span class="token punctuation">}</span>
	<span class="token keyword">else</span>
	<span class="token punctuation">{</span>	
		<span class="token function">printk</span><span class="token punctuation">(</span><span class="token string">"match failedn"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>	 
	<span class="token punctuation">}</span>
	<span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token comment">//实例化一个bus对象</span>
<span class="token keyword">struct</span> bus_type mybus <span class="token operator">=</span> <span class="token punctuation">{</span>
	<span class="token punctuation">.</span>name <span class="token operator">=</span> <span class="token string">"mybus"</span><span class="token punctuation">,</span>
	<span class="token punctuation">.</span>match <span class="token operator">=</span> mybus_match<span class="token punctuation">,</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
로그인 후 복사

【2】保证device对象和driver对象的名子一样:例如这儿都使用"fsdev_drv"才能保证能匹配成功

<span class="token comment">//构建一个device对象</span>
<span class="token keyword">struct</span> device mydev <span class="token operator">=</span> <span class="token punctuation">{</span>
	<span class="token punctuation">.</span>init_name <span class="token operator">=</span> <span class="token string">"fsdev_drv"</span><span class="token punctuation">,</span>
	<span class="token punctuation">.</span>bus <span class="token operator">=</span> <span class="token operator">&</span>mybus<span class="token punctuation">,</span>
	<span class="token punctuation">.</span>release <span class="token operator">=</span> mydev_release<span class="token punctuation">,</span>
	<span class="token punctuation">.</span>platform_data <span class="token operator">=</span> <span class="token operator">&</span>deviofo<span class="token punctuation">,</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
로그인 후 복사

<span class="token comment">//构建一个driver对象</span>
<span class="token keyword">struct</span> device_driver mydrv <span class="token operator">=</span> <span class="token punctuation">{</span>
	<span class="token punctuation">.</span>name <span class="token operator">=</span> <span class="token string">"fsdev_drv"</span><span class="token punctuation">,</span>
	<span class="token punctuation">.</span>bus <span class="token operator">=</span> <span class="token operator">&</span>mybus<span class="token punctuation">,</span>
	<span class="token punctuation">.</span>probe <span class="token operator">=</span> mydrv_probe<span class="token punctuation">,</span>
	<span class="token punctuation">.</span>remove <span class="token operator">=</span> mydrv_remove<span class="token punctuation">,</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
로그인 후 복사

위 내용은 Linux 장치 드라이버 모델의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

원천:itcool.net
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿