Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    1) // SPDX-License-Identifier: GPL-2.0+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    3)  * Driver for Alauda-based card readers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5)  * Current development and maintenance by:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6)  *   (c) 2005 Daniel Drake <dsd@gentoo.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8)  * The 'Alauda' is a chip manufacturered by RATOC for OEM use.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10)  * Alauda implements a vendor-specific command set to access two media reader
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11)  * ports (XD, SmartMedia). This driver converts SCSI commands to the commands
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12)  * which are accepted by these devices.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14)  * The driver was developed through reverse-engineering, with the help of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15)  * sddr09 driver which has many similarities, and with some help from the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16)  * (very old) vendor-supplied GPL sma03 driver.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18)  * For protocol info, see http://alauda.sourceforge.net
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24) #include <scsi/scsi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25) #include <scsi/scsi_cmnd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26) #include <scsi/scsi_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28) #include "usb.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29) #include "transport.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30) #include "protocol.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31) #include "debug.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32) #include "scsiglue.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34) #define DRV_NAME "ums-alauda"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36) MODULE_DESCRIPTION("Driver for Alauda-based card readers");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37) MODULE_AUTHOR("Daniel Drake <dsd@gentoo.org>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39) MODULE_IMPORT_NS(USB_STORAGE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42)  * Status bytes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44) #define ALAUDA_STATUS_ERROR		0x01
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45) #define ALAUDA_STATUS_READY		0x40
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48)  * Control opcodes (for request field)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50) #define ALAUDA_GET_XD_MEDIA_STATUS	0x08
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51) #define ALAUDA_GET_SM_MEDIA_STATUS	0x98
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52) #define ALAUDA_ACK_XD_MEDIA_CHANGE	0x0a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53) #define ALAUDA_ACK_SM_MEDIA_CHANGE	0x9a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54) #define ALAUDA_GET_XD_MEDIA_SIG		0x86
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55) #define ALAUDA_GET_SM_MEDIA_SIG		0x96
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58)  * Bulk command identity (byte 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60) #define ALAUDA_BULK_CMD			0x40
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63)  * Bulk opcodes (byte 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65) #define ALAUDA_BULK_GET_REDU_DATA	0x85
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66) #define ALAUDA_BULK_READ_BLOCK		0x94
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67) #define ALAUDA_BULK_ERASE_BLOCK		0xa3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68) #define ALAUDA_BULK_WRITE_BLOCK		0xb4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69) #define ALAUDA_BULK_GET_STATUS2		0xb7
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70) #define ALAUDA_BULK_RESET_MEDIA		0xe0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73)  * Port to operate on (byte 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75) #define ALAUDA_PORT_XD			0x00
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76) #define ALAUDA_PORT_SM			0x01
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79)  * LBA and PBA are unsigned ints. Special values.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81) #define UNDEF    0xffff
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82) #define SPARE    0xfffe
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83) #define UNUSABLE 0xfffd
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85) struct alauda_media_info {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86) 	unsigned long capacity;		/* total media size in bytes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87) 	unsigned int pagesize;		/* page size in bytes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88) 	unsigned int blocksize;		/* number of pages per block */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89) 	unsigned int uzonesize;		/* number of usable blocks per zone */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90) 	unsigned int zonesize;		/* number of blocks per zone */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91) 	unsigned int blockmask;		/* mask to get page from address */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93) 	unsigned char pageshift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94) 	unsigned char blockshift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95) 	unsigned char zoneshift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97) 	u16 **lba_to_pba;		/* logical to physical block map */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98) 	u16 **pba_to_lba;		/* physical to logical block map */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101) struct alauda_info {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102) 	struct alauda_media_info port[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103) 	int wr_ep;			/* endpoint to write data out of */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105) 	unsigned char sense_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106) 	unsigned long sense_asc;	/* additional sense code */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107) 	unsigned long sense_ascq;	/* additional sense code qualifier */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110) #define short_pack(lsb,msb) ( ((u16)(lsb)) | ( ((u16)(msb))<<8 ) )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111) #define LSB_of(s) ((s)&0xFF)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112) #define MSB_of(s) ((s)>>8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114) #define MEDIA_PORT(us) us->srb->device->lun
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115) #define MEDIA_INFO(us) ((struct alauda_info *)us->extra)->port[MEDIA_PORT(us)]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117) #define PBA_LO(pba) ((pba & 0xF) << 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118) #define PBA_HI(pba) (pba >> 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119) #define PBA_ZONE(pba) (pba >> 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121) static int init_alauda(struct us_data *us);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125)  * The table of devices
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127) #define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) 		    vendorName, productName, useProtocol, useTransport, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) 		    initFunction, flags) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) { USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131)   .driver_info = (flags) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) static struct usb_device_id alauda_usb_ids[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) #	include "unusual_alauda.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135) 	{ }		/* Terminating entry */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) MODULE_DEVICE_TABLE(usb, alauda_usb_ids);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) #undef UNUSUAL_DEV
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142)  * The flags table
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) #define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) 		    vendor_name, product_name, use_protocol, use_transport, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146) 		    init_function, Flags) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148) 	.vendorName = vendor_name,	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149) 	.productName = product_name,	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150) 	.useProtocol = use_protocol,	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151) 	.useTransport = use_transport,	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152) 	.initFunction = init_function,	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) static struct us_unusual_dev alauda_unusual_dev_list[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) #	include "unusual_alauda.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) 	{ }		/* Terminating entry */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) #undef UNUSUAL_DEV
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164)  * Media handling
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167) struct alauda_card_info {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) 	unsigned char id;		/* id byte */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169) 	unsigned char chipshift;	/* 1<<cs bytes total capacity */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170) 	unsigned char pageshift;	/* 1<<ps bytes in a page */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171) 	unsigned char blockshift;	/* 1<<bs pages per block */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) 	unsigned char zoneshift;	/* 1<<zs blocks per zone */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) static struct alauda_card_info alauda_card_ids[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) 	/* NAND flash */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177) 	{ 0x6e, 20, 8, 4, 8},	/* 1 MB */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) 	{ 0xe8, 20, 8, 4, 8},	/* 1 MB */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) 	{ 0xec, 20, 8, 4, 8},	/* 1 MB */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180) 	{ 0x64, 21, 8, 4, 9}, 	/* 2 MB */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181) 	{ 0xea, 21, 8, 4, 9},	/* 2 MB */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) 	{ 0x6b, 22, 9, 4, 9},	/* 4 MB */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) 	{ 0xe3, 22, 9, 4, 9},	/* 4 MB */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) 	{ 0xe5, 22, 9, 4, 9},	/* 4 MB */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) 	{ 0xe6, 23, 9, 4, 10},	/* 8 MB */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) 	{ 0x73, 24, 9, 5, 10},	/* 16 MB */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) 	{ 0x75, 25, 9, 5, 10},	/* 32 MB */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) 	{ 0x76, 26, 9, 5, 10},	/* 64 MB */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) 	{ 0x79, 27, 9, 5, 10},	/* 128 MB */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) 	{ 0x71, 28, 9, 5, 10},	/* 256 MB */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) 	/* MASK ROM */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) 	{ 0x5d, 21, 9, 4, 8},	/* 2 MB */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) 	{ 0xd5, 22, 9, 4, 9},	/* 4 MB */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195) 	{ 0xd6, 23, 9, 4, 10},	/* 8 MB */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) 	{ 0x57, 24, 9, 4, 11},	/* 16 MB */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197) 	{ 0x58, 25, 9, 4, 12},	/* 32 MB */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) 	{ 0,}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) static struct alauda_card_info *alauda_card_find_id(unsigned char id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205) 	for (i = 0; alauda_card_ids[i].id != 0; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206) 		if (alauda_card_ids[i].id == id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207) 			return &(alauda_card_ids[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212)  * ECC computation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215) static unsigned char parity[256];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216) static unsigned char ecc2[256];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) static void nand_init_ecc(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) 	int i, j, a;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) 	parity[0] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) 	for (i = 1; i < 256; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) 		parity[i] = (parity[i&(i-1)] ^ 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) 	for (i = 0; i < 256; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) 		a = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) 		for (j = 0; j < 8; j++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) 			if (i & (1<<j)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) 				if ((j & 1) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) 					a ^= 0x04;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) 				if ((j & 2) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) 					a ^= 0x10;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) 				if ((j & 4) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) 					a ^= 0x40;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) 		ecc2[i] = ~(a ^ (a<<1) ^ (parity[i] ? 0xa8 : 0));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) /* compute 3-byte ecc on 256 bytes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) static void nand_compute_ecc(unsigned char *data, unsigned char *ecc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) 	int i, j, a;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) 	unsigned char par = 0, bit, bits[8] = {0};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) 	/* collect 16 checksum bits */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) 	for (i = 0; i < 256; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) 		par ^= data[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) 		bit = parity[data[i]];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) 		for (j = 0; j < 8; j++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) 			if ((i & (1<<j)) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) 				bits[j] ^= bit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) 	/* put 4+4+4 = 12 bits in the ecc */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) 	a = (bits[3] << 6) + (bits[2] << 4) + (bits[1] << 2) + bits[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) 	ecc[0] = ~(a ^ (a<<1) ^ (parity[par] ? 0xaa : 0));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) 	a = (bits[7] << 6) + (bits[6] << 4) + (bits[5] << 2) + bits[4];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) 	ecc[1] = ~(a ^ (a<<1) ^ (parity[par] ? 0xaa : 0));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) 	ecc[2] = ecc2[par];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) static int nand_compare_ecc(unsigned char *data, unsigned char *ecc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) 	return (data[0] == ecc[0] && data[1] == ecc[1] && data[2] == ecc[2]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) static void nand_store_ecc(unsigned char *data, unsigned char *ecc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) 	memcpy(data, ecc, 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278)  * Alauda driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282)  * Forget our PBA <---> LBA mappings for a particular port
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) static void alauda_free_maps (struct alauda_media_info *media_info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) 	unsigned int shift = media_info->zoneshift
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) 		+ media_info->blockshift + media_info->pageshift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) 	unsigned int num_zones = media_info->capacity >> shift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) 	unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) 	if (media_info->lba_to_pba != NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) 		for (i = 0; i < num_zones; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) 			kfree(media_info->lba_to_pba[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294) 			media_info->lba_to_pba[i] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) 	if (media_info->pba_to_lba != NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) 		for (i = 0; i < num_zones; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) 			kfree(media_info->pba_to_lba[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) 			media_info->pba_to_lba[i] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305)  * Returns 2 bytes of status data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306)  * The first byte describes media status, and second byte describes door status
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) static int alauda_get_media_status(struct us_data *us, unsigned char *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) 	unsigned char command;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) 	if (MEDIA_PORT(us) == ALAUDA_PORT_XD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) 		command = ALAUDA_GET_XD_MEDIA_STATUS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) 		command = ALAUDA_GET_SM_MEDIA_STATUS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) 	rc = usb_stor_ctrl_transfer(us, us->recv_ctrl_pipe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) 		command, 0xc0, 0, 1, data, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) 	usb_stor_dbg(us, "Media status %02X %02X\n", data[0], data[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327)  * Clears the "media was changed" bit so that we know when it changes again
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328)  * in the future.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) static int alauda_ack_media(struct us_data *us)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) 	unsigned char command;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) 	if (MEDIA_PORT(us) == ALAUDA_PORT_XD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) 		command = ALAUDA_ACK_XD_MEDIA_CHANGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337) 		command = ALAUDA_ACK_SM_MEDIA_CHANGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339) 	return usb_stor_ctrl_transfer(us, us->send_ctrl_pipe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) 		command, 0x40, 0, 1, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344)  * Retrieves a 4-byte media signature, which indicates manufacturer, capacity,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345)  * and some other details.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) static int alauda_get_media_signature(struct us_data *us, unsigned char *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) 	unsigned char command;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) 	if (MEDIA_PORT(us) == ALAUDA_PORT_XD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) 		command = ALAUDA_GET_XD_MEDIA_SIG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) 		command = ALAUDA_GET_SM_MEDIA_SIG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) 	return usb_stor_ctrl_transfer(us, us->recv_ctrl_pipe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357) 		command, 0xc0, 0, 0, data, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361)  * Resets the media status (but not the whole device?)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) static int alauda_reset_media(struct us_data *us)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) 	unsigned char *command = us->iobuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) 	memset(command, 0, 9);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) 	command[0] = ALAUDA_BULK_CMD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) 	command[1] = ALAUDA_BULK_RESET_MEDIA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) 	command[8] = MEDIA_PORT(us);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) 	return usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) 		command, 9, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377)  * Examines the media and deduces capacity, etc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) static int alauda_init_media(struct us_data *us)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) 	unsigned char *data = us->iobuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) 	int ready = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) 	struct alauda_card_info *media_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) 	unsigned int num_zones;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) 	while (ready == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) 		msleep(20);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) 		if (alauda_get_media_status(us, data) != USB_STOR_XFER_GOOD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) 			return USB_STOR_TRANSPORT_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) 		if (data[0] & 0x10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) 			ready = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396) 	usb_stor_dbg(us, "We are ready for action!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398) 	if (alauda_ack_media(us) != USB_STOR_XFER_GOOD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399) 		return USB_STOR_TRANSPORT_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) 	msleep(10);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) 	if (alauda_get_media_status(us, data) != USB_STOR_XFER_GOOD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) 		return USB_STOR_TRANSPORT_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) 	if (data[0] != 0x14) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) 		usb_stor_dbg(us, "Media not ready after ack\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) 		return USB_STOR_TRANSPORT_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) 	if (alauda_get_media_signature(us, data) != USB_STOR_XFER_GOOD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) 		return USB_STOR_TRANSPORT_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) 	usb_stor_dbg(us, "Media signature: %4ph\n", data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) 	media_info = alauda_card_find_id(data[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) 	if (media_info == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) 		pr_warn("alauda_init_media: Unrecognised media signature: %4ph\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418) 			data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419) 		return USB_STOR_TRANSPORT_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) 	MEDIA_INFO(us).capacity = 1 << media_info->chipshift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) 	usb_stor_dbg(us, "Found media with capacity: %ldMB\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) 		     MEDIA_INFO(us).capacity >> 20);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) 	MEDIA_INFO(us).pageshift = media_info->pageshift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) 	MEDIA_INFO(us).blockshift = media_info->blockshift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) 	MEDIA_INFO(us).zoneshift = media_info->zoneshift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430) 	MEDIA_INFO(us).pagesize = 1 << media_info->pageshift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) 	MEDIA_INFO(us).blocksize = 1 << media_info->blockshift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) 	MEDIA_INFO(us).zonesize = 1 << media_info->zoneshift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) 	MEDIA_INFO(us).uzonesize = ((1 << media_info->zoneshift) / 128) * 125;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) 	MEDIA_INFO(us).blockmask = MEDIA_INFO(us).blocksize - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) 	num_zones = MEDIA_INFO(us).capacity >> (MEDIA_INFO(us).zoneshift
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) 		+ MEDIA_INFO(us).blockshift + MEDIA_INFO(us).pageshift);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) 	MEDIA_INFO(us).pba_to_lba = kcalloc(num_zones, sizeof(u16*), GFP_NOIO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) 	MEDIA_INFO(us).lba_to_pba = kcalloc(num_zones, sizeof(u16*), GFP_NOIO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) 	if (alauda_reset_media(us) != USB_STOR_XFER_GOOD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) 		return USB_STOR_TRANSPORT_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) 	return USB_STOR_TRANSPORT_GOOD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449)  * Examines the media status and does the right thing when the media has gone,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450)  * appeared, or changed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) static int alauda_check_media(struct us_data *us)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) 	struct alauda_info *info = (struct alauda_info *) us->extra;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) 	unsigned char status[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) 	alauda_get_media_status(us, status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) 	/* Check for no media or door open */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) 	if ((status[0] & 0x80) || ((status[0] & 0x1F) == 0x10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) 		|| ((status[1] & 0x01) == 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) 		usb_stor_dbg(us, "No media, or door open\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) 		alauda_free_maps(&MEDIA_INFO(us));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) 		info->sense_key = 0x02;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) 		info->sense_asc = 0x3A;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) 		info->sense_ascq = 0x00;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) 		return USB_STOR_TRANSPORT_FAILED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) 	/* Check for media change */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) 	if (status[0] & 0x08) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) 		usb_stor_dbg(us, "Media change detected\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) 		alauda_free_maps(&MEDIA_INFO(us));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) 		alauda_init_media(us);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) 		info->sense_key = UNIT_ATTENTION;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) 		info->sense_asc = 0x28;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) 		info->sense_ascq = 0x00;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) 		return USB_STOR_TRANSPORT_FAILED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) 	return USB_STOR_TRANSPORT_GOOD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486)  * Checks the status from the 2nd status register
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487)  * Returns 3 bytes of status data, only the first is known
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) static int alauda_check_status2(struct us_data *us)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) 	unsigned char command[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) 		ALAUDA_BULK_CMD, ALAUDA_BULK_GET_STATUS2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) 		0, 0, 0, 0, 3, 0, MEDIA_PORT(us)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496) 	unsigned char data[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) 	rc = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) 		command, 9, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) 	if (rc != USB_STOR_XFER_GOOD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) 	rc = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) 		data, 3, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) 	if (rc != USB_STOR_XFER_GOOD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) 	usb_stor_dbg(us, "%3ph\n", data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) 	if (data[0] & ALAUDA_STATUS_ERROR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) 		return USB_STOR_XFER_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) 	return USB_STOR_XFER_GOOD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516)  * Gets the redundancy data for the first page of a PBA
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517)  * Returns 16 bytes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) static int alauda_get_redu_data(struct us_data *us, u16 pba, unsigned char *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) 	unsigned char command[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) 		ALAUDA_BULK_CMD, ALAUDA_BULK_GET_REDU_DATA,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) 		PBA_HI(pba), PBA_ZONE(pba), 0, PBA_LO(pba), 0, 0, MEDIA_PORT(us)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) 	rc = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) 		command, 9, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) 	if (rc != USB_STOR_XFER_GOOD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) 	return usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) 		data, 16, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537)  * Finds the first unused PBA in a zone
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538)  * Returns the absolute PBA of an unused PBA, or 0 if none found.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) static u16 alauda_find_unused_pba(struct alauda_media_info *info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) 	unsigned int zone)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) 	u16 *pba_to_lba = info->pba_to_lba[zone];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) 	unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) 	for (i = 0; i < info->zonesize; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) 		if (pba_to_lba[i] == UNDEF)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) 			return (zone << info->zoneshift) + i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554)  * Reads the redundancy data for all PBA's in a zone
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555)  * Produces lba <--> pba mappings
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) static int alauda_read_map(struct us_data *us, unsigned int zone)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) 	unsigned char *data = us->iobuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) 	int result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) 	int i, j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) 	unsigned int zonesize = MEDIA_INFO(us).zonesize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) 	unsigned int uzonesize = MEDIA_INFO(us).uzonesize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) 	unsigned int lba_offset, lba_real, blocknum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) 	unsigned int zone_base_lba = zone * uzonesize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) 	unsigned int zone_base_pba = zone * zonesize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) 	u16 *lba_to_pba = kcalloc(zonesize, sizeof(u16), GFP_NOIO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) 	u16 *pba_to_lba = kcalloc(zonesize, sizeof(u16), GFP_NOIO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) 	if (lba_to_pba == NULL || pba_to_lba == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) 		result = USB_STOR_TRANSPORT_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) 		goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) 	usb_stor_dbg(us, "Mapping blocks for zone %d\n", zone);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) 	/* 1024 PBA's per zone */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) 	for (i = 0; i < zonesize; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) 		lba_to_pba[i] = pba_to_lba[i] = UNDEF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) 	for (i = 0; i < zonesize; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) 		blocknum = zone_base_pba + i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) 		result = alauda_get_redu_data(us, blocknum, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) 		if (result != USB_STOR_XFER_GOOD) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) 			result = USB_STOR_TRANSPORT_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) 			goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) 		/* special PBAs have control field 0^16 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) 		for (j = 0; j < 16; j++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) 			if (data[j] != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) 				goto nonz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) 		pba_to_lba[i] = UNUSABLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) 		usb_stor_dbg(us, "PBA %d has no logical mapping\n", blocknum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595) 		continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) 	nonz:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) 		/* unwritten PBAs have control field FF^16 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) 		for (j = 0; j < 16; j++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) 			if (data[j] != 0xff)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601) 				goto nonff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) 		continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) 	nonff:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) 		/* normal PBAs start with six FFs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) 		if (j < 6) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) 			usb_stor_dbg(us, "PBA %d has no logical mapping: reserved area = %02X%02X%02X%02X data status %02X block status %02X\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) 				     blocknum,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) 				     data[0], data[1], data[2], data[3],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) 				     data[4], data[5]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) 			pba_to_lba[i] = UNUSABLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) 		if ((data[6] >> 4) != 0x01) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) 			usb_stor_dbg(us, "PBA %d has invalid address field %02X%02X/%02X%02X\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) 				     blocknum, data[6], data[7],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) 				     data[11], data[12]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) 			pba_to_lba[i] = UNUSABLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) 		/* check even parity */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) 		if (parity[data[6] ^ data[7]]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) 			printk(KERN_WARNING
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) 			       "alauda_read_map: Bad parity in LBA for block %d"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) 			       " (%02X %02X)\n", i, data[6], data[7]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) 			pba_to_lba[i] = UNUSABLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) 		lba_offset = short_pack(data[7], data[6]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) 		lba_offset = (lba_offset & 0x07FF) >> 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) 		lba_real = lba_offset + zone_base_lba;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) 		 * Every 1024 physical blocks ("zone"), the LBA numbers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) 		 * go back to zero, but are within a higher block of LBA's.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) 		 * Also, there is a maximum of 1000 LBA's per zone.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) 		 * In other words, in PBA 1024-2047 you will find LBA 0-999
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) 		 * which are really LBA 1000-1999. This allows for 24 bad
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) 		 * or special physical blocks per zone.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) 		if (lba_offset >= uzonesize) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) 			printk(KERN_WARNING
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) 			       "alauda_read_map: Bad low LBA %d for block %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) 			       lba_real, blocknum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) 		if (lba_to_pba[lba_offset] != UNDEF) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) 			printk(KERN_WARNING
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) 			       "alauda_read_map: "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) 			       "LBA %d seen for PBA %d and %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) 			       lba_real, lba_to_pba[lba_offset], blocknum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) 		pba_to_lba[i] = lba_real;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) 		lba_to_pba[lba_offset] = blocknum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) 		continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) 	MEDIA_INFO(us).lba_to_pba[zone] = lba_to_pba;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666) 	MEDIA_INFO(us).pba_to_lba[zone] = pba_to_lba;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667) 	result = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668) 	goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) 	kfree(lba_to_pba);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) 	kfree(pba_to_lba);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) 	return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678)  * Checks to see whether we have already mapped a certain zone
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679)  * If we haven't, the map is generated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) static void alauda_ensure_map_for_zone(struct us_data *us, unsigned int zone)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) 	if (MEDIA_INFO(us).lba_to_pba[zone] == NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) 		|| MEDIA_INFO(us).pba_to_lba[zone] == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) 		alauda_read_map(us, zone);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689)  * Erases an entire block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) static int alauda_erase_block(struct us_data *us, u16 pba)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) 	unsigned char command[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) 		ALAUDA_BULK_CMD, ALAUDA_BULK_ERASE_BLOCK, PBA_HI(pba),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) 		PBA_ZONE(pba), 0, PBA_LO(pba), 0x02, 0, MEDIA_PORT(us)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) 	unsigned char buf[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) 	usb_stor_dbg(us, "Erasing PBA %d\n", pba);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) 	rc = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) 		command, 9, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) 	if (rc != USB_STOR_XFER_GOOD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) 	rc = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) 		buf, 2, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) 	if (rc != USB_STOR_XFER_GOOD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) 	usb_stor_dbg(us, "Erase result: %02X %02X\n", buf[0], buf[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717)  * Reads data from a certain offset page inside a PBA, including interleaved
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718)  * redundancy data. Returns (pagesize+64)*pages bytes in data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) static int alauda_read_block_raw(struct us_data *us, u16 pba,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) 		unsigned int page, unsigned int pages, unsigned char *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) 	unsigned char command[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) 		ALAUDA_BULK_CMD, ALAUDA_BULK_READ_BLOCK, PBA_HI(pba),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 		PBA_ZONE(pba), 0, PBA_LO(pba) + page, pages, 0, MEDIA_PORT(us)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) 	usb_stor_dbg(us, "pba %d page %d count %d\n", pba, page, pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) 	rc = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) 		command, 9, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) 	if (rc != USB_STOR_XFER_GOOD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) 	return usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) 		data, (MEDIA_INFO(us).pagesize + 64) * pages, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741)  * Reads data from a certain offset page inside a PBA, excluding redundancy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742)  * data. Returns pagesize*pages bytes in data. Note that data must be big enough
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743)  * to hold (pagesize+64)*pages bytes of data, but you can ignore those 'extra'
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744)  * trailing bytes outside this function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) static int alauda_read_block(struct us_data *us, u16 pba,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) 		unsigned int page, unsigned int pages, unsigned char *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) 	int i, rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) 	unsigned int pagesize = MEDIA_INFO(us).pagesize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) 	rc = alauda_read_block_raw(us, pba, page, pages, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) 	if (rc != USB_STOR_XFER_GOOD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) 	/* Cut out the redundancy data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) 	for (i = 0; i < pages; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) 		int dest_offset = i * pagesize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) 		int src_offset = i * (pagesize + 64);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) 		memmove(data + dest_offset, data + src_offset, pagesize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767)  * Writes an entire block of data and checks status after write.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768)  * Redundancy data must be already included in data. Data should be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769)  * (pagesize+64)*blocksize bytes in length.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) static int alauda_write_block(struct us_data *us, u16 pba, unsigned char *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) 	struct alauda_info *info = (struct alauda_info *) us->extra;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) 	unsigned char command[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) 		ALAUDA_BULK_CMD, ALAUDA_BULK_WRITE_BLOCK, PBA_HI(pba),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) 		PBA_ZONE(pba), 0, PBA_LO(pba), 32, 0, MEDIA_PORT(us)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) 	usb_stor_dbg(us, "pba %d\n", pba);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) 	rc = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) 		command, 9, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) 	if (rc != USB_STOR_XFER_GOOD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) 	rc = usb_stor_bulk_transfer_buf(us, info->wr_ep, data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) 		(MEDIA_INFO(us).pagesize + 64) * MEDIA_INFO(us).blocksize,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) 		NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) 	if (rc != USB_STOR_XFER_GOOD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) 	return alauda_check_status2(us);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797)  * Write some data to a specific LBA.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) static int alauda_write_lba(struct us_data *us, u16 lba,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) 		 unsigned int page, unsigned int pages,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) 		 unsigned char *ptr, unsigned char *blockbuffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) 	u16 pba, lbap, new_pba;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) 	unsigned char *bptr, *cptr, *xptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) 	unsigned char ecc[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) 	int i, result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) 	unsigned int uzonesize = MEDIA_INFO(us).uzonesize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) 	unsigned int zonesize = MEDIA_INFO(us).zonesize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) 	unsigned int pagesize = MEDIA_INFO(us).pagesize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) 	unsigned int blocksize = MEDIA_INFO(us).blocksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) 	unsigned int lba_offset = lba % uzonesize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) 	unsigned int new_pba_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) 	unsigned int zone = lba / uzonesize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) 	alauda_ensure_map_for_zone(us, zone);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) 	pba = MEDIA_INFO(us).lba_to_pba[zone][lba_offset];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) 	if (pba == 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) 		 * Maybe it is impossible to write to PBA 1.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) 		 * Fake success, but don't do anything.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) 		printk(KERN_WARNING
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) 		       "alauda_write_lba: avoid writing to pba 1\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) 		return USB_STOR_TRANSPORT_GOOD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) 	new_pba = alauda_find_unused_pba(&MEDIA_INFO(us), zone);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) 	if (!new_pba) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) 		printk(KERN_WARNING
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) 		       "alauda_write_lba: Out of unused blocks\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) 		return USB_STOR_TRANSPORT_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) 	/* read old contents */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) 	if (pba != UNDEF) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) 		result = alauda_read_block_raw(us, pba, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) 			blocksize, blockbuffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) 		if (result != USB_STOR_XFER_GOOD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) 			return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) 		memset(blockbuffer, 0, blocksize * (pagesize + 64));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845) 	lbap = (lba_offset << 1) | 0x1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846) 	if (parity[MSB_of(lbap) ^ LSB_of(lbap)])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) 		lbap ^= 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) 	/* check old contents and fill lba */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) 	for (i = 0; i < blocksize; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) 		bptr = blockbuffer + (i * (pagesize + 64));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) 		cptr = bptr + pagesize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) 		nand_compute_ecc(bptr, ecc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) 		if (!nand_compare_ecc(cptr+13, ecc)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) 			usb_stor_dbg(us, "Warning: bad ecc in page %d- of pba %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) 				     i, pba);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) 			nand_store_ecc(cptr+13, ecc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) 		nand_compute_ecc(bptr + (pagesize / 2), ecc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) 		if (!nand_compare_ecc(cptr+8, ecc)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) 			usb_stor_dbg(us, "Warning: bad ecc in page %d+ of pba %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) 				     i, pba);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) 			nand_store_ecc(cptr+8, ecc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) 		cptr[6] = cptr[11] = MSB_of(lbap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) 		cptr[7] = cptr[12] = LSB_of(lbap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) 	/* copy in new stuff and compute ECC */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) 	xptr = ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) 	for (i = page; i < page+pages; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) 		bptr = blockbuffer + (i * (pagesize + 64));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) 		cptr = bptr + pagesize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) 		memcpy(bptr, xptr, pagesize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) 		xptr += pagesize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) 		nand_compute_ecc(bptr, ecc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) 		nand_store_ecc(cptr+13, ecc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) 		nand_compute_ecc(bptr + (pagesize / 2), ecc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) 		nand_store_ecc(cptr+8, ecc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) 	result = alauda_write_block(us, new_pba, blockbuffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) 	if (result != USB_STOR_XFER_GOOD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) 		return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) 	new_pba_offset = new_pba - (zone * zonesize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) 	MEDIA_INFO(us).pba_to_lba[zone][new_pba_offset] = lba;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) 	MEDIA_INFO(us).lba_to_pba[zone][lba_offset] = new_pba;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) 	usb_stor_dbg(us, "Remapped LBA %d to PBA %d\n", lba, new_pba);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) 	if (pba != UNDEF) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) 		unsigned int pba_offset = pba - (zone * zonesize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) 		result = alauda_erase_block(us, pba);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) 		if (result != USB_STOR_XFER_GOOD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) 			return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) 		MEDIA_INFO(us).pba_to_lba[zone][pba_offset] = UNDEF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) 	return USB_STOR_TRANSPORT_GOOD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903)  * Read data from a specific sector address
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) static int alauda_read_data(struct us_data *us, unsigned long address,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) 		unsigned int sectors)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) 	unsigned char *buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) 	u16 lba, max_lba;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) 	unsigned int page, len, offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) 	unsigned int blockshift = MEDIA_INFO(us).blockshift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) 	unsigned int pageshift = MEDIA_INFO(us).pageshift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) 	unsigned int blocksize = MEDIA_INFO(us).blocksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) 	unsigned int pagesize = MEDIA_INFO(us).pagesize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) 	unsigned int uzonesize = MEDIA_INFO(us).uzonesize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) 	struct scatterlist *sg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) 	int result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) 	 * Since we only read in one block at a time, we have to create
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) 	 * a bounce buffer and move the data a piece at a time between the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) 	 * bounce buffer and the actual transfer buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) 	 * We make this buffer big enough to hold temporary redundancy data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) 	 * which we use when reading the data blocks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) 	len = min(sectors, blocksize) * (pagesize + 64);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) 	buffer = kmalloc(len, GFP_NOIO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) 	if (!buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) 		return USB_STOR_TRANSPORT_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) 	/* Figure out the initial LBA and page */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) 	lba = address >> blockshift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) 	page = (address & MEDIA_INFO(us).blockmask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) 	max_lba = MEDIA_INFO(us).capacity >> (blockshift + pageshift);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) 	result = USB_STOR_TRANSPORT_GOOD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) 	offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) 	sg = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) 	while (sectors > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) 		unsigned int zone = lba / uzonesize; /* integer division */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) 		unsigned int lba_offset = lba - (zone * uzonesize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) 		unsigned int pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) 		u16 pba;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) 		alauda_ensure_map_for_zone(us, zone);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) 		/* Not overflowing capacity? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) 		if (lba >= max_lba) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) 			usb_stor_dbg(us, "Error: Requested lba %u exceeds maximum %u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) 				     lba, max_lba);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) 			result = USB_STOR_TRANSPORT_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) 		/* Find number of pages we can read in this block */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) 		pages = min(sectors, blocksize - page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) 		len = pages << pageshift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) 		/* Find where this lba lives on disk */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) 		pba = MEDIA_INFO(us).lba_to_pba[zone][lba_offset];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) 		if (pba == UNDEF) {	/* this lba was never written */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) 			usb_stor_dbg(us, "Read %d zero pages (LBA %d) page %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) 				     pages, lba, page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) 			 * This is not really an error. It just means
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) 			 * that the block has never been written.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) 			 * Instead of returning USB_STOR_TRANSPORT_ERROR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) 			 * it is better to return all zero data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) 			memset(buffer, 0, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) 			usb_stor_dbg(us, "Read %d pages, from PBA %d (LBA %d) page %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) 				     pages, pba, lba, page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) 			result = alauda_read_block(us, pba, page, pages, buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) 			if (result != USB_STOR_TRANSPORT_GOOD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) 		/* Store the data in the transfer buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) 		usb_stor_access_xfer_buf(buffer, len, us->srb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) 				&sg, &offset, TO_XFER_BUF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) 		page = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) 		lba++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) 		sectors -= pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) 	kfree(buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) 	return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998)  * Write data to a specific sector address
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) static int alauda_write_data(struct us_data *us, unsigned long address,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) 		unsigned int sectors)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) 	unsigned char *buffer, *blockbuffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) 	unsigned int page, len, offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) 	unsigned int blockshift = MEDIA_INFO(us).blockshift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) 	unsigned int pageshift = MEDIA_INFO(us).pageshift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) 	unsigned int blocksize = MEDIA_INFO(us).blocksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) 	unsigned int pagesize = MEDIA_INFO(us).pagesize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) 	struct scatterlist *sg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) 	u16 lba, max_lba;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) 	int result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) 	 * Since we don't write the user data directly to the device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) 	 * we have to create a bounce buffer and move the data a piece
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) 	 * at a time between the bounce buffer and the actual transfer buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) 	len = min(sectors, blocksize) * pagesize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) 	buffer = kmalloc(len, GFP_NOIO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) 	if (!buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) 		return USB_STOR_TRANSPORT_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) 	 * We also need a temporary block buffer, where we read in the old data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) 	 * overwrite parts with the new data, and manipulate the redundancy data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) 	blockbuffer = kmalloc_array(pagesize + 64, blocksize, GFP_NOIO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) 	if (!blockbuffer) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) 		kfree(buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) 		return USB_STOR_TRANSPORT_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) 	/* Figure out the initial LBA and page */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) 	lba = address >> blockshift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) 	page = (address & MEDIA_INFO(us).blockmask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) 	max_lba = MEDIA_INFO(us).capacity >> (pageshift + blockshift);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) 	result = USB_STOR_TRANSPORT_GOOD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) 	offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) 	sg = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) 	while (sectors > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) 		/* Write as many sectors as possible in this block */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) 		unsigned int pages = min(sectors, blocksize - page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) 		len = pages << pageshift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) 		/* Not overflowing capacity? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) 		if (lba >= max_lba) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) 			usb_stor_dbg(us, "Requested lba %u exceeds maximum %u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) 				     lba, max_lba);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) 			result = USB_STOR_TRANSPORT_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) 		/* Get the data from the transfer buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) 		usb_stor_access_xfer_buf(buffer, len, us->srb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) 				&sg, &offset, FROM_XFER_BUF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) 		result = alauda_write_lba(us, lba, page, pages, buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) 			blockbuffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) 		if (result != USB_STOR_TRANSPORT_GOOD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) 		page = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) 		lba++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) 		sectors -= pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) 	kfree(buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) 	kfree(blockbuffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) 	return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076)  * Our interface with the rest of the world
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) static void alauda_info_destructor(void *extra)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) 	struct alauda_info *info = (struct alauda_info *) extra;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) 	int port;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) 	if (!info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) 	for (port = 0; port < 2; port++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) 		struct alauda_media_info *media_info = &info->port[port];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) 		alauda_free_maps(media_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) 		kfree(media_info->lba_to_pba);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) 		kfree(media_info->pba_to_lba);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097)  * Initialize alauda_info struct and find the data-write endpoint
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) static int init_alauda(struct us_data *us)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) 	struct alauda_info *info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) 	struct usb_host_interface *altsetting = us->pusb_intf->cur_altsetting;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) 	nand_init_ecc();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) 	us->extra = kzalloc(sizeof(struct alauda_info), GFP_NOIO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) 	if (!us->extra)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) 		return USB_STOR_TRANSPORT_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) 	info = (struct alauda_info *) us->extra;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) 	us->extra_destructor = alauda_info_destructor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) 	info->wr_ep = usb_sndbulkpipe(us->pusb_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) 		altsetting->endpoint[0].desc.bEndpointAddress
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) 		& USB_ENDPOINT_NUMBER_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) 	return USB_STOR_TRANSPORT_GOOD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) static int alauda_transport(struct scsi_cmnd *srb, struct us_data *us)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) 	struct alauda_info *info = (struct alauda_info *) us->extra;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) 	unsigned char *ptr = us->iobuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) 	static unsigned char inquiry_response[36] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) 		0x00, 0x80, 0x00, 0x01, 0x1F, 0x00, 0x00, 0x00
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) 	if (srb->cmnd[0] == INQUIRY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) 		usb_stor_dbg(us, "INQUIRY - Returning bogus response\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) 		memcpy(ptr, inquiry_response, sizeof(inquiry_response));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) 		fill_inquiry_response(us, ptr, 36);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) 		return USB_STOR_TRANSPORT_GOOD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) 	if (srb->cmnd[0] == TEST_UNIT_READY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) 		usb_stor_dbg(us, "TEST_UNIT_READY\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) 		return alauda_check_media(us);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) 	if (srb->cmnd[0] == READ_CAPACITY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) 		unsigned int num_zones;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) 		unsigned long capacity;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) 		rc = alauda_check_media(us);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) 		if (rc != USB_STOR_TRANSPORT_GOOD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) 			return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) 		num_zones = MEDIA_INFO(us).capacity >> (MEDIA_INFO(us).zoneshift
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) 			+ MEDIA_INFO(us).blockshift + MEDIA_INFO(us).pageshift);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) 		capacity = num_zones * MEDIA_INFO(us).uzonesize
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) 			* MEDIA_INFO(us).blocksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) 		/* Report capacity and page size */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) 		((__be32 *) ptr)[0] = cpu_to_be32(capacity - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) 		((__be32 *) ptr)[1] = cpu_to_be32(512);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) 		usb_stor_set_xfer_buf(ptr, 8, srb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) 		return USB_STOR_TRANSPORT_GOOD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) 	if (srb->cmnd[0] == READ_10) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) 		unsigned int page, pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) 		rc = alauda_check_media(us);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) 		if (rc != USB_STOR_TRANSPORT_GOOD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) 			return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) 		page = short_pack(srb->cmnd[3], srb->cmnd[2]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) 		page <<= 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) 		page |= short_pack(srb->cmnd[5], srb->cmnd[4]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) 		pages = short_pack(srb->cmnd[8], srb->cmnd[7]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) 		usb_stor_dbg(us, "READ_10: page %d pagect %d\n", page, pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) 		return alauda_read_data(us, page, pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) 	if (srb->cmnd[0] == WRITE_10) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) 		unsigned int page, pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) 		rc = alauda_check_media(us);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) 		if (rc != USB_STOR_TRANSPORT_GOOD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) 			return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) 		page = short_pack(srb->cmnd[3], srb->cmnd[2]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) 		page <<= 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) 		page |= short_pack(srb->cmnd[5], srb->cmnd[4]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) 		pages = short_pack(srb->cmnd[8], srb->cmnd[7]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) 		usb_stor_dbg(us, "WRITE_10: page %d pagect %d\n", page, pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) 		return alauda_write_data(us, page, pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) 	if (srb->cmnd[0] == REQUEST_SENSE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) 		usb_stor_dbg(us, "REQUEST_SENSE\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) 		memset(ptr, 0, 18);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) 		ptr[0] = 0xF0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) 		ptr[2] = info->sense_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) 		ptr[7] = 11;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) 		ptr[12] = info->sense_asc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) 		ptr[13] = info->sense_ascq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) 		usb_stor_set_xfer_buf(ptr, 18, srb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) 		return USB_STOR_TRANSPORT_GOOD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) 	if (srb->cmnd[0] == ALLOW_MEDIUM_REMOVAL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) 		 * sure.  whatever.  not like we can stop the user from popping
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) 		 * the media out of the device (no locking doors, etc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) 		return USB_STOR_TRANSPORT_GOOD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) 	usb_stor_dbg(us, "Gah! Unknown command: %d (0x%x)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) 		     srb->cmnd[0], srb->cmnd[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) 	info->sense_key = 0x05;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) 	info->sense_asc = 0x20;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) 	info->sense_ascq = 0x00;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) 	return USB_STOR_TRANSPORT_FAILED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) static struct scsi_host_template alauda_host_template;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) static int alauda_probe(struct usb_interface *intf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) 			 const struct usb_device_id *id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) 	struct us_data *us;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) 	int result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) 	result = usb_stor_probe1(&us, intf, id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) 			(id - alauda_usb_ids) + alauda_unusual_dev_list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) 			&alauda_host_template);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) 	if (result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) 		return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) 	us->transport_name  = "Alauda Control/Bulk";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) 	us->transport = alauda_transport;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) 	us->transport_reset = usb_stor_Bulk_reset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) 	us->max_lun = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) 	result = usb_stor_probe2(us);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) 	return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) static struct usb_driver alauda_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) 	.name =		DRV_NAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) 	.probe =	alauda_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) 	.disconnect =	usb_stor_disconnect,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) 	.suspend =	usb_stor_suspend,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) 	.resume =	usb_stor_resume,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) 	.reset_resume =	usb_stor_reset_resume,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) 	.pre_reset =	usb_stor_pre_reset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) 	.post_reset =	usb_stor_post_reset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) 	.id_table =	alauda_usb_ids,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) 	.soft_unbind =	1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) 	.no_dynamic_id = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) module_usb_stor_driver(alauda_driver, alauda_host_template, DRV_NAME);