Linux内核MMC里的轮询机制

简介: Linux内核MMC里的轮询机制

1 前言

最近遇到客户提的一个问题,大概意思是他们的SDIO Wi-Fi在卸载Wi-Fi驱动后再加载就检测不到Wi-Fi设备了。从他的问题看大概是热插拔有问题。

想要支持Wi-Fi复位后能重新扫描到Wi-Fi设备,需要host设置MMC_CAP_NEEDS_POLL

#define MMC_CAP_NEEDS_POLL  (1 << 5)  /* Needs polling for card-detection */

2 如何使用MMC里的轮询机制做探卡检测?

先说方法,后面再分析。

方法一:修改dts,在对应的节点增加字段broken-cd,同时,如果有non-removable字段,必须去掉该字段。

On Snow we've got sip WiFi and so can keep drive strengths low to* reduce EMI.*/
&mmc_3 {
status = "okay" num-slots = <1>? broken-cd; cap-sd1o-1rq;
keep-power-in-suspend;
card-detect-delay =<200>;
samsung,dw-mshc-ciu-div =<3>:
samsung,dw-mshc-sdr-timing = <2 3> samsung,dw-mshc-ddr-timing = <1 2> pinctrl-names ="default";
pinctr1-0 =<&sd3_clk &sd3_cmd &sd3_bus4 &wifi_en &wifi_rst>" bus-width = <4>" cap-sd-highspeed;
mmc-pwrseq =<&mmc3_pwrseq>; 哆哆jarvis  
};

方法二:通过其他手段设置host->caps |= MMC_CAP_NEEDS_POLL

3 MMC里的轮询机制剖析

3.1 在dts设置broken-cd字段,代码在哪里解析?

mmc_of_parse函数,路径是drivers\mmc\core\core.c,of_property_read_bool函数读broken-cd字段,如果读到,就给host设置MMC_CAP_NEEDS_POLL能力。

int mmc_of_parse(struct mmc_host *host)
{
    /* 省略 */
 /*
  * Configure CD and WP pins. They are both by default active low to
  * match the SDHCI spec. If GPIOs are provided for CD and / or WP, the
  * mmc-gpio helpers are used to attach, configure and use them. If
  * polarity inversion is specified in DT, one of MMC_CAP2_CD_ACTIVE_HIGH
  * and MMC_CAP2_RO_ACTIVE_HIGH capability-2 flags is set. If the
  * "broken-cd" property is provided, the MMC_CAP_NEEDS_POLL capability
  * is set. If the "non-removable" property is found, the
  * MMC_CAP_NONREMOVABLE capability is set and no card-detection
  * configuration is performed.
  */
 /* Parse Card Detection */
 if (of_property_read_bool(np, "non-removable")) {
  host->caps |= MMC_CAP_NONREMOVABLE;
  if (of_property_read_bool(np, "cd-post"))
   host->caps2 |= MMC_CAP2_CD_POST;
 } else {
  cd_cap_invert = of_property_read_bool(np, "cd-inverted");
  if (of_property_read_bool(np, "broken-cd"))
    host->caps |= MMC_CAP_NEEDS_POLL;
  ret = mmc_gpiod_request_cd(host, "cd", 0, true,
        0, &cd_gpio_invert);
  if (!ret)
   dev_info(host->parent, "Got CD GPIO\n");
  else if (ret != -ENOENT && ret != -ENOSYS)
   return ret;
  /*
   * There are two ways to flag that the CD line is inverted:
   * through the cd-inverted flag and by the GPIO line itself
   * being inverted from the GPIO subsystem. This is a leftover
   * from the times when the GPIO subsystem did not make it
   * possible to flag a line as inverted.
   *
   * If the capability on the host AND the GPIO line are
   * both inverted, the end result is that the CD line is
   * not inverted.
   */
  if (cd_cap_invert ^ cd_gpio_invert)
   host->caps2 |= MMC_CAP2_CD_ACTIVE_HIGH;
 }
    /* 省略 */
}

3.2 探卡检测流程

mmc_alloc_host函数会创建一个工作队列,mmc_rescan与host->detect绑定。

INIT_DELAYED_WORK(&host->detect, mmc_rescan);

mmc_rescan就是扫描卡的函数

void mmc_rescan(struct work_struct *work)
{
 struct mmc_host *host =
  container_of(work, struct mmc_host, detect.work);
 int i;
 if (host->trigger_card_event && host->ops->card_event) {
  host->ops->card_event(host);
  host->trigger_card_event = false;
 }
 if (host->rescan_disable)
  return;
 /* If there is a non-removable card registered, only scan once */
 if ((host->caps & MMC_CAP_NONREMOVABLE) && host->rescan_entered)
  return;
 host->rescan_entered = 1;
 mmc_bus_get(host);
 /*
  * if there is a _removable_ card registered, check whether it is
  * still present
  */
 if (host->bus_ops && !host->bus_dead
     && !(host->caps & MMC_CAP_NONREMOVABLE))
  host->bus_ops->detect(host);
 host->detect_change = 0;
 /*
  * Let mmc_bus_put() free the bus/bus_ops if we've found that
  * the card is no longer present.
  */
 mmc_bus_put(host);
 mmc_bus_get(host);
 /* if there still is a card present, stop here */
 if (host->bus_ops != NULL) {
  mmc_bus_put(host);
  goto out;
 }
 /*
  * Only we can add a new handler, so it's safe to
  * release the lock here.
  */
 mmc_bus_put(host);
 if (!(host->caps & MMC_CAP_NONREMOVABLE) && host->ops->get_cd &&
   host->ops->get_cd(host) == 0) {
  mmc_claim_host(host);
  mmc_power_off(host);
  mmc_release_host(host);
  goto out;
 }
 mmc_claim_host(host);
 for (i = 0; i < ARRAY_SIZE(freqs); i++) {
  if (!mmc_rescan_try_freq(host, max(freqs[i], host->f_min)))
   break;
  if (freqs[i] <= host->f_min)
   break;
 }
 mmc_release_host(host);
out:
  if (host->caps & MMC_CAP_NEEDS_POLL)
  mmc_schedule_delayed_work(&host->detect, HZ);
}

看到最后两行,判断host的能力,如果设置了MMC_CAP_NEEDS_POLL,也就是轮询机制,就会每隔HZ(这是个宏)时间执行一次host->detect,也就是mmc_rescan。

总结

对于探卡检测,通过在dts里面配置broken-cd就可以实现轮询探卡检测。

号主:一枚机械专业本科生,经历了转行,从外包逆袭到芯片原厂的Linux驱动开发工程师,深入操作系统的世界,贯彻终身学习、终身成长的理念。平时喜欢折腾,寒冬之下,抱团取暖,期待你来一起探讨技术、搞自媒体副业,程序员接单和投资理财。【对了,不定期送闲置开发板、书籍、键盘等等】。

如果你想了解我的转行经验,欢迎找我交流~gongzhong号【哆哆jarvis】

一起不断探索自我、走出迷茫、找到热爱,希望和你成为朋友,一起成长~

相关文章
|
9天前
|
缓存 安全 Unix
Linux 内核黑客不可靠指南【ChatGPT】
Linux 内核黑客不可靠指南【ChatGPT】
|
9天前
|
Linux 开发者
Linux内核贡献成熟度模型 【ChatGPT】
Linux内核贡献成熟度模型 【ChatGPT】
|
9天前
|
网络协议 Ubuntu Linux
用Qemu模拟vexpress-a9 (三)--- 实现用u-boot引导Linux内核
用Qemu模拟vexpress-a9 (三)--- 实现用u-boot引导Linux内核
|
9天前
|
Linux
用clang编译Linux内核
用clang编译Linux内核
|
9天前
|
Linux API C语言
Linux 内核补丁提交的清单 【ChatGPT】
Linux 内核补丁提交的清单 【ChatGPT】
|
9天前
|
安全 Linux 开发者
Linux内核管理风格 【ChatGPT】
Linux内核管理风格 【ChatGPT】
|
10天前
|
Linux 程序员 编译器
Linux内核驱动程序接口 【ChatGPT】
Linux内核驱动程序接口 【ChatGPT】
|
10天前
|
存储 Linux 开发工具
如何进行Linux内核开发【ChatGPT】
如何进行Linux内核开发【ChatGPT】
|
10天前
|
Unix Linux API
Linux内核许可规则 【ChatGPT】
Linux内核许可规则 【ChatGPT】
|
9天前
|
Linux API 调度
关于在Linux内核中使用不同延迟/休眠机制 【ChatGPT】
关于在Linux内核中使用不同延迟/休眠机制 【ChatGPT】