From: "Samuel Iglesias Gonsálvez" <siglesias@igalia.com>
To: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: devel@driverdev.osuosl.org, linux-kernel@vger.kernel.org,
industrypack-devel@lists.sourceforge.net,
"Jens Taprogge" <jens.taprogge@taprogge.org>,
"Samuel Iglesias Gonsálvez" <siglesias@igalia.com>
Subject: [PATCH 10/20] Staging: ipack: check the device ID space CRC.
Date: Mon, 10 Sep 2012 10:51:48 +0200 [thread overview]
Message-ID: <1347267118-9580-10-git-send-email-siglesias@igalia.com> (raw)
In-Reply-To: <1347267118-9580-1-git-send-email-siglesias@igalia.com>
From: Jens Taprogge <jens.taprogge@taprogge.org>
We check the CRC and store the result of the check in struct ipac_device.
A warning is emitted if the check fails. However we leave it to the
IPack module device to refuse to initialize due to a bad CRC. I have seen
otherwise good modules with bad CRCs.
Signed-off-by: Jens Taprogge <jens.taprogge@taprogge.org>
Signed-off-by: Samuel Iglesias Gonsálvez <siglesias@igalia.com>
---
drivers/staging/ipack/ipack.c | 59 ++++++++++++++++++++++++++++++++++++++++-
drivers/staging/ipack/ipack.h | 1 +
2 files changed, 59 insertions(+), 1 deletion(-)
diff --git a/drivers/staging/ipack/ipack.c b/drivers/staging/ipack/ipack.c
index eba2021..b368a26 100644
--- a/drivers/staging/ipack/ipack.c
+++ b/drivers/staging/ipack/ipack.c
@@ -252,20 +252,71 @@ void ipack_driver_unregister(struct ipack_driver *edrv)
}
EXPORT_SYMBOL_GPL(ipack_driver_unregister);
+static u16 ipack_crc_byte(u16 crc, u8 c)
+{
+ int i;
+
+ crc ^= c << 8;
+ for (i = 0; i < 8; i++)
+ crc = (crc << 1) ^ ((crc & 0x8000) ? 0x1021 : 0);
+ return crc;
+}
+
+/*
+ * The algorithm in lib/crc-ccitt.c does not seem to apply since it uses the
+ * opposite bit ordering.
+ */
+static u8 ipack_calc_crc1(struct ipack_device *dev)
+{
+ u8 c;
+ u16 crc;
+ unsigned int i;
+
+ crc = 0xffff;
+ for (i = 0; i < dev->id_avail; i++) {
+ c = (i != 11) ? dev->id[i] : 0;
+ crc = ipack_crc_byte(crc, c);
+ }
+ crc = ~crc;
+ return crc & 0xff;
+}
+
+static u16 ipack_calc_crc2(struct ipack_device *dev)
+{
+ u8 c;
+ u16 crc;
+ unsigned int i;
+
+ crc = 0xffff;
+ for (i = 0; i < dev->id_avail; i++) {
+ c = ((i != 0x18) && (i != 0x19)) ? dev->id[i] : 0;
+ crc = ipack_crc_byte(crc, c);
+ }
+ crc = ~crc;
+ return crc;
+}
+
static void ipack_parse_id1(struct ipack_device *dev)
{
u8 *id = dev->id;
+ u8 crc;
dev->id_vendor = id[4];
dev->id_device = id[5];
dev->speed_8mhz = 1;
dev->speed_32mhz = (id[7] == 'H');
+ crc = ipack_calc_crc1(dev);
+ dev->id_crc_correct = (crc == id[11]);
+ if (!dev->id_crc_correct) {
+ dev_warn(&dev->dev, "ID CRC invalid found 0x%x, expected 0x%x.\n",
+ id[11], crc);
+ }
}
static void ipack_parse_id2(struct ipack_device *dev)
{
__be16 *id = (__be16 *) dev->id;
- u16 flags;
+ u16 flags, crc;
dev->id_vendor = ((be16_to_cpu(id[3]) & 0xff) << 16)
+ be16_to_cpu(id[4]);
@@ -273,6 +324,12 @@ static void ipack_parse_id2(struct ipack_device *dev)
flags = be16_to_cpu(id[10]);
dev->speed_8mhz = ((flags & 2) != 0);
dev->speed_32mhz = ((flags & 4) != 0);
+ crc = ipack_calc_crc2(dev);
+ dev->id_crc_correct = (crc == be16_to_cpu(id[12]));
+ if (!dev->id_crc_correct) {
+ dev_warn(&dev->dev, "ID CRC invalid found 0x%x, expected 0x%x.\n",
+ id[11], crc);
+ }
}
static int ipack_device_read_id(struct ipack_device *dev)
diff --git a/drivers/staging/ipack/ipack.h b/drivers/staging/ipack/ipack.h
index d3be8ba67..e2cbf4a 100644
--- a/drivers/staging/ipack/ipack.h
+++ b/drivers/staging/ipack/ipack.h
@@ -77,6 +77,7 @@ struct ipack_device {
u32 id_vendor;
u32 id_device;
u8 id_format;
+ unsigned int id_crc_correct:1;
unsigned int speed_8mhz:1;
unsigned int speed_32mhz:1;
};
--
1.7.10.4
next prev parent reply other threads:[~2012-09-10 8:51 UTC|newest]
Thread overview: 43+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-09-10 8:51 [PATCH 01/20] Staging: ipack/bridges/tpci200: Put the TPCI200 control registers into a struct Samuel Iglesias Gonsálvez
2012-09-10 8:51 ` [PATCH 02/20] Staging: ipack: Provide several carrier callbacks Samuel Iglesias Gonsálvez
2012-09-10 8:51 ` [PATCH 03/20] Staging: ipack/bridges/tpci200: provide new callbacks to tpci200 Samuel Iglesias Gonsálvez
2012-09-11 8:47 ` Dan Carpenter
2012-09-12 9:28 ` Jens Taprogge
2012-09-12 11:13 ` Dan Carpenter
2012-09-12 11:21 ` Samuel Iglesias Gonsálvez
2012-09-12 11:59 ` Dan Carpenter
2012-09-12 12:28 ` Jens Taprogge
2012-09-12 12:47 ` Samuel Iglesias Gonsálvez
2012-09-12 11:58 ` Jens Taprogge
2012-09-10 8:51 ` [PATCH 04/20] Staging: ipack: Obtain supported speeds from ID ROM Samuel Iglesias Gonsálvez
2012-09-11 8:47 ` Dan Carpenter
2012-09-10 8:51 ` [PATCH 05/20] Staging: ipack: Choose the optimum bus speed by default Samuel Iglesias Gonsálvez
2012-09-11 8:47 ` Dan Carpenter
2012-09-10 8:51 ` [PATCH 06/20] Staging: ipack: remove field driver from struct ipack_device Samuel Iglesias Gonsálvez
2012-09-10 8:51 ` [PATCH 07/20] Staging: ipack/bridges/tpci200: remove struct list_head Samuel Iglesias Gonsálvez
2012-09-10 8:51 ` [PATCH 08/20] Staging: ipack: Switch to 8MHz operation before reading ID Samuel Iglesias Gonsálvez
2012-09-11 8:48 ` Dan Carpenter
2012-09-11 11:31 ` Samuel Iglesias Gonsálvez
2012-09-11 11:39 ` Jens Taprogge
2012-09-11 14:39 ` Samuel Iglesias Gonsálvez
2012-09-11 15:33 ` Dan Carpenter
2012-09-10 8:51 ` [PATCH 09/20] Staging: ipack: reset previous timeouts during device registration Samuel Iglesias Gonsálvez
2012-09-11 8:48 ` Dan Carpenter
2012-09-10 8:51 ` Samuel Iglesias Gonsálvez [this message]
2012-09-10 8:51 ` [PATCH 11/20] Staging: ipack/bridges/tpci200: reorder the iounmap and pci_release_region Samuel Iglesias Gonsálvez
2012-09-11 8:49 ` Dan Carpenter
2012-09-10 8:51 ` [PATCH 12/20] Staging: ipack/bridges/tpci200: increment the reference counter of the pci_dev Samuel Iglesias Gonsálvez
2012-09-10 8:51 ` [PATCH 13/20] Staging: ipack/bridges/tpci200: fix the uninstall the ipack device Samuel Iglesias Gonsálvez
2012-09-10 8:51 ` [PATCH 14/20] Staging: ipack/devices/ipoctal: change exiting procedure Samuel Iglesias Gonsálvez
2012-09-10 8:51 ` [PATCH 15/20] Staging: ipack/devices/ipoctal: free the IRQ Samuel Iglesias Gonsálvez
2012-09-10 8:51 ` [PATCH 16/20] Staging: ipack: unregister devices when uninstall the carrier device Samuel Iglesias Gonsálvez
2012-09-10 8:51 ` [PATCH 17/20] Staging: ipack/bridges/tpci200: delete ipack_device_unregister calls when exiting Samuel Iglesias Gonsálvez
2012-09-10 8:51 ` [PATCH 18/20] Staging: ipack/bridges/tpci200: remove tpci200_slot_unregister Samuel Iglesias Gonsálvez
2012-09-10 8:51 ` [PATCH 19/20] Staging: ipack: delete .remove_device() callback Samuel Iglesias Gonsálvez
2012-09-10 8:51 ` [PATCH 20/20] Staging: ipack/bridges/tpci200: Store the irq holder in slot_irq Samuel Iglesias Gonsálvez
2012-09-11 8:51 ` Dan Carpenter
2012-09-11 9:05 ` Samuel Iglesias Gonsálvez
2012-09-11 9:57 ` Dan Carpenter
2012-09-10 18:29 ` [PATCH 01/20] Staging: ipack/bridges/tpci200: Put the TPCI200 control registers into a struct Greg Kroah-Hartman
2012-09-11 7:01 ` Samuel Iglesias Gonsálvez
2012-09-11 7:42 ` Miguel Gómez
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1347267118-9580-10-git-send-email-siglesias@igalia.com \
--to=siglesias@igalia.com \
--cc=devel@driverdev.osuosl.org \
--cc=gregkh@linuxfoundation.org \
--cc=industrypack-devel@lists.sourceforge.net \
--cc=jens.taprogge@taprogge.org \
--cc=linux-kernel@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).