Linux Audio

Check our new training course

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