diff -Naur linux-2.6.10-ac1.orig/drivers/usb/storage/Kconfig ../linux-2.6.10-ac1/drivers/usb/storage/Kconfig --- linux-2.6.10-ac1.orig/drivers/usb/storage/Kconfig 2004-12-24 22:35:29.000000000 +0100 +++ ../linux-2.6.10-ac1/drivers/usb/storage/Kconfig 2004-12-27 11:45:36.000000000 +0100 @@ -111,6 +111,13 @@ Say Y here to include additional code to support the Sandisk SDDR-55 SmartMedia reader in the USB Mass Storage driver. +config USB_STORAGE_HH501 + bool "Foxconn HH501 SmartMedia support (EXPERIMENTAL)" + depends on USB_STORAGE && EXPERIMENTAL + help + Say Y here to include additional code to support the Lexar Jumpshot + USB CompactFlash reader. + config USB_STORAGE_JUMPSHOT bool "Lexar Jumpshot Compact Flash Reader (EXPERIMENTAL)" depends on USB_STORAGE && EXPERIMENTAL diff -Naur linux-2.6.10-ac1.orig/drivers/usb/storage/Makefile ../linux-2.6.10-ac1/drivers/usb/storage/Makefile --- linux-2.6.10-ac1.orig/drivers/usb/storage/Makefile 2004-12-24 22:35:01.000000000 +0100 +++ ../linux-2.6.10-ac1/drivers/usb/storage/Makefile 2004-12-27 11:45:36.000000000 +0100 @@ -18,6 +18,7 @@ usb-storage-obj-$(CONFIG_USB_STORAGE_ISD200) += isd200.o usb-storage-obj-$(CONFIG_USB_STORAGE_DATAFAB) += datafab.o usb-storage-obj-$(CONFIG_USB_STORAGE_JUMPSHOT) += jumpshot.o +usb-storage-obj-$(CONFIG_USB_STORAGE_HH501) += hh501.o usb-storage-objs := scsiglue.o protocol.o transport.o usb.o \ initializers.o $(usb-storage-obj-y) diff -Naur linux-2.6.10-ac1.orig/drivers/usb/storage/hh501.c ../linux-2.6.10-ac1/drivers/usb/storage/hh501.c --- linux-2.6.10-ac1.orig/drivers/usb/storage/hh501.c 1970-01-01 01:00:00.000000000 +0100 +++ ../linux-2.6.10-ac1/drivers/usb/storage/hh501.c 2004-12-27 11:45:36.000000000 +0100 @@ -0,0 +1,1052 @@ +/* Driver for Foxconn HH501 USB SmartMedia reader/writer + * + * v0.2: Luc Saillard + * Add some code to correct an error if i found a bad parity in the map transfer table. + * Add write operation !!!! + * + * Current development and maintenance by: + * (c) 2001-2004 Luc Saillard * + * + * This code borrow some code from the sddr09.c + * + * (c) 2000, 2001 Robert Baruch (autophile@starband.net) + * (c) 2002 Andries Brouwer (aeb@cwi.nl) + * (c) 2002 Alan Stern + * + * The Foxconn HH501 USB SmartMedia reader uses the EzUSB chip from + * Anchorchips. This a microcontroler that can be programmmed, so a firmware + * needs to be loaded before using the reader. There are another chip on the board + * made by carry (www.carry.com.tw). + * You can download the software at http://luc.saillard.free.fr/hh501/ or boot + * under Windows then reboot under Linux without unplug the reader. + * Note: I use the firmware v3.x found in the GA-HH501.ZIP driver for windows. + * It is far from perfect because i have no documentation on the protocol use + * by the reader. + * + * Ok, for now, i've harcoded some values. Sector need to be 512 bytes len. So + * it won't work with old smartmedia card (5v). + * + * Protocol use by the reader: + * + * - Only bulk message. + * - Each command is 8 bytes len. + * + * Commands List: + * + * 0x10: unknow + * Return 1 byte + * 0x11: inquiry + * out[0] = ManufacturerID + * out[1] = ModelID + * The firmware supports this ModelID: + * 0x64,0x6e,0x76,0x79,0xd9,0xda,0xe8,0xea,0xec + * 0x12: read the CIS table + * struct in { + * u8 cmd=0x12; + * u8 addr[3]; + * u16 len; // Little Endian format + * }; + * 0x13: write on the smartmedia + * struct in { + * u8 cmd=0x14; + * u8 addr[3]; + * u16 len; + * u8 data[2] = {0,0}; + * } + * 0x14: copy block on the memory card + * struct in { // for 8Mo card + * u8 cmd=0x14; + * u16 block_src; + * u16 block_dst; + * u8 data[3] = {1,0,0}; + * } + * struct in { // for 64Mo card + * u8 cmd=0x14; + * u8 addr_src[3]; + * u8 addr_dst[3]; + * u8 data = 1; + * } + * 0x15: release block on the memory card (DANGEROUS: don't remove all blocks) + * 0x17: read block from the memory card + * 0x20: return status after a write + * 0x21: BUG() the firmware + * 0x22: unknow + * 0x32: unknow + * 0x33: Reset all internal registers. + * out[0] = status + * + * + * When you read the status: + * bit 7: we have a card inserted + * bit 1: the card have changed we need to reload the mapping table + * + * How to write a block ? + * + * First you copy with the commande 0x14 a block a start adress (&~0xF) + * Then you write the block you can't to modify with command 0x13 or you + * use the command 0x14 which copy the block to another location. + * And finish, you delete the last block with the command 0x15. + * + * I've personally tested with this smarmedia card: + * 8Mb from Olympus + * 16Mb from Fuji and another manufacturer + * 64Mb from unknow manufacturer + * + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2, or (at your option) any + * later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +#include "transport.h" +#include "protocol.h" +#include "usb.h" +#include "debug.h" +#include "hh501.h" + +#include +#include +#include +#include +#include +#include + +#define short_pack(lsb,msb) ( ((u16)(lsb)) | ( ((u16)(msb))<<8 ) ) +#define LSB_of(s) ((s)&0xFF) +#define MSB_of(s) ((s)>>8) + +//#define US_DEBUGP printk + +/* Endpoints use by the device */ + +#define EP_WRITE_COMMAND 0x01 +#define EP_READ_STATUS (0x01 | USB_DIR_IN) +#define EP_WRITE_DATA 0x02 +#define EP_READ_DATA (0x02 | USB_DIR_IN) + + +/* List of know commands for the HH501 */ +#define HH501_CMD_RESET_ALL 0x33 +#define HH501_CMD_READ_STATUS 0x20 +#define HH501_CMD_READ_BLOCK 0x17 +#define HH501_CMD_READ_CIS 0x16 +#define HH501_CMD_FREE_CIS 0x15 +#define HH501_CMD_COPY_BLOCK 0x14 +#define HH501_CMD_WRITE_DATA 0x13 +#define HH501_CMD_INQUIRY 0x11 + +/* List of unknow commands */ +#define HH501_CMD_32 0x32 +#define HH501_CMD_22 0x22 +#define HH501_CMD_21 0x21 +#define HH501_CMD_20 0x20 +#define HH501_CMD_READ_CIS2 0x12 +#define HH501_CMD_10 0x10 + +#define set_sense_info(sk, asc, ascq) \ + do { \ + info->sense_data[2] = sk; \ + info->sense_data[12] = asc; \ + info->sense_data[13] = ascq; \ + US_DEBUGP("%s: set_sense_info(%d,%d,%d)\n",__FUNCTION__,sk,asc,ascq); \ + } while (0) + +/* + * ECC computation. + */ +/* + * This table is the precalculated form, if you want to generate it a runtime, please + * see the gen_ecctable() + */ +static unsigned char ecctable[256] = { + 0x00, 0x55, 0x56, 0x03, 0x59, 0x0C, 0x0F, 0x5A, 0x5A, 0x0F, 0x0C, 0x59, 0x03, 0x56, 0x55, 0x00, + 0x65, 0x30, 0x33, 0x66, 0x3C, 0x69, 0x6A, 0x3F, 0x3F, 0x6A, 0x69, 0x3C, 0x66, 0x33, 0x30, 0x65, + 0x66, 0x33, 0x30, 0x65, 0x3F, 0x6A, 0x69, 0x3C, 0x3C, 0x69, 0x6A, 0x3F, 0x65, 0x30, 0x33, 0x66, + 0x03, 0x56, 0x55, 0x00, 0x5A, 0x0F, 0x0C, 0x59, 0x59, 0x0C, 0x0F, 0x5A, 0x00, 0x55, 0x56, 0x03, + 0x69, 0x3C, 0x3F, 0x6A, 0x30, 0x65, 0x66, 0x33, 0x33, 0x66, 0x65, 0x30, 0x6A, 0x3F, 0x3C, 0x69, + 0x0C, 0x59, 0x5A, 0x0F, 0x55, 0x00, 0x03, 0x56, 0x56, 0x03, 0x00, 0x55, 0x0F, 0x5A, 0x59, 0x0C, + 0x0F, 0x5A, 0x59, 0x0C, 0x56, 0x03, 0x00, 0x55, 0x55, 0x00, 0x03, 0x56, 0x0C, 0x59, 0x5A, 0x0F, + 0x6A, 0x3F, 0x3C, 0x69, 0x33, 0x66, 0x65, 0x30, 0x30, 0x65, 0x66, 0x33, 0x69, 0x3C, 0x3F, 0x6A, + 0x6A, 0x3F, 0x3C, 0x69, 0x33, 0x66, 0x65, 0x30, 0x30, 0x65, 0x66, 0x33, 0x69, 0x3C, 0x3F, 0x6A, + 0x0F, 0x5A, 0x59, 0x0C, 0x56, 0x03, 0x00, 0x55, 0x55, 0x00, 0x03, 0x56, 0x0C, 0x59, 0x5A, 0x0F, + 0x0C, 0x59, 0x5A, 0x0F, 0x55, 0x00, 0x03, 0x56, 0x56, 0x03, 0x00, 0x55, 0x0F, 0x5A, 0x59, 0x0C, + 0x69, 0x3C, 0x3F, 0x6A, 0x30, 0x65, 0x66, 0x33, 0x33, 0x66, 0x65, 0x30, 0x6A, 0x3F, 0x3C, 0x69, + 0x03, 0x56, 0x55, 0x00, 0x5A, 0x0F, 0x0C, 0x59, 0x59, 0x0C, 0x0F, 0x5A, 0x00, 0x55, 0x56, 0x03, + 0x66, 0x33, 0x30, 0x65, 0x3F, 0x6A, 0x69, 0x3C, 0x3C, 0x69, 0x6A, 0x3F, 0x65, 0x30, 0x33, 0x66, + 0x65, 0x30, 0x33, 0x66, 0x3C, 0x69, 0x6A, 0x3F, 0x3F, 0x6A, 0x69, 0x3C, 0x66, 0x33, 0x30, 0x65, + 0x00, 0x55, 0x56, 0x03, 0x59, 0x0C, 0x0F, 0x5A, 0x5A, 0x0F, 0x0C, 0x59, 0x03, 0x56, 0x55, 0x00 +}; + +/* + * This is a transformation table + * 0101 ==> 00010001 + * 1111 ==> 01010101 + */ +static unsigned char fast_trans[16] = { + 0x00,0x01,0x04,0x05,0x10,0x11,0x14,0x15, + 0x40,0x41,0x44,0x45,0x50,0x51,0x54,0x55 +}; + + +/* Nombre de bits dans un octet par taille de 16: + * 0 = nombre de bit paire + * 1 = nombre de bit impaire */ +static unsigned char fast_parity[16]= { + 0,1,1,0,1,0,0,1, + 1,0,0,1,0,1,1,0, +}; + +/* + * Generate a table that contains the column parity already shifted by two. + * and the first bit contains a boolean that indicates if we need to calculate + * the crc for this byte (needed by the line parity checksum) + */ +#if 0 +static void gen_ecctable(void) +{ + int i; + unsigned int paritc; /* 6 bits for column parity */ + unsigned int paritl; /* 1 bit for line parity */ + /* To speed up the genration table, and because gcc can't optimize + * my code, i split all intermediate sum with this variable */ + unsigned char parit0c, parit1c, parit2c, parit3c; + unsigned char parit4c, parit5c, parit6c, parit7c; + unsigned char parit1_1, parit1_2; + unsigned char parit2_1, parit2_2; + unsigned char parit4_1, parit4_2; + + /* For any byte, calculate the crc */ + for (i=0;i<256;i++) + { + paritc = i; + + parit0c = (paritc & 0x01); paritc>>=1; + parit1c = (paritc & 0x01); paritc>>=1; + parit2c = (paritc & 0x01); paritc>>=1; + parit3c = (paritc & 0x01); paritc>>=1; + parit4c = (paritc & 0x01); paritc>>=1; + parit5c = (paritc & 0x01); paritc>>=1; + parit6c = (paritc & 0x01); paritc>>=1; + parit7c = (paritc & 0x01); + + parit1_2 = parit6c ^ parit4c ^ parit2c ^ parit0c; + parit1_1 = parit7c ^ parit5c ^ parit3c ^ parit1c; + parit2_2 = parit5c ^ parit4c ^ parit1c ^ parit0c; + parit2_1 = parit7c ^ parit6c ^ parit3c ^ parit2c; + parit4_2 = parit3c ^ parit2c ^ parit1c ^ parit0c; + parit4_1 = parit7c ^ parit6c ^ parit5c ^ parit4c; + + parit1_2 <<= 2; + parit1_1 <<= 3; + parit2_2 <<= 4; + parit2_1 <<= 5; + parit4_2 <<= 6; + parit4_1 <<= 7; + + paritc = (parit4_1|parit4_2|parit2_1|parit2_2|parit1_1|parit1_2); + + /* We do the same job for line parity, we update the crc line parity + * only when the count of line is even. */ + paritl = 1; + paritl ^= fast_parity[i & 0xF]; + paritl ^= fast_parity[(8+(i>>4))&0xF]; + + /* + * XXXXXX0Y + * X parity column + * Y parity line + */ + ecctable[i] = paritc | paritl; + } +} +#endif + +/* + * Take a 256 bytes page, and generate the CRC table + * ecc is stored like this + * ecc[0] = ecc2 => LP07,LP06,LP05,... + * ecc[1] = ecc1 => LP15,LP14,LP13,... + * ecc[2] = ecc3 => CP5,CP4,CP3,...,"1","1" + * LP: Line Parity + * CP: Column Parity + */ +static void +calculate_ecc ( const unsigned char *data, unsigned char *ecc) +{ + unsigned int i; /* For counting */ + unsigned int a; /* Working for table */ + unsigned int reg1; /* D-all,CP5,CP4,CP3,... */ + unsigned char reg2; /* LP14,LP12,L10,... */ + unsigned char reg3; /* LP15,LP13,L11,... */ + unsigned int lparity; + + reg1 = reg2 = reg3 = 0; /* Clear parameter */ + + for (i = 0; i < 256; i++) + { + a = ecctable[*data++]; /* Get CP0-CP5 code from table */ + reg1 ^= a; /* XOR with a */ + if ((a & 1)) + { /* If D_all(all bit XOR) = 1 */ + reg3 ^= (unsigned char) i; /* XOR with counter */ + reg2 ^= ~((unsigned char) i); /* XOR with inv. of counter */ + } + } + + /* Trans LP14,12,10,... & LP15,13,11,... -> LP15,14,13,... & LP7,6,5,.. */ + /* gcc can't reorder instruction, i keep a non optimize version + * lparity = 0; + * lparity |= fast_trans[reg2&0xf]; + * lparity |= (fast_trans[reg3&0xf])<<1; + * lparity |= (fast_trans[(reg2>>4)])<<8; + * lparity |= (fast_trans[(reg3>>4)])<<9; + * *ecc1 = ~(lparity>>8); + * *ecc2 = ~(lparity&0xFF); + */ + lparity = fast_trans[reg2&0xf]; + lparity |= (fast_trans[reg3&0xf])<<1; + ecc[0] = ~lparity; /* Even parity */ + lparity>>=8; + lparity |= (fast_trans[(reg2>>4)]); + lparity |= (fast_trans[(reg3>>4)])<<1; + ecc[1] = ~lparity; /* Even parity */ + + ecc[2] = ((~reg1)) | 3; /* Make TEL format */ +} + + +unsigned int lba_test_parity(unsigned int lba1, unsigned int lba2) +{ + unsigned char parity; + if (lba1 != lba2) + { + if ((lba1 & 0xF000) == 0x1000) + { + parity = 1; // the parity of 0x1000 + parity ^= fast_parity[lba1 & 0x000F]; + parity ^= fast_parity[(lba1 >> 4) & 0x000F]; + parity ^= fast_parity[(lba1 >> 8) & 0x000F]; + if (!parity) + return lba1; + } + if ((lba2 & 0xF000) == 0x1000) + { + parity = 1; // the parity of 0x1000 + parity ^= fast_parity[lba2 & 0x000F]; + parity ^= fast_parity[(lba2 >> 4) & 0x000F]; + parity ^= fast_parity[(lba2 >> 8) & 0x000F]; + if (!parity) + return lba2; + } + return 0xFFFFFFFF; /* Impossible block */ + } + return lba1; +} + + + +/* + * The actual driver starts here. + */ + +/* + * LBA and PBA are unsigned ints. Special values. + */ +#define UNDEF 0xffff +#define SPARE 0xfffe +#define UNUSABLE 0xfffd + +/* + * Send a message to the usb device. + */ +static int +hh501_bulk_send(struct us_data *us, unsigned int direction, + unsigned char *data, unsigned int len) +{ + + struct hh501_card_info *info = (struct hh501_card_info *)us->extra; + unsigned int pipe = info->pipe[direction]; + + info->last_access = jiffies; + return usb_stor_bulk_transfer_buf(us, pipe, data, len, NULL); +} + + +static int hh501_read_deviceID(struct us_data *us, + unsigned char *manufacturerID, + unsigned char *deviceID) { + + int result; + unsigned char *command = us->iobuf; + unsigned char *content = us->iobuf; + + memset(command, 0, 8); + command[0] = HH501_CMD_INQUIRY; + result = hh501_bulk_send(us, PIPE_WRITE_COMMAND, command, 8); + + US_DEBUGP("Result of send_control for device ID is %d\n", + result); + + if (result != USB_STOR_XFER_GOOD) + return USB_STOR_TRANSPORT_ERROR; + + result = hh501_bulk_send(us, PIPE_READ_DATA, content, 2); + + if (result != USB_STOR_XFER_GOOD) + return USB_STOR_TRANSPORT_ERROR; + + *manufacturerID = content[0]; + *deviceID = content[1]; + + return USB_STOR_TRANSPORT_GOOD; +} + +static unsigned long hh501_get_capacity(struct us_data *us) { + + unsigned char manufacturerID; + unsigned char deviceID; + int result; + struct hh501_card_info *info = (struct hh501_card_info *)us->extra; + + US_DEBUGP("Reading capacity...\n"); + + result = hh501_read_deviceID(us, &manufacturerID, &deviceID); + + US_DEBUGP("Result of read_deviceID is %d\n", result); + + if (result != USB_STOR_TRANSPORT_GOOD) + return 0; + + US_DEBUGP("Device ID = %02X\n", deviceID); + US_DEBUGP("Manuf ID = %02X\n", manufacturerID); + + info->pageshift = 9; + info->smallpageshift = 0; + info->blocksize = 16; + info->blockshift = 4; + info->blockmask = 15; + + switch (deviceID) { + + case 0x6e: // 1MB + case 0xe8: + case 0xec: + info->pageshift = 8; + info->smallpageshift = 1; + return 0x00100000; + + case 0xea: // 2MB + case 0x64: + info->pageshift = 8; + info->smallpageshift = 1; + case 0x5d: // 5d is a ROM card with pagesize 512. + return 0x00200000; + + case 0xe3: // 4MB + case 0xe5: + case 0x6b: + case 0xd5: + return 0x00400000; + + case 0xe6: // 8MB + case 0xd6: + return 0x00800000; + + case 0x73: // 16MB + info->blocksize = 32; + info->blockshift = 5; + info->blockmask = 31; + return 0x01000000; + + case 0x75: // 32MB + info->blocksize = 32; + info->blockshift = 5; + info->blockmask = 31; + return 0x02000000; + + case 0x76: // 64MB + info->blocksize = 32; + info->blockshift = 5; + info->blockmask = 31; + return 0x04000000; + + case 0x79: // 128MB + info->blocksize = 32; + info->blockshift = 5; + info->blockmask = 31; + return 0x08000000; + + default: // unknown + return 0; + + } +} + + + +void hh501_card_info_destructor(void *extra) +{ + struct hh501_card_info *info = (struct hh501_card_info *) extra; + + if (!extra) + return; + + if (info->lba_to_pba) + kfree(info->lba_to_pba); + if (info->pba_to_lba) + kfree(info->pba_to_lba); + info->lba_to_pba=NULL; + info->pba_to_lba=NULL; +} + + + +int hh501_init(struct us_data *us) +{ + int result, count; + struct hh501_card_info *info; + unsigned char *command = us->iobuf; + unsigned char *content = us->iobuf; + int not_initialized = 1; + + /* Alloc some private structure */ + info = (struct hh501_card_info *)us->extra; + if (!info) { + /* us->extra is free by usb.c */ + us->extra = kmalloc(sizeof(struct hh501_card_info), GFP_NOIO); + if (us->extra) { + memset(us->extra, 0, sizeof(struct hh501_card_info)); + us->extra_destructor = hh501_card_info_destructor; + info = (struct hh501_card_info *)us->extra; + } else { + return USB_STOR_TRANSPORT_ERROR; + } + } + + info->pipe[PIPE_WRITE_COMMAND] = usb_sndbulkpipe(us->pusb_dev, EP_WRITE_COMMAND & USB_ENDPOINT_NUMBER_MASK); + info->pipe[PIPE_READ_STATUS] = usb_rcvbulkpipe(us->pusb_dev, EP_READ_STATUS & USB_ENDPOINT_NUMBER_MASK); + info->pipe[PIPE_WRITE_DATA] = usb_sndbulkpipe(us->pusb_dev, EP_WRITE_DATA & USB_ENDPOINT_NUMBER_MASK); + info->pipe[PIPE_READ_DATA] = usb_rcvbulkpipe(us->pusb_dev, EP_READ_DATA & USB_ENDPOINT_NUMBER_MASK); + + // Initialise the reader + US_DEBUGP ("Attempting to init Foxconn HH501 Smartmedia Reader...\n"); + memset(command, 0, 8); + command[0] = HH501_CMD_RESET_ALL; + result = hh501_bulk_send(us, PIPE_WRITE_COMMAND, command, 8); + if (result != USB_STOR_XFER_GOOD) + { + US_DEBUGP ("hh501_init: reset failed %d...\n",result); + return result; + } + + US_DEBUGP ("hh501_init: waiting device is ready ...\n"); + + count = 0; + while (not_initialized && count < 500) { + mdelay(100); /* Wait 100ms */ + result = hh501_bulk_send(us, PIPE_READ_STATUS, content, 2); + US_DEBUGP("-- result is %d, content[0]=%2.2x\n", result,content[0]); + if ((content[0] & 0xF) == 0) + not_initialized = 0; + count++; + } + + if (not_initialized) + return USB_STOR_XFER_ERROR; + else + return USB_STOR_XFER_GOOD; +} + + +/* check if card inserted, if there is, update read_only status + * return non zero if no card + */ + +static int hh501_status(struct us_data *us) +{ + struct hh501_card_info *info = (struct hh501_card_info *)us->extra; + unsigned char *content = us->iobuf; + int result; + + result = hh501_bulk_send(us, PIPE_READ_STATUS, content, 2); + if (result != USB_STOR_XFER_GOOD) + { + set_sense_info (4, 0, 0); /* hardware error */ + return USB_STOR_TRANSPORT_ERROR; + } + + info->read_only = content[0]&0x20; /* card protected */ + + if (content[0] & 0x10) /* card changed */ + { + /* FIXME: we need to free some buffer ... */ +// set_sense_info (6, 0x28, 0); /* new media, set unit attention, not ready to ready */ +// return USB_STOR_TRANSPORT_FAILED; + } + + if (content[0] & 0x40) /* card inserted */ + { + return USB_STOR_TRANSPORT_GOOD; + } + else + { + info->fatal_error = 0; + info->force_read_only = 0; + + set_sense_info (2, 0x3a, 0); /* not ready, medium not present */ + return USB_STOR_TRANSPORT_FAILED; + } +} + +/* + * Read a single line in the CIS table + */ +int hh501_read_map_block(struct us_data *us, + unsigned int block, unsigned char *map) +{ + int result; + struct hh501_card_info *info = (struct hh501_card_info *) (us->extra); + unsigned char *cmd = us->iobuf; + unsigned char *content = us->iobuf; + int block_addr = block * info->blocksize; + + memset(cmd,0,8); + cmd[0] = HH501_CMD_READ_CIS; + if (info->capacity >= 0x4000000) { + cmd[3] = block_addr >> 16; + cmd[2] = block_addr >> 8; + cmd[1] = block_addr & 0xFF; + } else { + cmd[3] = block_addr >> 8; + cmd[2] = block_addr & 0xFF; + cmd[1] = 0; + } + + result = hh501_bulk_send(us, PIPE_WRITE_COMMAND, cmd, 8); + if (result) + { + US_DEBUGP("hh501_read_map_block: failed(%d) for addr=0x%4.4x\n", result,block); + return result; + } + + memset(content, 0, 0x10); + result = hh501_bulk_send(us, PIPE_READ_DATA, content, 16); + memcpy(map,content,16); + return result; +} + + + +static int hh501_read_map(struct us_data *us) { + + struct hh501_card_info *info = (struct hh501_card_info *)(us->extra); + int i, numblocks; + unsigned short lba, lba1, lba2; + unsigned char ptr[16]; + + if (!info->capacity) + return -1; + + numblocks = info->capacity >> (info->blockshift + info->pageshift); + + if (info->lba_to_pba) + kfree(info->lba_to_pba); + if (info->pba_to_lba) + kfree(info->pba_to_lba); + + info->lba_to_pba = kmalloc(numblocks*sizeof(int), GFP_NOIO); + info->pba_to_lba = kmalloc(numblocks*sizeof(int), GFP_NOIO); + + if (info->lba_to_pba == NULL || info->pba_to_lba == NULL) { + if (info->lba_to_pba != NULL) + kfree(info->lba_to_pba); + if (info->pba_to_lba != NULL) + kfree(info->pba_to_lba); + info->lba_to_pba = NULL; + info->pba_to_lba = NULL; + return -1; + } + + memset(info->lba_to_pba, 0xff, numblocks*sizeof(int)); + memset(info->pba_to_lba, 0xff, numblocks*sizeof(int)); + + for (i=0; i> 1; + + /* Every 1024 physical blocks ("zone"), the LBA numbers + * go back to zero, but are within a higher + * block of LBA's. Also, there is a maximum of + * 1000 LBA's per zone. In other words, in PBA + * 1024-2047 you will find LBA 0-999 which are + * really LBA 1000-1999. Yes, this wastes 24 + * physical blocks per zone. Go figure. + */ + + lba += 1000 * (i / 0x400); + + if (lba >= numblocks) { + US_DEBUGP("Bad LBA %04X for block %04X\n", lba, i); + continue; + } + + info->pba_to_lba[i] = lba; + info->lba_to_pba[lba] = i; + + } + + return 0; +} + + +#if 0 +static int hh501_read_data(struct us_data *us, + unsigned int lba, + unsigned int page, + unsigned short sectors) { +#else +/* + * + * Read a memory block in the smartmedia card. + * + * us - the pointer to the us_data structure for the device to use + * sector - the sector number we need to read + * sectors - the number of sectors + * + */ +int hh501_read_data(struct us_data *us, + unsigned int sector, + unsigned int sectors) { +#endif + + int result = USB_STOR_TRANSPORT_GOOD; + unsigned char *command = us->iobuf; + struct hh501_card_info *info = (struct hh501_card_info *)us->extra; + unsigned char *buffer; + + unsigned int pba, lba, page; + unsigned long address; + + unsigned short pages; + unsigned int len, index, offset; + + // Since we only read in one block at a time, we have to create + // a bounce buffer and move the data a piece at a time between the + // bounce buffer and the actual transfer buffer. + + len = min((unsigned int) sectors, (unsigned int) info->blocksize >> + info->smallpageshift) * 512; + buffer = kmalloc(len, GFP_NOIO); + if (buffer == NULL) + return USB_STOR_TRANSPORT_ERROR; /* out of memory */ + index = offset = 0; + + while (sectors>0) { + + /* Convert Logical Sector -> Physical sector on the smartmedia card */ + lba = sector >> info->blockshift; + page = sector & info->blockmask; + + if (lba >= info->max_log_blks) { + US_DEBUGP ("Error: Requested LBA %04X exceeds maximum block %04X\n", + lba, info->max_log_blks-1 ); + result = USB_STOR_TRANSPORT_ERROR; + goto leave; + } + + pba = info->lba_to_pba[lba]; + + // Read as many sectors as possible in this block + + pages = min((unsigned int) sectors << info->smallpageshift, + info->blocksize - page); + pages = 1; + len = pages << info->pageshift; + + US_DEBUGP("Read %02X pages, from PBA %04X" + " (LBA %04X) page %02X\n", + pages, pba, lba, page); + + if (pba == 0xFFFFFFFF) { + /* no pba for this lba, fill with zeroes */ + memset (buffer, 0, len); + } else { + + address = (pba << info->blockshift) + page; + + command[0] = HH501_CMD_READ_BLOCK; + if (info->capacity >= 0x4000000) { + command[3] = address >> 16; + command[2] = address >> 8; + command[1] = address & 0xFF; + } else { + command[3] = address >> 8; + command[2] = address & 0xFF; + command[1] = 0; + } + command[5] = len >> 8; + command[4] = len & 0xFF; + command[6] = 0; + command[7] = 0; + + /* send command */ + result = hh501_bulk_send(us, PIPE_WRITE_COMMAND, command, 8); + + if (result != USB_STOR_XFER_GOOD) { + US_DEBUGP("hh501_read_data: Result for write_command %d\n", + result); + result = USB_STOR_TRANSPORT_ERROR; + goto leave; + } + + /* read data */ + result = hh501_bulk_send(us, PIPE_READ_DATA, buffer, len); + + if (result != USB_STOR_XFER_GOOD) { + US_DEBUGP("hh501_read_data: Result for read_data %d\n", result); + result = USB_STOR_TRANSPORT_ERROR; + goto leave; + } + + } + + // Store the data in the transfer buffer + usb_stor_access_xfer_buf(buffer, len, us->srb, + &index, &offset, TO_XFER_BUF); + + sector++; + sectors-=pages; + //page = 0; + //lba++; + //sectors -= pages >> info->smallpageshift; + } + + US_DEBUGP("hh501_read_data: perfect\n"); + result = USB_STOR_TRANSPORT_GOOD; + +leave: + kfree(buffer); + + return result; +} + + + + +/* + * Transport for the HH501 Smartmedia reader + */ +int hh501_transport(struct scsi_cmnd *srb, struct us_data *us) +{ + int result; + static unsigned char inquiry_response[8] = { + 0x00, 0x80, 0x00, 0x02, 0x1F, 0x00, 0x00, 0x00 + }; + // write-protected for now, no block descriptor support + static unsigned char mode_page_01[20] = { + 0x0, 0x12, 0x80, 0x80, 0x0, 0x0, 0x0, 0x0, + 0x01, 0x0A, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 + }; + unsigned char *ptr = us->iobuf; + unsigned long capacity; + unsigned int lba; + unsigned int pba; + unsigned int page; + unsigned short pages; + struct hh501_card_info *info; + + info = (struct hh501_card_info *)(us->extra); + + if (srb->cmnd[0] == REQUEST_SENSE) { + US_DEBUGP("hh501_transport: REQUEST_SENSE. Returning faked response\n"); + + memcpy (ptr, info->sense_data, sizeof info->sense_data); + ptr[0] = 0x70; + ptr[7] = 11; + usb_stor_set_xfer_buf (ptr, sizeof info->sense_data, srb); + memset (info->sense_data, 0, sizeof info->sense_data); + + return USB_STOR_TRANSPORT_GOOD; + } + + memset (info->sense_data, 0, sizeof info->sense_data); + + /* Fake INQUIRY */ + if (srb->cmnd[0] == INQUIRY) { + memcpy(ptr, inquiry_response, 8); + fill_inquiry_response(us, ptr, 36); + return USB_STOR_TRANSPORT_GOOD; + } + + /* only check card status if the map isn't allocated, ie no card seen yet + * or if it's been over half a second since we last accessed it + */ + if (info->lba_to_pba == NULL || time_after(jiffies, info->last_access + HZ/2)) { + + /* check to see if a card is fitted */ + result = hh501_status (us); + if (result) { + return USB_STOR_TRANSPORT_FAILED; + } + } + + /* if we detected a problem with the map when writing, + don't allow any more access */ + if (info->fatal_error) { + + set_sense_info (3, 0x31, 0); + return USB_STOR_TRANSPORT_FAILED; + } + + if (srb->cmnd[0] == READ_CAPACITY) { + + capacity = hh501_get_capacity(us); + + if (!capacity || info->pageshift==8) { + set_sense_info (3, 0x30, 0); /* incompatible medium */ + return USB_STOR_TRANSPORT_FAILED; + } + + info->capacity = capacity; + /* figure out the maximum logical block number, allowing for + * the fact that only 250 out of every 256 are used */ + info->max_log_blks = ((info->capacity >> (info->pageshift + info->blockshift)) / 256) * 250; + capacity = (capacity / 256) * 250; + + capacity >>= info->pageshift; + capacity--; + + ((u32 *) ptr)[0] = cpu_to_be32(capacity); + ((u32 *) ptr)[1] = cpu_to_be32(1<pageshift); + usb_stor_set_xfer_buf(ptr, 8, srb); + + hh501_read_map(us); + + return USB_STOR_TRANSPORT_GOOD; + } + + if (srb->cmnd[0] == MODE_SENSE_10) { + + memcpy(ptr, mode_page_01, sizeof mode_page_01); + ptr[3] = (info->read_only || info->force_read_only) ? 0x80 : 0; + usb_stor_set_xfer_buf(ptr, sizeof(mode_page_01), srb); + + if ( (srb->cmnd[2] & 0x3F) == 0x01 ) { + US_DEBUGP( + "hh501_transport: Dummy up request for mode page 1\n"); + return USB_STOR_TRANSPORT_GOOD; + + } else if ( (srb->cmnd[2] & 0x3F) == 0x3F ) { + US_DEBUGP( + "hh501_transport: Dummy up request for all mode pages\n"); + return USB_STOR_TRANSPORT_GOOD; + } + + set_sense_info (5, 0x24, 0); /* invalid field in command */ + return USB_STOR_TRANSPORT_FAILED; + } + + if (srb->cmnd[0] == ALLOW_MEDIUM_REMOVAL) { + + US_DEBUGP( + "hh501_transport: %s medium removal. Not that I can do" + " anything about it...\n", + (srb->cmnd[4]&0x03) ? "Prevent" : "Allow"); + + return USB_STOR_TRANSPORT_GOOD; + + } + + if (srb->cmnd[0] == READ_10 /*|| srb->cmnd[0] == WRITE_10*/) { + + page = short_pack(srb->cmnd[3], srb->cmnd[2]); + page <<= 16; + page |= short_pack(srb->cmnd[5], srb->cmnd[4]); + pages = short_pack(srb->cmnd[8], srb->cmnd[7]); + + return hh501_read_data(us, page, pages); + +#if 0 + page <<= info->smallpageshift; + + // convert page to block and page-within-block + + lba = page >> info->blockshift; + page = page & info->blockmask; + + // locate physical block corresponding to logical block + + if (lba >= (info->capacity >> (info->blockshift + info->pageshift)) /* info->numblocks */) { + + US_DEBUGP("Error: Requested LBA %04X exceeds maximum " + "block %04X\n", lba, info->max_log_blks-1); + + set_sense_info (5, 0x24, 0); /* invalid field in command */ + + return USB_STOR_TRANSPORT_FAILED; + } + + pba = info->lba_to_pba[lba]; + + if (srb->cmnd[0] == WRITE_10) { + US_DEBUGP("WRITE_10: write block %04X (LBA %04X) page %01X" + " pages %d\n", + pba, lba, page, pages); + + //return hh501_write_data(us, lba, page, pages); + } else { + US_DEBUGP("READ_10: read block %04X (LBA %04X) page %01X" + " pages %d\n", + pba, lba, page, pages); + + return hh501_read_data(us, lba, page, pages); + } +#endif + } + + + if (srb->cmnd[0] == TEST_UNIT_READY) { + return USB_STOR_TRANSPORT_GOOD; + } + + if (srb->cmnd[0] == START_STOP) { + return USB_STOR_TRANSPORT_GOOD; + } + + set_sense_info (5, 0x20, 0); /* illegal command */ + + return USB_STOR_TRANSPORT_FAILED; // FIXME: sense buffer? +} + + diff -Naur linux-2.6.10-ac1.orig/drivers/usb/storage/hh501.h ../linux-2.6.10-ac1/drivers/usb/storage/hh501.h --- linux-2.6.10-ac1.orig/drivers/usb/storage/hh501.h 1970-01-01 01:00:00.000000000 +0100 +++ ../linux-2.6.10-ac1/drivers/usb/storage/hh501.h 2004-12-27 11:45:36.000000000 +0100 @@ -0,0 +1,57 @@ +/* + * Driver for Foxconn HH501 USB SmartMedia reader/writer + * + * $Id$ + * + * Current development and maintenance by: + * (c) 2001-2004 Luc Saillard + * + * See hh501.c for more explanation + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2, or (at your option) any + * later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +#ifndef _USB_HH501_H +#define _USB_HH501_H + +extern int hh501_transport(struct scsi_cmnd *srb, struct us_data *us); +extern int hh501_init(struct us_data *us); + +struct hh501_card_info { + + unsigned long capacity; /* Size of card in bytes */ + int max_log_blks; /* maximum number of logical blocks */ + int pageshift; /* log2 of pagesize */ + int smallpageshift; /* 1 if pagesize == 256 */ + int blocksize; /* Size of block in pages */ + int blockshift; /* log2 of blocksize */ + int blockmask; /* 2^blockshift - 1 */ + int read_only; /* non zero if card is write protected */ + int force_read_only; /* non zero if we find a map error*/ + int *lba_to_pba; /* logical to physical map */ + int *pba_to_lba; /* physical to logical map */ + int fatal_error; /* set if we detect something nasty */ + unsigned long last_access; /* number of jiffies since we last talked to device */ + unsigned char sense_data[18]; /* buffer used to send response to the SCSI layer */ + unsigned int pipe[4]; +#define PIPE_WRITE_COMMAND 0 +#define PIPE_READ_STATUS 1 +#define PIPE_WRITE_DATA 2 +#define PIPE_READ_DATA 3 + +}; + +#endif + diff -Naur linux-2.6.10-ac1.orig/drivers/usb/storage/transport.h ../linux-2.6.10-ac1/drivers/usb/storage/transport.h --- linux-2.6.10-ac1.orig/drivers/usb/storage/transport.h 2004-12-24 22:34:32.000000000 +0100 +++ ../linux-2.6.10-ac1/drivers/usb/storage/transport.h 2004-12-27 11:45:36.000000000 +0100 @@ -75,6 +75,10 @@ #define US_PR_JUMPSHOT 0xf3 /* Lexar Jumpshot */ #endif +#ifdef CONFIG_USB_STORAGE_HH501 +#define US_PR_HH501 0xf4 /* HH501 */ +#endif + #define US_PR_DEVICE 0xff /* Use device's value */ /* diff -Naur linux-2.6.10-ac1.orig/drivers/usb/storage/unusual_devs.h ../linux-2.6.10-ac1/drivers/usb/storage/unusual_devs.h --- linux-2.6.10-ac1.orig/drivers/usb/storage/unusual_devs.h 2004-12-24 22:35:24.000000000 +0100 +++ ../linux-2.6.10-ac1/drivers/usb/storage/unusual_devs.h 2004-12-27 11:45:36.000000000 +0100 @@ -116,6 +116,14 @@ US_SC_SCSI, US_PR_BULK, NULL, US_FL_FIX_INQUIRY), +#ifdef CONFIG_USB_STORAGE_HH501 +UNUSUAL_DEV( 0x0489, 0x0503, 0x0000, 0x9999, + "Foxconn", + "HH501", + US_SC_SCSI, US_PR_HH501, hh501_init, + US_FL_SINGLE_LUN ), +#endif + /* Reported by Paul Stewart * This entry is needed because the device reports Sub=ff */ UNUSUAL_DEV( 0x04a4, 0x0004, 0x0001, 0x0001, diff -Naur linux-2.6.10-ac1.orig/drivers/usb/storage/usb.c ../linux-2.6.10-ac1/drivers/usb/storage/usb.c --- linux-2.6.10-ac1.orig/drivers/usb/storage/usb.c 2004-12-24 22:34:27.000000000 +0100 +++ ../linux-2.6.10-ac1/drivers/usb/storage/usb.c 2004-12-27 11:45:36.000000000 +0100 @@ -87,6 +87,9 @@ #ifdef CONFIG_USB_STORAGE_JUMPSHOT #include "jumpshot.h" #endif +#ifdef CONFIG_USB_STORAGE_HH501 +#include "hh501.h" +#endif #include @@ -633,6 +636,15 @@ break; #endif +#ifdef CONFIG_USB_STORAGE_HH501 + case US_PR_HH501: + us->transport_name = "HH501 Bulk-Only"; + us->transport = hh501_transport; + us->transport_reset = usb_stor_Bulk_reset; + us->max_lun = 0; + break; +#endif + default: return -EIO; }