Skip to contentSkip to conversation

Forcing SCSI capacity for broken SD Card readers

PATCH In review Clemens Fruhwirth 4 participants 10 messages Oct 14, 2009 – Dec 20, 2009 3 messages not in the archive

Subsystems (2)
lore
Share & tools
lore
#1 lore raw

PATCH 1/1 Forcing SCSI capacity for broken SD Card readers

In reply to a message not in the archive

Hi everyone,

Short: Attached is an incomplete patch that allows user space to force the capacity of an scsi device to a specific value. Any not over my-dead-body objections with that?

Long: I was unfortunate and grabbed one of the cheaper USB SD card readers that do not correctly detect >1GB capacity sd cards. This is a known and common bug. See [1] or [2]. What's interesting is that this card reader was sold to work with these cards explicitly, and I figured it must have been tested only under Windows, which we all know believes in the partition table more strongly than in what the device reports. And indeed, my 2GB sd cards start to work if I simply ignore the capacity reported by the device. I have been using this successfully in the last half year with all my sd cards and now I'm tired of patching the kernel for just that thing. Could you imagine to merge that upstream? I doubt that any device will kill itself if this interface is used incorrectly.

A simple user space program using the new interface is available at [3].

Please CC, me not on list.
[1] http://en.wikipedia.org/wiki/Secure_Digital#Standard-SD_cards_.28non-SDHC.29_with_greater_than_1_GB_capacity
[2] http://www.hjreggel.net/cardspeed/special-sd.html
[3] http://blog.clemens.endorphin.org/2009/03/patching-for-broken-sd-card-readers.html
--
Fruhwirth Clemens http://clemens.endorphin.org

drivers/scsi/sd.c +18 -0
diff -ru linux-2.6.28/drivers/scsi/sd.c linux-2.6.28/drivers/scsi/sd.c
@@ -725,6 +725,19 @@
 	return 0;
 }
 
+static int scsi_ioctl_set_capacity(struct scsi_disk *sdkp, void __user *arg)
+{
+    int val, result;
+
+    result = get_user(val, (int __user *)arg);
+    if (result)
+	return result;
+    sdkp->capacity = val;
+    set_capacity(sdkp->disk, sdkp->capacity);
+
+    return 0;
+}
+
 /**
  *	sd_ioctl - process an ioctl
  *	@inode: only i_rdev/i_bdev members may be used
@@ -770,6 +786,11 @@
 		case SCSI_IOCTL_GET_IDLUN:
 		case SCSI_IOCTL_GET_BUS_NUMBER:
 			return scsi_ioctl(sdp, cmd, p);
+		case SCSI_IOCTL_SET_CAPACITY:
+			return scsi_ioctl_set_capacity(scsi_disk(disk), (int __user *)arg);
+		case SCSI_IOCTL_GET_CAPACITY:
+			return put_user(scsi_disk(disk)->capacity, (int __user *)arg);
+
 		default:
 			error = scsi_cmd_ioctl(disk->queue, disk, mode, cmd, p);
 			if (error != -ENOTTY)
include/scsi/scsi_ioctl.h +2 -0
diff -ru linux-2.6.28/include/scsi/scsi_ioctl.h linux-2.6.28/include/scsi/scsi_ioctl.h
@@ -11,6 +11,8 @@
    the cdrom */
 #define SCSI_IOCTL_DOORLOCK 0x5380		/* lock the eject mechanism */
 #define SCSI_IOCTL_DOORUNLOCK 0x5381		/* unlock the mechanism	  */
+#define SCSI_IOCTL_SET_CAPACITY 0x5388		/* set capacity */
+#define SCSI_IOCTL_GET_CAPACITY 0x5389		/* get capacity */
 
 #define	SCSI_REMOVAL_PREVENT	1
 #define	SCSI_REMOVAL_ALLOW	0
#2 lore raw
Quoted 5 lines · Clemens Fruhwirth

Hi everyone,

Short: Attached is an incomplete patch that allows user space to force the capacity of an scsi device to a specific value. Any not over my-dead-body objections with that?

No major objection - just the usual nits:
Why not make /sys/block/sdX/size writeable?
(instead of adding an ioctl())

grundler@parisc:~$ cat /sys/block/sda/size
976773168
grundler@parisc:~$ ls -l /sys/block/sda/size
-r--r--r-- 1 root root 4096 Oct 14 16:14 /sys/block/sda/size

After reading the wiki page[1], my impression is that the block size gets reported wrong and thus the total capacity is miscalculated for 1GB-4GB SD cards with 1024 or 2048 byte blocks. Two questions around this:
a) Does the host need to know the correct block size for correct operation?
b) Do the devices report which SD Card Association spec they are compliant with in the USB headers?

I'm asking (b) to start the conversation about if it's feasible to auto-detect this in the kernel and spare all the distro's from having to frob the device size.

If we can autodetect this case, *and* the device has a valid FAT partition, it should be trivial
to use the FAT partition info instead of what the USB card reader is reporting. Partitioning
a new card will probably still need "size" to be writable.

hth,
grant

Quoted 13 lines

Long: I was unfortunate and grabbed one of the cheaper USB SD card readers that do not correctly detect >1GB capacity sd cards. This is a known and common bug. See [1] or [2]. What's interesting is that this card reader was sold to work with these cards explicitly, and I figured it must have been tested only under Windows, which we all know believes in the partition table more strongly than in what the device reports. And indeed, my 2GB sd cards start to work if I simply ignore the capacity reported by the device. I have been using this successfully in the last half year with all my sd cards and now I'm tired of patching the kernel for just that thing. Could you imagine to merge that upstream? I doubt that any device will kill itself if this interface is used incorrectly.

Quoted 6 lines

A simple user space program using the new interface is available at [3].

Please CC, me not on list.
[1] http://en.wikipedia.org/wiki/Secure_Digital#Standard-SD_cards_.28non-SDHC.29_with_greater_than_1_GB_capacity
[2] http://www.hjreggel.net/cardspeed/special-sd.html
[3] http://blog.clemens.endorphin.org/2009/03/patching-for-broken-sd-card-readers.html

thanks,
grant

Quoted 3 lines

--
Fruhwirth Clemens http://clemens.endorphin.org

--
To unsubscribe from this list: send the line "unsubscribe linux-scsi" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#3 lore raw
Quoted 9 lines · Grant Grundler
Quoted 3 lines · Clemens Fruhwirth

Short: Attached is an incomplete patch that allows user space to force the capacity of an scsi device to a specific value. Any not over my-dead-body objections with that?

No major objection - just the usual nits:
Why not make /sys/block/sdX/size writeable?
   (instead of adding an ioctl())

Oh, I like that. Sorry it took me so long to reply. It turned out that this is a much better idea. The value that is printed when reading the size attribute is to the value that I set in my patch indirectly through set_capacity. So, making size writable replaces that totally.

Patch attached.

Quoted 6 lines

After reading the wiki page[1], my impression is that the block size gets reported wrong and thus the total capacity is miscalculated for 1GB-4GB SD cards with 1024 or 2048 byte blocks. Two questions around this:
a) Does the host need to know the correct block size for correct operation?

From experiments it does not look like that's the case. This is

surprising though, as one would suspect the card not to function properly otherwise. Frankly speaking, I have not looked down the calling chain, especially how usb-storage treats these blocks, but I'd say not at all. Seems like the USB reader, I have, just doesn't get the capacity right, by not considering BLOCK_LEN > 512.

Quoted 7 lines

b) Do the devices report which SD Card Association spec they are compliant with in the USB headers?

I'm asking (b) to start the conversation about if it's feasible to auto-detect this in the kernel and spare all the distro's from having to frob the device size.

Quoted 5 lines

If we can autodetect this case, *and* the device has a valid FAT partition, it should be trivial
to use the FAT partition info instead of what the USB card reader is reporting. Partitioning
a new card will probably still need "size" to be writable.

I'm sorry, I can't really answer (b).

Autofixing this doesn't seem possible to me (except for the partition trick) as the capacity = BLOCK_LEN * BLOCKS. If BLOCK_LEN isn't detected correctly, we can't distinguish a 1GB from 2GB card.

Best Regards,

Signature · 1 line
Fruhwirth Clemens http://clemens.endorphin.org
block/genhd.c +1 -1
@@ -865,7 +865,7 @@
 static DEVICE_ATTR(ext_range, S_IRUGO, disk_ext_range_show, NULL);
 static DEVICE_ATTR(removable, S_IRUGO, disk_removable_show, NULL);
 static DEVICE_ATTR(ro, S_IRUGO, disk_ro_show, NULL);
-static DEVICE_ATTR(size, S_IRUGO, part_size_show, NULL);
+static DEVICE_ATTR(size, S_IRUGO | S_IWUSR, part_size_show, part_size_set);
 static DEVICE_ATTR(alignment_offset, S_IRUGO, disk_alignment_offset_show, NULL);
 static DEVICE_ATTR(capability, S_IRUGO, disk_capability_show, NULL);
 static DEVICE_ATTR(stat, S_IRUGO, part_stat_show, NULL);
fs/partitions/check.c +13 -0
@@ -219,6 +219,19 @@
 	return sprintf(buf, "%llu\n",(unsigned long long)p->nr_sects);
 }
 
+ssize_t part_size_set(struct device *dev,
+		      struct device_attribute *attr, char *buf, size_t len)
+{
+	struct hd_struct *p = dev_to_part(dev);
+	char *endp;
+	unsigned long long val = simple_strtoull(buf, &endp, 10);
+	if (endp == buf)
+		return -EINVAL;
+	
+	p->nr_sects = val;
+	return len;
+}
+
 ssize_t part_alignment_offset_show(struct device *dev,
 				   struct device_attribute *attr, char *buf)
 {
include/linux/genhd.h +2 -0
@@ -549,6 +549,8 @@
 
 extern ssize_t part_size_show(struct device *dev,
 			      struct device_attribute *attr, char *buf);
+extern ssize_t part_size_set(struct device *dev,
+			     struct device_attribute *attr, char *buf, size_t len);
 extern ssize_t part_stat_show(struct device *dev,
 			      struct device_attribute *attr, char *buf);
 extern ssize_t part_inflight_show(struct device *dev,
#4 lore raw

+linux-usb

Quoted 11 lines · Clemens Fruhwirth
Quoted 9 lines · Grant Grundler
Quoted 3 lines · Clemens Fruhwirth

Short: Attached is an incomplete patch that allows user space to force the capacity of an scsi device to a specific value. Any not over my-dead-body objections with that?

No major objection - just the usual nits:
Why not make /sys/block/sdX/size writeable?
   (instead of adding an ioctl())

Quoted 6 lines

Oh, I like that. Sorry it took me so long to reply. It turned out that this is a much better idea. The value that is printed when reading the size attribute is to the value that I set in my patch indirectly through set_capacity. So, making size writable replaces that totally.

Patch attached.

The code looks fine to me. From a process point of view, you just need to repost the patch according Documentation/SubmittingPatches . James will take attachments if you can't inline, but definitely want a concise commit log entry and Signed-off-by: line.

Please also add "Reviewed-by: Grant Grundler <[email protected]>".

However, I'm suspect the is problem is really with how the block size is being reported and that bit of this puzzle isn't entirely explained yet.

Quoted 11 lines
Quoted 4 lines

After reading the wiki page[1], my impression is that the block size gets reported wrong and thus the total capacity is miscalculated for 1GB-4GB SD cards with 1024 or 2048 byte blocks. Two questions around this:
a) Does the host need to know the correct block size for correct operation?

From experiments it does not look like that's the case. This is surprising though, as one would suspect the card not to  function properly otherwise. Frankly speaking, I have not looked down the calling chain, especially how usb-storage treats these blocks, but I'd say not at all. Seems like the USB reader, I have, just doesn't get the capacity right, by not considering BLOCK_LEN > 512.

Can you run USBMON (see Documentation/usb/usbmon.txt) to capture the initialization sequence and post that to linux-usb? (CC'd already)

Quoted 7 lines
Quoted 7 lines

b) Do the devices report which SD Card Association spec they are compliant with in the USB headers?

I'm asking (b) to start the conversation about if it's feasible to auto-detect this in the kernel and spare all the distro's from having to frob the device size.

Quoted 7 lines
Quoted 5 lines

If we can autodetect this case, *and* the device has a valid FAT partition, it should be trivial
to use the FAT partition info instead of what the USB card reader is reporting. Partitioning
a new card will probably still need "size" to be writable.

I'm sorry, I can't really answer (b).

I can't either.
But if you post the usbmon output to linux-usb, I suspect someone else can.

Quoted 3 lines

Autofixing this doesn't seem possible to me (except for the partition trick) as the capacity = BLOCK_LEN * BLOCKS. If BLOCK_LEN isn't detected correctly, we can't distinguish a 1GB from 2GB card.

Agreed. That's why I'm thinking getting BLOCK_LEN right is more important than hacking the resulting partition size. I'd be surprised if this conversation hasn't already occurred on linux-usb mailing list.

thanks,
grant

ps I've cut/pasted the patch you attached below so linux-usb can see it.

Quoted 4 lines

Best Regards,
--
Fruhwirth Clemens http://clemens.endorphin.org

block/genhd.c 2009-12-15 08:49:31.608959038 +0100 renamed from block/genhd.c 2009-12-03 04:51:21.000000000 +0100 +1 -1
@@ -865,7 +865,7 @@
 static DEVICE_ATTR(ext_range, S_IRUGO, disk_ext_range_show, NULL);
 static DEVICE_ATTR(removable, S_IRUGO, disk_removable_show, NULL);
 static DEVICE_ATTR(ro, S_IRUGO, disk_ro_show, NULL);
-static DEVICE_ATTR(size, S_IRUGO, part_size_show, NULL);
+static DEVICE_ATTR(size, S_IRUGO | S_IWUSR, part_size_show, part_size_set);
 static DEVICE_ATTR(alignment_offset, S_IRUGO,

disk_alignment_offset_show, NULL);
static DEVICE_ATTR(capability, S_IRUGO, disk_capability_show, NULL); static DEVICE_ATTR(stat, S_IRUGO, part_stat_show, NULL);
--- linux-2.6.32/fs/partitions/check.c 2009-12-03 04:51:21.000000000 +0100 +++ linux-2.6.32-patched/fs/partitions/check.c 2009-12-15 08:49:01.063465819 +0100
@@ -219,6 +219,19 @@

        return sprintf(buf, "%llu\n",(unsigned long long)p->nr_sects);

}

+ssize_t part_size_set(struct device *dev,
+ struct device_attribute *attr, char *buf, size_t len)
+{

+       struct hd_struct *p = dev_to_part(dev);
+       char *endp;
+       unsigned long long val = simple_strtoull(buf, &endp, 10);
+       if (endp == buf)

+ return -EINVAL;
+
+ p->nr_sects = val;
+ return len;
+}
+
ssize_t part_alignment_offset_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
--- linux-2.6.32/include/linux/genhd.h 2009-12-03 04:51:21.000000000 +0100 +++ linux-2.6.32-patched/include/linux/genhd.h 2009-12-15 09:18:44.793088778 +0100
@@ -549,6 +549,8 @@

extern ssize_t part_size_show(struct device *dev,

                              struct device_attribute *attr, char *buf);

+extern ssize_t part_size_set(struct device *dev,
+ struct device_attribute *attr, char *buf,
size_t len);
extern ssize_t part_stat_show(struct device *dev,

                              struct device_attribute *attr, char *buf);

extern ssize_t part_inflight_show(struct device *dev,
--
To unsubscribe from this list: send the line "unsubscribe linux-scsi" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#5 lore raw
Quoted 21 lines · Grant Grundler

+linux-usb

Quoted 17 lines · Clemens Fruhwirth
Quoted 9 lines · Grant Grundler
Quoted 3 lines · Clemens Fruhwirth

Short: Attached is an incomplete patch that allows user space to force the capacity of an scsi device to a specific value. Any not over my-dead-body objections with that?

No major objection - just the usual nits:
Why not make /sys/block/sdX/size writeable?
   (instead of adding an ioctl())

Oh, I like that. Sorry it took me so long to reply. It turned out that this is a much better idea. The value that is printed when reading the size attribute is to the value that I set in my patch indirectly through set_capacity. So, making size writable replaces that totally.

Patch attached.

Isn't just changing a block device's size at random times a bad thing to do? The size gets used a for a bunch of different things (checking partition boundaries, validating I/O block ranges, and so on).

Quoted 11 lines
Quoted 11 lines
Quoted 4 lines

After reading the wiki page[1], my impression is that the block size gets reported wrong and thus the total capacity is miscalculated for 1GB-4GB SD cards with 1024 or 2048 byte blocks. Two questions around this:
a) Does the host need to know the correct block size for correct operation?

From experiments it does not look like that's the case. This is surprising though, as one would suspect the card not to  function properly otherwise. Frankly speaking, I have not looked down the calling chain, especially how usb-storage treats these blocks, but I'd say not at all. Seems like the USB reader, I have, just doesn't get the capacity right, by not considering BLOCK_LEN > 512.

A lot of older card readers can't handle cards larger than 1 or 2 GB (I forget which). Or they see that the card has a larger block size and then don't report it to the host.

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

b) Do the devices report which SD Card Association spec they are compliant   with in the USB headers?

No. Cards don't report anything but block data; only the reader provides meta-information. And it doesn't necessarily know anything about the card -- in fact, the card might not even be inserted when the reader is queried.

Quoted 7 lines
Quoted 3 lines

Autofixing this doesn't seem possible to me (except for the partition trick) as the capacity = BLOCK_LEN * BLOCKS. If BLOCK_LEN isn't detected correctly, we can't distinguish a 1GB from 2GB card.

Agreed. That's why I'm thinking getting BLOCK_LEN right is more important than hacking the resulting partition size. I'd be surprised if this conversation hasn't already occurred on linux-usb mailing list.

It has. Even if you managed to fix the block size, there's a good chance the reader wouldn't work with large cards.

Alan Stern

--
To unsubscribe from this list: send the line "unsubscribe linux-scsi" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#10 lore raw
In reply to Grant Grundler
Quoted 8 lines · Grant Grundler

+linux-usb

The code looks fine to me. From a process point of view, you just need to repost the patch according Documentation/SubmittingPatches . James will take attachments if you can't inline, but definitely want a concise commit log entry and Signed-off-by: line.

Please also add "Reviewed-by: Grant Grundler <[email protected]>".

Ok, I hope I done the homework correctly. See other mail. I took the liberty to retain your Reviewed-by line despite the change from simple_strtoul to strict_strtoul as I was instructed to do by checkpatch.pl.

Quoted 2 lines

Can you run USBMON (see Documentation/usb/usbmon.txt) to capture the initialization sequence and post that to linux-usb? (CC'd already)

I attached the usbmon dump. I hope you can make sense of it.

Signature · 1 line
Fruhwirth Clemens http://clemens.endorphin.org
#6 lore raw
In reply to Alan Stern
Quoted 3 lines · Alan Stern

Isn't just changing a block device's size at random times a bad thing to do?  The size gets used a for a bunch of different things (checking partition boundaries, validating I/O block ranges, and so on).

Forcefully extending partition boundaries and I/O block ranges was just what I had in mind. My card reader was listed as being compatible with card >1GB, and I suspected that this was only verified under Windows. I knew that Windows believed in the partition table more strongly than into device boundaries, so I figured that would probably work. And it did.

Quoted 3 lines

A lot of older card readers can't handle cards larger than 1 or 2 GB (I forget which).  Or they see that the card has a larger block size and then don't report it to the host.

That was quite helpful to me
http://www.hjreggel.net/cardspeed/special-sd.html

Signature · 5 lines
Fruhwirth Clemens http://clemens.endorphin.org
--
To unsubscribe from this list: send the line "unsubscribe linux-scsi" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#12 lore raw
In reply to a message not in the archive
Quoted 4 lines · Clemens Fruhwirth
Quoted 2 lines

Can you run USBMON (see Documentation/usb/usbmon.txt) to capture the initialization sequence and post that to linux-usb? (CC'd already)

I attached the usbmon dump. I hope you can make sense of it.

Here's the relevant part of the usbmon trace:

Quoted 6 lines

f6a3da00 145121701 S Bo:6:002:1 -115 31 = 55534243 49e50000 08000000 80010a25 00000000 00000000 00000000 000000 f6a3da00 145122669 C Bo:6:002:1 0 31 >
f042d280 145122683 S Bi:6:002:2 -115 8 <
f042d280 145123663 C Bi:6:002:2 0 8 = 001d87ff 00000200
f6a3da00 145123675 S Bi:6:002:2 -115 13 <
f6a3da00 145124659 C Bi:6:002:2 0 13 = 55534253 49e50000 00000000 00

The reader reports that the card has 0x1d87ff (= 1935359) blocks, each containing 0x200 (= 512) bytes, yielding a total size slightly under 1 GB.

Alan Stern

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

#8 lore raw
In reply to a message not in the archive
Quoted 6 lines · Clemens Fruhwirth
Quoted 3 lines · Alan Stern

Isn't just changing a block device's size at random times a bad thing to do?  The size gets used a for a bunch of different things (checking partition boundaries, validating I/O block ranges, and so on).

Quoted 6 lines

Forcefully extending partition boundaries and I/O block ranges was just what I had in mind. My card reader was listed as being compatible with card >1GB, and I suspected that this was only verified under Windows. I knew that Windows believed in the partition table more strongly than into device boundaries, so I figured that would probably work. And it did.

Just out of curiousity, what happens if you try to repartition and format a card using this reader under Windows? Does it restrict the total available size to 1 GB? If it does, doesn't that indicate the reader _isn't_ compatible with large cards?

Alan Stern

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

#9 lore raw
Quoted 8 lines · Alan Stern
Quoted 6 lines · Clemens Fruhwirth

Forcefully extending partition boundaries and I/O block ranges was just what I had in mind. My card reader was listed as being compatible with card >1GB, and I suspected that this was only verified under Windows. I knew that Windows believed in the partition table more strongly than into device boundaries, so I figured that would probably work. And it did.

Quoted 4 lines

Just out of curiousity, what happens if you try to repartition and format a card using this reader under Windows?  Does it restrict the total available size to 1 GB?  If it does, doesn't that indicate the reader _isn't_ compatible with large cards?

I turns out windows _does_ respect the values reported by the device. However, not sure if this is a new thing in Windows 7.

So from that perspective, the card reader isn't >1GB-ready. But from another perspective, it is compatible, namely the practical perspective -- when sending the right USB commands you get a 2GB card that is written to correctly.

Signature · 5 lines
Fruhwirth Clemens http://clemens.endorphin.org
--
To unsubscribe from this list: send the line "unsubscribe linux-scsi" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html