Skip to contentSkip to conversation

MMC host driver requirements

PATCH New Michał Mirosław 5 participants 21 messages Sep 7, 2008 – Jun 13, 2009

Subsystems (5)
lore
Share & tools
lore

Michał Mirosław

author maintainer
#0 lore raw

Hello,

I'm writing a driver for ENE CB710/720 memory card readers found in some laptops (ie. some versions of HP Compaq nx9500). This chip has three memory card interfaces: MMC/SD, SmartMedia, MemoryStick. I started with the MMC interface as I have a card to test it with, but I could not find any documentation about MMC stack besides the source code.

So here are some questions:

1. Can I call mmc_detect_change() after mmc_alloc_host() but before or during mmc_add_host() (ie. from interrupt handler)? If yes, what if mmc_alloc_host() fails then?
2. Can I call mmc_request_done() from ->request() handler?
3. Does MMC stack serialize calls to ->request() or any other host driver ops?
4. What is the difference (if any) between mrq->data and mrq->cmd->data as seen from ->request()?
5. Are there any constraints to scatterlist passed to ->request()
- number of elements, data alignment, element data size? (At first I assumed that there are none and have writted a simple wrapper to guarantee multiple-of-16-byte data blocks - but maybe its just not needed?)

If/when I finish the driver there is the question what are the requirements for it to be accepted to mainline? It's based on reverse-engineering work on a Windows driver so it will have some magic register-access sequences, as the original driver used completely different MMC/SD stack and probably not fully used chip's capabilities.

BTW, Google said nothing interesting about the hardware: most hits are about not existing drivers for it and manufacturer ignoring inquiries for datasheets.

Best Regards,
Michał Mirosław

#1 lore raw
Quoted 8 lines · On Sun, 7 Sep 2008 23:52:28 +0200 Michał Mirosław <[email protected]>…

Hello,

I'm writing a driver for ENE CB710/720 memory card readers found in some laptops (ie. some versions of HP Compaq nx9500). This chip has three memory card interfaces: MMC/SD, SmartMedia, MemoryStick. I started with the MMC interface as I have a card to test it with, but I could not find any documentation about MMC stack besides the source code.

Hmm... I thought the CB710/720 only had an SDHCI interface for the MMC/SD portion.

Quoted 5 lines

So here are some questions:

1. Can I call mmc_detect_change() after mmc_alloc_host() but before or during mmc_add_host() (ie. from interrupt handler)? If yes, what if mmc_alloc_host() fails then?

No you can't.

Quoted 1 line

2. Can I call mmc_request_done() from ->request() handler?

Yes, although I'm not sure how well tested it is.

Quoted 2 lines

3. Does MMC stack serialize calls to ->request() or any other host driver ops?

Requests are serialised, yes. But you could in theory get set_ios() calls during an ongoing request. Doing so would be very undefined though so it should be sufficient to just avoid crashing the entire system if that happens.

Quoted 2 lines

4. What is the difference (if any) between mrq->data and mrq->cmd->data as seen from ->request()?

Not much really. The cmd->data pointer is for convenience as it allows common code paths for handling mrq->cmd and mrq->stop.

Quoted 5 lines

5. Are there any constraints to scatterlist passed to ->request()
- number of elements, data alignment, element data size? (At first I assumed that there are none and have writted a simple wrapper to guarantee multiple-of-16-byte data blocks - but maybe its just not needed?)

None at all. You have to specify your restrictions in the mmc_host structure fields (note that you cannot restrict alignment in any way). The mmc_test driver is useful for testing some of the corner cases.

Quoted 6 lines

If/when I finish the driver there is the question what are the requirements for it to be accepted to mainline? It's based on reverse-engineering work on a Windows driver so it will have some magic register-access sequences, as the original driver used completely different MMC/SD stack and probably not fully used chip's capabilities.

The requirements are generally low for merging. Mostly you just need to promise to stick around and fix problems. You should be aware that the style requirements and API adherence are very important. Run you stuff through checkpatch.pl before submitting. Sparse can usually also find quite a few problems.

Quoted 3 lines

BTW, Google said nothing interesting about the hardware: most hits are about not existing drivers for it and manufacturer ignoring inquiries for datasheets.

As I said, to my knowledge the sdhci driver covers this hardware, but the support is rather crappy as the controller is buggy as hell. And ENE does indeed ignore all attempts at contact.

Rgds

Signature · 9 lines
     -- Pierre Ossman

  Linux kernel, MMC maintainer        http://www.kernel.org
  rdesktop, core developer          http://www.rdesktop.org

  WARNING: This correspondence is being monitored by the
  Swedish government. Make sure your server uses encryption
  for SMTP traffic and consider using PGP for end-to-end
  encryption.
author maintainer
#2 lore raw
Quoted 9 lines · Pierre Ossman
Quoted 5 lines · On Sun, 7 Sep 2008 23:52:28 +0200 Michał Mirosław <[email protected]>…

I'm writing a driver for ENE CB710/720 memory card readers found in some laptops (ie. some versions of HP Compaq nx9500). This chip has three memory card interfaces: MMC/SD, SmartMedia, MemoryStick. I started with the MMC interface as I have a card to test it with, but I could not find any documentation about MMC stack besides the source code.

Hmm... I thought the CB710/720 only had an SDHCI interface for the MMC/SD portion.

SDHCI supports CB712/714 parts. I already investigated that CB710 is not SDHCI-compatible (register layout is a bit different, for a start).

Quoted 9 lines
Quoted 5 lines

So here are some questions:

1. Can I call mmc_detect_change() after mmc_alloc_host() but before or during mmc_add_host() (ie. from interrupt handler)? If yes, what if mmc_alloc_host() fails then?

No you can't.

Quoted 1 line

2. Can I call mmc_request_done() from ->request() handler?

Yes, although I'm not sure how well tested it is.

I checked yesterday that indeed nothing bad happens, but looking at the code I can see that mmc_request_done can call ->request() with a retried request so, unless gcc optimizes tail-calls, recursion there might eat up all stack space someday.

Quoted 6 lines
Quoted 2 lines

3. Does MMC stack serialize calls to ->request() or any other host driver ops?

Requests are serialised, yes. But you could in theory get set_ios() calls during an ongoing request. Doing so would be very undefined though so it should be sufficient to just avoid crashing the entire system if that happens.

This should be easy.

Quoted 4 lines
Quoted 2 lines

4. What is the difference (if any) between mrq->data and mrq->cmd->data as seen from ->request()?

Not much really. The cmd->data pointer is for convenience as it allows common code paths for handling mrq->cmd and mrq->stop.

Is it safe to assume that cmd->data is NULL when command type is other than ADTC? I've looked briefly through drivers/mmc/core/*_ops.c, but maybe I missed something?

Quoted 8 lines
Quoted 5 lines

5. Are there any constraints to scatterlist passed to ->request()
- number of elements, data alignment, element data size? (At first I assumed that there are none and have writted a simple wrapper to guarantee multiple-of-16-byte data blocks - but maybe its just not needed?)

None at all. You have to specify your restrictions in the mmc_host structure fields (note that you cannot restrict alignment in any way). The mmc_test driver is useful for testing some of the corner cases.

I'll give it a try.

Thanks for your reply. I'll post the code for review when it does more than just blinking LEDs. ;)

Best Regards,
Michał Mirosław

author maintainer
#3 lore raw

Driver for CB710/720 memory card reader (MMC part)

Hello,

Here's a rough version of a driver for CB710/720 memory card reader. It was tested with Canon SDC-16M card and actually works... until first command error as there's no good error recovery. Driver spits out a lot of debugging output as it goes and currently when it starts failing commands it's necessary to reload the module to get it working again.

For the MMC part, TODO list includes at least:
- split the driver to a bus-like common device driver and reader specific parts like tifm driver has
- maybe get rid of sgbuf.c if there already is some similar API or more assumptions on scatterlist elements can be made

If you're brave enough to test this please do so, but I wouldn't bet my important data on it. ;)

Best Regards,
Michał Mirosław

This, obviously, is not ready to include anywhere.

cb710.h +158 -0
diff -urN empty/cb710.h cb710-pre-20080911/cb710.h
@@ -0,0 +1,158 @@
+/*
+ *  cb710/cb710.h
+ *
+ *  Copyleft by Michał Mirosław, 2008
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ */
+#ifndef LINUX_CB710_DRIVER_H
+#define LINUX_CB710_DRIVER_H
+
+#include <linux/spinlock.h>
+#include <linux/mmc/host.h>
+#include <linux/pci.h>
+#include <linux/io.h>
+#include <linux/interrupt.h>
+
+#define CB710_DRIVER_NAME "cb710"
+
+/* per-device structure */
+struct cb710_chip {
+	struct pci_dev *pdev;
+	void __iomem *iobase;
+
+	spinlock_t irq_lock;
+	unsigned irq_enabled;
+
+	struct mmc_host *mmc;
+};
+
+#define RDPORT(t, p) \
+	ioread##t(chip->iobase + (p))
+#define WRPORT(t, p, v) \
+	do { iowrite##t((v), chip->iobase + (p)); \
+		(void)ioread8(chip->iobase + 0x13); } while (0)
+#define UPDPORT(t, p, v, m) \
+	do { \
+		iowrite##t( \
+			(ioread##t(chip->iobase + (p)) & ~(m)) | (v), \
+			chip->iobase + (p)); \
+		(void)ioread8(chip->iobase + 0x13); \
+	} while (0)
+#define S_RDPORT(t, p, b, c) \
+	ioread##t##_rep(chip->iobase + (p), (b), (c))
+#define S_WRPORT(t, p, b, c) \
+	do { \
+		iowrite##t##_rep(chip->iobase + (p), (b), (c)); \
+		(void)ioread8(chip->iobase + 0x13); \
+	} while (0)
+
+
+void __cb710_pci_update_config_reg(struct pci_dev *pdev,
+	int reg, uint32_t mask, uint32_t xor);
+#define cb710_pci_update_config_reg(d, r, m, x) \
+	__cb710_pci_update_config_reg((d), (r), ~(m), (x))
+
+/* sg-to-PIO buffer */
+#define CB710_SG_BUFFER_BLOCK	16	/* power of two */
+struct cb710_sg_chain {
+	uint8_t bounce_buffer[CB710_SG_BUFFER_BLOCK];
+	struct scatterlist *sg;
+	unsigned int sg_num;
+	struct page *page;
+	void *mapped_page;
+	size_t cur_offset;
+	size_t need_advance;
+	unsigned page_no;
+	unsigned page_offset;
+	unsigned page_left;
+	unsigned need_bounce:1;
+	unsigned use_bounce:1;
+};
+
+void cb710_sg_init(struct cb710_sg_chain *buf,
+	struct scatterlist *sg, size_t nelem);
+int cb710_sg_next_buf(struct cb710_sg_chain *buf,
+	void **dataptr, size_t *len, int to_sg);
+void cb710_sg_abort(struct cb710_sg_chain *buf, int to_sg);
+
+#define cb710_sg_read_next(b, d, l) \
+	cb710_sg_next_buf((b), (d), (l), 0)
+#define cb710_sg_write_next(b, d, l) \
+	cb710_sg_next_buf((b), (d), (l), 1)
+#define cb710_sg_abort_read(b) \
+	cb710_sg_abort((b), 0)
+#define cb710_sg_abort_write(b) \
+	cb710_sg_abort((b), 1)
+
+/* per-MMC-reader structure */
+struct cb710_mmc_reader {
+	struct tasklet_struct finish_req_tasklet;
+	struct mmc_request *mrq;
+	unsigned char last_power_mode;
+	unsigned char app_cmd;
+	spinlock_t serialization_lock;
+	unsigned char active_req, active_ios;
+};
+
+int __devinit cb710_mmc_init(struct cb710_chip *chip);
+void __devexit cb710_mmc_exit(struct cb710_chip *chip);
+int cb710_mmc_irq_handler(struct cb710_chip *chip);
+
+/* registers */
+
+#define CB710_MMC_DATA_PORT		0x00
+#define CB710_MMC_CONFIG_PORT		0x04
+#define CB710_MMC_CONFIG0_PORT		0x04
+#define CB710_MMC_CONFIG1_PORT		0x05
+#define   CB710_MMC_C1_4BIT_DATA_BUS		0x40
+#define CB710_MMC_CONFIG2_PORT	0x06
+#define   CB710_MMC_C2_READ_BLOCK_SIZE_MASK	0x0F	/* N-1 */
+#define CB710_MMC_CONFIG3_PORT	0x07
+#define CB710_MMC_INTERRUPT_ENABLE_PORT	0x0D
+#define   CB710_MMC_IE_IRQ_ENABLE		0x80
+#define   CB710_MMC_IE_CARD_INSERTION_STATUS	0x10
+#define CB710_MMC_STATUS_PORT		0x10
+#define CB710_MMC_STATUS0_PORT		0x10
+#define CB710_MMC_STATUS1_PORT		0x11
+#define   CB710_MMC_S1_CARD_CHANGED		0x10
+#define   CB710_MMC_S1_RESET			0x20	/* XXX really? */
+#define CB710_MMC_STATUS2_PORT		0x12
+#define CB710_MMC_STATUS3_PORT		0x13
+#define   CB710_MMC_S3_CARD_DETECTED		0x02
+#define   CB710_MMC_S3_WRITE_PROTECTED		0x04
+#define CB710_MMC_CMD_TYPE_PORT		0x14
+#define   CB710_MMC_RSP_TYPE_MASK		0x0007
+#define     CB710_MMC_RSP_R1			(0)
+#define     CB710_MMC_RSP_136			(5)
+#define     CB710_MMC_RSP_NO_CRC		(2)
+#define   CB710_MMC_RSP_PRESENT_MASK		0x0018
+#define     CB710_MMC_RSP_NONE			(0 << 3)
+#define     CB710_MMC_RSP_PRESENT		(1 << 3)
+#define     CB710_MMC_RSP_PRESENT_X		(2 << 3)
+#define   CB710_MMC_CMD_TYPE_MASK		0x0060
+#define     CB710_MMC_CMD_BC			(0 << 5)
+#define     CB710_MMC_CMD_BCR			(1 << 5)
+#define     CB710_MMC_CMD_AC			(2 << 5)
+#define     CB710_MMC_CMD_ADTC			(3 << 5)
+#define   CB710_MMC_DATA_READ			0x0080
+#define   CB710_MMC_CMD_CODE_MASK		0x3F00
+#define   CB710_MMC_CMD_CODE_SHIFT		8
+#define   CB710_MMC_IS_APP_CMD			0x4000
+#define   CB710_MMC_RSP_BUSY			0x8000
+#define CB710_MMC_CMD_PARAM_PORT	0x18
+#define CB710_MMC_TRANSFER_SIZE_PORT	0x1C
+#define CB710_MMC_RESPONSE0_PORT	0x20
+#define CB710_MMC_RESPONSE1_PORT	0x24
+#define CB710_MMC_RESPONSE2_PORT	0x28
+#define CB710_MMC_RESPONSE3_PORT	0x2C
+
+void cb710_dump_regs(struct cb710_chip *chip, unsigned dump);
+#define CB710_DUMP_REGS_MMC	0x0F
+#define CB710_DUMP_REGS_MS	0x30
+#define CB710_DUMP_REGS_SM	0xC0
+#define CB710_DUMP_REGS_ALL	0xFF
+
+#endif /* LINUX_CB710_DRIVER_H */
main.c +238 -0

238 lines not on this page · raw message

Makefile +22 -0
diff -urN empty/Makefile cb710-pre-20080911/Makefile
@@ -0,0 +1,22 @@
+ifeq ($(KERNELRELEASE),)
+
+#KDIR	:= /usr/src/linux
+KDIR	:= /usr/src/jaja/build/rechot
+PWD     := $(shell pwd)
+
+.PHONY: module install clean
+module:
+	$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules
+
+install:
+	$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules_install
+
+clean:
+	$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) clean
+
+else	# kbuild part
+
+obj-m       := cb710.o
+cb710-y     := main.o mmc.o sgbuf.o
+
+endif
mmc.c +724 -0

724 lines not on this page · raw message

sgbuf.c +196 -0

196 lines not on this page · raw message

author maintainer
#4 lore raw

RFC: Driver for CB710/720 memory card reader (MMC part)

Hello again,

Here is next version of CB710 MMC-host driver. Since I don't like copying code I used platform device "bus" instead of duplicating tifm_core and modifying it to cb710 specifics. Debugging printk()s were retained for now.

Please comment,
Michał Mirosław

cb710.h +186 -0
diff -urN empty/cb710.h cb710-pre-20080913/cb710.h
@@ -0,0 +1,186 @@
+/*
+ *  cb710/cb710.h
+ *
+ *  Copyleft by Michał Mirosław, 2008
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ */
+#ifndef LINUX_CB710_DRIVER_H
+#define LINUX_CB710_DRIVER_H
+
+#include <linux/spinlock.h>
+#include <linux/mmc/host.h>
+#include <linux/pci.h>
+#include <linux/io.h>
+#include <linux/interrupt.h>
+#include <linux/platform_device.h>
+
+#define CB710_DRIVER_NAME "cb710"
+
+struct cb710_slot {
+	struct platform_device pdev;
+	int (*irq_handler)(struct cb710_slot *);
+};
+
+/* per-device structure */
+struct cb710_chip {
+	struct pci_dev *pdev;
+	void __iomem *iobase;
+
+	spinlock_t irq_lock;
+	unsigned irq_enabled;
+	unsigned platform_id;
+
+	unsigned slots;
+	struct cb710_slot slot[0];
+};
+
+#define CB710_SLOT_MASK		7
+#define CB710_SLOT_MMC		1
+#define CB710_SLOT_MS		2
+#define CB710_SLOT_SM		4
+#define CB710_SLOT_NR_SHIFT	8
+
+#define RDPORT(t, p) \
+	ioread##t(chip->iobase + (p))
+#define WRPORT(t, p, v) \
+	do { iowrite##t((v), chip->iobase + (p)); \
+		(void)ioread8(chip->iobase + 0x13); } while (0)
+#define UPDPORT(t, p, v, m) \
+	do { \
+		iowrite##t( \
+			(ioread##t(chip->iobase + (p)) & ~(m)) | (v), \
+			chip->iobase + (p)); \
+		(void)ioread8(chip->iobase + 0x13); \
+	} while (0)
+#define S_RDPORT(t, p, b, c) \
+	ioread##t##_rep(chip->iobase + (p), (b), (c))
+#define S_WRPORT(t, p, b, c) \
+	do { \
+		iowrite##t##_rep(chip->iobase + (p), (b), (c)); \
+		(void)ioread8(chip->iobase + 0x13); \
+	} while (0)
+
+
+void __cb710_pci_update_config_reg(struct pci_dev *pdev,
+	int reg, uint32_t mask, uint32_t xor);
+#define cb710_pci_update_config_reg(d, r, m, x) \
+	__cb710_pci_update_config_reg((d), (r), ~(m), (x))
+
+/* sg-to-PIO buffer */
+#define CB710_SG_BUFFER_BLOCK	16	/* power of two */
+struct cb710_sg_chain {
+	uint8_t bounce_buffer[CB710_SG_BUFFER_BLOCK];
+	struct scatterlist *sg;
+	unsigned int sg_num;
+	struct page *page;
+	void *mapped_page;
+	size_t cur_offset;
+	size_t need_advance;
+	unsigned page_no;
+	unsigned page_offset;
+	unsigned page_left;
+	unsigned need_bounce:1;
+	unsigned use_bounce:1;
+};
+
+void cb710_sg_init(struct cb710_sg_chain *buf,
+	struct scatterlist *sg, size_t nelem);
+int cb710_sg_next_buf(struct cb710_sg_chain *buf,
+	void **dataptr, size_t *len, int to_sg);
+void cb710_sg_abort(struct cb710_sg_chain *buf, int to_sg);
+
+#define cb710_sg_read_next(b, d, l) \
+	cb710_sg_next_buf((b), (d), (l), 0)
+#define cb710_sg_write_next(b, d, l) \
+	cb710_sg_next_buf((b), (d), (l), 1)
+#define cb710_sg_abort_read(b) \
+	cb710_sg_abort((b), 0)
+#define cb710_sg_abort_write(b) \
+	cb710_sg_abort((b), 1)
+
+/* per-MMC-reader structure */
+struct cb710_mmc_reader {
+	struct tasklet_struct finish_req_tasklet;
+	struct mmc_request *mrq;
+	unsigned char last_power_mode;
+	unsigned char app_cmd;
+	spinlock_t serialization_lock;
+	unsigned char active_req, active_ios;
+};
+
+/* some device struct walking */
+
+static inline struct cb710_chip *cb710_slot_to_chip(struct cb710_slot *slot)
+{
+	return dev_get_drvdata(slot->pdev.dev.parent);
+}
+
+static inline struct cb710_chip *cb710_mmc_to_chip(struct mmc_host *mmc)
+{
+	return dev_get_drvdata(mmc_dev(mmc));
+}
+
+static inline struct mmc_host *cb710_slot_to_mmc(struct cb710_slot *slot)
+{
+	return dev_get_drvdata(&slot->pdev.dev);
+}
+
+
+/* registers */
+
+#define CB710_MMC_DATA_PORT		0x00
+#define CB710_MMC_CONFIG_PORT		0x04
+#define CB710_MMC_CONFIG0_PORT		0x04
+#define CB710_MMC_CONFIG1_PORT		0x05
+#define   CB710_MMC_C1_4BIT_DATA_BUS		0x40
+#define CB710_MMC_CONFIG2_PORT	0x06
+#define   CB710_MMC_C2_READ_BLOCK_SIZE_MASK	0x0F	/* N-1 */
+#define CB710_MMC_CONFIG3_PORT	0x07
+#define CB710_MMC_INTERRUPT_ENABLE_PORT	0x0D
+#define   CB710_MMC_IE_IRQ_ENABLE		0x80
+#define   CB710_MMC_IE_CARD_INSERTION_STATUS	0x10
+#define CB710_MMC_STATUS_PORT		0x10
+#define CB710_MMC_STATUS0_PORT		0x10
+#define CB710_MMC_STATUS1_PORT		0x11
+#define   CB710_MMC_S1_CARD_CHANGED		0x10
+#define   CB710_MMC_S1_RESET			0x20	/* XXX really? */
+#define CB710_MMC_STATUS2_PORT		0x12
+#define CB710_MMC_STATUS3_PORT		0x13
+#define   CB710_MMC_S3_CARD_DETECTED		0x02
+#define   CB710_MMC_S3_WRITE_PROTECTED		0x04
+#define CB710_MMC_CMD_TYPE_PORT		0x14
+#define   CB710_MMC_RSP_TYPE_MASK		0x0007
+#define     CB710_MMC_RSP_R1			(0)
+#define     CB710_MMC_RSP_136			(5)
+#define     CB710_MMC_RSP_NO_CRC		(2)
+#define   CB710_MMC_RSP_PRESENT_MASK		0x0018
+#define     CB710_MMC_RSP_NONE			(0 << 3)
+#define     CB710_MMC_RSP_PRESENT		(1 << 3)
+#define     CB710_MMC_RSP_PRESENT_X		(2 << 3)
+#define   CB710_MMC_CMD_TYPE_MASK		0x0060
+#define     CB710_MMC_CMD_BC			(0 << 5)
+#define     CB710_MMC_CMD_BCR			(1 << 5)
+#define     CB710_MMC_CMD_AC			(2 << 5)
+#define     CB710_MMC_CMD_ADTC			(3 << 5)
+#define   CB710_MMC_DATA_READ			0x0080
+#define   CB710_MMC_CMD_CODE_MASK		0x3F00
+#define   CB710_MMC_CMD_CODE_SHIFT		8
+#define   CB710_MMC_IS_APP_CMD			0x4000
+#define   CB710_MMC_RSP_BUSY			0x8000
+#define CB710_MMC_CMD_PARAM_PORT	0x18
+#define CB710_MMC_TRANSFER_SIZE_PORT	0x1C
+#define CB710_MMC_RESPONSE0_PORT	0x20
+#define CB710_MMC_RESPONSE1_PORT	0x24
+#define CB710_MMC_RESPONSE2_PORT	0x28
+#define CB710_MMC_RESPONSE3_PORT	0x2C
+
+void cb710_dump_regs(struct cb710_chip *chip, unsigned dump);
+#define CB710_DUMP_REGS_MMC	0x0F
+#define CB710_DUMP_REGS_MS	0x30
+#define CB710_DUMP_REGS_SM	0xC0
+#define CB710_DUMP_REGS_ALL	0xFF
+
+#endif /* LINUX_CB710_DRIVER_H */
core.c +342 -0

342 lines not on this page · raw message

Makefile +24 -0
diff -urN empty/Makefile cb710-pre-20080913/Makefile
@@ -0,0 +1,24 @@
+ifeq ($(KERNELRELEASE),)
+
+#KDIR	:= /usr/src/linux
+KDIR	:= /usr/src/jaja/build/rechot
+PWD     := $(shell pwd)
+
+.PHONY: module install clean
+module:
+	$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules
+
+install:
+	$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules_install
+
+clean:
+	$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) clean
+
+else	# kbuild part
+
+obj-m		:= cb710-core.o cb710-mmc.o
+
+cb710-core-y	:= core.o sgbuf.o
+cb710-mmc-y	:= mmc.o
+
+endif
mmc.c +746 -0

746 lines not on this page · raw message

sgbuf.c +200 -0

200 lines not on this page · raw message

#5 lore raw

Re: RFC: Driver for CB710/720 memory card reader (MMC part)

Quoted 14 lines · On Sat, 13 Sep 2008 01:43:02 +0200 Michał Mirosław <[email protected]>…

Hello again,

Here is next version of CB710 MMC-host driver. Since I don't like copying code I used platform device "bus" instead of duplicating tifm_core and modifying it to cb710 specifics. Debugging printk()s were retained for now.

Please comment,
Michał Mirosław

diff -urN empty/cb710.h cb710-pre-20080913/cb710.h

cb710.h line 5

Quoted 5 lines
cb710.h +5 -0
@@ -0,0 +1,186 @@
+/*
+ *  cb710/cb710.h
+ *
+ *  Copyleft by Michał Mirosław, 2008
+ *

You should probably stick with "Copyright" even though it is a free software license.

Quoted 11 lines
cb710.h +11 -0
+
+#define RDPORT(t, p) \
+	ioread##t(chip->iobase + (p))
+#define WRPORT(t, p, v) \
+	do { iowrite##t((v), chip->iobase + (p)); \
+		(void)ioread8(chip->iobase + 0x13); } while (0)
+#define UPDPORT(t, p, v, m) \
+	do { \
+		iowrite##t( \
+			(ioread##t(chip->iobase + (p)) & ~(m)) | (v), \
+			chip->iobase + (p)); \

cb710.h line 65

Quoted 10 lines
cb710.h +10 -0
+		(void)ioread8(chip->iobase + 0x13); \
+	} while (0)
+#define S_RDPORT(t, p, b, c) \
+	ioread##t##_rep(chip->iobase + (p), (b), (c))
+#define S_WRPORT(t, p, b, c) \
+	do { \
+		iowrite##t##_rep(chip->iobase + (p), (b), (c)); \
+		(void)ioread8(chip->iobase + 0x13); \
+	} while (0)
+

This is a pretty bad case of obfuscation. Don't construct macros that reference local variables (chip in this case). Also, iowrite16_rep(chip->iobase + CB700_FOO, buffer, 12); is quite readable as it is. Using standard kernel functions allows your code to be more accessible to other kernel hackers.

cb710.h line 70

Quoted 5 lines
cb710.h +5 -0
+
+void __cb710_pci_update_config_reg(struct pci_dev *pdev,
+	int reg, uint32_t mask, uint32_t xor);
+#define cb710_pci_update_config_reg(d, r, m, x) \
+	__cb710_pci_update_config_reg((d), (r), ~(m), (x))

Again, this just obfuscates. If you keep the ~ in calling code it becomes more obvious that it is a mask.

Quoted 22 lines
cb710.h +22 -0
+/* sg-to-PIO buffer */
+#define CB710_SG_BUFFER_BLOCK	16	/* power of two */
+struct cb710_sg_chain {
+	uint8_t bounce_buffer[CB710_SG_BUFFER_BLOCK];
+	struct scatterlist *sg;
+	unsigned int sg_num;
+	struct page *page;
+	void *mapped_page;
+	size_t cur_offset;
+	size_t need_advance;
+	unsigned page_no;
+	unsigned page_offset;
+	unsigned page_left;
+	unsigned need_bounce:1;
+	unsigned use_bounce:1;
+};
+
+void cb710_sg_init(struct cb710_sg_chain *buf,
+	struct scatterlist *sg, size_t nelem);
+int cb710_sg_next_buf(struct cb710_sg_chain *buf,
+	void **dataptr, size_t *len, int to_sg);
+void cb710_sg_abort(struct cb710_sg_chain *buf, int to_sg);

cb710.h line 103

Quoted 10 lines
cb710.h +10 -0
+
+#define cb710_sg_read_next(b, d, l) \
+	cb710_sg_next_buf((b), (d), (l), 0)
+#define cb710_sg_write_next(b, d, l) \
+	cb710_sg_next_buf((b), (d), (l), 1)
+#define cb710_sg_abort_read(b) \
+	cb710_sg_abort((b), 0)
+#define cb710_sg_abort_write(b) \
+	cb710_sg_abort((b), 1)
+

Why this complex system? Can't you use the handlers the kernel already provides? You also get a lot of special handling with those, e.g. highmem.

Quoted 15 lines
cb710.h +15 -0
+void cb710_dump_regs(struct cb710_chip *chip, unsigned select)
+{
+	const unsigned allow[8] = {
+		0xFFF0, 0xFFFF, 0xFFFF, 0xFFFF,
+		0xFFF0, 0xFFFF, 0xFFFF, 0xFFFF,
+	};
+	const char *const prefix[sizeof(allow)/sizeof(*allow)] = {
+		"MMC", "MMC", "MMC", "MMC",
+		"MS?", "MS?", "SM?", "SM?"
+	};
+	u32 regs[sizeof(allow)/sizeof(*allow) << 2];
+
+	int i, j;
+	char msg[100], *p;
+

cb710.h line 88

Quoted 10 lines
cb710.h +10 -0
+	if (!select)
+		select = 0xFF;
+	if (!(select & 0x700))
+		select |= 0x100;
+
+#define reg(b, i) \
+	(((u##b*)regs)[(i) / (b/8)])
+#define allowed(b, i, j) \
+	(((allow[i >> 4] >> j) & ((1 << b/8)-1)) == ((1 << b/8)-1))
+#define dumpregs(b, f, x) { \

This thing is a bit big. It'll be more readable if you turn it into a function.

cb710.h line 97

Quoted 9 lines
cb710.h +9 -0
+
+static irqreturn_t cb710_irq_handler(int irq, void *data)
+{
+	struct cb710_chip *chip = data;
+	irqreturn_t handled = IRQ_NONE;
+	unsigned flags;
+	int nr;
+
+	spin_lock_irqsave(&chip->irq_lock, flags);

You shouldn't be using the interrupt versions inside the interrupt handler. Just do a normal spin_lock().

Quoted 8 lines
cb710.h +8 -0
+
+	pci_read_config_dword(pdev, 0x48, &val);
+	if (!(val & 0x80000000)) {
+		pci_write_config_dword(pdev, 0x48, val|0x71000000);
+		pci_read_config_dword(pdev, 0x48, &val);
+	}
+
+	printk(KERN_INFO CB710_DRIVER_NAME

cb710.h line 115

Quoted 10 lines
cb710.h +10 -0
+		": PCI config[0x48] = 0x%08X (%d %d %d %d %d %d)\n",
+		val,
+		!(val & 0x01000000),
+		(val >> 8) & 7,
+		!!(val & 0x10000000),
+		!!(val & 0x20000000),
+		!!(val & 0x40000000),
+		!(val & 0x02000000)
+	);
+

If this still is unknown voodoo, then please add a comment explaining that. Otherwise I expect to see more defines. :)

cb710.h line 120

Quoted 5 lines
cb710.h +5 -0
+
+static const struct pci_device_id cb710_pci_tbl[] = {
+	{ PCI_VENDOR_ID_ENE, 0x510, PCI_ANY_ID, PCI_ANY_ID, },
+	{ 0, }
+};

Allocate a define for 0x510, either in this file or pci_ids.h.

Makefile

Quoted 3 lines

diff -urN empty/Makefile cb710-pre-20080913/Makefile

Makefile +0 -0

It's easier if you develop against the real kernel tree and send diffs for that.

mmc.c

Quoted 5 lines

diff -urN empty/mmc.c cb710-pre-20080913/mmc.c

mmc.c +1 -0
 
+#include <linux/mmc/mmc.h>

You should never have to include this in a host driver.

mmc.c line 82

Quoted 5 lines
mmc.c +5 -0
+	printk(KERN_INFO CB710_DRIVER_NAME
+		": %s: clock set to %d Hz, wanted %d Hz; flag = %d\n",
+		mmc_hostname(mmc),
+		src_hz >> cb710_clock_divider_log2[divider_idx & 7],
+		hz, (divider_idx & 8) != 0);

Please use pr_debug() for things like this.

mmc.c line 110

Quoted 8 lines
mmc.c +8 -0
+static void cb710_mmc_enable_irq(struct cb710_chip *chip, int enable)
+{
+	unsigned long flags;
+
+	spin_lock_irqsave(&chip->irq_lock, flags);
+	__cb710_mmc_enable_irq(chip, enable);
+	spin_unlock_irqrestore(&chip->irq_lock, flags);
+}

This is a fairly useless wrapper. Look over how it is called instead.

mmc.c line 134

Quoted 7 lines
mmc.c +7 -0
+static int cb710_check(struct cb710_chip *chip, int what)
+{
+	uint8_t status1, status2;
+
+	/* all this is magic */
+	BUG_ON(what < 2 || what > 4);
+

You seem to know what some of the "what" values are. Put some defines for those.

mmc.c line 366

Quoted 8 lines
mmc.c +8 -0
+/*	if (flags & MMC_CMD_IS_APP) */
+	if (reader->app_cmd) {
+		/* original driver set this bit for MMC/SD application
+		 * commands. It apparently works without it, but... oh well.
+		 */
+		cb_flags |= 0x4000;
+		reader->app_cmd = 0;
+	}

ACMD:s are no different from "normal" commands. As you've discovered, you do not need this code.

mmc.c line 379

Quoted 9 lines
mmc.c +9 -0
+	if (flags & MMC_RSP_PRESENT) {
+		/* Windows driver set 01 at bits 4,3 except for
+		 * MMC_SET_BLOCKLEN. I assume that 00 here means no
+		 * response is expected.
+		 */
+		if (cmd->opcode != MMC_SET_BLOCKLEN)
+			cb_flags |= CB710_MMC_RSP_PRESENT;
+		else
+			cb_flags |= CB710_MMC_RSP_PRESENT_X;

Looking at the opcode is not acceptable, so you need to figure out what's really going on here. MMC_SET_BLOCKLEN has a common R1 response, so I don't know why the Windows driver special treated it.

mmc.c line 384

Quoted 4 lines
mmc.c +4 -0
+		if (flags & MMC_RSP_136)		/* R2 */
+			cb_flags |= CB710_MMC_RSP_136;
+		else if (!(flags & MMC_RSP_CRC))	/* R3 */
+			cb_flags |= CB710_MMC_RSP_NO_CRC;

I don't see a need for the "else".

mmc.c line 422

Quoted 3 lines
mmc.c +3 -0
+
+	if (rsp_opcode != ((cmd->flags & MMC_RSP_OPCODE) ? cmd->opcode : 0x3F))
+		cmd->error = -EILSEQ;

This isn't terribly readable. Something like this would be better:

	if (cmd->flags & MMC_RSP_OPCODE) {
		if (rsp_opcode != cmd->opcode)
			cmd->error = -EILSEQ;
	} else {
		if (rsp_opcode != 0x3F)
			cmd->error = -EILSEQ;
	}

The code generated by the compiler should be the same.

mmc.c line 440

Quoted 2 lines
mmc.c +2 -0
+	if (!error)	/* TODO: proper counting */
+		data->bytes_xfered = data->blksz * data->blocks;

You can probably never do proper counting when you don't know how the hardware behaves.

mmc.c line 643

Quoted 6 lines
mmc.c +6 -0
+static const struct mmc_host_ops cb710_mmc_host = {
+	.request = cb710_mmc_request,
+	.set_ios = cb710_mmc_set_ios,
+	.get_ro = cb710_mmc_get_ro,
+	.enable_sdio_irq = NULL,
+};

NULL is the default so you don't need to specify that.

Rgds

Signature · 9 lines
     -- Pierre Ossman

  Linux kernel, MMC maintainer        http://www.kernel.org
  rdesktop, core developer          http://www.rdesktop.org

  WARNING: This correspondence is being monitored by the
  Swedish government. Make sure your server uses encryption
  for SMTP traffic and consider using PGP for end-to-end
  encryption.
author maintainer
#6 lore raw

RFC: Driver for CB710/720 memory card reader (MMC part) - v2

Hello,

Thanks for your comments, Pierre - I have applied most of your suggestions and replied to some below.

Changes in this version:
- power management functions;
- IRQ handler modifications: core driver uses RCU, MMC-specific handler uses spinlock only in the uncommon case of 'test' interrupt;
- debugging printks converted to dev_dbg() and friends;
- code split to files such that MMC specific parts are self-contained

This should be almost ready. mmc-test passes up to a point where it tries non-sector-size writes to the card - I didn't have time to look into this further, yet.

Quoted 4 lines · Pierre Ossman

Review

Quoted 2 lines · On Sat, 13 Sep 2008 01:43:02 +0200 Michał Mirosław <[email protected]>…
(quoted diff) +2 -0
+#define RDPORT(t, p) \
+	ioread##t(chip->iobase + (p))

[and other such functions]

Quoted 5 lines

This is a pretty bad case of obfuscation. Don't construct macros that reference local variables (chip in this case). Also, iowrite16_rep(chip->iobase + CB700_FOO, buffer, 12); is quite readable as it is. Using standard kernel functions allows your code to be more accessible to other kernel hackers.

Sure. This was an attempt at imitating Perl-ish dynamically scoped package-local variable. ;-)

Quoted 1 line
Quoted 1 line

+/* sg-to-PIO buffer */

[and its API]

Quoted 3 lines

Why this complex system? Can't you use the handlers the kernel already provides? You also get a lot of special handling with those, e.g. highmem.

I looked at linux/lib/scatterlist.c and found nothing really useful. If there were some preconditions always met for scatterlists - like that block does not span multiple pages, or at least 16-byte block don't, then most of this code can go away. It looks complicated, because it implements a special iterator interface that always returns multiple-of-16-byte blocks for reading or writing. This makes the PIO loops very simple and fast (the bounce_buffer case was never triggered by mmc-block in my tests).

Quoted 5 lines

Makefile

Quoted 3 lines

diff -urN empty/Makefile cb710-pre-20080913/Makefile

Makefile +0 -0

It's easier if you develop against the real kernel tree and send diffs for that.

Hmm. For me it was easier to have the driver out-of-tree, as it's rebuilding went faster and all files are at hand. I will of course make a proper diff against vanilla kernel when it's ready to go in. I've put a .tgz at my server to make it easier for those that wan't to test the driver with other kernel versions.

Quoted 9 lines

Makefile

Quoted 8 lines
Makefile +8 -0
+static void cb710_mmc_enable_irq(struct cb710_chip *chip, int enable)
+{
+	unsigned long flags;
+
+	spin_lock_irqsave(&chip->irq_lock, flags);
+	__cb710_mmc_enable_irq(chip, enable);
+	spin_unlock_irqrestore(&chip->irq_lock, flags);
+}

This is a fairly useless wrapper. Look over how it is called instead.

Can you expand on this? Right now irq handler doesn't call __cb710_mmc_enable_irq() but modifies registers itself. This will change eventually.

Quoted 4 lines
Quoted 4 lines
Makefile +4 -0
+	if (flags & MMC_RSP_PRESENT) {
+		/* Windows driver set 01 at bits 4,3 except for
+		 * MMC_SET_BLOCKLEN. I assume that 00 here means no
+		 * response is expected.
Quoted 8 lines

Makefile

Quoted 5 lines
Makefile +5 -0
+		 */
+		if (cmd->opcode != MMC_SET_BLOCKLEN)
+			cb_flags |= CB710_MMC_RSP_PRESENT;
+		else
+			cb_flags |= CB710_MMC_RSP_PRESENT_X;

Looking at the opcode is not acceptable, so you need to figure out what's really going on here. MMC_SET_BLOCKLEN has a common R1 response, so I don't know why the Windows driver special treated it.

I removed this special-case for now and it still works.

Quoted 10 lines

Makefile

Quoted 2 lines
Makefile +2 -0
+	if (rsp_opcode != ((cmd->flags & MMC_RSP_OPCODE) ? cmd->opcode : 0x3F))
+		cmd->error = -EILSEQ;

This isn't terribly readable. Something like this would be better:

	if (cmd->flags & MMC_RSP_OPCODE) {
		if (rsp_opcode != cmd->opcode)
			cmd->error = -EILSEQ;
	} else {
		if (rsp_opcode != 0x3F)
			cmd->error = -EILSEQ;
	}

That's a lot of lines. I used another variable to make it clearer.

Thanks again for your comments,
Michał Mirosław

cb710.h +131 -0
diff -urN empty/cb710.h cb710/cb710.h
@@ -0,0 +1,131 @@
+/*
+ *  cb710/cb710.h
+ *
+ *  Copyright by Michał Mirosław, 2008
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ */
+#ifndef LINUX_CB710_DRIVER_H
+#define LINUX_CB710_DRIVER_H
+
+#include <linux/spinlock.h>
+#include <linux/mmc/host.h>
+#include <linux/pci.h>
+#include <linux/io.h>
+#include <linux/interrupt.h>
+#include <linux/platform_device.h>
+
+#define CB710_DRIVER_NAME "cb710"
+
+struct cb710_slot;
+
+typedef int (*cb710_irq_handler_t)(struct cb710_slot *);
+
+/* per-virtual-slot structure */
+struct cb710_slot {
+	struct platform_device	pdev;
+	void __iomem		*iobase;
+	cb710_irq_handler_t	irq_handler;	/* RCU */
+};
+
+/* per-device structure */
+struct cb710_chip {
+	struct pci_dev		*pdev;
+	void __iomem		*iobase;
+	unsigned		platform_id;
+	atomic_t		slots;		/* RCU */
+	unsigned		slot_mask;
+	struct cb710_slot	slot[0];
+};
+
+/* NOTE: cb710_chip.slots is modified only during device init/exit */
+
+/* cb710_chip.slot_mask values */
+#define CB710_SLOT_MMC		1
+#define CB710_SLOT_MS		2
+#define CB710_SLOT_SM		4
+
+/* slot port accessors - in case it turns out inX() is all that is needed */
+#define CB710_PORT_ACCESSORS(t) \
+static inline void cb710_write_port_##t(struct cb710_slot *slot,	\
+	unsigned port, u##t value)					\
+{									\
+	iowrite##t(value, slot->iobase + port);				\
+}									\
+									\
+static inline u##t cb710_read_port_##t(struct cb710_slot *slot,		\
+	unsigned port)							\
+{									\
+	return ioread##t(slot->iobase + port);				\
+}									\
+									\
+static inline void cb710_modify_port_##t(struct cb710_slot *slot,	\
+	unsigned port, u##t set, u##t clear)				\
+{									\
+	iowrite##t(							\
+		(ioread##t(slot->iobase + port) & ~clear)|set,		\
+		slot->iobase + port);					\
+}
+
+CB710_PORT_ACCESSORS(8)
+CB710_PORT_ACCESSORS(16)
+CB710_PORT_ACCESSORS(32)
+
+void cb710_pci_update_config_reg(struct pci_dev *pdev,
+	int reg, uint32_t and, uint32_t xor);
+
+/* some device struct walking */
+
+static inline struct cb710_slot *cb710_pdev_to_slot(
+	struct platform_device *pdev)
+{
+	return container_of(pdev, struct cb710_slot, pdev);
+}
+
+static inline struct cb710_chip *cb710_slot_to_chip(struct cb710_slot *slot)
+{
+	return dev_get_drvdata(slot->pdev.dev.parent);
+}
+
+static inline struct device *cb710_slot_dev(struct cb710_slot *slot)
+{
+	return &slot->pdev.dev;
+}
+
+static inline struct device *cb710_chip_dev(struct cb710_chip *chip)
+{
+	return &chip->pdev->dev;
+}
+
+/* helper functions */
+
+static inline void cb710_set_irq_handler(struct cb710_slot *slot,
+	cb710_irq_handler_t handler)
+{
+	rcu_assign_pointer(slot->irq_handler, handler);
+	synchronize_rcu();	/* sync to IRQ handler */
+}
+
+/* debugging aids */
+
+#ifdef DEBUG
+void cb710_dump_regs(struct cb710_chip *chip, unsigned dump);
+#else
+#define cb710_dump_regs(c, d) do {} while (0)
+#endif
+
+#define CB710_DUMP_REGS_MMC	0x0F
+#define CB710_DUMP_REGS_MS	0x30
+#define CB710_DUMP_REGS_SM	0xC0
+#define CB710_DUMP_REGS_ALL	0xFF
+#define CB710_DUMP_REGS_MASK	0xFF
+
+#define CB710_DUMP_ACCESS_8	0x100
+#define CB710_DUMP_ACCESS_16	0x200
+#define CB710_DUMP_ACCESS_32	0x400
+#define CB710_DUMP_ACCESS_ALL	0x700
+#define CB710_DUMP_ACCESS_MASK	0x700
+
+#endif /* LINUX_CB710_DRIVER_H */
cb710-mmc.h +114 -0

114 lines not on this page · raw message

cb710-sg.h +49 -0

49 lines not on this page · raw message

core.c +300 -0

300 lines not on this page · raw message

debug.c +120 -0

120 lines not on this page · raw message

Makefile +29 -0
diff -urN empty/Makefile cb710/Makefile
@@ -0,0 +1,29 @@
+ifeq ($(KERNELRELEASE),)
+
+#KDIR	:= /usr/src/linux
+KDIR	:= /usr/src/jaja/build/rechot
+PWD     := $(shell pwd)
+
+.PHONY: module install clean
+module:
+	$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules
+
+install:
+	$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules_install
+
+clean:
+	$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) clean
+	rm -f modules.order
+
+else	# kbuild part
+
+obj-m		:= cb710.o cb710-mmc.o
+
+cb710-y		:= core.o sgbuf.o
+cb710-mmc-y	:= mmc.o
+
+# debugging
+EXTRA_CFLAGS	:= -DDEBUG
+cb710-y		+= debug.o
+
+endif
mmc.c +822 -0

822 lines not on this page · raw message

sgbuf.c +206 -0

206 lines not on this page · raw message

#7 lore raw

Re: RFC: Driver for CB710/720 memory card reader (MMC part) - v2

Quoted 5 lines · On Thu, 25 Sep 2008 08:29:24 +0200 Michał Mirosław <[email protected]>…

This should be almost ready. mmc-test passes up to a point where it tries non-sector-size writes to the card - I didn't have time to look into this further, yet.

If you can't figure it out, just make sure the driver fails the requests with -EINVAL.

Quoted 6 lines
Quoted 1 line
Quoted 1 line

+/* sg-to-PIO buffer */

[and its API]

Quoted 3 lines

Why this complex system? Can't you use the handlers the kernel already provides? You also get a lot of special handling with those, e.g. highmem.

Quoted 8 lines

I looked at linux/lib/scatterlist.c and found nothing really useful. If there were some preconditions always met for scatterlists - like that block does not span multiple pages, or at least 16-byte block don't, then most of this code can go away. It looks complicated, because it implements a special iterator interface that always returns multiple-of-16-byte blocks for reading or writing. This makes the PIO loops very simple and fast (the bounce_buffer case was never triggered by mmc-block in my tests).

The buffers leave no alignment guarantees unfortunately.

Have a look at the current sdhci.c PIO routines though. It uses the sg iterator helpers and keeps track of four byte chunks (as opposed to the 16 byte ones you need).

Quoted 10 lines
Quoted 9 lines

core.c line 173

Quoted 8 lines
core.c +8 -0
+static void cb710_mmc_enable_irq(struct cb710_chip *chip, int enable)
+{
+	unsigned long flags;
+
+	spin_lock_irqsave(&chip->irq_lock, flags);
+	__cb710_mmc_enable_irq(chip, enable);
+	spin_unlock_irqrestore(&chip->irq_lock, flags);
+}

This is a fairly useless wrapper. Look over how it is called instead.

Quoted 4 lines

Can you expand on this? Right now irq handler doesn't call __cb710_mmc_enable_irq() but modifies registers itself. This will change eventually.

Just require that the lock must be held by the caller when invoking cb710_mmc_enable_irq(). It's usually easier to keep track of locks by keeping the lock handling at entry points into the driver.

Rgds

Signature · 9 lines
     -- Pierre Ossman

  Linux kernel, MMC maintainer        http://www.kernel.org
  rdesktop, core developer          http://www.rdesktop.org

  WARNING: This correspondence is being monitored by the
  Swedish government. Make sure your server uses encryption
  for SMTP traffic and consider using PGP for end-to-end
  encryption.
author maintainer
#8 lore raw

RFC: Driver for CB710/720 memory card reader (MMC part) - v3

Here is the driver for CB710 SD/MMC reader updated to recently released kernel version 2.6.27. The code is divided into PCI device driver (wrapped that registers three platform devices) and MMC reader driver. This idea was taken from TIFM driver.

Changes from v2:
- return EINVAL for unsupported data transfer sizes
- use sg_mapping_iter for PIO from/to scatterlist
- add slot release function and prevent kobject leak on device registration's error path

If this code looks reasonably good, then I'll post patches against the real kernel tree. The file distribution plan is:

cb710-mmc.h -> drivers/mmc/host/cb710-mmc.h
mmc.c -> drivers/mmc/host/cb710-mmc.c
sgbuf2.h -> merge to include/linux/scatterlist.h
sgbuf2.c -> merge to lib/scatterlist.c
cb710.h -> include/linux/cb710.h
core.c -> drivers/misc/cb710-core.c
debug.c -> drivers/misc/cb710-debug.c

Quoted 7 lines · Pierre Ossman
Quoted 3 lines · On Thu, 25 Sep 2008 08:29:24 +0200 Michał Mirosław <[email protected]>…

This should be almost ready. mmc-test passes up to a point where it tries non-sector-size writes to the card - I didn't have time to look into this further, yet.

If you can't figure it out, just make sure the driver fails the requests with -EINVAL.

Done.

Quoted 12 lines
Quoted 5 lines
Quoted 1 line
Quoted 1 line

+/* sg-to-PIO buffer */

[and its API]

Quoted 3 lines

Why this complex system? Can't you use the handlers the kernel already provides? You also get a lot of special handling with those, e.g. highmem.

Quoted 7 lines

I looked at linux/lib/scatterlist.c and found nothing really useful. If there were some preconditions always met for scatterlists - like that block does not span multiple pages, or at least 16-byte block don't, then most of this code can go away. It looks complicated, because it implements a special iterator interface that always returns multiple-of-16-byte blocks for reading or writing. This makes the PIO loops very simple and fast (the bounce_buffer case was never triggered by mmc-block in my tests).

Quoted 5 lines

The buffers leave no alignment guarantees unfortunately.

Have a look at the current sdhci.c PIO routines though. It uses the sg iterator helpers and keeps track of four byte chunks (as opposed to the 16 byte ones you need).

I got around it by introducing a small wrapper around sg_mapping_iter functions that uses 32-bit blocks. This reduces to what is in SDHCI driver's sdhci_*_block_pio() functions, but with optimized common case where the buffers are properly aligned. That leaves a question what should be inlined and what not for best effect. See: sgbuf2.{c,h}.

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

Review

Quoted 8 lines
(quoted diff) +8 -0
+static void cb710_mmc_enable_irq(struct cb710_chip *chip, int enable)
+{
+	unsigned long flags;
+
+	spin_lock_irqsave(&chip->irq_lock, flags);
+	__cb710_mmc_enable_irq(chip, enable);
+	spin_unlock_irqrestore(&chip->irq_lock, flags);
+}

This is a fairly useless wrapper. Look over how it is called instead.

Quoted 6 lines
Quoted 3 lines

Can you expand on this? Right now irq handler doesn't call __cb710_mmc_enable_irq() but modifies registers itself. This will change eventually.

Just require that the lock must be held by the caller when invoking cb710_mmc_enable_irq(). It's usually easier to keep track of locks by keeping the lock handling at entry points into the driver.

The chip->irq_lock (now in reader->irq_lock) protects only hardware IRQ-enabling registers. I think there is no point in moving the lock outside cb710_mmc_enable_irq().

Best Regards,
Michał Mirosław

cb710.h +132 -0
diff -urN -x '.git*' empty/cb710.h cb710/cb710.h
@@ -0,0 +1,132 @@
+/*
+ *  cb710/cb710.h
+ *
+ *  Copyright by Michał Mirosław, 2008
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ */
+#ifndef LINUX_CB710_DRIVER_H
+#define LINUX_CB710_DRIVER_H
+
+#include <linux/io.h>
+#include <linux/interrupt.h>
+#include <linux/spinlock.h>
+#include <linux/pci.h>
+#include <linux/platform_device.h>
+#include <linux/mmc/host.h>
+
+#define CB710_DRIVER_NAME "cb710"
+
+struct cb710_slot;
+
+typedef int (*cb710_irq_handler_t)(struct cb710_slot *);
+
+/* per-virtual-slot structure */
+struct cb710_slot {
+	struct platform_device	pdev;
+	void __iomem		*iobase;
+	cb710_irq_handler_t	irq_handler;	/* RCU */
+};
+
+/* per-device structure */
+struct cb710_chip {
+	struct pci_dev		*pdev;
+	void __iomem		*iobase;
+	unsigned		platform_id;
+	atomic_t		slots;		/* RCU */
+	unsigned		slot_mask;
+	struct cb710_slot	slot[0];
+	atomic_t		free_test;
+};
+
+/* NOTE: cb710_chip.slots is modified only during device init/exit */
+
+/* cb710_chip.slot_mask values */
+#define CB710_SLOT_MMC		1
+#define CB710_SLOT_MS		2
+#define CB710_SLOT_SM		4
+
+/* slot port accessors - in case it turns out inX() is all that is needed */
+#define CB710_PORT_ACCESSORS(t) \
+static inline void cb710_write_port_##t(struct cb710_slot *slot,	\
+	unsigned port, u##t value)					\
+{									\
+	iowrite##t(value, slot->iobase + port);				\
+}									\
+									\
+static inline u##t cb710_read_port_##t(struct cb710_slot *slot,		\
+	unsigned port)							\
+{									\
+	return ioread##t(slot->iobase + port);				\
+}									\
+									\
+static inline void cb710_modify_port_##t(struct cb710_slot *slot,	\
+	unsigned port, u##t set, u##t clear)				\
+{									\
+	iowrite##t(							\
+		(ioread##t(slot->iobase + port) & ~clear)|set,		\
+		slot->iobase + port);					\
+}
+
+CB710_PORT_ACCESSORS(8)
+CB710_PORT_ACCESSORS(16)
+CB710_PORT_ACCESSORS(32)
+
+void cb710_pci_update_config_reg(struct pci_dev *pdev,
+	int reg, uint32_t and, uint32_t xor);
+
+/* some device struct walking */
+
+static inline struct cb710_slot *cb710_pdev_to_slot(
+	struct platform_device *pdev)
+{
+	return container_of(pdev, struct cb710_slot, pdev);
+}
+
+static inline struct cb710_chip *cb710_slot_to_chip(struct cb710_slot *slot)
+{
+	return dev_get_drvdata(slot->pdev.dev.parent);
+}
+
+static inline struct device *cb710_slot_dev(struct cb710_slot *slot)
+{
+	return &slot->pdev.dev;
+}
+
+static inline struct device *cb710_chip_dev(struct cb710_chip *chip)
+{
+	return &chip->pdev->dev;
+}
+
+/* helper functions */
+
+static inline void cb710_set_irq_handler(struct cb710_slot *slot,
+	cb710_irq_handler_t handler)
+{
+	rcu_assign_pointer(slot->irq_handler, handler);
+	synchronize_rcu();	/* sync to IRQ handler */
+}
+
+/* debugging aids */
+
+#ifdef DEBUG
+void cb710_dump_regs(struct cb710_chip *chip, unsigned dump);
+#else
+#define cb710_dump_regs(c, d) do {} while (0)
+#endif
+
+#define CB710_DUMP_REGS_MMC	0x0F
+#define CB710_DUMP_REGS_MS	0x30
+#define CB710_DUMP_REGS_SM	0xC0
+#define CB710_DUMP_REGS_ALL	0xFF
+#define CB710_DUMP_REGS_MASK	0xFF
+
+#define CB710_DUMP_ACCESS_8	0x100
+#define CB710_DUMP_ACCESS_16	0x200
+#define CB710_DUMP_ACCESS_32	0x400
+#define CB710_DUMP_ACCESS_ALL	0x700
+#define CB710_DUMP_ACCESS_MASK	0x700
+
+#endif /* LINUX_CB710_DRIVER_H */
cb710-mmc.h +114 -0

114 lines not on this page · raw message

core.c +321 -0

321 lines not on this page · raw message

debug.c +120 -0

120 lines not on this page · raw message

Makefile +29 -0
diff -urN -x '.git*' empty/Makefile cb710/Makefile
@@ -0,0 +1,29 @@
+ifeq ($(KERNELRELEASE),)
+
+#KDIR	:= /usr/src/linux
+KDIR	:= /usr/src/jaja/build/rechot
+PWD     := $(shell pwd)
+
+.PHONY: module install clean
+module:
+	$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules
+
+install:
+	$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules_install
+
+clean:
+	$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) clean
+	rm -f modules.order Module.markers
+
+else	# kbuild part
+
+obj-m		:= cb710.o cb710-mmc.o
+
+cb710-y		:= core.o sgbuf2.o
+cb710-mmc-y	:= mmc.o
+
+# debugging
+EXTRA_CFLAGS	:= -DDEBUG
+cb710-y		+= debug.o
+
+endif
mmc.c +845 -0

845 lines not on this page · raw message

sgbuf2.c +113 -0

113 lines not on this page · raw message

sgbuf2.h +49 -0

49 lines not on this page · raw message

#9 lore raw

Re: RFC: Driver for CB710/720 memory card reader (MMC part) - v3

Quoted 5 lines · On Wed, 29 Oct 2008 15:11:47 +0100 Michał Mirosław <[email protected]>…

Here is the driver for CB710 SD/MMC reader updated to recently released kernel version 2.6.27. The code is divided into PCI device driver (wrapped that registers three platform devices) and MMC reader driver. This idea was taken from TIFM driver.

Quoted 9 lines

Changes from v2:
- return EINVAL for unsupported data transfer sizes
- use sg_mapping_iter for PIO from/to scatterlist
- add slot release function and prevent kobject leak on device registration's error path

If this code looks reasonably good, then I'll post patches against the real kernel tree. The file distribution plan is:

It looks more or less good to go. I have some small notes, and you probably need to poke some scatterlist folks to okay those changes (should probably be a separate patch).

Quoted 15 lines
cb710.h +15 -0
+/* slot port accessors - in case it turns out inX() is all that is needed */
+#define CB710_PORT_ACCESSORS(t) \
+static inline void cb710_write_port_##t(struct cb710_slot *slot,	\
+	unsigned port, u##t value)					\
+{									\
+	iowrite##t(value, slot->iobase + port);				\
+}									\
+									\
+static inline u##t cb710_read_port_##t(struct cb710_slot *slot,		\
+	unsigned port)							\
+{									\
+	return ioread##t(slot->iobase + port);				\
+}									\
+									\
+static inline void cb710_modify_port_##t(struct cb710_slot *slot,	\

cb710.h line 75

Quoted 10 lines
cb710.h +10 -0
+	unsigned port, u##t set, u##t clear)				\
+{									\
+	iowrite##t(							\
+		(ioread##t(slot->iobase + port) & ~clear)|set,		\
+		slot->iobase + port);					\
+}
+
+CB710_PORT_ACCESSORS(8)
+CB710_PORT_ACCESSORS(16)
+CB710_PORT_ACCESSORS(32)

This is clearer than what you had before, but I still think you should consider using the kernel functions directly.

cb710.h line 111

Quoted 9 lines
cb710.h +9 -0
+/* helper functions */
+
+static inline void cb710_set_irq_handler(struct cb710_slot *slot,
+	cb710_irq_handler_t handler)
+{
+	rcu_assign_pointer(slot->irq_handler, handler);
+	synchronize_rcu();	/* sync to IRQ handler */
+}
+

Why RCU:s? This smells of premature optimisation.

cb710-mmc.h line 31

Quoted 11 lines
cb710-mmc.h +11 -0
+/* per-MMC-reader structure */
+struct cb710_mmc_reader {
+	struct tasklet_struct finish_req_tasklet;
+	struct mmc_request *mrq;
+	spinlock_t irq_lock;
+	unsigned char last_power_mode;
+#ifdef VERBOSE_DEBUG
+	spinlock_t serialization_lock;
+	unsigned char active_req, active_ios;
+#endif
+};

I couldn't find VERBOSE_DEBUG defined somewhere. For ease of use with future testers, you should connect it to some Kconfig option.

Quoted 5 lines
core.c +5 -0
+static int cb710_suspend(struct pci_dev *pdev, pm_message_t state)
+{
+	pci_save_state(pdev);
+	pci_disable_device(pdev);
+	if (state.event & PM_EVENT_SLEEP)

core.c line 172

Quoted 10 lines
core.c +10 -0
+		pci_set_power_state(pdev, PCI_D3cold);
+	return 0;
+}
+
+static int cb710_resume(struct pci_dev *pdev)
+{
+	pci_set_power_state(pdev, PCI_D0);
+	pci_restore_state(pdev);
+	return pcim_enable_device(pdev);
+}

Free/restore interrupt?

core.c line 286

Quoted 2 lines
core.c +2 -0
+/* TODO: move to pci_ids.h before merging upstream */
+#define PCI_DEVICE_ID_ENE_710_FLASH	0x0510

It's a matter of taste really. Many drivers have their defines in the driver. Personally I think it is nice to have everything in pci_ids.h, but it's up to you really.

mmc.c line 413

Quoted 8 lines
mmc.c +8 -0
+#if 0
+	/* original driver set bit 14 for MMC/SD application
+	 * commands. There's no difference 'on the wire' and
+	 * it apparently works without it anyway.
+	 */
+	if (flags & MMC_CMD_IS_APPCMD)
+		cb_flags |= 0x4000;
+#endif

As this code is just useless, please clean it out before you submit the driver.

mmc.c line 767

Quoted 4 lines
mmc.c +4 -0
+	mmc->ops = &cb710_mmc_host;
+	mmc->f_max = val;
+	mmc->f_min = val >> cb710_clock_divider_log2[CB710_MAX_DIVIDER_IDX];
+	mmc->ocr_avail = MMC_VDD_32_33|MMC_VDD_33_34|MMC_VDD_34_35;

Since you can't change the voltage, this seems wrong. My guess is that the MMC_VDD_34_35 define should go.

mmc.c line 804

Quoted 9 lines
mmc.c +9 -0
+static int __devexit cb710_mmc_exit(struct platform_device *pdev)
+{
+	struct cb710_slot *slot = cb710_pdev_to_slot(pdev);
+	struct mmc_host *mmc = cb710_slot_to_mmc(slot);
+	struct cb710_mmc_reader *reader = mmc_priv(mmc);
+
+	mmc_remove_host(mmc);
+
+	/* XXX what if IRQ arrives now? */

Most drivers handle this by sticking their head in the sand, but the proper way is probably to disable the card insertions interrupt before mmc_remove_host().

(and there should be no other unsolicited interrupts)

Rgds

Signature · 9 lines
     -- Pierre Ossman

  Linux kernel, MMC maintainer        http://www.kernel.org
  rdesktop, core developer          http://www.rdesktop.org

  WARNING: This correspondence is being monitored by the
  Swedish government. Make sure your server uses encryption
  for SMTP traffic and consider using PGP for end-to-end
  encryption.
author maintainer
#10 lore raw

Re: RFC: Driver for CB710/720 memory card reader (MMC part) - v3

Hello again,

Sorry for a long delay with my reply.

Quoted 13 lines · Pierre Ossman
Quoted 11 lines · On Wed, 29 Oct 2008 15:11:47 +0100 Michał Mirosław <[email protected]>…

Here is the driver for CB710 SD/MMC reader updated to recently released kernel version 2.6.27. The code is divided into PCI device driver (wrapped that registers three platform devices) and MMC reader driver. This idea was taken from TIFM driver.

Changes from v2:
- return EINVAL for unsupported data transfer sizes
- use sg_mapping_iter for PIO from/to scatterlist
- add slot release function and prevent kobject leak on device registration's error path

Quoted 5 lines
Quoted 2 lines

If this code looks reasonably good, then I'll post patches against the real kernel tree. The file distribution plan is:

It looks more or less good to go. I have some small notes, and you probably need to poke some scatterlist folks to okay those changes (should probably be a separate patch).

There's no entry in MAINTAINERS list, but I assume it's Jens Axboe as he's in the copyright section in scatterlist.c.

Quoted 19 lines
Quoted 19 lines
cb710.h +19 -0
+/* slot port accessors - in case it turns out inX() is all that is needed */
+#define CB710_PORT_ACCESSORS(t) \
+static inline void cb710_write_port_##t(struct cb710_slot *slot,	\
+	unsigned port, u##t value)					\
+{									\
+	iowrite##t(value, slot->iobase + port);				\
+}									\
+									\
+static inline u##t cb710_read_port_##t(struct cb710_slot *slot,		\
+	unsigned port)							\
+{									\
+	return ioread##t(slot->iobase + port);				\
+}									\
+									\
+static inline void cb710_modify_port_##t(struct cb710_slot *slot,	\
+	unsigned port, u##t set, u##t clear)				\
+{									\
+	iowrite##t(							\
+		(ioread##t(slot->iobase + port) & ~clear)|set,		\
Quoted 8 lines

cb710.h line 75

Quoted 6 lines
cb710.h +6 -0
+		slot->iobase + port);					\
+}
+
+CB710_PORT_ACCESSORS(8)
+CB710_PORT_ACCESSORS(16)
+CB710_PORT_ACCESSORS(32)

This is clearer than what you had before, but I still think you should consider using the kernel functions directly.

The really useful one here is cb710_modify_port_*(), read/write are for consistency. I'd prefer to keep those wrappers because they help to eliminate some noise from register access logic in the driver.

Quoted 9 lines

cb710.h line 110

Quoted 8 lines
cb710.h +8 -0
+/* helper functions */
+
+static inline void cb710_set_irq_handler(struct cb710_slot *slot,
+	cb710_irq_handler_t handler)
+{
+	rcu_assign_pointer(slot->irq_handler, handler);
+	synchronize_rcu();	/* sync to IRQ handler */
+}

Why RCU:s? This smells of premature optimisation.

I thought this as the easiest way to assure that on return from cb710_set_irq_handler() there's no interrupt handler running using the old slot->irq_handler value.

Quoted 5 lines
Quoted 5 lines
cb710-mmc.h +5 -0
+/* per-MMC-reader structure */
+struct cb710_mmc_reader {
+	struct tasklet_struct finish_req_tasklet;
+	struct mmc_request *mrq;
+	spinlock_t irq_lock;
Quoted 8 lines

cb710-mmc.h line 31

Quoted 6 lines
cb710-mmc.h +6 -0
+	unsigned char last_power_mode;
+#ifdef VERBOSE_DEBUG
+	spinlock_t serialization_lock;
+	unsigned char active_req, active_ios;
+#endif
+};

I couldn't find VERBOSE_DEBUG defined somewhere. For ease of use with future testers, you should connect it to some Kconfig option.

This is mostly related to veryfying some assumptions on the MMC core infrastructure. I can remove this altogether if the calls to driver ->request() and ->set_ios() are really guaranteed to be serialized.

Quoted 8 lines
Quoted 8 lines
core.c +8 -0
+static int cb710_suspend(struct pci_dev *pdev, pm_message_t state)
+{
+	pci_save_state(pdev);
+	pci_disable_device(pdev);
+	if (state.event & PM_EVENT_SLEEP)
+		pci_set_power_state(pdev, PCI_D3cold);
+	return 0;
+}
Quoted 8 lines

core.c line 172

Quoted 7 lines
core.c +7 -0
+
+static int cb710_resume(struct pci_dev *pdev)
+{
+	pci_set_power_state(pdev, PCI_D0);
+	pci_restore_state(pdev);
+	return pcim_enable_device(pdev);
+}

Free/restore interrupt?

Hmm. I checked couple random drivers in kernel tree and none (including MMC host drivers) are touching interrupt allocation in suspend/resume.

Quoted 10 lines

mmc.c line 413

Quoted 8 lines
mmc.c +8 -0
+#if 0
+	/* original driver set bit 14 for MMC/SD application
+	 * commands. There's no difference 'on the wire' and
+	 * it apparently works without it anyway.
+	 */
+	if (flags & MMC_CMD_IS_APPCMD)
+		cb_flags |= 0x4000;
+#endif

As this code is just useless, please clean it out before you submit the driver.

Will turn this into pure comment - just to remember.

Quoted 6 lines

mmc.c line 767

Quoted 4 lines
mmc.c +4 -0
+	mmc->ops = &cb710_mmc_host;
+	mmc->f_max = val;
+	mmc->f_min = val >> cb710_clock_divider_log2[CB710_MAX_DIVIDER_IDX];
+	mmc->ocr_avail = MMC_VDD_32_33|MMC_VDD_33_34|MMC_VDD_34_35;

Since you can't change the voltage, this seems wrong. My guess is that the MMC_VDD_34_35 define should go.

That's what the original driver advertises. I can't verify the exact voltage that is supplied to the card, so I'll assume that's 3.3V and remove MMC_VDD_34_35 as you suggest.

Quoted 6 lines
Quoted 6 lines
mmc.c +6 -0
+static int __devexit cb710_mmc_exit(struct platform_device *pdev)
+{
+	struct cb710_slot *slot = cb710_pdev_to_slot(pdev);
+	struct mmc_host *mmc = cb710_slot_to_mmc(slot);
+	struct cb710_mmc_reader *reader = mmc_priv(mmc);
+
Quoted 8 lines

mmc.c line 804

Quoted 3 lines
mmc.c +3 -0
+	mmc_remove_host(mmc);
+
+	/* XXX what if IRQ arrives now? */

Most drivers handle this by sticking their head in the sand, but the proper way is probably to disable the card insertions interrupt before mmc_remove_host().
(and there should be no other unsolicited interrupts)

Will try.

Best Regards,
Michał Mirosław

#11 lore raw

Re: RFC: Driver for CB710/720 memory card reader (MMC part) - v3

Quoted 10 lines · On Sun, 1 Feb 2009 19:54:35 +0100 Michał Mirosław <[email protected]>…
Quoted 9 lines

cb710.h line 110

Quoted 8 lines
cb710.h +8 -0
+/* helper functions */
+
+static inline void cb710_set_irq_handler(struct cb710_slot *slot,
+	cb710_irq_handler_t handler)
+{
+	rcu_assign_pointer(slot->irq_handler, handler);
+	synchronize_rcu();	/* sync to IRQ handler */
+}

Why RCU:s? This smells of premature optimisation.

Quoted 4 lines

I thought this as the easiest way to assure that on return from cb710_set_irq_handler() there's no interrupt handler running using the old slot->irq_handler value.

A spinlock will suffice fine for that.

Quoted 14 lines
Quoted 5 lines
Quoted 5 lines
cb710-mmc.h +5 -0
+/* per-MMC-reader structure */
+struct cb710_mmc_reader {
+	struct tasklet_struct finish_req_tasklet;
+	struct mmc_request *mrq;
+	spinlock_t irq_lock;
Quoted 8 lines

cb710-mmc.h line 31

Quoted 6 lines
cb710-mmc.h +6 -0
+	unsigned char last_power_mode;
+#ifdef VERBOSE_DEBUG
+	spinlock_t serialization_lock;
+	unsigned char active_req, active_ios;
+#endif
+};

I couldn't find VERBOSE_DEBUG defined somewhere. For ease of use with future testers, you should connect it to some Kconfig option.

Quoted 4 lines

This is mostly related to veryfying some assumptions on the MMC core infrastructure. I can remove this altogether if the calls to driver ->request() and ->set_ios() are really guaranteed to be serialized.

They are. Changing device settings during an ongoing request is very undefined.

Quoted 11 lines
Quoted 7 lines

core.c line 172

Quoted 6 lines
core.c +6 -0
+static int cb710_resume(struct pci_dev *pdev)
+{
+	pci_set_power_state(pdev, PCI_D0);
+	pci_restore_state(pdev);
+	return pcim_enable_device(pdev);
+}

Free/restore interrupt?

Hmm. I checked couple random drivers in kernel tree and none (including MMC host drivers) are touching interrupt allocation in suspend/resume.

Check sdhci.c, it frees the irq and re-requests it during suspend. This is generally needed when you share interrupts as you need to avoid trying to handle interrupts before your device has been resumed.

Rgds

Signature · 9 lines
     -- Pierre Ossman

  Linux kernel, MMC maintainer        http://www.kernel.org
  rdesktop, core developer          http://www.rdesktop.org

  WARNING: This correspondence is being monitored by the
  Swedish government. Make sure your server uses encryption
  for SMTP traffic and consider using PGP for end-to-end
  encryption.
#12 lore raw

[PATCH/RFC 2.6.29.2] Driver for CB710/720 memory card reader (MMC part) - v4

Here is next version of the CB710 SD/MMC reader driver. As in previous versions the code is divided in two parts - virtual 'bus' driver that handles PCI device and registers three new devices one per card reader type. The other device handles SD/MMC part of the reader.

Key changes from v3:
- kerneldoc for sg_mapping_iterator extension
- free/restore IRQ during suspend/resume
- protect slot->irq_handler by spinlock instead of RCU
- remove verify_serialization()
- Kconfig
- complete patch for linux kernel 2.6.29.2

My patches for sg_mapping_iterator extension went to /dev/null, so I have incorporated them to the cb710 core driver module for now. Probably someone with more karma points should look at them (sgbuf2.{c,h}) and push further if they are of any value to other driver writers.

drivers/misc/cb710/core.c +356 -0
diff -urN linux-2.6.29.2/drivers/misc/cb710/core.c cb710-tree/drivers/misc/cb710/core.c
@@ -0,0 +1,356 @@
+/*
+ *  cb710/core.c
+ *
+ *  Copyright by Michał Mirosław, 2008-2009
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ */
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/slab.h>
+#include <linux/pci.h>
+#include <linux/spinlock.h>
+#include <linux/idr.h>
+#include <linux/cb710.h>
+
+static DEFINE_IDR(cb710_idr);
+static DEFINE_SPINLOCK(cb710_idr_lock);
+
+void cb710_pci_update_config_reg(struct pci_dev *pdev,
+	int reg, uint32_t mask, uint32_t xor)
+{
+	u32 rval;
+
+	pci_read_config_dword(pdev, reg, &rval);
+	rval = (rval & mask) ^ xor;
+	pci_write_config_dword(pdev, reg, rval);
+}
+EXPORT_SYMBOL_GPL(cb710_pci_update_config_reg);
+
+/* Some magic writes based on Windows driver init code */
+static int __devinit cb710_pci_configure(struct pci_dev *pdev)
+{
+	unsigned int devfn = PCI_DEVFN(PCI_SLOT(pdev->devfn), 0);
+	struct pci_dev *pdev0 = pci_get_slot(pdev->bus, devfn);
+	u32 val;
+
+	cb710_pci_update_config_reg(pdev, 0x48,
+		~0x000000FF, 0x0000003F);
+
+	pci_read_config_dword(pdev, 0x48, &val);
+	if (val & 0x80000000)
+		return 0;
+
+	if (!pdev0)
+		return -ENODEV;
+
+	if (pdev0->vendor == PCI_VENDOR_ID_ENE
+	    && pdev0->device == PCI_DEVICE_ID_ENE_720) {
+		cb710_pci_update_config_reg(pdev0, 0x8C,
+			~0x00F00000, 0x00100000);
+		cb710_pci_update_config_reg(pdev0, 0xB0,
+			~0x08000000, 0x08000000);
+	}
+
+	cb710_pci_update_config_reg(pdev0, 0x8C,
+		~0x00000F00, 0x00000200);
+	cb710_pci_update_config_reg(pdev0, 0x90,
+		~0x00060000, 0x00040000);
+
+	pci_dev_put(pdev0);
+
+	return 0;
+}
+
+static irqreturn_t cb710_irq_handler(int irq, void *data)
+{
+	struct cb710_chip *chip = data;
+	struct cb710_slot *slot = &chip->slot[0];
+	irqreturn_t handled = IRQ_NONE;
+	unsigned nr;
+
+	spin_lock(&chip->irq_lock); /* incl. smp_rmb() */
+
+	for (nr = chip->slots; nr; ++slot, --nr) {
+		cb710_irq_handler_t handler_func = slot->irq_handler;
+		if (handler_func && handler_func(slot))
+			handled = IRQ_HANDLED;
+	}
+
+	spin_unlock(&chip->irq_lock);
+
+	return handled;
+}
+
+static void cb710_release_slot(struct device *dev)
+{
+#ifdef CONFIG_CB710_DEBUG_ASSUMPTIONS
+	struct cb710_slot *slot = cb710_pdev_to_slot(to_platform_device(dev));
+	struct cb710_chip *chip = cb710_slot_to_chip(slot);
+
+	/* slot struct can be freed now */
+	atomic_dec(&chip->slot_refs_count);
+#endif
+}
+
+static int __devinit cb710_register_slot(struct cb710_chip *chip,
+	unsigned slot_mask, unsigned io_offset, const char *name)
+{
+	int nr = chip->slots;
+	struct cb710_slot *slot = &chip->slot[nr];
+	int err;
+
+	dev_dbg(cb710_chip_dev(chip),
+		"register: %s.%d; slot %d; mask %d; IO offset: 0x%02X\n",
+		name, chip->platform_id, nr, slot_mask, io_offset);
+
+	/* slot->irq_handler == NULL here; this needs to be
+	 * seen before platform_device_register() */
+	++chip->slots;
+	smp_wmb();
+
+	slot->iobase = chip->iobase + io_offset;
+	slot->pdev.name = name;
+	slot->pdev.id = chip->platform_id;
+	slot->pdev.dev.parent = &chip->pdev->dev;
+	slot->pdev.dev.release = cb710_release_slot;
+
+	err = platform_device_register(&slot->pdev);
+
+#ifdef CONFIG_CB710_DEBUG_ASSUMPTIONS
+	atomic_inc(&chip->slot_refs_count);
+#endif
+
+	if (err) {
+		/* device_initialize() called from platform_device_register()
+		 * wants this on error path */
+		platform_device_put(&slot->pdev);
+
+		/* slot->irq_handler == NULL here anyway, so no lock needed */
+		--chip->slots;
+		return err;
+	}
+
+	chip->slot_mask |= slot_mask;
+
+	return 0;
+}
+
+static void cb710_unregister_slot(struct cb710_chip *chip,
+	unsigned slot_mask)
+{
+	int nr = chip->slots - 1;
+
+	if (!(chip->slot_mask & slot_mask))
+		return;
+
+	platform_device_unregister(&chip->slot[nr].pdev);
+
+	/* complementary to spin_unlock() in cb710_set_irq_handler() */
+	smp_rmb();
+	BUG_ON(chip->slot[nr].irq_handler != NULL);
+
+	/* slot->irq_handler == NULL here, so no lock needed */
+	--chip->slots;
+	chip->slot_mask &= ~slot_mask;
+}
+
+void cb710_set_irq_handler(struct cb710_slot *slot,
+	cb710_irq_handler_t handler)
+{
+	struct cb710_chip *chip = cb710_slot_to_chip(slot);
+	unsigned long flags;
+
+	spin_lock_irqsave(&chip->irq_lock, flags);
+	slot->irq_handler = handler;
+	spin_unlock_irqrestore(&chip->irq_lock, flags);
+}
+EXPORT_SYMBOL_GPL(cb710_set_irq_handler);
+
+#ifdef CONFIG_PM
+
+static int cb710_suspend(struct pci_dev *pdev, pm_message_t state)
+{
+	struct cb710_chip *chip = pci_get_drvdata(pdev);
+
+	free_irq(pdev->irq, chip);
+	pci_save_state(pdev);
+	pci_disable_device(pdev);
+	if (state.event & PM_EVENT_SLEEP)
+		pci_set_power_state(pdev, PCI_D3cold);
+	return 0;
+}
+
+static int cb710_resume(struct pci_dev *pdev)
+{
+	struct cb710_chip *chip = pci_get_drvdata(pdev);
+	int err;
+
+	pci_set_power_state(pdev, PCI_D0);
+	pci_restore_state(pdev);
+	err = pcim_enable_device(pdev);
+	if (err)
+		return err;
+
+	return devm_request_irq(&pdev->dev, pdev->irq,
+		cb710_irq_handler, IRQF_SHARED, KBUILD_MODNAME, chip);
+}
+
+#endif /* CONFIG_PM */
+
+static int __devinit cb710_probe(struct pci_dev *pdev,
+	const struct pci_device_id *ent)
+{
+	struct cb710_chip *chip;
+	unsigned long flags;
+	u32 val;
+	int err;
+	int n = 0;
+
+	err = cb710_pci_configure(pdev);
+	if (err)
+		return err;
+
+	/* this is actually magic... */
+	pci_read_config_dword(pdev, 0x48, &val);
+	if (!(val & 0x80000000)) {
+		pci_write_config_dword(pdev, 0x48, val|0x71000000);
+		pci_read_config_dword(pdev, 0x48, &val);
+	}
+
+	dev_dbg(&pdev->dev, "PCI config[0x48] = 0x%08X\n", val);
+	if (!(val & 0x70000000))
+		return -ENODEV;
+	val = (val >> 28) & 7;
+	if (val & CB710_SLOT_MMC)
+		++n;
+	if (val & CB710_SLOT_MS)
+		++n;
+	if (val & CB710_SLOT_SM)
+		++n;
+
+	chip = devm_kzalloc(&pdev->dev,
+		sizeof(*chip) + n * sizeof(*chip->slot), GFP_KERNEL);
+	if (!chip)
+		return -ENOMEM;
+
+	err = pcim_enable_device(pdev);
+	if (err)
+		return err;
+
+	err = pcim_iomap_regions(pdev, 0x0001, KBUILD_MODNAME);
+	if (err)
+		return err;
+
+	chip->pdev = pdev;
+	chip->iobase = pcim_iomap_table(pdev)[0];
+
+	pci_set_drvdata(pdev, chip);
+
+	err = devm_request_irq(&pdev->dev, pdev->irq,
+		cb710_irq_handler, IRQF_SHARED, KBUILD_MODNAME, chip);
+	if (err)
+		return err;
+
+	do {
+		if (!idr_pre_get(&cb710_idr, GFP_KERNEL))
+			return -ENOMEM;
+
+		spin_lock_irqsave(&cb710_idr_lock, flags);
+		err = idr_get_new(&cb710_idr, chip, &chip->platform_id);
+		spin_unlock_irqrestore(&cb710_idr_lock, flags);
+
+		if (err && err != -EAGAIN)
+			return err;
+	} while (err);
+
+
+	dev_info(&pdev->dev, "id %d, IO 0x%p, IRQ %d\n",
+		chip->platform_id, chip->iobase, pdev->irq);
+
+	if (val & CB710_SLOT_MMC) {	/* MMC/SD slot */
+		err = cb710_register_slot(chip,
+			CB710_SLOT_MMC, 0x00, "cb710-mmc");
+		if (err)
+			return err;
+	}
+
+	if (val & CB710_SLOT_MS) {	/* MemoryStick slot */
+		err = cb710_register_slot(chip,
+			CB710_SLOT_MS, 0x40, "cb710-ms");
+		if (err)
+			goto unreg_mmc;
+	}
+
+	if (val & CB710_SLOT_SM) {	/* SmartMedia slot */
+		err = cb710_register_slot(chip,
+			CB710_SLOT_SM, 0x60, "cb710-sm");
+		if (err)
+			goto unreg_ms;
+	}
+
+	return 0;
+unreg_ms:
+	cb710_unregister_slot(chip, CB710_SLOT_MS);
+unreg_mmc:
+	cb710_unregister_slot(chip, CB710_SLOT_MMC);
+
+#ifdef CONFIG_CB710_DEBUG_ASSUMPTIONS
+	BUG_ON(atomic_read(&chip->slot_refs_count) != 0);
+#endif
+	return err;
+}
+
+static void __devexit cb710_remove_one(struct pci_dev *pdev)
+{
+	struct cb710_chip *chip = pci_get_drvdata(pdev);
+	unsigned long flags;
+
+	cb710_unregister_slot(chip, CB710_SLOT_SM);
+	cb710_unregister_slot(chip, CB710_SLOT_MS);
+	cb710_unregister_slot(chip, CB710_SLOT_MMC);
+#ifdef CONFIG_CB710_DEBUG_ASSUMPTIONS
+	BUG_ON(atomic_read(&chip->slot_refs_count) != 0);
+#endif
+
+	spin_lock_irqsave(&cb710_idr_lock, flags);
+	idr_remove(&cb710_idr, chip->platform_id);
+	spin_unlock_irqrestore(&cb710_idr_lock, flags);
+}
+
+static const struct pci_device_id cb710_pci_tbl[] = {
+	{ PCI_VENDOR_ID_ENE, PCI_DEVICE_ID_ENE_CB710_FLASH,
+		PCI_ANY_ID, PCI_ANY_ID, },
+	{ 0, }
+};
+
+static struct pci_driver cb710_driver = {
+	.name = KBUILD_MODNAME,
+	.id_table = cb710_pci_tbl,
+	.probe = cb710_probe,
+	.remove = __devexit_p(cb710_remove_one),
+#ifdef CONFIG_PM
+	.suspend = cb710_suspend,
+	.resume = cb710_resume,
+#endif
+};
+
+static int __init cb710_init_module(void)
+{
+	return pci_register_driver(&cb710_driver);
+}
+
+static void __exit cb710_cleanup_module(void)
+{
+	pci_unregister_driver(&cb710_driver);
+}
+
+module_init(cb710_init_module);
+module_exit(cb710_cleanup_module);
+
+MODULE_AUTHOR("Michał Mirosław <[email protected]>");
+MODULE_DESCRIPTION("ENE CB710 memory card reader driver");
+MODULE_LICENSE("GPL");
+MODULE_DEVICE_TABLE(pci, cb710_pci_tbl);
drivers/misc/cb710/debug.c +119 -0

119 lines not on this page · raw message

drivers/misc/cb710/Kconfig +21 -0
diff -urN linux-2.6.29.2/drivers/misc/cb710/Kconfig cb710-tree/drivers/misc/cb710/Kconfig
@@ -0,0 +1,21 @@
+config CB710_CORE
+	tristate "ENE CB710/720 Flash memory card reader support"
+	depends on PCI
+	help
+	  This option enables support for PCI ENE CB710/720 Flash memory card
+	  reader found in some laptops (ie. some versions of HP Compaq nx9500).
+
+	  You will also have to select some flash card format drivers (MMC/SD,
+	  MemoryStick).
+
+	  This driver can also be built as a module. If so, the module
+	  will be called cb710.
+
+config CB710_DEBUG
+	bool "Enable driver debugging"
+	depends on CB710_CORE != n
+	default n
+	help
+	  This is an option for use by developers; most people should
+	  say N here.  This adds a lot of debugging output to dmesg.
+
drivers/misc/cb710/Makefile +4 -0
diff -urN linux-2.6.29.2/drivers/misc/cb710/Makefile cb710-tree/drivers/misc/cb710/Makefile
@@ -0,0 +1,4 @@
+obj-$(CONFIG_CB710_CORE)	+= cb710.o
+
+cb710-y				:= core.o sgbuf2.o
+cb710-$(CONFIG_CB710_DEBUG)	+= debug.o
drivers/misc/cb710/sgbuf2.c +150 -0

150 lines not on this page · raw message

drivers/misc/Kconfig +1 -0
diff -urN linux-2.6.29.2/drivers/misc/Kconfig cb710-tree/drivers/misc/Kconfig
@@ -225,5 +225,6 @@
 
 source "drivers/misc/c2port/Kconfig"
 source "drivers/misc/eeprom/Kconfig"
+source "drivers/misc/cb710/Kconfig"
 
 endif # MISC_DEVICES
drivers/misc/Makefile +1 -0
diff -urN linux-2.6.29.2/drivers/misc/Makefile cb710-tree/drivers/misc/Makefile
@@ -20,3 +20,4 @@
 obj-$(CONFIG_HP_ILO)		+= hpilo.o
 obj-$(CONFIG_C2PORT)		+= c2port/
 obj-y				+= eeprom/
+obj-y				+= cb710/
drivers/mmc/host/cb710-mmc.c +805 -0

805 lines not on this page · raw message

drivers/mmc/host/cb710-mmc.h +104 -0

104 lines not on this page · raw message

drivers/mmc/host/Kconfig +12 -0
diff -urN linux-2.6.29.2/drivers/mmc/host/Kconfig cb710-tree/drivers/mmc/host/Kconfig
@@ -213,3 +213,15 @@
 	  This provides support for the SD/MMC cell found in TC6393XB,
 	  T7L66XB and also ipaq ASIC3
 
+config MMC_CB710
+	tristate "ENE CB710 MMC/SD Interface support"
+	depends on PCI
+	select CB710_CORE
+	help
+	  This option enables support for MMC/SD part of ENE CB710/720 Flash
+	  memory card reader found in some laptops (ie. some versions of
+	  HP Compaq nx9500).
+
+	  This driver can also be built as a module. If so, the module
+	  will be called cb710-mmc.
+
drivers/mmc/host/Makefile +1 -0
diff -urN linux-2.6.29.2/drivers/mmc/host/Makefile cb710-tree/drivers/mmc/host/Makefile
@@ -27,4 +27,5 @@
 obj-$(CONFIG_MMC_S3C)   	+= s3cmci.o
 obj-$(CONFIG_MMC_SDRICOH_CS)	+= sdricoh_cs.o
 obj-$(CONFIG_MMC_TMIO)		+= tmio_mmc.o
+obj-$(CONFIG_MMC_CB710)	+= cb710-mmc.o
 
include/linux/cb710.h +134 -0

134 lines not on this page · raw message

include/linux/pci_ids.h +1 -0
diff -urN linux-2.6.29.2/include/linux/pci_ids.h cb710-tree/include/linux/pci_ids.h
@@ -2077,6 +2077,7 @@
 #define PCI_VENDOR_ID_MAINPINE		0x1522
 #define PCI_DEVICE_ID_MAINPINE_PBRIDGE	0x0100
 #define PCI_VENDOR_ID_ENE		0x1524
+#define PCI_DEVICE_ID_ENE_CB710_FLASH	0x0510
 #define PCI_DEVICE_ID_ENE_CB712_SD	0x0550
 #define PCI_DEVICE_ID_ENE_CB712_SD_2	0x0551
 #define PCI_DEVICE_ID_ENE_CB714_SD	0x0750
include/linux/sgbuf2.h +104 -0

104 lines not on this page · raw message

#13 lore raw

Re: [PATCH/RFC 2.6.29.2] Driver for CB710/720 memory card reader (MMC part) - v4

Quoted 5 lines · On Fri, 8 May 2009 17:16:22 +0200 Michał Mirosław <[email protected]> wrote:

Here is next version of the CB710 SD/MMC reader driver. As in previous versions the code is divided in two parts - virtual 'bus' driver that handles PCI device and registers three new devices one per card reader type. The other device handles SD/MMC part of the reader.

Quoted 8 lines

Key changes from v3:
- kerneldoc for sg_mapping_iterator extension
- free/restore IRQ during suspend/resume
- protect slot->irq_handler by spinlock instead of RCU
- remove verify_serialization()
- Kconfig
- complete patch for linux kernel 2.6.29.2

Looks good to me. I just want you to add a MAINTAINERS entry so that people know who to contact if they have problems with this driver.

Quoted 4 lines

My patches for sg_mapping_iterator extension went to /dev/null, so I have incorporated them to the cb710 core driver module for now. Probably someone with more karma points should look at them (sgbuf2.{c,h}) and push further if they are of any value to other driver writers.

Give them to Andrew Morton. He's good at finding the right people and making sure they notice. :)

Rgds

Signature · 6 lines
     -- Pierre Ossman

  WARNING: This correspondence is being monitored by the
  Swedish government. Make sure your server uses encryption
  for SMTP traffic and consider using PGP for end-to-end
  encryption.
author maintainer
#14 lore raw

PATCH 1/1 Driver for CB710/720 memory card reader (MMC part) - v5

Here is a version of the CB710 SD/MMC reader driver ready for inclusion upstream. The code is divided in two parts. There is a virtual 'bus' driver that handles PCI device and registers three new devices one per card reader type. The other driver handles SD/MMC part of the reader.

Changes from v4:
- sg_mapping_iterator extension renamed to avoid namespace clashes when/if the extension would be merged upstream
- appended sgbuf2.h to cb710.h
- move from IDR to IDA for device ID generation
- cleanup after IDA on module unload
- added MAINTAINERS entry

drivers/misc/cb710/core.c +357 -0
diff -urpN linux-2.6.29.3/drivers/misc/cb710/core.c test2/drivers/misc/cb710/core.c
@@ -0,0 +1,357 @@
+/*
+ *  cb710/core.c
+ *
+ *  Copyright by Michał Mirosław, 2008-2009
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ */
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/slab.h>
+#include <linux/pci.h>
+#include <linux/spinlock.h>
+#include <linux/idr.h>
+#include <linux/cb710.h>
+
+static DEFINE_IDA(cb710_ida);
+static DEFINE_SPINLOCK(cb710_ida_lock);
+
+void cb710_pci_update_config_reg(struct pci_dev *pdev,
+	int reg, uint32_t mask, uint32_t xor)
+{
+	u32 rval;
+
+	pci_read_config_dword(pdev, reg, &rval);
+	rval = (rval & mask) ^ xor;
+	pci_write_config_dword(pdev, reg, rval);
+}
+EXPORT_SYMBOL_GPL(cb710_pci_update_config_reg);
+
+/* Some magic writes based on Windows driver init code */
+static int __devinit cb710_pci_configure(struct pci_dev *pdev)
+{
+	unsigned int devfn = PCI_DEVFN(PCI_SLOT(pdev->devfn), 0);
+	struct pci_dev *pdev0 = pci_get_slot(pdev->bus, devfn);
+	u32 val;
+
+	cb710_pci_update_config_reg(pdev, 0x48,
+		~0x000000FF, 0x0000003F);
+
+	pci_read_config_dword(pdev, 0x48, &val);
+	if (val & 0x80000000)
+		return 0;
+
+	if (!pdev0)
+		return -ENODEV;
+
+	if (pdev0->vendor == PCI_VENDOR_ID_ENE
+	    && pdev0->device == PCI_DEVICE_ID_ENE_720) {
+		cb710_pci_update_config_reg(pdev0, 0x8C,
+			~0x00F00000, 0x00100000);
+		cb710_pci_update_config_reg(pdev0, 0xB0,
+			~0x08000000, 0x08000000);
+	}
+
+	cb710_pci_update_config_reg(pdev0, 0x8C,
+		~0x00000F00, 0x00000200);
+	cb710_pci_update_config_reg(pdev0, 0x90,
+		~0x00060000, 0x00040000);
+
+	pci_dev_put(pdev0);
+
+	return 0;
+}
+
+static irqreturn_t cb710_irq_handler(int irq, void *data)
+{
+	struct cb710_chip *chip = data;
+	struct cb710_slot *slot = &chip->slot[0];
+	irqreturn_t handled = IRQ_NONE;
+	unsigned nr;
+
+	spin_lock(&chip->irq_lock); /* incl. smp_rmb() */
+
+	for (nr = chip->slots; nr; ++slot, --nr) {
+		cb710_irq_handler_t handler_func = slot->irq_handler;
+		if (handler_func && handler_func(slot))
+			handled = IRQ_HANDLED;
+	}
+
+	spin_unlock(&chip->irq_lock);
+
+	return handled;
+}
+
+static void cb710_release_slot(struct device *dev)
+{
+#ifdef CONFIG_CB710_DEBUG_ASSUMPTIONS
+	struct cb710_slot *slot = cb710_pdev_to_slot(to_platform_device(dev));
+	struct cb710_chip *chip = cb710_slot_to_chip(slot);
+
+	/* slot struct can be freed now */
+	atomic_dec(&chip->slot_refs_count);
+#endif
+}
+
+static int __devinit cb710_register_slot(struct cb710_chip *chip,
+	unsigned slot_mask, unsigned io_offset, const char *name)
+{
+	int nr = chip->slots;
+	struct cb710_slot *slot = &chip->slot[nr];
+	int err;
+
+	dev_dbg(cb710_chip_dev(chip),
+		"register: %s.%d; slot %d; mask %d; IO offset: 0x%02X\n",
+		name, chip->platform_id, nr, slot_mask, io_offset);
+
+	/* slot->irq_handler == NULL here; this needs to be
+	 * seen before platform_device_register() */
+	++chip->slots;
+	smp_wmb();
+
+	slot->iobase = chip->iobase + io_offset;
+	slot->pdev.name = name;
+	slot->pdev.id = chip->platform_id;
+	slot->pdev.dev.parent = &chip->pdev->dev;
+	slot->pdev.dev.release = cb710_release_slot;
+
+	err = platform_device_register(&slot->pdev);
+
+#ifdef CONFIG_CB710_DEBUG_ASSUMPTIONS
+	atomic_inc(&chip->slot_refs_count);
+#endif
+
+	if (err) {
+		/* device_initialize() called from platform_device_register()
+		 * wants this on error path */
+		platform_device_put(&slot->pdev);
+
+		/* slot->irq_handler == NULL here anyway, so no lock needed */
+		--chip->slots;
+		return err;
+	}
+
+	chip->slot_mask |= slot_mask;
+
+	return 0;
+}
+
+static void cb710_unregister_slot(struct cb710_chip *chip,
+	unsigned slot_mask)
+{
+	int nr = chip->slots - 1;
+
+	if (!(chip->slot_mask & slot_mask))
+		return;
+
+	platform_device_unregister(&chip->slot[nr].pdev);
+
+	/* complementary to spin_unlock() in cb710_set_irq_handler() */
+	smp_rmb();
+	BUG_ON(chip->slot[nr].irq_handler != NULL);
+
+	/* slot->irq_handler == NULL here, so no lock needed */
+	--chip->slots;
+	chip->slot_mask &= ~slot_mask;
+}
+
+void cb710_set_irq_handler(struct cb710_slot *slot,
+	cb710_irq_handler_t handler)
+{
+	struct cb710_chip *chip = cb710_slot_to_chip(slot);
+	unsigned long flags;
+
+	spin_lock_irqsave(&chip->irq_lock, flags);
+	slot->irq_handler = handler;
+	spin_unlock_irqrestore(&chip->irq_lock, flags);
+}
+EXPORT_SYMBOL_GPL(cb710_set_irq_handler);
+
+#ifdef CONFIG_PM
+
+static int cb710_suspend(struct pci_dev *pdev, pm_message_t state)
+{
+	struct cb710_chip *chip = pci_get_drvdata(pdev);
+
+	free_irq(pdev->irq, chip);
+	pci_save_state(pdev);
+	pci_disable_device(pdev);
+	if (state.event & PM_EVENT_SLEEP)
+		pci_set_power_state(pdev, PCI_D3cold);
+	return 0;
+}
+
+static int cb710_resume(struct pci_dev *pdev)
+{
+	struct cb710_chip *chip = pci_get_drvdata(pdev);
+	int err;
+
+	pci_set_power_state(pdev, PCI_D0);
+	pci_restore_state(pdev);
+	err = pcim_enable_device(pdev);
+	if (err)
+		return err;
+
+	return devm_request_irq(&pdev->dev, pdev->irq,
+		cb710_irq_handler, IRQF_SHARED, KBUILD_MODNAME, chip);
+}
+
+#endif /* CONFIG_PM */
+
+static int __devinit cb710_probe(struct pci_dev *pdev,
+	const struct pci_device_id *ent)
+{
+	struct cb710_chip *chip;
+	unsigned long flags;
+	u32 val;
+	int err;
+	int n = 0;
+
+	err = cb710_pci_configure(pdev);
+	if (err)
+		return err;
+
+	/* this is actually magic... */
+	pci_read_config_dword(pdev, 0x48, &val);
+	if (!(val & 0x80000000)) {
+		pci_write_config_dword(pdev, 0x48, val|0x71000000);
+		pci_read_config_dword(pdev, 0x48, &val);
+	}
+
+	dev_dbg(&pdev->dev, "PCI config[0x48] = 0x%08X\n", val);
+	if (!(val & 0x70000000))
+		return -ENODEV;
+	val = (val >> 28) & 7;
+	if (val & CB710_SLOT_MMC)
+		++n;
+	if (val & CB710_SLOT_MS)
+		++n;
+	if (val & CB710_SLOT_SM)
+		++n;
+
+	chip = devm_kzalloc(&pdev->dev,
+		sizeof(*chip) + n * sizeof(*chip->slot), GFP_KERNEL);
+	if (!chip)
+		return -ENOMEM;
+
+	err = pcim_enable_device(pdev);
+	if (err)
+		return err;
+
+	err = pcim_iomap_regions(pdev, 0x0001, KBUILD_MODNAME);
+	if (err)
+		return err;
+
+	chip->pdev = pdev;
+	chip->iobase = pcim_iomap_table(pdev)[0];
+
+	pci_set_drvdata(pdev, chip);
+
+	err = devm_request_irq(&pdev->dev, pdev->irq,
+		cb710_irq_handler, IRQF_SHARED, KBUILD_MODNAME, chip);
+	if (err)
+		return err;
+
+	do {
+		if (!ida_pre_get(&cb710_ida, GFP_KERNEL))
+			return -ENOMEM;
+
+		spin_lock_irqsave(&cb710_ida_lock, flags);
+		err = ida_get_new(&cb710_ida, &chip->platform_id);
+		spin_unlock_irqrestore(&cb710_ida_lock, flags);
+
+		if (err && err != -EAGAIN)
+			return err;
+	} while (err);
+
+
+	dev_info(&pdev->dev, "id %d, IO 0x%p, IRQ %d\n",
+		chip->platform_id, chip->iobase, pdev->irq);
+
+	if (val & CB710_SLOT_MMC) {	/* MMC/SD slot */
+		err = cb710_register_slot(chip,
+			CB710_SLOT_MMC, 0x00, "cb710-mmc");
+		if (err)
+			return err;
+	}
+
+	if (val & CB710_SLOT_MS) {	/* MemoryStick slot */
+		err = cb710_register_slot(chip,
+			CB710_SLOT_MS, 0x40, "cb710-ms");
+		if (err)
+			goto unreg_mmc;
+	}
+
+	if (val & CB710_SLOT_SM) {	/* SmartMedia slot */
+		err = cb710_register_slot(chip,
+			CB710_SLOT_SM, 0x60, "cb710-sm");
+		if (err)
+			goto unreg_ms;
+	}
+
+	return 0;
+unreg_ms:
+	cb710_unregister_slot(chip, CB710_SLOT_MS);
+unreg_mmc:
+	cb710_unregister_slot(chip, CB710_SLOT_MMC);
+
+#ifdef CONFIG_CB710_DEBUG_ASSUMPTIONS
+	BUG_ON(atomic_read(&chip->slot_refs_count) != 0);
+#endif
+	return err;
+}
+
+static void __devexit cb710_remove_one(struct pci_dev *pdev)
+{
+	struct cb710_chip *chip = pci_get_drvdata(pdev);
+	unsigned long flags;
+
+	cb710_unregister_slot(chip, CB710_SLOT_SM);
+	cb710_unregister_slot(chip, CB710_SLOT_MS);
+	cb710_unregister_slot(chip, CB710_SLOT_MMC);
+#ifdef CONFIG_CB710_DEBUG_ASSUMPTIONS
+	BUG_ON(atomic_read(&chip->slot_refs_count) != 0);
+#endif
+
+	spin_lock_irqsave(&cb710_ida_lock, flags);
+	ida_remove(&cb710_ida, chip->platform_id);
+	spin_unlock_irqrestore(&cb710_ida_lock, flags);
+}
+
+static const struct pci_device_id cb710_pci_tbl[] = {
+	{ PCI_VENDOR_ID_ENE, PCI_DEVICE_ID_ENE_CB710_FLASH,
+		PCI_ANY_ID, PCI_ANY_ID, },
+	{ 0, }
+};
+
+static struct pci_driver cb710_driver = {
+	.name = KBUILD_MODNAME,
+	.id_table = cb710_pci_tbl,
+	.probe = cb710_probe,
+	.remove = __devexit_p(cb710_remove_one),
+#ifdef CONFIG_PM
+	.suspend = cb710_suspend,
+	.resume = cb710_resume,
+#endif
+};
+
+static int __init cb710_init_module(void)
+{
+	return pci_register_driver(&cb710_driver);
+}
+
+static void __exit cb710_cleanup_module(void)
+{
+	pci_unregister_driver(&cb710_driver);
+	ida_destroy(&cb710_ida);
+}
+
+module_init(cb710_init_module);
+module_exit(cb710_cleanup_module);
+
+MODULE_AUTHOR("Michał Mirosław <[email protected]>");
+MODULE_DESCRIPTION("ENE CB710 memory card reader driver");
+MODULE_LICENSE("GPL");
+MODULE_DEVICE_TABLE(pci, cb710_pci_tbl);
drivers/misc/cb710/debug.c +119 -0

119 lines not on this page · raw message

drivers/misc/cb710/Kconfig +21 -0
diff -urpN linux-2.6.29.3/drivers/misc/cb710/Kconfig test2/drivers/misc/cb710/Kconfig
@@ -0,0 +1,21 @@
+config CB710_CORE
+	tristate "ENE CB710/720 Flash memory card reader support"
+	depends on PCI
+	help
+	  This option enables support for PCI ENE CB710/720 Flash memory card
+	  reader found in some laptops (ie. some versions of HP Compaq nx9500).
+
+	  You will also have to select some flash card format drivers (MMC/SD,
+	  MemoryStick).
+
+	  This driver can also be built as a module. If so, the module
+	  will be called cb710.
+
+config CB710_DEBUG
+	bool "Enable driver debugging"
+	depends on CB710_CORE != n
+	default n
+	help
+	  This is an option for use by developers; most people should
+	  say N here.  This adds a lot of debugging output to dmesg.
+
drivers/misc/cb710/Makefile +4 -0
diff -urpN linux-2.6.29.3/drivers/misc/cb710/Makefile test2/drivers/misc/cb710/Makefile
@@ -0,0 +1,4 @@
+obj-$(CONFIG_CB710_CORE)	+= cb710.o
+
+cb710-y				:= core.o sgbuf2.o
+cb710-$(CONFIG_CB710_DEBUG)	+= debug.o
drivers/misc/cb710/sgbuf2.c +150 -0

150 lines not on this page · raw message

drivers/misc/Kconfig +1 -0
diff -urpN linux-2.6.29.3/drivers/misc/Kconfig test2/drivers/misc/Kconfig
@@ -225,5 +225,6 @@ config DELL_LAPTOP
 
 source "drivers/misc/c2port/Kconfig"
 source "drivers/misc/eeprom/Kconfig"
+source "drivers/misc/cb710/Kconfig"
 
 endif # MISC_DEVICES
drivers/misc/Makefile +1 -0
diff -urpN linux-2.6.29.3/drivers/misc/Makefile test2/drivers/misc/Makefile
@@ -20,3 +20,4 @@ obj-$(CONFIG_SGI_GRU)		+= sgi-gru/
 obj-$(CONFIG_HP_ILO)		+= hpilo.o
 obj-$(CONFIG_C2PORT)		+= c2port/
 obj-y				+= eeprom/
+obj-y				+= cb710/
drivers/mmc/host/cb710-mmc.c +804 -0

804 lines not on this page · raw message

drivers/mmc/host/cb710-mmc.h +104 -0

104 lines not on this page · raw message

drivers/mmc/host/Kconfig +13 -0
diff -urpN linux-2.6.29.3/drivers/mmc/host/Kconfig test2/drivers/mmc/host/Kconfig
@@ -212,3 +212,16 @@ config MMC_TMIO
 	help
 	  This provides support for the SD/MMC cell found in TC6393XB,
 	  T7L66XB and also ipaq ASIC3
+
+config MMC_CB710
+	tristate "ENE CB710 MMC/SD Interface support"
+	depends on PCI
+	select CB710_CORE
+	help
+	  This option enables support for MMC/SD part of ENE CB710/720 Flash
+	  memory card reader found in some laptops (ie. some versions of
+	  HP Compaq nx9500).
+
+	  This driver can also be built as a module. If so, the module
+	  will be called cb710-mmc.
+
drivers/mmc/host/Makefile +1 -0
diff -urpN linux-2.6.29.3/drivers/mmc/host/Makefile test2/drivers/mmc/host/Makefile
@@ -27,4 +27,5 @@ endif
 obj-$(CONFIG_MMC_S3C)   	+= s3cmci.o
 obj-$(CONFIG_MMC_SDRICOH_CS)	+= sdricoh_cs.o
 obj-$(CONFIG_MMC_TMIO)		+= tmio_mmc.o
+obj-$(CONFIG_MMC_CB710)	+= cb710-mmc.o
 
include/linux/cb710.h +238 -0

238 lines not on this page · raw message

include/linux/pci_ids.h +1 -0
diff -urpN linux-2.6.29.3/include/linux/pci_ids.h test2/include/linux/pci_ids.h
@@ -2077,6 +2077,7 @@
 #define PCI_VENDOR_ID_MAINPINE		0x1522
 #define PCI_DEVICE_ID_MAINPINE_PBRIDGE	0x0100
 #define PCI_VENDOR_ID_ENE		0x1524
+#define PCI_DEVICE_ID_ENE_CB710_FLASH	0x0510
 #define PCI_DEVICE_ID_ENE_CB712_SD	0x0550
 #define PCI_DEVICE_ID_ENE_CB712_SD_2	0x0551
 #define PCI_DEVICE_ID_ENE_CB714_SD	0x0750
MAINTAINERS +9 -0
diff -urpN linux-2.6.29.3/MAINTAINERS test2/MAINTAINERS
@@ -1660,6 +1660,15 @@ L:	[email protected]
 W:	http://sourceforge.net/projects/lpfcxxxx
 S:	Supported
 
+ENE CB710 FLASH CARD READER DRIVER
+P:	Michał Mirosław
+M:	[email protected]
+L:	[email protected]
+S:	Maintained
+F:	drivers/misc/cb710/
+F:	drivers/mmc/host/cb710-mmc.*
+F:	include/linux/cb710.h
+
 EPSON 1355 FRAMEBUFFER DRIVER
 P:	Christopher Hoover
 M:	[email protected], [email protected]
author maintainer
#15 lore raw

PATCH 1/1 Driver for CB710/720 memory card reader (MMC part) - v5 fixed

[I messed up source trees for previous diff, please use this one instead.]

Here is a version of the CB710 SD/MMC reader driver ready for inclusion upstream. The code is divided in two parts. There is a virtual 'bus' driver that handles PCI device and registers three new devices one per card reader type. The other driver handles SD/MMC part of the reader.

Changes from v4:
- sg_mapping_iterator extension renamed to avoid namespace clashes when/if the extension would be merged upstream
- appended sgbuf2.h to cb710.h
- move from IDR to IDA for device ID generation
- cleanup after IDA on module unload
- added MAINTAINERS entry

drivers/misc/cb710/core.c +357 -0
diff -urpN linux-2.6.29.4/drivers/misc/cb710/core.c test2/drivers/misc/cb710/core.c
@@ -0,0 +1,357 @@
+/*
+ *  cb710/core.c
+ *
+ *  Copyright by Michał Mirosław, 2008-2009
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ */
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/slab.h>
+#include <linux/pci.h>
+#include <linux/spinlock.h>
+#include <linux/idr.h>
+#include <linux/cb710.h>
+
+static DEFINE_IDA(cb710_ida);
+static DEFINE_SPINLOCK(cb710_ida_lock);
+
+void cb710_pci_update_config_reg(struct pci_dev *pdev,
+	int reg, uint32_t mask, uint32_t xor)
+{
+	u32 rval;
+
+	pci_read_config_dword(pdev, reg, &rval);
+	rval = (rval & mask) ^ xor;
+	pci_write_config_dword(pdev, reg, rval);
+}
+EXPORT_SYMBOL_GPL(cb710_pci_update_config_reg);
+
+/* Some magic writes based on Windows driver init code */
+static int __devinit cb710_pci_configure(struct pci_dev *pdev)
+{
+	unsigned int devfn = PCI_DEVFN(PCI_SLOT(pdev->devfn), 0);
+	struct pci_dev *pdev0 = pci_get_slot(pdev->bus, devfn);
+	u32 val;
+
+	cb710_pci_update_config_reg(pdev, 0x48,
+		~0x000000FF, 0x0000003F);
+
+	pci_read_config_dword(pdev, 0x48, &val);
+	if (val & 0x80000000)
+		return 0;
+
+	if (!pdev0)
+		return -ENODEV;
+
+	if (pdev0->vendor == PCI_VENDOR_ID_ENE
+	    && pdev0->device == PCI_DEVICE_ID_ENE_720) {
+		cb710_pci_update_config_reg(pdev0, 0x8C,
+			~0x00F00000, 0x00100000);
+		cb710_pci_update_config_reg(pdev0, 0xB0,
+			~0x08000000, 0x08000000);
+	}
+
+	cb710_pci_update_config_reg(pdev0, 0x8C,
+		~0x00000F00, 0x00000200);
+	cb710_pci_update_config_reg(pdev0, 0x90,
+		~0x00060000, 0x00040000);
+
+	pci_dev_put(pdev0);
+
+	return 0;
+}
+
+static irqreturn_t cb710_irq_handler(int irq, void *data)
+{
+	struct cb710_chip *chip = data;
+	struct cb710_slot *slot = &chip->slot[0];
+	irqreturn_t handled = IRQ_NONE;
+	unsigned nr;
+
+	spin_lock(&chip->irq_lock); /* incl. smp_rmb() */
+
+	for (nr = chip->slots; nr; ++slot, --nr) {
+		cb710_irq_handler_t handler_func = slot->irq_handler;
+		if (handler_func && handler_func(slot))
+			handled = IRQ_HANDLED;
+	}
+
+	spin_unlock(&chip->irq_lock);
+
+	return handled;
+}
+
+static void cb710_release_slot(struct device *dev)
+{
+#ifdef CONFIG_CB710_DEBUG_ASSUMPTIONS
+	struct cb710_slot *slot = cb710_pdev_to_slot(to_platform_device(dev));
+	struct cb710_chip *chip = cb710_slot_to_chip(slot);
+
+	/* slot struct can be freed now */
+	atomic_dec(&chip->slot_refs_count);
+#endif
+}
+
+static int __devinit cb710_register_slot(struct cb710_chip *chip,
+	unsigned slot_mask, unsigned io_offset, const char *name)
+{
+	int nr = chip->slots;
+	struct cb710_slot *slot = &chip->slot[nr];
+	int err;
+
+	dev_dbg(cb710_chip_dev(chip),
+		"register: %s.%d; slot %d; mask %d; IO offset: 0x%02X\n",
+		name, chip->platform_id, nr, slot_mask, io_offset);
+
+	/* slot->irq_handler == NULL here; this needs to be
+	 * seen before platform_device_register() */
+	++chip->slots;
+	smp_wmb();
+
+	slot->iobase = chip->iobase + io_offset;
+	slot->pdev.name = name;
+	slot->pdev.id = chip->platform_id;
+	slot->pdev.dev.parent = &chip->pdev->dev;
+	slot->pdev.dev.release = cb710_release_slot;
+
+	err = platform_device_register(&slot->pdev);
+
+#ifdef CONFIG_CB710_DEBUG_ASSUMPTIONS
+	atomic_inc(&chip->slot_refs_count);
+#endif
+
+	if (err) {
+		/* device_initialize() called from platform_device_register()
+		 * wants this on error path */
+		platform_device_put(&slot->pdev);
+
+		/* slot->irq_handler == NULL here anyway, so no lock needed */
+		--chip->slots;
+		return err;
+	}
+
+	chip->slot_mask |= slot_mask;
+
+	return 0;
+}
+
+static void cb710_unregister_slot(struct cb710_chip *chip,
+	unsigned slot_mask)
+{
+	int nr = chip->slots - 1;
+
+	if (!(chip->slot_mask & slot_mask))
+		return;
+
+	platform_device_unregister(&chip->slot[nr].pdev);
+
+	/* complementary to spin_unlock() in cb710_set_irq_handler() */
+	smp_rmb();
+	BUG_ON(chip->slot[nr].irq_handler != NULL);
+
+	/* slot->irq_handler == NULL here, so no lock needed */
+	--chip->slots;
+	chip->slot_mask &= ~slot_mask;
+}
+
+void cb710_set_irq_handler(struct cb710_slot *slot,
+	cb710_irq_handler_t handler)
+{
+	struct cb710_chip *chip = cb710_slot_to_chip(slot);
+	unsigned long flags;
+
+	spin_lock_irqsave(&chip->irq_lock, flags);
+	slot->irq_handler = handler;
+	spin_unlock_irqrestore(&chip->irq_lock, flags);
+}
+EXPORT_SYMBOL_GPL(cb710_set_irq_handler);
+
+#ifdef CONFIG_PM
+
+static int cb710_suspend(struct pci_dev *pdev, pm_message_t state)
+{
+	struct cb710_chip *chip = pci_get_drvdata(pdev);
+
+	free_irq(pdev->irq, chip);
+	pci_save_state(pdev);
+	pci_disable_device(pdev);
+	if (state.event & PM_EVENT_SLEEP)
+		pci_set_power_state(pdev, PCI_D3cold);
+	return 0;
+}
+
+static int cb710_resume(struct pci_dev *pdev)
+{
+	struct cb710_chip *chip = pci_get_drvdata(pdev);
+	int err;
+
+	pci_set_power_state(pdev, PCI_D0);
+	pci_restore_state(pdev);
+	err = pcim_enable_device(pdev);
+	if (err)
+		return err;
+
+	return devm_request_irq(&pdev->dev, pdev->irq,
+		cb710_irq_handler, IRQF_SHARED, KBUILD_MODNAME, chip);
+}
+
+#endif /* CONFIG_PM */
+
+static int __devinit cb710_probe(struct pci_dev *pdev,
+	const struct pci_device_id *ent)
+{
+	struct cb710_chip *chip;
+	unsigned long flags;
+	u32 val;
+	int err;
+	int n = 0;
+
+	err = cb710_pci_configure(pdev);
+	if (err)
+		return err;
+
+	/* this is actually magic... */
+	pci_read_config_dword(pdev, 0x48, &val);
+	if (!(val & 0x80000000)) {
+		pci_write_config_dword(pdev, 0x48, val|0x71000000);
+		pci_read_config_dword(pdev, 0x48, &val);
+	}
+
+	dev_dbg(&pdev->dev, "PCI config[0x48] = 0x%08X\n", val);
+	if (!(val & 0x70000000))
+		return -ENODEV;
+	val = (val >> 28) & 7;
+	if (val & CB710_SLOT_MMC)
+		++n;
+	if (val & CB710_SLOT_MS)
+		++n;
+	if (val & CB710_SLOT_SM)
+		++n;
+
+	chip = devm_kzalloc(&pdev->dev,
+		sizeof(*chip) + n * sizeof(*chip->slot), GFP_KERNEL);
+	if (!chip)
+		return -ENOMEM;
+
+	err = pcim_enable_device(pdev);
+	if (err)
+		return err;
+
+	err = pcim_iomap_regions(pdev, 0x0001, KBUILD_MODNAME);
+	if (err)
+		return err;
+
+	chip->pdev = pdev;
+	chip->iobase = pcim_iomap_table(pdev)[0];
+
+	pci_set_drvdata(pdev, chip);
+
+	err = devm_request_irq(&pdev->dev, pdev->irq,
+		cb710_irq_handler, IRQF_SHARED, KBUILD_MODNAME, chip);
+	if (err)
+		return err;
+
+	do {
+		if (!ida_pre_get(&cb710_ida, GFP_KERNEL))
+			return -ENOMEM;
+
+		spin_lock_irqsave(&cb710_ida_lock, flags);
+		err = ida_get_new(&cb710_ida, &chip->platform_id);
+		spin_unlock_irqrestore(&cb710_ida_lock, flags);
+
+		if (err && err != -EAGAIN)
+			return err;
+	} while (err);
+
+
+	dev_info(&pdev->dev, "id %d, IO 0x%p, IRQ %d\n",
+		chip->platform_id, chip->iobase, pdev->irq);
+
+	if (val & CB710_SLOT_MMC) {	/* MMC/SD slot */
+		err = cb710_register_slot(chip,
+			CB710_SLOT_MMC, 0x00, "cb710-mmc");
+		if (err)
+			return err;
+	}
+
+	if (val & CB710_SLOT_MS) {	/* MemoryStick slot */
+		err = cb710_register_slot(chip,
+			CB710_SLOT_MS, 0x40, "cb710-ms");
+		if (err)
+			goto unreg_mmc;
+	}
+
+	if (val & CB710_SLOT_SM) {	/* SmartMedia slot */
+		err = cb710_register_slot(chip,
+			CB710_SLOT_SM, 0x60, "cb710-sm");
+		if (err)
+			goto unreg_ms;
+	}
+
+	return 0;
+unreg_ms:
+	cb710_unregister_slot(chip, CB710_SLOT_MS);
+unreg_mmc:
+	cb710_unregister_slot(chip, CB710_SLOT_MMC);
+
+#ifdef CONFIG_CB710_DEBUG_ASSUMPTIONS
+	BUG_ON(atomic_read(&chip->slot_refs_count) != 0);
+#endif
+	return err;
+}
+
+static void __devexit cb710_remove_one(struct pci_dev *pdev)
+{
+	struct cb710_chip *chip = pci_get_drvdata(pdev);
+	unsigned long flags;
+
+	cb710_unregister_slot(chip, CB710_SLOT_SM);
+	cb710_unregister_slot(chip, CB710_SLOT_MS);
+	cb710_unregister_slot(chip, CB710_SLOT_MMC);
+#ifdef CONFIG_CB710_DEBUG_ASSUMPTIONS
+	BUG_ON(atomic_read(&chip->slot_refs_count) != 0);
+#endif
+
+	spin_lock_irqsave(&cb710_ida_lock, flags);
+	ida_remove(&cb710_ida, chip->platform_id);
+	spin_unlock_irqrestore(&cb710_ida_lock, flags);
+}
+
+static const struct pci_device_id cb710_pci_tbl[] = {
+	{ PCI_VENDOR_ID_ENE, PCI_DEVICE_ID_ENE_CB710_FLASH,
+		PCI_ANY_ID, PCI_ANY_ID, },
+	{ 0, }
+};
+
+static struct pci_driver cb710_driver = {
+	.name = KBUILD_MODNAME,
+	.id_table = cb710_pci_tbl,
+	.probe = cb710_probe,
+	.remove = __devexit_p(cb710_remove_one),
+#ifdef CONFIG_PM
+	.suspend = cb710_suspend,
+	.resume = cb710_resume,
+#endif
+};
+
+static int __init cb710_init_module(void)
+{
+	return pci_register_driver(&cb710_driver);
+}
+
+static void __exit cb710_cleanup_module(void)
+{
+	pci_unregister_driver(&cb710_driver);
+	ida_destroy(&cb710_ida);
+}
+
+module_init(cb710_init_module);
+module_exit(cb710_cleanup_module);
+
+MODULE_AUTHOR("Michał Mirosław <[email protected]>");
+MODULE_DESCRIPTION("ENE CB710 memory card reader driver");
+MODULE_LICENSE("GPL");
+MODULE_DEVICE_TABLE(pci, cb710_pci_tbl);
drivers/misc/cb710/debug.c +119 -0

119 lines not on this page · raw message

drivers/misc/cb710/Kconfig +21 -0
diff -urpN linux-2.6.29.4/drivers/misc/cb710/Kconfig test2/drivers/misc/cb710/Kconfig
@@ -0,0 +1,21 @@
+config CB710_CORE
+	tristate "ENE CB710/720 Flash memory card reader support"
+	depends on PCI
+	help
+	  This option enables support for PCI ENE CB710/720 Flash memory card
+	  reader found in some laptops (ie. some versions of HP Compaq nx9500).
+
+	  You will also have to select some flash card format drivers (MMC/SD,
+	  MemoryStick).
+
+	  This driver can also be built as a module. If so, the module
+	  will be called cb710.
+
+config CB710_DEBUG
+	bool "Enable driver debugging"
+	depends on CB710_CORE != n
+	default n
+	help
+	  This is an option for use by developers; most people should
+	  say N here.  This adds a lot of debugging output to dmesg.
+
drivers/misc/cb710/Makefile +4 -0
diff -urpN linux-2.6.29.4/drivers/misc/cb710/Makefile test2/drivers/misc/cb710/Makefile
@@ -0,0 +1,4 @@
+obj-$(CONFIG_CB710_CORE)	+= cb710.o
+
+cb710-y				:= core.o sgbuf2.o
+cb710-$(CONFIG_CB710_DEBUG)	+= debug.o
drivers/misc/cb710/sgbuf2.c +150 -0

150 lines not on this page · raw message

drivers/misc/Kconfig +1 -0
diff -urpN linux-2.6.29.4/drivers/misc/Kconfig test2/drivers/misc/Kconfig
@@ -225,5 +225,6 @@ config DELL_LAPTOP
 
 source "drivers/misc/c2port/Kconfig"
 source "drivers/misc/eeprom/Kconfig"
+source "drivers/misc/cb710/Kconfig"
 
 endif # MISC_DEVICES
drivers/misc/Makefile +1 -0
diff -urpN linux-2.6.29.4/drivers/misc/Makefile test2/drivers/misc/Makefile
@@ -20,3 +20,4 @@ obj-$(CONFIG_SGI_GRU)		+= sgi-gru/
 obj-$(CONFIG_HP_ILO)		+= hpilo.o
 obj-$(CONFIG_C2PORT)		+= c2port/
 obj-y				+= eeprom/
+obj-y				+= cb710/
drivers/mmc/host/cb710-mmc.c +804 -0

804 lines not on this page · raw message

drivers/mmc/host/cb710-mmc.h +104 -0

104 lines not on this page · raw message

drivers/mmc/host/Kconfig +13 -0
diff -urpN linux-2.6.29.4/drivers/mmc/host/Kconfig test2/drivers/mmc/host/Kconfig
@@ -212,3 +212,16 @@ config MMC_TMIO
 	help
 	  This provides support for the SD/MMC cell found in TC6393XB,
 	  T7L66XB and also ipaq ASIC3
+
+config MMC_CB710
+	tristate "ENE CB710 MMC/SD Interface support"
+	depends on PCI
+	select CB710_CORE
+	help
+	  This option enables support for MMC/SD part of ENE CB710/720 Flash
+	  memory card reader found in some laptops (ie. some versions of
+	  HP Compaq nx9500).
+
+	  This driver can also be built as a module. If so, the module
+	  will be called cb710-mmc.
+
drivers/mmc/host/Makefile +1 -0
diff -urpN linux-2.6.29.4/drivers/mmc/host/Makefile test2/drivers/mmc/host/Makefile
@@ -27,4 +27,5 @@ endif
 obj-$(CONFIG_MMC_S3C)   	+= s3cmci.o
 obj-$(CONFIG_MMC_SDRICOH_CS)	+= sdricoh_cs.o
 obj-$(CONFIG_MMC_TMIO)		+= tmio_mmc.o
+obj-$(CONFIG_MMC_CB710)	+= cb710-mmc.o
 
include/linux/cb710.h +238 -0

238 lines not on this page · raw message

include/linux/pci_ids.h +1 -0
diff -urpN linux-2.6.29.4/include/linux/pci_ids.h test2/include/linux/pci_ids.h
@@ -2077,6 +2077,7 @@
 #define PCI_VENDOR_ID_MAINPINE		0x1522
 #define PCI_DEVICE_ID_MAINPINE_PBRIDGE	0x0100
 #define PCI_VENDOR_ID_ENE		0x1524
+#define PCI_DEVICE_ID_ENE_CB710_FLASH	0x0510
 #define PCI_DEVICE_ID_ENE_CB712_SD	0x0550
 #define PCI_DEVICE_ID_ENE_CB712_SD_2	0x0551
 #define PCI_DEVICE_ID_ENE_CB714_SD	0x0750
MAINTAINERS +9 -0
diff -urpN linux-2.6.29.4/MAINTAINERS test2/MAINTAINERS
@@ -1660,6 +1660,15 @@ L:	[email protected]
 W:	http://sourceforge.net/projects/lpfcxxxx
 S:	Supported
 
+ENE CB710 FLASH CARD READER DRIVER
+P:	Michał Mirosław
+M:	[email protected]
+L:	[email protected]
+S:	Maintained
+F:	drivers/misc/cb710/
+F:	drivers/mmc/host/cb710-mmc.*
+F:	include/linux/cb710.h
+
 EPSON 1355 FRAMEBUFFER DRIVER
 P:	Christopher Hoover
 M:	[email protected], [email protected]
#16 lore raw

Re: [PATCH 2.6.29.4] Driver for CB710/720 memory card reader (MMC part) - v5 fixed

Quoted 15 lines · On Fri, 22 May 2009 20:33:59 +0200 Michał Mirosław <[email protected]>…

[I messed up source trees for previous diff, please use this one instead.]

Here is a version of the CB710 SD/MMC reader driver ready for inclusion upstream. The code is divided in two parts. There is a virtual 'bus' driver that handles PCI device and registers three new devices one per card reader type. The other driver handles SD/MMC part of the reader.

Changes from v4:
- sg_mapping_iterator extension renamed to avoid namespace clashes when/if the extension would be merged upstream
- appended sgbuf2.h to cb710.h
- move from IDR to IDA for device ID generation
- cleanup after IDA on module unload
- added MAINTAINERS entry

Quoted 2 lines

Queued.

Nice work. :)

Signature · 6 lines
     -- Pierre Ossman

  WARNING: This correspondence is being monitored by the
  Swedish government. Make sure your server uses encryption
  for SMTP traffic and consider using PGP for end-to-end
  encryption.
author maintainer
#17 lore raw

Re: [PATCH 2.6.29.4] Driver for CB710/720 memory card reader (MMC part) - v5 fixed

Quoted 2 lines · Pierre Ossman

On Fri, 22 May 2009 20:33:59 +0200
Michał Mirosław <[email protected]> wrote:

[cut: CB710 driver patch description]

Quoted 1 line

Queued.

One more fixup for the driver following.

Best Regards,
Michał Mirosław

---here---

cb710: Fix compilation warning in DEBUG case.

drivers/misc/cb710/Kconfig +5 -0
diff -urpN test1/drivers/misc/cb710/Kconfig test2/drivers/misc/cb710/Kconfig
@@ -19,3 +19,8 @@ config CB710_DEBUG
 	  This is an option for use by developers; most people should
 	  say N here.  This adds a lot of debugging output to dmesg.
 
+config CB710_DEBUG_ASSUMPTIONS
+	bool
+	depends on CB710_CORE != n
+	default y
+
drivers/misc/cb710/Makefile +2 -0
diff -urpN test1/drivers/misc/cb710/Makefile test2/drivers/misc/cb710/Makefile
@@ -2,3 +2,5 @@ obj-$(CONFIG_CB710_CORE)	+= cb710.o
 
 cb710-y				:= core.o sgbuf2.o
 cb710-$(CONFIG_CB710_DEBUG)	+= debug.o
+
+ccflags-$(CONFIG_CB710_DEBUG)	+= -DDEBUG
drivers/mmc/host/Makefile +4 -0
diff -urpN test1/drivers/mmc/host/Makefile test2/drivers/mmc/host/Makefile
@@ -29,3 +29,7 @@ obj-$(CONFIG_MMC_SDRICOH_CS)	+= sdricoh_
 obj-$(CONFIG_MMC_TMIO)		+= tmio_mmc.o
 obj-$(CONFIG_MMC_CB710)	+= cb710-mmc.o
 
+ifeq ($(CONFIG_CB710_DEBUG),y)
+CFLAGS-cb710-mmc		+= -DDEBUG
+endif
+
include/linux/cb710.h +0 -7
diff -urpN test1/include/linux/cb710.h test2/include/linux/cb710.h
@@ -10,13 +10,6 @@
 #ifndef LINUX_CB710_DRIVER_H
 #define LINUX_CB710_DRIVER_H
 
-#ifdef CONFIG_CB710_DEBUG
-#define DEBUG
-#endif
-
-/* verify assumptions on platform_device framework */
-#define CONFIG_CB710_DEBUG_ASSUMPTIONS
-
 #include <linux/io.h>
 #include <linux/interrupt.h>
 #include <linux/spinlock.h>
author maintainer
#18 lore raw

Re: [PATCH 2.6.29.4] Driver for CB710/720 memory card reader (MMC part) - v5 fixed

cb710: Fix compilation warnings

Fix compilation warnings reported by Stephen Rothwell and change the only dev_vdbg() to dev_dbg().

drivers/misc/cb710/debug.c: In function 'cb710_read_regs_8':
drivers/misc/cb710/debug.c:100: warning: suggest parentheses around operand of '!' or change '&' to '&&' or '!' to '~' drivers/misc/cb710/debug.c: In function 'cb710_read_regs_16':
drivers/misc/cb710/debug.c:101: warning: suggest parentheses around operand of '!' or change '&' to '&&' or '!' to '~' drivers/misc/cb710/debug.c: In function 'cb710_read_regs_32':
drivers/misc/cb710/debug.c:102: warning: suggest parentheses around operand of '!' or change '&' to '&&' or '!' to '~' drivers/mmc/host/cb710-mmc.c: In function 'cb710_mmc_set_transfer_size':
drivers/mmc/host/cb710-mmc.c:222: warning: format '%d' expects type 'int', but argument 4 has type 'size_t' drivers/mmc/host/cb710-mmc.c:222: warning: format '%d' expects type 'int', but argument 6 has type 'size_t'

drivers/misc/cb710/debug.c +1 -1
diff -urpN test2/drivers/misc/cb710/debug.c test3/drivers/misc/cb710/debug.c
@@ -37,7 +37,7 @@ static void cb710_read_regs_##t(void __i
 	unsigned i, j;							\
 									\
 	for (i = 0; i < ARRAY_SIZE(allow); ++i, reg += 16/(t/8)) {	\
-		if (!select & (1 << i))					\
+		if (!(select & (1 << i)))				\
 			continue;					\
 									\
 		for (j = 0; j < 0x10/(t/8); ++j) {			\
drivers/mmc/host/cb710-mmc.c +1 -1
diff -urpN test2/drivers/mmc/host/cb710-mmc.c test3/drivers/mmc/host/cb710-mmc.c
@@ -219,7 +219,7 @@ static void cb710_mmc_set_transfer_size(
 	cb710_write_port_32(slot, CB710_MMC_TRANSFER_SIZE_PORT,
 		((count - 1) << 16)|(blocksize - 1));
 
-	dev_vdbg(cb710_slot_dev(slot), "set up for %d block%s of %d bytes\n",
+	dev_dbg(cb710_slot_dev(slot), "set up for %Zu block%s of %Zu bytes\n",
 		count, count == 1 ? "" : "s", blocksize);
 }
 
#19 lore raw

Re: [PATCH 2.6.29.4] Driver for CB710/720 memory card reader (MMC part) - v5 fixed

In reply to Michał Mirosław
Quoted 8 lines · On Thu, 4 Jun 2009 12:24:55 +0200 Michał Mirosław <[email protected]>…
Quoted 2 lines · Pierre Ossman

On Fri, 22 May 2009 20:33:59 +0200
Michał Mirosław <[email protected]> wrote:

[cut: CB710 driver patch description]

Quoted 1 line

Queued.

One more fixup for the driver following.

I had already done some of this, but I've merged the rest of the changes you had here.

Rgds

Signature · 6 lines
     -- Pierre Ossman

  WARNING: This correspondence is being monitored by the
  Swedish government. Make sure your server uses encryption
  for SMTP traffic and consider using PGP for end-to-end
  encryption.
#20 lore raw

Re: [PATCH/RFC 2.6.29.2] Driver for CB710/720 memory card reader (MMC part) - v4

In reply to Pierre Ossman
Quoted 17 lines · On Friday 22 May 2009 13:27:55 Pierre Ossman wrote:
Quoted 5 lines · On Fri, 8 May 2009 17:16:22 +0200 Michał Mirosław <[email protected]> wrote:

Here is next version of the CB710 SD/MMC reader driver. As in previous versions the code is divided in two parts - virtual 'bus' driver that handles PCI device and registers three new devices one per card reader type. The other device handles SD/MMC part of the reader.

Quoted 8 lines

Key changes from v3:
- kerneldoc for sg_mapping_iterator extension
- free/restore IRQ during suspend/resume
- protect slot->irq_handler by spinlock instead of RCU
- remove verify_serialization()
- Kconfig
- complete patch for linux kernel 2.6.29.2

Quoted 7 lines

Looks good to me. I just want you to add a MAINTAINERS entry so that people know who to contact if they have problems with this driver.

Quoted 4 lines

My patches for sg_mapping_iterator extension went to /dev/null, so I have incorporated them to the cb710 core driver module for now. Probably someone with more karma points should look at them (sgbuf2.{c,h}) and push further if they are of any value to other driver writers.

I like the general idea and I think that this could serve as a good starting point into making generic PIO handling library (which has been on my personal TODO of things to look into for a long time).

Michał, I'll try ping you back about it once I'm done with 2.6.31 IDE stuff..

Quoted 2 lines

Give them to Andrew Morton. He's good at finding the right people and making sure they notice. :)

Hehe, please keep me on cc:.

Thanks.
Bart