在drivers/mmc下面是mmc卡,SD卡和SDIO卡驅動部分,其中包括host驅動,card驅動和core部分,因爲網絡接口卡掛接在SDIO總線上,因此在此以前咱們先看一下SDIO的驅動結構。其驅動在drivers/mmc目錄下的結構爲: linux
|-- mmc android
| |-- card ios
| |-- core 網絡
| |-- host less
主要關注的目錄是core目錄,這個目錄是真個驅動的核心目錄,是媒體卡的通用代碼部分,包括core.c,host.c和sdio.c等。 CORE 層完成了不一樣協議和規範的實現,併爲HOST 層的驅動提供了接口函數,該目錄完成sdio總線的註冊操做,相應的ops操做,以及支持mmc的代碼。詳細的狀況將在函數接口部分詳細討論。 ide
Host目錄是不一樣平臺根據平臺的特性而編寫的host驅動。 函數
全球有線和無線通訊半導體市場的領導者Broadcom(博通)公司(Nasdaq:BRCM)宣佈,推出最新無線組合芯片BCM4330,該芯片 可支持更多媒體形式和數據應用,且不會增大智能手機、平板電腦及其餘移動設備的尺寸或縮短其電池壽命。BCM4330在單個芯片上集成了業界領先的 Broadcom 802.11n Wi-Fi、藍牙和FM無線技術,與分立式半導體器件組成的解決方案相比,在成本、尺寸、功耗和性能上有顯著優點,是移動設備的理想選擇。 性能
BCM4330採用了新的Wi-Fi和藍牙標準,可支持新的、使人振奮的應用。例如,Broadcom BCM4330是業界第一款通過藍牙4.0標準認證的組合芯片解決方案, 集成了藍牙低功耗(BLE)標準。該標準使藍牙技術能以超低功耗運行,所以BCM4330很是適用於須要很長電池壽命的系統,如無線傳感器、醫療和健身監 控設備等。BCM4330還支持Wi-Fi Direct™和藍牙高速(HS)標準,所以採用BCM4330的移動設備能直接相互通訊,而沒必要先鏈接到接入點、成爲傳統網絡的一部分,從而爲不少無線 設備之間新的應用和使用模式創造了機會。 ui
Broadcom一直支持全部主流的操做系統(OS)平臺,如MicrosoftWindows和Windows Phone、Google Chrome、Android等等,並且不只是BCM4330,全部藍牙、WLAN和GPS芯片組都提供這樣的支持。 this
Bcm4330驅動源碼通常被廠商單獨提供,若是要在開發的LINUX系統中(固然它還支持多種平臺)使用該源碼,能夠添加到linux kernel源碼樹裏,也能夠單獨組織存放,能夠直接編譯到kernel,也能夠編譯成模塊,而後再系統啓動的流程中或其餘適當的實際加載到kernel 中,通常建議單獨組織並編譯成模塊在須要的時候加載如kernel。
|-- src
| |-- bcmsdio
| |-- dhd
| |--dongle
| |--include
| |-- shared
| |-- wl
這裏主要內容到bcmsdio,dhd和wl三個目錄下,bcm4330驅動的入口在dhd/sys/dhd_linux.c文件中的dhd_module()函數,設備的初始化和相關驅動註冊都從這裏開始,
以boardcom bcm4329芯片驅動爲例,相應的函數流程圖以下:
Platform_driver_register(wifi_device[_legacy])的調用將wifi_device[_legacy]驅動註冊到系統中,wifi_device_legacy是爲了兼容老版本的驅動。
Path: wl/sys/wl_android.c
Static struct Platform_driver wifi_device ={
.probe = wifi_probe
.remove = wifi_remove
.suspend = wifi_supend
.resume = wifi_resume
.driver = {
.name = 「bcmdhd_wlan」
}
}
Static struct Platform_driver wifi_device_legacy ={
.probe = wifi_probe
.remove = wifi_remove
.suspend = wifi_supend
.resume = wifi_resume
.driver = {
.name = 「bcm4329_wlan」
}
}
上面的展現了wifi平臺設備驅動的註冊過程,那麼在平臺相關的代碼區應該有wifi做爲平臺設備被初始化和註冊的地方:
Path: kernel/arch/arm/mach-msm/msm_
static struct resource mahimahi_wifi_resources[] = {
[0] = {
.name = "bcm4329_wlan_irq",
.start =MSM_GPIO_TO_INT(MAHIMAHI_GPIO_WIFI_IRQ),
.end = MSM_GPIO_TO_INT(MAHIMAHI_GPIO_WIFI_IRQ),
.flags = IORESOURCE_IRQ |IORESOURCE_IRQ_HIGHLEVEL | IORESOURCE_IRQ_SHAREABLE,
},
};
static structwifi_platform_data mahimahi_wifi_control = {
.set_power = mahimahi_wifi_power,
.set_reset = mahimahi_wifi_reset,
.set_carddetect = mahimahi_wifi_set_carddetect,
.mem_prealloc = mahimahi_wifi_mem_prealloc,
};
static struct platform_device mahimahi_wifi_device = {
.name = "bcm4329_wlan",
.id = 1,
.num_resources = ARRAY_SIZE(mahimahi_wifi_resources),
.resource = mahimahi_wifi_resources,
.dev = {
.platform_data = &mahimahi_wifi_control,
},
};
上面是對wifi_device設備的初始化,下面是對該設備的註冊:
static int __initmahimahi_wifi_init(void)
{
int ret;
if (!machine_is_mahimahi())
return 0;
printk("%s: start\n",__func__);
mahimahi_wifi_update_nvs("sd_oobonly=1\r\n", 0);
mahimahi_wifi_update_nvs("btc_params70=0x32\r\n", 1);
mahimahi_init_wifi_mem();
ret = platform_device_register(&mahimahi_wifi_device);
return ret;
}
late_initcall(mahimahi_wifi_init); //代表在系統啓動的後期會自動調用加載該模塊
這樣,經過上面的初始化和註冊流程,wifi設備做爲平臺設備和驅動就能夠握手成功了,這裏的平臺驅動只是對wifi設備的簡單管理,如對wifi設備的掛起和恢復等操做了。可是在wifi設備初始化以前是不可以被掛起和恢復的,那麼wifi設備是如何初始化的呢?
Path: wl/sys/wl_android.c
static int wifi_probe(structplatform_device *pdev)
{
struct wifi_platform_data *wifi_ctrl =
(structwifi_platform_data *)(pdev->dev.platform_data);
DHD_ERROR(("## %s\n",__FUNCTION__));
wifi_irqres = platform_get_resource_byname(pdev,IORESOURCE_IRQ, "bcmdhd_wlan_irq");
if (wifi_irqres == NULL)
wifi_irqres =platform_get_resource_byname(pdev,
IORESOURCE_IRQ,"bcm4329_wlan_irq");
wifi_control_data = wifi_ctrl;
wifi_set_power(1,0); /* Power On */
wifi_set_carddetect(1); /* CardDetect (0->1) */
up(&wifi_control_sem);
return 0;
}
這是wifi平臺設備驅動註冊時成功匹配wifi設備後調用的函數wifi_probe(),它的主要工做就是從wifi設備中獲取終端資源,並獲 取wifi_platform_data類型結構賦予wifi_control_data變量,這一步很重要,下面就能夠看出了它的重要性。而後調用 wifi_set_power和wifi_set_carddetect函數給wifi芯片上電並檢測。
int wifi_set_power(int on, unsignedlong msec)
{
DHD_ERROR(("%s = %d\n",__FUNCTION__, on));
if (wifi_control_data &&wifi_control_data->set_power) {
wifi_control_data->set_power(on);
}
if (msec)
msleep(msec);
return 0;
}
Wifi_set_power函數中調用 wifi_control_data->set_power(on),wifi_control_data就是剛纔說的那個重要變量,注意它是從 wifi_device平臺設備的wifi_platform_data獲取的,那麼看看上面的wifi_device初始化的代碼:
static struct platform_device mahimahi_wifi_device = {
.name = "bcm4329_wlan",
.id = 1,
.num_resources = ARRAY_SIZE(mahimahi_wifi_resources),
.resource = mahimahi_wifi_resources,
.dev = {
.platform_data =&mahimahi_wifi_control,
},
};
static struct wifi_platform_datamahimahi_wifi_control= {
.set_power = mahimahi_wifi_power,
.set_reset = mahimahi_wifi_reset,
.set_carddetect = mahimahi_wifi_set_carddetect,
.mem_prealloc = mahimahi_wifi_mem_prealloc,
};
因此它實際調用的是mahimahi_wifi_power函數,該函數的定義在kernel/arch/arm /mach-msm/board-mahimahi-mmc.c之中:
int mahimahi_wifi_power(int on)
{
printk("%s: %d\n", __func__, on);
if (on) {
config_gpio_table(wifi_on_gpio_table,
ARRAY_SIZE(wifi_on_gpio_table));
mdelay(50);
} else {
config_gpio_table(wifi_off_gpio_table,
ARRAY_SIZE(wifi_off_gpio_table));
}
mdelay(100);
gpio_set_value(MAHIMAHI_GPIO_WIFI_SHUTDOWN_N,on); /* WIFI_SHUTDOWN */
mdelay(200);
mahimahi_wifi_power_state = on;
return 0;
}
調用gpio_set_value操做wifi芯片,給wifi芯片上電。那麼來看看wifi_set_ carddetect函數究竟幹了什麼:
Path:wl/sys/wl_android.c
static int wifi_set_carddetect(int on)
{
DHD_ERROR(("%s = %d\n", __FUNCTION__, on));
if(wifi_control_data && wifi_control_data->set_carddetect) {
wifi_control_data->set_carddetect(on);
}
return 0;
}
一樣會調用wifi_device的mahimahi_wifi_set_carddetect函數:
Path:kernel/arch/arm/mach-msm/board-mahimahi-mmc.c
int mahimahi_wifi_set_carddetect(int val)
{
pr_info("%s: %d\n", __func__, val);
mahimahi_wifi_cd = val;
if(wifi_status_cb) {
wifi_status_cb(val,wifi_status_cb_devid);
} else
pr_warning("%s: Nobody to notify\n", __func__);
return 0;
}
Wifi_status_cb代碼:
static int mahimahi_wifi_status_register(
void (*callback)(intcard_present, void *dev_id),
void *dev_id)
{
if (wifi_status_cb)
return -EAGAIN;
wifi_status_cb = callback;
wifi_status_cb_devid = dev_id;
return 0;
}
static unsigned intmahimahi_wifi_status(struct device *dev)
{
return mahimahi_wifi_cd;
}
static structmmc_platform_data mahimahi_wifi_data = {
.ocr_mask = MMC_VDD_28_29,
.built_in = 1,
.status = mahimahi_wifi_status,
.register_status_notify= mahimahi_wifi_status_register,
.embedded_sdio = &mahimahi_wifi_emb_data,
};
由上面代碼;不難看出會有個地方調用mahimahi_wifi_status_register設置wifi_status_cb這個回調函數,能夠跟蹤這個mahimahi_wifi_data結構體,來看看它被傳遞給了誰:
int msm_add_sdcc(unsigned intcontroller, struct mmc_platform_data *plat,
unsigned int stat_irq,unsigned long stat_irq_flags);
int __initmahimahi_init_mmc(unsigned int sys_rev, unsigned debug_uart)
{
……
msm_add_sdcc(1, &mahimahi_wifi_data, 0, 0);
……
if (system_rev > 0)
msm_add_sdcc(2,&mahimahi_sdslot_data, 0, 0);
else {
mahimahi_sdslot_data.status =mahimahi_sdslot_status_rev0;
mahimahi_sdslot_data.register_status_notify = NULL;
set_irq_wake(MSM_GPIO_TO_INT(MAHIMAHI_GPIO_SDMC_CD_REV0_N), 1);
msm_add_sdcc(2, &mahimahi_sdslot_data,
……
}
能夠跟蹤到這裏Path:kernel/arch/arm/mach-msm/devices-msm7x30.c
struct platform_device msm_device_sdc1 = {
.name = "msm_sdcc",
.id = 1,
.num_resources = ARRAY_SIZE(resources_sdc1),
.resource = resources_sdc1,
.dev = {
.coherent_dma_mask =0xffffffff,
},
};
struct platform_device msm_device_sdc2 = {
.name = "msm_sdcc",
.id = 2,
.num_resources = ARRAY_SIZE(resources_sdc2),
.resource = resources_sdc2,
.dev = {
.coherent_dma_mask =0xffffffff,
},
};
struct platform_devicemsm_device_sdc3 = {
.name = "msm_sdcc",
.id = 3,
.num_resources = ARRAY_SIZE(resources_sdc3),
.resource = resources_sdc3,
.dev = {
.coherent_dma_mask = 0xffffffff,
},
};
struct platform_device msm_device_sdc4= {
.name = "msm_sdcc",
.id = 4,
.num_resources = ARRAY_SIZE(resources_sdc4),
.resource = resources_sdc4,
.dev = {
439,2-16 62%
.coherent_dma_mask = 0xffffffff,
},
};
static struct platform_device *msm_sdcc_devices[] __initdata = {
&msm_device_sdc1,
&msm_device_sdc2,
&msm_device_sdc3,
&msm_device_sdc4,
};
int __initmsm_add_sdcc(unsigned int controller, struct mmc_platform_data *plat,
unsigned int stat_irq,unsigned long stat_irq_flags)
{
……
pdev =msm_sdcc_devices[controller-1]; //由於傳過來的controller是1,因此下面註冊的是第一個平臺設備
pdev->dev.platform_data= plat; //被傳遞給平臺設備的platform_data
res =platform_get_resource_byname(pdev, IORESOURCE_IRQ, "status_irq");
if (!res)
return -EINVAL;
else if (stat_irq) {
res->start = res->end =stat_irq;
res->flags &=~IORESOURCE_DISABLED;
res->flags |=stat_irq_flags;
}
return platform_device_register(pdev); //如上所述
}
那麼這個平臺設備是什麼呢,就是sd卡控制器,也就是前面說的host驅動所驅動的主機控制設備。
Path: drivers/mmc/host/msm_sdcc.c
static struct platform_drivermsmsdcc_driver = {
.probe = msmsdcc_probe,
.suspend = msmsdcc_suspend,
.resume = msmsdcc_resume,
.driver = {
.name = "msm_sdcc",
},
};
static int __initmsmsdcc_init(void)
{
return platform_driver_register(&msmsdcc_driver);
}
驅動成功匹配設備後,調用probe函數:
static int
msmsdcc_probe(structplatform_device *pdev)
{
......
if (stat_irqres &&!(stat_irqres->flags & IORESOURCE_DISABLED)) {
……
} else if(plat->register_status_notify) {
plat->register_status_notify(msmsdcc_status_notify_cb,host);
} else if (!plat->status)
......
}
msmsdcc_status_notify_cb調用msmsdcc_check_status函數:
msmsdcc_status_notify_cb(intcard_present, void *dev_id)
{
struct msmsdcc_host *host = dev_id;
printk(KERN_DEBUG "%s:card_present %d\n", mmc_hostname(host->mmc),
card_present);
msmsdcc_check_status((unsigned long) host);
}
msmsdcc_check_status調用mmc_detect_change函數:
static void
msmsdcc_check_status(unsignedlong data)
{
……
if (status ^ host->oldstat) {
pr_info("%s: Slot statuschange detected (%d -> %d)\n",
mmc_hostname(host->mmc),host->oldstat, status);
if (status &&!host->plat->built_in)
mmc_detect_change(host->mmc, (5 * HZ) / 2);
else
mmc_detect_change(host->mmc, 0);
}
host->oldstat = status;
out:
if (host->timer.function)
mod_timer(&host->timer,jiffies + HZ);
}
能夠看到mmc_detect_change被調用了,這個函數觸發了一個延時工做:
void mmc_detect_change(structmmc_host *host, unsigned long delay)
{
……
mmc_schedule_delayed_work(&host->detect, delay);
}
這個時候它會在delay時間後,執行host->detect延時工做對應的函數,在host驅動註冊並匹配設備成功後執行的probe函數裏,會調用mmc_alloc_host動態建立一個mmc_host:
msmsdcc_probe(structplatform_device *pdev)
{
......
/*
* Setup our host structure
*/
mmc = mmc_alloc_host(sizeof(struct msmsdcc_host),&pdev->dev);
if (!mmc) {
ret = -ENOMEM;
goto out;
}
......
}
mmc_alloc_host初始化工做入口:
struct mmc_host*mmc_alloc_host(int extra, struct device *dev)
{
......
INIT_DELAYED_WORK(&host->detect, mmc_rescan);
......
}
mmc_rescan是core.c中一個很重要的函數,它遵守 SDIO 卡協議的 SDIO 卡啓動過程,包括了非激活模式、卡識別模式和數據傳輸模式三種模式共九種狀態的轉換,你須要參照相關規範來理解。
void mmc_rescan(structwork_struct *work)
{
struct mmc_host *host =
container_of(work, structmmc_host, detect.work);
......
mmc_power_up(host);
sdio_reset(host);
mmc_go_idle(host);
mmc_send_if_cond(host, host->ocr_avail);
/*
* First we search for SDIO...
*/
err = mmc_send_io_op_cond(host, 0, &ocr);
if (!err) {
if (mmc_attach_sdio(host, ocr))
mmc_power_off(host);
extend_wakelock = 1;
goto out;
}
......
}
這個mmc_attach_sdio函數很重要,它是SDIO卡的初始化的起點,主要工做包括:匹配SDIO卡的工做電壓,分配並初始化mmc_card結構,而後註冊mmc_card到系統中:
/*
* Starting point for SDIO card init.
*/
int mmc_attach_sdio(structmmc_host *host, u32 ocr)
{
……
mmc_attach_bus(host,&mmc_sdio_ops); //初始化host的bus_ops
……
host->ocr = mmc_select_voltage(host, ocr); //匹配SDIO卡工做電壓
……
/*
* Detect and init the card.
*/
err = mmc_sdio_init_card(host, host->ocr, NULL, 0);//檢測,分配初始化mmc_card
if (err)
goto err;
card = host->card;
/*
* If needed, disconnect card detectionpull-up resistor.
*/
err = sdio_disable_cd(card);
if (err)
goto remove;
/*
* Initialize (but don't add) all present functions.
*/
for (i = 0; i < funcs; i++, card->sdio_funcs++) {
#ifdef CONFIG_MMC_EMBEDDED_SDIO
if(host->embedded_sdio_data.funcs) {
struct sdio_func *tmp;
tmp = sdio_alloc_func(host->card);
if(IS_ERR(tmp))
goto remove;
tmp->num = (i + 1);
card->sdio_func[i] = tmp;
tmp->class = host->embedded_sdio_data.funcs[i].f_class;
tmp->max_blksize = host->embedded_sdio_data.funcs[i].f_maxblksize;
tmp->vendor = card->cis.vendor;
tmp->device = card->cis.device;
} else {
#endif
err =sdio_init_func(host->card, i + 1);
if (err)
goto remove;
#ifdefCONFIG_MMC_EMBEDDED_SDIO
}
#endif
}
mmc_release_host(host);
/*
* First add the card to the drivermodel...
*/
err = mmc_add_card(host->card); //添加mmc_card
if (err)
goto remove_added;
/*
* ...then the SDIO functions.
*/
for (i = 0;i < funcs;i++) {
err =sdio_add_func(host->card->sdio_func[i]); //將sdio_func加入系統
if (err)
goto remove_added;
}
return 0;
......
}
這樣,SDIO卡已經初始化成功並添加到了驅動中。上面說的過程是在SDIO設備註冊時的調用流程,mmc_rescan是整個流程主體部分,由它 來完成SDIO設備的初始化和添加。其實上面的流程只是建立,初始化,添加SDIO設備的一條線,還有另外的兩條線也會調用mmc_rescan函數進行 SDIO設備的上述操做:
(1) 加載SDIO host驅動模塊
(2) SDIO設備中斷
Host做爲平臺設備被註冊,前面也有列出相應源碼:
static struct platform_drivermsmsdcc_driver = {
.probe = msmsdcc_probe,
.suspend = msmsdcc_suspend,
.resume = msmsdcc_resume,
.driver = {
.name = "msm_sdcc",
},
};
static int __initmsmsdcc_init(void)
{
returnplatform_driver_register(&msmsdcc_driver);
}
Probe函數會調用mmc_alloc_host函數(代碼前面已經貼出)來建立mmc_host結構變量,進行必要的初始化以後,調用mmc_add_host函數將它添加到驅動裏面:
int mmc_add_host(structmmc_host *host)
{
……
err =device_add(&host->class_dev);
if (err)
return err;
mmc_start_host(host);
if (!(host->pm_flags &MMC_PM_IGNORE_PM_NOTIFY))
register_pm_notifier(&host->pm_notify);
return 0;
}
Mmc_start_host定義以下:
void mmc_start_host(structmmc_host *host)
{
mmc_power_off(host);
mmc_detect_change(host, 0);
}
mmc_power_off中對 ios進行了設置,而後調用 mmc_set_ios(host);
host->ios.power_mode = MMC_POWER_OFF;
host->ios.bus_width = MMC_BUS_WIDTH_1;
host->ios.timing =MMC_TIMING_LEGACY;
mmc_set_ios(host);
mmc_set_ios(host) 中的關鍵語句 host->ops->set_ios(host, ios),實際上在host驅動的probe函數中就已經對host->ops進行了初始化:
……
/*
* Setup MMC host structure
*/
mmc->ops = &msmsdcc_ops;
……
static const structmmc_host_ops msmsdcc_ops = {
.request = msmsdcc_request,
.set_ios =msmsdcc_set_ios,
.enable_sdio_irq =msmsdcc_enable_sdio_irq,
};
因此實際上調用的是msmsdcc_set_ios,關於這個函數就不介紹了,能夠參考源碼,再看 mmc_detect_change(host, 0),最後一句是:
mmc_schedule_delayed_work(&host->detect,delay);
實際上就是調用咱們前面說的延時函數 mmc_rescan,後面的流程是同樣的。
SDIO設備經過SDIO總線與host相連,SDIO總線的DAT[1]即pin8能夠做爲中斷線使用,當SDIO設備向host產生中斷時,host會對終端作出相應的動做,在host驅動的probe函數中申請並註冊相應的中斷函數:
static int
msmsdcc_probe(structplatform_device *pdev)
{
......
cmd_irqres = platform_get_resource_byname(pdev, IORESOURCE_IRQ,
"cmd_irq");
pio_irqres =platform_get_resource_byname(pdev, IORESOURCE_IRQ,
"pio_irq");
stat_irqres =platform_get_resource_byname(pdev, IORESOURCE_IRQ,
"status_irq");
......
if (stat_irqres && !(stat_irqres->flags &IORESOURCE_DISABLED)) {
unsigned long irqflags =IRQF_SHARED |
(stat_irqres->flags& IRQF_TRIGGER_MASK);
host->stat_irq = stat_irqres->start;
ret = request_irq(host->stat_irq,
msmsdcc_platform_status_irq,
irqflags,
DRIVER_NAME " (slot)",
host);
if (ret) {
pr_err("%s: Unableto get slot IRQ %d (%d)\n",
mmc_hostname(mmc), host->stat_irq, ret);
goto clk_disable;
}
}
......
}
當產生相應的中斷時調用msmsdcc_platform_status_irq中斷處理函數,這個函數的處理流程:
msmsdcc_platform_status_irq—>
msmsdcc_check_statusà
mmc_detect_changeà
mmc_rescanà
那麼,這裏爲什麼調用mmc_rescan呢?由於前面說過mmc_rescanrescan函數主要用於SDIO設備的初始化,若是SDIO設備產 生中斷不該該是已經初始化可使用了嗎?其實mmc_rescan還有其它的工做,從函數名就能看出來它還有再掃描檢測功能,即若是設備產生了中 斷,mmc_rescan函數一開始就會再次檢測全部掛接在該host上的全部SDIO設備,確認是否存在,若是不存在就作相應的釋放工做,以確保數據的 一致性。若是檢測到了新的設備那麼它就會建立一個新的mmc_card,初始化並添加該設備。
中斷引起的調用mmc_rescan動做的意義:實現了SDIO設備的熱插拔功能。
此調用流程由dhd_bus_register發起,經過sdio_register_driver註冊一個sdio設備驅動,而後經過 dhdsdio_probe初始化並註冊一個網絡設備,網絡設備的註冊標誌着wifi驅動已經成功加載,關於網絡設備的建立,初始化和註冊後面會有詳細介 紹,先來理一下上面的調用流程,:
dhd_mudule_init—> //path:dhd/sys/dhd_linux.c
Dhd_bus_registerà // dhd/sys/dhd_sdio.c
Bcmsdh_registerà // bcmsdio/sys/bcmsdh_linux.c
Sdio_function_inità // bcmsdio/sys/bcmsdh_sdmmc_linux.c
Sdio_register_driverà // bcmsdio/sys/bcmsdh_sdmmc_linux.c
Bcmsdh_sdmmc_probeà//bcmsdio/sys/bcmsdh_sdmmc_linux.c
Bcmsdh_probeà//bcmsdio/sys/bcmsdh_linux.c
Bcmsdio_probeà //dhd/sys/dhd_sdio.c
這裏注意上面兩個紅色標記的函數,sdio_register_driver註冊了一個sdio設備,在匹配成功後調用 bcmsdh_sdmmc_probe函數,這個函數會調用bcmsdh_probe。這裏有一點要注意:瀏覽bcmsdh_linux.c文件能夠看 出,在bcmsdh_register函數中,當定義了BCMLXSDMMC宏時,會調用sdio_function_init函數,不然調用 driver_register函數:
int
bcmsdh_register(bcmsdh_driver_t*driver)
{
int error = 0;
drvinfo = *driver; //注意這裏,後面會介紹到它的用處
#if defined(BCMPLATFORM_BUS)
#if defined(BCMLXSDMMC)
SDLX_MSG(("Linux Kernel SDIO/MMC Driver\n"));
error =sdio_function_init();
#else
SDLX_MSG(("Intel PXA270 SDIO Driver\n"));
error =driver_register(&bcmsdh_driver);
#endif /* defined(BCMLXSDMMC) */
return error;
#endif /*defined(BCMPLATFORM_BUS) */
#if !defined(BCMPLATFORM_BUS)&& !defined(BCMLXSDMMC)
#if (LINUX_VERSION_CODE <KERNEL_VERSION(2, 6, 0))
if (!(error =pci_module_init(&bcmsdh_pci_driver)))
return 0;
#else
if (!(error =pci_register_driver(&bcmsdh_pci_driver)))
return 0;
#endif
SDLX_MSG(("%s: pci_module_initfailed 0x%x\n", __FUNCTION__, error));
#endif /* BCMPLATFORM_BUS */
return error;
}
上面的流程中有sdio_function_init的調用出現,因此這裏實際上BCMLXSDMMC宏被定義了,bcmsdh_probe函數只 是做爲一個普通函數被調用,若是不定義該宏,那麼bcmsdh_probe函數會被做爲驅動匹配設備後第一個調用的函數而被自動調用。
再看看dhdsdio_probe函數調用的玄機,從上面的bcmsdh_register函數能夠看出它的參數被傳遞給了drvinfo,看看bcmsdh_register的調用地方:
static bcmsdh_driver_t dhd_sdio = {
dhdsdio_probe,
dhdsdio_disconnect
};
int
dhd_bus_register(void)
{
DHD_TRACE(("%s: Enter\n",__FUNCTION__));
return bcmsdh_register(&dhd_sdio);
}
上面傳遞的參數是dhd_sdio結構變量,被用兩個函數初始化了,那麼哪個是attach呢?須要找到定義bcmsdh_driver_t結構定義的地方:
Path:src/include/bcmsdh.h
/* callback functions */
typedef struct {
/* attach to device */
void *(*attach)(uint16 vend_id, uint16 dev_id, uint16 bus,uint16 slot,
uint16 func, uint bustype, void * regsva, osl_t * osh,
void * param);
/* detach from device */
void (*detach)(void *ch);
} bcmsdh_driver_t;
沒錯,就是第一個dhdsdio_probe函數,再來看看什麼地方調用了這個attach函數:
Path:bcmsdio/sys/bcmsdh_linux.c
#ifndef BCMLXSDMMC
static
#endif /* BCMLXSDMMC */
int bcmsdh_probe(struct device*dev)
{
......
if (!(sdhc->ch = drvinfo.attach((vendevid>> 16),
(vendevid & 0xFFFF), 0, 0, 0, 0,
(void*)regs, NULL, sdh))) {
SDLX_MSG(("%s: device attachfailed\n", __FUNCTION__));
goto err;
}
return 0;
......
}
紅色部分的函數調用是drvinfo.attach,就是上面傳遞過來的dhdsdio_probe函數了,仔細閱讀你會發現上面那個bcmsdh_driver_t結構體定義的地方有個說明,即把該結構的成員函數當作callback函數來使用,這就是它的用意所在。
上面是網絡設備註冊流程,在dhdsdio_probe函數中前後對dhd_attach和dhd_net_attach兩個函數調 用,dhd_attach主要用於建立和初始化dhd_info_t和net_device兩個結構變量,而後調用dhd_add_if將建立的 net_device變量添加到dhd_info_t變量的iflist列表中(支持多接口)。
Dhd_attach的流程以下:
dhd_pub_t *
dhd_attach(osl_t *osh, structdhd_bus *bus, uint bus_hdrlen)
{
dhd_info_t *dhd = NULL;
struct net_device *net = NULL;
......
/* Allocate etherdev, including spacefor private structure */
if (!(net = alloc_etherdev(sizeof(dhd)))) { //網絡設備的建立
DHD_ERROR(("%s: OOM -alloc_etherdev\n", __FUNCTION__));
goto fail;
}
dhd_state |=DHD_ATTACH_STATE_NET_ALLOC;
/* Allocate primary dhd_info */
if (!(dhd = MALLOC(osh, sizeof(dhd_info_t)))) { //dhd的建立
DHD_ERROR(("%s: OOM -alloc dhd_info\n", __FUNCTION__));
goto fail;
}
......
/* Set network interface name if it was provided as moduleparameter */
if (iface_name[0]) {
int len;
char ch;
strncpy(net->name,iface_name, IFNAMSIZ);
net->name[IFNAMSIZ - 1] = 0;
len = strlen(net->name);
ch = net->name[len - 1];
if ((ch > '9' || ch <'0') && (len < IFNAMSIZ - 2))
strcat(net->name,"%d");
}
if (dhd_add_if(dhd, 0, (void *)net, net->name, NULL, 0, 0)== DHD_BAD_IF) //將前面建立的net添加到iflist列表中
goto fail;
dhd_state |= DHD_ATTACH_STATE_ADD_IF;
......
Memcpy(netdev_priv(net), &dhd, sizeof(dhd)); //關聯dhd和net
//dhd的初始化工做
}
Dhd_add_if的添加網絡接口流程:
int
dhd_add_if(dhd_info_t *dhd, int ifidx, void *handle, char *name,
uint8 *mac_addr,uint32 flags, uint8 bssidx)
{
dhd_if_t *ifp;
DHD_TRACE(("%s: idx %d,handle->%p\n", __FUNCTION__, ifidx, handle));
ASSERT(dhd && (ifidx <DHD_MAX_IFS));
ifp =dhd->iflist[ifidx];
if (ifp != NULL) {
if (ifp->net != NULL) {
netif_stop_queue(ifp->net);
unregister_netdev(ifp->net);
free_netdev(ifp->net); //若是已經存在,釋放net成員
}
} else
if ((ifp = MALLOC(dhd->pub.osh,sizeof(dhd_if_t))) == NULL) {
DHD_ERROR(("%s: OOM - dhd_if_t\n", __FUNCTION__)); //不然,建立一個dhd_if_t結構變量
return -ENOMEM;
}
memset(ifp, 0, sizeof(dhd_if_t));
ifp->info = dhd; //進行系列初始化,添加工做
dhd->iflist[ifidx] = ifp;
strncpy(ifp->name, name, IFNAMSIZ);
ifp->name[IFNAMSIZ] = '\0';
if (mac_addr != NULL)
memcpy(&ifp->mac_addr, mac_addr,ETHER_ADDR_LEN);
if (handle == NULL) {
ifp->state = DHD_IF_ADD;
ifp->idx = ifidx;
ifp->bssidx = bssidx;
ASSERT(&dhd->thr_sysioc_ctl.thr_pid >= 0);
up(&dhd->thr_sysioc_ctl.sema);
} else
ifp->net = (struct net_device *)handle; //handle即一個net_device變量
return 0;
}
這樣,一個net_device網路設備就被添加到了接口管理列表中了,可是這是網路設備尚未完成初始化和註冊工做,bcmsdio_probe函數隨後對dhd_net_attach的調用完成了這個操做:
int
dhd_net_attach(dhd_pub_t*dhdp, int ifidx)
{
dhd_info_t *dhd = (dhd_info_t*)dhdp->info;
struct net_device *net = NULL;
int err = 0;
uint8 temp_addr[ETHER_ADDR_LEN] = {0x00, 0x90, 0x4c, 0x11, 0x22, 0x33 };
DHD_TRACE(("%s: ifidx %d\n",__FUNCTION__, ifidx));
ASSERT(dhd &&dhd->iflist[ifidx]);
net = dhd->iflist[ifidx]->net; //首先從剛纔添加的接口列表中取出net,而後進行下面的系列初始化工做
ASSERT(net);
//根據內核版本信息,選擇對net成員函數的初始化方式,假設是2.6.30的版本
#if (LINUX_VERSION_CODE <KERNEL_VERSION(2, 6, 31))
ASSERT(!net->open);
net->get_stats = dhd_get_stats;
net->do_ioctl =dhd_ioctl_entry;
net->hard_start_xmit = dhd_start_xmit;
net->set_mac_address = dhd_set_mac_address;
net->set_multicast_list = dhd_set_multicast_list;
net->open =net->stop = NULL;
#else
ASSERT(!net->netdev_ops);
net->netdev_ops = &dhd_ops_virt;
#endif
/* Ok, link into the network layer...*/
if (ifidx == 0) {
/*
* device functions for theprimary interface only
*/
#if (LINUX_VERSION_CODE <KERNEL_VERSION(2, 6, 31))
net->open = dhd_open;
net->stop = dhd_stop;
#else
net->netdev_ops = &dhd_ops_pri;
#endif
} else {
/*
* We have to use the primaryMAC for virtual interfaces
3417,1-8 66%
*/
memcpy(temp_addr,dhd->iflist[ifidx]->mac_addr, ETHER_ADDR_LEN);
/*
* Android sets the locallyadministered bit to indicate that this is a
* portable hotspot. This will not work in simultaneous AP/STAmode,
* nor with P2P. Need to set the Donlge's MAC address, andthen use that.
*/
if(!memcmp(temp_addr, dhd->iflist[0]->mac_addr,
ETHER_ADDR_LEN)) {
DHD_ERROR(("%sinterface [%s]: set locally administered bit in MAC\n",
__func__,net->name));
temp_addr[0] |= 0x02;
}
}
net->hard_header_len = ETH_HLEN + dhd->pub.hdrlen;
#if LINUX_VERSION_CODE >=KERNEL_VERSION(2, 6, 24)
net->ethtool_ops = &dhd_ethtool_ops;
#endif /* LINUX_VERSION_CODE>= KERNEL_VERSION(2, 6, 24) */
#ifdefined(CONFIG_WIRELESS_EXT)
#if WIRELESS_EXT < 19
net->get_wireless_stats = dhd_get_wireless_stats;
#endif /* WIRELESS_EXT < 19*/
#if WIRELESS_EXT > 12
net->wireless_handlers = (struct iw_handler_def*)&wl_iw_handler_def; //這裏的初始化工做很重要,以後的ioctl流程會涉及到對它的使用
#endif /* WIRELESS_EXT > 12*/
#endif /*defined(CONFIG_WIRELESS_EXT) */
dhd->pub.rxsz =DBUS_RX_BUFFER_SIZE_DHD(net);
//設置設備地址
memcpy(net->dev_addr, temp_addr, ETHER_ADDR_LEN);
if ((err =register_netdev(net)) != 0) { //註冊net
DHD_ERROR(("couldn'tregister the net device, err %d\n", err));
goto fail;
}
……
}
到這裏net網絡設備就被註冊到系統中了,設備準備好了就好對設備進行訪問了