⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 kernel_objects.html

📁 ADI 公司blackfin系列的用户使用文挡。
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<p> A Kset belongs to a subsystem, it has a type, a list of members and</p><pre class="code"> also contains a kobject of its own.</pre><p> Ksets also participate in helping with hotplug operations by</p><pre class="code"> specifying a hotplug name and adding items to the hotplug helper's  environment.</pre><pre class="code c">&nbsp;   <span class="kw4">struct</span> kset <span class="br0">&#123;</span>        <span class="kw4">struct</span> subsystem        * subsys;        <span class="kw4">struct</span> kobj_type        * ktype;        <span class="kw4">struct</span> list_head        list;        <span class="kw4">struct</span> kobject          kobj;        <span class="kw4">struct</span> kset_hotplug_ops * hotplug_ops;   <span class="br0">&#125;</span>;&nbsp;</pre></div><!-- SECTION [5640-6170] --><h3><a name="ktype_overview" id="ktype_overview">Ktype Overview</a></h3><div class="level3"><p> A Ktype defines the show and store operations that can be performed on the objectit also sets up the default attributes for the object.</p><pre class="code c">&nbsp;   <span class="kw4">struct</span> kobj_type <span class="br0">&#123;</span>        <span class="kw4">void</span> <span class="br0">&#40;</span>*release<span class="br0">&#41;</span><span class="br0">&#40;</span><span class="kw4">struct</span> kobject *<span class="br0">&#41;</span>;        <span class="kw4">struct</span> sysfs_ops        * sysfs_ops; <span class="co1">// just show and store</span>        <span class="kw4">struct</span> attribute        ** default_attrs;   <span class="br0">&#125;</span>;&nbsp;</pre></div><!-- SECTION [6171-6542] --><h3><a name="subsystem_overview" id="subsystem_overview">Subsystem Overview</a></h3><div class="level3"><p> This is a base level system it has a kset whose kobject allows the subsystem to be placed in the system hierarchy properly.</p><pre class="code">   struct subsystem {        struct kset             kset;        struct rw_semaphore     rwsem;   };</pre></div><!-- SECTION [6543-6821] --><h3><a name="confused" id="confused">Confused ??</a></h3><div class="level3"><p>An example will show the basic mechanics of setting up a small subsystem. </p><ul><li class="level1"><div class="li"> Remember Kobjects determine the position in the file system</div></li><li class="level1"><div class="li"> The KSET glues a kset to a subsystem</div></li></ul></div><!-- SECTION [6822-7032] --><h2><a name="kernel_object_sysfs_example" id="kernel_object_sysfs_example">Kernel Object / Sysfs Example</a></h2><div class="level2"><p>The example discussed will show the basics of setting up a subsystem of a single type of object with attributes.</p></div><h4><a name="the_computer_object" id="the_computer_object">The Computer Object</a></h4><div class="level4"><p>The important kernel entity here is the kobject, the attributes are</p><pre class="code"> defined by the use of the object.</pre><pre class="code c"><span class="co1">//</span><span class="co1">// Step 1 define an object</span><span class="co1">//</span>   <span class="kw4">struct</span> computer <span class="br0">&#123;</span>       <span class="kw4">struct</span> kobject kobj;  <span class="co1">// contains the name is also the directory entry</span>&nbsp;       <span class="co1">// user defined attributes follow</span>       <span class="kw4">char</span> make<span class="br0">&#91;</span><span class="nu0">16</span><span class="br0">&#93;</span>;       <span class="kw4">char</span> ip<span class="br0">&#91;</span><span class="nu0">16</span><span class="br0">&#93;</span>;       <span class="kw4">int</span> speed;       <span class="kw4">int</span> capacity;       <span class="kw4">int</span> used;   <span class="br0">&#125;</span>;&nbsp;</pre></div><h4><a name="define_the_attributes" id="define_the_attributes">Define the attributes</a></h4><div class="level4"><p>The attributes of the object will appear as regular files</p><pre class="code">  permissions and names need to be set at this stage.  The only other element in the attribute structure is the  module owner.</pre><p>Note that no show / store access functions are defined in basic attributes.</p><pre class="code c"><span class="co1">//</span><span class="co1">// Step 2 define its attributes</span><span class="co1">//</span><span class="co2">#define COM_MODE ( S_IRUGO | S_IWUGO )</span><span class="kw4">static</span> <span class="kw4">struct</span> attribute computer_attr_make =<span class="br0">&#123;</span>.<span class="me1">name</span>=<span class="st0">"make"</span>,.<span class="me1">mode</span>=COM_MODE <span class="br0">&#125;</span>;<span class="kw4">static</span> <span class="kw4">struct</span> attribute computer_attr_ip   =<span class="br0">&#123;</span>.<span class="me1">name</span>=<span class="st0">"ip"</span>,  .<span class="me1">mode</span>=COM_MODE <span class="br0">&#125;</span>;<span class="kw4">static</span> <span class="kw4">struct</span> attribute computer_attr_speed=<span class="br0">&#123;</span>.<span class="me1">name</span>=<span class="st0">"speed"</span>,.<span class="me1">mode</span>=COM_MODE <span class="br0">&#125;</span>;<span class="kw4">static</span> <span class="kw4">struct</span> attribute computer_attr_capacity=<span class="br0">&#123;</span>.<span class="me1">name</span>=<span class="st0">"capacity"</span>,.<span class="me1">mode</span>=COM_MODE<span class="br0">&#125;</span>;<span class="kw4">static</span> <span class="kw4">struct</span> attribute computer_attr_used = <span class="br0">&#123;</span>.<span class="me1">name</span>=<span class="st0">"used"</span>,.<span class="me1">mode</span>=COM__MODE <span class="br0">&#125;</span>;<span class="co1">//</span><span class="co1">//</span><span class="co1">// Now build an attribute structure as a null terminated list</span><span class="co1">//</span><span class="kw4">static</span> <span class="kw4">struct</span> attribute* computer_default_attrs<span class="br0">&#91;</span><span class="br0">&#93;</span> = <span class="br0">&#123;</span>    &amp;computer_attr_make,    &amp;computer_attr_ip,    &amp;computer_attr_speed,    &amp;computer_attr_capacity,    &amp;computer_attr_used,    <span class="kw2">NULL</span><span class="br0">&#125;</span>;&nbsp;</pre></div><h4><a name="define_the_object_type" id="define_the_object_type">Define the object type</a></h4><div class="level4"><p> The sysfs_ops table will contain the show and store routines</p><p>The release routine will tell the system how to remove the object.</p><p>The type also defines the default attributes for the object.</p><pre class="code c">&nbsp;<span class="co1">//</span><span class="co1">// Now we can define the type</span><span class="co1">//</span><span class="kw4">static</span> <span class="kw4">struct</span> kobj_type computer_ktype = <span class="br0">&#123;</span>    .<span class="me1">release</span> = computer_release,    .<span class="me1">sysfs_ops</span> = &amp;computer_sysfs_ops,    .<span class="me1">default_attrs</span> = computer_default_attrs<span class="br0">&#125;</span>;&nbsp;</pre></div><h4><a name="define_the_subsystem_type" id="define_the_subsystem_type">Define the subsystem  type</a></h4><div class="level4"><p> The subsystem type simply contains the notifier and clean up routine used when the system is deleted.</p><p>Again the sysfs_ops table will contain the show and store routines</p><p>The release routine will tell the system how to remove the object.</p><p>The type also defines the default attributes for th object.</p><pre class="code c">&nbsp;<span class="kw4">static</span> <span class="kw4">struct</span> kobj_type sysfs_computer_ktype = <span class="br0">&#123;</span>    .<span class="me1">release</span> = sysfs_computer_release,    .<span class="me1">sysfs_ops</span> = <span class="kw2">NULL</span>,    .<span class="me1">default_attrs</span> = <span class="kw2">NULL</span><span class="br0">&#125;</span>;&nbsp;</pre></div><h4><a name="define_the_set" id="define_the_set">Define the Set</a></h4><div class="level4"><p> In this example a static definition is used for a set.</p><p>All the generated objects will be added to this set which will also be used  by the subsystem to find its components.</p><pre class="code c">&nbsp;   <span class="kw4">static</span> <span class="kw4">struct</span> kset computer_set;&nbsp;</pre></div><h4><a name="declare_the_subsystem" id="declare_the_subsystem">Declare the Subsystem</a></h4><div class="level4"><p> The decl_subsystem helper function can be used here</p><pre class="code c">&nbsp;<span class="co1">// The following macro completes the definition of the subsystem</span>decl_subsys<span class="br0">&#40;</span>sysfs_computer, &amp;sysfs_computer_ktype, <span class="nu0">0</span> <span class="br0">&#41;</span>;&nbsp;<span class="co1">//or by hand ..</span>&nbsp;<span class="kw4">struct</span> subsystem sysfs_computer_subsys = <span class="br0">&#123;</span>        .<span class="me1">kset</span> = <span class="br0">&#123;</span>                .<span class="me1">kobj</span> = <span class="br0">&#123;</span> .<span class="me1">name</span> = __stringify<span class="br0">&#40;</span>sysfs_computer<span class="br0">&#41;</span> <span class="br0">&#125;</span>,                .<span class="me1">ktype</span> = &amp;sysfs_computer_ktype,                .<span class="me1">hotplug_ops</span> =<span class="nu0">0</span>        <span class="br0">&#125;</span><span class="br0">&#125;</span>;</pre></div><!-- SECTION [7033-10334] --><h3><a name="define_the_kset" id="define_the_kset">Define the KSET</a></h3><div class="level3"><p> <strong></strong> The KOBJECT SET is the key to the whole system <strong></strong></p><pre class="code c">&nbsp;    <span class="kw4">char</span> * name = <span class="st0">"computers"</span>;    kobject_set_name<span class="br0">&#40;</span>&amp;computer_set-&gt;kobj, <span class="st0">"%s"</span>, name<span class="br0">&#41;</span>;&nbsp;    <span class="co1">// associate the set with a subsystem</span>    computer_set-&gt;subsys = &amp;sysfs_computer_subsys;&nbsp;    <span class="co1">// define the type of the set</span>    computer_set-&gt;kobj.<span class="me1">ktype</span> = &amp;sysfs_computer_ktype;&nbsp;    <span class="co1">// define the type of objects contained in the set</span>    computer_set-&gt;ktype = &amp;computer_ktype;&nbsp;    <span class="co1">// No hotplug for this object</span>    computer_set-&gt;hotplug_ops = <span class="kw2">NULL</span>;&nbsp;   <span class="co1">// register the set to make it appear</span>   kset_register<span class="br0">&#40;</span>computer_set<span class="br0">&#41;</span>;&nbsp;</pre></div><h4><a name="register_the_subsystem" id="register_the_subsystem">Register the Subsystem</a></h4><div class="level4"><p>This will make sysfs aware of the subsystem.</p><pre class="code c">&nbsp;        subsystem_register<span class="br0">&#40;</span>&amp;sysfs_computer_subsys<span class="br0">&#41;</span>;&nbsp;</pre></div><h4><a name="initialize_each_object_s_system_components" id="initialize_each_object_s_system_components">Initialize each object's system components</a></h4><div class="level4"><p>This sets up the object in the sysfs hierarchy</p><pre class="code c">&nbsp;    <span class="co1">// create a new computer</span>    <span class="kw4">struct</span> computer * computer = <span class="br0">&#40;</span><span class="kw4">struct</span> computer*<span class="br0">&#41;</span>kmalloc<span class="br0">&#40;</span>        <span class="kw4">sizeof</span><span class="br0">&#40;</span><span class="kw4">struct</span> computer<span class="br0">&#41;</span>,GFP_KERNEL<span class="br0">&#41;</span>;    <span class="kw1">if</span> <span class="br0">&#40;</span> <span class="kw2">NULL</span> == computer <span class="br0">&#41;</span> <span class="kw1">return</span> <span class="kw2">NULL</span>;    memset<span class="br0">&#40;</span>computer, 0x00, <span class="kw4">sizeof</span><span class="br0">&#40;</span><span class="kw4">struct</span> computer<span class="br0">&#41;</span><span class="br0">&#41;</span>;&nbsp;    kobject_set_name<span class="br0">&#40;</span>&amp;computer-&gt;kobj, <span class="st0">"%s"</span> , name<span class="br0">&#41;</span>;    computer-&gt;kobj.<span class="me1">parent</span> = &amp;computer_set.<span class="me1">kobj</span>;    computer-&gt;kobj.<span class="me1">kset</span> = &amp;computer_set;    computer-&gt;kobj.<span class="me1">ktype</span> = computer_set.<span class="me1">ktype</span>;&nbsp;</pre></div><h4><a name="initialize_each_object_s_attributes" id="initialize_each_object_s_attributes">Initialize each object's attributes</a></h4><div class="level4"><p>Having established the object&rsquo;s place in the hierarchy now set its attributes.</p><pre class="code c">&nbsp;    strncpy<span class="br0">&#40;</span>computer-&gt;make,make,<span class="nu0">16</span><span class="br0">&#41;</span>;    strncpy<span class="br0">&#40;</span>computer-&gt;ip,ip,<span class="nu0">16</span><span class="br0">&#41;</span>;    computer-&gt;speed = speed;    computer-&gt;capacity = capacity;    computer-&gt;used = used;&nbsp;</pre></div><h4><a name="register_the_object" id="register_the_object">Register the object</a></h4><div class="level4"><p>This step will expose the object to the sysfs system.</p><pre class="code c">&nbsp;    kobject_register<span class="br0">&#40;</span>&amp;computer-&gt;kobj<span class="br0">&#41;</span>;&nbsp;</pre><p> &lt;h3&gt; Clean up &lt;/h3&gt;</p><p>&lt;p&gt; This step will remove  object from the sysfs system.</p><pre class="code c">&nbsp;        <span class="kw4">struct</span> kobject * kobj = <span class="kw2">NULL</span>;        <span class="kw1">while</span><span class="br0">&#40;</span>!list_empty<span class="br0">&#40;</span>&amp;computer_set.<span class="me1">list</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>            kobj = <span class="br0">&#40;</span><span class="kw4">struct</span> kobject *<span class="br0">&#41;</span> list_entry<span class="br0">&#40;</span>computer_set.<span class="me1">list</span>.<span class="me1">next</span>,                                                 <span class="kw4">struct</span> kobject, entry<span class="br0">&#41;</span>;            kobject_unregister<span class="br0">&#40;</span>kobj<span class="br0">&#41;</span>;&nbsp;        <span class="br0">&#125;</span>        kset_unregister<span class="br0">&#40;</span>&amp;computer_set<span class="br0">&#41;</span>;        subsystem_unregister<span class="br0">&#40;</span>&amp;sysfs_computer_subsys<span class="br0">&#41;</span>;&nbsp;</pre></div><!-- SECTION [10335-12577] --><h3><a name="the_system_in_action" id="the_system_in_action">The system in action</a></h3><div class="level3"><p> Here is an example of this system in action</p><p>&lt;code&gt;</p><p>/sys/sysfs_computer/ `&ndash; computers</p><pre class="code">  |-- comp_node_0  |   |-- capacity  |   |-- ip  |   |-- make  |   |-- speed  |   `-- used</pre><p>[...] </p><pre class="code">  `-- comp_node_4      |-- capacity      |-- ip      |-- make      |-- speed      `-- used</pre></div><!-- SECTION [12578-] --></body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -