diff options
Diffstat (limited to 'drivers')
-rw-r--r-- | drivers/Kconfig | 2 | ||||
-rw-r--r-- | drivers/Makefile | 1 | ||||
-rw-r--r-- | drivers/memstick/Kconfig | 26 | ||||
-rw-r--r-- | drivers/memstick/Makefile | 11 | ||||
-rw-r--r-- | drivers/memstick/core/Kconfig | 26 | ||||
-rw-r--r-- | drivers/memstick/core/Makefile | 11 | ||||
-rw-r--r-- | drivers/memstick/core/memstick.c | 614 | ||||
-rw-r--r-- | drivers/memstick/core/mspro_block.c | 1351 | ||||
-rw-r--r-- | drivers/memstick/host/Kconfig | 22 | ||||
-rw-r--r-- | drivers/memstick/host/Makefile | 10 | ||||
-rw-r--r-- | drivers/memstick/host/tifm_ms.c | 685 | ||||
-rw-r--r-- | drivers/misc/tifm_7xx1.c | 17 | ||||
-rw-r--r-- | drivers/misc/tifm_core.c | 7 |
13 files changed, 2783 insertions, 0 deletions
diff --git a/drivers/Kconfig b/drivers/Kconfig index b86877bdc7a..3a0e3549739 100644 --- a/drivers/Kconfig +++ b/drivers/Kconfig @@ -80,6 +80,8 @@ source "drivers/usb/Kconfig" source "drivers/mmc/Kconfig" +source "drivers/memstick/Kconfig" + source "drivers/leds/Kconfig" source "drivers/infiniband/Kconfig" diff --git a/drivers/Makefile b/drivers/Makefile index 30ba97ec5eb..e5e394a7e6c 100644 --- a/drivers/Makefile +++ b/drivers/Makefile @@ -78,6 +78,7 @@ obj-y += lguest/ obj-$(CONFIG_CPU_FREQ) += cpufreq/ obj-$(CONFIG_CPU_IDLE) += cpuidle/ obj-$(CONFIG_MMC) += mmc/ +obj-$(CONFIG_MEMSTICK) += memstick/ obj-$(CONFIG_NEW_LEDS) += leds/ obj-$(CONFIG_INFINIBAND) += infiniband/ obj-$(CONFIG_SGI_SN) += sn/ diff --git a/drivers/memstick/Kconfig b/drivers/memstick/Kconfig new file mode 100644 index 00000000000..1093fdb0729 --- /dev/null +++ b/drivers/memstick/Kconfig @@ -0,0 +1,26 @@ +# +# MemoryStick subsystem configuration +# + +menuconfig MEMSTICK + tristate "Sony MemoryStick card support (EXPERIMENTAL)" + help + Sony MemoryStick is a proprietary storage/extension card protocol. + + If you want MemoryStick support, you should say Y here and also + to the specific driver for your MMC interface. + +if MEMSTICK + +config MEMSTICK_DEBUG + bool "MemoryStick debugging" + help + This is an option for use by developers; most people should + say N here. This enables MemoryStick core and driver debugging. + + +source "drivers/memstick/core/Kconfig" + +source "drivers/memstick/host/Kconfig" + +endif # MEMSTICK diff --git a/drivers/memstick/Makefile b/drivers/memstick/Makefile new file mode 100644 index 00000000000..dc160fb4351 --- /dev/null +++ b/drivers/memstick/Makefile @@ -0,0 +1,11 @@ +# +# Makefile for the kernel MemoryStick device drivers. +# + +ifeq ($(CONFIG_MEMSTICK_DEBUG),y) + EXTRA_CFLAGS += -DDEBUG +endif + +obj-$(CONFIG_MEMSTICK) += core/ +obj-$(CONFIG_MEMSTICK) += host/ + diff --git a/drivers/memstick/core/Kconfig b/drivers/memstick/core/Kconfig new file mode 100644 index 00000000000..95f1814b536 --- /dev/null +++ b/drivers/memstick/core/Kconfig @@ -0,0 +1,26 @@ +# +# MemoryStick core configuration +# + +comment "MemoryStick drivers" + +config MEMSTICK_UNSAFE_RESUME + bool "Allow unsafe resume (DANGEROUS)" + help + If you say Y here, the MemoryStick layer will assume that all + cards stayed in their respective slots during the suspend. The + normal behaviour is to remove them at suspend and + redetecting them at resume. Breaking this assumption will + in most cases result in data corruption. + + This option is usually just for embedded systems which use + a MemoryStick card for rootfs. Most people should say N here. + +config MSPRO_BLOCK + tristate "MemoryStick Pro block device driver" + depends on BLOCK + help + Say Y here to enable the MemoryStick Pro block device driver + 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. diff --git a/drivers/memstick/core/Makefile b/drivers/memstick/core/Makefile new file mode 100644 index 00000000000..8b2b5293877 --- /dev/null +++ b/drivers/memstick/core/Makefile @@ -0,0 +1,11 @@ +# +# Makefile for the kernel MemoryStick core. +# + +ifeq ($(CONFIG_MEMSTICK_DEBUG),y) + EXTRA_CFLAGS += -DDEBUG +endif + +obj-$(CONFIG_MEMSTICK) += memstick.o + +obj-$(CONFIG_MSPRO_BLOCK) += mspro_block.o diff --git a/drivers/memstick/core/memstick.c b/drivers/memstick/core/memstick.c new file mode 100644 index 00000000000..bba467fe4bc --- /dev/null +++ b/drivers/memstick/core/memstick.c @@ -0,0 +1,614 @@ +/* + * Sony MemoryStick support + * + * Copyright (C) 2007 Alex Dubov <oakad@yahoo.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. + * + * Special thanks to Carlos Corbacho for providing various MemoryStick cards + * that made this driver possible. + * + */ + +#include <linux/memstick.h> +#include <linux/idr.h> +#include <linux/fs.h> +#include <linux/delay.h> + +#define DRIVER_NAME "memstick" +#define DRIVER_VERSION "0.2" + +static unsigned int cmd_retries = 3; +module_param(cmd_retries, uint, 0644); + +static struct workqueue_struct *workqueue; +static DEFINE_IDR(memstick_host_idr); +static DEFINE_SPINLOCK(memstick_host_lock); + +static int memstick_dev_match(struct memstick_dev *card, + struct memstick_device_id *id) +{ + if (id->match_flags & MEMSTICK_MATCH_ALL) { + if ((id->type == card->id.type) + && (id->category == card->id.category) + && (id->class == card->id.class)) + return 1; + } + + return 0; +} + +static int memstick_bus_match(struct device *dev, struct device_driver *drv) +{ + struct memstick_dev *card = container_of(dev, struct memstick_dev, + dev); + struct memstick_driver *ms_drv = container_of(drv, + struct memstick_driver, + driver); + struct memstick_device_id *ids = ms_drv->id_table; + + if (ids) { + while (ids->match_flags) { + if (memstick_dev_match(card, ids)) + return 1; + ++ids; + } + } + return 0; +} + +static int memstick_uevent(struct device *dev, struct kobj_uevent_env *env) +{ + struct memstick_dev *card = container_of(dev, struct memstick_dev, + dev); + + if (add_uevent_var(env, "MEMSTICK_TYPE=%02X", card->id.type)) + return -ENOMEM; + + if (add_uevent_var(env, "MEMSTICK_CATEGORY=%02X", card->id.category)) + return -ENOMEM; + + if (add_uevent_var(env, "MEMSTICK_CLASS=%02X", card->id.class)) + return -ENOMEM; + + return 0; +} + +static int memstick_device_probe(struct device *dev) +{ + struct memstick_dev *card = container_of(dev, struct memstick_dev, + dev); + struct memstick_driver *drv = container_of(dev->driver, + struct memstick_driver, + driver); + int rc = -ENODEV; + + if (dev->driver && drv->probe) { + rc = drv->probe(card); + if (!rc) + get_device(dev); + } + return rc; +} + +static int memstick_device_remove(struct device *dev) +{ + struct memstick_dev *card = container_of(dev, struct memstick_dev, + dev); + struct memstick_driver *drv = container_of(dev->driver, + struct memstick_driver, + driver); + + if (dev->driver && drv->remove) { + drv->remove(card); + card->dev.driver = NULL; + } + + put_device(dev); + return 0; +} + +#ifdef CONFIG_PM + +static int memstick_device_suspend(struct device *dev, pm_message_t state) +{ + struct memstick_dev *card = container_of(dev, struct memstick_dev, + dev); + struct memstick_driver *drv = container_of(dev->driver, + struct memstick_driver, + driver); + + if (dev->driver && drv->suspend) + return drv->suspend(card, state); + return 0; +} + +static int memstick_device_resume(struct device *dev) +{ + struct memstick_dev *card = container_of(dev, struct memstick_dev, + dev); + struct memstick_driver *drv = container_of(dev->driver, + struct memstick_driver, + driver); + + if (dev->driver && drv->resume) + return drv->resume(card); + return 0; +} + +#else + +#define memstick_device_suspend NULL +#define memstick_device_resume NULL + +#endif /* CONFIG_PM */ + +#define MEMSTICK_ATTR(name, format) \ +static ssize_t name##_show(struct device *dev, struct device_attribute *attr, \ + char *buf) \ +{ \ + struct memstick_dev *card = container_of(dev, struct memstick_dev, \ + dev); \ + return sprintf(buf, format, card->id.name); \ +} + +MEMSTICK_ATTR(type, "%02X"); +MEMSTICK_ATTR(category, "%02X"); +MEMSTICK_ATTR(class, "%02X"); + +#define MEMSTICK_ATTR_RO(name) __ATTR(name, S_IRUGO, name##_show, NULL) + +static struct device_attribute memstick_dev_attrs[] = { + MEMSTICK_ATTR_RO(type), + MEMSTICK_ATTR_RO(category), + MEMSTICK_ATTR_RO(class), + __ATTR_NULL +}; + +static struct bus_type memstick_bus_type = { + .name = "memstick", + .dev_attrs = memstick_dev_attrs, + .match = memstick_bus_match, + .uevent = memstick_uevent, + .probe = memstick_device_probe, + .remove = memstick_device_remove, + .suspend = memstick_device_suspend, + .resume = memstick_device_resume +}; + +static void memstick_free(struct class_device *cdev) +{ + struct memstick_host *host = container_of(cdev, struct memstick_host, + cdev); + kfree(host); +} + +static struct class memstick_host_class = { + .name = "memstick_host", + .release = memstick_free +}; + +static void memstick_free_card(struct device *dev) +{ + struct memstick_dev *card = container_of(dev, struct memstick_dev, + dev); + kfree(card); +} + +static int memstick_dummy_check(struct memstick_dev *card) +{ + return 0; +} + +/** + * memstick_detect_change - schedule media detection on memstick host + * @host - host to use + */ +void memstick_detect_change(struct memstick_host *host) +{ + queue_work(workqueue, &host->media_checker); +} +EXPORT_SYMBOL(memstick_detect_change); + +/** + * memstick_next_req - called by host driver to obtain next request to process + * @host - host to use + * @mrq - pointer to stick the request to + * + * Host calls this function from idle state (*mrq == NULL) or after finishing + * previous request (*mrq should point to it). If previous request was + * unsuccessful, it is retried for predetermined number of times. Return value + * of 0 means that new request was assigned to the host. + */ +int memstick_next_req(struct memstick_host *host, struct memstick_request **mrq) +{ + int rc = -ENXIO; + + if ((*mrq) && (*mrq)->error && host->retries) { + (*mrq)->error = rc; + host->retries--; + return 0; + } + + if (host->card && host->card->next_request) + rc = host->card->next_request(host->card, mrq); + + if (!rc) + host->retries = cmd_retries; + else + *mrq = NULL; + + return rc; +} +EXPORT_SYMBOL(memstick_next_req); + +/** + * memstick_new_req - notify the host that some requests are pending + * @host - host to use + */ +void memstick_new_req(struct memstick_host *host) +{ + host->retries = cmd_retries; + host->request(host); +} +EXPORT_SYMBOL(memstick_new_req); + +/** + * memstick_init_req_sg - set request fields needed for bulk data transfer + * @mrq - request to use + * @tpc - memstick Transport Protocol Command + * @sg - TPC argument + */ +void memstick_init_req_sg(struct memstick_request *mrq, unsigned char tpc, + struct scatterlist *sg) +{ + mrq->tpc = tpc; + if (tpc & 8) + mrq->data_dir = WRITE; + else + mrq->data_dir = READ; + + mrq->sg = *sg; + mrq->io_type = MEMSTICK_IO_SG; + + if (tpc == MS_TPC_SET_CMD || tpc == MS_TPC_EX_SET_CMD) + mrq->need_card_int = 1; + else + mrq->need_card_int = 0; + + mrq->get_int_reg = 0; +} +EXPORT_SYMBOL(memstick_init_req_sg); + +/** + * memstick_init_req - set request fields needed for short data transfer + * @mrq - request to use + * @tpc - memstick Transport Protocol Command + * @buf - TPC argument buffer + * @length - TPC argument size + * + * The intended use of this function (transfer of data items several bytes + * in size) allows us to just copy the value between request structure and + * user supplied buffer. + */ +void memstick_init_req(struct memstick_request *mrq, unsigned char tpc, + void *buf, size_t length) +{ + mrq->tpc = tpc; + if (tpc & 8) + mrq->data_dir = WRITE; + else + mrq->data_dir = READ; + + mrq->data_len = length > sizeof(mrq->data) ? sizeof(mrq->data) : length; + if (mrq->data_dir == WRITE) + memcpy(mrq->data, buf, mrq->data_len); + + mrq->io_type = MEMSTICK_IO_VAL; + + if (tpc == MS_TPC_SET_CMD || tpc == MS_TPC_EX_SET_CMD) + mrq->need_card_int = 1; + else + mrq->need_card_int = 0; + + mrq->get_int_reg = 0; +} +EXPORT_SYMBOL(memstick_init_req); + +/* + * Functions prefixed with "h_" are protocol callbacks. They can be called from + * interrupt context. Return value of 0 means that request processing is still + * ongoing, while special error value of -EAGAIN means that current request is + * finished (and request processor should come back some time later). + */ + +static int h_memstick_read_dev_id(struct memstick_dev *card, + struct memstick_request **mrq) +{ + struct ms_id_register id_reg; + + if (!(*mrq)) { + memstick_init_req(&card->current_mrq, MS_TPC_READ_REG, NULL, + sizeof(struct ms_id_register)); + *mrq = &card->current_mrq; + return 0; + } else { + if (!(*mrq)->error) { + memcpy(&id_reg, (*mrq)->data, sizeof(id_reg)); + card->id.match_flags = MEMSTICK_MATCH_ALL; + card->id.type = id_reg.type; + card->id.category = id_reg.category; + card->id.class = id_reg.class; + } + complete(&card->mrq_complete); + return -EAGAIN; + } +} + +static int h_memstick_set_rw_addr(struct memstick_dev *card, + struct memstick_request **mrq) +{ + if (!(*mrq)) { + memstick_init_req(&card->current_mrq, MS_TPC_SET_RW_REG_ADRS, + (char *)&card->reg_addr, + sizeof(card->reg_addr)); + *mrq = &card->current_mrq; + return 0; + } else { + complete(&card->mrq_complete); + return -EAGAIN; + } +} + +/** + * memstick_set_rw_addr - issue SET_RW_REG_ADDR request and wait for it to + * complete + * @card - media device to use + */ +int memstick_set_rw_addr(struct memstick_dev *card) +{ + card->next_request = h_memstick_set_rw_addr; + memstick_new_req(card->host); + wait_for_completion(&card->mrq_complete); + + return card->current_mrq.error; +} +EXPORT_SYMBOL(memstick_set_rw_addr); + +static struct memstick_dev *memstick_alloc_card(struct memstick_host *host) +{ + struct memstick_dev *card = kzalloc(sizeof(struct memstick_dev), + GFP_KERNEL); + struct memstick_dev *old_card = host->card; + struct ms_id_register id_reg; + + if (card) { + card->host = host; + snprintf(card->dev.bus_id, sizeof(card->dev.bus_id), + "%s", host->cdev.class_id); + card->dev.parent = host->cdev.dev; + card->dev.bus = &memstick_bus_type; + card->dev.release = memstick_free_card; + card->check = memstick_dummy_check; + + card->reg_addr.r_offset = offsetof(struct ms_register, id); + card->reg_addr.r_length = sizeof(id_reg); + card->reg_addr.w_offset = offsetof(struct ms_register, id); + card->reg_addr.w_length = sizeof(id_reg); + + init_completion(&card->mrq_complete); + + host->card = card; + if (memstick_set_rw_addr(card)) + goto err_out; + + card->next_request = h_memstick_read_dev_id; + memstick_new_req(host); + wait_for_completion(&card->mrq_complete); + + if (card->current_mrq.error) + goto err_out; + } + host->card = old_card; + return card; +err_out: + host->card = old_card; + kfree(card); + return NULL; +} + +static void memstick_power_on(struct memstick_host *host) +{ + host->set_param(host, MEMSTICK_POWER, MEMSTICK_POWER_ON); + host->set_param(host, MEMSTICK_INTERFACE, MEMSTICK_SERIAL); + msleep(1); +} + +static void memstick_check(struct work_struct *work) +{ + struct memstick_host *host = container_of(work, struct memstick_host, + media_checker); + struct memstick_dev *card; + + dev_dbg(host->cdev.dev, "memstick_check started\n"); + mutex_lock(&host->lock); + if (!host->card) + memstick_power_on(host); + + card = memstick_alloc_card(host); + + if (!card) { + if (host->card) { + device_unregister(&host->card->dev); + host->card = NULL; + } + } else { + dev_dbg(host->cdev.dev, "new card %02x, %02x, %02x\n", + card->id.type, card->id.category, card->id.class); + if (host->card) { + if (memstick_set_rw_addr(host->card) + || !memstick_dev_match(host->card, &card->id) + || !(host->card->check(host->card))) { + device_unregister(&host->card->dev); + host->card = NULL; + } + } + + if (!host->card) { + host->card = card; + if (device_register(&card->dev)) { + kfree(host->card); + host->card = NULL; + } + } else + kfree(card); + } + + if (!host->card) + host->set_param(host, MEMSTICK_POWER, MEMSTICK_POWER_OFF); + + mutex_unlock(&host->lock); + dev_dbg(host->cdev.dev, "memstick_check finished\n"); +} + +/** + * memstick_alloc_host - allocate a memstick_host structure + * @extra: size of the user private data to allocate + * @dev: parent device of the host + */ +struct memstick_host *memstick_alloc_host(unsigned int extra, + struct device *dev) +{ + struct memstick_host *host; + + host = kzalloc(sizeof(struct memstick_host) + extra, GFP_KERNEL); + if (host) { + mutex_init(&host->lock); + INIT_WORK(&host->media_checker, memstick_check); + host->cdev.class = &memstick_host_class; + host->cdev.dev = dev; + class_device_initialize(&host->cdev); + } + return host; +} +EXPORT_SYMBOL(memstick_alloc_host); + +/** + * memstick_add_host - start request processing on memstick host + * @host - host to use + */ +int memstick_add_host(struct memstick_host *host) +{ + int rc; + + if (!idr_pre_get(&memstick_host_idr, GFP_KERNEL)) + return -ENOMEM; + + spin_lock(&memstick_host_lock); + rc = idr_get_new(&memstick_host_idr, host, &host->id); + spin_unlock(&memstick_host_lock); + if (rc) + return rc; + + snprintf(host->cdev.class_id, BUS_ID_SIZE, + "memstick%u", host->id); + + rc = class_device_add(&host->cdev); + if (rc) { + spin_lock(&memstick_host_lock); + idr_remove(&memstick_host_idr, host->id); + spin_unlock(&memstick_host_lock); + return rc; + } + + host->set_param(host, MEMSTICK_POWER, MEMSTICK_POWER_OFF); + memstick_detect_change(host); + return 0; +} +EXPORT_SYMBOL(memstick_add_host); + +/** + * memstick_remove_host - stop request processing on memstick host + * @host - host to use + */ +void memstick_remove_host(struct memstick_host *host) +{ + flush_workqueue(workqueue); + mutex_lock(&host->lock); + if (host->card) + device_unregister(&host->card->dev); + host->card = NULL; + host->set_param(host, MEMSTICK_POWER, MEMSTICK_POWER_OFF); + mutex_unlock(&host->lock); + + spin_lock(&memstick_host_lock); + idr_remove(&memstick_host_idr, host->id); + spin_unlock(&memstick_host_lock); + class_device_del(&host->cdev); +} +EXPORT_SYMBOL(memstick_remove_host); + +/** + * memstick_free_host - free memstick host + * @host - host to use + */ +void memstick_free_host(struct memstick_host *host) +{ + mutex_destroy(&host->lock); + class_device_put(&host->cdev); +} +EXPORT_SYMBOL(memstick_free_host); + +int memstick_register_driver(struct memstick_driver *drv) +{ + drv->driver.bus = &memstick_bus_type; + + return driver_register(&drv->driver); +} +EXPORT_SYMBOL(memstick_register_driver); + +void memstick_unregister_driver(struct memstick_driver *drv) +{ + driver_unregister(&drv->driver); +} +EXPORT_SYMBOL(memstick_unregister_driver); + + +static int __init memstick_init(void) +{ + int rc; + + workqueue = create_freezeable_workqueue("kmemstick"); + if (!workqueue) + return -ENOMEM; + + rc = bus_register(&memstick_bus_type); + if (!rc) + rc = class_register(&memstick_host_class); + + if (!rc) + return 0; + + bus_unregister(&memstick_bus_type); + destroy_workqueue(workqueue); + + return rc; +} + +static void __exit memstick_exit(void) +{ + class_unregister(&memstick_host_class); + bus_unregister(&memstick_bus_type); + destroy_workqueue(workqueue); + idr_destroy(&memstick_host_idr); +} + +module_init(memstick_init); +module_exit(memstick_exit); + +MODULE_AUTHOR("Alex Dubov"); +MODULE_LICENSE("GPL"); +MODULE_DESCRIPTION("Sony MemoryStick core driver"); +MODULE_VERSION(DRIVER_VERSION); diff --git a/drivers/memstick/core/mspro_block.c b/drivers/memstick/core/mspro_block.c new file mode 100644 index 00000000000..423ad8cf4bb --- /dev/null +++ b/drivers/memstick/core/mspro_block.c @@ -0,0 +1,1351 @@ +/* + * Sony MemoryStick Pro storage support + * + * Copyright (C) 2007 Alex Dubov <oakad@yahoo.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. + * + * Special thanks to Carlos Corbacho for providing various MemoryStick cards + * that made this driver possible. + * + */ + +#include <linux/blkdev.h> +#include <linux/idr.h> +#include <linux/hdreg.h> +#include <linux/kthread.h> +#include <linux/memstick.h> + +#define DRIVER_NAME "mspro_block" +#define DRIVER_VERSION "0.2" + +static int major; +module_param(major, int, 0644); + +#define MSPRO_BLOCK_MAX_SEGS 32 +#define MSPRO_BLOCK_MAX_PAGES ((2 << 16) - 1) + +#define MSPRO_BLOCK_SIGNATURE 0xa5c3 +#define MSPRO_BLOCK_MAX_ATTRIBUTES 41 + +enum { + MSPRO_BLOCK_ID_SYSINFO = 0x10, + MSPRO_BLOCK_ID_MODELNAME = 0x15, + MSPRO_BLOCK_ID_MBR = 0x20, + MSPRO_BLOCK_ID_PBR16 = 0x21, + MSPRO_BLOCK_ID_PBR32 = 0x22, + MSPRO_BLOCK_ID_SPECFILEVALUES1 = 0x25, + MSPRO_BLOCK_ID_SPECFILEVALUES2 = 0x26, + MSPRO_BLOCK_ID_DEVINFO = 0x30 +}; + +struct mspro_sys_attr { + size_t size; + void *data; + unsigned char id; + char name[32]; + struct device_attribute dev_attr; +}; + +struct mspro_attr_entry { + unsigned int address; + unsigned int size; + unsigned char id; + unsigned char reserved[3]; +} __attribute__((packed)); + +struct mspro_attribute { + unsigned short signature; + unsigned short version; + unsigned char count; + unsigned char reserved[11]; + struct mspro_attr_entry entries[]; +} __attribute__((packed)); + +struct mspro_sys_info { + unsigned char class; + unsigned char reserved0; + unsigned short block_size; + unsigned short block_count; + unsigned short user_block_count; + unsigned short page_size; + unsigned char reserved1[2]; + unsigned char assembly_date[8]; + unsigned int serial_number; + unsigned char assembly_maker_code; + unsigned char assembly_model_code[3]; + unsigned short memory_maker_code; + unsigned short memory_model_code; + unsigned char reserved2[4]; + unsigned char vcc; + unsigned char vpp; + unsigned short controller_number; + unsigned short controller_function; + unsigned short start_sector; + unsigned short unit_size; + unsigned char ms_sub_class; + unsigned char reserved3[4]; + unsigned char interface_type; + unsigned short controller_code; + unsigned char format_type; + unsigned char reserved4; + unsigned char device_type; + unsigned char reserved5[7]; + unsigned char mspro_id[16]; + unsigned char reserved6[16]; +} __attribute__((packed)); + +struct mspro_mbr { + unsigned char boot_partition; + unsigned char start_head; + unsigned char start_sector; + unsigned char start_cylinder; + unsigned char partition_type; + unsigned char end_head; + unsigned char end_sector; + unsigned char end_cylinder; + unsigned int start_sectors; + unsigned int sectors_per_partition; +} __attribute__((packed)); + +struct mspro_devinfo { + unsigned short cylinders; + unsigned short heads; + unsigned short bytes_per_track; + unsigned short bytes_per_sector; + unsigned short sectors_per_track; + unsigned char reserved[6]; +} __attribute__((packed)); + +struct mspro_block_data { + struct memstick_dev *card; + unsigned int usage_count; + struct gendisk *disk; + struct request_queue *queue; + spinlock_t q_lock; + wait_queue_head_t q_wait; + struct task_struct *q_thread; + + unsigned short page_size; + unsigned short cylinders; + unsigned short heads; + unsigned short sectors_per_track; + + unsigned char system; + unsigned char read_only:1, + active:1, + has_request:1, + data_dir:1; + unsigned char transfer_cmd; + + int (*mrq_handler)(struct memstick_dev *card, + struct memstick_request **mrq); + + struct attribute_group attr_group; + + struct scatterlist req_sg[MSPRO_BLOCK_MAX_SEGS]; + unsigned int seg_count; + unsigned int current_seg; + unsigned short current_page; +}; + +static DEFINE_IDR(mspro_block_disk_idr); +static DEFINE_MUTEX(mspro_block_disk_lock); + +/*** Block device ***/ + +static int mspro_block_bd_open(struct inode *inode, struct file *filp) +{ + struct gendisk *disk = inode->i_bdev->bd_disk; + struct mspro_block_data *msb = disk->private_data; + int rc = -ENXIO; + + mutex_lock(&mspro_block_disk_lock); + + if (msb && msb->card) { + msb->usage_count++; + if ((filp->f_mode & FMODE_WRITE) && msb->read_only) + rc = -EROFS; + else + rc = 0; + } + + mutex_unlock(&mspro_block_disk_lock); + + return rc; +} + + +static int mspro_block_disk_release(struct gendisk *disk) +{ + struct mspro_block_data *msb = disk->private_data; + int disk_id = disk->first_minor >> MEMSTICK_PART_SHIFT; + + mutex_lock(&mspro_block_disk_lock); + + if (msb->usage_count) { + msb->usage_count--; + if (!msb->usage_count) { + kfree(msb); + disk->private_data = NULL; + idr_remove(&mspro_block_disk_idr, disk_id); + put_disk(disk); + } + } + + mutex_unlock(&mspro_block_disk_lock); + + return 0; +} + +static int mspro_block_bd_release(struct inode *inode, struct file *filp) +{ + struct gendisk *disk = inode->i_bdev->bd_disk; + return mspro_block_disk_release(disk); +} + +static int mspro_block_bd_getgeo(struct block_device *bdev, + struct hd_geometry *geo) +{ + struct mspro_block_data *msb = bdev->bd_disk->private_data; + + geo->heads = msb->heads; + geo->sectors = msb->sectors_per_track; + geo->cylinders = msb->cylinders; + + return 0; +} + +static struct block_device_operations ms_block_bdops = { + .open = mspro_block_bd_open, + .release = mspro_block_bd_release, + .getgeo = mspro_block_bd_getgeo, + .owner = THIS_MODULE +}; + +/*** Information ***/ + +static struct mspro_sys_attr *mspro_from_sysfs_attr(struct attribute *attr) +{ + struct device_attribute *dev_attr + = container_of(attr, struct device_attribute, attr); + return container_of(dev_attr, struct mspro_sys_attr, dev_attr); +} + +static const char *mspro_block_attr_name(unsigned char tag) +{ + switch (tag) { + case MSPRO_BLOCK_ID_SYSINFO: + return "attr_sysinfo"; + case MSPRO_BLOCK_ID_MODELNAME: + return "attr_modelname"; + case MSPRO_BLOCK_ID_MBR: + return "attr_mbr"; + case MSPRO_BLOCK_ID_PBR16: + return "attr_pbr16"; + case MSPRO_BLOCK_ID_PBR32: + return "attr_pbr32"; + case MSPRO_BLOCK_ID_SPECFILEVALUES1: + return "attr_specfilevalues1"; + case MSPRO_BLOCK_ID_SPECFILEVALUES2: + return "attr_specfilevalues2"; + case MSPRO_BLOCK_ID_DEVINFO: + return "attr_devinfo"; + default: + return NULL; + }; +} + +typedef ssize_t (*sysfs_show_t)(struct device *dev, + struct device_attribute *attr, + char *buffer); + +static ssize_t mspro_block_attr_show_default(struct device *dev, + struct device_attribute *attr, + char *buffer) +{ + struct mspro_sys_attr *s_attr = container_of(attr, + struct mspro_sys_attr, + dev_attr); + + ssize_t cnt, rc = 0; + + for (cnt = 0; cnt < s_attr->size; cnt++) { + if (cnt && !(cnt % 16)) { + if (PAGE_SIZE - rc) + buffer[rc++] = '\n'; + } + + rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "%02x ", + ((unsigned char *)s_attr->data)[cnt]); + } + return rc; +} + +static ssize_t mspro_block_attr_show_sysinfo(struct device *dev, + struct device_attribute *attr, + char *buffer) +{ + struct mspro_sys_attr *x_attr = container_of(attr, + struct mspro_sys_attr, + dev_attr); + struct mspro_sys_info *x_sys = x_attr->data; + ssize_t rc = 0; + + rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "class: %x\n", + x_sys->class); + rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "block size: %x\n", + be16_to_cpu(x_sys->block_size)); + rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "block count: %x\n", + be16_to_cpu(x_sys->block_count)); + rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "user block count: %x\n", + be16_to_cpu(x_sys->user_block_count)); + rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "page size: %x\n", + be16_to_cpu(x_sys->page_size)); + rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "assembly date: " + "%d %04u-%02u-%02u %02u:%02u:%02u\n", + x_sys->assembly_date[0], + be16_to_cpu(*(unsigned short *) + &x_sys->assembly_date[1]), + x_sys->assembly_date[3], x_sys->assembly_date[4], + x_sys->assembly_date[5], x_sys->assembly_date[6], + x_sys->assembly_date[7]); + rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "serial number: %x\n", + be32_to_cpu(x_sys->serial_number)); + rc += scnprintf(buffer + rc, PAGE_SIZE - rc, + "assembly maker code: %x\n", + x_sys->assembly_maker_code); + rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "assembly model code: " + "%02x%02x%02x\n", x_sys->assembly_model_code[0], + x_sys->assembly_model_code[1], + x_sys->assembly_model_code[2]); + rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "memory maker code: %x\n", + be16_to_cpu(x_sys->memory_maker_code)); + rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "memory model code: %x\n", + be16_to_cpu(x_sys->memory_model_code)); + rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "vcc: %x\n", + x_sys->vcc); + rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "vpp: %x\n", + x_sys->vpp); + rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "controller number: %x\n", + be16_to_cpu(x_sys->controller_number)); + rc += scnprintf(buffer + rc, PAGE_SIZE - rc, + "controller function: %x\n", + be16_to_cpu(x_sys->controller_function)); + rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "start sector: %x\n", + be16_to_cpu(x_sys->start_sector)); + rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "unit size: %x\n", + be16_to_cpu(x_sys->unit_size)); + rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "sub class: %x\n", + x_sys->ms_sub_class); + rc += scnprintf(buffer + rc, P |