<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>憂藍夢境‧部落格 &#187; 驅動程式</title>
	<atom:link href="http://blog.linym.net/archives/tag/%e9%a9%85%e5%8b%95%e7%a8%8b%e5%bc%8f/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.linym.net</link>
	<description>我的學習心得、筆記</description>
	<lastBuildDate>Fri, 09 Dec 2011 12:33:34 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Linux 自動配置主設備號</title>
		<link>http://blog.linym.net/archives/176</link>
		<comments>http://blog.linym.net/archives/176#comments</comments>
		<pubDate>Sat, 08 Sep 2007 16:58:58 +0000</pubDate>
		<dc:creator>lym520</dc:creator>
				<category><![CDATA[Embedded]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[driver]]></category>
		<category><![CDATA[驅動程式]]></category>

		<guid isPermaLink="false">http://blog.linym.net/archives/176</guid>
		<description><![CDATA[在撰寫 Linux 設備驅動程式的時候，如果指定主設備號(MAJOR)為 0，就可以利用 alloc_chrdev_region() 函式讓系統自動分配一個可用 MAJOR 給裝置，程式片段： #define DEV_NAME "LED" //設備名稱 int DEV_MAJOR = 0; //主設備號 int DEV_MINOR = 0; //次設備號 int count = 1; dev_t led_dev; /* Dynamic assign major */ result = alloc_chrdev_region(&#038;led_dev, DEV_MAJOR, count, DEV_NAME); DEV_MAJOR = MAJOR(led_dev); 這樣驅動寫好後載入核心執行，就會分配到一個 MAJOR，可利用 cat /proc/devices 查看，但是如果要存取這個設備，還需要手動用 mknod 在 /dev 建立設備檔，但問題是怎麼知道分配到的設備號碼是什麼？這裡利用一個 shell script 搭配 awk [...]]]></description>
			<content:encoded><![CDATA[<p>在撰寫 Linux 設備驅動程式的時候，如果指定主設備號(MAJOR)為 0，就可以利用 alloc_chrdev_region() 函式讓系統自動分配一個可用 MAJOR 給裝置，程式片段：</p>
<pre>
#define DEV_NAME "LED" //設備名稱
int DEV_MAJOR = 0; //主設備號
int DEV_MINOR = 0; //次設備號
int count = 1;
dev_t led_dev;
/*  Dynamic assign major */
result = alloc_chrdev_region(&#038;led_dev, DEV_MAJOR, count, DEV_NAME);
DEV_MAJOR = MAJOR(led_dev);
</pre>
<p>這樣驅動寫好後載入核心執行，就會分配到一個 MAJOR，可利用 cat /proc/devices 查看，但是如果要存取這個設備，還需要手動用 mknod 在 /dev 建立設備檔，但問題是怎麼知道分配到的設備號碼是什麼？這裡利用一個 shell script 搭配 awk 來達成。</p>
<pre title="code" class="c">

#!/bin/sh
module = &quot;LED&quot; // /proc/device 顯示的名稱
device = &quot;LED&quot; // /dev 要建立的設備檔名稱

major=`cat /proc/devices | awk &quot;\\$2==\&quot;$module\&quot; {print \\$1}&quot;`
mknod /dev/$device c $major 0 //建立字元設備
</pre>
<p>然後只要將這個 script 檔案設成啟動時執行就可以囉！</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.linym.net/archives/176/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

