Linux Audio

Check our new training course

Loading...
v4.10.11
 
   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		/*
 833		 * Maybe it is impossible to write to PBA 1.
 834		 * Fake success, but don't do anything.
 835		 */
 836		printk(KERN_WARNING
 837		       "alauda_write_lba: avoid writing to pba 1\n");
 838		return USB_STOR_TRANSPORT_GOOD;
 839	}
 840
 841	new_pba = alauda_find_unused_pba(&MEDIA_INFO(us), zone);
 842	if (!new_pba) {
 843		printk(KERN_WARNING
 844		       "alauda_write_lba: Out of unused blocks\n");
 845		return USB_STOR_TRANSPORT_ERROR;
 846	}
 847
 848	/* read old contents */
 849	if (pba != UNDEF) {
 850		result = alauda_read_block_raw(us, pba, 0,
 851			blocksize, blockbuffer);
 852		if (result != USB_STOR_XFER_GOOD)
 853			return result;
 854	} else {
 855		memset(blockbuffer, 0, blocksize * (pagesize + 64));
 856	}
 857
 858	lbap = (lba_offset << 1) | 0x1000;
 859	if (parity[MSB_of(lbap) ^ LSB_of(lbap)])
 860		lbap ^= 1;
 861
 862	/* check old contents and fill lba */
 863	for (i = 0; i < blocksize; i++) {
 864		bptr = blockbuffer + (i * (pagesize + 64));
 865		cptr = bptr + pagesize;
 866		nand_compute_ecc(bptr, ecc);
 867		if (!nand_compare_ecc(cptr+13, ecc)) {
 868			usb_stor_dbg(us, "Warning: bad ecc in page %d- of pba %d\n",
 869				     i, pba);
 870			nand_store_ecc(cptr+13, ecc);
 871		}
 872		nand_compute_ecc(bptr + (pagesize / 2), ecc);
 873		if (!nand_compare_ecc(cptr+8, ecc)) {
 874			usb_stor_dbg(us, "Warning: bad ecc in page %d+ of pba %d\n",
 875				     i, pba);
 876			nand_store_ecc(cptr+8, ecc);
 877		}
 878		cptr[6] = cptr[11] = MSB_of(lbap);
 879		cptr[7] = cptr[12] = LSB_of(lbap);
 880	}
 881
 882	/* copy in new stuff and compute ECC */
 883	xptr = ptr;
 884	for (i = page; i < page+pages; i++) {
 885		bptr = blockbuffer + (i * (pagesize + 64));
 886		cptr = bptr + pagesize;
 887		memcpy(bptr, xptr, pagesize);
 888		xptr += pagesize;
 889		nand_compute_ecc(bptr, ecc);
 890		nand_store_ecc(cptr+13, ecc);
 891		nand_compute_ecc(bptr + (pagesize / 2), ecc);
 892		nand_store_ecc(cptr+8, ecc);
 893	}
 894
 895	result = alauda_write_block(us, new_pba, blockbuffer);
 896	if (result != USB_STOR_XFER_GOOD)
 897		return result;
 898
 899	new_pba_offset = new_pba - (zone * zonesize);
 900	MEDIA_INFO(us).pba_to_lba[zone][new_pba_offset] = lba;
 901	MEDIA_INFO(us).lba_to_pba[zone][lba_offset] = new_pba;
 902	usb_stor_dbg(us, "Remapped LBA %d to PBA %d\n", lba, new_pba);
 903
 904	if (pba != UNDEF) {
 905		unsigned int pba_offset = pba - (zone * zonesize);
 906		result = alauda_erase_block(us, pba);
 907		if (result != USB_STOR_XFER_GOOD)
 908			return result;
 909		MEDIA_INFO(us).pba_to_lba[zone][pba_offset] = UNDEF;
 910	}
 911
 912	return USB_STOR_TRANSPORT_GOOD;
 913}
 914
 915/*
 916 * Read data from a specific sector address
 917 */
 918static int alauda_read_data(struct us_data *us, unsigned long address,
 919		unsigned int sectors)
 920{
 921	unsigned char *buffer;
 922	u16 lba, max_lba;
 923	unsigned int page, len, offset;
 924	unsigned int blockshift = MEDIA_INFO(us).blockshift;
 925	unsigned int pageshift = MEDIA_INFO(us).pageshift;
 926	unsigned int blocksize = MEDIA_INFO(us).blocksize;
 927	unsigned int pagesize = MEDIA_INFO(us).pagesize;
 928	unsigned int uzonesize = MEDIA_INFO(us).uzonesize;
 929	struct scatterlist *sg;
 930	int result;
 931
 932	/*
 933	 * Since we only read in one block at a time, we have to create
 934	 * a bounce buffer and move the data a piece at a time between the
 935	 * bounce buffer and the actual transfer buffer.
 936	 * We make this buffer big enough to hold temporary redundancy data,
 937	 * which we use when reading the data blocks.
 938	 */
 939
 940	len = min(sectors, blocksize) * (pagesize + 64);
 941	buffer = kmalloc(len, GFP_NOIO);
 942	if (!buffer)
 943		return USB_STOR_TRANSPORT_ERROR;
 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			/*
 981			 * This is not really an error. It just means
 982			 * that the block has never been written.
 983			 * Instead of returning USB_STOR_TRANSPORT_ERROR
 984			 * it is better to return all zero data.
 985			 */
 986
 987			memset(buffer, 0, len);
 988		} else {
 989			usb_stor_dbg(us, "Read %d pages, from PBA %d (LBA %d) page %d\n",
 990				     pages, pba, lba, page);
 991
 992			result = alauda_read_block(us, pba, page, pages, buffer);
 993			if (result != USB_STOR_TRANSPORT_GOOD)
 994				break;
 995		}
 996
 997		/* Store the data in the transfer buffer */
 998		usb_stor_access_xfer_buf(buffer, len, us->srb,
 999				&sg, &offset, TO_XFER_BUF);
1000
1001		page = 0;
1002		lba++;
1003		sectors -= pages;
1004	}
1005
1006	kfree(buffer);
1007	return result;
1008}
1009
1010/*
1011 * Write data to a specific sector address
1012 */
1013static int alauda_write_data(struct us_data *us, unsigned long address,
1014		unsigned int sectors)
1015{
1016	unsigned char *buffer, *blockbuffer;
1017	unsigned int page, len, offset;
1018	unsigned int blockshift = MEDIA_INFO(us).blockshift;
1019	unsigned int pageshift = MEDIA_INFO(us).pageshift;
1020	unsigned int blocksize = MEDIA_INFO(us).blocksize;
1021	unsigned int pagesize = MEDIA_INFO(us).pagesize;
1022	struct scatterlist *sg;
1023	u16 lba, max_lba;
1024	int result;
1025
1026	/*
1027	 * Since we don't write the user data directly to the device,
1028	 * we have to create a bounce buffer and move the data a piece
1029	 * at a time between the bounce buffer and the actual transfer buffer.
1030	 */
1031
1032	len = min(sectors, blocksize) * pagesize;
1033	buffer = kmalloc(len, GFP_NOIO);
1034	if (!buffer)
1035		return USB_STOR_TRANSPORT_ERROR;
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) {
1043		kfree(buffer);
1044		return USB_STOR_TRANSPORT_ERROR;
1045	}
1046
1047	/* Figure out the initial LBA and page */
1048	lba = address >> blockshift;
1049	page = (address & MEDIA_INFO(us).blockmask);
1050	max_lba = MEDIA_INFO(us).capacity >> (pageshift + blockshift);
1051
1052	result = USB_STOR_TRANSPORT_GOOD;
1053	offset = 0;
1054	sg = NULL;
1055
1056	while (sectors > 0) {
1057		/* Write as many sectors as possible in this block */
1058		unsigned int pages = min(sectors, blocksize - page);
1059		len = pages << pageshift;
1060
1061		/* Not overflowing capacity? */
1062		if (lba >= max_lba) {
1063			usb_stor_dbg(us, "Requested lba %u exceeds maximum %u\n",
1064				     lba, max_lba);
1065			result = USB_STOR_TRANSPORT_ERROR;
1066			break;
1067		}
1068
1069		/* Get the data from the transfer buffer */
1070		usb_stor_access_xfer_buf(buffer, len, us->srb,
1071				&sg, &offset, FROM_XFER_BUF);
1072
1073		result = alauda_write_lba(us, lba, page, pages, buffer,
1074			blockbuffer);
1075		if (result != USB_STOR_TRANSPORT_GOOD)
1076			break;
1077
1078		page = 0;
1079		lba++;
1080		sectors -= pages;
1081	}
1082
1083	kfree(buffer);
1084	kfree(blockbuffer);
1085	return result;
1086}
1087
1088/*
1089 * Our interface with the rest of the world
1090 */
1091
1092static void alauda_info_destructor(void *extra)
1093{
1094	struct alauda_info *info = (struct alauda_info *) extra;
1095	int port;
1096
1097	if (!info)
1098		return;
1099
1100	for (port = 0; port < 2; port++) {
1101		struct alauda_media_info *media_info = &info->port[port];
1102
1103		alauda_free_maps(media_info);
1104		kfree(media_info->lba_to_pba);
1105		kfree(media_info->pba_to_lba);
1106	}
1107}
1108
1109/*
1110 * Initialize alauda_info struct and find the data-write endpoint
1111 */
1112static int init_alauda(struct us_data *us)
1113{
1114	struct alauda_info *info;
1115	struct usb_host_interface *altsetting = us->pusb_intf->cur_altsetting;
1116	nand_init_ecc();
1117
1118	us->extra = kzalloc(sizeof(struct alauda_info), GFP_NOIO);
1119	if (!us->extra)
1120		return USB_STOR_TRANSPORT_ERROR;
1121
1122	info = (struct alauda_info *) us->extra;
1123	us->extra_destructor = alauda_info_destructor;
1124
1125	info->wr_ep = usb_sndbulkpipe(us->pusb_dev,
1126		altsetting->endpoint[0].desc.bEndpointAddress
1127		& USB_ENDPOINT_NUMBER_MASK);
1128
1129	return USB_STOR_TRANSPORT_GOOD;
1130}
1131
1132static int alauda_transport(struct scsi_cmnd *srb, struct us_data *us)
1133{
1134	int rc;
1135	struct alauda_info *info = (struct alauda_info *) us->extra;
1136	unsigned char *ptr = us->iobuf;
1137	static unsigned char inquiry_response[36] = {
1138		0x00, 0x80, 0x00, 0x01, 0x1F, 0x00, 0x00, 0x00
1139	};
1140
1141	if (srb->cmnd[0] == INQUIRY) {
1142		usb_stor_dbg(us, "INQUIRY - Returning bogus response\n");
1143		memcpy(ptr, inquiry_response, sizeof(inquiry_response));
1144		fill_inquiry_response(us, ptr, 36);
1145		return USB_STOR_TRANSPORT_GOOD;
1146	}
1147
1148	if (srb->cmnd[0] == TEST_UNIT_READY) {
1149		usb_stor_dbg(us, "TEST_UNIT_READY\n");
1150		return alauda_check_media(us);
1151	}
1152
1153	if (srb->cmnd[0] == READ_CAPACITY) {
1154		unsigned int num_zones;
1155		unsigned long capacity;
1156
1157		rc = alauda_check_media(us);
1158		if (rc != USB_STOR_TRANSPORT_GOOD)
1159			return rc;
1160
1161		num_zones = MEDIA_INFO(us).capacity >> (MEDIA_INFO(us).zoneshift
1162			+ MEDIA_INFO(us).blockshift + MEDIA_INFO(us).pageshift);
1163
1164		capacity = num_zones * MEDIA_INFO(us).uzonesize
1165			* MEDIA_INFO(us).blocksize;
1166
1167		/* Report capacity and page size */
1168		((__be32 *) ptr)[0] = cpu_to_be32(capacity - 1);
1169		((__be32 *) ptr)[1] = cpu_to_be32(512);
1170
1171		usb_stor_set_xfer_buf(ptr, 8, srb);
1172		return USB_STOR_TRANSPORT_GOOD;
1173	}
1174
1175	if (srb->cmnd[0] == READ_10) {
1176		unsigned int page, pages;
1177
1178		rc = alauda_check_media(us);
1179		if (rc != USB_STOR_TRANSPORT_GOOD)
1180			return rc;
1181
1182		page = short_pack(srb->cmnd[3], srb->cmnd[2]);
1183		page <<= 16;
1184		page |= short_pack(srb->cmnd[5], srb->cmnd[4]);
1185		pages = short_pack(srb->cmnd[8], srb->cmnd[7]);
1186
1187		usb_stor_dbg(us, "READ_10: page %d pagect %d\n", page, pages);
1188
1189		return alauda_read_data(us, page, pages);
1190	}
1191
1192	if (srb->cmnd[0] == WRITE_10) {
1193		unsigned int page, pages;
1194
1195		rc = alauda_check_media(us);
1196		if (rc != USB_STOR_TRANSPORT_GOOD)
1197			return rc;
1198
1199		page = short_pack(srb->cmnd[3], srb->cmnd[2]);
1200		page <<= 16;
1201		page |= short_pack(srb->cmnd[5], srb->cmnd[4]);
1202		pages = short_pack(srb->cmnd[8], srb->cmnd[7]);
1203
1204		usb_stor_dbg(us, "WRITE_10: page %d pagect %d\n", page, pages);
1205
1206		return alauda_write_data(us, page, pages);
1207	}
1208
1209	if (srb->cmnd[0] == REQUEST_SENSE) {
1210		usb_stor_dbg(us, "REQUEST_SENSE\n");
1211
1212		memset(ptr, 0, 18);
1213		ptr[0] = 0xF0;
1214		ptr[2] = info->sense_key;
1215		ptr[7] = 11;
1216		ptr[12] = info->sense_asc;
1217		ptr[13] = info->sense_ascq;
1218		usb_stor_set_xfer_buf(ptr, 18, srb);
1219
1220		return USB_STOR_TRANSPORT_GOOD;
1221	}
1222
1223	if (srb->cmnd[0] == ALLOW_MEDIUM_REMOVAL) {
1224		/*
1225		 * sure.  whatever.  not like we can stop the user from popping
1226		 * the media out of the device (no locking doors, etc)
1227		 */
1228		return USB_STOR_TRANSPORT_GOOD;
1229	}
1230
1231	usb_stor_dbg(us, "Gah! Unknown command: %d (0x%x)\n",
1232		     srb->cmnd[0], srb->cmnd[0]);
1233	info->sense_key = 0x05;
1234	info->sense_asc = 0x20;
1235	info->sense_ascq = 0x00;
1236	return USB_STOR_TRANSPORT_FAILED;
1237}
1238
1239static struct scsi_host_template alauda_host_template;
1240
1241static int alauda_probe(struct usb_interface *intf,
1242			 const struct usb_device_id *id)
1243{
1244	struct us_data *us;
1245	int result;
1246
1247	result = usb_stor_probe1(&us, intf, id,
1248			(id - alauda_usb_ids) + alauda_unusual_dev_list,
1249			&alauda_host_template);
1250	if (result)
1251		return result;
1252
1253	us->transport_name  = "Alauda Control/Bulk";
1254	us->transport = alauda_transport;
1255	us->transport_reset = usb_stor_Bulk_reset;
1256	us->max_lun = 1;
1257
1258	result = usb_stor_probe2(us);
1259	return result;
1260}
1261
1262static struct usb_driver alauda_driver = {
1263	.name =		DRV_NAME,
1264	.probe =	alauda_probe,
1265	.disconnect =	usb_stor_disconnect,
1266	.suspend =	usb_stor_suspend,
1267	.resume =	usb_stor_resume,
1268	.reset_resume =	usb_stor_reset_resume,
1269	.pre_reset =	usb_stor_pre_reset,
1270	.post_reset =	usb_stor_post_reset,
1271	.id_table =	alauda_usb_ids,
1272	.soft_unbind =	1,
1273	.no_dynamic_id = 1,
1274};
1275
1276module_usb_stor_driver(alauda_driver, alauda_host_template, DRV_NAME);
v5.4
   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	int rc;
 457
 458	rc = alauda_get_media_status(us, status);
 459
 460	/* Check for no media or door open */
 461	if ((status[0] & 0x80) || ((status[0] & 0x1F) == 0x10)
 462		|| ((status[1] & 0x01) == 0)) {
 463		usb_stor_dbg(us, "No media, or door open\n");
 464		alauda_free_maps(&MEDIA_INFO(us));
 465		info->sense_key = 0x02;
 466		info->sense_asc = 0x3A;
 467		info->sense_ascq = 0x00;
 468		return USB_STOR_TRANSPORT_FAILED;
 469	}
 470
 471	/* Check for media change */
 472	if (status[0] & 0x08) {
 473		usb_stor_dbg(us, "Media change detected\n");
 474		alauda_free_maps(&MEDIA_INFO(us));
 475		alauda_init_media(us);
 476
 477		info->sense_key = UNIT_ATTENTION;
 478		info->sense_asc = 0x28;
 479		info->sense_ascq = 0x00;
 480		return USB_STOR_TRANSPORT_FAILED;
 481	}
 482
 483	return USB_STOR_TRANSPORT_GOOD;
 484}
 485
 486/*
 487 * Checks the status from the 2nd status register
 488 * Returns 3 bytes of status data, only the first is known
 489 */
 490static int alauda_check_status2(struct us_data *us)
 491{
 492	int rc;
 493	unsigned char command[] = {
 494		ALAUDA_BULK_CMD, ALAUDA_BULK_GET_STATUS2,
 495		0, 0, 0, 0, 3, 0, MEDIA_PORT(us)
 496	};
 497	unsigned char data[3];
 498
 499	rc = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
 500		command, 9, NULL);
 501	if (rc != USB_STOR_XFER_GOOD)
 502		return rc;
 503
 504	rc = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
 505		data, 3, NULL);
 506	if (rc != USB_STOR_XFER_GOOD)
 507		return rc;
 508
 509	usb_stor_dbg(us, "%3ph\n", data);
 510	if (data[0] & ALAUDA_STATUS_ERROR)
 511		return USB_STOR_XFER_ERROR;
 512
 513	return USB_STOR_XFER_GOOD;
 514}
 515
 516/*
 517 * Gets the redundancy data for the first page of a PBA
 518 * Returns 16 bytes.
 519 */
 520static int alauda_get_redu_data(struct us_data *us, u16 pba, unsigned char *data)
 521{
 522	int rc;
 523	unsigned char command[] = {
 524		ALAUDA_BULK_CMD, ALAUDA_BULK_GET_REDU_DATA,
 525		PBA_HI(pba), PBA_ZONE(pba), 0, PBA_LO(pba), 0, 0, MEDIA_PORT(us)
 526	};
 527
 528	rc = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
 529		command, 9, NULL);
 530	if (rc != USB_STOR_XFER_GOOD)
 531		return rc;
 532
 533	return usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
 534		data, 16, NULL);
 535}
 536
 537/*
 538 * Finds the first unused PBA in a zone
 539 * Returns the absolute PBA of an unused PBA, or 0 if none found.
 540 */
 541static u16 alauda_find_unused_pba(struct alauda_media_info *info,
 542	unsigned int zone)
 543{
 544	u16 *pba_to_lba = info->pba_to_lba[zone];
 545	unsigned int i;
 546
 547	for (i = 0; i < info->zonesize; i++)
 548		if (pba_to_lba[i] == UNDEF)
 549			return (zone << info->zoneshift) + i;
 550
 551	return 0;
 552}
 553
 554/*
 555 * Reads the redundancy data for all PBA's in a zone
 556 * Produces lba <--> pba mappings
 557 */
 558static int alauda_read_map(struct us_data *us, unsigned int zone)
 559{
 560	unsigned char *data = us->iobuf;
 561	int result;
 562	int i, j;
 563	unsigned int zonesize = MEDIA_INFO(us).zonesize;
 564	unsigned int uzonesize = MEDIA_INFO(us).uzonesize;
 565	unsigned int lba_offset, lba_real, blocknum;
 566	unsigned int zone_base_lba = zone * uzonesize;
 567	unsigned int zone_base_pba = zone * zonesize;
 568	u16 *lba_to_pba = kcalloc(zonesize, sizeof(u16), GFP_NOIO);
 569	u16 *pba_to_lba = kcalloc(zonesize, sizeof(u16), GFP_NOIO);
 570	if (lba_to_pba == NULL || pba_to_lba == NULL) {
 571		result = USB_STOR_TRANSPORT_ERROR;
 572		goto error;
 573	}
 574
 575	usb_stor_dbg(us, "Mapping blocks for zone %d\n", zone);
 576
 577	/* 1024 PBA's per zone */
 578	for (i = 0; i < zonesize; i++)
 579		lba_to_pba[i] = pba_to_lba[i] = UNDEF;
 580
 581	for (i = 0; i < zonesize; i++) {
 582		blocknum = zone_base_pba + i;
 583
 584		result = alauda_get_redu_data(us, blocknum, data);
 585		if (result != USB_STOR_XFER_GOOD) {
 586			result = USB_STOR_TRANSPORT_ERROR;
 587			goto error;
 588		}
 589
 590		/* special PBAs have control field 0^16 */
 591		for (j = 0; j < 16; j++)
 592			if (data[j] != 0)
 593				goto nonz;
 594		pba_to_lba[i] = UNUSABLE;
 595		usb_stor_dbg(us, "PBA %d has no logical mapping\n", blocknum);
 596		continue;
 597
 598	nonz:
 599		/* unwritten PBAs have control field FF^16 */
 600		for (j = 0; j < 16; j++)
 601			if (data[j] != 0xff)
 602				goto nonff;
 603		continue;
 604
 605	nonff:
 606		/* normal PBAs start with six FFs */
 607		if (j < 6) {
 608			usb_stor_dbg(us, "PBA %d has no logical mapping: reserved area = %02X%02X%02X%02X data status %02X block status %02X\n",
 609				     blocknum,
 610				     data[0], data[1], data[2], data[3],
 611				     data[4], data[5]);
 612			pba_to_lba[i] = UNUSABLE;
 613			continue;
 614		}
 615
 616		if ((data[6] >> 4) != 0x01) {
 617			usb_stor_dbg(us, "PBA %d has invalid address field %02X%02X/%02X%02X\n",
 618				     blocknum, data[6], data[7],
 619				     data[11], data[12]);
 620			pba_to_lba[i] = UNUSABLE;
 621			continue;
 622		}
 623
 624		/* check even parity */
 625		if (parity[data[6] ^ data[7]]) {
 626			printk(KERN_WARNING
 627			       "alauda_read_map: Bad parity in LBA for block %d"
 628			       " (%02X %02X)\n", i, data[6], data[7]);
 629			pba_to_lba[i] = UNUSABLE;
 630			continue;
 631		}
 632
 633		lba_offset = short_pack(data[7], data[6]);
 634		lba_offset = (lba_offset & 0x07FF) >> 1;
 635		lba_real = lba_offset + zone_base_lba;
 636
 637		/*
 638		 * Every 1024 physical blocks ("zone"), the LBA numbers
 639		 * go back to zero, but are within a higher block of LBA's.
 640		 * Also, there is a maximum of 1000 LBA's per zone.
 641		 * In other words, in PBA 1024-2047 you will find LBA 0-999
 642		 * which are really LBA 1000-1999. This allows for 24 bad
 643		 * or special physical blocks per zone.
 644		 */
 645
 646		if (lba_offset >= uzonesize) {
 647			printk(KERN_WARNING
 648			       "alauda_read_map: Bad low LBA %d for block %d\n",
 649			       lba_real, blocknum);
 650			continue;
 651		}
 652
 653		if (lba_to_pba[lba_offset] != UNDEF) {
 654			printk(KERN_WARNING
 655			       "alauda_read_map: "
 656			       "LBA %d seen for PBA %d and %d\n",
 657			       lba_real, lba_to_pba[lba_offset], blocknum);
 658			continue;
 659		}
 660
 661		pba_to_lba[i] = lba_real;
 662		lba_to_pba[lba_offset] = blocknum;
 663		continue;
 664	}
 665
 666	MEDIA_INFO(us).lba_to_pba[zone] = lba_to_pba;
 667	MEDIA_INFO(us).pba_to_lba[zone] = pba_to_lba;
 668	result = 0;
 669	goto out;
 670
 671error:
 672	kfree(lba_to_pba);
 673	kfree(pba_to_lba);
 674out:
 675	return result;
 676}
 677
 678/*
 679 * Checks to see whether we have already mapped a certain zone
 680 * If we haven't, the map is generated
 681 */
 682static void alauda_ensure_map_for_zone(struct us_data *us, unsigned int zone)
 683{
 684	if (MEDIA_INFO(us).lba_to_pba[zone] == NULL
 685		|| MEDIA_INFO(us).pba_to_lba[zone] == NULL)
 686		alauda_read_map(us, zone);
 687}
 688
 689/*
 690 * Erases an entire block
 691 */
 692static int alauda_erase_block(struct us_data *us, u16 pba)
 693{
 694	int rc;
 695	unsigned char command[] = {
 696		ALAUDA_BULK_CMD, ALAUDA_BULK_ERASE_BLOCK, PBA_HI(pba),
 697		PBA_ZONE(pba), 0, PBA_LO(pba), 0x02, 0, MEDIA_PORT(us)
 698	};
 699	unsigned char buf[2];
 700
 701	usb_stor_dbg(us, "Erasing PBA %d\n", pba);
 702
 703	rc = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
 704		command, 9, NULL);
 705	if (rc != USB_STOR_XFER_GOOD)
 706		return rc;
 707
 708	rc = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
 709		buf, 2, NULL);
 710	if (rc != USB_STOR_XFER_GOOD)
 711		return rc;
 712
 713	usb_stor_dbg(us, "Erase result: %02X %02X\n", buf[0], buf[1]);
 714	return rc;
 715}
 716
 717/*
 718 * Reads data from a certain offset page inside a PBA, including interleaved
 719 * redundancy data. Returns (pagesize+64)*pages bytes in data.
 720 */
 721static int alauda_read_block_raw(struct us_data *us, u16 pba,
 722		unsigned int page, unsigned int pages, unsigned char *data)
 723{
 724	int rc;
 725	unsigned char command[] = {
 726		ALAUDA_BULK_CMD, ALAUDA_BULK_READ_BLOCK, PBA_HI(pba),
 727		PBA_ZONE(pba), 0, PBA_LO(pba) + page, pages, 0, MEDIA_PORT(us)
 728	};
 729
 730	usb_stor_dbg(us, "pba %d page %d count %d\n", pba, page, pages);
 731
 732	rc = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
 733		command, 9, NULL);
 734	if (rc != USB_STOR_XFER_GOOD)
 735		return rc;
 736
 737	return usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
 738		data, (MEDIA_INFO(us).pagesize + 64) * pages, NULL);
 739}
 740
 741/*
 742 * Reads data from a certain offset page inside a PBA, excluding redundancy
 743 * data. Returns pagesize*pages bytes in data. Note that data must be big enough
 744 * to hold (pagesize+64)*pages bytes of data, but you can ignore those 'extra'
 745 * trailing bytes outside this function.
 746 */
 747static int alauda_read_block(struct us_data *us, u16 pba,
 748		unsigned int page, unsigned int pages, unsigned char *data)
 749{
 750	int i, rc;
 751	unsigned int pagesize = MEDIA_INFO(us).pagesize;
 752
 753	rc = alauda_read_block_raw(us, pba, page, pages, data);
 754	if (rc != USB_STOR_XFER_GOOD)
 755		return rc;
 756
 757	/* Cut out the redundancy data */
 758	for (i = 0; i < pages; i++) {
 759		int dest_offset = i * pagesize;
 760		int src_offset = i * (pagesize + 64);
 761		memmove(data + dest_offset, data + src_offset, pagesize);
 762	}
 763
 764	return rc;
 765}
 766
 767/*
 768 * Writes an entire block of data and checks status after write.
 769 * Redundancy data must be already included in data. Data should be
 770 * (pagesize+64)*blocksize bytes in length.
 771 */
 772static int alauda_write_block(struct us_data *us, u16 pba, unsigned char *data)
 773{
 774	int rc;
 775	struct alauda_info *info = (struct alauda_info *) us->extra;
 776	unsigned char command[] = {
 777		ALAUDA_BULK_CMD, ALAUDA_BULK_WRITE_BLOCK, PBA_HI(pba),
 778		PBA_ZONE(pba), 0, PBA_LO(pba), 32, 0, MEDIA_PORT(us)
 779	};
 780
 781	usb_stor_dbg(us, "pba %d\n", pba);
 782
 783	rc = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
 784		command, 9, NULL);
 785	if (rc != USB_STOR_XFER_GOOD)
 786		return rc;
 787
 788	rc = usb_stor_bulk_transfer_buf(us, info->wr_ep, data,
 789		(MEDIA_INFO(us).pagesize + 64) * MEDIA_INFO(us).blocksize,
 790		NULL);
 791	if (rc != USB_STOR_XFER_GOOD)
 792		return rc;
 793
 794	return alauda_check_status2(us);
 795}
 796
 797/*
 798 * Write some data to a specific LBA.
 799 */
 800static int alauda_write_lba(struct us_data *us, u16 lba,
 801		 unsigned int page, unsigned int pages,
 802		 unsigned char *ptr, unsigned char *blockbuffer)
 803{
 804	u16 pba, lbap, new_pba;
 805	unsigned char *bptr, *cptr, *xptr;
 806	unsigned char ecc[3];
 807	int i, result;
 808	unsigned int uzonesize = MEDIA_INFO(us).uzonesize;
 809	unsigned int zonesize = MEDIA_INFO(us).zonesize;
 810	unsigned int pagesize = MEDIA_INFO(us).pagesize;
 811	unsigned int blocksize = MEDIA_INFO(us).blocksize;
 812	unsigned int lba_offset = lba % uzonesize;
 813	unsigned int new_pba_offset;
 814	unsigned int zone = lba / uzonesize;
 815
 816	alauda_ensure_map_for_zone(us, zone);
 817
 818	pba = MEDIA_INFO(us).lba_to_pba[zone][lba_offset];
 819	if (pba == 1) {
 820		/*
 821		 * Maybe it is impossible to write to PBA 1.
 822		 * Fake success, but don't do anything.
 823		 */
 824		printk(KERN_WARNING
 825		       "alauda_write_lba: avoid writing to pba 1\n");
 826		return USB_STOR_TRANSPORT_GOOD;
 827	}
 828
 829	new_pba = alauda_find_unused_pba(&MEDIA_INFO(us), zone);
 830	if (!new_pba) {
 831		printk(KERN_WARNING
 832		       "alauda_write_lba: Out of unused blocks\n");
 833		return USB_STOR_TRANSPORT_ERROR;
 834	}
 835
 836	/* read old contents */
 837	if (pba != UNDEF) {
 838		result = alauda_read_block_raw(us, pba, 0,
 839			blocksize, blockbuffer);
 840		if (result != USB_STOR_XFER_GOOD)
 841			return result;
 842	} else {
 843		memset(blockbuffer, 0, blocksize * (pagesize + 64));
 844	}
 845
 846	lbap = (lba_offset << 1) | 0x1000;
 847	if (parity[MSB_of(lbap) ^ LSB_of(lbap)])
 848		lbap ^= 1;
 849
 850	/* check old contents and fill lba */
 851	for (i = 0; i < blocksize; i++) {
 852		bptr = blockbuffer + (i * (pagesize + 64));
 853		cptr = bptr + pagesize;
 854		nand_compute_ecc(bptr, ecc);
 855		if (!nand_compare_ecc(cptr+13, ecc)) {
 856			usb_stor_dbg(us, "Warning: bad ecc in page %d- of pba %d\n",
 857				     i, pba);
 858			nand_store_ecc(cptr+13, ecc);
 859		}
 860		nand_compute_ecc(bptr + (pagesize / 2), ecc);
 861		if (!nand_compare_ecc(cptr+8, ecc)) {
 862			usb_stor_dbg(us, "Warning: bad ecc in page %d+ of pba %d\n",
 863				     i, pba);
 864			nand_store_ecc(cptr+8, ecc);
 865		}
 866		cptr[6] = cptr[11] = MSB_of(lbap);
 867		cptr[7] = cptr[12] = LSB_of(lbap);
 868	}
 869
 870	/* copy in new stuff and compute ECC */
 871	xptr = ptr;
 872	for (i = page; i < page+pages; i++) {
 873		bptr = blockbuffer + (i * (pagesize + 64));
 874		cptr = bptr + pagesize;
 875		memcpy(bptr, xptr, pagesize);
 876		xptr += pagesize;
 877		nand_compute_ecc(bptr, ecc);
 878		nand_store_ecc(cptr+13, ecc);
 879		nand_compute_ecc(bptr + (pagesize / 2), ecc);
 880		nand_store_ecc(cptr+8, ecc);
 881	}
 882
 883	result = alauda_write_block(us, new_pba, blockbuffer);
 884	if (result != USB_STOR_XFER_GOOD)
 885		return result;
 886
 887	new_pba_offset = new_pba - (zone * zonesize);
 888	MEDIA_INFO(us).pba_to_lba[zone][new_pba_offset] = lba;
 889	MEDIA_INFO(us).lba_to_pba[zone][lba_offset] = new_pba;
 890	usb_stor_dbg(us, "Remapped LBA %d to PBA %d\n", lba, new_pba);
 891
 892	if (pba != UNDEF) {
 893		unsigned int pba_offset = pba - (zone * zonesize);
 894		result = alauda_erase_block(us, pba);
 895		if (result != USB_STOR_XFER_GOOD)
 896			return result;
 897		MEDIA_INFO(us).pba_to_lba[zone][pba_offset] = UNDEF;
 898	}
 899
 900	return USB_STOR_TRANSPORT_GOOD;
 901}
 902
 903/*
 904 * Read data from a specific sector address
 905 */
 906static int alauda_read_data(struct us_data *us, unsigned long address,
 907		unsigned int sectors)
 908{
 909	unsigned char *buffer;
 910	u16 lba, max_lba;
 911	unsigned int page, len, offset;
 912	unsigned int blockshift = MEDIA_INFO(us).blockshift;
 913	unsigned int pageshift = MEDIA_INFO(us).pageshift;
 914	unsigned int blocksize = MEDIA_INFO(us).blocksize;
 915	unsigned int pagesize = MEDIA_INFO(us).pagesize;
 916	unsigned int uzonesize = MEDIA_INFO(us).uzonesize;
 917	struct scatterlist *sg;
 918	int result;
 919
 920	/*
 921	 * Since we only read in one block at a time, we have to create
 922	 * a bounce buffer and move the data a piece at a time between the
 923	 * bounce buffer and the actual transfer buffer.
 924	 * We make this buffer big enough to hold temporary redundancy data,
 925	 * which we use when reading the data blocks.
 926	 */
 927
 928	len = min(sectors, blocksize) * (pagesize + 64);
 929	buffer = kmalloc(len, GFP_NOIO);
 930	if (!buffer)
 931		return USB_STOR_TRANSPORT_ERROR;
 932
 933	/* Figure out the initial LBA and page */
 934	lba = address >> blockshift;
 935	page = (address & MEDIA_INFO(us).blockmask);
 936	max_lba = MEDIA_INFO(us).capacity >> (blockshift + pageshift);
 937
 938	result = USB_STOR_TRANSPORT_GOOD;
 939	offset = 0;
 940	sg = NULL;
 941
 942	while (sectors > 0) {
 943		unsigned int zone = lba / uzonesize; /* integer division */
 944		unsigned int lba_offset = lba - (zone * uzonesize);
 945		unsigned int pages;
 946		u16 pba;
 947		alauda_ensure_map_for_zone(us, zone);
 948
 949		/* Not overflowing capacity? */
 950		if (lba >= max_lba) {
 951			usb_stor_dbg(us, "Error: Requested lba %u exceeds maximum %u\n",
 952				     lba, max_lba);
 953			result = USB_STOR_TRANSPORT_ERROR;
 954			break;
 955		}
 956
 957		/* Find number of pages we can read in this block */
 958		pages = min(sectors, blocksize - page);
 959		len = pages << pageshift;
 960
 961		/* Find where this lba lives on disk */
 962		pba = MEDIA_INFO(us).lba_to_pba[zone][lba_offset];
 963
 964		if (pba == UNDEF) {	/* this lba was never written */
 965			usb_stor_dbg(us, "Read %d zero pages (LBA %d) page %d\n",
 966				     pages, lba, page);
 967
 968			/*
 969			 * This is not really an error. It just means
 970			 * that the block has never been written.
 971			 * Instead of returning USB_STOR_TRANSPORT_ERROR
 972			 * it is better to return all zero data.
 973			 */
 974
 975			memset(buffer, 0, len);
 976		} else {
 977			usb_stor_dbg(us, "Read %d pages, from PBA %d (LBA %d) page %d\n",
 978				     pages, pba, lba, page);
 979
 980			result = alauda_read_block(us, pba, page, pages, buffer);
 981			if (result != USB_STOR_TRANSPORT_GOOD)
 982				break;
 983		}
 984
 985		/* Store the data in the transfer buffer */
 986		usb_stor_access_xfer_buf(buffer, len, us->srb,
 987				&sg, &offset, TO_XFER_BUF);
 988
 989		page = 0;
 990		lba++;
 991		sectors -= pages;
 992	}
 993
 994	kfree(buffer);
 995	return result;
 996}
 997
 998/*
 999 * Write data to a specific sector address
1000 */
1001static int alauda_write_data(struct us_data *us, unsigned long address,
1002		unsigned int sectors)
1003{
1004	unsigned char *buffer, *blockbuffer;
1005	unsigned int page, len, offset;
1006	unsigned int blockshift = MEDIA_INFO(us).blockshift;
1007	unsigned int pageshift = MEDIA_INFO(us).pageshift;
1008	unsigned int blocksize = MEDIA_INFO(us).blocksize;
1009	unsigned int pagesize = MEDIA_INFO(us).pagesize;
1010	struct scatterlist *sg;
1011	u16 lba, max_lba;
1012	int result;
1013
1014	/*
1015	 * Since we don't write the user data directly to the device,
1016	 * we have to create a bounce buffer and move the data a piece
1017	 * at a time between the bounce buffer and the actual transfer buffer.
1018	 */
1019
1020	len = min(sectors, blocksize) * pagesize;
1021	buffer = kmalloc(len, GFP_NOIO);
1022	if (!buffer)
1023		return USB_STOR_TRANSPORT_ERROR;
1024
1025	/*
1026	 * We also need a temporary block buffer, where we read in the old data,
1027	 * overwrite parts with the new data, and manipulate the redundancy data
1028	 */
1029	blockbuffer = kmalloc_array(pagesize + 64, blocksize, GFP_NOIO);
1030	if (!blockbuffer) {
1031		kfree(buffer);
1032		return USB_STOR_TRANSPORT_ERROR;
1033	}
1034
1035	/* Figure out the initial LBA and page */
1036	lba = address >> blockshift;
1037	page = (address & MEDIA_INFO(us).blockmask);
1038	max_lba = MEDIA_INFO(us).capacity >> (pageshift + blockshift);
1039
1040	result = USB_STOR_TRANSPORT_GOOD;
1041	offset = 0;
1042	sg = NULL;
1043
1044	while (sectors > 0) {
1045		/* Write as many sectors as possible in this block */
1046		unsigned int pages = min(sectors, blocksize - page);
1047		len = pages << pageshift;
1048
1049		/* Not overflowing capacity? */
1050		if (lba >= max_lba) {
1051			usb_stor_dbg(us, "Requested lba %u exceeds maximum %u\n",
1052				     lba, max_lba);
1053			result = USB_STOR_TRANSPORT_ERROR;
1054			break;
1055		}
1056
1057		/* Get the data from the transfer buffer */
1058		usb_stor_access_xfer_buf(buffer, len, us->srb,
1059				&sg, &offset, FROM_XFER_BUF);
1060
1061		result = alauda_write_lba(us, lba, page, pages, buffer,
1062			blockbuffer);
1063		if (result != USB_STOR_TRANSPORT_GOOD)
1064			break;
1065
1066		page = 0;
1067		lba++;
1068		sectors -= pages;
1069	}
1070
1071	kfree(buffer);
1072	kfree(blockbuffer);
1073	return result;
1074}
1075
1076/*
1077 * Our interface with the rest of the world
1078 */
1079
1080static void alauda_info_destructor(void *extra)
1081{
1082	struct alauda_info *info = (struct alauda_info *) extra;
1083	int port;
1084
1085	if (!info)
1086		return;
1087
1088	for (port = 0; port < 2; port++) {
1089		struct alauda_media_info *media_info = &info->port[port];
1090
1091		alauda_free_maps(media_info);
1092		kfree(media_info->lba_to_pba);
1093		kfree(media_info->pba_to_lba);
1094	}
1095}
1096
1097/*
1098 * Initialize alauda_info struct and find the data-write endpoint
1099 */
1100static int init_alauda(struct us_data *us)
1101{
1102	struct alauda_info *info;
1103	struct usb_host_interface *altsetting = us->pusb_intf->cur_altsetting;
1104	nand_init_ecc();
1105
1106	us->extra = kzalloc(sizeof(struct alauda_info), GFP_NOIO);
1107	if (!us->extra)
1108		return USB_STOR_TRANSPORT_ERROR;
1109
1110	info = (struct alauda_info *) us->extra;
1111	us->extra_destructor = alauda_info_destructor;
1112
1113	info->wr_ep = usb_sndbulkpipe(us->pusb_dev,
1114		altsetting->endpoint[0].desc.bEndpointAddress
1115		& USB_ENDPOINT_NUMBER_MASK);
1116
1117	return USB_STOR_TRANSPORT_GOOD;
1118}
1119
1120static int alauda_transport(struct scsi_cmnd *srb, struct us_data *us)
1121{
1122	int rc;
1123	struct alauda_info *info = (struct alauda_info *) us->extra;
1124	unsigned char *ptr = us->iobuf;
1125	static unsigned char inquiry_response[36] = {
1126		0x00, 0x80, 0x00, 0x01, 0x1F, 0x00, 0x00, 0x00
1127	};
1128
1129	if (srb->cmnd[0] == INQUIRY) {
1130		usb_stor_dbg(us, "INQUIRY - Returning bogus response\n");
1131		memcpy(ptr, inquiry_response, sizeof(inquiry_response));
1132		fill_inquiry_response(us, ptr, 36);
1133		return USB_STOR_TRANSPORT_GOOD;
1134	}
1135
1136	if (srb->cmnd[0] == TEST_UNIT_READY) {
1137		usb_stor_dbg(us, "TEST_UNIT_READY\n");
1138		return alauda_check_media(us);
1139	}
1140
1141	if (srb->cmnd[0] == READ_CAPACITY) {
1142		unsigned int num_zones;
1143		unsigned long capacity;
1144
1145		rc = alauda_check_media(us);
1146		if (rc != USB_STOR_TRANSPORT_GOOD)
1147			return rc;
1148
1149		num_zones = MEDIA_INFO(us).capacity >> (MEDIA_INFO(us).zoneshift
1150			+ MEDIA_INFO(us).blockshift + MEDIA_INFO(us).pageshift);
1151
1152		capacity = num_zones * MEDIA_INFO(us).uzonesize
1153			* MEDIA_INFO(us).blocksize;
1154
1155		/* Report capacity and page size */
1156		((__be32 *) ptr)[0] = cpu_to_be32(capacity - 1);
1157		((__be32 *) ptr)[1] = cpu_to_be32(512);
1158
1159		usb_stor_set_xfer_buf(ptr, 8, srb);
1160		return USB_STOR_TRANSPORT_GOOD;
1161	}
1162
1163	if (srb->cmnd[0] == READ_10) {
1164		unsigned int page, pages;
1165
1166		rc = alauda_check_media(us);
1167		if (rc != USB_STOR_TRANSPORT_GOOD)
1168			return rc;
1169
1170		page = short_pack(srb->cmnd[3], srb->cmnd[2]);
1171		page <<= 16;
1172		page |= short_pack(srb->cmnd[5], srb->cmnd[4]);
1173		pages = short_pack(srb->cmnd[8], srb->cmnd[7]);
1174
1175		usb_stor_dbg(us, "READ_10: page %d pagect %d\n", page, pages);
1176
1177		return alauda_read_data(us, page, pages);
1178	}
1179
1180	if (srb->cmnd[0] == WRITE_10) {
1181		unsigned int page, pages;
1182
1183		rc = alauda_check_media(us);
1184		if (rc != USB_STOR_TRANSPORT_GOOD)
1185			return rc;
1186
1187		page = short_pack(srb->cmnd[3], srb->cmnd[2]);
1188		page <<= 16;
1189		page |= short_pack(srb->cmnd[5], srb->cmnd[4]);
1190		pages = short_pack(srb->cmnd[8], srb->cmnd[7]);
1191
1192		usb_stor_dbg(us, "WRITE_10: page %d pagect %d\n", page, pages);
1193
1194		return alauda_write_data(us, page, pages);
1195	}
1196
1197	if (srb->cmnd[0] == REQUEST_SENSE) {
1198		usb_stor_dbg(us, "REQUEST_SENSE\n");
1199
1200		memset(ptr, 0, 18);
1201		ptr[0] = 0xF0;
1202		ptr[2] = info->sense_key;
1203		ptr[7] = 11;
1204		ptr[12] = info->sense_asc;
1205		ptr[13] = info->sense_ascq;
1206		usb_stor_set_xfer_buf(ptr, 18, srb);
1207
1208		return USB_STOR_TRANSPORT_GOOD;
1209	}
1210
1211	if (srb->cmnd[0] == ALLOW_MEDIUM_REMOVAL) {
1212		/*
1213		 * sure.  whatever.  not like we can stop the user from popping
1214		 * the media out of the device (no locking doors, etc)
1215		 */
1216		return USB_STOR_TRANSPORT_GOOD;
1217	}
1218
1219	usb_stor_dbg(us, "Gah! Unknown command: %d (0x%x)\n",
1220		     srb->cmnd[0], srb->cmnd[0]);
1221	info->sense_key = 0x05;
1222	info->sense_asc = 0x20;
1223	info->sense_ascq = 0x00;
1224	return USB_STOR_TRANSPORT_FAILED;
1225}
1226
1227static struct scsi_host_template alauda_host_template;
1228
1229static int alauda_probe(struct usb_interface *intf,
1230			 const struct usb_device_id *id)
1231{
1232	struct us_data *us;
1233	int result;
1234
1235	result = usb_stor_probe1(&us, intf, id,
1236			(id - alauda_usb_ids) + alauda_unusual_dev_list,
1237			&alauda_host_template);
1238	if (result)
1239		return result;
1240
1241	us->transport_name  = "Alauda Control/Bulk";
1242	us->transport = alauda_transport;
1243	us->transport_reset = usb_stor_Bulk_reset;
1244	us->max_lun = 1;
1245
1246	result = usb_stor_probe2(us);
1247	return result;
1248}
1249
1250static struct usb_driver alauda_driver = {
1251	.name =		DRV_NAME,
1252	.probe =	alauda_probe,
1253	.disconnect =	usb_stor_disconnect,
1254	.suspend =	usb_stor_suspend,
1255	.resume =	usb_stor_resume,
1256	.reset_resume =	usb_stor_reset_resume,
1257	.pre_reset =	usb_stor_pre_reset,
1258	.post_reset =	usb_stor_post_reset,
1259	.id_table =	alauda_usb_ids,
1260	.soft_unbind =	1,
1261	.no_dynamic_id = 1,
1262};
1263
1264module_usb_stor_driver(alauda_driver, alauda_host_template, DRV_NAME);