Skip to content

Instantly share code, notes, and snippets.

@Skirmisher
Created July 18, 2021 01:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Skirmisher/1dc00643c773ce12508822e16c03ee1c to your computer and use it in GitHub Desktop.
Save Skirmisher/1dc00643c773ce12508822e16c03ee1c to your computer and use it in GitHub Desktop.
SD card reader telling you the write-protect switch is set when it's not? This is the patch for you.
From 70ad5fbe600b3e8adac086bcabf6cfb54259e819 Mon Sep 17 00:00:00 2001
From: Will Springer <skirmisher@protonmail.com>
Date: Sat, 17 Jul 2021 16:59:26 -0700
Subject: [PATCH] mmc: core: add module param to override SD lock switch state
Some card readers, with age, incorrectly read the physical write-protect switch
on SD cards as being enabled, to the frustration of the user. Such a defect may
be fixable by e.g. cleaning, but not everyone has compressed air on hand.
Further, replacing a malfunctioning reader is inconvenient when said reader is
built into the device, such as a laptop. Allow the user to ignore the switch
state and treat all SD cards as read-write.
---
drivers/mmc/core/core.c | 9 +++++++++
drivers/mmc/core/sd.c | 6 ++++++
2 files changed, 15 insertions(+)
diff --git a/drivers/mmc/core/core.c b/drivers/mmc/core/core.c
index d42037f0f..1503fff2a 100644
--- a/drivers/mmc/core/core.c
+++ b/drivers/mmc/core/core.c
@@ -60,6 +60,15 @@ static const unsigned freqs[] = { 400000, 300000, 200000, 100000 };
bool use_spi_crc = 1;
module_param(use_spi_crc, bool, 0);
+/*
+ * Some card readers have flaky detection of the write-protect switch, and
+ * may erroneously determine it to be enabled. Allow the user to override
+ * the reported state.
+*/
+bool ignore_lock_switch = 0;
+module_param(ignore_lock_switch, bool, 0);
+MODULE_PARM_DESC(ignore_lock_switch, "Disable write-protect switch detection");
+
static int mmc_schedule_delayed_work(struct delayed_work *work,
unsigned long delay)
{
diff --git a/drivers/mmc/core/sd.c b/drivers/mmc/core/sd.c
index 6f054c449..0f1581a06 100644
--- a/drivers/mmc/core/sd.c
+++ b/drivers/mmc/core/sd.c
@@ -26,6 +26,8 @@
#include "sd.h"
#include "sd_ops.h"
+extern bool ignore_lock_switch;
+
static const unsigned int tran_exp[] = {
10000, 100000, 1000000, 10000000,
0, 0, 0, 0
@@ -882,6 +884,10 @@ static int mmc_sd_get_ro(struct mmc_host *host)
{
int ro;
+ /* Assume card is read-write if the user requests it. */
+ if (ignore_lock_switch)
+ return 0;
+
/*
* Some systems don't feature a write-protect pin and don't need one.
* E.g. because they only have micro-SD card slot. For those systems
--
2.31.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment