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】

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

相关文章
|
2月前
|
安全 网络协议 Linux
深入理解Linux内核模块:加载机制、参数传递与实战开发
本文深入解析了Linux内核模块的加载机制、参数传递方式及实战开发技巧。内容涵盖模块基础概念、加载与卸载流程、生命周期管理、参数配置方法,并通过“Hello World”模块和字符设备驱动实例,带领读者逐步掌握模块开发技能。同时,介绍了调试手段、常见问题排查、开发规范及高级特性,如内核线程、模块间通信与性能优化策略。适合希望深入理解Linux内核机制、提升系统编程能力的技术人员阅读与实践。
257 1
|
2月前
|
Ubuntu Linux
Ubuntu 23.04 用上 Linux 6.2 内核,预计下放到 22.04 LTS 版本
Linux 6.2 带来了多项内容更新,修复了 AMD 锐龙处理器设备在启用 fTPM 后的运行卡顿问题,还增强了文件系统。
|
2月前
|
Ubuntu Linux
Ubuntu 23.10 现在由Linux内核6.3提供支持
如果你想在你的个人电脑上测试一下Ubuntu 23.10的最新开发快照,你可以从官方下载服务器下载最新的每日构建ISO。然而,请记住,这是一个预发布版本,所以不要在生产机器上使用或安装它。
|
2月前
|
传感器 监控 Ubuntu
10 月发布,Ubuntu 23.10 已升级到 Linux Kernel 6.3 内核
硬件方面,Linux 6.3 引入了在 HID 中引入了原生的 Steam Deck 控制器接口,允许罗技 G923 Xbox 版赛车方向盘在 Linux 上运行;改善 8BitDo Pro 2 有线控制器的行为;并为一系列华硕 Ryzen 主板添加传感器监控。
|
2月前
|
Ubuntu Linux
Ubuntu24.04LTS默认采用Linux 6.8内核,实验性版本可通过PPA获得
IT之家提醒,当下的 Ubuntu 23.10 也是一个“短期支持版本”,该版本将在今年 7 月终止支持,而今年 4 月推出的 Ubuntu 24.04 LTS 长期支持版本将获得 5 年的更新支持。
|
2月前
|
监控 Ubuntu Linux
什么Linux,Linux内核及Linux操作系统
上面只是简单的介绍了一下Linux操作系统的几个核心组件,其实Linux的整体架构要复杂的多。单纯从Linux内核的角度,它要管理CPU、内存、网卡、硬盘和输入输出等设备,因此内核本身分为进程调度,内存管理,虚拟文件系统,网络接口等4个核心子系统。
224 0
|
2月前
|
Web App开发 缓存 Rust
|
2月前
|
Ubuntu 安全 Linux
Ubuntu 发行版更新 Linux 内核,修复 17 个安全漏洞
本地攻击者可以利用上述漏洞,攻击 Ubuntu 22.10、Ubuntu 22.04、Ubuntu 20.04 LTS 发行版,导致拒绝服务(系统崩溃)或执行任意代码。
|
2月前
|
Ubuntu 机器人 物联网
Linux Ubuntu 22.04 LTS 测试版实时内核已可申请
请注意,在启用实时内核后您需要手动配置 grub 以恢复到原始内核。更多内容请参考: