Linux Audio

Check our new training course

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