* [PATCH 2/2] MEMSTICK: Add driver for Ricoh R5C592 Card reader.
2010-08-03 14:53 [PATCH 0/2] Driver for Ricoh cardreader Maxim Levitsky
@ 2010-08-03 14:53 ` Maxim Levitsky
2010-08-04 7:57 ` Alex Dubov
0 siblings, 1 reply; 51+ messages in thread
From: Maxim Levitsky @ 2010-08-03 14:53 UTC (permalink / raw)
To: Alex Dubov; +Cc: LKML, Maxim Levitsky
Signed-off-by: Maxim Levitsky <maximlevitsky@gmail.com>
---
MAINTAINERS | 6 +
drivers/memstick/host/Kconfig | 13 +
drivers/memstick/host/Makefile | 1 +
drivers/memstick/host/r592.c | 920 ++++++++++++++++++++++++++++++++++++++++
drivers/memstick/host/r592.h | 178 ++++++++
5 files changed, 1118 insertions(+), 0 deletions(-)
create mode 100644 drivers/memstick/host/r592.c
create mode 100644 drivers/memstick/host/r592.h
diff --git a/MAINTAINERS b/MAINTAINERS
index 88fcb7c..7ebdd32 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -4822,6 +4822,12 @@ S: Maintained
F: drivers/mtd/nand/r822.c
F: drivers/mtd/nand/r822.h
+RICOH R5C592 MEMORYSTICK DRIVER
+M: Maxim Levitsky <maximlevitsky@gmail.com>
+S: Maintained
+F: drivers/memstick/host/r592.c
+F: drivers/memstick/host/r592.h
+
RISCOM8 DRIVER
S: Orphan
F: Documentation/serial/riscom8.txt
diff --git a/drivers/memstick/host/Kconfig b/drivers/memstick/host/Kconfig
index 4ce5c8d..7022a2a 100644
--- a/drivers/memstick/host/Kconfig
+++ b/drivers/memstick/host/Kconfig
@@ -30,3 +30,16 @@ config MEMSTICK_JMICRON_38X
To compile this driver as a module, choose M here: the
module will be called jmb38x_ms.
+
+config MEMSTICK_R592
+ tristate "Ricoh R5C592 MemoryStick interface support (EXPERIMENTAL)"
+ depends on EXPERIMENTAL && PCI
+
+ help
+ Say Y here if you want to be able to access MemoryStick cards with
+ the Ricoh R5C592 MemoryStick card reader (which is part of 5 in one
+ multifunction reader)
+
+ To compile this driver as a module, choose M here: the module will
+ be called r592.
+
diff --git a/drivers/memstick/host/Makefile b/drivers/memstick/host/Makefile
index 12530e4..ad63c16 100644
--- a/drivers/memstick/host/Makefile
+++ b/drivers/memstick/host/Makefile
@@ -8,3 +8,4 @@ endif
obj-$(CONFIG_MEMSTICK_TIFM_MS) += tifm_ms.o
obj-$(CONFIG_MEMSTICK_JMICRON_38X) += jmb38x_ms.o
+obj-$(CONFIG_MEMSTICK_R592) += r592.o
diff --git a/drivers/memstick/host/r592.c b/drivers/memstick/host/r592.c
new file mode 100644
index 0000000..bff5bc3
--- /dev/null
+++ b/drivers/memstick/host/r592.c
@@ -0,0 +1,920 @@
+/*
+ * Copyright (C) 2010 - Maxim Levitsky
+ * driver for Ricoh memstick readers
+ *
+ * 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/freezer.h>
+#include <linux/jiffies.h>
+#include <linux/interrupt.h>
+#include <linux/pci.h>
+#include <linux/pci_ids.h>
+#include <linux/delay.h>
+#include <linux/slab.h>
+#include <linux/kthread.h>
+#include <linux/sched.h>
+#include <linux/highmem.h>
+#include <asm/byteorder.h>
+#include "r592.h"
+
+static int enable_dma = 1;
+static int debug;
+
+static char *tpc_names[] = {
+ "MS_TPC_READ_MG_STATUS",
+ "MS_TPC_READ_LONG_DATA",
+ "MS_TPC_READ_SHORT_DATA",
+ "MS_TPC_READ_REG",
+ "MS_TPC_READ_QUAD_DATA",
+ "MS_INVALID",
+ "MS_TPC_GET_INT",
+ "MS_TPC_SET_RW_REG_ADRS",
+ "MS_TPC_EX_SET_CMD",
+ "MS_TPC_WRITE_QUAD_DATA",
+ "MS_TPC_WRITE_REG",
+ "MS_TPC_WRITE_SHORT_DATA",
+ "MS_TPC_WRITE_LONG_DATA",
+ "MS_TPC_SET_CMD",
+};
+
+
+static char *dbg_tpc_name(int tpc)
+{
+ return tpc_names[tpc-1];
+}
+
+/* Read a register*/
+static inline u32 r592_read_reg(struct r592_device *dev, int address)
+{
+ u32 value = le32_to_cpu(readl(dev->mmio + address));
+ dbg_reg("reg #%02d == 0x%08x", address, value);
+ return value;
+}
+
+/* Write a register */
+static inline void r592_write_reg(struct r592_device *dev,
+ int address, u32 value)
+{
+ dbg_reg("reg #%02d <- 0x%08x", address, value);
+ writel(cpu_to_le32(value), dev->mmio + address);
+ mmiowb();
+}
+
+/* Reads a big endian DWORD register */
+static inline u32 r592_read_reg_be(struct r592_device *dev, int address)
+{
+ return be32_to_cpu(r592_read_reg(dev, address));
+}
+
+/* Writes a big endian DWORD register */
+static inline void r592_write_reg_be(struct r592_device *dev,
+ int address, u32 value)
+{
+ return r592_write_reg(dev, address, cpu_to_be32(value));
+}
+
+/* Set specific bits in a register (little endian)*/
+static inline void r592_set_reg_mask(struct r592_device *dev,
+ int address, u32 mask)
+{
+ u32 reg = readl(dev->mmio + address);
+ dbg_reg("reg #%02d |= 0x%08x (old =0x%08x)", address, mask, reg);
+ writel(cpu_to_le32(reg | mask) , dev->mmio + address);
+ mmiowb();
+}
+
+/* Clear specific bits in a register (little endian)*/
+static inline void r592_clear_reg_mask(struct r592_device *dev,
+ int address, u32 mask)
+{
+ u32 reg = readl(dev->mmio + address);
+ dbg_reg("reg #%02d &= 0x%08x (old = 0x%08x, mask = 0x%08x)",
+ address, ~mask, reg, mask);
+ writel(cpu_to_le32(reg & ~mask), dev->mmio + address);
+ mmiowb();
+}
+
+/* Wait till specific bits are set/clear in a register */
+/* Note that this is intended for waits that take very little time */
+static inline int r592_reg_wait(struct r592_device *dev, int address,
+ u32 mask, u32 wanted_mask)
+{
+ unsigned long timeout = jiffies + msecs_to_jiffies(2000);
+
+ if ((r592_read_reg(dev, address) & mask) == wanted_mask)
+ return 0;
+
+ while (time_before(jiffies, timeout)) {
+ if ((r592_read_reg(dev, address) & mask) == wanted_mask)
+ return 0;
+ cpu_relax();
+ }
+ return -ETIME;
+}
+
+
+/* Enable/disable device */
+/* A common part of initialization between serial and parallel mode */
+/* This is not enough to actually use the device */
+static void r592_enable_device(struct r592_device *dev, bool enable)
+{
+ u32 reg = enable ? 3 : 0;
+ dbg("%sabling the device", enable ? "en" : "dis");
+ r592_write_reg(dev, R592_REG32, reg);
+
+ /* These error bits aren't used.
+ Still they are usefull for debug */
+ r592_clear_reg_mask(dev, R592_REG_MSC,
+ R592_REG_MSC_FIFO_USER_ORN | R592_REG_MSC_FIFO_MISMATH);
+}
+
+
+/* Set serial/parallel mode */
+static void r592_set_mode(struct r592_device *dev, bool parallel_mode)
+{
+ r592_set_reg_mask(dev, R592_IO, R592_IO_RESET);
+ msleep(100);
+
+ if (!parallel_mode) {
+ dbg("enabling serial mode");
+ r592_write_reg(dev, R592_REG36, 1);
+ } else {
+ dbg("enabling parallel mode");
+ r592_set_reg_mask(dev, R592_REG32, R592_REG32_20);
+
+ r592_clear_reg_mask(dev, R592_IO,
+ R592_IO_SERIAL1 | R592_IO_SERIAL2);
+
+ r592_write_reg(dev, R592_REG36, 3);
+ }
+}
+
+static void r592_reset(struct r592_device *dev)
+{
+ dbg("resetting device due to error");
+ /* TODO: extend 'card' interface to add a reset there */
+ /*dev->host->card->reset(dev->host->card);*/
+}
+
+
+/* Tests if there is an CRC error */
+static int r592_test_io_error(struct r592_device *dev)
+{
+ if (r592_read_reg(dev, R592_STATUS) &
+ (R592_STATUS_SEND_ERR | R592_STATUS_RECV_ERR)) {
+ dbg("got CRC error");
+ return -EILSEQ;
+ }
+ return 0;
+}
+
+/* Wait for empty fifo. Mostly precation */
+static int r592_wait_fifo_empty(struct r592_device *dev)
+{
+ /* Ensure fifo is ready */
+ int error = r592_reg_wait(dev, R592_REG_MSC,
+ R592_REG_MSC_FIFO_EMPTY, R592_REG_MSC_FIFO_EMPTY);
+
+ if (error) {
+ dbg("wait for FIFO empty failed");
+ return error;
+ }
+
+ return 0;
+}
+
+/* Activates the DMA transfer from to FIFO */
+static void r592_start_dma(struct r592_device *dev, bool is_write)
+{
+ unsigned long flags;
+ u32 reg;
+ spin_lock_irqsave(&dev->irq_lock, flags);
+
+ /* Ack interrupts (just in case) + enable them */
+ r592_clear_reg_mask(dev, R592_REG_MSC, DMA_IRQ_ACK_MASK);
+ r592_set_reg_mask(dev, R592_REG_MSC, DMA_IRQ_EN_MASK);
+
+ /* Set DMA address */
+ r592_write_reg(dev, R592_FIFO_DMA, sg_dma_address(&dev->req->sg));
+
+ /* Enable the DMA */
+ reg = r592_read_reg(dev, R592_FIFO_DMA_SETTINGS);
+ reg |= R592_FIFO_DMA_SETTINGS_EN;
+
+ if (!is_write)
+ reg |= R592_FIFO_DMA_SETTINGS_DIR;
+ else
+ reg &= ~R592_FIFO_DMA_SETTINGS_DIR;
+ r592_write_reg(dev, R592_FIFO_DMA_SETTINGS, reg);
+
+ spin_unlock_irqrestore(&dev->irq_lock, flags);
+}
+
+/* Cleanups DMA related settings */
+static void r592_stop_dma(struct r592_device *dev, int error)
+{
+ r592_clear_reg_mask(dev, R592_FIFO_DMA_SETTINGS,
+ R592_FIFO_DMA_SETTINGS_EN);
+
+ r592_write_reg(dev, R592_FIFO_DMA,
+ dev->scrath_dma_page_physical_address);
+
+ r592_clear_reg_mask(dev, R592_REG_MSC, DMA_IRQ_EN_MASK);
+ r592_clear_reg_mask(dev, R592_REG_MSC, DMA_IRQ_ACK_MASK);
+ dev->dma_error = error;
+}
+
+/* Test if hardware supports DMA */
+static void r592_check_dma(struct r592_device *dev)
+{
+ dev->dma_capable = enable_dma &&
+ (r592_read_reg(dev, R592_FIFO_DMA_SETTINGS) &
+ R592_FIFO_DMA_SETTINGS_CAP);
+}
+
+/* Transfers fifo contents in/out using DMA */
+static int r592_transfer_fifo_dma(struct r592_device *dev)
+{
+ int len, sg_count;
+ bool is_write;
+
+ if (!dev->dma_capable || !dev->req->long_data)
+ return -EINVAL;
+
+ len = dev->req->sg.length;
+ is_write = dev->req->data_dir == WRITE;
+
+ if (len != R592_LFIFO_SIZE)
+ return -EINVAL;
+
+ dbg_verbose("doing dma transfer");
+
+ dev->dma_error = 0;
+ INIT_COMPLETION(dev->dma_done);
+
+ /* TODO: hidden assumption about nenth beeing always 1 */
+ sg_count = dma_map_sg(&dev->pci_dev->dev, &dev->req->sg, 1, is_write ?
+ PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE);
+
+ if (sg_count != 1 ||
+ (sg_dma_len(&dev->req->sg) < dev->req->sg.length)) {
+ dbg("problem in dma_map_sg");
+ return -EIO;
+ }
+
+ r592_start_dma(dev, is_write);
+
+ /* Wait for DMA completion */
+ if (!wait_for_completion_timeout(
+ &dev->dma_done, msecs_to_jiffies(1000))) {
+ message("DMA timeout");
+ r592_stop_dma(dev, -ETIMEDOUT);
+ }
+
+ /* Cleanup */
+ return dev->dma_error;
+}
+
+/* writes the FIFO in 4 byte chunks
+ if lenght isn't 4 byte aligned, it spills remaining bytes into 'spill' buffer
+ it also accepts spill from former write
+ to flush a write, just call this with null buffer */
+static void r592_write_fifo_pio(struct r592_device *dev,
+ struct dword_buffer *spill, unsigned char *buffer, int len)
+{
+ unsigned char dword[4] = {0};
+
+ if (!buffer) {
+ buffer = dword;
+ len = sizeof(dword);
+ }
+
+ /* flush spill from former write */
+ while (spill->len < 4 && len) {
+ spill->dword[spill->len++] = *buffer++;
+ len--;
+ }
+
+ if (spill->len == 4) {
+ r592_write_reg_be(dev, R592_FIFO_PIO, *(u32 *)spill->dword);
+ spill->len = 0;
+ }
+
+ if (!len)
+ return;
+
+ /* write full dwords */
+ while (len >= 4) {
+ r592_write_reg_be(
+ dev, R592_FIFO_PIO, *(u32 *)buffer);
+ buffer += 4;
+ len -= 4;
+ }
+
+ /* put remaining bytes to the spill */
+ while (len--)
+ spill->dword[spill->len++] = *buffer++;
+}
+
+/* Read a fifo in 4 bytes chunks.
+ if input doesn't fit the buffer, it places bytes of last dword in spill
+ buffer, so that they don't get lost
+ on last read, just throw these away
+*/
+static void r592_read_fifo_pio(struct r592_device *dev,
+ struct dword_buffer *spill, unsigned char *buffer, int len)
+{
+ int i;
+ u32 tmp;
+
+ for (i = 0 ; len && i < spill->len ; i++) {
+ *buffer++ = spill->dword[i];
+ len--;
+ spill->len--;
+ }
+
+ if (spill->len) {
+ memmove(spill->dword, spill->dword + i, spill->len);
+ return;
+ }
+
+ while (len >= 4) {
+ *(u32 *)buffer = r592_read_reg_be(
+ dev, R592_FIFO_PIO);
+ buffer += 4;
+ len -= 4;
+ }
+
+ if (!len)
+ return;
+
+ tmp = r592_read_reg_be(dev, R592_FIFO_PIO);
+
+ spill->len = 4;
+ while (len--) {
+ *buffer++ = tmp & 0xFF;
+ tmp >>= 8;
+ spill->len--;
+ }
+
+ for (i = 0 ; i < spill->len ; i++) {
+ spill->dword[i] = tmp & 0xFF;
+ tmp >>= 8;
+ }
+
+ return;
+}
+
+/* Transfers actual data using PIO. */
+static int r592_transfer_fifo_pio(struct r592_device *dev)
+{
+ unsigned char *buffer;
+ struct dword_buffer spill = {{0}, 0};
+ unsigned long flags;
+
+ bool is_write = dev->req->tpc >= MS_TPC_SET_RW_REG_ADRS;
+ int p_len, p_offset, len, offset;
+ struct page *page;
+
+
+ if (!dev->req) {
+ dbg("pio transfer called without request!");
+ return -EINVAL;
+ }
+
+ if (!dev->req->long_data) {
+ if (is_write) {
+ r592_write_fifo_pio(dev, &spill, dev->req->data,
+ dev->req->data_len);
+ r592_write_fifo_pio(dev, &spill, NULL, 0);
+ } else
+ r592_read_fifo_pio(dev, &spill, dev->req->data,
+ dev->req->data_len);
+ return 0;
+ }
+
+ spin_lock_irqsave(&dev->atomic_lock, flags);
+
+ len = dev->req->sg.length;
+ offset = dev->req->sg.offset;
+
+ while (len) {
+
+ p_offset = offset_in_page(offset);
+ p_len = min((int)(PAGE_SIZE - p_offset), len);
+
+ page = nth_page(sg_page(&dev->req->sg), offset >> PAGE_SHIFT);
+ buffer = kmap_atomic(page, KM_BIO_SRC_IRQ) + p_offset;
+
+
+ /* Do the transfer fifo<->memory*/
+ if (is_write)
+ r592_write_fifo_pio(dev, &spill, buffer, p_len);
+ else
+ r592_read_fifo_pio(dev, &spill, buffer, p_len);
+
+ kunmap_atomic(buffer - p_offset, KM_BIO_SRC_IRQ);
+
+ offset += p_len;
+ len -= p_len;
+
+ }
+
+ if (is_write)
+ r592_write_fifo_pio(dev, &spill, NULL, 0);
+
+ spin_unlock_irqrestore(&dev->atomic_lock, flags);
+ return 0;
+}
+
+
+/* Executes one TPC (data is read/written from small or large fifo) */
+static void r592_process_request(struct r592_device *dev)
+{
+ bool is_write = dev->req->tpc >= MS_TPC_SET_RW_REG_ADRS;
+ int len, error;
+ u32 status, reg;
+
+ if (!dev->req) {
+ dbg("r592_process_request with no request!");
+ return;
+ }
+
+ len = dev->req->long_data ?
+ dev->req->sg.length : dev->req->data_len;
+
+ /* Ensure that FIFO can hold the input data */
+ WARN_ON(len > R592_LFIFO_SIZE);
+ if (len > R592_LFIFO_SIZE) {
+ error = -EINVAL;
+ goto out;
+ }
+
+ dbg("executing %s LEN=%d", dbg_tpc_name(dev->req->tpc), len);
+
+
+ /* Set IO direction */
+ if (is_write)
+ r592_set_reg_mask(dev, R592_IO, R592_IO_DIRECTION);
+ else
+ r592_clear_reg_mask(dev, R592_IO, R592_IO_DIRECTION);
+
+
+ error = r592_wait_fifo_empty(dev);
+ if (error)
+ goto out;
+
+ /* Transfer write data */
+ if (is_write) {
+ error = r592_transfer_fifo_dma(dev);
+ if (error)
+ error = r592_transfer_fifo_pio(dev);
+ }
+
+ if (error)
+ goto out;
+
+ /* Trigger the TPC */
+ reg = (len << R592_TPC_EXEC_LEN_SHIFT) |
+ (dev->req->tpc << R592_TPC_EXEC_TPC_SHIFT) |
+ R592_TPC_EXEC_BIG_FIFO;
+
+ r592_write_reg(dev, R592_TPC_EXEC, reg);
+
+ /* Wait for TPC completion */
+ status = R592_STATUS_RDY;
+ if (dev->req->need_card_int)
+ status |= R592_STATUS_CED;
+
+ error = r592_reg_wait(dev, R592_STATUS, status, status);
+ if (error) {
+ dbg("card didn't respond");
+ goto out;
+ }
+
+ /* Test IO errors */
+ error = r592_test_io_error(dev);
+ if (error)
+ goto out;
+
+ /* Read data from FIFO */
+ if (!is_write) {
+ error = r592_transfer_fifo_dma(dev);
+
+ if (error)
+ error = r592_transfer_fifo_pio(dev);
+ }
+
+ /* read INT reg. This can be shortened with shifts, but that way
+ its more readable */
+ if (dev->parallel_mode && dev->req->need_card_int) {
+
+ dev->req->int_reg = 0;
+ status = r592_read_reg(dev, R592_STATUS);
+
+ if (status & R592_STATUS_P_CMDNACK)
+ dev->req->int_reg |= MEMSTICK_INT_CMDNAK;
+
+ if (status & R592_STATUS_P_BREQ)
+ dev->req->int_reg |= MEMSTICK_INT_BREQ;
+
+ if (status & R592_STATUS_P_INTERR)
+ dev->req->int_reg |= MEMSTICK_INT_ERR;
+
+ if (status & R592_STATUS_P_CED)
+ dev->req->int_reg |= MEMSTICK_INT_CED;
+ }
+out:
+ dev->req->error = error;
+ r592_clear_reg_mask(dev, R592_REG_MSC, R592_REG_MSC_LED);
+ return;
+}
+
+/* Main request processing thread */
+static int r592_process_thread(void *data)
+{
+ int error;
+ struct r592_device *dev = (struct r592_device *)data;
+
+ while (!kthread_should_stop()) {
+
+ try_to_freeze();
+ set_current_state(TASK_INTERRUPTIBLE);
+ error = memstick_next_req(dev->host, &dev->req);
+
+ if (error) {
+
+ if (error == -EAGAIN) {
+ dbg_verbose("IO: end of data, sleeping now");
+ } else {
+ dbg("IO: unknown error from "
+ "memstick_next_req %d", error);
+ }
+
+ schedule();
+ } else {
+ set_current_state(TASK_RUNNING);
+ dbg_verbose("IO: got request, processing it");
+ r592_process_request(dev);
+ if (dev->req->error)
+ r592_reset(dev);
+ }
+ }
+
+ return 0;
+}
+
+/* Reprogram chip to detect change in card state */
+/* eg, if card is detected, arm it to detect removal, and vice versa */
+static void r592_update_card_detect(struct r592_device *dev, bool enable)
+{
+ u32 reg = r592_read_reg(dev, R592_REG_MSC);
+ bool card_detected = reg & R592_REG_MSC_PRSNT;
+
+ dbg("update card detect. Card state: %s", card_detected ?
+ "present" : "absent");
+
+ reg &= ~(R592_REG_MSC_IRQ_REMOVE | R592_REG_MSC_IRQ_INSERT);
+
+ if (enable) {
+ if (card_detected)
+ reg |= (R592_REG_MSC_IRQ_REMOVE << 16);
+ else
+ reg |= (R592_REG_MSC_IRQ_INSERT << 16);
+ }
+
+ r592_write_reg(dev, R592_REG_MSC, reg);
+}
+
+/* Timer routine that fires 1 second after last card detection event, */
+static void r592_detect_timer(long unsigned int data)
+{
+ struct r592_device *dev = (struct r592_device *)data;
+ r592_update_card_detect(dev, true);
+ memstick_detect_change(dev->host);
+}
+
+/* Interrupt handler */
+static irqreturn_t r592_irq(int irq, void *data)
+{
+ struct r592_device *dev = (struct r592_device *)data;
+ irqreturn_t ret = IRQ_NONE;
+ u32 reg;
+ u16 irq_enable, irq_status;
+ unsigned long flags;
+
+ spin_lock_irqsave(&dev->irq_lock, flags);
+
+ if (dev->insuspend)
+ goto unlock;
+
+ reg = r592_read_reg(dev, R592_REG_MSC);
+ irq_enable = reg >> 16;
+ irq_status = reg & 0xFFFF;
+
+ /* Ack the interrupts */
+ reg &= ~irq_status;
+ r592_write_reg(dev, R592_REG_MSC, reg);
+
+ /* Get the IRQ status minus bits that aren't enabled */
+ irq_status & (irq_enable);
+
+ /* Due to limitation of memstick core, we don't look at bits that
+ indicate that card was removed/inserted and/or present */
+ if (irq_status & (R592_REG_MSC_IRQ_INSERT | R592_REG_MSC_IRQ_REMOVE)) {
+
+ ret = IRQ_HANDLED;
+
+ message("IRQ: card %s", irq_status & R592_REG_MSC_IRQ_INSERT ?
+ "added" : "removed");
+
+ /* Ack the interrupt */
+ r592_clear_reg_mask(dev, R592_REG_MSC, R592_REG_MSC_IRQ_INSERT |
+ R592_REG_MSC_IRQ_REMOVE);
+
+ mod_timer(&dev->detect_timer, jiffies + msecs_to_jiffies(500));
+ goto unlock;
+ }
+
+ if (irq_status &
+ (R592_REG_MSC_FIFO_DMA_DONE | R592_REG_MSC_FIFO_DMA_ERR)) {
+ ret = IRQ_HANDLED;
+
+ if (irq_status & R592_REG_MSC_FIFO_DMA_ERR) {
+ dbg("IRQ: DMA error");
+ r592_stop_dma(dev, -EIO);
+ } else {
+ dbg_verbose("IRQ: dma done");
+ r592_stop_dma(dev, 0);
+ }
+
+ complete(&dev->dma_done);
+ goto unlock;
+ }
+
+
+ dbg("got unhandled IRQ status = %x", reg);
+unlock:
+ spin_unlock_irqrestore(&dev->irq_lock, flags);
+ return ret;
+}
+
+/* External inteface: set settings */
+static int r592_set_param(struct memstick_host *host,
+ enum memstick_param param,
+ int value)
+{
+ struct r592_device *dev = memstick_priv(host);
+
+ switch (param) {
+ case MEMSTICK_POWER:
+ switch (value) {
+ case MEMSTICK_POWER_ON:
+ r592_enable_device(dev, true);
+ break;
+ case MEMSTICK_POWER_OFF:
+ r592_enable_device(dev, false);
+ break;
+ default:
+ return -EINVAL;
+ }
+ break;
+ case MEMSTICK_INTERFACE:
+ switch (value) {
+ case MEMSTICK_SERIAL:
+ r592_set_mode(dev, 0);
+ dev->parallel_mode = 0;
+ dev->host->caps &= ~MEMSTICK_CAP_AUTO_GET_INT;
+ break;
+ case MEMSTICK_PAR4:
+ r592_set_mode(dev, 1);
+ dev->host->caps |= MEMSTICK_CAP_AUTO_GET_INT;
+ dev->parallel_mode = 1;
+ break;
+ default:
+ return -EINVAL;
+ }
+ break;
+ default:
+ return -EINVAL;
+ }
+
+ return 0;
+}
+
+/* External interface: submit requests */
+static void r592_submit_req(struct memstick_host *host)
+{
+ struct r592_device *dev = memstick_priv(host);
+
+ dbg("new request from user");
+ wake_up_process(dev->io_thread);
+ dbg("woke IO thread...");
+}
+
+
+static const struct pci_device_id r592_pci_id_tbl[] = {
+
+ { PCI_VDEVICE(RICOH, 0x0592), },
+ { },
+};
+
+/* Main entry */
+int r592_probe(struct pci_dev *pdev, const struct pci_device_id *id)
+{
+ int error = -ENOMEM;
+ struct memstick_host *host;
+ struct r592_device *dev;
+
+ /* Allocate memory */
+ host = memstick_alloc_host(sizeof(struct r592_device), &pdev->dev);
+ if (!host)
+ goto error1;
+
+ dev = memstick_priv(host);
+ dev->host = host;
+ dev->pci_dev = pdev;
+ pci_set_drvdata(pdev, dev);
+
+
+ /* pci initialization */
+ error = pci_enable_device(pdev);
+ if (error)
+ goto error2;
+
+ pci_set_master(pdev);
+ error = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
+ if (error)
+ goto error3;
+
+ error = pci_request_regions(pdev, DRV_NAME);
+ if (error)
+ goto error3;
+
+ dev->mmio = pci_ioremap_bar(pdev, 0);
+ if (!dev->mmio)
+ goto error4;
+
+
+ dev->irq = pdev->irq;
+ spin_lock_init(&dev->irq_lock);
+ spin_lock_init(&dev->atomic_lock);
+ init_completion(&dev->dma_done);
+ setup_timer(&dev->detect_timer,
+ r592_detect_timer, (long unsigned int)dev);
+
+ /* Host initialization */
+ host->caps = MEMSTICK_CAP_PAR4;
+ host->request = r592_submit_req;
+ host->set_param = r592_set_param;
+ r592_check_dma(dev);
+
+
+ dev->io_thread = kthread_run(r592_process_thread, dev, "r592");
+ if (IS_ERR(dev->io_thread)) {
+ error = PTR_ERR(dev->io_thread);
+ goto error5;
+ }
+
+ /* This is just a precation, so don't fail */
+ dev->scrath_dma_page = dma_alloc_from_coherent(pdev->dev, PAGE_SIZE,
+ &dev->scrath_dma_page_physical_address, &error);
+
+ if (error) {
+ dev->scrath_dma_page = NULL;
+ dev->scrath_dma_page_physical_address = 0;
+ }
+ r592_stop_dma(dev , 0);
+
+
+ if (request_irq(dev->irq, &r592_irq, IRQF_SHARED,
+ DRV_NAME, dev))
+ goto error6;
+
+ r592_update_card_detect(dev, true);
+ if (memstick_add_host(host))
+ goto error7;
+
+ message("driver succesfully loaded");
+ return 0;
+error7:
+ free_irq(dev->irq, dev);
+error6:
+ if (dev->scrath_dma_page)
+ dma_free_coherent(&pdev->dev, PAGE_SIZE, dev->scrath_dma_page,
+ dev->scrath_dma_page_physical_address);
+ kthread_stop(dev->io_thread);
+error5:
+ iounmap(dev->mmio);
+error4:
+ pci_release_regions(pdev);
+error3:
+ pci_disable_device(pdev);
+error2:
+ memstick_free_host(host);
+error1:
+ return error;
+}
+
+
+void r592_remove(struct pci_dev *pdev)
+{
+ int error = 0;
+ struct r592_device *dev = pci_get_drvdata(pdev);
+
+ /* Stop the processing thread.
+ That ensures that we don't take more requests */
+ kthread_stop(dev->io_thread);
+
+ while (!error && dev->req) {
+ dev->req->error = -ETIME;
+ error = memstick_next_req(dev->host, &dev->req);
+ }
+ memstick_remove_host(dev->host);
+
+ free_irq(dev->irq, dev);
+ iounmap(dev->mmio);
+ pci_release_regions(pdev);
+ pci_disable_device(pdev);
+ memstick_free_host(dev->host);
+
+ if (dev->scrath_dma_page)
+ dma_free_coherent(&pdev->dev, PAGE_SIZE, dev->scrath_dma_page,
+ dev->scrath_dma_page_physical_address);
+
+}
+
+int r592_suspend(struct device *core_dev)
+{
+ struct pci_dev *pdev = to_pci_dev(core_dev);
+ struct r592_device *dev = pci_get_drvdata(pdev);
+ unsigned long flags;
+
+ memstick_suspend_host(dev->host);
+
+ spin_lock_irqsave(&dev->irq_lock, flags);
+ dev->insuspend = 1;
+ spin_unlock_irqrestore(&dev->irq_lock, flags);
+ synchronize_irq(dev->irq);
+
+ r592_stop_dma(dev, -ETIME);
+ r592_update_card_detect(dev, false);
+ del_timer_sync(&dev->detect_timer);
+ return 0;
+}
+
+int r592_resume(struct device *core_dev)
+{
+ unsigned long flags;
+ struct pci_dev *pdev = to_pci_dev(core_dev);
+ struct r592_device *dev = pci_get_drvdata(pdev);
+
+ spin_lock_irqsave(&dev->irq_lock, flags);
+ dev->insuspend = 0;
+ spin_unlock_irqrestore(&dev->irq_lock, flags);
+ r592_update_card_detect(dev, true);
+
+ memstick_resume_host(dev->host);
+ return 0;
+}
+
+
+SIMPLE_DEV_PM_OPS(r592_pm_ops, r592_suspend, r592_resume);
+MODULE_DEVICE_TABLE(pci, r592_pci_id_tbl);
+
+
+static struct pci_driver r852_pci_driver = {
+ .name = DRV_NAME,
+ .id_table = r592_pci_id_tbl,
+ .probe = r592_probe,
+ .remove = r592_remove,
+ .driver.pm = &r592_pm_ops,
+};
+
+static __init int r592_module_init(void)
+{
+ return pci_register_driver(&r852_pci_driver);
+}
+
+static void __exit r592_module_exit(void)
+{
+ pci_unregister_driver(&r852_pci_driver);
+}
+
+module_init(r592_module_init);
+module_exit(r592_module_exit);
+
+
+module_param(enable_dma, bool, S_IRUGO);
+MODULE_PARM_DESC(enable_dma, "Enable usage of the DMA (default)");
+module_param(debug, int, S_IRUGO | S_IWUSR);
+MODULE_PARM_DESC(debug, "Debug level (0-2)");
+
+MODULE_LICENSE("GPL");
+MODULE_AUTHOR("Maxim Levitsky <maximlevitsky@gmail.com>");
+MODULE_DESCRIPTION("Ricoh R5C592 Memstick/Memstick PRO card reader driver");
diff --git a/drivers/memstick/host/r592.h b/drivers/memstick/host/r592.h
new file mode 100644
index 0000000..05c8da9
--- /dev/null
+++ b/drivers/memstick/host/r592.h
@@ -0,0 +1,178 @@
+/*
+ * Copyright (C) 2010 - Maxim Levitsky
+ * driver for Ricoh memstick readers
+ *
+ * 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/memstick.h>
+#include <linux/spinlock.h>
+#include <linux/interrupt.h>
+#include <linux/workqueue.h>
+#include <linux/ctype.h>
+
+
+
+/* write to this reg (number,len) triggers TPC execution */
+#define R592_TPC_EXEC 0x00
+#define R592_TPC_EXEC_LEN_SHIFT 16 /* Bits 16..25 are TPC len */
+#define R592_TPC_EXEC_BIG_FIFO (1 << 26) /* If bit 26 is set, large fifo is used (reg 48) */
+#define R592_TPC_EXEC_TPC_SHIFT 28 /* Bits 28..31 are the TPC number */
+
+
+/* Window for small TPC fifo (big endian)*/
+/* reads and writes always are done in 8 byte chunks */
+/* Not used in driver, because large fifo does better job */
+#define R592_SFIFO 0x08
+
+
+/* Status register (ms int, small fifo, IO)*/
+#define R592_STATUS 0x10
+ /* Parallel INT bits */
+#define R592_STATUS_P_CMDNACK (1 << 16) /* INT reg: NACK (parallel mode) */
+#define R592_STATUS_P_BREQ (1 << 17) /* INT reg: card ready (parallel mode)*/
+#define R592_STATUS_P_INTERR (1 << 18) /* INT reg: int error (parallel mode)*/
+#define R592_STATUS_P_CED (1 << 19) /* INT reg: command done (parallel mode) */
+
+ /* Fifo status */
+#define R592_STATUS_SFIFO_FULL (1 << 20) /* Small Fifo almost full (last chunk is written) */
+#define R592_STATUS_SFIFO_EMPTY (1 << 21) /* Small Fifo empty */
+
+ /* Error detection via CRC */
+#define R592_STATUS_SEND_ERR (1 << 24) /* Send failed */
+#define R592_STATUS_RECV_ERR (1 << 25) /* Recieve failed */
+
+ /* Card state */
+#define R592_STATUS_RDY (1 << 28) /* RDY signal recieved */
+#define R592_STATUS_CED (1 << 29) /* INT: Command done (serial mode)*/
+#define R592_STATUS_SFIFO_INPUT (1 << 30) /* Small fifo recieved data*/
+
+
+#define R592_SFIFO_SIZE 32 /* total size of small fifo is 32 bytes */
+#define R592_SFIFO_PACKET 8 /* packet size of small fifo */
+
+/* IO control */
+#define R592_IO 0x18
+ /* 5 */
+#define R592_IO_16 (1 << 16) /* Set by default, can be cleared */
+#define R592_IO_18 (1 << 18) /* Set by default, can be cleared */
+ /* 5 */
+#define R592_IO_SERIAL1 (1 << 20) /* Set by default, can be cleared, (cleared on parallel) */
+#define R592_IO_22 (1 << 22) /* Set by default, can be cleared */
+ /* 4 or 5*/
+#define R592_IO_DIRECTION (1 << 24) /* TPC direction (1 write 0 read) */
+#define R592_IO_26 (1 << 26) /* Set by default, can be cleared */
+ /* 4 8, or C*/
+#define R592_IO_SERIAL2 (1 << 30) /* Set by default, can be cleared (cleared on parallel), serial doesn't work if unset */
+#define R592_IO_RESET (1 << 31) /* Reset, sets defaults*/
+
+
+/* Unknown register 1 (enables internal blocks ???)*/
+#define R592_REG32 0x20 /* bits 0-7 writeable */
+#define R592_REG32_0 (1 << 0) /* set on start, cleared on stop - must be set*/
+#define R592_REG32_1 (1 << 1) /* set on start, cleared on stop - must be set*/
+#define R592_REG32_3 (1 << 3) /* must be clear */
+#define R592_REG32_20 (1 << 5) /* set before switch to parallel */
+
+/* Unknown register (clock control???)*/
+#define R592_REG36 0x24
+#define R592_REG36_0 (1 << 0) /* set on init, never reset (enable clock engine???)*/
+#define R592_REG36_1 (1 << 1) /* set after switch to parallel (fast clock??)*/
+
+
+/* IRQ,card detection,large fifo (first word irq status, second enable) */
+/* IRQs are ACKed by clearing the bits */
+#define R592_REG_MSC 0x28
+#define R592_REG_MSC_PRSNT (1 << 1) /* card present (only status)*/
+#define R592_REG_MSC_IRQ_INSERT (1 << 8) /* detect insert / card insered */
+#define R592_REG_MSC_IRQ_REMOVE (1 << 9) /* detect removal / card removed */
+#define R592_REG_MSC_FIFO_EMPTY (1 << 10) /* fifo is empty */
+#define R592_REG_MSC_FIFO_DMA_DONE (1 << 11) /* dma enable / dma done */
+
+#define R592_REG_MSC_FIFO_USER_ORN (1 << 12) /* set if software reads empty fifo (if R592_REG_MSC_FIFO_EMPTY is set) */
+#define R592_REG_MSC_FIFO_MISMATH (1 << 13) /* set if amount of data in fifo doesn't match amount in TPC */
+#define R592_REG_MSC_FIFO_DMA_ERR (1 << 14) /* IO failure */
+#define R592_REG_MSC_LED (1 << 15) /* clear to turn led off (only status)*/
+
+#define DMA_IRQ_ACK_MASK (R592_REG_MSC_FIFO_DMA_DONE | R592_REG_MSC_FIFO_DMA_ERR)
+#define DMA_IRQ_EN_MASK (DMA_IRQ_ACK_MASK << 16)
+
+/* DMA address for large FIFO read/writes*/
+#define R592_FIFO_DMA 0x2C
+
+/* PIO access to large FIFO (512 bytes) (big endian)*/
+#define R592_FIFO_PIO 0x30
+#define R592_LFIFO_SIZE 512 /* large fifo size */
+
+
+/* large FIFO DMA settings */
+#define R592_FIFO_DMA_SETTINGS 0x34
+#define R592_FIFO_DMA_SETTINGS_EN (1 << 0) /* DMA enabled */
+#define R592_FIFO_DMA_SETTINGS_DIR (1 << 1) /* Dma direction (1 read, 0 write) */
+#define R592_FIFO_DMA_SETTINGS_CAP (1 << 24) /* Dma is aviable */
+
+
+/* Maybe just an delay */
+/* Bits 17..19 are just number */
+/* bit 16 is set, then bit 20 is waited */
+/* time to wait is about 50 spins * 2 ^ (bits 17..19) */
+/* seems to be possible just to ignore */
+#define R592_REG56 0x38
+#define R592_REG56_START (1 << 16) /* Start bit */
+#define R592_REG56_WAIT (1 << 20) /* HW set this after the delay */
+
+/* Scrach register, written (0xABCD00) when error happens - not used*/
+#define R592_REG_DEBUG 0x3C
+
+struct r592_device {
+ struct pci_dev *pci_dev;
+ struct memstick_host *host; /* host backpointer */
+ struct memstick_request *req; /* current request */
+
+ /* Registers, IRQ */
+ void __iomem *mmio;
+ int irq;
+ int insuspend;
+ spinlock_t irq_lock;
+ struct timer_list detect_timer;
+
+
+ struct task_struct *io_thread;
+ bool parallel_mode;
+
+
+ /* DMA area */
+ int dma_capable;
+ int dma_error;
+ struct completion dma_done;
+ void *scrath_dma_page;
+ u32 scrath_dma_page_physical_address;
+ spinlock_t atomic_lock; /* used to enter atomic mode only in PIO */
+};
+
+
+struct dword_buffer {
+ unsigned char dword[4];
+ unsigned char len;
+};
+
+
+#define DRV_NAME "r592"
+
+
+#define dbg(format, ...) \
+ if (debug) \
+ printk(KERN_DEBUG DRV_NAME ": " format "\n", ## __VA_ARGS__)
+
+#define dbg_verbose(format, ...) \
+ if (debug > 1) \
+ printk(KERN_DEBUG DRV_NAME ": " format "\n", ## __VA_ARGS__)
+
+#define dbg_reg(format, ...) \
+ if (debug > 2) \
+ printk(KERN_DEBUG DRV_NAME ": " format "\n", ## __VA_ARGS__)
+
+#define message(format, ...) \
+ printk(KERN_INFO DRV_NAME ": " format "\n", ## __VA_ARGS__)
--
1.7.0.4
^ permalink raw reply related [flat|nested] 51+ messages in thread
* Re: [PATCH 2/2] MEMSTICK: Add driver for Ricoh R5C592 Card reader.
2010-08-03 14:53 ` [PATCH 2/2] MEMSTICK: Add driver for Ricoh R5C592 Card reader Maxim Levitsky
@ 2010-08-04 7:57 ` Alex Dubov
2010-08-04 16:48 ` Maxim Levitsky
0 siblings, 1 reply; 51+ messages in thread
From: Alex Dubov @ 2010-08-04 7:57 UTC (permalink / raw)
To: Maxim Levitsky; +Cc: LKML
I see two immediate problems with this patch:
1. On cosmetic level, custom debug macros should not be employed. Device
core already have this functionality (dynamic debug levels and such). Please,
use dev_dbg and friends for print-outs.
2. On a structural level, I'd rather prefer host drivers to not start their
own threads. If you look at both current host implementations, they operate
in callback fashion. Apart from saving some resources, this reduces the
amount of problems encountered during suspend/resume and shutdown.
^ permalink raw reply [flat|nested] 51+ messages in thread
* Re: [PATCH 2/2] MEMSTICK: Add driver for Ricoh R5C592 Card reader.
2010-08-04 7:57 ` Alex Dubov
@ 2010-08-04 16:48 ` Maxim Levitsky
2010-08-04 19:31 ` Maxim Levitsky
0 siblings, 1 reply; 51+ messages in thread
From: Maxim Levitsky @ 2010-08-04 16:48 UTC (permalink / raw)
To: Alex Dubov; +Cc: LKML
On Wed, 2010-08-04 at 00:57 -0700, Alex Dubov wrote:
> I see two immediate problems with this patch:
>
> 1. On cosmetic level, custom debug macros should not be employed. Device
> core already have this functionality (dynamic debug levels and such). Please,
> use dev_dbg and friends for print-outs.
This allows much easier control for debug.
Single module parameter is enough to adjust it.
This helps me help users.
(Eg, kernel compilation is out of question)
>
> 2. On a structural level, I'd rather prefer host drivers to not start their
> own threads. If you look at both current host implementations, they operate
> in callback fashion. Apart from saving some resources, this reduces the
> amount of problems encountered during suspend/resume and shutdown.
This isn't possible.
Hardware doesn't support interrupts on memstick bus changes, it only
supports DMA done from/to internal FIFO, and DMA it only possible for
512 byte TPCs.
Best regards,
Maxim Levitsky
^ permalink raw reply [flat|nested] 51+ messages in thread
* Re: [PATCH 2/2] MEMSTICK: Add driver for Ricoh R5C592 Card reader.
2010-08-04 16:48 ` Maxim Levitsky
@ 2010-08-04 19:31 ` Maxim Levitsky
0 siblings, 0 replies; 51+ messages in thread
From: Maxim Levitsky @ 2010-08-04 19:31 UTC (permalink / raw)
To: Alex Dubov; +Cc: LKML
On Wed, 2010-08-04 at 19:48 +0300, Maxim Levitsky wrote:
> On Wed, 2010-08-04 at 00:57 -0700, Alex Dubov wrote:
> > I see two immediate problems with this patch:
> >
> > 1. On cosmetic level, custom debug macros should not be employed. Device
> > core already have this functionality (dynamic debug levels and such). Please,
> > use dev_dbg and friends for print-outs.
> This allows much easier control for debug.
> Single module parameter is enough to adjust it.
> This helps me help users.
> (Eg, kernel compilation is out of question)
>
>
> >
> > 2. On a structural level, I'd rather prefer host drivers to not start their
> > own threads. If you look at both current host implementations, they operate
> > in callback fashion. Apart from saving some resources, this reduces the
> > amount of problems encountered during suspend/resume and shutdown.
> This isn't possible.
> Hardware doesn't support interrupts on memstick bus changes, it only
> supports DMA done from/to internal FIFO, and DMA it only possible for
> 512 byte TPCs.
>
Another question.
I see that current code ignores MEMSTICK_CAP_AUTO_GET_INT
Instread mspro_blk.c enables this capability for parallel mode, assuming
that hw supports it. Its true in my case, but might not be true in other
cases.
I think I should fix that, right?
Also I see that you bath TPC_READ_LONG_DATA/TPC_READ_LONG_DATA
Does that mean that every HW sector is larger that 512?
If so, you are doing copy on write, right?
I have small caching in my sm_ftl of last sector. It helps performance a
lot.
Also I want to clarify that the only kind of interrupts supported by hw
(besides usual card detection interrupt), is DMA done interrupt.
Thats why I have to use thread.
Doing polling in r592_submit_req (which runs in atomic context is just
cruel).
Besides, under moderate IO load, the IO thread doesn't sleep, thus there
is no overhead of wake/sleep.
Best regards,
Maxim Levitsky
^ permalink raw reply [flat|nested] 51+ messages in thread
* Re: [PATCH 2/2] MEMSTICK: Add driver for Ricoh R5C592 Card reader.
@ 2010-08-05 8:30 Alex Dubov
2010-08-05 11:20 ` Maxim Levitsky
` (2 more replies)
0 siblings, 3 replies; 51+ messages in thread
From: Alex Dubov @ 2010-08-05 8:30 UTC (permalink / raw)
To: Maxim Levitsky; +Cc: LKML
> Maxim Levitsky wrote:
> > On Wed, 2010-08-04 at 00:57 -0700, Alex Dubov wrote:
> > > I see two immediate problems with this patch:
> > >
> > > 1. On cosmetic level, custom debug macros should
> not be employed. Device
> > > core already have this functionality (dynamic
> debug levels and such). Please,
> > > use dev_dbg and friends for print-outs.
> > This allows much easier control for debug.
> > Single module parameter is enough to adjust it.
> > This helps me help users.
> > (Eg, kernel compilation is out of question)
I doubt it will be that useful, but it's not my call to make anyway.
> >
> >
> > >
> > > 2. On a structural level, I'd rather prefer host
> drivers to not start their
> > > own threads. If you look at both current host
> implementations, they operate
> > > in callback fashion. Apart from saving some
> resources, this reduces the
> > > amount of problems encountered during
> suspend/resume and shutdown.
> > This isn't possible.
> > Hardware doesn't support interrupts on memstick bus
> changes, it only
> > supports DMA done from/to internal FIFO, and DMA it
> only possible for
> > 512 byte TPCs.
> >
>
How depressing.
>
> Another question.
>
> I see that current code ignores MEMSTICK_CAP_AUTO_GET_INT
> Instread mspro_blk.c enables this capability for parallel
> mode, assuming
> that hw supports it. Its true in my case, but might not be
> true in other
> cases.
> I think I should fix that, right?
This is mandated by the spec. INT should be available automatically in
parallel mode, and some hardware does it in serial as well.
>
> Also I see that you bath
> TPC_READ_LONG_DATA/TPC_READ_LONG_DATA
> Does that mean that every HW sector is larger that 512?
> If so, you are doing copy on write, right?
> I have small caching in my sm_ftl of last sector. It helps
> performance a
> lot.
That's how its called in the spec.
Sectors can be larger than 512b on Pro-HG sticks, and there's additional
TPC_READ/WRITE_QUAD_DATA which operates on larger quantities.
>
>
> Also I want to clarify that the only kind of interrupts
> supported by hw
> (besides usual card detection interrupt), is DMA done
> interrupt.
> Thats why I have to use thread.
> Doing polling in r592_submit_req (which runs in atomic
> context is just
> cruel).
Yes, I see you have a timed wait there.
> Besides, under moderate IO load, the IO thread doesn't
> sleep, thus there
> is no overhead of wake/sleep.
>
>
> Best regards,
> Maxim Levitsky
>
>
^ permalink raw reply [flat|nested] 51+ messages in thread
* Re: [PATCH 2/2] MEMSTICK: Add driver for Ricoh R5C592 Card reader.
2010-08-05 8:30 [PATCH 2/2] MEMSTICK: Add driver for Ricoh R5C592 Card reader Alex Dubov
@ 2010-08-05 11:20 ` Maxim Levitsky
2010-08-05 11:48 ` Alex Dubov
2010-08-05 12:46 ` Maxim Levitsky
2010-08-07 20:22 ` Maxim Levitsky
2 siblings, 1 reply; 51+ messages in thread
From: Maxim Levitsky @ 2010-08-05 11:20 UTC (permalink / raw)
To: Alex Dubov; +Cc: LKML
On Thu, 2010-08-05 at 01:30 -0700, Alex Dubov wrote:
> > Maxim Levitsky wrote:
> > > On Wed, 2010-08-04 at 00:57 -0700, Alex Dubov wrote:
> > > > I see two immediate problems with this patch:
> > > >
> > > > 1. On cosmetic level, custom debug macros should
> > not be employed. Device
> > > > core already have this functionality (dynamic
> > debug levels and such). Please,
> > > > use dev_dbg and friends for print-outs.
> > > This allows much easier control for debug.
> > > Single module parameter is enough to adjust it.
> > > This helps me help users.
> > > (Eg, kernel compilation is out of question)
>
> I doubt it will be that useful, but it's not my call to make anyway.
>
>
> > >
> > >
> > > >
> > > > 2. On a structural level, I'd rather prefer host
> > drivers to not start their
> > > > own threads. If you look at both current host
> > implementations, they operate
> > > > in callback fashion. Apart from saving some
> > resources, this reduces the
> > > > amount of problems encountered during
> > suspend/resume and shutdown.
> > > This isn't possible.
> > > Hardware doesn't support interrupts on memstick bus
> > changes, it only
> > > supports DMA done from/to internal FIFO, and DMA it
> > only possible for
> > > 512 byte TPCs.
> > >
> >
>
> How depressing.
>
> >
> > Another question.
> >
> > I see that current code ignores MEMSTICK_CAP_AUTO_GET_INT
> > Instread mspro_blk.c enables this capability for parallel
> > mode, assuming
> > that hw supports it. Its true in my case, but might not be
> > true in other
> > cases.
> > I think I should fix that, right?
>
> This is mandated by the spec. INT should be available automatically in
> parallel mode, and some hardware does it in serial as well.
>
> >
> > Also I see that you bath
> > TPC_READ_LONG_DATA/TPC_READ_LONG_DATA
> > Does that mean that every HW sector is larger that 512?
> > If so, you are doing copy on write, right?
> > I have small caching in my sm_ftl of last sector. It helps
> > performance a
> > lot.
>
>
> That's how its called in the spec.
> Sectors can be larger than 512b on Pro-HG sticks, and there's additional
> TPC_READ/WRITE_QUAD_DATA which operates on larger quantities.
But not on ordinary PRO, right?
Small question, can I use Pro-HG stick in my reader that is designed for
Standard/PRO only? Does your subsystem support it?
8-bit mode really isn't supported here, but it is optional I am sure.
>
> >
> >
> > Also I want to clarify that the only kind of interrupts
> > supported by hw
> > (besides usual card detection interrupt), is DMA done
> > interrupt.
> > Thats why I have to use thread.
> > Doing polling in r592_submit_req (which runs in atomic
> > context is just
> > cruel).
>
> Yes, I see you have a timed wait there.
>
Alex, how should I proceed in merge of my driver?
Do you have any objections against it now?
Best regards,
Maxim Levitsky
^ permalink raw reply [flat|nested] 51+ messages in thread
* Re: [PATCH 2/2] MEMSTICK: Add driver for Ricoh R5C592 Card reader.
2010-08-05 11:20 ` Maxim Levitsky
@ 2010-08-05 11:48 ` Alex Dubov
2010-08-05 12:30 ` Maxim Levitsky
0 siblings, 1 reply; 51+ messages in thread
From: Alex Dubov @ 2010-08-05 11:48 UTC (permalink / raw)
To: Maxim Levitsky; +Cc: LKML
> >
> > That's how its called in the spec.
> > Sectors can be larger than 512b on Pro-HG sticks, and
> there's additional
> > TPC_READ/WRITE_QUAD_DATA which operates on larger
> quantities.
> But not on ordinary PRO, right?
Pro sectors are normally 512 bytes.
>
> Small question, can I use Pro-HG stick in my reader that is
> designed for
> Standard/PRO only? Does your subsystem support it?
It should work. It works for me on TI, which has no 8b mode either.
> >
>
> Alex, how should I proceed in merge of my driver?
> Do you have any objections against it now?
>
I may have done a thing or two differently, but otherwise no objection.
I suppose, you should ask Andrew Morton to put it in.
^ permalink raw reply [flat|nested] 51+ messages in thread
* Re: [PATCH 2/2] MEMSTICK: Add driver for Ricoh R5C592 Card reader.
2010-08-05 11:48 ` Alex Dubov
@ 2010-08-05 12:30 ` Maxim Levitsky
2010-08-05 17:47 ` Maxim Levitsky
2010-08-06 8:01 ` Alex Dubov
0 siblings, 2 replies; 51+ messages in thread
From: Maxim Levitsky @ 2010-08-05 12:30 UTC (permalink / raw)
To: Alex Dubov; +Cc: LKML
On Thu, 2010-08-05 at 04:48 -0700, Alex Dubov wrote:
> > >
> > > That's how its called in the spec.
> > > Sectors can be larger than 512b on Pro-HG sticks, and
> > there's additional
> > > TPC_READ/WRITE_QUAD_DATA which operates on larger
> > quantities.
> > But not on ordinary PRO, right?
>
> Pro sectors are normally 512 bytes.
>
> >
> > Small question, can I use Pro-HG stick in my reader that is
> > designed for
> > Standard/PRO only? Does your subsystem support it?
>
> It should work. It works for me on TI, which has no 8b mode either.
In that case, is there an upper limit on sector size?
>
> > >
> >
> > Alex, how should I proceed in merge of my driver?
> > Do you have any objections against it now?
> >
>
> I may have done a thing or two differently, but otherwise no objection.
> I suppose, you should ask Andrew Morton to put it in.
Ok, will do.
Best regards,
Maxim Levitsky
^ permalink raw reply [flat|nested] 51+ messages in thread
* Re: [PATCH 2/2] MEMSTICK: Add driver for Ricoh R5C592 Card reader.
2010-08-05 8:30 [PATCH 2/2] MEMSTICK: Add driver for Ricoh R5C592 Card reader Alex Dubov
2010-08-05 11:20 ` Maxim Levitsky
@ 2010-08-05 12:46 ` Maxim Levitsky
2010-08-06 7:59 ` Alex Dubov
2010-08-07 20:22 ` Maxim Levitsky
2 siblings, 1 reply; 51+ messages in thread
From: Maxim Levitsky @ 2010-08-05 12:46 UTC (permalink / raw)
To: Alex Dubov; +Cc: LKML
On Thu, 2010-08-05 at 01:30 -0700, Alex Dubov wrote:
> > Maxim Levitsky wrote:
> > > On Wed, 2010-08-04 at 00:57 -0700, Alex Dubov wrote:
> > > > I see two immediate problems with this patch:
> > > >
> > > > 1. On cosmetic level, custom debug macros should
> > not be employed. Device
> > > > core already have this functionality (dynamic
> > debug levels and such). Please,
> > > > use dev_dbg and friends for print-outs.
> > > This allows much easier control for debug.
> > > Single module parameter is enough to adjust it.
> > > This helps me help users.
> > > (Eg, kernel compilation is out of question)
>
> I doubt it will be that useful, but it's not my call to make anyway.
>
>
> > >
> > >
> > > >
> > > > 2. On a structural level, I'd rather prefer host
> > drivers to not start their
> > > > own threads. If you look at both current host
> > implementations, they operate
> > > > in callback fashion. Apart from saving some
> > resources, this reduces the
> > > > amount of problems encountered during
> > suspend/resume and shutdown.
> > > This isn't possible.
> > > Hardware doesn't support interrupts on memstick bus
> > changes, it only
> > > supports DMA done from/to internal FIFO, and DMA it
> > only possible for
> > > 512 byte TPCs.
> > >
> >
>
> How depressing.
>
> >
> > Another question.
> >
> > I see that current code ignores MEMSTICK_CAP_AUTO_GET_INT
> > Instread mspro_blk.c enables this capability for parallel
> > mode, assuming
> > that hw supports it. Its true in my case, but might not be
> > true in other
> > cases.
> > I think I should fix that, right?
>
> This is mandated by the spec. INT should be available automatically in
> parallel mode, and some hardware does it in serial as well.
Thinking again about that I agree with you.
Then I can remove 2 more lines from my driver.
Btw, I want to add a callback from driver to card driver to be able to
reset card in case of error (windows driver does that in case of any
error)
I would use most of the mspro_block_resume for the implementation for
mspro.
Any objections, suggestions?
>
> >
> > Also I see that you bath
> > TPC_READ_LONG_DATA/TPC_READ_LONG_DATA
> > Does that mean that every HW sector is larger that 512?
> > If so, you are doing copy on write, right?
> > I have small caching in my sm_ftl of last sector. It helps
> > performance a
> > lot.
>
>
> That's how its called in the spec.
> Sectors can be larger than 512b on Pro-HG sticks, and there's additional
> TPC_READ/WRITE_QUAD_DATA which operates on larger quantities.
>
> >
> >
> > Also I want to clarify that the only kind of interrupts
> > supported by hw
> > (besides usual card detection interrupt), is DMA done
> > interrupt.
> > Thats why I have to use thread.
> > Doing polling in r592_submit_req (which runs in atomic
> > context is just
> > cruel).
>
> Yes, I see you have a timed wait there.
>
>
> > Besides, under moderate IO load, the IO thread doesn't
> > sleep, thus there
> > is no overhead of wake/sleep.
> >
> >
> > Best regards,
> > Maxim Levitsky
> >
Best regards,
Maxim Levitsky
^ permalink raw reply [flat|nested] 51+ messages in thread
* Re: [PATCH 2/2] MEMSTICK: Add driver for Ricoh R5C592 Card reader.
2010-08-05 12:30 ` Maxim Levitsky
@ 2010-08-05 17:47 ` Maxim Levitsky
2010-08-06 7:43 ` Alex Dubov
2010-08-06 8:01 ` Alex Dubov
1 sibling, 1 reply; 51+ messages in thread
From: Maxim Levitsky @ 2010-08-05 17:47 UTC (permalink / raw)
To: Alex Dubov; +Cc: LKML
On Thu, 2010-08-05 at 15:30 +0300, Maxim Levitsky wrote:
> On Thu, 2010-08-05 at 04:48 -0700, Alex Dubov wrote:
> > > >
> > > > That's how its called in the spec.
> > > > Sectors can be larger than 512b on Pro-HG sticks, and
> > > there's additional
> > > > TPC_READ/WRITE_QUAD_DATA which operates on larger
> > > quantities.
> > > But not on ordinary PRO, right?
> >
> > Pro sectors are normally 512 bytes.
> >
> > >
> > > Small question, can I use Pro-HG stick in my reader that is
> > > designed for
> > > Standard/PRO only? Does your subsystem support it?
> >
> > It should work. It works for me on TI, which has no 8b mode either.
> In that case, is there an upper limit on sector size?
Also, you don't use the MS_TPC_READ_QUAD_DATA at all.
So mspro_blk.c won't work with >512 bytes/sector disk?
Or there is some compatibility?
Best regards,
Maxim Levitsky
^ permalink raw reply [flat|nested] 51+ messages in thread
* Re: [PATCH 2/2] MEMSTICK: Add driver for Ricoh R5C592 Card reader.
2010-08-05 17:47 ` Maxim Levitsky
@ 2010-08-06 7:43 ` Alex Dubov
2010-08-06 10:56 ` Maxim Levitsky
0 siblings, 1 reply; 51+ messages in thread
From: Alex Dubov @ 2010-08-06 7:43 UTC (permalink / raw)
To: Maxim Levitsky; +Cc: LKML
> > > > >
> > > > > That's how its called in the spec.
> > > > > Sectors can be larger than 512b on
> Pro-HG sticks, and
> > > > there's additional
> > > > > TPC_READ/WRITE_QUAD_DATA which operates
> on larger
> > > > quantities.
> > > > But not on ordinary PRO, right?
> > >
> > > Pro sectors are normally 512 bytes.
> > >
> > > >
> > > > Small question, can I use Pro-HG stick in my
> reader that is
> > > > designed for
> > > > Standard/PRO only? Does your subsystem
> support it?
> > >
> > > It should work. It works for me on TI, which has
> no 8b mode either.
> > In that case, is there an upper limit on sector size?
> Also, you don't use the MS_TPC_READ_QUAD_DATA at all.
> So mspro_blk.c won't work with >512 bytes/sector disk?
> Or there is some compatibility?
>
The code can work with arbitrarily sized pages, 512b
value is not hard coded. All that is needed is to slightly alter
h_mspro_block_transfer_data function, given, of course, that adapters
support longer transfer sizes.
^ permalink raw reply [flat|nested] 51+ messages in thread
* Re: [PATCH 2/2] MEMSTICK: Add driver for Ricoh R5C592 Card reader.
2010-08-05 12:46 ` Maxim Levitsky
@ 2010-08-06 7:59 ` Alex Dubov
2010-08-06 10:59 ` Maxim Levitsky
0 siblings, 1 reply; 51+ messages in thread
From: Alex Dubov @ 2010-08-06 7:59 UTC (permalink / raw)
To: Maxim Levitsky; +Cc: LKML
>
> Btw, I want to add a callback from driver to card driver to
> be able to
> reset card in case of error (windows driver does that in
> case of any
> error)
>
> I would use most of the mspro_block_resume for the
> implementation for
> mspro.
>
> Any objections, suggestions?
>
Just toggle a power on it. Power off/power on will do the full reset of
the controller and the media. You don't have to reinitialize it, as you
are sure that it's the same stick.
^ permalink raw reply [flat|nested] 51+ messages in thread
* Re: [PATCH 2/2] MEMSTICK: Add driver for Ricoh R5C592 Card reader.
2010-08-05 12:30 ` Maxim Levitsky
2010-08-05 17:47 ` Maxim Levitsky
@ 2010-08-06 8:01 ` Alex Dubov
1 sibling, 0 replies; 51+ messages in thread
From: Alex Dubov @ 2010-08-06 8:01 UTC (permalink / raw)
To: Maxim Levitsky; +Cc: LKML
> >
> > >
> > > Small question, can I use Pro-HG stick in my
> reader that is
> > > designed for
> > > Standard/PRO only? Does your subsystem support
> it?
> >
> > It should work. It works for me on TI, which has no 8b
> mode either.
> In that case, is there an upper limit on sector size?
>
Good code has no artificially induced limits. Less than machine page.
^ permalink raw reply [flat|nested] 51+ messages in thread
* Re: [PATCH 2/2] MEMSTICK: Add driver for Ricoh R5C592 Card reader.
2010-08-06 7:43 ` Alex Dubov
@ 2010-08-06 10:56 ` Maxim Levitsky
2010-08-07 13:15 ` Alex Dubov
0 siblings, 1 reply; 51+ messages in thread
From: Maxim Levitsky @ 2010-08-06 10:56 UTC (permalink / raw)
To: Alex Dubov; +Cc: LKML
On Fri, 2010-08-06 at 00:43 -0700, Alex Dubov wrote:
> > > > > >
> > > > > > That's how its called in the spec.
> > > > > > Sectors can be larger than 512b on
> > Pro-HG sticks, and
> > > > > there's additional
> > > > > > TPC_READ/WRITE_QUAD_DATA which operates
> > on larger
> > > > > quantities.
> > > > > But not on ordinary PRO, right?
> > > >
> > > > Pro sectors are normally 512 bytes.
> > > >
> > > > >
> > > > > Small question, can I use Pro-HG stick in my
> > reader that is
> > > > > designed for
> > > > > Standard/PRO only? Does your subsystem
> > support it?
> > > >
> > > > It should work. It works for me on TI, which has
> > no 8b mode either.
> > > In that case, is there an upper limit on sector size?
> > Also, you don't use the MS_TPC_READ_QUAD_DATA at all.
> > So mspro_blk.c won't work with >512 bytes/sector disk?
> > Or there is some compatibility?
> >
>
> The code can work with arbitrarily sized pages, 512b
> value is not hard coded. All that is needed is to slightly alter
> h_mspro_block_transfer_data function, given, of course, that adapters
> support longer transfer
No, I mean if I go and buy memstick PRO HG, that has > 512 bytes/sector,
will it work with current code?
Btw, there is currently no way of telling core about maximum transfer
length.
Here absolute maximum is 1024 (number of bit that _might_ hold TPC
length), and FIFO size is 512 bytes (maybe its possible to use fifo
twice)
Best regards,
Maxim Levitsky
^ permalink raw reply [flat|nested] 51+ messages in thread
* Re: [PATCH 2/2] MEMSTICK: Add driver for Ricoh R5C592 Card reader.
2010-08-06 7:59 ` Alex Dubov
@ 2010-08-06 10:59 ` Maxim Levitsky
2010-08-07 13:12 ` Alex Dubov
0 siblings, 1 reply; 51+ messages in thread
From: Maxim Levitsky @ 2010-08-06 10:59 UTC (permalink / raw)
To: Alex Dubov; +Cc: LKML
On Fri, 2010-08-06 at 00:59 -0700, Alex Dubov wrote:
> >
> > Btw, I want to add a callback from driver to card driver to
> > be able to
> > reset card in case of error (windows driver does that in
> > case of any
> > error)
> >
> > I would use most of the mspro_block_resume for the
> > implementation for
> > mspro.
> >
> > Any objections, suggestions?
> >
>
> Just toggle a power on it. Power off/power on will do the full reset of
> the controller and the media. You don't have to reinitialize it, as you
> are sure that it's the same stick.
Yea, but after such reboot, the device will be in serial mode. So, I
will need to send switch TPC to device. My driver doesn't know how to do
that...., so it would be nice to have callback to the core.
Best regards,
Maxim Levitsky
^ permalink raw reply [flat|nested] 51+ messages in thread
* Re: [PATCH 2/2] MEMSTICK: Add driver for Ricoh R5C592 Card reader.
2010-08-06 10:59 ` Maxim Levitsky
@ 2010-08-07 13:12 ` Alex Dubov
2010-08-07 16:03 ` Maxim Levitsky
0 siblings, 1 reply; 51+ messages in thread
From: Alex Dubov @ 2010-08-07 13:12 UTC (permalink / raw)
To: Maxim Levitsky; +Cc: LKML
> > >
> > > Btw, I want to add a callback from driver to card
> driver to
> > > be able to
> > > reset card in case of error (windows driver does
> that in
> > > case of any
> > > error)
> > >
> > > I would use most of the mspro_block_resume for
> the
> > > implementation for
> > > mspro.
> > >
> > > Any objections, suggestions?
> > >
> >
> > Just toggle a power on it. Power off/power on will do
> the full reset of
> > the controller and the media. You don't have to
> reinitialize it, as you
> > are sure that it's the same stick.
>
> Yea, but after such reboot, the device will be in serial
> mode. So, I
> will need to send switch TPC to device. My driver doesn't
> know how to do
> that...., so it would be nice to have callback to the
> core.
>
Lets ask a different question: why do you think this
particular functionality is needed at all? Have you encountered any
problems which require it (I haven't, btw)?
Windows drivers do a lot of things, but it does not mean all of them must
be copied literally.
^ permalink raw reply [flat|nested] 51+ messages in thread
* Re: [PATCH 2/2] MEMSTICK: Add driver for Ricoh R5C592 Card reader.
2010-08-06 10:56 ` Maxim Levitsky
@ 2010-08-07 13:15 ` Alex Dubov
2010-08-07 15:58 ` Maxim Levitsky
0 siblings, 1 reply; 51+ messages in thread
From: Alex Dubov @ 2010-08-07 13:15 UTC (permalink / raw)
To: Maxim Levitsky; +Cc: LKML
> > > > > > >
> > > > > > > That's how its called in the
> spec.
> > > > > > > Sectors can be larger than
> 512b on
> > > Pro-HG sticks, and
> > > > > > there's additional
> > > > > > > TPC_READ/WRITE_QUAD_DATA
> which operates
> > > on larger
> > > > > > quantities.
> > > > > > But not on ordinary PRO, right?
> > > > >
> > > > > Pro sectors are normally 512 bytes.
> > > > >
> > > > > >
> > > > > > Small question, can I use Pro-HG
> stick in my
> > > reader that is
> > > > > > designed for
> > > > > > Standard/PRO only? Does your
> subsystem
> > > support it?
> > > > >
> > > > > It should work. It works for me on TI,
> which has
> > > no 8b mode either.
> > > > In that case, is there an upper limit on
> sector size?
> > > Also, you don't use the MS_TPC_READ_QUAD_DATA at
> all.
> > > So mspro_blk.c won't work with >512
> bytes/sector disk?
> > > Or there is some compatibility?
> > >
> >
> > The code can work with arbitrarily sized pages, 512b
> > value is not hard coded. All that is needed is to
> slightly alter
> > h_mspro_block_transfer_data function, given, of
> course, that adapters
> > support longer transfer
>
> No, I mean if I go and buy memstick PRO HG, that has >
> 512 bytes/sector,
> will it work with current code?
>
> Btw, there is currently no way of telling core about
> maximum transfer
> length.
> Here absolute maximum is 1024 (number of bit that _might_
> hold TPC
> length), and FIFO size is 512 bytes (maybe its possible to
> use fifo
> twice)
>
On TI adapters, FIFO can be reused and DMA restarted. Jmicron adapters are
funny beasts, but their team is keen to support MSpro-HG, so at some
point it will be fully possible, though, probably, not with every version
of the chipset.
^ permalink raw reply [flat|nested] 51+ messages in thread
* Re: [PATCH 2/2] MEMSTICK: Add driver for Ricoh R5C592 Card reader.
2010-08-07 13:15 ` Alex Dubov
@ 2010-08-07 15:58 ` Maxim Levitsky
2010-08-08 13:31 ` Alex Dubov
0 siblings, 1 reply; 51+ messages in thread
From: Maxim Levitsky @ 2010-08-07 15:58 UTC (permalink / raw)
To: Alex Dubov; +Cc: LKML
On Sat, 2010-08-07 at 06:15 -0700, Alex Dubov wrote:
> > > > > > > >
> > > > > > > > That's how its called in the
> > spec.
> > > > > > > > Sectors can be larger than
> > 512b on
> > > > Pro-HG sticks, and
> > > > > > > there's additional
> > > > > > > > TPC_READ/WRITE_QUAD_DATA
> > which operates
> > > > on larger
> > > > > > > quantities.
> > > > > > > But not on ordinary PRO, right?
> > > > > >
> > > > > > Pro sectors are normally 512 bytes.
> > > > > >
> > > > > > >
> > > > > > > Small question, can I use Pro-HG
> > stick in my
> > > > reader that is
> > > > > > > designed for
> > > > > > > Standard/PRO only? Does your
> > subsystem
> > > > support it?
> > > > > >
> > > > > > It should work. It works for me on TI,
> > which has
> > > > no 8b mode either.
> > > > > In that case, is there an upper limit on
> > sector size?
> > > > Also, you don't use the MS_TPC_READ_QUAD_DATA at
> > all.
> > > > So mspro_blk.c won't work with >512
> > bytes/sector disk?
> > > > Or there is some compatibility?
> > > >
> > >
> > > The code can work with arbitrarily sized pages, 512b
> > > value is not hard coded. All that is needed is to
> > slightly alter
> > > h_mspro_block_transfer_data function, given, of
> > course, that adapters
> > > support longer transfer
> >
> > No, I mean if I go and buy memstick PRO HG, that has >
> > 512 bytes/sector,
> > will it work with current code?
> >
> > Btw, there is currently no way of telling core about
> > maximum transfer
> > length.
> > Here absolute maximum is 1024 (number of bit that _might_
> > hold TPC
> > length), and FIFO size is 512 bytes (maybe its possible to
> > use fifo
> > twice)
> >
>
> On TI adapters, FIFO can be reused and DMA restarted. Jmicron adapters are
> funny beasts, but their team is keen to support MSpro-HG, so at some
> point it will be fully possible, though, probably, not with every version
> of the chipset.
>
I still need an answer for whether HG sticks need MS_TPC_READ_QUAD_DATA
to be used.
But anyway, I buy a HG stick and see how it works.
Best regards,
Maxim Levitsky
^ permalink raw reply [flat|nested] 51+ messages in thread
* Re: [PATCH 2/2] MEMSTICK: Add driver for Ricoh R5C592 Card reader.
2010-08-07 13:12 ` Alex Dubov
@ 2010-08-07 16:03 ` Maxim Levitsky
2010-08-08 13:33 ` Alex Dubov
0 siblings, 1 reply; 51+ messages in thread
From: Maxim Levitsky @ 2010-08-07 16:03 UTC (permalink / raw)
To: Alex Dubov; +Cc: LKML
On Sat, 2010-08-07 at 06:12 -0700, Alex Dubov wrote:
> > > >
> > > > Btw, I want to add a callback from driver to card
> > driver to
> > > > be able to
> > > > reset card in case of error (windows driver does
> > that in
> > > > case of any
> > > > error)
> > > >
> > > > I would use most of the mspro_block_resume for
> > the
> > > > implementation for
> > > > mspro.
> > > >
> > > > Any objections, suggestions?
> > > >
> > >
> > > Just toggle a power on it. Power off/power on will do
> > the full reset of
> > > the controller and the media. You don't have to
> > reinitialize it, as you
> > > are sure that it's the same stick.
> >
> > Yea, but after such reboot, the device will be in serial
> > mode. So, I
> > will need to send switch TPC to device. My driver doesn't
> > know how to do
> > that...., so it would be nice to have callback to the
> > core.
> >
>
> Lets ask a different question: why do you think this
> particular functionality is needed at all? Have you encountered any
> problems which require it (I haven't, btw)?
I would image using this to verify that stick is still in place on I/O
error.
If I just power down/up it, it will loose state, for example IO mode,
reg range, etc...
But anyway, this isn't very important.
Best regards,
Maxim Levitsky
^ permalink raw reply [flat|nested] 51+ messages in thread
* Re: [PATCH 2/2] MEMSTICK: Add driver for Ricoh R5C592 Card reader.
2010-08-05 8:30 [PATCH 2/2] MEMSTICK: Add driver for Ricoh R5C592 Card reader Alex Dubov
2010-08-05 11:20 ` Maxim Levitsky
2010-08-05 12:46 ` Maxim Levitsky
@ 2010-08-07 20:22 ` Maxim Levitsky
2010-08-08 14:26 ` Alex Dubov
2 siblings, 1 reply; 51+ messages in thread
From: Maxim Levitsky @ 2010-08-07 20:22 UTC (permalink / raw)
To: Alex Dubov; +Cc: LKML
Hi,
I have few more questions about memsticks.
First of all, I need an explanation of overwrite flag.
You already explained it to me once, but I still not sure about few
things.
#define MEMSTICK_OVERWRITE_UDST 0x10
This one I understand, thinking about xD again, I think it is very
handy.
My idea (from xD of course) is that copyonwrite is done this way:
1. read old sector
2. allocate new sector
2. write what was just read to new sector.
3. erase old sector.
Could you explain when I need to set and reset the
MEMSTICK_OVERWRITE_UDST?
#define MEMSTICK_OVERWRITE_PGST1 0x20
#define MEMSTICK_OVERWRITE_PGST0 0x40
I suppose these indicate that page(sector) contains incorrect data, just
like in xD there is page status?
Again, better explanation is welcome.
Also, should I touch that flag when I update sector?
#define MEMSTICK_OVERWRITE_BKST 0x80
This marks bad blocks?
Another question is about write of oob data.
When I write it, overwrite flag is updated, or I need to use
MEMSTICK_CP_OVERWRITE to update it?
I think former is true.
When I write a sector, I just write 0 to management flag, right?
And last question,
If I use MEMSTICK_CP_BLOCK, can I start reading a block from non-zero
page offset?
And surely last question, what is 'MS_CMD_BLOCK_END'
Thanks again for all help so far,
Best regards,
Maxim Levitsky
^ permalink raw reply [flat|nested] 51+ messages in thread
* Re: [PATCH 2/2] MEMSTICK: Add driver for Ricoh R5C592 Card reader.
2010-08-07 15:58 ` Maxim Levitsky
@ 2010-08-08 13:31 ` Alex Dubov
0 siblings, 0 replies; 51+ messages in thread
From: Alex Dubov @ 2010-08-08 13:31 UTC (permalink / raw)
To: Maxim Levitsky; +Cc: LKML
--- On Sat, 7/8/10, Maxim Levitsky <maximlevitsky@gmail.com> wrote:
> From: Maxim Levitsky <maximlevitsky@gmail.com>
> Subject: Re: [PATCH 2/2] MEMSTICK: Add driver for Ricoh R5C592 Card reader.
> To: "Alex Dubov" <oakad@yahoo.com>
> Cc: "LKML" <linux-kernel@vger.kernel.org>
> Received: Saturday, 7 August, 2010, 8:58 AM
> On Sat, 2010-08-07 at 06:15 -0700,
> Alex Dubov wrote:
> > > > > > > > >
> > > > > > > > > That's how its
> called in the
> > > spec.
> > > > > > > > > Sectors can be
> larger than
> > > 512b on
> > > > > Pro-HG sticks, and
> > > > > > > > there's additional
> > > > > > > > >
> TPC_READ/WRITE_QUAD_DATA
> > > which operates
> > > > > on larger
> > > > > > > > quantities.
> > > > > > > > But not on ordinary PRO,
> right?
> > > > > > >
> > > > > > > Pro sectors are normally 512
> bytes.
> > > > > > >
> > > > > > > >
> > > > > > > > Small question, can I
> use Pro-HG
> > > stick in my
> > > > > reader that is
> > > > > > > > designed for
> > > > > > > > Standard/PRO only? Does
> your
> > > subsystem
> > > > > support it?
> > > > > > >
> > > > > > > It should work. It works for
> me on TI,
> > > which has
> > > > > no 8b mode either.
> > > > > > In that case, is there an upper
> limit on
> > > sector size?
> > > > > Also, you don't use the
> MS_TPC_READ_QUAD_DATA at
> > > all.
> > > > > So mspro_blk.c won't work with >512
> > > bytes/sector disk?
> > > > > Or there is some compatibility?
> > > > >
> > > >
> > > > The code can work with arbitrarily sized
> pages, 512b
> > > > value is not hard coded. All that is needed
> is to
> > > slightly alter
> > > > h_mspro_block_transfer_data function, given,
> of
> > > course, that adapters
> > > > support longer transfer
> > >
> > > No, I mean if I go and buy memstick PRO HG, that
> has >
> > > 512 bytes/sector,
> > > will it work with current code?
> > >
> > > Btw, there is currently no way of telling core
> about
> > > maximum transfer
> > > length.
> > > Here absolute maximum is 1024 (number of bit that
> _might_
> > > hold TPC
> > > length), and FIFO size is 512 bytes (maybe its
> possible to
> > > use fifo
> > > twice)
> > >
> >
> > On TI adapters, FIFO can be reused and DMA restarted.
> Jmicron adapters are
> > funny beasts, but their team is keen to support
> MSpro-HG, so at some
> > point it will be fully possible, though, probably, not
> with every version
> > of the chipset.
> >
>
> I still need an answer for whether HG sticks need
> MS_TPC_READ_QUAD_DATA
> to be used.
No, they work fine with LONG_DATA, at least the ones I tried.
>
> But anyway, I buy a HG stick and see how it works.
This is always a right thing to do.
>
> Best regards,
> Maxim Levitsky
>
>
^ permalink raw reply [flat|nested] 51+ messages in thread
* Re: [PATCH 2/2] MEMSTICK: Add driver for Ricoh R5C592 Card reader.
2010-08-07 16:03 ` Maxim Levitsky
@ 2010-08-08 13:33 ` Alex Dubov
0 siblings, 0 replies; 51+ messages in thread
From: Alex Dubov @ 2010-08-08 13:33 UTC (permalink / raw)
To: Maxim Levitsky; +Cc: LKML
--- On Sat, 7/8/10, Maxim Levitsky <maximlevitsky@gmail.com> wrote:
> > > > >
> > > > > Btw, I want to add a callback from
> driver to card
> > > driver to
> > > > > be able to
> > > > > reset card in case of error (windows
> driver does
> > > that in
> > > > > case of any
> > > > > error)
> > > > >
> > > > > I would use most of the
> mspro_block_resume for
> > > the
> > > > > implementation for
> > > > > mspro.
> > > > >
> > > > > Any objections, suggestions?
> > > > >
> > > >
> > > > Just toggle a power on it. Power off/power
> on will do
> > > the full reset of
> > > > the controller and the media. You don't have
> to
> > > reinitialize it, as you
> > > > are sure that it's the same stick.
> > >
> > > Yea, but after such reboot, the device will be in
> serial
> > > mode. So, I
> > > will need to send switch TPC to device. My driver
> doesn't
> > > know how to do
> > > that...., so it would be nice to have callback to
> the
> > > core.
> > >
> >
> > Lets ask a different question: why do you think this
> > particular functionality is needed at all? Have you
> encountered any
> > problems which require it (I haven't, btw)?
>
> I would image using this to verify that stick is still in
> place on I/O
> error.
>
> If I just power down/up it, it will loose state, for
> example IO mode,
> reg range, etc...
>
> But anyway, this isn't very important.
>
My point was, that problems are better solved as they come.
Windows drivers do a lot of things (like SCSI device emulation), but it
doesn't mean all of them are necessary. Adding code "just because" often
means more work later on.
^ permalink raw reply [flat|nested] 51+ messages in thread
* Re: [PATCH 2/2] MEMSTICK: Add driver for Ricoh R5C592 Card reader.
2010-08-07 20:22 ` Maxim Levitsky
@ 2010-08-08 14:26 ` Alex Dubov
2010-08-08 15:07 ` Maxim Levitsky
0 siblings, 1 reply; 51+ messages in thread
From: Alex Dubov @ 2010-08-08 14:26 UTC (permalink / raw)
To: Maxim Levitsky; +Cc: LKML
> Hi,
>
> I have few more questions about memsticks.
>
> First of all, I need an explanation of overwrite flag.
> You already explained it to me once, but I still not sure
> about few
> things.
Overwrite register can be accessed either as part of extra data access
or separately (CP_OVERWRITE access mode).
>
> #define MEMSTICK_OVERWRITE_UDST 0x10
> This one I understand, thinking about xD again, I think it
> is very
> handy.
>
> My idea (from xD of course) is that copyonwrite is done
> this way:
>
> 1. read old sector
> 2. allocate new sector
> 2. write what was just read to new sector.
> 3. erase old sector.
This is correct.
>
> Could you explain when I need to set and reset the
> MEMSTICK_OVERWRITE_UDST?
UDST flag should be set when you're marking the block for
reallocation during the read/modify/write cycle. You read the existing
physical block, mark it with UDST flag (setting it to zero), then write
different physical block on behalf of the same logical one, then erase the
original block. The UDST flag is supposed to guard against a situation,
whereupon power fails during the write cycle and you're left with two
physical blocks mapped to the same logical one (so the one marked with
zero UDST value is supposedly "known good").
>
>
> #define MEMSTICK_OVERWRITE_PGST1 0x20
> #define MEMSTICK_OVERWRITE_PGST0 0x40
> I suppose these indicate that page(sector) contains
> incorrect data, just
> like in xD there is page status?
> Again, better explanation is welcome.
> Also, should I touch that flag when I update sector?
>
>
>
> #define MEMSTICK_OVERWRITE_BKST 0x80
> This marks bad blocks?
BKST set to zero indicates that the whole block is bad and shouldn't be
used.
PGST1:0 has several values:
11: default, r/w page
10: reserved value, shouldn't be used
01: page is read-only (soft write-protect)
00: page is accessible, but the value is not guaranteed (faulty page that
sort-of works)
That's what the spec says.
>
>
>
> Another question is about write of oob data.
> When I write it, overwrite flag is updated, or I need to
> use
> MEMSTICK_CP_OVERWRITE to update it?
> I think former is true.
As I mentioned above, it can be accessed either as part of extra data
or separately.
>
> When I write a sector, I just write 0 to management flag,
> right?
You shouldn't touch management_flag at all, as far as I can tell.
It's only used to indicate special purpose blocks, such as factory
written boot blocks, volatile look-up table blocks (for systems with
tight RAM requirements) and DRM marked blocks which I has no info about.
>
>
> And last question,
> If I use MEMSTICK_CP_BLOCK, can I start reading a block
> from non-zero
> page offset?
Yes, it starts from the user specified page address and auto increments it
until the current block end is hit.
>
>
> And surely last question, what is 'MS_CMD_BLOCK_END'
This command is used to terminate the currently ongoing block operation.
If you are using one of the auto-increment modes (with CP_BLOCK set) but
do not want to access all the pages until the block end, you must issue
this command after the desired number of pages is transferred to return
the media's state machine to the initial state. This command never hurts,
as you can guess.
>
>
> Thanks again for all help so far,
>
You're welcome.
^ permalink raw reply [flat|nested] 51+ messages in thread
* Re: [PATCH 2/2] MEMSTICK: Add driver for Ricoh R5C592 Card reader.
2010-08-08 14:26 ` Alex Dubov
@ 2010-08-08 15:07 ` Maxim Levitsky
2010-08-08 20:08 ` Maxim Levitsky
0 siblings, 1 reply; 51+ messages in thread
From: Maxim Levitsky @ 2010-08-08 15:07 UTC (permalink / raw)
To: Alex Dubov; +Cc: LKML
On Sun, 2010-08-08 at 07:26 -0700, Alex Dubov wrote:
> > Hi,
> >
> > I have few more questions about memsticks.
> >
> > First of all, I need an explanation of overwrite flag.
> > You already explained it to me once, but I still not sure
> > about few
> > things.
>
> Overwrite register can be accessed either as part of extra data access
> or separately (CP_OVERWRITE access mode).
>
> >
> > #define MEMSTICK_OVERWRITE_UDST 0x10
> > This one I understand, thinking about xD again, I think it
> > is very
> > handy.
> >
> > My idea (from xD of course) is that copyonwrite is done
> > this way:
> >
> > 1. read old sector
> > 2. allocate new sector
> > 2. write what was just read to new sector.
> > 3. erase old sector.
>
> This is correct.
>
> >
> > Could you explain when I need to set and reset the
> > MEMSTICK_OVERWRITE_UDST?
>
> UDST flag should be set when you're marking the block for
> reallocation during the read/modify/write cycle. You read the existing
> physical block, mark it with UDST flag (setting it to zero), then write
> different physical block on behalf of the same logical one, then erase the
> original block. The UDST flag is supposed to guard against a situation,
> whereupon power fails during the write cycle and you're left with two
> physical blocks mapped to the same logical one (so the one marked with
> zero UDST value is supposedly "known good").
>
>
> >
> >
> > #define MEMSTICK_OVERWRITE_PGST1 0x20
> > #define MEMSTICK_OVERWRITE_PGST0 0x40
> > I suppose these indicate that page(sector) contains
> > incorrect data, just
> > like in xD there is page status?
> > Again, better explanation is welcome.
> > Also, should I touch that flag when I update sector?
> >
> >
> >
> > #define MEMSTICK_OVERWRITE_BKST 0x80
> > This marks bad blocks?
>
> BKST set to zero indicates that the whole block is bad and shouldn't be
> used.
>
> PGST1:0 has several values:
> 11: default, r/w page
> 10: reserved value, shouldn't be used
> 01: page is read-only (soft write-protect)
> 00: page is accessible, but the value is not guaranteed (faulty page that
> sort-of works)
>
> That's what the spec says.
Thank you very much.
>
> >
> >
> >
> > Another question is about write of oob data.
> > When I write it, overwrite flag is updated, or I need to
> > use
> > MEMSTICK_CP_OVERWRITE to update it?
> > I think former is true.
>
> As I mentioned above, it can be accessed either as part of extra data
> or separately.
>
> >
> > When I write a sector, I just write 0 to management flag,
> > right?
>
> You shouldn't touch management_flag at all, as far as I can tell.
> It's only used to indicate special purpose blocks, such as factory
> written boot blocks, volatile look-up table blocks (for systems with
> tight RAM requirements) and DRM marked blocks which I has no info about.
>
> >
> >
> > And last question,
> > If I use MEMSTICK_CP_BLOCK, can I start reading a block
> > from non-zero
> > page offset?
>
> Yes, it starts from the user specified page address and auto increments it
> until the current block end is hit.
>
> >
> >
> > And surely last question, what is 'MS_CMD_BLOCK_END'
>
> This command is used to terminate the currently ongoing block operation.
> If you are using one of the auto-increment modes (with CP_BLOCK set) but
> do not want to access all the pages until the block end, you must issue
> this command after the desired number of pages is transferred to return
> the media's state machine to the initial state. This command never hurts,
> as you can guess.
That what I expected, thanks!
>
> >
> >
> > Thanks again for all help so far,
> >
>
> You're welcome.
Thank you very much!
Best regards,
Maxim Levitsky
^ permalink raw reply [flat|nested] 51+ messages in thread
* Re: [PATCH 2/2] MEMSTICK: Add driver for Ricoh R5C592 Card reader.
2010-08-08 15:07 ` Maxim Levitsky
@ 2010-08-08 20:08 ` Maxim Levitsky
2010-08-09 6:31 ` Alex Dubov
0 siblings, 1 reply; 51+ messages in thread
From: Maxim Levitsky @ 2010-08-08 20:08 UTC (permalink / raw)
To: Alex Dubov; +Cc: LKML
On Sun, 2010-08-08 at 18:07 +0300, Maxim Levitsky wrote:
> On Sun, 2010-08-08 at 07:26 -0700, Alex Dubov wrote:
> > > Hi,
> > >
> > > I have few more questions about memsticks.
> > >
> > > First of all, I need an explanation of overwrite flag.
> > > You already explained it to me once, but I still not sure
> > > about few
> > > things.
> >
> > Overwrite register can be accessed either as part of extra data access
> > or separately (CP_OVERWRITE access mode).
> >
Few more questions I gathered today.
I currently assume that if I set req->need_card_int, the driver will
wait till device raises MEMSTICK_INT_CED regardless of serial/parallel
mode. This bit is always available on serial line.
Is that OK to assume?
Another thing that I want to ask is about writes to overwrite/managment
flag.
Common sense tells me that I can write the flash many times, but write
can only clear bits. Therefore if I write 0xFF, this just means do
nothing.
Probably same applies to data content, but that isn't much of the use.
Thats why I see that default (good) value of bits in overwrite flag is
1.
This is correct I assume?
Another interesting thing I observed was that MEMSTICK_INT_ERR can mean
a correctable error, therefore it shouldn't alone reject read/write of
a sector. (I think that it means that one of
MEMSTICK_STATUS1_UCFG..MEMSTICK_STATUS1_DTER is set)
Same question about this.
Best regards,
Maxim Levitsky
^ permalink raw reply [flat|nested] 51+ messages in thread
* Re: [PATCH 2/2] MEMSTICK: Add driver for Ricoh R5C592 Card reader.
2010-08-08 20:08 ` Maxim Levitsky
@ 2010-08-09 6:31 ` Alex Dubov
2010-08-09 6:56 ` Maxim Levitsky
` (2 more replies)
0 siblings, 3 replies; 51+ messages in thread
From: Alex Dubov @ 2010-08-09 6:31 UTC (permalink / raw)
To: Maxim Levitsky; +Cc: LKML
>
> I currently assume that if I set req->need_card_int, the
> driver will
> wait till device raises MEMSTICK_INT_CED regardless of
> serial/parallel
> mode. This bit is always available on serial line.
> Is that OK to assume?
I'm not quite sure about this question.
Normally, when you wait for the media interrupt, you want to see the whole
value of the INT register (CED by itself doesn't indicate successful
command completion; in fact it's value is undefined in presence of other
INT flags, like BREQ or CMDNK). In parallel mode, host is required to
fetch all meaningful INT bits from the media bus, while in serial mode
this is only possible, if host supports automatic INT retrieval (the host
will issue GET_INT tpc behind the scenes before alerting the software).
>
> Another thing that I want to ask is about writes to
> overwrite/managment
> flag.
> Common sense tells me that I can write the flash many
> times, but write
> can only clear bits. Therefore if I write 0xFF, this just
> means do
> nothing.
> Probably same applies to data content, but that isn't much
> of the use.
Yes, all memsticks are NAND flash, so writing 1s has no effect whatsoever.
> Thats why I see that default (good) value of bits in
> overwrite flag is
> 1.
> This is correct I assume?
Yes, a direct consequence of the above.
>
>
> Another interesting thing I observed was that
> MEMSTICK_INT_ERR can mean
> a correctable error, therefore it shouldn't
> alone reject read/write of
> a sector. (I think that it means that one of
> MEMSTICK_STATUS1_UCFG..MEMSTICK_STATUS1_DTER is set)
> Same question about this.
>
There are three groups of error flags. While overwrite_flag register is
accessible as part of extra data, being the indicator of block goodness
it earned its own error flag:
DTER (UCDT) - error (uncorrectable) in data area
EXER (UCEX) - error (uncorrectable) in extra data area
FGER (UCFG) - error (uncorrectable) in overwrite_flag register
Uncorrectable error means that you've got some bit errors in the data
you've obtained from the media. If uncorrectable flag is not set, it
means that media's ECC circuit managed to correct the bit flip. The
desired way of action is, of course, to reallocate the block before it
develops an uncorrectable failure.
^ permalink raw reply [flat|nested] 51+ messages in thread
* Re: [PATCH 2/2] MEMSTICK: Add driver for Ricoh R5C592 Card reader.
2010-08-09 6:31 ` Alex Dubov
@ 2010-08-09 6:56 ` Maxim Levitsky
2010-08-09 15:30 ` Maxim Levitsky
2010-08-09 19:19 ` Maxim Levitsky
2 siblings, 0 replies; 51+ messages in thread
From: Maxim Levitsky @ 2010-08-09 6:56 UTC (permalink / raw)
To: Alex Dubov; +Cc: LKML
On Sun, 2010-08-08 at 23:31 -0700, Alex Dubov wrote:
> >
> > I currently assume that if I set req->need_card_int, the
> > driver will
> > wait till device raises MEMSTICK_INT_CED regardless of
> > serial/parallel
> > mode. This bit is always available on serial line.
> > Is that OK to assume?
>
> I'm not quite sure about this question.
> Normally, when you wait for the media interrupt, you want to see the whole
> value of the INT register (CED by itself doesn't indicate successful
> command completion; in fact it's value is undefined in presence of other
> INT flags, like BREQ or CMDNK). In parallel mode, host is required to
> fetch all meaningful INT bits from the media bus, while in serial mode
> this is only possible, if host supports automatic INT retrieval (the host
> will issue GET_INT tpc behind the scenes before alerting the software).
Ok, maybe I didn't explain myself correctly.
Device is in serial mode.
I set need_card_int
I send a command.
<here I assume that driver waits for CED bit, which is exposed on data
line, if CED won't be set, driver/hardware will timeout>
I send GET_INT _once_, look at flags. If I see CED, no NACK, then ok,
otherwise I send error result.
>
> >
> > Another thing that I want to ask is about writes to
> > overwrite/managment
> > flag.
> > Common sense tells me that I can write the flash many
> > times, but write
> > can only clear bits. Therefore if I write 0xFF, this just
> > means do
> > nothing.
> > Probably same applies to data content, but that isn't much
> > of the use.
>
> Yes, all memsticks are NAND flash, so writing 1s has no effect whatsoever.
>
> > Thats why I see that default (good) value of bits in
> > overwrite flag is
> > 1.
> > This is correct I assume?
>
> Yes, a direct consequence of the above.
I suspect that managment flag also has default value of 0xFF, and these
'special' features (drm, boot block, temporary table) clear bits out of
it.
>
> >
> >
> > Another interesting thing I observed was that
> > MEMSTICK_INT_ERR can mean
> > a correctable error, therefore it shouldn't
> > alone reject read/write of
> > a sector. (I think that it means that one of
> > MEMSTICK_STATUS1_UCFG..MEMSTICK_STATUS1_DTER is set)
> > Same question about this.
> >
>
> There are three groups of error flags. While overwrite_flag register is
> accessible as part of extra data, being the indicator of block goodness
> it earned its own error flag:
>
> DTER (UCDT) - error (uncorrectable) in data area
> EXER (UCEX) - error (uncorrectable) in extra data area
> FGER (UCFG) - error (uncorrectable) in overwrite_flag register
>
> Uncorrectable error means that you've got some bit errors in the data
> you've obtained from the media. If uncorrectable flag is not set, it
> means that media's ECC circuit managed to correct the bit flip. The
> desired way of action is, of course, to reallocate the block before it
> develops an uncorrectable failure.
That I understand clearly.
I ask what the meaning of MEMSTICK_INT_ERR is.
I expect it to be set if any of (un) correctable errors are found.
Therefore if MEMSTICK_INT_ERR, I can't report error instantly, but I
need to look at status1 to see if error is correctable or not.
This is correct?
Best regards,
Maxim Levitsky
^ permalink raw reply [flat|nested] 51+ messages in thread
* Re: [PATCH 2/2] MEMSTICK: Add driver for Ricoh R5C592 Card reader.
2010-08-09 6:31 ` Alex Dubov
2010-08-09 6:56 ` Maxim Levitsky
@ 2010-08-09 15:30 ` Maxim Levitsky
2010-08-10 8:12 ` Alex Dubov
2010-08-09 19:19 ` Maxim Levitsky
2 siblings, 1 reply; 51+ messages in thread
From: Maxim Levitsky @ 2010-08-09 15:30 UTC (permalink / raw)
To: Alex Dubov; +Cc: LKML
I have another question.
Looking at ms_block.c, I see that it sometimes changes register window.
This doesn't look good.
I see it does put the register window back, but still its a bit obscure.
I added tracking of current register window, so every time I send
MS_TPC_SET_RW_REG_ADRS I note the ranges.
And read/write functions now always attempt to send
MS_TPC_SET_RW_REG_ADRS. If the window is same as was set last time, TPC
is skipped.
However, I am thinking, that maybe I should always write both param and
extra register? I just write 0xFF to extra register and thats all.
Windows driver does that partially. It writes 0xFF to managmemt and
0xF8 to overwrite flag (why???), but doesn't touch the LBA. I don't
think that matters.
It also always sends the MS_TPC_SET_RW_REG_ADRS, which I don't like.
Best regards,
Maxim Levitsky
^ permalink raw reply [flat|nested] 51+ messages in thread
* Re: [PATCH 2/2] MEMSTICK: Add driver for Ricoh R5C592 Card reader.
2010-08-09 6:31 ` Alex Dubov
2010-08-09 6:56 ` Maxim Levitsky
2010-08-09 15:30 ` Maxim Levitsky
@ 2010-08-09 19:19 ` Maxim Levitsky
2010-08-10 7:53 ` Alex Dubov
2 siblings, 1 reply; 51+ messages in thread
From: Maxim Levitsky @ 2010-08-09 19:19 UTC (permalink / raw)
To: Alex Dubov; +Cc: LKML
About INT bits, I still don't understand them exactly.
First of all we have 4 meaningful bits.
In parallel mode these are exposed on data lines, in serial mode only
MEMSTICK_INT_CED is.
And I can always send MS_TPC_GET_INT or just read the registers.
#define MEMSTICK_INT_CMDNAK 0x01
#define MEMSTICK_INT_BREQ 0x20
#define MEMSTICK_INT_ERR 0x40
#define MEMSTICK_INT_CED 0x80
Now, I send a command to device, say MS_CMD_BLOCK_READ.
What bits I need to poll until I can be sure that command is completed?
Also the MEMSTICK_INT_BREQ tells that input is available in firmware
buffer (to read using TPC_READ_LONG_DATA)?
Is that true that MEMSTICK_INT_BREQ is a summary of fifo full/empty bits
in status0?
And same about MEMSTICK_INT_ERR and status1.
I try my best to create a driver that actually works, simple, and error
free, even in unusual conditions.
Thats why I am asking all these questions.
Thanks for help,
Best regards,
Maxim Levitsky
^ permalink raw reply [flat|nested] 51+ messages in thread
* Re: [PATCH 2/2] MEMSTICK: Add driver for Ricoh R5C592 Card reader.
2010-08-09 19:19 ` Maxim Levitsky
@ 2010-08-10 7:53 ` Alex Dubov
2010-08-10 9:50 ` Maxim Levitsky
0 siblings, 1 reply; 51+ messages in thread
From: Alex Dubov @ 2010-08-10 7:53 UTC (permalink / raw)
To: Maxim Levitsky; +Cc: LKML
> About INT bits, I still don't
> understand them exactly.
>
> First of all we have 4 meaningful bits.
> In parallel mode these are exposed on data lines, in serial
> mode only
> MEMSTICK_INT_CED is.
> And I can always send MS_TPC_GET_INT or just read the
> registers.
>
> #define MEMSTICK_INT_CMDNAK 0x01
This bit means command was not acknowledged by the media (media not ready).
> #define MEMSTICK_INT_BREQ 0x20
This bit means media is ready for long data transfer (either in or out).
> #define MEMSTICK_INT_ERR 0x40
This bit means that some error had occurred during command execution.
> #define MEMSTICK_INT_CED 0x80
This bit marks a completion of the command, but it may switch value in
between, so it is not that reliable by itself.
>
>
> Now, I send a command to device, say MS_CMD_BLOCK_READ.
> What bits I need to poll until I can be sure that command
> is completed?
All of them.
>
> Also the MEMSTICK_INT_BREQ tells that input is available in
> firmware
> buffer (to read using TPC_READ_LONG_DATA)?
>
Not exactly. BREQ signal indicated that media's state machine is in the
mode to pump data in or out. You wait for it, than you do the data
transfer (it works the same with READ and WRITE).
>
>
> Is that true that MEMSTICK_INT_BREQ is a summary of fifo
> full/empty bits
> in status0?
This can not be relied upon. Sony states, that only BREQ bit must be used
in block transfer operations. BE/BF bits are probably of more use in
memstick IO cards (there exists such a beast).
>
> And same about MEMSTICK_INT_ERR and status1.
status1 errors apply only to data errors (bit flips).
There are errors in command execution that do not result in bit flips,
rather data can not be delivered at all or command/parameters are invalid.
>
> I try my best to create a driver that actually works,
> simple, and error
> free, even in unusual conditions.
> Thats why I am asking all these questions.
>
> Thanks for help,
> Best regards,
> Maxim Levitsky
>
>
^ permalink raw reply [flat|nested] 51+ messages in thread
* Re: [PATCH 2/2] MEMSTICK: Add driver for Ricoh R5C592 Card reader.
2010-08-09 15:30 ` Maxim Levitsky
@ 2010-08-10 8:12 ` Alex Dubov
2010-08-10 9:47 ` Maxim Levitsky
0 siblings, 1 reply; 51+ messages in thread
From: Alex Dubov @ 2010-08-10 8:12 UTC (permalink / raw)
To: Maxim Levitsky; +Cc: LKML
> Received: Monday, 9 August, 2010, 8:30 AM
> I have another question.
>
> Looking at ms_block.c, I see that it sometimes changes
> register window.
> This doesn't look good.
> I see it does put the register window back, but still its a
> bit obscure.
It looks very good, in fact, it is the Sony specified way to operate
the media. MS Pro works quite the same, it just needs fewer operations
to actually access data.
>
> I added tracking of current register window, so every time
> I send
> MS_TPC_SET_RW_REG_ADRS I note the ranges.
> And read/write functions now always attempt to send
> MS_TPC_SET_RW_REG_ADRS. If the window is same as was
> set last time, TPC
> is skipped.
Sure it is. The media will remember the window set.
Media has all its registers in a sort of flat file. SET_RW_REG_ADDR
selects the subset of the registers that will receive the data delivered
within TPC. This subset is remembered until power off or until changed.
>
> However, I am thinking, that maybe I should always write
> both param and
> extra register? I just write 0xFF to extra register and
> thats all.
You should write into a param register when you want to alter the command
parameters. You cannot do so during auto incrementing block access, for
example.
But, if you're using the auto incrementing write, you will have to write
extra register for every page transferred.
That's where changing RW_REG_ADDR comes handy.
> Windows driver does that partially. It writes 0xFF to
> managmemt and
> 0xF8 to overwrite flag (why???)
It's a factory default.
Try to read it from some empty block. :-)
(My theory is that missing bits contain invisible ECC data).
> I don't
> think that matters.
> It also always sends the MS_TPC_SET_RW_REG_ADRS, which I
> don't like.
>
This only reduces the performance slightly. SET_RW_REG_ADDR does not
influence the media's state machine as far as I can tell, unless you try
to push it during the data transfer cycle (whereupon you will end up
having a literal value of the tpc in the media data buffer).
^ permalink raw reply [flat|nested] 51+ messages in thread
* Re: [PATCH 2/2] MEMSTICK: Add driver for Ricoh R5C592 Card reader.
2010-08-10 8:12 ` Alex Dubov
@ 2010-08-10 9:47 ` Maxim Levitsky
2010-08-11 8:08 ` Alex Dubov
0 siblings, 1 reply; 51+ messages in thread
From: Maxim Levitsky @ 2010-08-10 9:47 UTC (permalink / raw)
To: Alex Dubov; +Cc: LKML
On Tue, 2010-08-10 at 01:12 -0700, Alex Dubov wrote:
> > Received: Monday, 9 August, 2010, 8:30 AM
> > I have another question.
> >
> > Looking at ms_block.c, I see that it sometimes changes
> > register window.
> > This doesn't look good.
> > I see it does put the register window back, but still its a
> > bit obscure.
>
> It looks very good, in fact, it is the Sony specified way to operate
> the media. MS Pro works quite the same, it just needs fewer operations
> to actually access data.
>
> >
> > I added tracking of current register window, so every time
> > I send
> > MS_TPC_SET_RW_REG_ADRS I note the ranges.
> > And read/write functions now always attempt to send
> > MS_TPC_SET_RW_REG_ADRS. If the window is same as was
> > set last time, TPC
> > is skipped.
>
> Sure it is. The media will remember the window set.
> Media has all its registers in a sort of flat file. SET_RW_REG_ADDR
> selects the subset of the registers that will receive the data delivered
> within TPC. This subset is remembered until power off or until changed.
I know everything you have just said.
I just want to point out that code in many places assumes that register
window is the same as set on device initialization.
However some code changes the register window, and therefore has to
change it back at the end of execution.
If error happens, window can be left changed, while rest of code thinks
it isn't changed.
Thats is why a tracking of it would eliminate the problem safely.
>
>
> >
> > However, I am thinking, that maybe I should always write
> > both param and
> > extra register? I just write 0xFF to extra register and
> > thats all.
>
> You should write into a param register when you want to alter the command
> parameters. You cannot do so during auto incrementing block access, for
> example.
>
> But, if you're using the auto incrementing write, you will have to write
> extra register for every page transferred.
But what if I fill extra register with 0xFF?
And besides on reads, the fact that I *write* the extra register before
I execute read command shouldn't matter at all regardless of what I
write there.
On writes however I *do* need to write extra register anyway with proper
values.
Therefore I see no reason why I can't set write window to cover both
param and extra register, and leave it always like that.
>
> That's where changing RW_REG_ADDR comes handy.
>
> > Windows driver does that partially. It writes 0xFF to
> > managmemt and
> > 0xF8 to overwrite flag (why???)
>
> It's a factory default.
> Try to read it from some empty block. :-)
> (My theory is that missing bits contain invisible ECC data).
>
>
> > I don't
> > think that matters.
> > It also always sends the MS_TPC_SET_RW_REG_ADRS, which I
> > don't like.
> >
>
> This only reduces the performance slightly. SET_RW_REG_ADDR does not
> influence the media's state machine as far as I can tell, unless you try
> to push it during the data transfer cycle (whereupon you will end up
> having a literal value of the tpc in the media data buffer).
Indeed. Maybe I too should just send the MS_TPC_SET_RW_REG_ADRS at start
of command, and know that nothing will go south....
Even if that reduces performance by 0.2%, it isn't big deal. Data
corruptions is very big deal.
Best regards,
Maxim Levitsky
^ permalink raw reply [flat|nested] 51+ messages in thread
* Re: [PATCH 2/2] MEMSTICK: Add driver for Ricoh R5C592 Card reader.
2010-08-10 7:53 ` Alex Dubov
@ 2010-08-10 9:50 ` Maxim Levitsky
2010-08-11 8:16 ` Alex Dubov
0 siblings, 1 reply; 51+ messages in thread
From: Maxim Levitsky @ 2010-08-10 9:50 UTC (permalink / raw)
To: Alex Dubov; +Cc: LKML
On Tue, 2010-08-10 at 00:53 -0700, Alex Dubov wrote:
> > About INT bits, I still don't
> > understand them exactly.
> >
> > First of all we have 4 meaningful bits.
> > In parallel mode these are exposed on data lines, in serial
> > mode only
> > MEMSTICK_INT_CED is.
> > And I can always send MS_TPC_GET_INT or just read the
> > registers.
> >
> > #define MEMSTICK_INT_CMDNAK 0x01
>
> This bit means command was not acknowledged by the media (media not ready).
>
> > #define MEMSTICK_INT_BREQ 0x20
>
> This bit means media is ready for long data transfer (either in or out).
>
> > #define MEMSTICK_INT_ERR 0x40
>
> This bit means that some error had occurred during command execution.
>
> > #define MEMSTICK_INT_CED 0x80
>
> This bit marks a completion of the command, but it may switch value in
> between, so it is not that reliable by itself.
>
> >
> >
> > Now, I send a command to device, say MS_CMD_BLOCK_READ.
> > What bits I need to poll until I can be sure that command
> > is completed?
>
> All of them.
>
> >
> > Also the MEMSTICK_INT_BREQ tells that input is available in
> > firmware
> > buffer (to read using TPC_READ_LONG_DATA)?
> >
>
> Not exactly. BREQ signal indicated that media's state machine is in the
> mode to pump data in or out. You wait for it, than you do the data
> transfer (it works the same with READ and WRITE).
>
> >
> >
> > Is that true that MEMSTICK_INT_BREQ is a summary of fifo
> > full/empty bits
> > in status0?
>
> This can not be relied upon. Sony states, that only BREQ bit must be used
> in block transfer operations. BE/BF bits are probably of more use in
> memstick IO cards (there exists such a beast).
Even better.
>
> >
> > And same about MEMSTICK_INT_ERR and status1.
>
> status1 errors apply only to data errors (bit flips).
> There are errors in command execution that do not result in bit flips,
> rather data can not be delivered at all or command/parameters are invalid.
Understood.
However if I get MEMSTICK_INT_ERR, I should also look at status1,
because there might be correctable error.
Thank you very much.
Best regards,
Maxim Levitsky
^ permalink raw reply [flat|nested] 51+ messages in thread
* Re: [PATCH 2/2] MEMSTICK: Add driver for Ricoh R5C592 Card reader.
2010-08-10 9:47 ` Maxim Levitsky
@ 2010-08-11 8:08 ` Alex Dubov
2010-08-11 8:32 ` Maxim Levitsky
0 siblings, 1 reply; 51+ messages in thread
From: Alex Dubov @ 2010-08-11 8:08 UTC (permalink / raw)
To: Maxim Levitsky; +Cc: LKML
> I know everything you have just said.
> I just want to point out that code in many places assumes
> that register
> window is the same as set on device initialization.
It's not.
Can you please point at a particular place?
> > But, if you're using the auto incrementing write, you
> will have to write
> > extra register for every page transferred.
> But what if I fill extra register with 0xFF?
> And besides on reads, the fact that I *write* the extra
> register before
> I execute read command shouldn't matter at all regardless
> of what I
> write there.
> On writes however I *do* need to write extra register
> anyway with proper
> values.
>
> Therefore I see no reason why I can't set write window to
> cover both
> param and extra register, and leave it always like that.
Because when you do autoincrementing write you _can not_ write into param register. You will break the command execution.
On the other thought, it may be unnecessary to write unique extra data to every page, so one full register write at the beginning of the command may do. Considering, that legacy memstick is not going to evolve, this may be reasonable assumption.
^ permalink raw reply [flat|nested] 51+ messages in thread
* Re: [PATCH 2/2] MEMSTICK: Add driver for Ricoh R5C592 Card reader.
2010-08-10 9:50 ` Maxim Levitsky
@ 2010-08-11 8:16 ` Alex Dubov
0 siblings, 0 replies; 51+ messages in thread
From: Alex Dubov @ 2010-08-11 8:16 UTC (permalink / raw)
To: Maxim Levitsky; +Cc: LKML
> result in bit flips,
> > rather data can not be delivered at all or
> command/parameters are invalid.
> Understood.
> However if I get MEMSTICK_INT_ERR, I should also look at
> status1,
> because there might be correctable error.
>
>
I don't remember the details, but it may be, that status1 errors do not
raise INT_ERR (or do not always raise?). The idea is, first you check
INT_ERR. If command completed successfully and you received the data, you
may still need to check status1 for possible bit flips.
Some experimentation may be required, especially for the case of
correctable errors. After all, they should not prevent the command from
successful completion, yet action should be taken on them (at the very
least - log warning, just like with normal disks).
^ permalink raw reply [flat|nested] 51+ messages in thread
* Re: [PATCH 2/2] MEMSTICK: Add driver for Ricoh R5C592 Card reader.
2010-08-11 8:08 ` Alex Dubov
@ 2010-08-11 8:32 ` Maxim Levitsky
2010-08-12 7:22 ` Alex Dubov
0 siblings, 1 reply; 51+ messages in thread
From: Maxim Levitsky @ 2010-08-11 8:32 UTC (permalink / raw)
To: Alex Dubov; +Cc: LKML
On Wed, 2010-08-11 at 01:08 -0700, Alex Dubov wrote:
> > I know everything you have just said.
> > I just want to point out that code in many places assumes
> > that register
> > window is the same as set on device initialization.
>
> It's not.
> Can you please point at a particular place?
>
> > > But, if you're using the auto incrementing write, you
> > will have to write
> > > extra register for every page transferred.
> > But what if I fill extra register with 0xFF?
> > And besides on reads, the fact that I *write* the extra
> > register before
> > I execute read command shouldn't matter at all regardless
> > of what I
> > write there.
> > On writes however I *do* need to write extra register
> > anyway with proper
> > values.
> >
> > Therefore I see no reason why I can't set write window to
> > cover both
> > param and extra register, and leave it always like that.
>
> Because when you do autoincrementing write you _can not_ write into param register. You will break the command execution.
Thanks, I finally understand you, so you are objection to write of
_param_ register, not the _extra_.
I agree with you.
>
> On the other thought, it may be unnecessary to write unique extra data to every page, so one full register write at the beginning of the command may do. Considering, that legacy memstick is not going to evolve, this may be reasonable assumption.
Indeed that what I do now.
Maximum I might need to clear page status bits, but I can do that later
after I write the block.
This won't be any performance impact because amount of bad pages
shouldn't be normally greater that zero.
(Otherwise there will be data loss...)
One interesting thing that I just want your opinion on is what to do
with correctable errors.
Common sense suggests to relocate the sector + and mark it bad.
But I don't know how common such sectors are, and thus I could do more
harm that good by marking too many sectors as bad.
Of course all such problems are reason why today flash devices contain
the FTL inside, and can improve/define it in the way they want.
No more questions for now, thank you very much for help.
I hope I create ms_block.c soon, and put that old problem to the rest.
As time permits I will also port your driver for xD portion of jMicron
device (which I have).
Best regards,
Maxim Levitsky
^ permalink raw reply [flat|nested] 51+ messages in thread
* Re: [PATCH 2/2] MEMSTICK: Add driver for Ricoh R5C592 Card reader.
2010-08-11 8:32 ` Maxim Levitsky
@ 2010-08-12 7:22 ` Alex Dubov
2010-08-12 7:58 ` Maxim Levitsky
0 siblings, 1 reply; 51+ messages in thread
From: Alex Dubov @ 2010-08-12 7:22 UTC (permalink / raw)
To: Maxim Levitsky; +Cc: LKML
> Maximum I might need to clear page status bits, but I can
> do that later
> after I write the block.
> This won't be any performance impact because amount of bad
> pages
> shouldn't be normally greater that zero.
> (Otherwise there will be data loss...)
These things do degrade. I think, memsticks do write-verify, so bad blocks
will appear during write and can be marked as such without any data loss.
>
> One interesting thing that I just want your opinion on is
> what to do
> with correctable errors.
> Common sense suggests to relocate the sector + and mark it
> bad.
> But I don't know how common such sectors are, and thus I
> could do more
> harm that good by marking too many sectors as bad.
I agree that number of writes to the media should be kept minimal.
So bad (in either way) blocks encountered should be logged, but not
touched, unless the error appears during an actual write/modify operation.
>
> I hope I create ms_block.c soon, and put that old problem
> to the rest.
If you have time and desire, try to put a low level format option in -
some function to erase all blocks, except the system ones.
>
> As time permits I will also port your driver for xD portion
> of jMicron
> device (which I have).
By the way, I've got some errata for the newer JMicron chipsets. If they
have not contacted you yet, I'll forward it to you.
^ permalink raw reply [flat|nested] 51+ messages in thread
* Re: [PATCH 2/2] MEMSTICK: Add driver for Ricoh R5C592 Card reader.
2010-08-12 7:22 ` Alex Dubov
@ 2010-08-12 7:58 ` Maxim Levitsky
0 siblings, 0 replies; 51+ messages in thread
From: Maxim Levitsky @ 2010-08-12 7:58 UTC (permalink / raw)
To: Alex Dubov; +Cc: LKML
On Thu, 2010-08-12 at 00:22 -0700, Alex Dubov wrote:
> > Maximum I might need to clear page status bits, but I can
> > do that later
> > after I write the block.
> > This won't be any performance impact because amount of bad
> > pages
> > shouldn't be normally greater that zero.
> > (Otherwise there will be data loss...)
>
>
> These things do degrade. I think, memsticks do write-verify, so bad blocks
> will appear during write and can be marked as such without any data loss.
>
> >
> > One interesting thing that I just want your opinion on is
> > what to do
> > with correctable errors.
> > Common sense suggests to relocate the sector + and mark it
> > bad.
> > But I don't know how common such sectors are, and thus I
> > could do more
> > harm that good by marking too many sectors as bad.
>
> I agree that number of writes to the media should be kept minimal.
> So bad (in either way) blocks encountered should be logged, but not
> touched, unless the error appears during an actual write/modify operation.
>
> >
> > I hope I create ms_block.c soon, and put that old problem
> > to the rest.
>
> If you have time and desire, try to put a low level format option in -
> some function to erase all blocks, except the system ones.
>
> >
> > As time permits I will also port your driver for xD portion
> > of jMicron
> > device (which I have).
>
> By the way, I've got some errata for the newer JMicron chipsets. If they
> have not contacted you yet, I'll forward it to you.
Yeah, I didn't done anything at that direction, but eventually I get to
it.
Best regards,
Maxim Levitsky
^ permalink raw reply [flat|nested] 51+ messages in thread
* MEMSTICK: Add my 2 drivers
@ 2010-12-09 2:39 Maxim Levitsky
2010-12-09 2:42 ` [PATCH 1/2] memstick: add support for legacy memorysticks Maxim Levitsky
` (2 more replies)
0 siblings, 3 replies; 51+ messages in thread
From: Maxim Levitsky @ 2010-12-09 2:39 UTC (permalink / raw)
To: linux-kernel; +Cc: Alex Dubov, Andrew Morton, Takashi Iwai
Hi,
This is standalone version of two drivers that I wrote for memstick
subsystem.
Due to attitude of Alex, I decided to avoid contributing to memstick
subsystem anymore unless I run into another unsupported chip, or
something like that, or any bugs in my code will be found.
Of course I will do my best to fix such bugs as fast as possible.
And I really care to have as little as possible bugs in my code.
Anderew Morton, older version of these drivers were in your tree for 2
cycles now, could you please set them for 2.6.38 inclusion?
Since I don't touch (and don't want anymore to) any code written by
Alex, and I even added workarounds for bugs in his jmicron driver to the
generic ms_block.c code (it tested and works on my both card readers),
maybe even add these to 2.6.37?
And as a last comment about flamewar with Alex, which I don't want to
repeat, I think he ought to add himself as a maintainer of memstick code
he wrote, because it seems absurd that he refuses my contributions and
yet nether maintains it (code has few bugs that are result of bitrot, I
even have a merged commit for biggest one) nor he even appears in
MAINTAINERS for mspro_blk.c and memstick.c.
Sorry for my attitude, but its frustrating to see a month's work aimed
to make the code readable to be lost.
Best regards,
Maxim Levitsky
^ permalink raw reply [flat|nested] 51+ messages in thread
* [PATCH 1/2] memstick: add support for legacy memorysticks
2010-12-09 2:39 MEMSTICK: Add my 2 drivers Maxim Levitsky
@ 2010-12-09 2:42 ` Maxim Levitsky
2010-12-09 7:19 ` Takashi Iwai
2010-12-09 21:09 ` Andrew Morton
2010-12-09 2:42 ` [PATCH 2/2] memstick: Add driver for Ricoh R5C592 card reader Maxim Levitsky
2010-12-09 2:44 ` MEMSTICK: Add my 2 drivers Alex Dubov
2 siblings, 2 replies; 51+ messages in thread
From: Maxim Levitsky @ 2010-12-09 2:42 UTC (permalink / raw)
To: linux-kernel; +Cc: Alex Dubov, Andrew Morton, Takashi Iwai, Maxim Levitsky
Based partialy on spec quotes from Alex Dubov.
As any code that works with user data this driver isn't recommened to use
with valuable data.
It tries its best though to avoid data corruption and possible damage to
the card.
Tested with MS DUO 64 MB card on Ricoh and Jmicron reader.
Signed-off-by: Maxim Levitsky <maximlevitsky@gmail.com>
---
MAINTAINERS | 5 +
drivers/memstick/core/Kconfig | 12 +
drivers/memstick/core/Makefile | 2 +-
drivers/memstick/core/ms_block.c | 2385 ++++++++++++++++++++++++++++++++++++++
drivers/memstick/core/ms_block.h | 245 ++++
5 files changed, 2648 insertions(+), 1 deletions(-)
create mode 100644 drivers/memstick/core/ms_block.c
create mode 100644 drivers/memstick/core/ms_block.h
diff --git a/MAINTAINERS b/MAINTAINERS
index b3be8b3..a441ce4 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -5545,6 +5545,11 @@ W: http://tifmxx.berlios.de/
S: Maintained
F: drivers/memstick/host/tifm_ms.c
+SONY MEMORYSTICK STANDARD SUPPORT
+M: Maxim Levitsky <maximlevitsky@gmail.com>
+S: Maintained
+F: drivers/memstick/core/ms_block.*
+
SOUND
M: Jaroslav Kysela <perex@perex.cz>
M: Takashi Iwai <tiwai@suse.de>
diff --git a/drivers/memstick/core/Kconfig b/drivers/memstick/core/Kconfig
index 95f1814..f79f2a8 100644
--- a/drivers/memstick/core/Kconfig
+++ b/drivers/memstick/core/Kconfig
@@ -24,3 +24,15 @@ config MSPRO_BLOCK
support. This provides a block device driver, which you can use
to mount the filesystem. Almost everyone wishing MemoryStick
support should say Y or M here.
+
+config MS_BLOCK
+ tristate "MemoryStick Standard device driver"
+ depends on BLOCK && EXPERIMENTAL
+ help
+ Say Y here to enable the MemoryStick Standard device driver
+ support. This provides a block device driver, which you can use
+ to mount the filesystem.
+ This driver works with old (bulky) MemoryStick and MemoryStick Duo
+ but not PRO. Say Y if you have such card.
+ Driver is new and not yet well tested, thus it can damage your card
+ (even permanently)
diff --git a/drivers/memstick/core/Makefile b/drivers/memstick/core/Makefile
index 8b2b529..19d960b 100644
--- a/drivers/memstick/core/Makefile
+++ b/drivers/memstick/core/Makefile
@@ -7,5 +7,5 @@ ifeq ($(CONFIG_MEMSTICK_DEBUG),y)
endif
obj-$(CONFIG_MEMSTICK) += memstick.o
-
+obj-$(CONFIG_MS_BLOCK) += ms_block.o
obj-$(CONFIG_MSPRO_BLOCK) += mspro_block.o
diff --git a/drivers/memstick/core/ms_block.c b/drivers/memstick/core/ms_block.c
new file mode 100644
index 0000000..5933421
--- /dev/null
+++ b/drivers/memstick/core/ms_block.c
@@ -0,0 +1,2385 @@
+/*
+ * ms_block.c - Sony MemoryStick (legacy) storage support
+
+ * Copyright (C) 2010 Maxim Levitsky <maximlevitsky@gmail.com>
+ *
+ * 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.
+ *
+ * Minor portions of the driver were copied from mspro_block.c which is
+ * Copyright (C) 2007 Alex Dubov <oakad@yahoo.com>
+ *
+ */
+
+#include <linux/blkdev.h>
+#include <linux/mm.h>
+#include <linux/idr.h>
+#include <linux/hdreg.h>
+#include <linux/kthread.h>
+#include <linux/delay.h>
+#include <linux/slab.h>
+#include <linux/random.h>
+#include <linux/memstick.h>
+#include <linux/bitmap.h>
+#include <linux/scatterlist.h>
+#include <linux/sched.h>
+#include <linux/jiffies.h>
+#include "ms_block.h"
+
+static int major;
+static int debug;
+static int cache_flush_timeout = 1000;
+static bool verify_writes;
+
+
+/*
+ * Advance scatterlist by 'consumed' bytes
+ * Returns new scatterlist, or NULL if can't advance that much
+ */
+static struct scatterlist *sg_advance(struct scatterlist *sg, int consumed)
+{
+ while (consumed >= sg->length) {
+ consumed -= sg->length;
+
+ sg = sg_next(sg);
+ if (!sg)
+ break;
+ }
+
+ WARN_ON(!sg && consumed);
+
+ if (!sg)
+ return NULL;
+
+ sg->offset += consumed;
+ sg->length -= consumed;
+
+ if (sg->offset >= PAGE_SIZE) {
+ struct page *page =
+ nth_page(sg_page(sg), sg->offset / PAGE_SIZE);
+ sg_set_page(sg, page, sg->length, sg->offset % PAGE_SIZE);
+ }
+
+ return sg;
+}
+
+/* Calculate number of sg entries in sg list */
+static int sg_nents(struct scatterlist *sg)
+{
+ int nents = 0;
+ while (sg) {
+ nents++;
+ sg = sg_next(sg);
+ }
+
+ return nents;
+}
+
+/* Calculate total lenght of scatterlist */
+static int sg_total_len(struct scatterlist *sg)
+{
+ int len = 0;
+ while (sg) {
+ len += sg->length;
+ sg = sg_next(sg);
+ }
+ return len;
+}
+
+/* Compare contents of an sg to a buffer */
+static bool sg_compare_to_buffer(struct scatterlist *sg, u8 *buffer, size_t len)
+{
+ unsigned long flags;
+ int retval = 0;
+ struct sg_mapping_iter miter;
+
+ if (sg_total_len(sg) < len)
+ return 1;
+
+ local_irq_save(flags);
+ sg_miter_start(&miter, sg, sg_nents(sg),
+ SG_MITER_ATOMIC | SG_MITER_FROM_SG);
+
+ while (sg_miter_next(&miter) && len > 0) {
+
+ int cmplen = min(miter.length, len);
+ if (memcmp(miter.addr, buffer, cmplen)) {
+ retval = 1;
+ break;
+ }
+
+ buffer += cmplen;
+ len -= cmplen;
+ }
+
+ sg_miter_stop(&miter);
+ local_irq_restore(flags);
+ return retval;
+}
+
+
+/* Get zone at which block with logical address 'lba' lives
+ * Flash is broken into zones.
+ * Each zone consists of 512 eraseblocks, out of which in first
+ * zone 494 are used and 496 are for all following zones.
+ * Therefore zone #0 hosts blocks 0-493, zone #1 blocks 494-988, etc...
+*/
+static int msb_get_zone_from_lba(int lba)
+{
+ if (lba < 494)
+ return 0;
+ return ((lba - 494) / 496) + 1;
+}
+
+/* Get zone of physical block. Trivial */
+static int msb_get_zone_from_pba(int pba)
+{
+ return pba / MS_BLOCKS_IN_ZONE;
+}
+
+/* Debug test to validate free block counts */
+#ifdef DEBUG
+static int msb_validate_used_block_bitmap(struct msb_data *msb)
+{
+ int total_free_blocks = 0;
+ int i;
+
+ for (i = 0 ; i < msb->zone_count ; i++)
+ total_free_blocks += msb->free_block_count[i];
+
+ if (msb->block_count - bitmap_weight(msb->used_blocks_bitmap,
+ msb->block_count) == total_free_blocks)
+ return 0;
+
+ ms_printk("BUG: free block counts don't match the bitmap");
+ msb->read_only = true;
+ return -EINVAL;
+}
+#endif
+
+/* Mark physical block as used */
+static void msb_mark_block_used(struct msb_data *msb, int pba)
+{
+ int zone = msb_get_zone_from_pba(pba);
+
+ if (test_bit(pba, msb->used_blocks_bitmap)) {
+ ms_printk("BUG: attempt to mark "
+ "already used pba %d as used", pba);
+ msb->read_only = true;
+ return;
+ }
+
+#ifdef DEBUG
+ if (msb_validate_used_block_bitmap(msb))
+ return;
+#endif
+ set_bit(pba, msb->used_blocks_bitmap);
+ msb->free_block_count[zone]--;
+}
+
+/* Mark physical block as free */
+static void msb_mark_block_unused(struct msb_data *msb, int pba)
+{
+ int zone = msb_get_zone_from_pba(pba);
+
+ if (!test_bit(pba, msb->used_blocks_bitmap)) {
+ ms_printk("BUG: attempt to mark "
+ "already unused pba %d as unused" , pba);
+ msb->read_only = true;
+ return;
+ }
+
+#ifdef DEBUG
+ if (msb_validate_used_block_bitmap(msb))
+ return;
+#endif
+ clear_bit(pba, msb->used_blocks_bitmap);
+ msb->free_block_count[zone]++;
+}
+
+/*
+ * Create a sg that spans page from current scatterlist
+ * used by read/write functions
+ */
+static void msb_set_sg(struct msb_data *msb, struct scatterlist *sg)
+{
+ int offset = msb->current_sg->offset + msb->sg_offset;
+ struct page *page = nth_page(sg_page(msb->current_sg),
+ offset >> PAGE_SHIFT);
+ sg_init_table(sg, 1);
+ sg_set_page(sg, page, msb->page_size, offset_in_page(offset));
+}
+
+/* Advances the current sg by one page. Returns error if can't */
+static int msb_advance_sg(struct msb_data *msb)
+{
+ msb->sg_offset += msb->page_size;
+
+ if (msb->sg_offset & (msb->page_size - 1)) {
+ ms_printk("BUG: sg not aligned");
+ return -EINVAL;
+ }
+
+ if (msb->sg_offset > msb->current_sg->length) {
+ dbg("BUG: sg overrun");
+ return -EINVAL;
+ }
+
+ if (msb->sg_offset == msb->current_sg->length) {
+ msb->current_sg = sg_next(msb->current_sg);
+ msb->sg_offset = 0;
+ }
+
+ if (!msb->current_sg)
+ return -EINVAL;
+ return 0;
+}
+
+/* Invalidate current register window (QA passed)*/
+static void msb_invalidate_reg_window(struct msb_data *msb)
+{
+ msb->reg_addr.w_offset = offsetof(struct ms_register, id);
+ msb->reg_addr.w_length = sizeof(struct ms_id_register);
+ msb->reg_addr.r_offset = offsetof(struct ms_register, id);
+ msb->reg_addr.r_length = sizeof(struct ms_id_register);
+ msb->addr_valid = false;
+}
+
+/* Sane way to start a state machine (QA passed)*/
+static int msb_run_state_machine(struct msb_data *msb, int (*state_func)
+ (struct memstick_dev *card, struct memstick_request **req))
+{
+ struct memstick_dev *card = msb->card;
+
+ WARN_ON(msb->state != -1);
+ msb->int_polling = false;
+ msb->state = 0;
+ msb->exit_error = 0;
+
+ memset(&card->current_mrq, 0, sizeof(card->current_mrq));
+
+ card->next_request = state_func;
+ memstick_new_req(card->host);
+ wait_for_completion(&card->mrq_complete);
+
+ WARN_ON(msb->state != -1);
+ return msb->exit_error;
+}
+
+/* State machines call that to exit (QA passed) */
+int msb_exit_state_machine(struct msb_data *msb, int error)
+{
+ WARN_ON(msb->state == -1);
+
+ msb->state = -1;
+ msb->exit_error = error;
+ msb->card->next_request = h_msb_default_bad;
+
+ /* Invalidate reg window on errors */
+ if (error)
+ msb_invalidate_reg_window(msb);
+
+ complete(&msb->card->mrq_complete);
+ return -ENXIO;
+}
+
+/* read INT register (QA passed) */
+int msb_read_int_reg(struct msb_data *msb, long timeout)
+{
+ struct memstick_request *mrq = &msb->card->current_mrq;
+ WARN_ON(msb->state == -1);
+
+ if (!msb->int_polling) {
+ msb->int_timeout = jiffies +
+ msecs_to_jiffies(timeout == -1 ? 500 : timeout);
+ msb->int_polling = true;
+ } else if (time_after(jiffies, msb->int_timeout)) {
+ mrq->data[0] = MEMSTICK_INT_CMDNAK;
+ return 0;
+ }
+
+ if ((msb->caps & MEMSTICK_CAP_AUTO_GET_INT) &&
+ mrq->need_card_int && !mrq->error) {
+ mrq->data[0] = mrq->int_reg;
+ mrq->need_card_int = false;
+ return 0;
+ } else {
+ memstick_init_req(mrq, MS_TPC_GET_INT, NULL, 1);
+ return 1;
+ }
+}
+
+/* Read a register (QA passed) */
+int msb_read_regs(struct msb_data *msb, int offset, int len)
+{
+ struct memstick_request *req = &msb->card->current_mrq;
+
+ if (msb->reg_addr.r_offset != offset ||
+ msb->reg_addr.r_length != len || !msb->addr_valid) {
+
+ msb->reg_addr.r_offset = offset;
+ msb->reg_addr.r_length = len;
+ msb->addr_valid = true;
+
+ memstick_init_req(req, MS_TPC_SET_RW_REG_ADRS,
+ &msb->reg_addr, sizeof(msb->reg_addr));
+ return 0;
+ }
+
+ memstick_init_req(req, MS_TPC_READ_REG, NULL, len);
+ return 1;
+}
+
+/* Write a card register */
+int msb_write_regs(struct msb_data *msb, int offset, int len, void *buf)
+{
+ struct memstick_request *req = &msb->card->current_mrq;
+
+ if (msb->reg_addr.w_offset != offset ||
+ msb->reg_addr.w_length != len || !msb->addr_valid) {
+
+ msb->reg_addr.w_offset = offset;
+ msb->reg_addr.w_length = len;
+ msb->addr_valid = true;
+
+ memstick_init_req(req, MS_TPC_SET_RW_REG_ADRS,
+ &msb->reg_addr, sizeof(msb->reg_addr));
+ return 0;
+ }
+
+ memstick_init_req(req, MS_TPC_WRITE_REG, buf, len);
+ return 1;
+}
+
+/* Handler for absense of IO */
+static int h_msb_default_bad(struct memstick_dev *card,
+ struct memstick_request **mrq)
+{
+ return -ENXIO;
+}
+
+/*
+ * This function is a handler for reads of one page from device.
+ * Writes output to msb->current_sg, takes sector address from msb->reg.param
+ * Can also be used to read extra data only. Set params accordintly.
+ */
+static int h_msb_read_page(struct memstick_dev *card,
+ struct memstick_request **out_mrq)
+{
+ struct msb_data *msb = memstick_get_drvdata(card);
+ struct memstick_request *mrq = *out_mrq = &card->current_mrq;
+ struct scatterlist sg;
+ u8 command, intreg;
+
+ if (mrq->error) {
+ dbg("read_page, unknown error");
+ return msb_exit_state_machine(msb, mrq->error);
+ }
+again:
+ switch (msb->state) {
+ case 0: /* Write the sector address */
+ if (!msb_write_regs(msb,
+ offsetof(struct ms_register, param),
+ sizeof(struct ms_param_register),
+ (unsigned char *)&msb->regs.param))
+ return 0;
+ break;
+
+ case 1: /* Execute the read command*/
+ command = MS_CMD_BLOCK_READ;
+ memstick_init_req(mrq, MS_TPC_SET_CMD, &command, 1);
+ break;
+
+ case 2: /* send INT request */
+ if (msb_read_int_reg(msb, -1))
+ break;
+ msb->state++;
+
+ case 3: /* get result of the INT request*/
+ intreg = mrq->data[0];
+ msb->regs.status.interrupt = intreg;
+
+ if (intreg & MEMSTICK_INT_CMDNAK)
+ return msb_exit_state_machine(msb, -EIO);
+
+ if (!(intreg & MEMSTICK_INT_CED)) {
+ msb->state--;
+ goto again;
+ }
+
+ msb->int_polling = false;
+
+ if (intreg & MEMSTICK_INT_ERR)
+ msb->state++;
+ else
+ msb->state = 6;
+
+ goto again;
+
+ case 4: /* read the status register
+ to understand source of the INT_ERR */
+ if (!msb_read_regs(msb,
+ offsetof(struct ms_register, status),
+ sizeof(struct ms_status_register)))
+ return 0;
+ break;
+
+ case 5: /* get results of status check */
+ msb->regs.status = *(struct ms_status_register *)mrq->data;
+ msb->state++;
+
+ case 6: /* Send extra data read request */
+ if (!msb_read_regs(msb,
+ offsetof(struct ms_register, extra_data),
+ sizeof(struct ms_extra_data_register)))
+ return 0;
+ break;
+
+ case 7: /* Save result of extra data request */
+ msb->regs.extra_data =
+ *(struct ms_extra_data_register *) mrq->data;
+ msb->state++;
+
+ case 8: /* Send the MS_TPC_READ_LONG_DATA to read IO buffer */
+
+ /* Skip that state if we only read the oob */
+ if (msb->regs.param.cp == MEMSTICK_CP_EXTRA) {
+ msb->state++;
+ goto again;
+ }
+
+ msb_set_sg(msb, &sg);
+ memstick_init_req_sg(mrq, MS_TPC_READ_LONG_DATA, &sg);
+ break;
+
+ case 9: /* check validity of data buffer & done */
+
+ if (!(msb->regs.status.interrupt & MEMSTICK_INT_ERR))
+ return msb_exit_state_machine(msb, 0);
+
+ if (msb->regs.status.status1 & MEMSTICK_UNCORR_ERROR) {
+ dbg("read_page: uncorrectable error");
+ return msb_exit_state_machine(msb, -EBADMSG);
+ }
+
+ if (msb->regs.status.status1 & MEMSTICK_CORR_ERROR) {
+ dbg("read_page: correctable error");
+ return msb_exit_state_machine(msb, -EUCLEAN);
+ } else {
+ dbg("read_page: INT error, but no status error bits");
+ return msb_exit_state_machine(msb, -EIO);
+ }
+ default:
+ BUG();
+ }
+ msb->state++;
+ return 0;
+}
+
+/*
+ * Handler of writes of exactly one block.
+ * Takes address from msb->regs.param.
+ * Writes same extra data to blocks, also taken
+ * from msb->regs.extra
+ * Returns -EBADMSG if write fails due to uncorrectable error, or -EIO if
+ * device refuses to take the command or something else
+ */
+static int h_msb_write_block(struct memstick_dev *card,
+ struct memstick_request **out_mrq)
+{
+ struct msb_data *msb = memstick_get_drvdata(card);
+ struct memstick_request *mrq = *out_mrq = &card->current_mrq;
+ struct scatterlist sg;
+ u8 intreg, command;
+
+ if (mrq->error)
+ return msb_exit_state_machine(msb, mrq->error);
+
+again:
+ switch (msb->state) {
+
+ /* HACK: Jmicon handling of TPCs between 8 and
+ * sizeof(memstick_request.data) is broken due to hardware
+ * bug in PIO mode that is used for these TPCs
+ * Therefore split the write
+ */
+
+ case 0: /* write param register*/
+ if (!msb_write_regs(msb,
+ offsetof(struct ms_register, param),
+ sizeof(struct ms_param_register),
+ &msb->regs.param))
+ return 0;
+ break;
+
+ case 1: /* write extra data */
+ if (!msb_write_regs(msb,
+ offsetof(struct ms_register, extra_data),
+ sizeof(struct ms_extra_data_register),
+ &msb->regs.extra_data))
+ return 0;
+ break;
+
+
+ case 2: /* execute the write command*/
+ command = MS_CMD_BLOCK_WRITE;
+ memstick_init_req(mrq, MS_TPC_SET_CMD, &command, 1);
+ break;
+
+ case 3: /* send INT request */
+ if (msb_read_int_reg(msb, -1))
+ break;
+ msb->state++;
+
+ case 4: /* read INT response */
+ intreg = mrq->data[0];
+ msb->regs.status.interrupt = intreg;
+
+ /* errors mean out of here, and fast... */
+ if (intreg & (MEMSTICK_INT_CMDNAK))
+ return msb_exit_state_machine(msb, -EIO);
+
+ if (intreg & MEMSTICK_INT_ERR)
+ return msb_exit_state_machine(msb, -EBADMSG);
+
+
+ /* for last page we need to poll CED */
+ if (msb->current_page == msb->pages_in_block) {
+ if (intreg & MEMSTICK_INT_CED)
+ return msb_exit_state_machine(msb, 0);
+ msb->state--;
+ goto again;
+
+ }
+
+ /* for non-last page we need BREQ before writing next chunk */
+ if (!(intreg & MEMSTICK_INT_BREQ)) {
+ msb->state--;
+ goto again;
+ }
+
+ msb->int_polling = false;
+ msb->state++;
+
+ case 5: /* send the MS_TPC_WRITE_LONG_DATA to perform the write*/
+ msb_set_sg(msb, &sg);
+ memstick_init_req_sg(mrq, MS_TPC_WRITE_LONG_DATA, &sg);
+ mrq->need_card_int = 1;
+ break;
+
+ case 6: /* Switch to next page + go back to int polling */
+ msb->current_page++;
+
+ if (msb->current_page < msb->pages_in_block) {
+ if (msb_advance_sg(msb)) {
+ ms_printk(
+ "BUG: out of data while writing block!");
+ return msb_exit_state_machine(msb, -EFAULT);
+ }
+ }
+ msb->state = 3;
+ goto again;
+ default:
+ BUG();
+ }
+ msb->state++;
+ return 0;
+}
+
+/*
+ * This function is used to send simple IO requests to device that consist
+ * of register write + command
+ */
+static int h_msb_send_command(struct memstick_dev *card,
+ struct memstick_request **out_mrq)
+{
+ struct msb_data *msb = memstick_get_drvdata(card);
+ struct memstick_request *mrq = *out_mrq = &card->current_mrq;
+
+ u8 intreg;
+
+ if (mrq->error) {
+ dbg("send_command: unknown error");
+ return msb_exit_state_machine(msb, mrq->error);
+ }
+again:
+ switch (msb->state) {
+
+ /* HACK: see h_msb_write_block */
+
+ case 0: /* write param register*/
+ if (!msb_write_regs(msb,
+ offsetof(struct ms_register, param),
+ sizeof(struct ms_param_register),
+ &msb->regs.param))
+ return 0;
+ break;
+
+ case 1: /* write extra data */
+ if (!msb->command_need_oob) {
+ msb->state++;
+ goto again;
+ }
+
+ if (!msb_write_regs(msb,
+ offsetof(struct ms_register, extra_data),
+ sizeof(struct ms_extra_data_register),
+ &msb->regs.extra_data))
+ return 0;
+ break;
+
+ case 2: /* execute the command*/
+ memstick_init_req(mrq, MS_TPC_SET_CMD, &msb->command_value, 1);
+ break;
+
+ case 3: /* send INT request */
+ if (msb_read_int_reg(msb, -1))
+ break;
+ msb->state++;
+
+ case 4: /* poll for int bits */
+ intreg = mrq->data[0];
+
+ if (intreg & MEMSTICK_INT_CMDNAK)
+ return msb_exit_state_machine(msb, -EIO);
+ if (intreg & MEMSTICK_INT_ERR)
+ return msb_exit_state_machine(msb, -EBADMSG);
+
+
+ if (!(intreg & MEMSTICK_INT_CED)) {
+ msb->state--;
+ goto again;
+ }
+
+ return msb_exit_state_machine(msb, 0);
+ }
+ msb->state++;
+ return 0;
+}
+
+/* Small handler for card reset */
+static int h_msb_reset(struct memstick_dev *card,
+ struct memstick_request **out_mrq)
+{
+ u8 command = MS_CMD_RESET;
+ struct msb_data *msb = memstick_get_drvdata(card);
+ struct memstick_request *mrq = *out_mrq = &card->current_mrq;
+
+ if (mrq->error)
+ return msb_exit_state_machine(msb, mrq->error);
+
+ switch (msb->state) {
+ case 0:
+ memstick_init_req(mrq, MS_TPC_SET_CMD, &command, 1);
+ mrq->need_card_int = 0;
+ break;
+ case 1:
+ return msb_exit_state_machine(msb, 0);
+ }
+ msb->state++;
+ return 0;
+}
+
+/* This handler is used to do serial->parallel switch */
+static int h_msb_parallel_switch(struct memstick_dev *card,
+ struct memstick_request **out_mrq)
+{
+ struct msb_data *msb = memstick_get_drvdata(card);
+ struct memstick_request *mrq = *out_mrq = &card->current_mrq;
+
+ struct memstick_host *host = card->host;
+
+ if (mrq->error) {
+ dbg("parallel_switch: error");
+ msb->regs.param.system &= ~MEMSTICK_SYS_PAM;
+ return msb_exit_state_machine(msb, mrq->error);
+ }
+
+ switch (msb->state) {
+ case 0: /* Set the parallel interface on memstick side */
+ msb->regs.param.system |= MEMSTICK_SYS_PAM;
+
+ if (!msb_write_regs(msb,
+ offsetof(struct ms_register, param),
+ 1,
+ (unsigned char *)&msb->regs.param))
+ return 0;
+ break;
+
+ case 1: /* Set parallel interface on our side + send a dummy request
+ to see if card responds */
+ host->set_param(host, MEMSTICK_INTERFACE, MEMSTICK_PAR4);
+ memstick_init_req(mrq, MS_TPC_GET_INT, NULL, 1);
+ break;
+
+ case 2:
+ return msb_exit_state_machine(msb, 0);
+ }
+ msb->state++;
+ return 0;
+}
+
+static int msb_switch_to_parallel(struct msb_data *msb);
+
+/* Reset the card, to guard against hw errors beeing treated as bad blocks */
+static int msb_reset(struct msb_data *msb, bool full)
+{
+
+ /*bool was_parallel = msb->regs.param.system & MEMSTICK_SYS_PAM; */
+ struct memstick_dev *card = msb->card;
+ struct memstick_host *host = card->host;
+ int error;
+
+ /* Reset the card */
+ msb->regs.param.system = MEMSTICK_SYS_BAMD;
+
+ if (full) {
+ error = host->set_param(host, MEMSTICK_POWER, MEMSTICK_POWER_OFF);
+ if (error)
+ goto out_error;
+
+ msb_invalidate_reg_window(msb);
+
+ error = host->set_param(host, MEMSTICK_POWER, MEMSTICK_POWER_ON);
+ if (error)
+ goto out_error;
+
+ error = host->set_param(host, MEMSTICK_INTERFACE, MEMSTICK_SERIAL);
+ if (error) {
+out_error:
+ dbg("Failed to reset the host controller");
+ msb->read_only = true;
+ return -EFAULT;
+ }
+ }
+
+ error = msb_run_state_machine(msb, h_msb_reset);
+ if (error) {
+ dbg("Failed to reset the card");
+ msb->read_only = true;
+ return -ENODEV;
+ }
+
+ /* Set parallel mode */
+ /* if (was_parallel) */
+ msb_switch_to_parallel(msb);
+ return 0;
+}
+
+/* Attempts to switch interface to parallel mode */
+static int msb_switch_to_parallel(struct msb_data *msb)
+{
+ int error;
+
+ error = msb_run_state_machine(msb, h_msb_parallel_switch);
+ if (error) {
+ ms_printk("Switch to parallel failed");
+ msb->regs.param.system &= ~MEMSTICK_SYS_PAM;
+ msb_reset(msb, true);
+ return -EFAULT;
+ }
+
+ msb->caps |= MEMSTICK_CAP_AUTO_GET_INT;
+ return 0;
+}
+
+/* Changes overwrite flag on a page */
+static int msb_set_overwrite_flag(struct msb_data *msb,
+ u16 pba, u8 page, u8 flag)
+{
+ if (msb->read_only)
+ return -EROFS;
+
+ msb->regs.param.block_address = cpu_to_be16(pba);
+ msb->regs.param.page_address = page;
+ msb->regs.param.cp = MEMSTICK_CP_OVERWRITE;
+ msb->regs.extra_data.overwrite_flag = flag;
+ msb->command_value = MS_CMD_BLOCK_WRITE;
+ msb->command_need_oob = true;
+
+ dbg_verbose("changing overwrite flag to %02x for sector %d, page %d",
+ flag, pba, page);
+ return msb_run_state_machine(msb, h_msb_send_command);
+}
+
+static int msb_mark_bad(struct msb_data *msb, int pba)
+{
+ ms_printk("marking pba %d as bad", pba);
+ msb_reset(msb, true);
+ return msb_set_overwrite_flag(
+ msb, pba, 0, 0xFF & ~MEMSTICK_OVERWRITE_BKST);
+}
+
+static int msb_mark_page_bad(struct msb_data *msb, int pba, int page)
+{
+ dbg("marking page %d of pba %d as bad", page, pba);
+ msb_reset(msb, true);
+ return msb_set_overwrite_flag(msb,
+ pba, page, ~MEMSTICK_OVERWRITE_PGST0);
+}
+
+/* Erases one physical block */
+static int msb_erase_block(struct msb_data *msb, u16 pba)
+{
+ int error, try;
+ if (msb->read_only)
+ return -EROFS;
+
+ dbg_verbose("erasing pba %d", pba);
+
+ for (try = 1 ; try < 3 ; try++) {
+ msb->regs.param.block_address = cpu_to_be16(pba);
+ msb->regs.param.page_address = 0;
+ msb->regs.param.cp = MEMSTICK_CP_BLOCK;
+ msb->command_value = MS_CMD_BLOCK_ERASE;
+ msb->command_need_oob = false;
+
+
+ error = msb_run_state_machine(msb, h_msb_send_command);
+ if (!error || msb_reset(msb, true))
+ break;
+ }
+
+ if (error) {
+ ms_printk("erase failed, marking pba %d as bad", pba);
+ msb_mark_bad(msb, pba);
+ }
+
+ dbg_verbose("erase success, marking pba %d as unused", pba);
+ msb_mark_block_unused(msb, pba);
+ set_bit(pba, msb->erased_blocks_bitmap);
+ return error;
+}
+
+/* Reads one page from device */
+static int msb_read_page(struct msb_data *msb,
+ u16 pba, u8 page, struct ms_extra_data_register *extra,
+ struct scatterlist *sg)
+{
+ int try, error;
+
+ if (sg && sg->length < msb->page_size) {
+ ms_printk(
+ "BUG: attempt to read pba %d page %d with too small sg",
+ pba, page);
+ return -EINVAL;
+ }
+
+ if (pba == MS_BLOCK_INVALID) {
+ u8 *ptr;
+ unsigned long flags;
+
+ dbg_verbose("read unmapped sector. returning 0xFF");
+
+ local_irq_save(flags);
+ ptr = kmap_atomic(sg_page(sg), KM_IRQ0) + sg->offset;
+ memset(ptr, 0xFF, msb->page_size);
+ kunmap_atomic(ptr - sg->offset, KM_IRQ0);
+ local_irq_restore(flags);
+
+ if (extra)
+ memset(extra, 0xFF, sizeof(*extra));
+ return 0;
+ }
+
+ if (pba >= msb->block_count) {
+ ms_printk("BUG: attempt to read beyond"
+ " the end of the card at pba %d", pba);
+ return -EINVAL;
+ }
+
+ for (try = 1 ; try < 3 ; try++) {
+ msb->regs.param.block_address = cpu_to_be16(pba);
+ msb->regs.param.page_address = page;
+ msb->regs.param.cp = MEMSTICK_CP_PAGE;
+
+ msb->current_sg = sg;
+ msb->sg_offset = 0;
+ error = msb_run_state_machine(msb, h_msb_read_page);
+
+
+ if (error == -EUCLEAN) {
+ ms_printk("correctable error on pba %d, page %d",
+ pba, page);
+ error = 0;
+ }
+
+ if (!error && extra)
+ *extra = msb->regs.extra_data;
+
+ if (!error || msb_reset(msb, true))
+ break;
+
+ }
+
+ /* Mark bad pages */
+ if (error == -EBADMSG) {
+ ms_printk("uncorrectable error on read of pba %d, page %d",
+ pba, page);
+
+ if (msb->regs.extra_data.overwrite_flag &
+ MEMSTICK_OVERWRITE_PGST0)
+ msb_mark_page_bad(msb, pba, page);
+ return -EBADMSG;
+ }
+
+ if (error)
+ ms_printk("read of pba %d, page %d failed with error %d",
+ pba, page, error);
+ return error;
+}
+
+/* Reads oob of page only */
+static int msb_read_oob(struct msb_data *msb, u16 pba, u16 page,
+ struct ms_extra_data_register *extra)
+{
+ int error;
+ BUG_ON(!extra);
+
+ msb->regs.param.block_address = cpu_to_be16(pba);
+ msb->regs.param.page_address = page;
+ msb->regs.param.cp = MEMSTICK_CP_EXTRA;
+
+ if (pba > msb->block_count) {
+ ms_printk("BUG: attempt to read beyond"
+ " the end of card at pba %d", pba);
+ return -EINVAL;
+ }
+
+ error = msb_run_state_machine(msb, h_msb_read_page);
+ *extra = msb->regs.extra_data;
+
+ if (error == -EUCLEAN) {
+ ms_printk("correctable error on pba %d, page %d",
+ pba, page);
+ return 0;
+ }
+
+ return error;
+}
+
+
+/* Reads a block and compares it with data contained in scatterlist orig_sg */
+static bool msb_verify_block(struct msb_data *msb, u16 pba,
+ struct scatterlist *orig_sg)
+{
+ struct scatterlist sg;
+ int page = 0, error;
+
+ while (page < msb->pages_in_block) {
+ sg_init_one(&sg, msb->block_buffer +
+ page * msb->page_size, msb->page_size);
+
+ error = msb_read_page(msb, pba, page, NULL, &sg);
+ if (error)
+ return -EIO;
+ page++;
+ }
+
+ if (sg_compare_to_buffer(orig_sg, msb->block_buffer, msb->block_size))
+ return -EIO;
+ return 0;
+}
+
+/* Writes exectly one block + oob */
+static int msb_write_block(struct msb_data *msb,
+ u16 pba, u32 lba, struct scatterlist *sg)
+{
+ int error, current_try = 1;
+ BUG_ON(sg->length < msb->page_size);
+
+ if (msb->read_only)
+ return -EROFS;
+
+ if (sg_total_len(sg) < msb->block_size) {
+ ms_printk("BUG: write: sg underrrun");
+ return -EINVAL;
+ }
+
+ if (pba == MS_BLOCK_INVALID) {
+ ms_printk(
+ "BUG: write: attempt to write MS_BLOCK_INVALID block");
+ return -EINVAL;
+ }
+
+ if (pba >= msb->block_count || lba >= msb->logical_block_count) {
+ ms_printk(
+ "BUG: write: attempt to write beyond the end of device");
+ return -EINVAL;
+ }
+
+ if (msb_get_zone_from_lba(lba) != msb_get_zone_from_pba(pba)) {
+ ms_printk("BUG: write: lba zone mismatch");
+ return -EINVAL;
+ }
+
+ if (pba == msb->boot_block_locations[0] ||
+ pba == msb->boot_block_locations[1]) {
+ ms_printk("BUG: write: attempt to write to boot blocks!");
+ return -EINVAL;
+ }
+
+ while (1) {
+
+ if (msb->read_only)
+ return -EROFS;
+
+ msb->regs.param.cp = MEMSTICK_CP_BLOCK;
+ msb->regs.param.page_address = 0;
+ msb->regs.param.block_address = cpu_to_be16(pba);
+
+ msb->regs.extra_data.management_flag = 0xFF;
+ msb->regs.extra_data.overwrite_flag = 0xF8;
+ msb->regs.extra_data.logical_address = cpu_to_be16(lba);
+
+ msb->current_sg = sg;
+ msb->current_page = 0;
+ msb->sg_offset = 0;
+
+ error = msb_run_state_machine(msb, h_msb_write_block);
+
+ /* Sector we just wrote to is assumed erased since its pba
+ was erased. If it wasn't erased, write will succeed
+ and will just clear the bits that were set in the block
+ thus test that what we have written,
+ matches what we expect.
+ We do trust the blocks that we erased */
+ if (!error && (verify_writes ||
+ !test_bit(pba, msb->erased_blocks_bitmap)))
+ error = msb_verify_block(msb, pba, sg);
+
+ if (!error)
+ break;
+
+ if (current_try > 1 || msb_reset(msb, true))
+ break;
+
+ ms_printk("write failed, trying to erase the pba %d", pba);
+ error = msb_erase_block(msb, pba);
+ if (error)
+ break;
+
+ current_try++;
+ }
+ return error;
+}
+
+/* Finds a free block for write replacement */
+static u16 msb_get_free_block(struct msb_data *msb, int zone)
+{
+ u16 pos;
+ int pba = zone * MS_BLOCKS_IN_ZONE;
+ int i;
+
+ get_random_bytes(&pos, sizeof(pos));
+
+ if (!msb->free_block_count[zone]) {
+ ms_printk("NO free blocks in the zone %d, to use for a write, "
+ "(media is WORN out) switching to RO mode", zone);
+ msb->read_only = true;
+ return MS_BLOCK_INVALID;
+ }
+
+ pos %= msb->free_block_count[zone];
+
+ dbg_verbose("have %d choices for a free block, selected randomally: %d",
+ msb->free_block_count[zone], pos);
+
+ pba = find_next_zero_bit(msb->used_blocks_bitmap,
+ msb->block_count, pba);
+ for (i = 0 ; i < pos ; ++i)
+ pba = find_next_zero_bit(msb->used_blocks_bitmap,
+ msb->block_count, pba + 1);
+
+ dbg_verbose("result of the free blocks scan: pba %d", pba);
+
+ if (pba == msb->block_count || (msb_get_zone_from_pba(pba)) != zone) {
+ ms_printk("BUG: cant get a free block");
+ msb->read_only = true;
+ return MS_BLOCK_INVALID;
+ }
+
+ msb_mark_block_used(msb, pba);
+ return pba;
+}
+
+static int msb_update_block(struct msb_data *msb, u16 lba,
+ struct scatterlist *sg)
+{
+ u16 pba, new_pba;
+ int error, try;
+
+ pba = msb->lba_to_pba_table[lba];
+ dbg_verbose("start of a block update at lba %d, pba %d", lba, pba);
+
+ if (pba != MS_BLOCK_INVALID) {
+ dbg_verbose("setting the update flag on the block");
+ msb_set_overwrite_flag(msb, pba, 0,
+ 0xFF & ~MEMSTICK_OVERWRITE_UDST);
+ }
+
+ for (try = 0 ; try < 3 ; try++) {
+ new_pba = msb_get_free_block(msb,
+ msb_get_zone_from_lba(lba));
+
+ if (new_pba == MS_BLOCK_INVALID) {
+ error = -EIO;
+ goto out;
+ }
+
+ dbg_verbose("block update: writing updated block to the pba %d",
+ new_pba);
+ error = msb_write_block(msb, new_pba, lba, sg);
+ if (error == -EBADMSG) {
+ msb_mark_bad(msb, new_pba);
+ continue;
+ }
+
+ if (error)
+ goto out;
+
+ dbg_verbose("block update: erasing the old block");
+ msb_erase_block(msb, pba);
+ msb->lba_to_pba_table[lba] = new_pba;
+ return 0;
+ }
+out:
+ if (error) {
+ ms_printk("block update error after %d tries, "
+ "switching to r/o mode", try);
+ msb->read_only = true;
+ }
+ return error;
+}
+
+/* Converts endiannes in the boot block for easy use */
+static void msb_fix_boot_page_endianness(struct ms_boot_page *p)
+{
+ p->header.block_id = be16_to_cpu(p->header.block_id);
+ p->header.format_reserved = be16_to_cpu(p->header.format_reserved);
+ p->entry.disabled_block.start_addr
+ = be32_to_cpu(p->entry.disabled_block.start_addr);
+ p->entry.disabled_block.data_size
+ = be32_to_cpu(p->entry.disabled_block.data_size);
+ p->entry.cis_idi.start_addr
+ = be32_to_cpu(p->entry.cis_idi.start_addr);
+ p->entry.cis_idi.data_size
+ = be32_to_cpu(p->entry.cis_idi.data_size);
+ p->attr.block_size = be16_to_cpu(p->attr.block_size);
+ p->attr.number_of_blocks = be16_to_cpu(p->attr.number_of_blocks);
+ p->attr.number_of_effective_blocks
+ = be16_to_cpu(p->attr.number_of_effective_blocks);
+ p->attr.page_size = be16_to_cpu(p->attr.page_size);
+ p->attr.memory_manufacturer_code
+ = be16_to_cpu(p->attr.memory_manufacturer_code);
+ p->attr.memory_device_code = be16_to_cpu(p->attr.memory_device_code);
+ p->attr.implemented_capacity
+ = be16_to_cpu(p->attr.implemented_capacity);
+ p->attr.controller_number = be16_to_cpu(p->attr.controller_number);
+ p->attr.controller_function = be16_to_cpu(p->attr.controller_function);
+}
+
+static int msb_read_boot_blocks(struct msb_data *msb)
+{
+ int pba = 0;
+ struct scatterlist sg;
+ struct ms_extra_data_register extra;
+ struct ms_boot_page *page;
+
+ msb->boot_block_locations[0] = MS_BLOCK_INVALID;
+ msb->boot_block_locations[1] = MS_BLOCK_INVALID;
+ msb->boot_block_count = 0;
+
+ dbg_verbose("Start of a scan for the boot blocks");
+
+ if (!msb->boot_page) {
+ page = kmalloc(sizeof(struct ms_boot_page) * 2, GFP_KERNEL);
+ if (!page)
+ return -ENOMEM;
+
+ msb->boot_page = page;
+ }
+
+ msb->block_count = MS_BLOCK_MAX_BOOT_ADDR;
+
+ for (pba = 0 ; pba < MS_BLOCK_MAX_BOOT_ADDR ; pba++) {
+
+ sg_init_one(&sg, page, sizeof(*page));
+ if (msb_read_page(msb, pba, 0, &extra, &sg)) {
+ dbg("boot scan: can't read pba %d", pba);
+ continue;
+ }
+
+ if (extra.management_flag & MEMSTICK_MANAGEMENT_SYSFLG) {
+ dbg("managment flag doesn't indicate boot block %d",
+ pba);
+ continue;
+ }
+
+ if (be16_to_cpu(page->header.block_id) != MS_BLOCK_BOOT_ID) {
+ dbg("the pba at %d doesn' contain boot block ID", pba);
+ continue;
+ }
+
+ msb_fix_boot_page_endianness(page);
+ msb->boot_block_locations[msb->boot_block_count] = pba;
+
+ page++;
+ msb->boot_block_count++;
+
+ if (msb->boot_block_count == 2)
+ break;
+ }
+
+ if (!msb->boot_block_count) {
+ ms_printk("media doesn't contain master page, aborting");
+ return -EIO;
+ }
+
+ dbg_verbose("End of scan for boot blocks");
+ return 0;
+}
+
+static int msb_read_bad_block_table(struct msb_data *msb, int block_nr)
+{
+ struct ms_boot_page *boot_block;
+ struct scatterlist sg;
+ struct scatterlist *sg_ptr = &sg;
+ u16 *buffer = NULL;
+
+ int i, error = 0;
+ int data_size, data_offset, page, page_offset, size_to_read;
+ u16 pba;
+
+ BUG_ON(block_nr > 1);
+
+ boot_block = &msb->boot_page[block_nr];
+ pba = msb->boot_block_locations[block_nr];
+
+ if (msb->boot_block_locations[block_nr] == MS_BLOCK_INVALID)
+ return -EINVAL;
+
+ data_size = boot_block->entry.disabled_block.data_size;
+ data_offset = sizeof(struct ms_boot_page) +
+ boot_block->entry.disabled_block.start_addr;
+ if (!data_size)
+ return 0;
+
+ page = data_offset / msb->page_size;
+ page_offset = data_offset % msb->page_size;
+ size_to_read =
+ DIV_ROUND_UP(data_size + page_offset, msb->page_size) *
+ msb->page_size;
+
+ dbg("reading bad block of boot block at pba %d, offset %d len %d",
+ pba, data_offset, data_size);
+
+ buffer = kzalloc(size_to_read, GFP_KERNEL);
+ if (!buffer)
+ return -ENOMEM;
+
+ /* Read the buffer */
+ sg_init_one(&sg, buffer, size_to_read);
+
+ while (sg_ptr) {
+ error = msb_read_page(msb, pba, page, NULL, sg_ptr);
+ if (error)
+ goto out;
+
+ sg_ptr = sg_advance(sg_ptr, msb->page_size);
+ page++;
+ if (page == msb->pages_in_block) {
+ ms_printk(
+ "bad block table extends beyond the boot block");
+ break;
+ }
+ }
+
+ /* Process the bad block table */
+ for (i = page_offset ; i < data_size / sizeof(u16) ; i++) {
+
+ u16 bad_block = be16_to_cpu(buffer[i]);
+
+ if (bad_block >= msb->block_count) {
+ dbg("bad block table contains invalid block %d",
+ bad_block);
+ continue;
+ }
+
+ if (test_bit(bad_block, msb->used_blocks_bitmap)) {
+ dbg("duplicate bad block %d in the table",
+ bad_block);
+ continue;
+ }
+
+ dbg("block %d is marked as factory bad", bad_block);
+ msb_mark_block_used(msb, bad_block);
+ }
+out:
+ kfree(buffer);
+ return error;
+}
+
+static int msb_ftl_initialize(struct msb_data *msb)
+{
+ int i;
+
+ if (msb->ftl_initialized)
+ return 0;
+
+ msb->zone_count = msb->block_count / MS_BLOCKS_IN_ZONE;
+ msb->logical_block_count = msb->zone_count * 496 - 2;
+
+ msb->used_blocks_bitmap = kzalloc(msb->block_count / 8, GFP_KERNEL);
+ msb->erased_blocks_bitmap = kzalloc(msb->block_count / 8, GFP_KERNEL);
+ msb->lba_to_pba_table =
+ kmalloc(msb->logical_block_count * sizeof(u16), GFP_KERNEL);
+
+ if (!msb->used_blocks_bitmap || !msb->lba_to_pba_table ||
+ !msb->erased_blocks_bitmap) {
+ kfree(msb->used_blocks_bitmap);
+ kfree(msb->lba_to_pba_table);
+ kfree(msb->erased_blocks_bitmap);
+ return -ENOMEM;
+ }
+
+ for (i = 0 ; i < msb->zone_count ; i++)
+ msb->free_block_count[i] = MS_BLOCKS_IN_ZONE;
+
+ memset(msb->lba_to_pba_table, MS_BLOCK_INVALID,
+ msb->logical_block_count * sizeof(u16));
+
+ dbg("initial FTL tables created. Zone count = %d, "
+ "Logical block count = %d",
+ msb->zone_count, msb->logical_block_count);
+
+ msb->ftl_initialized = true;
+ return 0;
+}
+
+static int msb_ftl_scan(struct msb_data *msb)
+{
+ u16 pba, lba, other_block;
+ u8 overwrite_flag, managment_flag, other_overwrite_flag;
+ int error;
+ struct ms_extra_data_register extra;
+ u8 *overwrite_flags = kzalloc(msb->block_count, GFP_KERNEL);
+
+ if (!overwrite_flags)
+ return -ENOMEM;
+
+ dbg("Start of media scanning");
+ for (pba = 0 ; pba < msb->block_count ; pba++) {
+
+ if (pba == msb->boot_block_locations[0] ||
+ pba == msb->boot_block_locations[1]) {
+ dbg_verbose("pba %05d -> [boot block]", pba);
+ msb_mark_block_used(msb, pba);
+ continue;
+ }
+
+ if (test_bit(pba, msb->used_blocks_bitmap)) {
+ dbg_verbose("pba %05d -> [factory bad]", pba);
+ continue;
+ }
+
+ error = msb_read_oob(msb, pba, 0, &extra);
+
+ /* can't trust the page if we can't read the oob */
+ if (error == -EBADMSG) {
+ ms_printk(
+ "oob of pba %d damaged, will try to erase it", pba);
+ msb_mark_block_used(msb, pba);
+ msb_erase_block(msb, pba);
+ continue;
+ } else if (error)
+ return error;
+
+ lba = be16_to_cpu(extra.logical_address);
+ managment_flag = extra.management_flag;
+ overwrite_flag = extra.overwrite_flag;
+ overwrite_flags[pba] = overwrite_flag;
+
+ /* Skip bad blocks */
+ if (!(overwrite_flag & MEMSTICK_OVERWRITE_BKST)) {
+ dbg("pba %05d -> [BAD]", pba);
+ msb_mark_block_used(msb, pba);
+ continue;
+ }
+
+ /* Skip system/drm blocks */
+ if ((managment_flag & MEMSTICK_MANAGMENT_FLAG_NORMAL) !=
+ MEMSTICK_MANAGMENT_FLAG_NORMAL) {
+ dbg("pba %05d -> [reserved managment flag %02x]",
+ pba, managment_flag);
+ msb_mark_block_used(msb, pba);
+ continue;
+ }
+
+ /* Erase temporary tables */
+ if (!(managment_flag & MEMSTICK_MANAGEMENT_ATFLG)) {
+ dbg("pba %05d -> [temp table] - will erase", pba);
+
+ msb_mark_block_used(msb, pba);
+ msb_erase_block(msb, pba);
+ continue;
+ }
+
+ if (lba == MS_BLOCK_INVALID) {
+ dbg_verbose("pba %05d -> [free]", pba);
+ continue;
+ }
+
+ msb_mark_block_used(msb, pba);
+
+ /* Block has LBA not according to zoning*/
+ if (msb_get_zone_from_lba(lba) != msb_get_zone_from_pba(pba)) {
+ ms_printk("pba %05d -> [bad lba %05d] - will erase",
+ pba, lba);
+ msb_erase_block(msb, pba);
+ continue;
+ }
+
+ /* No collisions - great */
+ if (msb->lba_to_pba_table[lba] == MS_BLOCK_INVALID) {
+ dbg_verbose("pba %05d -> [lba %05d]", pba, lba);
+ msb->lba_to_pba_table[lba] = pba;
+ continue;
+ }
+
+ other_block = msb->lba_to_pba_table[lba];
+ other_overwrite_flag = overwrite_flags[other_block];
+
+ ms_printk("Collision between pba %d and pba %d",
+ pba, other_block);
+
+ if (!(overwrite_flag & MEMSTICK_OVERWRITE_UDST)) {
+ ms_printk("pba %d is marked as stable, use it", pba);
+ msb_erase_block(msb, other_block);
+ msb->lba_to_pba_table[lba] = pba;
+ continue;
+ }
+
+ if (!(other_overwrite_flag & MEMSTICK_OVERWRITE_UDST)) {
+ ms_printk("pba %d is marked as stable, use it",
+ other_block);
+ msb_erase_block(msb, pba);
+ continue;
+ }
+
+ ms_printk("collision between blocks %d and %d with"
+ " without stable flag set on both, erasing pba %d",
+ pba, other_block, other_block);
+
+ msb_erase_block(msb, other_block);
+ msb->lba_to_pba_table[lba] = pba;
+ }
+
+ dbg("End of media scanning");
+ kfree(overwrite_flags);
+ return 0;
+}
+
+static void msb_cache_flush_timer(unsigned long data)
+{
+ struct msb_data *msb = (struct msb_data *)data;
+ msb->need_flush_cache = true;
+ wake_up_process(msb->io_thread);
+}
+
+
+static void msb_cache_discard(struct msb_data *msb)
+{
+ if (msb->cache_block_lba == MS_BLOCK_INVALID)
+ return;
+
+ del_timer_sync(&msb->cache_flush_timer);
+
+ dbg_verbose("Discarding the write cache");
+ msb->cache_block_lba = MS_BLOCK_INVALID;
+ bitmap_zero(&msb->valid_cache_bitmap, msb->pages_in_block);
+}
+
+static int msb_cache_init(struct msb_data *msb)
+{
+ setup_timer(&msb->cache_flush_timer, msb_cache_flush_timer,
+ (unsigned long)msb);
+
+ if (!msb->cache)
+ msb->cache = kzalloc(msb->block_size, GFP_KERNEL);
+ if (!msb->cache)
+ return -ENOMEM;
+
+ msb_cache_discard(msb);
+ return 0;
+}
+
+static int msb_cache_flush(struct msb_data *msb)
+{
+ struct scatterlist sg;
+ struct ms_extra_data_register extra;
+ int page, offset, error;
+ u16 pba, lba;
+
+ if (msb->read_only)
+ return -EROFS;
+
+ if (msb->cache_block_lba == MS_BLOCK_INVALID)
+ return 0;
+
+ lba = msb->cache_block_lba;
+ pba = msb->lba_to_pba_table[lba];
+
+ dbg_verbose("Flushing the write cache of pba %d (LBA %d)",
+ pba, msb->cache_block_lba);
+
+ /* Read all missing pages in cache */
+ for (page = 0 ; page < msb->pages_in_block ; page++) {
+
+ if (test_bit(page, &msb->valid_cache_bitmap))
+ continue;
+
+ offset = page * msb->page_size;
+ sg_init_one(&sg, msb->cache + offset , msb->page_size);
+
+
+ dbg_verbose("reading non-present sector %d of cache block %d",
+ page, lba);
+ error = msb_read_page(msb, pba, page, &extra, &sg);
+
+ /* Bad pages are copied with 00 page status */
+ if (error == -EBADMSG) {
+ ms_printk("read error on sector %d, contents probably"
+ " damaged", page);
+ continue;
+ }
+
+ if (error)
+ return error;
+
+ if ((extra.overwrite_flag & MEMSTICK_OV_PG_NORMAL) !=
+ MEMSTICK_OV_PG_NORMAL) {
+ dbg("page %d is marked as bad", page);
+ continue;
+ }
+
+ set_bit(page, &msb->valid_cache_bitmap);
+ }
+
+ /* Write the cache now */
+ sg_init_one(&sg, msb->cache , msb->block_size);
+ error = msb_update_block(msb, msb->cache_block_lba, &sg);
+ pba = msb->lba_to_pba_table[msb->cache_block_lba];
+
+ /* Mark invalid pages */
+ if (!error) {
+ for (page = 0 ; page < msb->pages_in_block ; page++) {
+
+ if (test_bit(page, &msb->valid_cache_bitmap))
+ continue;
+
+ dbg("marking page %d as containing damaged data",
+ page);
+ msb_set_overwrite_flag(msb,
+ pba , page, 0xFF & ~MEMSTICK_OV_PG_NORMAL);
+ }
+ }
+
+ msb_cache_discard(msb);
+ return error;
+}
+
+static int msb_cache_write(struct msb_data *msb, int lba,
+ int page, bool add_to_cache_only, struct scatterlist *sg)
+{
+ int error;
+ if (msb->read_only)
+ return -EROFS;
+
+ if (msb->cache_block_lba == MS_BLOCK_INVALID ||
+ lba != msb->cache_block_lba)
+ if (add_to_cache_only)
+ return 0;
+
+ /* If we need to write different block */
+ if (msb->cache_block_lba != MS_BLOCK_INVALID &&
+ lba != msb->cache_block_lba) {
+ dbg_verbose("first flush the cache");
+ error = msb_cache_flush(msb);
+ if (error)
+ return error;
+ }
+
+ if (msb->cache_block_lba == MS_BLOCK_INVALID) {
+ msb->cache_block_lba = lba;
+ mod_timer(&msb->cache_flush_timer,
+ jiffies + msecs_to_jiffies(cache_flush_timeout));
+ }
+
+ dbg_verbose("Write of LBA %d page %d to cache ", lba, page);
+
+ sg_copy_to_buffer(sg, 1, msb->cache + page * msb->page_size,
+ msb->page_size);
+ set_bit(page, &msb->valid_cache_bitmap);
+ return 0;
+}
+
+static int msb_cache_read(struct msb_data *msb, int lba,
+ int page, struct scatterlist *sg)
+{
+ int pba = msb->lba_to_pba_table[lba];
+ int error = 0;
+
+ if (lba == msb->cache_block_lba &&
+ test_bit(page, &msb->valid_cache_bitmap)) {
+
+ dbg_verbose("Read of LBA %d (pba %d) sector %d from cache",
+ lba, pba, page);
+ sg_copy_from_buffer(sg, 1,
+ msb->cache + msb->page_size * page,
+ msb->page_size);
+ } else {
+ dbg_verbose("Read of LBA %d (pba %d) sector %d from device",
+ lba, pba, page);
+
+ error = msb_read_page(msb, pba, page, NULL, sg);
+ if (error)
+ return error;
+
+ msb_cache_write(msb, lba, page, true, sg);
+ }
+ return error;
+}
+
+/* Emulated geometry table
+ * This table content isn't that importaint,
+ * One could put here different values, providing that they still
+ * cover whole disk.
+ * 64 MB entry is what windows reports for my 64M memstick */
+
+static const struct chs_entry chs_table[] = {
+/* size sectors cylynders heads */
+ { 4, 16, 247, 2 },
+ { 8, 16, 495, 2 },
+ { 16, 16, 495, 4 },
+ { 32, 16, 991, 4 },
+ { 64, 16, 991, 8 },
+ {128, 16, 991, 16 },
+ { 0 }
+};
+
+/* Load information about the card */
+static int msb_init_card(struct memstick_dev *card)
+{
+ struct msb_data *msb = memstick_get_drvdata(card);
+ /*struct memstick_host *host = card->host;*/
+ struct ms_boot_page *boot_block;
+ int error = 0, i, raw_size_in_megs;
+
+ msb->caps = 0;
+
+ if (card->id.class >= MEMSTICK_CLASS_ROM &&
+ card->id.class <= MEMSTICK_CLASS_ROM)
+ msb->read_only = true;
+
+ error = msb_reset(msb, false);
+ if (error)
+ return error;
+
+ msb->page_size = sizeof(struct ms_boot_page);
+
+ /* Read the boot page */
+ error = msb_read_boot_blocks(msb);
+ if (error)
+ return -EIO;
+
+ boot_block = &msb->boot_page[0];
+
+ /* Save intersting attributes from boot page */
+ msb->block_count = boot_block->attr.number_of_blocks;
+ msb->page_size = boot_block->attr.page_size;
+
+ msb->pages_in_block = boot_block->attr.block_size * 2;
+ msb->block_size = msb->page_size * msb->pages_in_block;
+
+ if (msb->page_size > PAGE_SIZE) {
+ /* this isn't supported by linux at all, anyway*/
+ dbg("device page %d size isn't supported", msb->page_size);
+ return -EINVAL;
+ }
+
+ msb->block_buffer = kzalloc(msb->block_size, GFP_KERNEL);
+ if (!msb->block_buffer)
+ return -ENOMEM;
+
+ raw_size_in_megs = (msb->block_size * msb->block_count) >> 20;
+
+ for (i = 0 ; chs_table[i].size ; i++) {
+
+ if (chs_table[i].size != raw_size_in_megs)
+ continue;
+
+ msb->geometry.cylinders = chs_table[i].cyl;
+ msb->geometry.heads = chs_table[i].head;
+ msb->geometry.sectors = chs_table[i].sec;
+ break;
+ }
+
+ if (boot_block->attr.transfer_supporting == 1)
+ msb->caps |= MEMSTICK_CAP_PAR4;
+
+ if (boot_block->attr.device_type & 0x03)
+ msb->read_only = true;
+
+ dbg("Total block count = %d", msb->block_count);
+ dbg("Each block consists of %d pages", msb->pages_in_block);
+ dbg("Page size = %d bytes", msb->page_size);
+ dbg("Parallel mode supported: %d", !!(msb->caps & MEMSTICK_CAP_PAR4));
+ dbg("Read only: %d", msb->read_only);
+
+ /* Due to a bug in Jmicron driver, its serial mode barely works
+ so we switch to parallel mode right away */
+#if 0
+ /* Now we can switch the interface */
+ if (host->caps & msb->caps & MEMSTICK_CAP_PAR4)
+ msb_switch_to_parallel(msb);
+#endif
+
+ error = msb_cache_init(msb);
+ if (error)
+ return error;
+
+ error = msb_ftl_initialize(msb);
+ if (error)
+ return error;
+
+
+ /* Read the bad block table */
+ error = msb_read_bad_block_table(msb, 0);
+
+ if (error && error != -ENOMEM) {
+ dbg("failed to read bad block table from primary boot block,"
+ " trying from backup");
+ error = msb_read_bad_block_table(msb, 1);
+ }
+
+ if (error)
+ return error;
+
+ /* *drum roll* Scan the media */
+ error = msb_ftl_scan(msb);
+ if (error) {
+ ms_printk("Scan of media failed");
+ return error;
+ }
+
+ return 0;
+
+}
+
+static int msb_do_write_request(struct msb_data *msb, int lba,
+ int page, struct scatterlist *sg, int *sucessfuly_written)
+{
+ int error = 0;
+ *sucessfuly_written = 0;
+
+ while (sg) {
+ if (page == 0 && sg_total_len(sg) >= msb->block_size) {
+
+ if (msb->cache_block_lba == lba)
+ msb_cache_discard(msb);
+
+ dbg_verbose("Writing whole lba %d", lba);
+ error = msb_update_block(msb, lba, sg);
+ if (error)
+ return error;
+
+ sg = sg_advance(sg, msb->block_size);
+ *sucessfuly_written += msb->block_size;
+ lba++;
+ continue;
+ }
+
+ error = msb_cache_write(msb, lba, page, false, sg);
+ if (error)
+ return error;
+
+ sg = sg_advance(sg, msb->page_size);
+ *sucessfuly_written += msb->page_size;
+
+ page++;
+ if (page == msb->pages_in_block) {
+ page = 0;
+ lba++;
+ }
+ }
+ return 0;
+}
+
+static int msb_do_read_request(struct msb_data *msb, int lba,
+ int page, struct scatterlist *sg, int *sucessfuly_read)
+{
+ int error = 0;
+ *sucessfuly_read = 0;
+
+ while (sg) {
+
+ error = msb_cache_read(msb, lba, page, sg);
+ if (error)
+ return error;
+
+ sg = sg_advance(sg, msb->page_size);
+ *sucessfuly_read += msb->page_size;
+
+ page++;
+ if (page == msb->pages_in_block) {
+ page = 0;
+ lba++;
+ }
+ }
+ return 0;
+}
+
+static int msb_io_thread(void *data)
+{
+ struct msb_data *msb = data;
+ int page, error, len;
+ sector_t lba;
+ unsigned long flags;
+
+ dbg("IO: thread started");
+
+ while (1) {
+
+ if (kthread_should_stop()) {
+ if (msb->req)
+ blk_requeue_request(msb->queue, msb->req);
+ break;
+ }
+
+ spin_lock_irqsave(&msb->q_lock, flags);
+
+ if (msb->need_flush_cache) {
+ msb->need_flush_cache = false;
+ spin_unlock_irqrestore(&msb->q_lock, flags);
+ msb_cache_flush(msb);
+ continue;
+ }
+
+ if (!msb->req) {
+ msb->req = blk_fetch_request(msb->queue);
+
+ if (!msb->req) {
+ dbg_verbose("IO: no more requests, sleeping");
+ set_current_state(TASK_INTERRUPTIBLE);
+ spin_unlock_irqrestore(&msb->q_lock, flags);
+ schedule();
+ dbg_verbose("IO: thread woken up");
+ continue;
+ }
+ }
+
+ spin_unlock_irqrestore(&msb->q_lock, flags);
+
+ /* If card was removed meanwhile */
+ if (!msb->req)
+ continue;
+
+ /* process the request */
+ dbg_verbose("IO: thread processing new request");
+ blk_rq_map_sg(msb->queue, msb->req, msb->req_sg);
+
+ lba = blk_rq_pos(msb->req);
+
+ sector_div(lba, msb->page_size / 512);
+ page = do_div(lba, msb->pages_in_block);
+
+ if (rq_data_dir(msb->req) == READ)
+ error = msb_do_read_request(
+ msb, lba, page, msb->req_sg, &len);
+ else
+ error = msb_do_write_request(
+ msb, lba, page, msb->req_sg, &len);
+
+ spin_lock_irqsave(&msb->q_lock, flags);
+
+ if (len)
+ if (!__blk_end_request(msb->req, 0, len))
+ msb->req = NULL;
+
+ if (error && msb->req) {
+ dbg_verbose("IO: ending one sector "
+ "of the request with error");
+ if (!__blk_end_request(msb->req, error, msb->page_size))
+ msb->req = NULL;
+ }
+
+ if (msb->req)
+ dbg_verbose("IO: request still pending");
+
+ spin_unlock_irqrestore(&msb->q_lock, flags);
+ }
+ return 0;
+}
+
+static DEFINE_IDR(msb_disk_idr);
+static DEFINE_MUTEX(msb_disk_lock);
+
+static int msb_bd_open(struct block_device *bdev, fmode_t mode)
+{
+ struct gendisk *disk = bdev->bd_disk;
+ struct msb_data *msb = disk->private_data;
+
+ dbg_verbose("block device open");
+
+ mutex_lock(&msb_disk_lock);
+
+ if (msb && msb->card)
+ msb->usage_count++;
+
+ mutex_unlock(&msb_disk_lock);
+ return 0;
+}
+
+static void msb_data_clear(struct msb_data *msb)
+{
+ kfree(msb->boot_page);
+ kfree(msb->used_blocks_bitmap);
+ kfree(msb->lba_to_pba_table);
+ kfree(msb->cache);
+ msb->card = NULL;
+}
+
+static int msb_disk_release(struct gendisk *disk)
+{
+ struct msb_data *msb = disk->private_data;
+ int disk_id = MINOR(disk_devt(disk)) >> MS_BLOCK_PART_SHIFT;
+
+ dbg_verbose("block device release");
+
+ mutex_lock(&msb_disk_lock);
+
+ if (msb) {
+ if (msb->usage_count)
+ msb->usage_count--;
+
+ if (!msb->usage_count) {
+ kfree(msb);
+ disk->private_data = NULL;
+ idr_remove(&msb_disk_idr, disk_id);
+ put_disk(disk);
+ }
+ }
+ mutex_unlock(&msb_disk_lock);
+ return 0;
+}
+
+static int msb_bd_release(struct gendisk *disk, fmode_t mode)
+{
+ return msb_disk_release(disk);
+}
+
+static int msb_bd_getgeo(struct block_device *bdev,
+ struct hd_geometry *geo)
+{
+ struct msb_data *msb = bdev->bd_disk->private_data;
+ *geo = msb->geometry;
+ return 0;
+}
+
+static int msb_prepare_req(struct request_queue *q, struct request *req)
+{
+ if (req->cmd_type != REQ_TYPE_FS &&
+ req->cmd_type != REQ_TYPE_BLOCK_PC) {
+ blk_dump_rq_flags(req, "MS unsupported request");
+ return BLKPREP_KILL;
+ }
+ req->cmd_flags |= REQ_DONTPREP;
+ return BLKPREP_OK;
+}
+
+static void msb_submit_req(struct request_queue *q)
+{
+ struct memstick_dev *card = q->queuedata;
+ struct msb_data *msb = memstick_get_drvdata(card);
+ struct request *req = NULL;
+
+ dbg_verbose("Submit request");
+
+ if (msb->card_dead) {
+ dbg("Refusing requests on removed card");
+
+ WARN_ON(msb->io_thread);
+
+ while ((req = blk_fetch_request(q)) != NULL)
+ __blk_end_request_all(req, -ENODEV);
+ return;
+ }
+
+ if (msb->req)
+ return;
+
+ if (msb->io_thread)
+ wake_up_process(msb->io_thread);
+}
+
+static int msb_check_card(struct memstick_dev *card)
+{
+ struct msb_data *msb = memstick_get_drvdata(card);
+ return (msb->card_dead == 0);
+}
+
+static void msb_stop(struct memstick_dev *card)
+{
+ struct msb_data *msb = memstick_get_drvdata(card);
+ unsigned long flags;
+ struct task_struct *io_thread;
+
+ dbg("Stopping all msblock IO");
+
+ /* Just stop the IO thread.
+ Be carefull not to race against submit_request
+ If it is called, all pending requests will be processed by
+ the IO thread as soon as msb_start is called */
+
+ spin_lock_irqsave(&msb->q_lock, flags);
+ blk_stop_queue(msb->queue);
+ io_thread = msb->io_thread;
+ msb->io_thread = NULL;
+ spin_unlock_irqrestore(&msb->q_lock, flags);
+
+ del_timer_sync(&msb->cache_flush_timer);
+
+ if (io_thread)
+ kthread_stop(io_thread);
+}
+
+static void msb_start(struct memstick_dev *card)
+{
+ struct msb_data *msb = memstick_get_drvdata(card);
+ unsigned long flags;
+ int disk_id = MINOR(disk_devt(msb->disk)) >> MS_BLOCK_PART_SHIFT;
+
+ dbg("Resuming IO from msblock");
+
+ msb_invalidate_reg_window(msb);
+
+ spin_lock_irqsave(&msb->q_lock, flags);
+ if (msb->io_thread || msb->card_dead) {
+ spin_unlock_irqrestore(&msb->q_lock, flags);
+ return;
+ }
+ spin_unlock_irqrestore(&msb->q_lock, flags);
+
+ /* Kick cache flush anyway, its harmless */
+ msb->need_flush_cache = true;
+
+ msb->io_thread = kthread_run(msb_io_thread, msb, "kms_block%d",
+ disk_id);
+
+ spin_lock_irqsave(&msb->q_lock, flags);
+ blk_start_queue(msb->queue);
+ spin_unlock_irqrestore(&msb->q_lock, flags);
+}
+
+static const struct block_device_operations msb_bdops = {
+ .open = msb_bd_open,
+ .release = msb_bd_release,
+ .getgeo = msb_bd_getgeo,
+ .owner = THIS_MODULE
+};
+
+/* Registers the block device */
+static int msb_init_disk(struct memstick_dev *card)
+{
+ struct msb_data *msb = memstick_get_drvdata(card);
+ struct memstick_host *host = card->host;
+ int rc, disk_id;
+ u64 limit = BLK_BOUNCE_HIGH;
+ unsigned long capacity;
+
+ if (host->dev.dma_mask && *(host->dev.dma_mask))
+ limit = *(host->dev.dma_mask);
+
+ mutex_lock(&msb_disk_lock);
+
+ if (!idr_pre_get(&msb_disk_idr, GFP_KERNEL)) {
+ mutex_unlock(&msb_disk_lock);
+ return -ENOMEM;
+ }
+
+ rc = idr_get_new(&msb_disk_idr, card, &disk_id);
+ mutex_unlock(&msb_disk_lock);
+
+ if (rc)
+ return rc;
+
+ if ((disk_id << MS_BLOCK_PART_SHIFT) > 255) {
+ rc = -ENOSPC;
+ goto out_release_id;
+ }
+
+ msb->disk = alloc_disk(1 << MS_BLOCK_PART_SHIFT);
+ if (!msb->disk) {
+ rc = -ENOMEM;
+ goto out_release_id;
+ }
+
+ msb->queue = blk_init_queue(msb_submit_req, &msb->q_lock);
+ if (!msb->queue) {
+ rc = -ENOMEM;
+ goto out_put_disk;
+ }
+
+ msb->queue->queuedata = card;
+ blk_queue_prep_rq(msb->queue, msb_prepare_req);
+
+ blk_queue_bounce_limit(msb->queue, limit);
+ blk_queue_max_hw_sectors(msb->queue, MS_BLOCK_MAX_PAGES);
+ blk_queue_max_segments(msb->queue, MS_BLOCK_MAX_SEGS);
+ blk_queue_max_segment_size(msb->queue,
+ MS_BLOCK_MAX_PAGES * msb->page_size);
+ msb->disk->major = major;
+ msb->disk->first_minor = disk_id << MS_BLOCK_PART_SHIFT;
+ msb->disk->fops = &msb_bdops;
+ msb->usage_count = 1;
+ msb->disk->private_data = msb;
+ msb->disk->queue = msb->queue;
+ msb->disk->driverfs_dev = &card->dev;
+ sg_init_table(msb->req_sg, MS_BLOCK_MAX_SEGS+1);
+
+ sprintf(msb->disk->disk_name, "msblk%d", disk_id);
+
+ blk_queue_logical_block_size(msb->queue, msb->page_size);
+
+ capacity = msb->pages_in_block * msb->logical_block_count;
+ capacity *= (msb->page_size / 512);
+
+ set_capacity(msb->disk, capacity);
+ dbg("Set total disk size to %lu sectors", capacity);
+
+ if (msb->read_only)
+ set_disk_ro(msb->disk, 1);
+
+ msb_start(card);
+ add_disk(msb->disk);
+ dbg("Disk added");
+ return 0;
+
+out_put_disk:
+ put_disk(msb->disk);
+out_release_id:
+ mutex_lock(&msb_disk_lock);
+ idr_remove(&msb_disk_idr, disk_id);
+ mutex_unlock(&msb_disk_lock);
+ return rc;
+}
+
+static int msb_probe(struct memstick_dev *card)
+{
+ struct msb_data *msb;
+ int rc = 0;
+
+ msb = kzalloc(sizeof(struct msb_data), GFP_KERNEL);
+ if (!msb)
+ return -ENOMEM;
+ memstick_set_drvdata(card, msb);
+ msb->card = card;
+ spin_lock_init(&msb->q_lock);
+
+ rc = msb_init_card(card);
+ if (rc)
+ goto out_free;
+
+ rc = msb_init_disk(card);
+ if (!rc) {
+ card->check = msb_check_card;
+ card->stop = msb_stop;
+ card->start = msb_start;
+ return 0;
+ }
+out_free:
+ memstick_set_drvdata(card, NULL);
+ msb_data_clear(msb);
+ kfree(msb);
+ return rc;
+}
+
+static void msb_remove(struct memstick_dev *card)
+{
+ struct msb_data *msb = memstick_get_drvdata(card);
+ unsigned long flags;
+
+ if (msb->io_thread)
+ msb_stop(card);
+
+ dbg("Removing the disk device");
+
+ /* Take care of unhandled + new requests from now on */
+ spin_lock_irqsave(&msb->q_lock, flags);
+ msb->card_dead = true;
+ blk_start_queue(msb->queue);
+ spin_unlock_irqrestore(&msb->q_lock, flags);
+
+ /* Remove the disk */
+ del_gendisk(msb->disk);
+ blk_cleanup_queue(msb->queue);
+ msb->queue = NULL;
+
+ mutex_lock(&msb_disk_lock);
+ msb_data_clear(msb);
+ mutex_unlock(&msb_disk_lock);
+
+ msb_disk_release(msb->disk);
+ memstick_set_drvdata(card, NULL);
+}
+
+#ifdef CONFIG_PM
+
+static int msb_suspend(struct memstick_dev *card, pm_message_t state)
+{
+ msb_stop(card);
+ return 0;
+}
+
+static int msb_resume(struct memstick_dev *card)
+{
+ struct msb_data *msb = memstick_get_drvdata(card);
+ struct msb_data *new_msb = NULL;
+ bool card_dead = true;
+
+#ifndef CONFIG_MEMSTICK_UNSAFE_RESUME
+ msb->card_dead = true;
+ return 0;
+#endif
+ mutex_lock(&card->host->lock);
+
+ new_msb = kzalloc(sizeof(struct msb_data), GFP_KERNEL);
+ if (!new_msb)
+ goto out;
+
+ new_msb->card = card;
+ memstick_set_drvdata(card, new_msb);
+ spin_lock_init(&new_msb->q_lock);
+
+ if (msb_init_card(card))
+ goto out;
+
+ if (msb->block_size != new_msb->block_size)
+ goto out;
+
+ if (memcmp(msb->boot_page, new_msb->boot_page,
+ sizeof(struct ms_boot_page)))
+ goto out;
+
+ if (msb->logical_block_count != new_msb->logical_block_count ||
+ memcmp(msb->lba_to_pba_table, new_msb->lba_to_pba_table,
+ msb->logical_block_count))
+ goto out;
+
+ if (msb->block_count != new_msb->block_count ||
+ memcmp(msb->used_blocks_bitmap, new_msb->used_blocks_bitmap,
+ msb->block_count / 8))
+ goto out;
+
+ card_dead = false;
+out:
+ if (card_dead)
+ dbg("Card was removed/replaced during suspend");
+
+ msb->card_dead = card_dead;
+ memstick_set_drvdata(card, msb);
+
+ if (new_msb) {
+ msb_data_clear(new_msb);
+ kfree(new_msb);
+ }
+
+ msb_start(card);
+ mutex_unlock(&card->host->lock);
+ return 0;
+}
+#else
+
+#define msb_suspend NULL
+#define msb_resume NULL
+
+#endif /* CONFIG_PM */
+
+static struct memstick_device_id msb_id_tbl[] = {
+ {MEMSTICK_MATCH_ALL, MEMSTICK_TYPE_LEGACY, MEMSTICK_CATEGORY_STORAGE,
+ MEMSTICK_CLASS_FLASH},
+
+ {MEMSTICK_MATCH_ALL, MEMSTICK_TYPE_LEGACY, MEMSTICK_CATEGORY_STORAGE,
+ MEMSTICK_CLASS_ROM},
+
+ {MEMSTICK_MATCH_ALL, MEMSTICK_TYPE_LEGACY, MEMSTICK_CATEGORY_STORAGE,
+ MEMSTICK_CLASS_RO},
+
+ {MEMSTICK_MATCH_ALL, MEMSTICK_TYPE_LEGACY, MEMSTICK_CATEGORY_STORAGE,
+ MEMSTICK_CLASS_WP},
+
+ {MEMSTICK_MATCH_ALL, MEMSTICK_TYPE_DUO, MEMSTICK_CATEGORY_STORAGE_DUO,
+ MEMSTICK_CLASS_DUO},
+ {}
+};
+MODULE_DEVICE_TABLE(memstick, msb_id_tbl);
+
+
+static struct memstick_driver msb_driver = {
+ .driver = {
+ .name = DRIVER_NAME,
+ .owner = THIS_MODULE
+ },
+ .id_table = msb_id_tbl,
+ .probe = msb_probe,
+ .remove = msb_remove,
+ .suspend = msb_suspend,
+ .resume = msb_resume
+};
+
+static int __init msb_init(void)
+{
+ int rc = -ENOMEM;
+
+ rc = register_blkdev(major, DRIVER_NAME);
+ if (rc < 0) {
+ printk(KERN_ERR DRIVER_NAME ": failed to register "
+ "major %d, error %d\n", major, rc);
+ return rc;
+ }
+ if (!major)
+ major = rc;
+
+ rc = memstick_register_driver(&msb_driver);
+ if (rc)
+ unregister_blkdev(major, DRIVER_NAME);
+ return rc;
+}
+
+static void __exit msb_exit(void)
+{
+ memstick_unregister_driver(&msb_driver);
+ unregister_blkdev(major, DRIVER_NAME);
+ idr_destroy(&msb_disk_idr);
+}
+
+module_init(msb_init);
+module_exit(msb_exit);
+
+module_param(major, int, S_IRUGO);
+MODULE_PARM_DESC(major, "Major to use for block device (default auto)");
+
+module_param(cache_flush_timeout, int, S_IRUGO);
+MODULE_PARM_DESC(cache_flush_timeout,
+ "Cache flush timeout in msec (1000 default)");
+module_param(debug, int, S_IRUGO | S_IWUSR);
+MODULE_PARM_DESC(debug, "Debug level (0-3)");
+
+module_param(verify_writes, bool, S_IRUGO);
+MODULE_PARM_DESC(verify_writes, "Read back and check all data that is written");
+
+MODULE_LICENSE("GPL");
+MODULE_AUTHOR("Maxim Levitsky");
+MODULE_DESCRIPTION("Sony MemoryStick block device driver");
diff --git a/drivers/memstick/core/ms_block.h b/drivers/memstick/core/ms_block.h
new file mode 100644
index 0000000..4607763
--- /dev/null
+++ b/drivers/memstick/core/ms_block.h
@@ -0,0 +1,245 @@
+/*
+ * ms_block.c - Sony MemoryStick (legacy) storage support
+
+ * Copyright (C) 2010 Maxim Levitsky <maximlevitsky@gmail.com>
+ *
+ * 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.
+ *
+ * Minor portions of the driver are copied from mspro_block.c which is
+ * Copyright (C) 2007 Alex Dubov <oakad@yahoo.com>
+ *
+ * Also ms structures were copied from old broken driver by same author
+ * These probably come from MS spec
+ *
+ */
+
+#ifndef MS_BLOCK_NEW_H
+#define MS_BLOCK_NEW_H
+
+#define MS_BLOCK_MAX_SEGS 32
+#define MS_BLOCK_MAX_PAGES ((2 << 16) - 1)
+
+#define MS_BLOCK_MAX_BOOT_ADDR 0x000c
+#define MS_BLOCK_BOOT_ID 0x0001
+#define MS_BLOCK_INVALID 0xffff
+#define MS_MAX_ZONES 16
+#define MS_BLOCKS_IN_ZONE 512
+
+#define MS_BLOCK_MAP_LINE_SZ 16
+#define MS_BLOCK_PART_SHIFT 3
+
+
+#define MEMSTICK_UNCORR_ERROR (MEMSTICK_STATUS1_UCFG | \
+ MEMSTICK_STATUS1_UCEX | MEMSTICK_STATUS1_UCDT)
+
+#define MEMSTICK_CORR_ERROR (MEMSTICK_STATUS1_FGER | MEMSTICK_STATUS1_EXER | \
+ MEMSTICK_STATUS1_DTER)
+
+#define MEMSTICK_INT_ERROR (MEMSTICK_INT_CMDNAK | MEMSTICK_INT_ERR)
+
+#define MEMSTICK_OVERWRITE_FLAG_NORMAL \
+ (MEMSTICK_OVERWRITE_PGST1 | \
+ MEMSTICK_OVERWRITE_PGST0 | \
+ MEMSTICK_OVERWRITE_BKST)
+
+#define MEMSTICK_OV_PG_NORMAL \
+ (MEMSTICK_OVERWRITE_PGST1 | MEMSTICK_OVERWRITE_PGST0)
+
+#define MEMSTICK_MANAGMENT_FLAG_NORMAL \
+ (MEMSTICK_MANAGEMENT_SYSFLG | \
+ MEMSTICK_MANAGEMENT_SCMS1 | \
+ MEMSTICK_MANAGEMENT_SCMS0) \
+
+struct ms_boot_header {
+ unsigned short block_id;
+ unsigned short format_reserved;
+ unsigned char reserved0[184];
+ unsigned char data_entry;
+ unsigned char reserved1[179];
+} __packed;
+
+
+struct ms_system_item {
+ unsigned int start_addr;
+ unsigned int data_size;
+ unsigned char data_type_id;
+ unsigned char reserved[3];
+} __packed;
+
+struct ms_system_entry {
+ struct ms_system_item disabled_block;
+ struct ms_system_item cis_idi;
+ unsigned char reserved[24];
+} __packed;
+
+struct ms_boot_attr_info {
+ unsigned char memorystick_class;
+ unsigned char format_unique_value1;
+ unsigned short block_size;
+ unsigned short number_of_blocks;
+ unsigned short number_of_effective_blocks;
+ unsigned short page_size;
+ unsigned char extra_data_size;
+ unsigned char format_unique_value2;
+ unsigned char assembly_time[8];
+ unsigned char format_unique_value3;
+ unsigned char serial_number[3];
+ unsigned char assembly_manufacturer_code;
+ unsigned char assembly_model_code[3];
+ unsigned short memory_manufacturer_code;
+ unsigned short memory_device_code;
+ unsigned short implemented_capacity;
+ unsigned char format_unique_value4[2];
+ unsigned char vcc;
+ unsigned char vpp;
+ unsigned short controller_number;
+ unsigned short controller_function;
+ unsigned char reserved0[9];
+ unsigned char transfer_supporting;
+ unsigned short format_unique_value5;
+ unsigned char format_type;
+ unsigned char memorystick_application;
+ unsigned char device_type;
+ unsigned char reserved1[22];
+ unsigned char format_uniqure_value6[2];
+ unsigned char reserved2[15];
+} __packed;
+
+struct ms_cis_idi {
+ unsigned short general_config;
+ unsigned short logical_cylinders;
+ unsigned short reserved0;
+ unsigned short logical_heads;
+ unsigned short track_size;
+ unsigned short page_size;
+ unsigned short pages_per_track;
+ unsigned short msw;
+ unsigned short lsw;
+ unsigned short reserved1;
+ unsigned char serial_number[20];
+ unsigned short buffer_type;
+ unsigned short buffer_size_increments;
+ unsigned short long_command_ecc;
+ unsigned char firmware_version[28];
+ unsigned char model_name[18];
+ unsigned short reserved2[5];
+ unsigned short pio_mode_number;
+ unsigned short dma_mode_number;
+ unsigned short field_validity;
+ unsigned short current_logical_cylinders;
+ unsigned short current_logical_heads;
+ unsigned short current_pages_per_track;
+ unsigned int current_page_capacity;
+ unsigned short mutiple_page_setting;
+ unsigned int addressable_pages;
+ unsigned short single_word_dma;
+ unsigned short multi_word_dma;
+ unsigned char reserved3[128];
+} __packed;
+
+
+struct ms_boot_page {
+ struct ms_boot_header header;
+ struct ms_system_entry entry;
+ struct ms_boot_attr_info attr;
+} __packed;
+
+struct msb_data {
+ unsigned int usage_count;
+ struct memstick_dev *card;
+ struct gendisk *disk;
+ struct request_queue *queue;
+ spinlock_t q_lock;
+ struct hd_geometry geometry;
+ struct attribute_group attr_group;
+ struct request *req;
+ int caps;
+
+ /* IO */
+ struct task_struct *io_thread;
+ bool card_dead;
+
+ /* Media properties */
+ struct ms_boot_page *boot_page;
+ u16 boot_block_locations[2];
+ int boot_block_count;
+
+ bool read_only;
+ unsigned short page_size;
+ int block_size;
+ int pages_in_block;
+ int zone_count;
+ int block_count;
+ int logical_block_count;
+
+ /* FTL tables */
+ unsigned long *used_blocks_bitmap;
+ unsigned long *erased_blocks_bitmap;
+ u16 *lba_to_pba_table;
+ int free_block_count[MS_MAX_ZONES];
+ bool ftl_initialized;
+
+ /* Cache */
+ unsigned char *cache;
+ unsigned long valid_cache_bitmap;
+ int cache_block_lba;
+ bool need_flush_cache;
+ struct timer_list cache_flush_timer;
+
+
+ struct scatterlist req_sg[MS_BLOCK_MAX_SEGS+1];
+ unsigned char *block_buffer;
+
+ struct ms_register_addr reg_addr;
+ bool addr_valid;
+
+ /* handler's local data */
+ u8 command_value;
+ bool command_need_oob;
+ struct scatterlist *current_sg;
+ int sg_offset;
+
+ struct ms_register regs;
+ int current_page;
+
+ int state;
+ int exit_error;
+ bool int_polling;
+ unsigned long int_timeout;
+
+};
+
+
+struct chs_entry {
+ unsigned long size;
+ unsigned char sec;
+ unsigned short cyl;
+ unsigned char head;
+};
+
+static int msb_reset(struct msb_data *msb, bool full);
+
+static int h_msb_default_bad(struct memstick_dev *card,
+ struct memstick_request **mrq);
+
+
+
+#define DRIVER_NAME "ms_block"
+
+#define ms_printk(format, ...) \
+ printk(KERN_INFO DRIVER_NAME ": " format "\n", ## __VA_ARGS__)
+
+#define __dbg(level, format, ...) \
+ do { \
+ if (debug >= level) \
+ printk(KERN_DEBUG DRIVER_NAME \
+ ": " format "\n", ## __VA_ARGS__); \
+ } while (0)
+
+
+#define dbg(format, ...) __dbg(1, format, ## __VA_ARGS__)
+#define dbg_verbose(format, ...) __dbg(2, format, ## __VA_ARGS__)
+
+#endif
--
1.7.1
^ permalink raw reply related [flat|nested] 51+ messages in thread
* [PATCH 2/2] memstick: Add driver for Ricoh R5C592 card reader.
2010-12-09 2:39 MEMSTICK: Add my 2 drivers Maxim Levitsky
2010-12-09 2:42 ` [PATCH 1/2] memstick: add support for legacy memorysticks Maxim Levitsky
@ 2010-12-09 2:42 ` Maxim Levitsky
2010-12-09 2:44 ` MEMSTICK: Add my 2 drivers Alex Dubov
2 siblings, 0 replies; 51+ messages in thread
From: Maxim Levitsky @ 2010-12-09 2:42 UTC (permalink / raw)
To: linux-kernel; +Cc: Alex Dubov, Andrew Morton, Takashi Iwai, Maxim Levitsky
Signed-off-by: Maxim Levitsky <maximlevitsky@gmail.com>
---
MAINTAINERS | 5 +
drivers/memstick/host/Kconfig | 12 +
drivers/memstick/host/Makefile | 1 +
drivers/memstick/host/r592.c | 908 ++++++++++++++++++++++++++++++++++++++++
drivers/memstick/host/r592.h | 175 ++++++++
5 files changed, 1101 insertions(+), 0 deletions(-)
create mode 100644 drivers/memstick/host/r592.c
create mode 100644 drivers/memstick/host/r592.h
diff --git a/MAINTAINERS b/MAINTAINERS
index a441ce4..9960e0c 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -5011,6 +5011,11 @@ S: Maintained
F: drivers/mtd/nand/r852.c
F: drivers/mtd/nand/r852.h
+RICOH R5C592 MEMORYSTICK DRIVER
+M: Maxim Levitsky <maximlevitsky@gmail.com>
+S: Maintained
+F: drivers/memstick/host/r592.*
+
RISCOM8 DRIVER
S: Orphan
F: Documentation/serial/riscom8.txt
diff --git a/drivers/memstick/host/Kconfig b/drivers/memstick/host/Kconfig
index 4ce5c8d..cc0997a 100644
--- a/drivers/memstick/host/Kconfig
+++ b/drivers/memstick/host/Kconfig
@@ -30,3 +30,15 @@ config MEMSTICK_JMICRON_38X
To compile this driver as a module, choose M here: the
module will be called jmb38x_ms.
+
+config MEMSTICK_R592
+ tristate "Ricoh R5C592 MemoryStick interface support (EXPERIMENTAL)"
+ depends on EXPERIMENTAL && PCI
+
+ help
+ Say Y here if you want to be able to access MemoryStick cards with
+ the Ricoh R5C592 MemoryStick card reader (which is part of 5 in one
+ multifunction reader)
+
+ To compile this driver as a module, choose M here: the module will
+ be called r592.
diff --git a/drivers/memstick/host/Makefile b/drivers/memstick/host/Makefile
index 12530e4..ad63c16 100644
--- a/drivers/memstick/host/Makefile
+++ b/drivers/memstick/host/Makefile
@@ -8,3 +8,4 @@ endif
obj-$(CONFIG_MEMSTICK_TIFM_MS) += tifm_ms.o
obj-$(CONFIG_MEMSTICK_JMICRON_38X) += jmb38x_ms.o
+obj-$(CONFIG_MEMSTICK_R592) += r592.o
diff --git a/drivers/memstick/host/r592.c b/drivers/memstick/host/r592.c
new file mode 100644
index 0000000..767406c
--- /dev/null
+++ b/drivers/memstick/host/r592.c
@@ -0,0 +1,908 @@
+/*
+ * Copyright (C) 2010 - Maxim Levitsky
+ * driver for Ricoh memstick readers
+ *
+ * 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/freezer.h>
+#include <linux/jiffies.h>
+#include <linux/interrupt.h>
+#include <linux/pci.h>
+#include <linux/pci_ids.h>
+#include <linux/delay.h>
+#include <linux/slab.h>
+#include <linux/kthread.h>
+#include <linux/sched.h>
+#include <linux/highmem.h>
+#include <asm/byteorder.h>
+#include <linux/swab.h>
+#include "r592.h"
+
+static int enable_dma = 1;
+static int debug;
+
+static const char *tpc_names[] = {
+ "MS_TPC_READ_MG_STATUS",
+ "MS_TPC_READ_LONG_DATA",
+ "MS_TPC_READ_SHORT_DATA",
+ "MS_TPC_READ_REG",
+ "MS_TPC_READ_QUAD_DATA",
+ "INVALID",
+ "MS_TPC_GET_INT",
+ "MS_TPC_SET_RW_REG_ADRS",
+ "MS_TPC_EX_SET_CMD",
+ "MS_TPC_WRITE_QUAD_DATA",
+ "MS_TPC_WRITE_REG",
+ "MS_TPC_WRITE_SHORT_DATA",
+ "MS_TPC_WRITE_LONG_DATA",
+ "MS_TPC_SET_CMD",
+};
+
+/**
+ * memstick_debug_get_tpc_name - debug helper that returns string for
+ * a TPC number
+ */
+const char *memstick_debug_get_tpc_name(int tpc)
+{
+ return tpc_names[tpc-1];
+}
+EXPORT_SYMBOL(memstick_debug_get_tpc_name);
+
+
+/* Read a register*/
+static inline u32 r592_read_reg(struct r592_device *dev, int address)
+{
+ u32 value = readl(dev->mmio + address);
+ dbg_reg("reg #%02d == 0x%08x", address, value);
+ return value;
+}
+
+/* Write a register */
+static inline void r592_write_reg(struct r592_device *dev,
+ int address, u32 value)
+{
+ dbg_reg("reg #%02d <- 0x%08x", address, value);
+ writel(value, dev->mmio + address);
+}
+
+/* Reads a big endian DWORD register */
+static inline u32 r592_read_reg_raw_be(struct r592_device *dev, int address)
+{
+ u32 value = __raw_readl(dev->mmio + address);
+ dbg_reg("reg #%02d == 0x%08x", address, value);
+ return be32_to_cpu(value);
+}
+
+/* Writes a big endian DWORD register */
+static inline void r592_write_reg_raw_be(struct r592_device *dev,
+ int address, u32 value)
+{
+ dbg_reg("reg #%02d <- 0x%08x", address, value);
+ __raw_writel(cpu_to_be32(value), dev->mmio + address);
+}
+
+/* Set specific bits in a register (little endian) */
+static inline void r592_set_reg_mask(struct r592_device *dev,
+ int address, u32 mask)
+{
+ u32 reg = readl(dev->mmio + address);
+ dbg_reg("reg #%02d |= 0x%08x (old =0x%08x)", address, mask, reg);
+ writel(reg | mask , dev->mmio + address);
+}
+
+/* Clear specific bits in a register (little endian) */
+static inline void r592_clear_reg_mask(struct r592_device *dev,
+ int address, u32 mask)
+{
+ u32 reg = readl(dev->mmio + address);
+ dbg_reg("reg #%02d &= 0x%08x (old = 0x%08x, mask = 0x%08x)",
+ address, ~mask, reg, mask);
+ writel(reg & ~mask, dev->mmio + address);
+}
+
+
+/* Wait for status bits while checking for errors */
+static int r592_wait_status(struct r592_device *dev, u32 mask, u32 wanted_mask)
+{
+ unsigned long timeout = jiffies + msecs_to_jiffies(1000);
+ u32 reg = r592_read_reg(dev, R592_STATUS);
+
+ if ((reg & mask) == wanted_mask)
+ return 0;
+
+ while (time_before(jiffies, timeout)) {
+
+ reg = r592_read_reg(dev, R592_STATUS);
+
+ if ((reg & mask) == wanted_mask)
+ return 0;
+
+ if (reg & (R592_STATUS_SEND_ERR | R592_STATUS_RECV_ERR))
+ return -EIO;
+
+ cpu_relax();
+ }
+ return -ETIME;
+}
+
+
+/* Enable/disable device */
+static int r592_enable_device(struct r592_device *dev, bool enable)
+{
+ dbg("%sabling the device", enable ? "en" : "dis");
+
+ if (enable) {
+
+ /* Power up the card */
+ r592_write_reg(dev, R592_POWER, R592_POWER_0 | R592_POWER_1);
+
+ /* Perform a reset */
+ r592_set_reg_mask(dev, R592_IO, R592_IO_RESET);
+
+ msleep(100);
+ } else
+ /* Power down the card */
+ r592_write_reg(dev, R592_POWER, 0);
+
+ return 0;
+}
+
+/* Set serial/parallel mode */
+static int r592_set_mode(struct r592_device *dev, bool parallel_mode)
+{
+ if (!parallel_mode) {
+ dbg("switching to serial mode");
+
+ /* Set serial mode */
+ r592_write_reg(dev, R592_IO_MODE, R592_IO_MODE_SERIAL);
+
+ r592_clear_reg_mask(dev, R592_POWER, R592_POWER_20);
+
+ } else {
+ dbg("switching to parallel mode");
+
+ /* This setting should be set _before_ switch TPC */
+ r592_set_reg_mask(dev, R592_POWER, R592_POWER_20);
+
+ r592_clear_reg_mask(dev, R592_IO,
+ R592_IO_SERIAL1 | R592_IO_SERIAL2);
+
+ /* Set the parallel mode now */
+ r592_write_reg(dev, R592_IO_MODE, R592_IO_MODE_PARALLEL);
+ }
+
+ dev->parallel_mode = parallel_mode;
+ return 0;
+}
+
+/* Perform a controller reset without powering down the card */
+static void r592_host_reset(struct r592_device *dev)
+{
+ r592_set_reg_mask(dev, R592_IO, R592_IO_RESET);
+ msleep(100);
+ r592_set_mode(dev, dev->parallel_mode);
+}
+
+/* Disable all hardware interrupts */
+static void r592_clear_interrupts(struct r592_device *dev)
+{
+ /* Disable & ACK all interrupts */
+ r592_clear_reg_mask(dev, R592_REG_MSC, IRQ_ALL_ACK_MASK);
+ r592_clear_reg_mask(dev, R592_REG_MSC, IRQ_ALL_EN_MASK);
+}
+
+/* Tests if there is an CRC error */
+static int r592_test_io_error(struct r592_device *dev)
+{
+ if (!(r592_read_reg(dev, R592_STATUS) &
+ (R592_STATUS_SEND_ERR | R592_STATUS_RECV_ERR)))
+ return 0;
+
+ return -EIO;
+}
+
+/* Ensure that FIFO is ready for use */
+static int r592_test_fifo_empty(struct r592_device *dev)
+{
+ if (r592_read_reg(dev, R592_REG_MSC) & R592_REG_MSC_FIFO_EMPTY)
+ return 0;
+
+ dbg("FIFO not ready, trying to reset the device");
+ r592_host_reset(dev);
+
+ if (r592_read_reg(dev, R592_REG_MSC) & R592_REG_MSC_FIFO_EMPTY)
+ return 0;
+
+ message("FIFO still not ready, giving up");
+ return -EIO;
+}
+
+/* Activates the DMA transfer from to FIFO */
+static void r592_start_dma(struct r592_device *dev, bool is_write)
+{
+ unsigned long flags;
+ u32 reg;
+ spin_lock_irqsave(&dev->irq_lock, flags);
+
+ /* Ack interrupts (just in case) + enable them */
+ r592_clear_reg_mask(dev, R592_REG_MSC, DMA_IRQ_ACK_MASK);
+ r592_set_reg_mask(dev, R592_REG_MSC, DMA_IRQ_EN_MASK);
+
+ /* Set DMA address */
+ r592_write_reg(dev, R592_FIFO_DMA, sg_dma_address(&dev->req->sg));
+
+ /* Enable the DMA */
+ reg = r592_read_reg(dev, R592_FIFO_DMA_SETTINGS);
+ reg |= R592_FIFO_DMA_SETTINGS_EN;
+
+ if (!is_write)
+ reg |= R592_FIFO_DMA_SETTINGS_DIR;
+ else
+ reg &= ~R592_FIFO_DMA_SETTINGS_DIR;
+ r592_write_reg(dev, R592_FIFO_DMA_SETTINGS, reg);
+
+ spin_unlock_irqrestore(&dev->irq_lock, flags);
+}
+
+/* Cleanups DMA related settings */
+static void r592_stop_dma(struct r592_device *dev, int error)
+{
+ r592_clear_reg_mask(dev, R592_FIFO_DMA_SETTINGS,
+ R592_FIFO_DMA_SETTINGS_EN);
+
+ /* This is only a precation */
+ r592_write_reg(dev, R592_FIFO_DMA,
+ dev->dummy_dma_page_physical_address);
+
+ r592_clear_reg_mask(dev, R592_REG_MSC, DMA_IRQ_EN_MASK);
+ r592_clear_reg_mask(dev, R592_REG_MSC, DMA_IRQ_ACK_MASK);
+ dev->dma_error = error;
+}
+
+/* Test if hardware supports DMA */
+static void r592_check_dma(struct r592_device *dev)
+{
+ dev->dma_capable = enable_dma &&
+ (r592_read_reg(dev, R592_FIFO_DMA_SETTINGS) &
+ R592_FIFO_DMA_SETTINGS_CAP);
+}
+
+/* Transfers fifo contents in/out using DMA */
+static int r592_transfer_fifo_dma(struct r592_device *dev)
+{
+ int len, sg_count;
+ bool is_write;
+
+ if (!dev->dma_capable || !dev->req->long_data)
+ return -EINVAL;
+
+ len = dev->req->sg.length;
+ is_write = dev->req->data_dir == WRITE;
+
+ if (len != R592_LFIFO_SIZE)
+ return -EINVAL;
+
+ dbg_verbose("doing dma transfer");
+
+ dev->dma_error = 0;
+ INIT_COMPLETION(dev->dma_done);
+
+ /* TODO: hidden assumption about nenth beeing always 1 */
+ sg_count = dma_map_sg(&dev->pci_dev->dev, &dev->req->sg, 1, is_write ?
+ PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE);
+
+ if (sg_count != 1 ||
+ (sg_dma_len(&dev->req->sg) < dev->req->sg.length)) {
+ message("problem in dma_map_sg");
+ return -EIO;
+ }
+
+ r592_start_dma(dev, is_write);
+
+ /* Wait for DMA completion */
+ if (!wait_for_completion_timeout(
+ &dev->dma_done, msecs_to_jiffies(1000))) {
+ message("DMA timeout");
+ r592_stop_dma(dev, -ETIMEDOUT);
+ }
+
+ dma_unmap_sg(&dev->pci_dev->dev, &dev->req->sg, 1, is_write ?
+ PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE);
+
+
+ return dev->dma_error;
+}
+
+/*
+ * Writes the FIFO in 4 byte chunks.
+ * If length isn't 4 byte aligned, rest of the data if put to a fifo
+ * to be written later
+ * Use r592_flush_fifo_write to flush that fifo when writing for the
+ * last time
+ */
+static void r592_write_fifo_pio(struct r592_device *dev,
+ unsigned char *buffer, int len)
+{
+ /* flush spill from former write */
+ if (!kfifo_is_empty(&dev->pio_fifo)) {
+
+ u8 tmp[4] = {0};
+ int copy_len = kfifo_in(&dev->pio_fifo, buffer, len);
+
+ if (!kfifo_is_full(&dev->pio_fifo))
+ return;
+ len -= copy_len;
+ buffer += copy_len;
+
+ copy_len = kfifo_out(&dev->pio_fifo, tmp, 4);
+ WARN_ON(copy_len != 4);
+ r592_write_reg_raw_be(dev, R592_FIFO_PIO, *(u32 *)tmp);
+ }
+
+ WARN_ON(!kfifo_is_empty(&dev->pio_fifo));
+
+ /* write full dwords */
+ while (len >= 4) {
+ r592_write_reg_raw_be(dev, R592_FIFO_PIO, *(u32 *)buffer);
+ buffer += 4;
+ len -= 4;
+ }
+
+ /* put remaining bytes to the spill */
+ if (len)
+ kfifo_in(&dev->pio_fifo, buffer, len);
+}
+
+/* Flushes the temporary FIFO used to make aligned DWORD writes */
+static void r592_flush_fifo_write(struct r592_device *dev)
+{
+ u8 buffer[4] = { 0 };
+ int len;
+
+ if (kfifo_is_empty(&dev->pio_fifo))
+ return;
+
+ len = kfifo_out(&dev->pio_fifo, buffer, 4);
+ r592_write_reg_raw_be(dev, R592_FIFO_PIO, *(u32 *)buffer);
+}
+
+/*
+ * Read a fifo in 4 bytes chunks.
+ * If input doesn't fit the buffer, it places bytes of last dword in spill
+ * buffer, so that they don't get lost on last read, just throw these away.
+ */
+static void r592_read_fifo_pio(struct r592_device *dev,
+ unsigned char *buffer, int len)
+{
+ u8 tmp[4];
+
+ /* Read from last spill */
+ if (!kfifo_is_empty(&dev->pio_fifo)) {
+ int bytes_copied =
+ kfifo_out(&dev->pio_fifo, buffer, min(4, len));
+ buffer += bytes_copied;
+ len -= bytes_copied;
+
+ if (!kfifo_is_empty(&dev->pio_fifo))
+ return;
+ }
+
+ /* Reads dwords from FIFO */
+ while (len >= 4) {
+ *(u32 *)buffer = r592_read_reg_raw_be(dev, R592_FIFO_PIO);
+ buffer += 4;
+ len -= 4;
+ }
+
+ if (len) {
+ *(u32 *)tmp = r592_read_reg_raw_be(dev, R592_FIFO_PIO);
+ kfifo_in(&dev->pio_fifo, tmp, 4);
+ len -= kfifo_out(&dev->pio_fifo, buffer, len);
+ }
+
+ WARN_ON(len);
+ return;
+}
+
+/* Transfers actual data using PIO. */
+static int r592_transfer_fifo_pio(struct r592_device *dev)
+{
+ unsigned long flags;
+
+ bool is_write = dev->req->tpc >= MS_TPC_SET_RW_REG_ADRS;
+ struct sg_mapping_iter miter;
+
+ kfifo_reset(&dev->pio_fifo);
+
+ if (!dev->req->long_data) {
+ if (is_write) {
+ r592_write_fifo_pio(dev, dev->req->data,
+ dev->req->data_len);
+ r592_flush_fifo_write(dev);
+ } else
+ r592_read_fifo_pio(dev, dev->req->data,
+ dev->req->data_len);
+ return 0;
+ }
+
+ local_irq_save(flags);
+ sg_miter_start(&miter, &dev->req->sg, 1, SG_MITER_ATOMIC |
+ (is_write ? SG_MITER_FROM_SG : SG_MITER_TO_SG));
+
+ /* Do the transfer fifo<->memory*/
+ while (sg_miter_next(&miter))
+ if (is_write)
+ r592_write_fifo_pio(dev, miter.addr, miter.length);
+ else
+ r592_read_fifo_pio(dev, miter.addr, miter.length);
+
+
+ /* Write last few non aligned bytes*/
+ if (is_write)
+ r592_flush_fifo_write(dev);
+
+ sg_miter_stop(&miter);
+ local_irq_restore(flags);
+ return 0;
+}
+
+/* Executes one TPC (data is read/written from small or large fifo) */
+static void r592_execute_tpc(struct r592_device *dev)
+{
+ bool is_write = dev->req->tpc >= MS_TPC_SET_RW_REG_ADRS;
+ int len, error;
+ u32 status, reg;
+
+ if (!dev->req) {
+ message("BUG: tpc execution without request!");
+ return;
+ }
+
+ len = dev->req->long_data ?
+ dev->req->sg.length : dev->req->data_len;
+
+ /* Ensure that FIFO can hold the input data */
+ if (len > R592_LFIFO_SIZE) {
+ message("IO: hardware doesn't support TPCs longer that 512");
+ error = -ENOSYS;
+ goto out;
+ }
+
+ if (!(r592_read_reg(dev, R592_REG_MSC) & R592_REG_MSC_PRSNT)) {
+ dbg("IO: refusing to send TPC because card is absent");
+ error = -ENODEV;
+ goto out;
+ }
+
+ dbg("IO: executing %s LEN=%d",
+ memstick_debug_get_tpc_name(dev->req->tpc), len);
+
+ /* Set IO direction */
+ if (is_write)
+ r592_set_reg_mask(dev, R592_IO, R592_IO_DIRECTION);
+ else
+ r592_clear_reg_mask(dev, R592_IO, R592_IO_DIRECTION);
+
+
+ error = r592_test_fifo_empty(dev);
+ if (error)
+ goto out;
+
+ /* Transfer write data */
+ if (is_write) {
+ error = r592_transfer_fifo_dma(dev);
+ if (error == -EINVAL)
+ error = r592_transfer_fifo_pio(dev);
+ }
+
+ if (error)
+ goto out;
+
+ /* Trigger the TPC */
+ reg = (len << R592_TPC_EXEC_LEN_SHIFT) |
+ (dev->req->tpc << R592_TPC_EXEC_TPC_SHIFT) |
+ R592_TPC_EXEC_BIG_FIFO;
+
+ r592_write_reg(dev, R592_TPC_EXEC, reg);
+
+ /* Wait for TPC completion */
+ status = R592_STATUS_RDY;
+ if (dev->req->need_card_int)
+ status |= R592_STATUS_CED;
+
+ error = r592_wait_status(dev, status, status);
+ if (error) {
+ message("card didn't respond");
+ goto out;
+ }
+
+ /* Test IO errors */
+ error = r592_test_io_error(dev);
+ if (error) {
+ dbg("IO error");
+ goto out;
+ }
+
+ /* Read data from FIFO */
+ if (!is_write) {
+ error = r592_transfer_fifo_dma(dev);
+ if (error == -EINVAL)
+ error = r592_transfer_fifo_pio(dev);
+ }
+
+ /* read INT reg. This can be shortened with shifts, but that way
+ its more readable */
+ if (dev->parallel_mode && dev->req->need_card_int) {
+
+ dev->req->int_reg = 0;
+ status = r592_read_reg(dev, R592_STATUS);
+
+ if (status & R592_STATUS_P_CMDNACK)
+ dev->req->int_reg |= MEMSTICK_INT_CMDNAK;
+ if (status & R592_STATUS_P_BREQ)
+ dev->req->int_reg |= MEMSTICK_INT_BREQ;
+ if (status & R592_STATUS_P_INTERR)
+ dev->req->int_reg |= MEMSTICK_INT_ERR;
+ if (status & R592_STATUS_P_CED)
+ dev->req->int_reg |= MEMSTICK_INT_CED;
+ }
+
+ if (error)
+ dbg("FIFO read error");
+out:
+ dev->req->error = error;
+ r592_clear_reg_mask(dev, R592_REG_MSC, R592_REG_MSC_LED);
+ return;
+}
+
+/* Main request processing thread */
+static int r592_process_thread(void *data)
+{
+ int error;
+ struct r592_device *dev = (struct r592_device *)data;
+ unsigned long flags;
+
+ while (!kthread_should_stop()) {
+ spin_lock_irqsave(&dev->io_thread_lock, flags);
+ set_current_state(TASK_INTERRUPTIBLE);
+ error = memstick_next_req(dev->host, &dev->req);
+ spin_unlock_irqrestore(&dev->io_thread_lock, flags);
+
+ if (error) {
+ if (error == -ENXIO || error == -EAGAIN) {
+ dbg_verbose("IO: done IO, sleeping");
+ } else {
+ dbg("IO: unknown error from "
+ "memstick_next_req %d", error);
+ }
+
+ if (kthread_should_stop())
+ set_current_state(TASK_RUNNING);
+
+ schedule();
+ } else {
+ set_current_state(TASK_RUNNING);
+ r592_execute_tpc(dev);
+ }
+ }
+ return 0;
+}
+
+/* Reprogram chip to detect change in card state */
+/* eg, if card is detected, arm it to detect removal, and vice versa */
+static void r592_update_card_detect(struct r592_device *dev)
+{
+ u32 reg = r592_read_reg(dev, R592_REG_MSC);
+ bool card_detected = reg & R592_REG_MSC_PRSNT;
+
+ dbg("update card detect. card state: %s", card_detected ?
+ "present" : "absent");
+
+ reg &= ~((R592_REG_MSC_IRQ_REMOVE | R592_REG_MSC_IRQ_INSERT) << 16);
+
+ if (card_detected)
+ reg |= (R592_REG_MSC_IRQ_REMOVE << 16);
+ else
+ reg |= (R592_REG_MSC_IRQ_INSERT << 16);
+
+ r592_write_reg(dev, R592_REG_MSC, reg);
+}
+
+/* Timer routine that fires 1 second after last card detection event, */
+static void r592_detect_timer(long unsigned int data)
+{
+ struct r592_device *dev = (struct r592_device *)data;
+ r592_update_card_detect(dev);
+ memstick_detect_change(dev->host);
+}
+
+/* Interrupt handler */
+static irqreturn_t r592_irq(int irq, void *data)
+{
+ struct r592_device *dev = (struct r592_device *)data;
+ irqreturn_t ret = IRQ_NONE;
+ u32 reg;
+ u16 irq_enable, irq_status;
+ unsigned long flags;
+ int error;
+
+ spin_lock_irqsave(&dev->irq_lock, flags);
+
+ reg = r592_read_reg(dev, R592_REG_MSC);
+ irq_enable = reg >> 16;
+ irq_status = reg & 0xFFFF;
+
+ /* Ack the interrupts */
+ reg &= ~irq_status;
+ r592_write_reg(dev, R592_REG_MSC, reg);
+
+ /* Get the IRQ status minus bits that aren't enabled */
+ irq_status &= (irq_enable);
+
+ /* Due to limitation of memstick core, we don't look at bits that
+ indicate that card was removed/inserted and/or present */
+ if (irq_status & (R592_REG_MSC_IRQ_INSERT | R592_REG_MSC_IRQ_REMOVE)) {
+
+ bool card_was_added = irq_status & R592_REG_MSC_IRQ_INSERT;
+ ret = IRQ_HANDLED;
+
+ message("IRQ: card %s", card_was_added ? "added" : "removed");
+
+ mod_timer(&dev->detect_timer,
+ jiffies + msecs_to_jiffies(card_was_added ? 500 : 50));
+ }
+
+ if (irq_status &
+ (R592_REG_MSC_FIFO_DMA_DONE | R592_REG_MSC_FIFO_DMA_ERR)) {
+ ret = IRQ_HANDLED;
+
+ if (irq_status & R592_REG_MSC_FIFO_DMA_ERR) {
+ message("IRQ: DMA error");
+ error = -EIO;
+ } else {
+ dbg_verbose("IRQ: dma done");
+ error = 0;
+ }
+
+ r592_stop_dma(dev, error);
+ complete(&dev->dma_done);
+ }
+
+ spin_unlock_irqrestore(&dev->irq_lock, flags);
+ return ret;
+}
+
+/* External inteface: set settings */
+static int r592_set_param(struct memstick_host *host,
+ enum memstick_param param, int value)
+{
+ struct r592_device *dev = memstick_priv(host);
+
+ switch (param) {
+ case MEMSTICK_POWER:
+ switch (value) {
+ case MEMSTICK_POWER_ON:
+ return r592_enable_device(dev, true);
+ case MEMSTICK_POWER_OFF:
+ return r592_enable_device(dev, false);
+ default:
+ return -EINVAL;
+ }
+ case MEMSTICK_INTERFACE:
+ switch (value) {
+ case MEMSTICK_SERIAL:
+ return r592_set_mode(dev, 0);
+ case MEMSTICK_PAR4:
+ return r592_set_mode(dev, 1);
+ default:
+ return -EINVAL;
+ }
+ default:
+ return -EINVAL;
+ }
+}
+
+/* External interface: submit requests */
+static void r592_submit_req(struct memstick_host *host)
+{
+ struct r592_device *dev = memstick_priv(host);
+ unsigned long flags;
+
+ if (dev->req)
+ return;
+
+ spin_lock_irqsave(&dev->io_thread_lock, flags);
+ if (wake_up_process(dev->io_thread))
+ dbg_verbose("IO thread woken to process requests");
+ spin_unlock_irqrestore(&dev->io_thread_lock, flags);
+}
+
+static const struct pci_device_id r592_pci_id_tbl[] = {
+
+ { PCI_VDEVICE(RICOH, 0x0592), },
+ { },
+};
+
+/* Main entry */
+static int r592_probe(struct pci_dev *pdev, const struct pci_device_id *id)
+{
+ int error = -ENOMEM;
+ struct memstick_host *host;
+ struct r592_device *dev;
+
+ /* Allocate memory */
+ host = memstick_alloc_host(sizeof(struct r592_device), &pdev->dev);
+ if (!host)
+ goto error1;
+
+ dev = memstick_priv(host);
+ dev->host = host;
+ dev->pci_dev = pdev;
+ pci_set_drvdata(pdev, dev);
+
+ /* pci initialization */
+ error = pci_enable_device(pdev);
+ if (error)
+ goto error2;
+
+ pci_set_master(pdev);
+ error = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
+ if (error)
+ goto error3;
+
+ error = pci_request_regions(pdev, DRV_NAME);
+ if (error)
+ goto error3;
+
+ dev->mmio = pci_ioremap_bar(pdev, 0);
+ if (!dev->mmio)
+ goto error4;
+
+ dev->irq = pdev->irq;
+ spin_lock_init(&dev->irq_lock);
+ spin_lock_init(&dev->io_thread_lock);
+ init_completion(&dev->dma_done);
+ INIT_KFIFO(dev->pio_fifo);
+ setup_timer(&dev->detect_timer,
+ r592_detect_timer, (long unsigned int)dev);
+
+ /* Host initialization */
+ host->caps = MEMSTICK_CAP_PAR4;
+ host->request = r592_submit_req;
+ host->set_param = r592_set_param;
+ r592_check_dma(dev);
+
+ dev->io_thread = kthread_run(r592_process_thread, dev, "r592_io");
+ if (IS_ERR(dev->io_thread)) {
+ error = PTR_ERR(dev->io_thread);
+ goto error5;
+ }
+
+ /* This is just a precation, so don't fail */
+ dev->dummy_dma_page = pci_alloc_consistent(pdev, PAGE_SIZE,
+ &dev->dummy_dma_page_physical_address);
+ r592_stop_dma(dev , 0);
+
+ if (request_irq(dev->irq, &r592_irq, IRQF_SHARED,
+ DRV_NAME, dev))
+ goto error6;
+
+ r592_update_card_detect(dev);
+ if (memstick_add_host(host))
+ goto error7;
+
+ message("driver succesfully loaded");
+ return 0;
+error7:
+ free_irq(dev->irq, dev);
+error6:
+ if (dev->dummy_dma_page)
+ pci_free_consistent(pdev, PAGE_SIZE, dev->dummy_dma_page,
+ dev->dummy_dma_page_physical_address);
+
+ kthread_stop(dev->io_thread);
+error5:
+ iounmap(dev->mmio);
+error4:
+ pci_release_regions(pdev);
+error3:
+ pci_disable_device(pdev);
+error2:
+ memstick_free_host(host);
+error1:
+ return error;
+}
+
+static void r592_remove(struct pci_dev *pdev)
+{
+ int error = 0;
+ struct r592_device *dev = pci_get_drvdata(pdev);
+
+ /* Stop the processing thread.
+ That ensures that we won't take any more requests */
+ kthread_stop(dev->io_thread);
+
+ r592_enable_device(dev, false);
+
+ while (!error && dev->req) {
+ dev->req->error = -ETIME;
+ error = memstick_next_req(dev->host, &dev->req);
+ }
+ memstick_remove_host(dev->host);
+
+ free_irq(dev->irq, dev);
+ iounmap(dev->mmio);
+ pci_release_regions(pdev);
+ pci_disable_device(pdev);
+ memstick_free_host(dev->host);
+
+ if (dev->dummy_dma_page)
+ pci_free_consistent(pdev, PAGE_SIZE, dev->dummy_dma_page,
+ dev->dummy_dma_page_physical_address);
+}
+
+#ifdef CONFIG_PM
+static int r592_suspend(struct device *core_dev)
+{
+ struct pci_dev *pdev = to_pci_dev(core_dev);
+ struct r592_device *dev = pci_get_drvdata(pdev);
+
+ r592_clear_interrupts(dev);
+ memstick_suspend_host(dev->host);
+ del_timer_sync(&dev->detect_timer);
+ return 0;
+}
+
+static int r592_resume(struct device *core_dev)
+{
+ struct pci_dev *pdev = to_pci_dev(core_dev);
+ struct r592_device *dev = pci_get_drvdata(pdev);
+
+ r592_clear_interrupts(dev);
+ r592_enable_device(dev, false);
+ memstick_resume_host(dev->host);
+ r592_update_card_detect(dev);
+ return 0;
+}
+
+SIMPLE_DEV_PM_OPS(r592_pm_ops, r592_suspend, r592_resume);
+#endif
+
+MODULE_DEVICE_TABLE(pci, r592_pci_id_tbl);
+
+static struct pci_driver r852_pci_driver = {
+ .name = DRV_NAME,
+ .id_table = r592_pci_id_tbl,
+ .probe = r592_probe,
+ .remove = r592_remove,
+#ifdef CONFIG_PM
+ .driver.pm = &r592_pm_ops,
+#endif
+};
+
+static __init int r592_module_init(void)
+{
+ return pci_register_driver(&r852_pci_driver);
+}
+
+static void __exit r592_module_exit(void)
+{
+ pci_unregister_driver(&r852_pci_driver);
+}
+
+module_init(r592_module_init);
+module_exit(r592_module_exit);
+
+module_param(enable_dma, bool, S_IRUGO);
+MODULE_PARM_DESC(enable_dma, "Enable usage of the DMA (default)");
+module_param(debug, int, S_IRUGO | S_IWUSR);
+MODULE_PARM_DESC(debug, "Debug level (0-3)");
+
+MODULE_LICENSE("GPL");
+MODULE_AUTHOR("Maxim Levitsky <maximlevitsky@gmail.com>");
+MODULE_DESCRIPTION("Ricoh R5C592 Memstick/Memstick PRO card reader driver");
diff --git a/drivers/memstick/host/r592.h b/drivers/memstick/host/r592.h
new file mode 100644
index 0000000..eee264e
--- /dev/null
+++ b/drivers/memstick/host/r592.h
@@ -0,0 +1,175 @@
+/*
+ * Copyright (C) 2010 - Maxim Levitsky
+ * driver for Ricoh memstick readers
+ *
+ * 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 R592_H
+
+#include <linux/memstick.h>
+#include <linux/spinlock.h>
+#include <linux/interrupt.h>
+#include <linux/workqueue.h>
+#include <linux/kfifo.h>
+#include <linux/ctype.h>
+
+/* write to this reg (number,len) triggers TPC execution */
+#define R592_TPC_EXEC 0x00
+#define R592_TPC_EXEC_LEN_SHIFT 16 /* Bits 16..25 are TPC len */
+#define R592_TPC_EXEC_BIG_FIFO (1 << 26) /* If bit 26 is set, large fifo is used (reg 48) */
+#define R592_TPC_EXEC_TPC_SHIFT 28 /* Bits 28..31 are the TPC number */
+
+
+/* Window for small TPC fifo (big endian)*/
+/* reads and writes always are done in 8 byte chunks */
+/* Not used in driver, because large fifo does better job */
+#define R592_SFIFO 0x08
+
+
+/* Status register (ms int, small fifo, IO)*/
+#define R592_STATUS 0x10
+ /* Parallel INT bits */
+#define R592_STATUS_P_CMDNACK (1 << 16) /* INT reg: NACK (parallel mode) */
+#define R592_STATUS_P_BREQ (1 << 17) /* INT reg: card ready (parallel mode)*/
+#define R592_STATUS_P_INTERR (1 << 18) /* INT reg: int error (parallel mode)*/
+#define R592_STATUS_P_CED (1 << 19) /* INT reg: command done (parallel mode) */
+
+ /* Fifo status */
+#define R592_STATUS_SFIFO_FULL (1 << 20) /* Small Fifo almost full (last chunk is written) */
+#define R592_STATUS_SFIFO_EMPTY (1 << 21) /* Small Fifo empty */
+
+ /* Error detection via CRC */
+#define R592_STATUS_SEND_ERR (1 << 24) /* Send failed */
+#define R592_STATUS_RECV_ERR (1 << 25) /* Recieve failed */
+
+ /* Card state */
+#define R592_STATUS_RDY (1 << 28) /* RDY signal recieved */
+#define R592_STATUS_CED (1 << 29) /* INT: Command done (serial mode)*/
+#define R592_STATUS_SFIFO_INPUT (1 << 30) /* Small fifo recieved data*/
+
+#define R592_SFIFO_SIZE 32 /* total size of small fifo is 32 bytes */
+#define R592_SFIFO_PACKET 8 /* packet size of small fifo */
+
+/* IO control */
+#define R592_IO 0x18
+#define R592_IO_16 (1 << 16) /* Set by default, can be cleared */
+#define R592_IO_18 (1 << 18) /* Set by default, can be cleared */
+#define R592_IO_SERIAL1 (1 << 20) /* Set by default, can be cleared, (cleared on parallel) */
+#define R592_IO_22 (1 << 22) /* Set by default, can be cleared */
+#define R592_IO_DIRECTION (1 << 24) /* TPC direction (1 write 0 read) */
+#define R592_IO_26 (1 << 26) /* Set by default, can be cleared */
+#define R592_IO_SERIAL2 (1 << 30) /* Set by default, can be cleared (cleared on parallel), serial doesn't work if unset */
+#define R592_IO_RESET (1 << 31) /* Reset, sets defaults*/
+
+
+/* Turns hardware on/off */
+#define R592_POWER 0x20 /* bits 0-7 writeable */
+#define R592_POWER_0 (1 << 0) /* set on start, cleared on stop - must be set*/
+#define R592_POWER_1 (1 << 1) /* set on start, cleared on stop - must be set*/
+#define R592_POWER_3 (1 << 3) /* must be clear */
+#define R592_POWER_20 (1 << 5) /* set before switch to parallel */
+
+/* IO mode*/
+#define R592_IO_MODE 0x24
+#define R592_IO_MODE_SERIAL 1
+#define R592_IO_MODE_PARALLEL 3
+
+
+/* IRQ,card detection,large fifo (first word irq status, second enable) */
+/* IRQs are ACKed by clearing the bits */
+#define R592_REG_MSC 0x28
+#define R592_REG_MSC_PRSNT (1 << 1) /* card present (only status)*/
+#define R592_REG_MSC_IRQ_INSERT (1 << 8) /* detect insert / card insered */
+#define R592_REG_MSC_IRQ_REMOVE (1 << 9) /* detect removal / card removed */
+#define R592_REG_MSC_FIFO_EMPTY (1 << 10) /* fifo is empty */
+#define R592_REG_MSC_FIFO_DMA_DONE (1 << 11) /* dma enable / dma done */
+
+#define R592_REG_MSC_FIFO_USER_ORN (1 << 12) /* set if software reads empty fifo (if R592_REG_MSC_FIFO_EMPTY is set) */
+#define R592_REG_MSC_FIFO_MISMATH (1 << 13) /* set if amount of data in fifo doesn't match amount in TPC */
+#define R592_REG_MSC_FIFO_DMA_ERR (1 << 14) /* IO failure */
+#define R592_REG_MSC_LED (1 << 15) /* clear to turn led off (only status)*/
+
+#define DMA_IRQ_ACK_MASK \
+ (R592_REG_MSC_FIFO_DMA_DONE | R592_REG_MSC_FIFO_DMA_ERR)
+
+#define DMA_IRQ_EN_MASK (DMA_IRQ_ACK_MASK << 16)
+
+#define IRQ_ALL_ACK_MASK 0x00007F00
+#define IRQ_ALL_EN_MASK (IRQ_ALL_ACK_MASK << 16)
+
+/* DMA address for large FIFO read/writes*/
+#define R592_FIFO_DMA 0x2C
+
+/* PIO access to large FIFO (512 bytes) (big endian)*/
+#define R592_FIFO_PIO 0x30
+#define R592_LFIFO_SIZE 512 /* large fifo size */
+
+
+/* large FIFO DMA settings */
+#define R592_FIFO_DMA_SETTINGS 0x34
+#define R592_FIFO_DMA_SETTINGS_EN (1 << 0) /* DMA enabled */
+#define R592_FIFO_DMA_SETTINGS_DIR (1 << 1) /* Dma direction (1 read, 0 write) */
+#define R592_FIFO_DMA_SETTINGS_CAP (1 << 24) /* Dma is aviable */
+
+/* Maybe just an delay */
+/* Bits 17..19 are just number */
+/* bit 16 is set, then bit 20 is waited */
+/* time to wait is about 50 spins * 2 ^ (bits 17..19) */
+/* seems to be possible just to ignore */
+/* Probably debug register */
+#define R592_REG38 0x38
+#define R592_REG38_CHANGE (1 << 16) /* Start bit */
+#define R592_REG38_DONE (1 << 20) /* HW set this after the delay */
+#define R592_REG38_SHIFT 17
+
+/* Debug register, written (0xABCDEF00) when error happens - not used*/
+#define R592_REG_3C 0x3C
+
+struct r592_device {
+ struct pci_dev *pci_dev;
+ struct memstick_host *host; /* host backpointer */
+ struct memstick_request *req; /* current request */
+
+ /* Registers, IRQ */
+ void __iomem *mmio;
+ int irq;
+ spinlock_t irq_lock;
+ spinlock_t io_thread_lock;
+ struct timer_list detect_timer;
+
+ struct task_struct *io_thread;
+ bool parallel_mode;
+
+ DECLARE_KFIFO(pio_fifo, u8, sizeof(u32));
+
+ /* DMA area */
+ int dma_capable;
+ int dma_error;
+ struct completion dma_done;
+ void *dummy_dma_page;
+ dma_addr_t dummy_dma_page_physical_address;
+
+};
+
+#define DRV_NAME "r592"
+
+
+#define message(format, ...) \
+ printk(KERN_INFO DRV_NAME ": " format "\n", ## __VA_ARGS__)
+
+#define __dbg(level, format, ...) \
+ do { \
+ if (debug >= level) \
+ printk(KERN_DEBUG DRV_NAME \
+ ": " format "\n", ## __VA_ARGS__); \
+ } while (0)
+
+
+#define dbg(format, ...) __dbg(1, format, ## __VA_ARGS__)
+#define dbg_verbose(format, ...) __dbg(2, format, ## __VA_ARGS__)
+#define dbg_reg(format, ...) __dbg(3, format, ## __VA_ARGS__)
+
+#endif
--
1.7.1
^ permalink raw reply related [flat|nested] 51+ messages in thread
* Re: MEMSTICK: Add my 2 drivers
2010-12-09 2:39 MEMSTICK: Add my 2 drivers Maxim Levitsky
2010-12-09 2:42 ` [PATCH 1/2] memstick: add support for legacy memorysticks Maxim Levitsky
2010-12-09 2:42 ` [PATCH 2/2] memstick: Add driver for Ricoh R5C592 card reader Maxim Levitsky
@ 2010-12-09 2:44 ` Alex Dubov
2010-12-09 2:48 ` Maxim Levitsky
2010-12-09 7:15 ` Takashi Iwai
2 siblings, 2 replies; 51+ messages in thread
From: Alex Dubov @ 2010-12-09 2:44 UTC (permalink / raw)
To: linux-kernel, Maxim Levitsky; +Cc: Andrew Morton, Takashi Iwai
>
> Since I don't touch (and don't want anymore to) any code
> written by
> Alex, and I even added workarounds for bugs in his jmicron
> driver to the
> generic ms_block.c code (it tested and works on my both
> card readers),
> maybe even add these to 2.6.37?
This are worked around by the recent patches, are not they?
^ permalink raw reply [flat|nested] 51+ messages in thread
* Re: MEMSTICK: Add my 2 drivers
2010-12-09 2:44 ` MEMSTICK: Add my 2 drivers Alex Dubov
@ 2010-12-09 2:48 ` Maxim Levitsky
2010-12-09 7:15 ` Takashi Iwai
1 sibling, 0 replies; 51+ messages in thread
From: Maxim Levitsky @ 2010-12-09 2:48 UTC (permalink / raw)
To: Alex Dubov; +Cc: linux-kernel, Andrew Morton, Takashi Iwai
On Wed, 2010-12-08 at 18:44 -0800, Alex Dubov wrote:
> >
> > Since I don't touch (and don't want anymore to) any code
> > written by
> > Alex, and I even added workarounds for bugs in his jmicron
> > driver to the
> > generic ms_block.c code (it tested and works on my both
> > card readers),
> > maybe even add these to 2.6.37?
>
> This are worked around by the recent patches, are not they?
I don't care, really.
This is your code, I don't want another frustration and flamewar.
I will eventually test your patches (I did see them before I send that
mail), if they don't work, I just ask for a revert.
Best regards,
Maxim Levitsky
^ permalink raw reply [flat|nested] 51+ messages in thread
* Re: MEMSTICK: Add my 2 drivers
2010-12-09 2:44 ` MEMSTICK: Add my 2 drivers Alex Dubov
2010-12-09 2:48 ` Maxim Levitsky
@ 2010-12-09 7:15 ` Takashi Iwai
2010-12-10 22:23 ` Maxim Levitsky
1 sibling, 1 reply; 51+ messages in thread
From: Takashi Iwai @ 2010-12-09 7:15 UTC (permalink / raw)
To: Alex Dubov; +Cc: linux-kernel, Maxim Levitsky, Andrew Morton
At Wed, 8 Dec 2010 18:44:04 -0800 (PST),
Alex Dubov wrote:
>
> >
> > Since I don't touch (and don't want anymore to) any code
> > written by
> > Alex, and I even added workarounds for bugs in his jmicron
> > driver to the
> > generic ms_block.c code (it tested and works on my both
> > card readers),
> > maybe even add these to 2.6.37?
>
> This are worked around by the recent patches, are not they?
Do you mean that the support for old memstick media was added / worked
around in the recent patches? That would be great. It's what I was
looking for right now. If so, could you point your patches?
thanks,
Takashi
^ permalink raw reply [flat|nested] 51+ messages in thread
* Re: [PATCH 1/2] memstick: add support for legacy memorysticks
2010-12-09 2:42 ` [PATCH 1/2] memstick: add support for legacy memorysticks Maxim Levitsky
@ 2010-12-09 7:19 ` Takashi Iwai
2010-12-09 11:56 ` Takashi Iwai
2010-12-09 21:09 ` Andrew Morton
1 sibling, 1 reply; 51+ messages in thread
From: Takashi Iwai @ 2010-12-09 7:19 UTC (permalink / raw)
To: Maxim Levitsky; +Cc: linux-kernel, Alex Dubov, Andrew Morton
At Thu, 9 Dec 2010 04:42:25 +0200,
Maxim Levitsky wrote:
>
> Based partialy on spec quotes from Alex Dubov.
>
> As any code that works with user data this driver isn't recommened to use
> with valuable data.
>
> It tries its best though to avoid data corruption and possible damage to
> the card.
>
> Tested with MS DUO 64 MB card on Ricoh and Jmicron reader.
>
> Signed-off-by: Maxim Levitsky <maximlevitsky@gmail.com>
Oh, this is interesting. Just now I looked for a solution for the old
MS with new JMicron controllers. (I should have checked mm tree
beforehand...)
I'll give it a try and report back later.
thanks,
Takashi
^ permalink raw reply [flat|nested] 51+ messages in thread
* Re: [PATCH 1/2] memstick: add support for legacy memorysticks
2010-12-09 7:19 ` Takashi Iwai
@ 2010-12-09 11:56 ` Takashi Iwai
2010-12-09 15:44 ` Takashi Iwai
0 siblings, 1 reply; 51+ messages in thread
From: Takashi Iwai @ 2010-12-09 11:56 UTC (permalink / raw)
To: Maxim Levitsky; +Cc: linux-kernel, Alex Dubov, Andrew Morton, Aries Lee
At Thu, 09 Dec 2010 08:19:47 +0100,
Takashi Iwai wrote:
>
> At Thu, 9 Dec 2010 04:42:25 +0200,
> Maxim Levitsky wrote:
> >
> > Based partialy on spec quotes from Alex Dubov.
> >
> > As any code that works with user data this driver isn't recommened to use
> > with valuable data.
> >
> > It tries its best though to avoid data corruption and possible damage to
> > the card.
> >
> > Tested with MS DUO 64 MB card on Ricoh and Jmicron reader.
> >
> > Signed-off-by: Maxim Levitsky <maximlevitsky@gmail.com>
>
> Oh, this is interesting. Just now I looked for a solution for the old
> MS with new JMicron controllers. (I should have checked mm tree
> beforehand...)
>
> I'll give it a try and report back later.
I tried your patch now.
But got a WARNING and errors without working properly:
[ 86.894497] WARNING: at drivers/memstick/core/ms_block.c:255 msb_run_state_machine+0x90/0xb0 [ms_block]()
[ 86.894502] Hardware name:
[ 86.894504] Modules linked in: ms_block(+) nls_iso8859_1 nls_cp437 vfat fat mmc_block netconsole configfs nfs lockd nfs_acl auth_rpcgss sunrpc af_packet i915 drm_kms_helper drm i2c_algo_bit snd_pcm_oss cpufreq_conservative snd_mixer_oss cpufreq_userspace snd_seq cpufreq_powersave acpi_cpufreq mperf binfmt_misc snd_seq_device edd ipv6 fuse loop dm_mod acpiphp pci_hotplug snd_hda_codec_hdmi snd_hda_codec_idt processor rtc_cmos snd_hda_intel snd_hda_codec snd_hwdep snd_pcm snd_timer uvcvideo video thermal thermal_sys snd rtc_core sr_mod soundcore snd_page_alloc sdhci_pci hwmon videodev cdrom r8169 rtc_lib v4l1_compat tpm_tis tpm intel_agp tpm_bios pcspkr hp_accel intel_gtt iTCO_wdt jmb38x_ms memstick sdhci mmc_core mii iTCO_vendor_support sg joydev lis3lv02d serio_raw i2c_core input_polldev output wmi container button battery ac ata_piix sd_mod crc_t10dif ehci_hcd ahci libahci libata usbcore scsi_mod ext3 jbd mbcache
[ 86.895023] Pid: 4606, comm: modprobe Not tainted 2.6.37-rc5-test #2
[ 86.895037] Call Trace:
[ 86.895048] [<f84fa460>] ? msb_run_state_machine+0x90/0xb0 [ms_block]
[ 86.895062] [<f84fa460>] ? msb_run_state_machine+0x90/0xb0 [ms_block]
[ 86.895080] [<c0241d6c>] warn_slowpath_common+0x6c/0xa0
[ 86.895092] [<f84fa460>] ? msb_run_state_machine+0x90/0xb0 [ms_block]
[ 86.895105] [<f84fc280>] ? h_msb_reset+0x0/0x90 [ms_block]
[ 86.895118] [<c0241dbd>] warn_slowpath_null+0x1d/0x20
[ 86.895132] [<f84fa460>] msb_run_state_machine+0x90/0xb0 [ms_block]
[ 86.895151] [<f84fa52c>] msb_reset+0xac/0x130 [ms_block]
[ 86.895169] [<f84fc9db>] msb_probe+0x6b/0xcc8 [ms_block]
[ 86.895175] [<c0337345>] ? sysfs_addrm_finish+0x15/0xc0
[ 86.895187] [<c0337d75>] ? sysfs_do_create_link+0xb5/0x1b0
[ 86.895191] [<c0337345>] ? sysfs_addrm_finish+0x15/0xc0
[ 86.895212] [<f92ba344>] memstick_device_probe+0x34/0x50 [memstick]
[ 86.895228] [<c042cbf5>] driver_probe_device+0x95/0x280
[ 86.895233] [<c038be9a>] ? kobject_add_internal+0xca/0x210
[ 86.895249] [<c0395543>] ? kvasprintf+0x43/0x60
[ 86.895252] [<c042ce59>] __driver_attach+0x79/0x80
[ 86.895256] [<c042c3d3>] bus_for_each_dev+0x53/0x80
[ 86.895274] [<f92ba2b0>] ? memstick_device_remove+0x0/0x50 [memstick]
[ 86.895278] [<c042c9f9>] driver_attach+0x19/0x20
[ 86.895290] [<c042cde0>] ? __driver_attach+0x0/0x80
[ 86.895292] [<c042bcc7>] bus_add_driver+0x197/0x280
[ 86.895297] [<f92ba2b0>] ? memstick_device_remove+0x0/0x50 [memstick]
[ 86.895308] [<c042d0f5>] driver_register+0x75/0x160
[ 86.895313] [<c0381f18>] ? register_blkdev+0xf8/0x130
[ 86.895326] [<f8234000>] ? msb_init+0x0/0x6d [ms_block]
[ 86.895332] [<f92ba422>] memstick_register_driver+0x12/0x20 [memstick]
[ 86.895337] [<f8234035>] msb_init+0x35/0x6d [ms_block]
[ 86.895351] [<c020112b>] do_one_initcall+0x2b/0x160
[ 86.895365] [<c0273943>] sys_init_module+0x93/0x1d0
[ 86.895377] [<c0202d98>] sysenter_do_call+0x12/0x28
[ 86.895381] ---[ end trace 0f1b7248462dd657 ]---
[ 86.895593] ms_block: Switch to parallel failed
[ 86.908121] ms_block: Switch to parallel failed
And "switch to parallel failed" continues endlessly until you unplug.
Also, at another time, the machine got frozen with plugging.
After unplugging:
[ 91.398455] ms_block: read of pba 0, page 0 failed with error -62
[ 91.414437] ms_block: read of pba 1, page 0 failed with error -62
[ 91.430412] ms_block: read of pba 2, page 0 failed with error -62
[ 91.446389] ms_block: read of pba 3, page 0 failed with error -62
[ 91.462370] ms_block: read of pba 4, page 0 failed with error -62
[ 91.478242] ms_block: read of pba 5, page 0 failed with error -62
[ 91.494224] ms_block: read of pba 6, page 0 failed with error -62
[ 91.510205] ms_block: read of pba 7, page 0 failed with error -62
[ 91.526187] ms_block: read of pba 8, page 0 failed with error -62
[ 91.542163] ms_block: read of pba 9, page 0 failed with error -62
[ 91.558145] ms_block: read of pba 10, page 0 failed with error -62
[ 91.574127] ms_block: read of pba 11, page 0 failed with error -62
[ 91.574153] ms_block: media doesn't contain master page, aborting
[ 91.574176] ms_block: probe of memstick0 failed with error -5
thanks,
Takashi
^ permalink raw reply [flat|nested] 51+ messages in thread
* Re: [PATCH 1/2] memstick: add support for legacy memorysticks
2010-12-09 11:56 ` Takashi Iwai
@ 2010-12-09 15:44 ` Takashi Iwai
2010-12-10 22:34 ` Maxim Levitsky
0 siblings, 1 reply; 51+ messages in thread
From: Takashi Iwai @ 2010-12-09 15:44 UTC (permalink / raw)
To: Maxim Levitsky; +Cc: linux-kernel, Alex Dubov, Andrew Morton, Aries Lee
At Thu, 09 Dec 2010 12:56:44 +0100,
Takashi Iwai wrote:
>
> At Thu, 09 Dec 2010 08:19:47 +0100,
> Takashi Iwai wrote:
> >
> > At Thu, 9 Dec 2010 04:42:25 +0200,
> > Maxim Levitsky wrote:
> > >
> > > Based partialy on spec quotes from Alex Dubov.
> > >
> > > As any code that works with user data this driver isn't recommened to use
> > > with valuable data.
> > >
> > > It tries its best though to avoid data corruption and possible damage to
> > > the card.
> > >
> > > Tested with MS DUO 64 MB card on Ricoh and Jmicron reader.
> > >
> > > Signed-off-by: Maxim Levitsky <maximlevitsky@gmail.com>
> >
> > Oh, this is interesting. Just now I looked for a solution for the old
> > MS with new JMicron controllers. (I should have checked mm tree
> > beforehand...)
> >
> > I'll give it a try and report back later.
>
> I tried your patch now.
> But got a WARNING and errors without working properly:
>
> [ 86.894497] WARNING: at drivers/memstick/core/ms_block.c:255 msb_run_state_machine+0x90/0xb0 [ms_block]()
> [ 86.894502] Hardware name:
> [ 86.894504] Modules linked in: ms_block(+) nls_iso8859_1 nls_cp437 vfat fat mmc_block netconsole configfs nfs lockd nfs_acl auth_rpcgss sunrpc af_packet i915 drm_kms_helper drm i2c_algo_bit snd_pcm_oss cpufreq_conservative snd_mixer_oss cpufreq_userspace snd_seq cpufreq_powersave acpi_cpufreq mperf binfmt_misc snd_seq_device edd ipv6 fuse loop dm_mod acpiphp pci_hotplug snd_hda_codec_hdmi snd_hda_codec_idt processor rtc_cmos snd_hda_intel snd_hda_codec snd_hwdep snd_pcm snd_timer uvcvideo video thermal thermal_sys snd rtc_core sr_mod soundcore snd_page_alloc sdhci_pci hwmon videodev cdrom r8169 rtc_lib v4l1_compat tpm_tis tpm intel_agp tpm_bios pcspkr hp_accel intel_gtt iTCO_wdt jmb38x_ms memstick sdhci mmc_core mii iTCO_vendor_support sg joydev lis3lv02d serio_raw i2c_core input_polldev output wmi container button battery ac ata_piix sd_mod crc_t10dif ehci_hcd ahci libahci libata usbcore scsi_mod ext3 jbd mbcache
> [ 86.895023] Pid: 4606, comm: modprobe Not tainted 2.6.37-rc5-test #2
> [ 86.895037] Call Trace:
> [ 86.895048] [<f84fa460>] ? msb_run_state_machine+0x90/0xb0 [ms_block]
> [ 86.895062] [<f84fa460>] ? msb_run_state_machine+0x90/0xb0 [ms_block]
> [ 86.895080] [<c0241d6c>] warn_slowpath_common+0x6c/0xa0
> [ 86.895092] [<f84fa460>] ? msb_run_state_machine+0x90/0xb0 [ms_block]
> [ 86.895105] [<f84fc280>] ? h_msb_reset+0x0/0x90 [ms_block]
> [ 86.895118] [<c0241dbd>] warn_slowpath_null+0x1d/0x20
> [ 86.895132] [<f84fa460>] msb_run_state_machine+0x90/0xb0 [ms_block]
> [ 86.895151] [<f84fa52c>] msb_reset+0xac/0x130 [ms_block]
> [ 86.895169] [<f84fc9db>] msb_probe+0x6b/0xcc8 [ms_block]
> [ 86.895175] [<c0337345>] ? sysfs_addrm_finish+0x15/0xc0
> [ 86.895187] [<c0337d75>] ? sysfs_do_create_link+0xb5/0x1b0
> [ 86.895191] [<c0337345>] ? sysfs_addrm_finish+0x15/0xc0
> [ 86.895212] [<f92ba344>] memstick_device_probe+0x34/0x50 [memstick]
> [ 86.895228] [<c042cbf5>] driver_probe_device+0x95/0x280
> [ 86.895233] [<c038be9a>] ? kobject_add_internal+0xca/0x210
> [ 86.895249] [<c0395543>] ? kvasprintf+0x43/0x60
> [ 86.895252] [<c042ce59>] __driver_attach+0x79/0x80
> [ 86.895256] [<c042c3d3>] bus_for_each_dev+0x53/0x80
> [ 86.895274] [<f92ba2b0>] ? memstick_device_remove+0x0/0x50 [memstick]
> [ 86.895278] [<c042c9f9>] driver_attach+0x19/0x20
> [ 86.895290] [<c042cde0>] ? __driver_attach+0x0/0x80
> [ 86.895292] [<c042bcc7>] bus_add_driver+0x197/0x280
> [ 86.895297] [<f92ba2b0>] ? memstick_device_remove+0x0/0x50 [memstick]
> [ 86.895308] [<c042d0f5>] driver_register+0x75/0x160
> [ 86.895313] [<c0381f18>] ? register_blkdev+0xf8/0x130
> [ 86.895326] [<f8234000>] ? msb_init+0x0/0x6d [ms_block]
> [ 86.895332] [<f92ba422>] memstick_register_driver+0x12/0x20 [memstick]
> [ 86.895337] [<f8234035>] msb_init+0x35/0x6d [ms_block]
> [ 86.895351] [<c020112b>] do_one_initcall+0x2b/0x160
> [ 86.895365] [<c0273943>] sys_init_module+0x93/0x1d0
> [ 86.895377] [<c0202d98>] sysenter_do_call+0x12/0x28
> [ 86.895381] ---[ end trace 0f1b7248462dd657 ]---
This turned to be an uninitialized state field. Trivial to fix.
> [ 86.895593] ms_block: Switch to parallel failed
> [ 86.908121] ms_block: Switch to parallel failed
>
> And "switch to parallel failed" continues endlessly until you unplug.
> Also, at another time, the machine got frozen with plugging.
This was because I'm using the new JMicron chip which doesn't like
the parallel mode. Reverting your commented-out code for checking
CAP_PAR4 bit makes it working. I attach the patch below.
Maybe it's safer to "parallel-mode-as-default" only for the old
JMicron chip using some quirk bits, etc.
thanks,
Takashi
---
drivers/memstick/core/ms_block.c | 11 ++++++-----
1 file changed, 6 insertions(+), 5 deletions(-)
--- a/drivers/memstick/core/ms_block.c
+++ b/drivers/memstick/core/ms_block.c
@@ -726,7 +726,7 @@
static int msb_reset(struct msb_data *msb, bool full)
{
- /*bool was_parallel = msb->regs.param.system & MEMSTICK_SYS_PAM; */
+ bool was_parallel = msb->regs.param.system & MEMSTICK_SYS_PAM;
struct memstick_dev *card = msb->card;
struct memstick_host *host = card->host;
int error;
@@ -762,8 +762,8 @@
}
/* Set parallel mode */
- /* if (was_parallel) */
- msb_switch_to_parallel(msb);
+ if (was_parallel)
+ msb_switch_to_parallel(msb);
return 0;
}
@@ -1673,7 +1673,7 @@
static int msb_init_card(struct memstick_dev *card)
{
struct msb_data *msb = memstick_get_drvdata(card);
- /*struct memstick_host *host = card->host;*/
+ struct memstick_host *host = card->host;
struct ms_boot_page *boot_block;
int error = 0, i, raw_size_in_megs;
@@ -1740,7 +1740,7 @@
/* Due to a bug in Jmicron driver, its serial mode barely works
so we switch to parallel mode right away */
-#if 0
+#if 1
/* Now we can switch the interface */
if (host->caps & msb->caps & MEMSTICK_CAP_PAR4)
msb_switch_to_parallel(msb);
@@ -2184,6 +2184,7 @@
msb = kzalloc(sizeof(struct msb_data), GFP_KERNEL);
if (!msb)
return -ENOMEM;
+ msb->state = -1;
memstick_set_drvdata(card, msb);
msb->card = card;
spin_lock_init(&msb->q_lock);
^ permalink raw reply [flat|nested] 51+ messages in thread
* Re: [PATCH 1/2] memstick: add support for legacy memorysticks
2010-12-09 2:42 ` [PATCH 1/2] memstick: add support for legacy memorysticks Maxim Levitsky
2010-12-09 7:19 ` Takashi Iwai
@ 2010-12-09 21:09 ` Andrew Morton
2010-12-10 22:27 ` Maxim Levitsky
1 sibling, 1 reply; 51+ messages in thread
From: Andrew Morton @ 2010-12-09 21:09 UTC (permalink / raw)
To: Maxim Levitsky; +Cc: linux-kernel, Alex Dubov, Takashi Iwai
On Thu, 9 Dec 2010 04:42:25 +0200
Maxim Levitsky <maximlevitsky@gmail.com> wrote:
> +/*
> + * Advance scatterlist by 'consumed' bytes
> + * Returns new scatterlist, or NULL if can't advance that much
> + */
> +static struct scatterlist *sg_advance(struct scatterlist *sg, int consumed)
> +{
> + while (consumed >= sg->length) {
> + consumed -= sg->length;
> +
> + sg = sg_next(sg);
> + if (!sg)
> + break;
> + }
> +
> + WARN_ON(!sg && consumed);
> +
> + if (!sg)
> + return NULL;
> +
> + sg->offset += consumed;
> + sg->length -= consumed;
> +
> + if (sg->offset >= PAGE_SIZE) {
> + struct page *page =
> + nth_page(sg_page(sg), sg->offset / PAGE_SIZE);
> + sg_set_page(sg, page, sg->length, sg->offset % PAGE_SIZE);
> + }
> +
> + return sg;
> +}
> +
> +/* Calculate number of sg entries in sg list */
> +static int sg_nents(struct scatterlist *sg)
> +{
> + int nents = 0;
> + while (sg) {
> + nents++;
> + sg = sg_next(sg);
> + }
> +
> + return nents;
> +}
> +
> +/* Calculate total lenght of scatterlist */
(typo)
> +static int sg_total_len(struct scatterlist *sg)
> +{
> + int len = 0;
> + while (sg) {
> + len += sg->length;
> + sg = sg_next(sg);
> + }
> + return len;
> +}
> +
> +/* Compare contents of an sg to a buffer */
> +static bool sg_compare_to_buffer(struct scatterlist *sg, u8 *buffer, size_t len)
> +{
> + unsigned long flags;
> + int retval = 0;
> + struct sg_mapping_iter miter;
> +
> + if (sg_total_len(sg) < len)
> + return 1;
> +
> + local_irq_save(flags);
> + sg_miter_start(&miter, sg, sg_nents(sg),
> + SG_MITER_ATOMIC | SG_MITER_FROM_SG);
> +
> + while (sg_miter_next(&miter) && len > 0) {
> +
> + int cmplen = min(miter.length, len);
> + if (memcmp(miter.addr, buffer, cmplen)) {
> + retval = 1;
> + break;
> + }
> +
> + buffer += cmplen;
> + len -= cmplen;
> + }
> +
> + sg_miter_stop(&miter);
> + local_irq_restore(flags);
> + return retval;
> +}
I think I mentioned this before, but ... it's really bad that the
above functions appear in a particular device driver. They are, to
varying degrees, independent of that driver and should be provided by
the sglist core code.
Also, the local_irq_save/restore in sg_compare_to_buffer() is quite
mysterious and should have a comment explaining why it is there. Once
that's done, I might understand why it isn't a bug on SMP machines ;)
^ permalink raw reply [flat|nested] 51+ messages in thread
* Re: MEMSTICK: Add my 2 drivers
2010-12-09 7:15 ` Takashi Iwai
@ 2010-12-10 22:23 ` Maxim Levitsky
0 siblings, 0 replies; 51+ messages in thread
From: Maxim Levitsky @ 2010-12-10 22:23 UTC (permalink / raw)
To: Takashi Iwai; +Cc: Alex Dubov, linux-kernel, Andrew Morton
On Thu, 2010-12-09 at 08:15 +0100, Takashi Iwai wrote:
> At Wed, 8 Dec 2010 18:44:04 -0800 (PST),
> Alex Dubov wrote:
> >
> > >
> > > Since I don't touch (and don't want anymore to) any code
> > > written by
> > > Alex, and I even added workarounds for bugs in his jmicron
> > > driver to the
> > > generic ms_block.c code (it tested and works on my both
> > > card readers),
> > > maybe even add these to 2.6.37?
> >
> > This are worked around by the recent patches, are not they?
>
> Do you mean that the support for old memstick media was added / worked
> around in the recent patches? That would be great. It's what I was
> looking for right now. If so, could you point your patches?
No, Alex is talking about your patches.
Best regards,
Maxim Levitsky
^ permalink raw reply [flat|nested] 51+ messages in thread
* Re: [PATCH 1/2] memstick: add support for legacy memorysticks
2010-12-09 21:09 ` Andrew Morton
@ 2010-12-10 22:27 ` Maxim Levitsky
0 siblings, 0 replies; 51+ messages in thread
From: Maxim Levitsky @ 2010-12-10 22:27 UTC (permalink / raw)
To: Andrew Morton; +Cc: linux-kernel, Alex Dubov, Takashi Iwai
On Thu, 2010-12-09 at 13:09 -0800, Andrew Morton wrote:
> On Thu, 9 Dec 2010 04:42:25 +0200
> Maxim Levitsky <maximlevitsky@gmail.com> wrote:
>
> > +/*
> > + * Advance scatterlist by 'consumed' bytes
> > + * Returns new scatterlist, or NULL if can't advance that much
> > + */
> > +static struct scatterlist *sg_advance(struct scatterlist *sg, int consumed)
> > +{
> > + while (consumed >= sg->length) {
> > + consumed -= sg->length;
> > +
> > + sg = sg_next(sg);
> > + if (!sg)
> > + break;
> > + }
> > +
> > + WARN_ON(!sg && consumed);
> > +
> > + if (!sg)
> > + return NULL;
> > +
> > + sg->offset += consumed;
> > + sg->length -= consumed;
> > +
> > + if (sg->offset >= PAGE_SIZE) {
> > + struct page *page =
> > + nth_page(sg_page(sg), sg->offset / PAGE_SIZE);
> > + sg_set_page(sg, page, sg->length, sg->offset % PAGE_SIZE);
> > + }
> > +
> > + return sg;
> > +}
> > +
> > +/* Calculate number of sg entries in sg list */
> > +static int sg_nents(struct scatterlist *sg)
> > +{
> > + int nents = 0;
> > + while (sg) {
> > + nents++;
> > + sg = sg_next(sg);
> > + }
> > +
> > + return nents;
> > +}
> > +
> > +/* Calculate total lenght of scatterlist */
>
> (typo)
>
> > +static int sg_total_len(struct scatterlist *sg)
> > +{
> > + int len = 0;
> > + while (sg) {
> > + len += sg->length;
> > + sg = sg_next(sg);
> > + }
> > + return len;
> > +}
> > +
> > +/* Compare contents of an sg to a buffer */
> > +static bool sg_compare_to_buffer(struct scatterlist *sg, u8 *buffer, size_t len)
> > +{
> > + unsigned long flags;
> > + int retval = 0;
> > + struct sg_mapping_iter miter;
> > +
> > + if (sg_total_len(sg) < len)
> > + return 1;
> > +
> > + local_irq_save(flags);
> > + sg_miter_start(&miter, sg, sg_nents(sg),
> > + SG_MITER_ATOMIC | SG_MITER_FROM_SG);
> > +
> > + while (sg_miter_next(&miter) && len > 0) {
> > +
> > + int cmplen = min(miter.length, len);
> > + if (memcmp(miter.addr, buffer, cmplen)) {
> > + retval = 1;
> > + break;
> > + }
> > +
> > + buffer += cmplen;
> > + len -= cmplen;
> > + }
> > +
> > + sg_miter_stop(&miter);
> > + local_irq_restore(flags);
> > + return retval;
> > +}
>
> I think I mentioned this before, but ... it's really bad that the
> above functions appear in a particular device driver. They are, to
> varying degrees, independent of that driver and should be provided by
> the sglist core code.
I agree with you.
However, note that Alex's mspro_blk.c, also contains the above logic, it
is just not separated from the code.
I don't mind adding these to scatterlist code.
>
> Also, the local_irq_save/restore in sg_compare_to_buffer() is quite
> mysterious and should have a comment explaining why it is there. Once
> that's done, I might understand why it isn't a bug on SMP machines ;)
Its is done to be able to use kmap_atomic.
I think I can use sg_mitter there, I will try to.
Best regards,
Maxim Levitsky.
^ permalink raw reply [flat|nested] 51+ messages in thread
* Re: [PATCH 1/2] memstick: add support for legacy memorysticks
2010-12-09 15:44 ` Takashi Iwai
@ 2010-12-10 22:34 ` Maxim Levitsky
0 siblings, 0 replies; 51+ messages in thread
From: Maxim Levitsky @ 2010-12-10 22:34 UTC (permalink / raw)
To: Takashi Iwai; +Cc: linux-kernel, Alex Dubov, Andrew Morton, Aries Lee
On Thu, 2010-12-09 at 16:44 +0100, Takashi Iwai wrote:
> At Thu, 09 Dec 2010 12:56:44 +0100,
> Takashi Iwai wrote:
> >
> > At Thu, 09 Dec 2010 08:19:47 +0100,
> > Takashi Iwai wrote:
> > >
> > > At Thu, 9 Dec 2010 04:42:25 +0200,
> > > Maxim Levitsky wrote:
> > > >
> > > > Based partialy on spec quotes from Alex Dubov.
> > > >
> > > > As any code that works with user data this driver isn't recommened to use
> > > > with valuable data.
> > > >
> > > > It tries its best though to avoid data corruption and possible damage to
> > > > the card.
> > > >
> > > > Tested with MS DUO 64 MB card on Ricoh and Jmicron reader.
> > > >
> > > > Signed-off-by: Maxim Levitsky <maximlevitsky@gmail.com>
> > >
> > > Oh, this is interesting. Just now I looked for a solution for the old
> > > MS with new JMicron controllers. (I should have checked mm tree
> > > beforehand...)
> > >
> > > I'll give it a try and report back later.
> >
> > I tried your patch now.
> > But got a WARNING and errors without working properly:
> >
> > [ 86.894497] WARNING: at drivers/memstick/core/ms_block.c:255 msb_run_state_machine+0x90/0xb0 [ms_block]()
> > [ 86.894502] Hardware name:
> > [ 86.894504] Modules linked in: ms_block(+) nls_iso8859_1 nls_cp437 vfat fat mmc_block netconsole configfs nfs lockd nfs_acl auth_rpcgss sunrpc af_packet i915 drm_kms_helper drm i2c_algo_bit snd_pcm_oss cpufreq_conservative snd_mixer_oss cpufreq_userspace snd_seq cpufreq_powersave acpi_cpufreq mperf binfmt_misc snd_seq_device edd ipv6 fuse loop dm_mod acpiphp pci_hotplug snd_hda_codec_hdmi snd_hda_codec_idt processor rtc_cmos snd_hda_intel snd_hda_codec snd_hwdep snd_pcm snd_timer uvcvideo video thermal thermal_sys snd rtc_core sr_mod soundcore snd_page_alloc sdhci_pci hwmon videodev cdrom r8169 rtc_lib v4l1_compat tpm_tis tpm intel_agp tpm_bios pcspkr hp_accel intel_gtt iTCO_wdt jmb38x_ms memstick sdhci mmc_core mii iTCO_vendor_support sg joydev lis3lv02d serio_raw i2c_core input_polldev output wmi container button battery ac ata_piix sd_mod crc_t10dif ehci_hcd ahci libahci libata usbcore scsi_mod ext3 jbd mbcache
> > [ 86.895023] Pid: 4606, comm: modprobe Not tainted 2.6.37-rc5-test #2
> > [ 86.895037] Call Trace:
> > [ 86.895048] [<f84fa460>] ? msb_run_state_machine+0x90/0xb0 [ms_block]
> > [ 86.895062] [<f84fa460>] ? msb_run_state_machine+0x90/0xb0 [ms_block]
> > [ 86.895080] [<c0241d6c>] warn_slowpath_common+0x6c/0xa0
> > [ 86.895092] [<f84fa460>] ? msb_run_state_machine+0x90/0xb0 [ms_block]
> > [ 86.895105] [<f84fc280>] ? h_msb_reset+0x0/0x90 [ms_block]
> > [ 86.895118] [<c0241dbd>] warn_slowpath_null+0x1d/0x20
> > [ 86.895132] [<f84fa460>] msb_run_state_machine+0x90/0xb0 [ms_block]
> > [ 86.895151] [<f84fa52c>] msb_reset+0xac/0x130 [ms_block]
> > [ 86.895169] [<f84fc9db>] msb_probe+0x6b/0xcc8 [ms_block]
> > [ 86.895175] [<c0337345>] ? sysfs_addrm_finish+0x15/0xc0
> > [ 86.895187] [<c0337d75>] ? sysfs_do_create_link+0xb5/0x1b0
> > [ 86.895191] [<c0337345>] ? sysfs_addrm_finish+0x15/0xc0
> > [ 86.895212] [<f92ba344>] memstick_device_probe+0x34/0x50 [memstick]
> > [ 86.895228] [<c042cbf5>] driver_probe_device+0x95/0x280
> > [ 86.895233] [<c038be9a>] ? kobject_add_internal+0xca/0x210
> > [ 86.895249] [<c0395543>] ? kvasprintf+0x43/0x60
> > [ 86.895252] [<c042ce59>] __driver_attach+0x79/0x80
> > [ 86.895256] [<c042c3d3>] bus_for_each_dev+0x53/0x80
> > [ 86.895274] [<f92ba2b0>] ? memstick_device_remove+0x0/0x50 [memstick]
> > [ 86.895278] [<c042c9f9>] driver_attach+0x19/0x20
> > [ 86.895290] [<c042cde0>] ? __driver_attach+0x0/0x80
> > [ 86.895292] [<c042bcc7>] bus_add_driver+0x197/0x280
> > [ 86.895297] [<f92ba2b0>] ? memstick_device_remove+0x0/0x50 [memstick]
> > [ 86.895308] [<c042d0f5>] driver_register+0x75/0x160
> > [ 86.895313] [<c0381f18>] ? register_blkdev+0xf8/0x130
> > [ 86.895326] [<f8234000>] ? msb_init+0x0/0x6d [ms_block]
> > [ 86.895332] [<f92ba422>] memstick_register_driver+0x12/0x20 [memstick]
> > [ 86.895337] [<f8234035>] msb_init+0x35/0x6d [ms_block]
> > [ 86.895351] [<c020112b>] do_one_initcall+0x2b/0x160
> > [ 86.895365] [<c0273943>] sys_init_module+0x93/0x1d0
> > [ 86.895377] [<c0202d98>] sysenter_do_call+0x12/0x28
> > [ 86.895381] ---[ end trace 0f1b7248462dd657 ]---
>
> This turned to be an uninitialized state field. Trivial to fix.
>
> > [ 86.895593] ms_block: Switch to parallel failed
> > [ 86.908121] ms_block: Switch to parallel failed
> >
> > And "switch to parallel failed" continues endlessly until you unplug.
> > Also, at another time, the machine got frozen with plugging.
>
> This was because I'm using the new JMicron chip which doesn't like
> the parallel mode. Reverting your commented-out code for checking
> CAP_PAR4 bit makes it working. I attach the patch below.
It is commented out because on my Jmicron, it doesn't work in serial
mode.
I fixed this in my patch series, but like I said, due to alex attitude,
I don't want to fix that code.
Probably by commenting out the code, I introduced run away recursion in
the code if switch fails.
Takashi, you are welcome to look at my version of code, the one that
Alex mostly refuses to merge.
It is located at http://gitorious.org/ricoh-kernel/ricoh-kernel
(This contains everything I did for ricoh and jmicron)
Try this code and see if MS standard works for you in parallel mode.
If it doesn't, every module contains built-in debugging, activated by
debug=1 or 2 module parameter, yet another thing Alex refused to
consider.
Best regards,
Maxim Levitsky
>
> Maybe it's safer to "parallel-mode-as-default" only for the old
> JMicron chip using some quirk bits, etc.
>
>
> thanks,
>
> Takashi
>
> ---
> drivers/memstick/core/ms_block.c | 11 ++++++-----
> 1 file changed, 6 insertions(+), 5 deletions(-)
>
> --- a/drivers/memstick/core/ms_block.c
> +++ b/drivers/memstick/core/ms_block.c
> @@ -726,7 +726,7 @@
> static int msb_reset(struct msb_data *msb, bool full)
> {
>
> - /*bool was_parallel = msb->regs.param.system & MEMSTICK_SYS_PAM; */
> + bool was_parallel = msb->regs.param.system & MEMSTICK_SYS_PAM;
> struct memstick_dev *card = msb->card;
> struct memstick_host *host = card->host;
> int error;
> @@ -762,8 +762,8 @@
> }
>
> /* Set parallel mode */
> - /* if (was_parallel) */
> - msb_switch_to_parallel(msb);
> + if (was_parallel)
> + msb_switch_to_parallel(msb);
> return 0;
> }
>
> @@ -1673,7 +1673,7 @@
> static int msb_init_card(struct memstick_dev *card)
> {
> struct msb_data *msb = memstick_get_drvdata(card);
> - /*struct memstick_host *host = card->host;*/
> + struct memstick_host *host = card->host;
> struct ms_boot_page *boot_block;
> int error = 0, i, raw_size_in_megs;
>
> @@ -1740,7 +1740,7 @@
>
> /* Due to a bug in Jmicron driver, its serial mode barely works
> so we switch to parallel mode right away */
> -#if 0
> +#if 1
> /* Now we can switch the interface */
> if (host->caps & msb->caps & MEMSTICK_CAP_PAR4)
> msb_switch_to_parallel(msb);
> @@ -2184,6 +2184,7 @@
> msb = kzalloc(sizeof(struct msb_data), GFP_KERNEL);
> if (!msb)
> return -ENOMEM;
> + msb->state = -1;
> memstick_set_drvdata(card, msb);
> msb->card = card;
> spin_lock_init(&msb->q_lock);
^ permalink raw reply [flat|nested] 51+ messages in thread
end of thread, other threads:[~2010-12-10 22:34 UTC | newest]
Thread overview: 51+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-12-09 2:39 MEMSTICK: Add my 2 drivers Maxim Levitsky
2010-12-09 2:42 ` [PATCH 1/2] memstick: add support for legacy memorysticks Maxim Levitsky
2010-12-09 7:19 ` Takashi Iwai
2010-12-09 11:56 ` Takashi Iwai
2010-12-09 15:44 ` Takashi Iwai
2010-12-10 22:34 ` Maxim Levitsky
2010-12-09 21:09 ` Andrew Morton
2010-12-10 22:27 ` Maxim Levitsky
2010-12-09 2:42 ` [PATCH 2/2] memstick: Add driver for Ricoh R5C592 card reader Maxim Levitsky
2010-12-09 2:44 ` MEMSTICK: Add my 2 drivers Alex Dubov
2010-12-09 2:48 ` Maxim Levitsky
2010-12-09 7:15 ` Takashi Iwai
2010-12-10 22:23 ` Maxim Levitsky
-- strict thread matches above, loose matches on Subject: below --
2010-08-05 8:30 [PATCH 2/2] MEMSTICK: Add driver for Ricoh R5C592 Card reader Alex Dubov
2010-08-05 11:20 ` Maxim Levitsky
2010-08-05 11:48 ` Alex Dubov
2010-08-05 12:30 ` Maxim Levitsky
2010-08-05 17:47 ` Maxim Levitsky
2010-08-06 7:43 ` Alex Dubov
2010-08-06 10:56 ` Maxim Levitsky
2010-08-07 13:15 ` Alex Dubov
2010-08-07 15:58 ` Maxim Levitsky
2010-08-08 13:31 ` Alex Dubov
2010-08-06 8:01 ` Alex Dubov
2010-08-05 12:46 ` Maxim Levitsky
2010-08-06 7:59 ` Alex Dubov
2010-08-06 10:59 ` Maxim Levitsky
2010-08-07 13:12 ` Alex Dubov
2010-08-07 16:03 ` Maxim Levitsky
2010-08-08 13:33 ` Alex Dubov
2010-08-07 20:22 ` Maxim Levitsky
2010-08-08 14:26 ` Alex Dubov
2010-08-08 15:07 ` Maxim Levitsky
2010-08-08 20:08 ` Maxim Levitsky
2010-08-09 6:31 ` Alex Dubov
2010-08-09 6:56 ` Maxim Levitsky
2010-08-09 15:30 ` Maxim Levitsky
2010-08-10 8:12 ` Alex Dubov
2010-08-10 9:47 ` Maxim Levitsky
2010-08-11 8:08 ` Alex Dubov
2010-08-11 8:32 ` Maxim Levitsky
2010-08-12 7:22 ` Alex Dubov
2010-08-12 7:58 ` Maxim Levitsky
2010-08-09 19:19 ` Maxim Levitsky
2010-08-10 7:53 ` Alex Dubov
2010-08-10 9:50 ` Maxim Levitsky
2010-08-11 8:16 ` Alex Dubov
2010-08-03 14:53 [PATCH 0/2] Driver for Ricoh cardreader Maxim Levitsky
2010-08-03 14:53 ` [PATCH 2/2] MEMSTICK: Add driver for Ricoh R5C592 Card reader Maxim Levitsky
2010-08-04 7:57 ` Alex Dubov
2010-08-04 16:48 ` Maxim Levitsky
2010-08-04 19:31 ` Maxim Levitsky
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox